1 /*
2 * Copyright © 2014 Connor Abbott
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott ([email protected])
25 *
26 */
27
28 #ifndef NIR_H
29 #define NIR_H
30
31 #include <stdint.h>
32 #include "compiler/glsl_types.h"
33 #include "compiler/glsl/list.h"
34 #include "compiler/shader_enums.h"
35 #include "compiler/shader_info.h"
36 #include "util/bitscan.h"
37 #include "util/bitset.h"
38 #include "util/compiler.h"
39 #include "util/enum_operators.h"
40 #include "util/format/u_format.h"
41 #include "util/hash_table.h"
42 #include "util/list.h"
43 #include "util/log.h"
44 #include "util/macros.h"
45 #include "util/ralloc.h"
46 #include "util/set.h"
47 #include "util/u_printf.h"
48 #define XXH_INLINE_ALL
49 #include <stdio.h>
50 #include "util/xxhash.h"
51
52 #ifndef NDEBUG
53 #include "util/u_debug.h"
54 #endif /* NDEBUG */
55
56 #include "nir_opcodes.h"
57
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62 extern uint32_t nir_debug;
63 extern bool nir_debug_print_shader[MESA_SHADER_KERNEL + 1];
64
65 #ifndef NDEBUG
66 #define NIR_DEBUG(flag) unlikely(nir_debug &(NIR_DEBUG_##flag))
67 #else
68 #define NIR_DEBUG(flag) false
69 #endif
70
71 #define NIR_DEBUG_CLONE (1u << 0)
72 #define NIR_DEBUG_SERIALIZE (1u << 1)
73 #define NIR_DEBUG_NOVALIDATE (1u << 2)
74 #define NIR_DEBUG_VALIDATE_SSA_DOMINANCE (1u << 3)
75 #define NIR_DEBUG_TGSI (1u << 4)
76 #define NIR_DEBUG_PRINT_VS (1u << 5)
77 #define NIR_DEBUG_PRINT_TCS (1u << 6)
78 #define NIR_DEBUG_PRINT_TES (1u << 7)
79 #define NIR_DEBUG_PRINT_GS (1u << 8)
80 #define NIR_DEBUG_PRINT_FS (1u << 9)
81 #define NIR_DEBUG_PRINT_CS (1u << 10)
82 #define NIR_DEBUG_PRINT_TS (1u << 11)
83 #define NIR_DEBUG_PRINT_MS (1u << 12)
84 #define NIR_DEBUG_PRINT_RGS (1u << 13)
85 #define NIR_DEBUG_PRINT_AHS (1u << 14)
86 #define NIR_DEBUG_PRINT_CHS (1u << 15)
87 #define NIR_DEBUG_PRINT_MHS (1u << 16)
88 #define NIR_DEBUG_PRINT_IS (1u << 17)
89 #define NIR_DEBUG_PRINT_CBS (1u << 18)
90 #define NIR_DEBUG_PRINT_KS (1u << 19)
91 #define NIR_DEBUG_PRINT_NO_INLINE_CONSTS (1u << 20)
92 #define NIR_DEBUG_PRINT_INTERNAL (1u << 21)
93 #define NIR_DEBUG_PRINT_PASS_FLAGS (1u << 22)
94
95 #define NIR_DEBUG_PRINT (NIR_DEBUG_PRINT_VS | \
96 NIR_DEBUG_PRINT_TCS | \
97 NIR_DEBUG_PRINT_TES | \
98 NIR_DEBUG_PRINT_GS | \
99 NIR_DEBUG_PRINT_FS | \
100 NIR_DEBUG_PRINT_CS | \
101 NIR_DEBUG_PRINT_TS | \
102 NIR_DEBUG_PRINT_MS | \
103 NIR_DEBUG_PRINT_RGS | \
104 NIR_DEBUG_PRINT_AHS | \
105 NIR_DEBUG_PRINT_CHS | \
106 NIR_DEBUG_PRINT_MHS | \
107 NIR_DEBUG_PRINT_IS | \
108 NIR_DEBUG_PRINT_CBS | \
109 NIR_DEBUG_PRINT_KS)
110
111 #define NIR_FALSE 0u
112 #define NIR_TRUE (~0u)
113 #define NIR_MAX_VEC_COMPONENTS 16
114 #define NIR_MAX_MATRIX_COLUMNS 4
115 #define NIR_STREAM_PACKED (1 << 8)
116 typedef uint16_t nir_component_mask_t;
117
118 static inline bool
nir_num_components_valid(unsigned num_components)119 nir_num_components_valid(unsigned num_components)
120 {
121 return (num_components >= 1 &&
122 num_components <= 5) ||
123 num_components == 8 ||
124 num_components == 16;
125 }
126
127 static inline nir_component_mask_t
nir_component_mask(unsigned num_components)128 nir_component_mask(unsigned num_components)
129 {
130 assert(nir_num_components_valid(num_components));
131 return (1u << num_components) - 1;
132 }
133
134 void
135 nir_process_debug_variable(void);
136
137 bool nir_component_mask_can_reinterpret(nir_component_mask_t mask,
138 unsigned old_bit_size,
139 unsigned new_bit_size);
140 nir_component_mask_t
141 nir_component_mask_reinterpret(nir_component_mask_t mask,
142 unsigned old_bit_size,
143 unsigned new_bit_size);
144
145 /** Defines a cast function
146 *
147 * This macro defines a cast function from in_type to out_type where
148 * out_type is some structure type that contains a field of type out_type.
149 *
150 * Note that you have to be a bit careful as the generated cast function
151 * destroys constness.
152 */
153 #define NIR_DEFINE_CAST(name, in_type, out_type, field, \
154 type_field, type_value) \
155 static inline out_type * \
156 name(const in_type *parent) \
157 { \
158 assert(parent && parent->type_field == type_value); \
159 return exec_node_data(out_type, parent, field); \
160 }
161
162 struct nir_function;
163 struct nir_shader;
164 struct nir_instr;
165 struct nir_builder;
166 struct nir_xfb_info;
167
168 /**
169 * Description of built-in state associated with a uniform
170 *
171 * :c:member:`nir_variable.state_slots`
172 */
173 typedef struct {
174 gl_state_index16 tokens[STATE_LENGTH];
175 } nir_state_slot;
176
177 /* clang-format off */
178 typedef enum {
179 nir_var_system_value = (1 << 0),
180 nir_var_uniform = (1 << 1),
181 nir_var_shader_in = (1 << 2),
182 nir_var_shader_out = (1 << 3),
183 nir_var_image = (1 << 4),
184 /** Incoming call or ray payload data for ray-tracing shaders */
185 nir_var_shader_call_data = (1 << 5),
186 /** Ray hit attributes */
187 nir_var_ray_hit_attrib = (1 << 6),
188
189 /* Modes named nir_var_mem_* have explicit data layout */
190 nir_var_mem_ubo = (1 << 7),
191 nir_var_mem_push_const = (1 << 8),
192 nir_var_mem_ssbo = (1 << 9),
193 nir_var_mem_constant = (1 << 10),
194 nir_var_mem_task_payload = (1 << 11),
195 nir_var_mem_node_payload = (1 << 12),
196 nir_var_mem_node_payload_in = (1 << 13),
197
198 /* Generic modes intentionally come last. See encode_dref_modes() in
199 * nir_serialize.c for more details.
200 */
201 nir_var_shader_temp = (1 << 14),
202 nir_var_function_temp = (1 << 15),
203 nir_var_mem_shared = (1 << 16),
204 nir_var_mem_global = (1 << 17),
205
206 nir_var_mem_generic = (nir_var_shader_temp |
207 nir_var_function_temp |
208 nir_var_mem_shared |
209 nir_var_mem_global),
210
211 nir_var_read_only_modes = nir_var_shader_in | nir_var_uniform |
212 nir_var_system_value | nir_var_mem_constant |
213 nir_var_mem_ubo,
214 /* Modes where vector derefs can be indexed as arrays. nir_var_shader_out
215 * is only for mesh stages. nir_var_system_value is only for kernel stages.
216 */
217 nir_var_vec_indexable_modes = nir_var_shader_temp | nir_var_function_temp |
218 nir_var_mem_ubo | nir_var_mem_ssbo |
219 nir_var_mem_shared | nir_var_mem_global |
220 nir_var_mem_push_const | nir_var_mem_task_payload |
221 nir_var_shader_out | nir_var_system_value,
222 nir_num_variable_modes = 18,
223 nir_var_all = (1 << nir_num_variable_modes) - 1,
224 } nir_variable_mode;
225 MESA_DEFINE_CPP_ENUM_BITFIELD_OPERATORS(nir_variable_mode)
226 /* clang-format on */
227
228 /**
229 * Rounding modes.
230 */
231 typedef enum {
232 nir_rounding_mode_undef = 0,
233 nir_rounding_mode_rtne = 1, /* round to nearest even */
234 nir_rounding_mode_ru = 2, /* round up */
235 nir_rounding_mode_rd = 3, /* round down */
236 nir_rounding_mode_rtz = 4, /* round towards zero */
237 } nir_rounding_mode;
238
239 /**
240 * Ray query values that can read from a RayQueryKHR object.
241 */
242 typedef enum {
243 nir_ray_query_value_intersection_type,
244 nir_ray_query_value_intersection_t,
245 nir_ray_query_value_intersection_instance_custom_index,
246 nir_ray_query_value_intersection_instance_id,
247 nir_ray_query_value_intersection_instance_sbt_index,
248 nir_ray_query_value_intersection_geometry_index,
249 nir_ray_query_value_intersection_primitive_index,
250 nir_ray_query_value_intersection_barycentrics,
251 nir_ray_query_value_intersection_front_face,
252 nir_ray_query_value_intersection_object_ray_direction,
253 nir_ray_query_value_intersection_object_ray_origin,
254 nir_ray_query_value_intersection_object_to_world,
255 nir_ray_query_value_intersection_world_to_object,
256 nir_ray_query_value_intersection_candidate_aabb_opaque,
257 nir_ray_query_value_tmin,
258 nir_ray_query_value_flags,
259 nir_ray_query_value_world_ray_direction,
260 nir_ray_query_value_world_ray_origin,
261 nir_ray_query_value_intersection_triangle_vertex_positions
262 } nir_ray_query_value;
263
264 /**
265 * Intel resource flags
266 */
267 typedef enum {
268 nir_resource_intel_bindless = 1u << 0,
269 nir_resource_intel_pushable = 1u << 1,
270 nir_resource_intel_sampler = 1u << 2,
271 nir_resource_intel_non_uniform = 1u << 3,
272 nir_resource_intel_sampler_embedded = 1u << 4,
273 } nir_resource_data_intel;
274
275 /**
276 * Which components to interpret as signed in cmat_muladd.
277 * See 'Cooperative Matrix Operands' in SPV_KHR_cooperative_matrix.
278 */
279 typedef enum {
280 NIR_CMAT_A_SIGNED = 1u << 0,
281 NIR_CMAT_B_SIGNED = 1u << 1,
282 NIR_CMAT_C_SIGNED = 1u << 2,
283 NIR_CMAT_RESULT_SIGNED = 1u << 3,
284 } nir_cmat_signed;
285
286 typedef union {
287 bool b;
288 float f32;
289 double f64;
290 int8_t i8;
291 uint8_t u8;
292 int16_t i16;
293 uint16_t u16;
294 int32_t i32;
295 uint32_t u32;
296 int64_t i64;
297 uint64_t u64;
298 } nir_const_value;
299
300 #define nir_const_value_to_array(arr, c, components, m) \
301 do { \
302 for (unsigned i = 0; i < components; ++i) \
303 arr[i] = c[i].m; \
304 } while (false)
305
306 static inline nir_const_value
nir_const_value_for_raw_uint(uint64_t x,unsigned bit_size)307 nir_const_value_for_raw_uint(uint64_t x, unsigned bit_size)
308 {
309 nir_const_value v;
310 memset(&v, 0, sizeof(v));
311
312 /* clang-format off */
313 switch (bit_size) {
314 case 1: v.b = x; break;
315 case 8: v.u8 = x; break;
316 case 16: v.u16 = x; break;
317 case 32: v.u32 = x; break;
318 case 64: v.u64 = x; break;
319 default:
320 unreachable("Invalid bit size");
321 }
322 /* clang-format on */
323
324 return v;
325 }
326
327 static inline nir_const_value
nir_const_value_for_int(int64_t i,unsigned bit_size)328 nir_const_value_for_int(int64_t i, unsigned bit_size)
329 {
330 assert(bit_size <= 64);
331 if (bit_size < 64) {
332 assert(i >= (-(1ll << (bit_size - 1))));
333 assert(i < (1ll << (bit_size - 1)));
334 }
335
336 return nir_const_value_for_raw_uint(i, bit_size);
337 }
338
339 static inline nir_const_value
nir_const_value_for_uint(uint64_t u,unsigned bit_size)340 nir_const_value_for_uint(uint64_t u, unsigned bit_size)
341 {
342 assert(bit_size <= 64);
343 if (bit_size < 64)
344 assert(u < (1ull << bit_size));
345
346 return nir_const_value_for_raw_uint(u, bit_size);
347 }
348
349 static inline nir_const_value
nir_const_value_for_bool(bool b,unsigned bit_size)350 nir_const_value_for_bool(bool b, unsigned bit_size)
351 {
352 /* Booleans use a 0/-1 convention */
353 return nir_const_value_for_int(-(int)b, bit_size);
354 }
355
356 /* This one isn't inline because it requires half-float conversion */
357 nir_const_value nir_const_value_for_float(double b, unsigned bit_size);
358
359 static inline int64_t
nir_const_value_as_int(nir_const_value value,unsigned bit_size)360 nir_const_value_as_int(nir_const_value value, unsigned bit_size)
361 {
362 /* clang-format off */
363 switch (bit_size) {
364 /* int1_t uses 0/-1 convention */
365 case 1: return -(int)value.b;
366 case 8: return value.i8;
367 case 16: return value.i16;
368 case 32: return value.i32;
369 case 64: return value.i64;
370 default:
371 unreachable("Invalid bit size");
372 }
373 /* clang-format on */
374 }
375
376 static inline uint64_t
nir_const_value_as_uint(nir_const_value value,unsigned bit_size)377 nir_const_value_as_uint(nir_const_value value, unsigned bit_size)
378 {
379 /* clang-format off */
380 switch (bit_size) {
381 case 1: return value.b;
382 case 8: return value.u8;
383 case 16: return value.u16;
384 case 32: return value.u32;
385 case 64: return value.u64;
386 default:
387 unreachable("Invalid bit size");
388 }
389 /* clang-format on */
390 }
391
392 static inline bool
nir_const_value_as_bool(nir_const_value value,unsigned bit_size)393 nir_const_value_as_bool(nir_const_value value, unsigned bit_size)
394 {
395 int64_t i = nir_const_value_as_int(value, bit_size);
396
397 /* Booleans of any size use 0/-1 convention */
398 assert(i == 0 || i == -1);
399
400 return i;
401 }
402
403 /* This one isn't inline because it requires half-float conversion */
404 double nir_const_value_as_float(nir_const_value value, unsigned bit_size);
405
406 typedef struct nir_constant {
407 /**
408 * Value of the constant.
409 *
410 * The field used to back the values supplied by the constant is determined
411 * by the type associated with the ``nir_variable``. Constants may be
412 * scalars, vectors, or matrices.
413 */
414 nir_const_value values[NIR_MAX_VEC_COMPONENTS];
415
416 /* Indicates all the values are 0s which can enable some optimizations */
417 bool is_null_constant;
418
419 /* we could get this from the var->type but makes clone *much* easier to
420 * not have to care about the type.
421 */
422 unsigned num_elements;
423
424 /* Array elements / Structure Fields */
425 struct nir_constant **elements;
426 } nir_constant;
427
428 /**
429 * Layout qualifiers for gl_FragDepth.
430 *
431 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
432 * with a layout qualifier.
433 */
434 typedef enum {
435 /** No depth layout is specified. */
436 nir_depth_layout_none,
437 nir_depth_layout_any,
438 nir_depth_layout_greater,
439 nir_depth_layout_less,
440 nir_depth_layout_unchanged
441 } nir_depth_layout;
442
443 /**
444 * Enum keeping track of how a variable was declared.
445 */
446 typedef enum {
447 /**
448 * Normal declaration.
449 */
450 nir_var_declared_normally = 0,
451
452 /**
453 * Variable is an implicitly declared built-in that has not been explicitly
454 * re-declared by the shader.
455 */
456 nir_var_declared_implicitly,
457
458 /**
459 * Variable is implicitly generated by the compiler and should not be
460 * visible via the API.
461 */
462 nir_var_hidden,
463 } nir_var_declaration_type;
464
465 /**
466 * Either a uniform, global variable, shader input, or shader output. Based on
467 * ir_variable - it should be easy to translate between the two.
468 */
469
470 typedef struct nir_variable {
471 struct exec_node node;
472
473 /**
474 * Declared type of the variable
475 */
476 const struct glsl_type *type;
477
478 /**
479 * Declared name of the variable
480 */
481 char *name;
482
483 struct nir_variable_data {
484 /**
485 * Storage class of the variable.
486 *
487 * :c:struct:`nir_variable_mode`
488 */
489 unsigned mode : 18;
490
491 /**
492 * Is the variable read-only?
493 *
494 * This is set for variables declared as ``const``, shader inputs,
495 * and uniforms.
496 */
497 unsigned read_only : 1;
498 unsigned centroid : 1;
499 unsigned sample : 1;
500 unsigned patch : 1;
501 unsigned invariant : 1;
502
503 /**
504 * Was an 'invariant' qualifier explicitly set in the shader?
505 *
506 * This is used to cross validate glsl qualifiers.
507 */
508 unsigned explicit_invariant:1;
509
510 /**
511 * Is the variable a ray query?
512 */
513 unsigned ray_query : 1;
514
515 /**
516 * Precision qualifier.
517 *
518 * In desktop GLSL we do not care about precision qualifiers at all, in
519 * fact, the spec says that precision qualifiers are ignored.
520 *
521 * To make things easy, we make it so that this field is always
522 * GLSL_PRECISION_NONE on desktop shaders. This way all the variables
523 * have the same precision value and the checks we add in the compiler
524 * for this field will never break a desktop shader compile.
525 */
526 unsigned precision : 2;
527
528 /**
529 * Has this variable been statically assigned?
530 *
531 * This answers whether the variable was assigned in any path of
532 * the shader during ast_to_hir. This doesn't answer whether it is
533 * still written after dead code removal, nor is it maintained in
534 * non-ast_to_hir.cpp (GLSL parsing) paths.
535 */
536 unsigned assigned : 1;
537
538 /**
539 * Can this variable be coalesced with another?
540 *
541 * This is set by nir_lower_io_to_temporaries to say that any
542 * copies involving this variable should stay put. Propagating it can
543 * duplicate the resulting load/store, which is not wanted, and may
544 * result in a load/store of the variable with an indirect offset which
545 * the backend may not be able to handle.
546 */
547 unsigned cannot_coalesce : 1;
548
549 /**
550 * When separate shader programs are enabled, only input/outputs between
551 * the stages of a multi-stage separate program can be safely removed
552 * from the shader interface. Other input/outputs must remains active.
553 *
554 * This is also used to make sure xfb varyings that are unused by the
555 * fragment shader are not removed.
556 */
557 unsigned always_active_io : 1;
558
559 /**
560 * Interpolation mode for shader inputs / outputs
561 *
562 * :c:enum:`glsl_interp_mode`
563 */
564 unsigned interpolation : 3;
565
566 /**
567 * If non-zero, then this variable may be packed along with other variables
568 * into a single varying slot, so this offset should be applied when
569 * accessing components. For example, an offset of 1 means that the x
570 * component of this variable is actually stored in component y of the
571 * location specified by ``location``.
572 */
573 unsigned location_frac : 2;
574
575 /**
576 * If true, this variable represents an array of scalars that should
577 * be tightly packed. In other words, consecutive array elements
578 * should be stored one component apart, rather than one slot apart.
579 */
580 unsigned compact : 1;
581
582 /**
583 * Whether this is a fragment shader output implicitly initialized with
584 * the previous contents of the specified render target at the
585 * framebuffer location corresponding to this shader invocation.
586 */
587 unsigned fb_fetch_output : 1;
588
589 /**
590 * Non-zero if this variable is considered bindless as defined by
591 * ARB_bindless_texture.
592 */
593 unsigned bindless : 1;
594
595 /**
596 * Was an explicit binding set in the shader?
597 */
598 unsigned explicit_binding : 1;
599
600 /**
601 * Was the location explicitly set in the shader?
602 *
603 * If the location is explicitly set in the shader, it **cannot** be changed
604 * by the linker or by the API (e.g., calls to ``glBindAttribLocation`` have
605 * no effect).
606 */
607 unsigned explicit_location : 1;
608
609 /* Was the array implicitly sized during linking */
610 unsigned implicit_sized_array : 1;
611
612 /**
613 * Highest element accessed with a constant array index
614 *
615 * Not used for non-array variables. -1 is never accessed.
616 */
617 int max_array_access;
618
619 /**
620 * Does this variable have an initializer?
621 *
622 * This is used by the linker to cross-validiate initializers of global
623 * variables.
624 */
625 unsigned has_initializer:1;
626
627 /**
628 * Is the initializer created by the compiler (glsl_zero_init)
629 */
630 unsigned is_implicit_initializer:1;
631
632 /**
633 * Is this varying used by transform feedback?
634 *
635 * This is used by the linker to decide if it's safe to pack the varying.
636 */
637 unsigned is_xfb : 1;
638
639 /**
640 * Is this varying used only by transform feedback?
641 *
642 * This is used by the linker to decide if its safe to pack the varying.
643 */
644 unsigned is_xfb_only : 1;
645
646 /**
647 * Was a transfer feedback buffer set in the shader?
648 */
649 unsigned explicit_xfb_buffer : 1;
650
651 /**
652 * Was a transfer feedback stride set in the shader?
653 */
654 unsigned explicit_xfb_stride : 1;
655
656 /**
657 * Was an explicit offset set in the shader?
658 */
659 unsigned explicit_offset : 1;
660
661 /**
662 * Layout of the matrix. Uses glsl_matrix_layout values.
663 */
664 unsigned matrix_layout : 2;
665
666 /**
667 * Non-zero if this variable was created by lowering a named interface
668 * block.
669 */
670 unsigned from_named_ifc_block : 1;
671
672 /**
673 * Unsized array buffer variable.
674 */
675 unsigned from_ssbo_unsized_array : 1;
676
677 /**
678 * Non-zero if the variable must be a shader input. This is useful for
679 * constraints on function parameters.
680 */
681 unsigned must_be_shader_input : 1;
682
683 /**
684 * Has this variable been used for reading or writing?
685 *
686 * Several GLSL semantic checks require knowledge of whether or not a
687 * variable has been used. For example, it is an error to redeclare a
688 * variable as invariant after it has been used.
689 */
690 unsigned used:1;
691
692 /**
693 * How the variable was declared. See nir_var_declaration_type.
694 *
695 * This is used to detect variables generated by the compiler, so should
696 * not be visible via the API.
697 */
698 unsigned how_declared : 2;
699
700 /**
701 * Is this variable per-view? If so, we know it must be an array with
702 * size corresponding to the number of views.
703 */
704 unsigned per_view : 1;
705
706 /**
707 * Whether the variable is per-primitive.
708 * Can be use by Mesh Shader outputs and corresponding Fragment Shader inputs.
709 */
710 unsigned per_primitive : 1;
711
712 /**
713 * Whether the variable is declared to indicate that a fragment shader
714 * input will not have interpolated values.
715 */
716 unsigned per_vertex : 1;
717
718 /**
719 * Layout qualifier for gl_FragDepth. See nir_depth_layout.
720 *
721 * This is not equal to ``ir_depth_layout_none`` if and only if this
722 * variable is ``gl_FragDepth`` and a layout qualifier is specified.
723 */
724 unsigned depth_layout : 3;
725
726 /**
727 * Vertex stream output identifier.
728 *
729 * For packed outputs, NIR_STREAM_PACKED is set and bits [2*i+1,2*i]
730 * indicate the stream of the i-th component.
731 */
732 unsigned stream : 9;
733
734 /**
735 * See gl_access_qualifier.
736 *
737 * Access flags for memory variables (SSBO/global), image uniforms, and
738 * bindless images in uniforms/inputs/outputs.
739 */
740 unsigned access : 9;
741
742 /**
743 * Descriptor set binding for sampler or UBO.
744 */
745 unsigned descriptor_set : 5;
746
747 /**
748 * output index for dual source blending.
749 */
750 unsigned index;
751
752 /**
753 * Initial binding point for a sampler or UBO.
754 *
755 * For array types, this represents the binding point for the first element.
756 */
757 unsigned binding;
758
759 /**
760 * Storage location of the base of this variable
761 *
762 * The precise meaning of this field depends on the nature of the variable.
763 *
764 * - Vertex shader input: one of the values from ``gl_vert_attrib``.
765 * - Vertex shader output: one of the values from ``gl_varying_slot``.
766 * - Geometry shader input: one of the values from ``gl_varying_slot``.
767 * - Geometry shader output: one of the values from ``gl_varying_slot``.
768 * - Fragment shader input: one of the values from ``gl_varying_slot``.
769 * - Fragment shader output: one of the values from ``gl_frag_result``.
770 * - Task shader output: one of the values from ``gl_varying_slot``.
771 * - Mesh shader input: one of the values from ``gl_varying_slot``.
772 * - Mesh shader output: one of the values from ``gl_varying_slot``.
773 * - Uniforms: Per-stage uniform slot number for default uniform block.
774 * - Uniforms: Index within the uniform block definition for UBO members.
775 * - Non-UBO Uniforms: uniform slot number.
776 * - Other: This field is not currently used.
777 *
778 * If the variable is a uniform, shader input, or shader output, and the
779 * slot has not been assigned, the value will be -1.
780 */
781 int location;
782
783 /** Required alignment of this variable */
784 unsigned alignment;
785
786 /**
787 * The actual location of the variable in the IR. Only valid for inputs,
788 * outputs, uniforms (including samplers and images), and for UBO and SSBO
789 * variables in GLSL.
790 */
791 unsigned driver_location;
792
793 /**
794 * Location an atomic counter or transform feedback is stored at.
795 */
796 unsigned offset;
797
798 union {
799 struct {
800 /** Image internal format if specified explicitly, otherwise PIPE_FORMAT_NONE. */
801 enum pipe_format format;
802 } image;
803
804 struct {
805 /**
806 * For OpenCL inline samplers. See cl_sampler_addressing_mode and cl_sampler_filter_mode
807 */
808 unsigned is_inline_sampler : 1;
809 unsigned addressing_mode : 3;
810 unsigned normalized_coordinates : 1;
811 unsigned filter_mode : 1;
812 } sampler;
813
814 struct {
815 /**
816 * Transform feedback buffer.
817 */
818 uint16_t buffer : 2;
819
820 /**
821 * Transform feedback stride.
822 */
823 uint16_t stride;
824 } xfb;
825 };
826
827 /** Name of the node this payload will be enqueued to. */
828 const char *node_name;
829 } data;
830
831 /**
832 * Identifier for this variable generated by nir_index_vars() that is unique
833 * among other variables in the same exec_list.
834 */
835 unsigned index;
836
837 /* Number of nir_variable_data members */
838 uint16_t num_members;
839
840 /**
841 * Built-in state that backs this uniform
842 *
843 * Once set at variable creation, ``state_slots`` must remain invariant.
844 * This is because, ideally, this array would be shared by all clones of
845 * this variable in the IR tree. In other words, we'd really like for it
846 * to be a fly-weight.
847 *
848 * If the variable is not a uniform, ``num_state_slots`` will be zero and
849 * ``state_slots`` will be ``NULL``.
850 *
851 * Number of state slots used.
852 */
853 uint16_t num_state_slots;
854 /** State descriptors. */
855 nir_state_slot *state_slots;
856
857 /**
858 * Constant expression assigned in the initializer of the variable
859 *
860 * This field should only be used temporarily by creators of NIR shaders
861 * and then nir_lower_variable_initializers can be used to get rid of them.
862 * Most of the rest of NIR ignores this field or asserts that it's NULL.
863 */
864 nir_constant *constant_initializer;
865
866 /**
867 * Global variable assigned in the initializer of the variable
868 * This field should only be used temporarily by creators of NIR shaders
869 * and then nir_lower_variable_initializers can be used to get rid of them.
870 * Most of the rest of NIR ignores this field or asserts that it's NULL.
871 */
872 struct nir_variable *pointer_initializer;
873
874 /**
875 * For variables that are in an interface block or are an instance of an
876 * interface block, this is the ``GLSL_TYPE_INTERFACE`` type for that block.
877 *
878 * ``ir_variable.location``
879 */
880 const struct glsl_type *interface_type;
881
882 /**
883 * Description of per-member data for per-member struct variables
884 *
885 * This is used for variables which are actually an amalgamation of
886 * multiple entities such as a struct of built-in values or a struct of
887 * inputs each with their own layout specifier. This is only allowed on
888 * variables with a struct or array of array of struct type.
889 */
890 struct nir_variable_data *members;
891 } nir_variable;
892
893 static inline bool
_nir_shader_variable_has_mode(nir_variable * var,unsigned modes)894 _nir_shader_variable_has_mode(nir_variable *var, unsigned modes)
895 {
896 /* This isn't a shader variable */
897 assert(!(modes & nir_var_function_temp));
898 return var->data.mode & modes;
899 }
900
901 #define nir_foreach_variable_in_list(var, var_list) \
902 foreach_list_typed(nir_variable, var, node, var_list)
903
904 #define nir_foreach_variable_in_list_safe(var, var_list) \
905 foreach_list_typed_safe(nir_variable, var, node, var_list)
906
907 #define nir_foreach_variable_in_shader(var, shader) \
908 nir_foreach_variable_in_list(var, &(shader)->variables)
909
910 #define nir_foreach_variable_in_shader_safe(var, shader) \
911 nir_foreach_variable_in_list_safe(var, &(shader)->variables)
912
913 #define nir_foreach_variable_with_modes(var, shader, modes) \
914 nir_foreach_variable_in_shader(var, shader) \
915 if (_nir_shader_variable_has_mode(var, modes))
916
917 #define nir_foreach_variable_with_modes_safe(var, shader, modes) \
918 nir_foreach_variable_in_shader_safe(var, shader) \
919 if (_nir_shader_variable_has_mode(var, modes))
920
921 #define nir_foreach_shader_in_variable(var, shader) \
922 nir_foreach_variable_with_modes(var, shader, nir_var_shader_in)
923
924 #define nir_foreach_shader_in_variable_safe(var, shader) \
925 nir_foreach_variable_with_modes_safe(var, shader, nir_var_shader_in)
926
927 #define nir_foreach_shader_out_variable(var, shader) \
928 nir_foreach_variable_with_modes(var, shader, nir_var_shader_out)
929
930 #define nir_foreach_shader_out_variable_safe(var, shader) \
931 nir_foreach_variable_with_modes_safe(var, shader, nir_var_shader_out)
932
933 #define nir_foreach_uniform_variable(var, shader) \
934 nir_foreach_variable_with_modes(var, shader, nir_var_uniform)
935
936 #define nir_foreach_uniform_variable_safe(var, shader) \
937 nir_foreach_variable_with_modes_safe(var, shader, nir_var_uniform)
938
939 #define nir_foreach_image_variable(var, shader) \
940 nir_foreach_variable_with_modes(var, shader, nir_var_image)
941
942 #define nir_foreach_image_variable_safe(var, shader) \
943 nir_foreach_variable_with_modes_safe(var, shader, nir_var_image)
944
945 static inline bool
nir_variable_is_global(const nir_variable * var)946 nir_variable_is_global(const nir_variable *var)
947 {
948 return var->data.mode != nir_var_function_temp;
949 }
950
951 typedef enum ENUM_PACKED {
952 nir_instr_type_alu,
953 nir_instr_type_deref,
954 nir_instr_type_call,
955 nir_instr_type_tex,
956 nir_instr_type_intrinsic,
957 nir_instr_type_load_const,
958 nir_instr_type_jump,
959 nir_instr_type_undef,
960 nir_instr_type_phi,
961 nir_instr_type_parallel_copy,
962 nir_instr_type_debug_info,
963 } nir_instr_type;
964
965 typedef struct nir_instr {
966 struct exec_node node;
967 struct nir_block *block;
968 nir_instr_type type;
969
970 /* A temporary for optimization and analysis passes to use for storing
971 * flags. For instance, DCE uses this to store the "dead/live" info.
972 */
973 uint8_t pass_flags;
974
975 /** generic instruction index. */
976 uint32_t index;
977 } nir_instr;
978
979 static inline nir_instr *
nir_instr_next(nir_instr * instr)980 nir_instr_next(nir_instr *instr)
981 {
982 struct exec_node *next = exec_node_get_next(&instr->node);
983 if (exec_node_is_tail_sentinel(next))
984 return NULL;
985 else
986 return exec_node_data(nir_instr, next, node);
987 }
988
989 static inline nir_instr *
nir_instr_prev(nir_instr * instr)990 nir_instr_prev(nir_instr *instr)
991 {
992 struct exec_node *prev = exec_node_get_prev(&instr->node);
993 if (exec_node_is_head_sentinel(prev))
994 return NULL;
995 else
996 return exec_node_data(nir_instr, prev, node);
997 }
998
999 static inline bool
nir_instr_is_first(const nir_instr * instr)1000 nir_instr_is_first(const nir_instr *instr)
1001 {
1002 return exec_node_is_head_sentinel(exec_node_get_prev_const(&instr->node));
1003 }
1004
1005 static inline bool
nir_instr_is_last(const nir_instr * instr)1006 nir_instr_is_last(const nir_instr *instr)
1007 {
1008 return exec_node_is_tail_sentinel(exec_node_get_next_const(&instr->node));
1009 }
1010
1011 typedef struct nir_def {
1012 /** Instruction which produces this SSA value. */
1013 nir_instr *parent_instr;
1014
1015 /** set of nir_instrs where this register is used (read from) */
1016 struct list_head uses;
1017
1018 /** generic SSA definition index. */
1019 unsigned index;
1020
1021 uint8_t num_components;
1022
1023 /* The bit-size of each channel; must be one of 1, 8, 16, 32, or 64 */
1024 uint8_t bit_size;
1025
1026 /**
1027 * True if this SSA value may have different values in different SIMD
1028 * invocations of the shader. This is set by nir_divergence_analysis.
1029 */
1030 bool divergent;
1031 } nir_def;
1032
1033 struct nir_src;
1034 struct nir_if;
1035
1036 typedef struct nir_src {
1037 /* Instruction or if-statement that consumes this value as a source. This
1038 * should only be accessed through nir_src_* helpers.
1039 *
1040 * Internally, it is a tagged pointer to a nir_instr or nir_if.
1041 */
1042 uintptr_t _parent;
1043
1044 struct list_head use_link;
1045 nir_def *ssa;
1046 } nir_src;
1047
1048 /* Layout of the _parent pointer. Bottom bit is set for nir_if parents (clear
1049 * for nir_instr parents). Remaining bits are the pointer.
1050 */
1051 #define NIR_SRC_PARENT_IS_IF (0x1)
1052 #define NIR_SRC_PARENT_MASK (~((uintptr_t) NIR_SRC_PARENT_IS_IF))
1053
1054 static inline bool
nir_src_is_if(const nir_src * src)1055 nir_src_is_if(const nir_src *src)
1056 {
1057 return src->_parent & NIR_SRC_PARENT_IS_IF;
1058 }
1059
1060 static inline nir_instr *
nir_src_parent_instr(const nir_src * src)1061 nir_src_parent_instr(const nir_src *src)
1062 {
1063 assert(!nir_src_is_if(src));
1064
1065 /* Because it is not an if, the tag is 0, therefore we do not need to mask */
1066 return (nir_instr *)(src->_parent);
1067 }
1068
1069 static inline struct nir_if *
nir_src_parent_if(const nir_src * src)1070 nir_src_parent_if(const nir_src *src)
1071 {
1072 assert(nir_src_is_if(src));
1073
1074 /* Because it is an if, the tag is 1, so we need to mask */
1075 return (struct nir_if *)(src->_parent & NIR_SRC_PARENT_MASK);
1076 }
1077
1078 static inline void
_nir_src_set_parent(nir_src * src,void * parent,bool is_if)1079 _nir_src_set_parent(nir_src *src, void *parent, bool is_if)
1080 {
1081 uintptr_t ptr = (uintptr_t) parent;
1082 assert((ptr & ~NIR_SRC_PARENT_MASK) == 0 && "pointer must be aligned");
1083
1084 if (is_if)
1085 ptr |= NIR_SRC_PARENT_IS_IF;
1086
1087 src->_parent = ptr;
1088 }
1089
1090 static inline void
nir_src_set_parent_instr(nir_src * src,nir_instr * parent_instr)1091 nir_src_set_parent_instr(nir_src *src, nir_instr *parent_instr)
1092 {
1093 _nir_src_set_parent(src, parent_instr, false);
1094 }
1095
1096 static inline void
nir_src_set_parent_if(nir_src * src,struct nir_if * parent_if)1097 nir_src_set_parent_if(nir_src *src, struct nir_if *parent_if)
1098 {
1099 _nir_src_set_parent(src, parent_if, true);
1100 }
1101
1102 static inline nir_src
nir_src_init(void)1103 nir_src_init(void)
1104 {
1105 nir_src src = { 0 };
1106 return src;
1107 }
1108
1109 #define NIR_SRC_INIT nir_src_init()
1110
1111 #define nir_foreach_use_including_if(src, reg_or_ssa_def) \
1112 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
1113
1114 #define nir_foreach_use_including_if_safe(src, reg_or_ssa_def) \
1115 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
1116
1117 #define nir_foreach_use(src, reg_or_ssa_def) \
1118 nir_foreach_use_including_if(src, reg_or_ssa_def) \
1119 if (!nir_src_is_if(src))
1120
1121 #define nir_foreach_use_safe(src, reg_or_ssa_def) \
1122 nir_foreach_use_including_if_safe(src, reg_or_ssa_def) \
1123 if (!nir_src_is_if(src))
1124
1125 #define nir_foreach_if_use(src, reg_or_ssa_def) \
1126 nir_foreach_use_including_if(src, reg_or_ssa_def) \
1127 if (nir_src_is_if(src))
1128
1129 #define nir_foreach_if_use_safe(src, reg_or_ssa_def) \
1130 nir_foreach_use_including_if_safe(src, reg_or_ssa_def) \
1131 if (nir_src_is_if(src))
1132
1133 static inline bool
nir_def_used_by_if(const nir_def * def)1134 nir_def_used_by_if(const nir_def *def)
1135 {
1136 nir_foreach_if_use(_, def)
1137 return true;
1138
1139 return false;
1140 }
1141
1142 static inline bool
nir_def_only_used_by_if(const nir_def * def)1143 nir_def_only_used_by_if(const nir_def *def)
1144 {
1145 nir_foreach_use(_, def)
1146 return false;
1147
1148 return true;
1149 }
1150
1151 static inline nir_src
nir_src_for_ssa(nir_def * def)1152 nir_src_for_ssa(nir_def *def)
1153 {
1154 nir_src src = NIR_SRC_INIT;
1155
1156 src.ssa = def;
1157
1158 return src;
1159 }
1160
1161 static inline unsigned
nir_src_bit_size(nir_src src)1162 nir_src_bit_size(nir_src src)
1163 {
1164 return src.ssa->bit_size;
1165 }
1166
1167 static inline unsigned
nir_src_num_components(nir_src src)1168 nir_src_num_components(nir_src src)
1169 {
1170 return src.ssa->num_components;
1171 }
1172
1173 static inline bool
nir_src_is_const(nir_src src)1174 nir_src_is_const(nir_src src)
1175 {
1176 return src.ssa->parent_instr->type == nir_instr_type_load_const;
1177 }
1178
1179 static inline bool
nir_src_is_undef(nir_src src)1180 nir_src_is_undef(nir_src src)
1181 {
1182 return src.ssa->parent_instr->type == nir_instr_type_undef;
1183 }
1184
1185 static inline bool
nir_src_is_divergent(nir_src src)1186 nir_src_is_divergent(nir_src src)
1187 {
1188 return src.ssa->divergent;
1189 }
1190
1191 /* Are all components the same, ie. .xxxx */
1192 static inline bool
nir_is_same_comp_swizzle(uint8_t * swiz,unsigned nr_comp)1193 nir_is_same_comp_swizzle(uint8_t *swiz, unsigned nr_comp)
1194 {
1195 for (unsigned i = 1; i < nr_comp; i++)
1196 if (swiz[i] != swiz[0])
1197 return false;
1198 return true;
1199 }
1200
1201 /* Are all components sequential, ie. .yzw */
1202 static inline bool
nir_is_sequential_comp_swizzle(uint8_t * swiz,unsigned nr_comp)1203 nir_is_sequential_comp_swizzle(uint8_t *swiz, unsigned nr_comp)
1204 {
1205 for (unsigned i = 1; i < nr_comp; i++)
1206 if (swiz[i] != (swiz[0] + i))
1207 return false;
1208 return true;
1209 }
1210
1211 /***/
1212 typedef struct nir_alu_src {
1213 /** Base source */
1214 nir_src src;
1215
1216 /**
1217 * For each input component, says which component of the register it is
1218 * chosen from.
1219 *
1220 * Note that which elements of the swizzle are used and which are ignored
1221 * are based on the write mask for most opcodes - for example, a statement
1222 * like "foo.xzw = bar.zyx" would have a writemask of 1101b and a swizzle
1223 * of {2, 1, x, 0} where x means "don't care."
1224 */
1225 uint8_t swizzle[NIR_MAX_VEC_COMPONENTS];
1226 } nir_alu_src;
1227
1228 /** NIR sized and unsized types
1229 *
1230 * The values in this enum are carefully chosen so that the sized type is
1231 * just the unsized type OR the number of bits.
1232 */
1233 /* clang-format off */
1234 typedef enum ENUM_PACKED {
1235 nir_type_invalid = 0, /* Not a valid type */
1236 nir_type_int = 2,
1237 nir_type_uint = 4,
1238 nir_type_bool = 6,
1239 nir_type_float = 128,
1240 nir_type_bool1 = 1 | nir_type_bool,
1241 nir_type_bool8 = 8 | nir_type_bool,
1242 nir_type_bool16 = 16 | nir_type_bool,
1243 nir_type_bool32 = 32 | nir_type_bool,
1244 nir_type_int1 = 1 | nir_type_int,
1245 nir_type_int8 = 8 | nir_type_int,
1246 nir_type_int16 = 16 | nir_type_int,
1247 nir_type_int32 = 32 | nir_type_int,
1248 nir_type_int64 = 64 | nir_type_int,
1249 nir_type_uint1 = 1 | nir_type_uint,
1250 nir_type_uint8 = 8 | nir_type_uint,
1251 nir_type_uint16 = 16 | nir_type_uint,
1252 nir_type_uint32 = 32 | nir_type_uint,
1253 nir_type_uint64 = 64 | nir_type_uint,
1254 nir_type_float16 = 16 | nir_type_float,
1255 nir_type_float32 = 32 | nir_type_float,
1256 nir_type_float64 = 64 | nir_type_float,
1257 } nir_alu_type;
1258 /* clang-format on */
1259
1260 #define NIR_ALU_TYPE_SIZE_MASK 0x79
1261 #define NIR_ALU_TYPE_BASE_TYPE_MASK 0x86
1262
1263 static inline unsigned
nir_alu_type_get_type_size(nir_alu_type type)1264 nir_alu_type_get_type_size(nir_alu_type type)
1265 {
1266 return type & NIR_ALU_TYPE_SIZE_MASK;
1267 }
1268
1269 static inline nir_alu_type
nir_alu_type_get_base_type(nir_alu_type type)1270 nir_alu_type_get_base_type(nir_alu_type type)
1271 {
1272 return (nir_alu_type)(type & NIR_ALU_TYPE_BASE_TYPE_MASK);
1273 }
1274
1275 nir_alu_type
1276 nir_get_nir_type_for_glsl_base_type(enum glsl_base_type base_type);
1277
1278 static inline nir_alu_type
nir_get_nir_type_for_glsl_type(const struct glsl_type * type)1279 nir_get_nir_type_for_glsl_type(const struct glsl_type *type)
1280 {
1281 return nir_get_nir_type_for_glsl_base_type(glsl_get_base_type(type));
1282 }
1283
1284 enum glsl_base_type
1285 nir_get_glsl_base_type_for_nir_type(nir_alu_type base_type);
1286
1287 nir_op nir_type_conversion_op(nir_alu_type src, nir_alu_type dst,
1288 nir_rounding_mode rnd);
1289
1290 /**
1291 * Atomic intrinsics perform different operations depending on the value of
1292 * their atomic_op constant index. nir_atomic_op defines the operations.
1293 */
1294 typedef enum {
1295 nir_atomic_op_iadd,
1296 nir_atomic_op_imin,
1297 nir_atomic_op_umin,
1298 nir_atomic_op_imax,
1299 nir_atomic_op_umax,
1300 nir_atomic_op_iand,
1301 nir_atomic_op_ior,
1302 nir_atomic_op_ixor,
1303 nir_atomic_op_xchg,
1304 nir_atomic_op_fadd,
1305 nir_atomic_op_fmin,
1306 nir_atomic_op_fmax,
1307 nir_atomic_op_cmpxchg,
1308 nir_atomic_op_fcmpxchg,
1309 nir_atomic_op_inc_wrap,
1310 nir_atomic_op_dec_wrap,
1311 nir_atomic_op_ordered_add_gfx12_amd,
1312 } nir_atomic_op;
1313
1314 static inline nir_alu_type
nir_atomic_op_type(nir_atomic_op op)1315 nir_atomic_op_type(nir_atomic_op op)
1316 {
1317 switch (op) {
1318 case nir_atomic_op_imin:
1319 case nir_atomic_op_imax:
1320 return nir_type_int;
1321
1322 case nir_atomic_op_fadd:
1323 case nir_atomic_op_fmin:
1324 case nir_atomic_op_fmax:
1325 case nir_atomic_op_fcmpxchg:
1326 return nir_type_float;
1327
1328 case nir_atomic_op_iadd:
1329 case nir_atomic_op_iand:
1330 case nir_atomic_op_ior:
1331 case nir_atomic_op_ixor:
1332 case nir_atomic_op_xchg:
1333 case nir_atomic_op_cmpxchg:
1334 case nir_atomic_op_umin:
1335 case nir_atomic_op_umax:
1336 case nir_atomic_op_inc_wrap:
1337 case nir_atomic_op_dec_wrap:
1338 case nir_atomic_op_ordered_add_gfx12_amd:
1339 return nir_type_uint;
1340 }
1341
1342 unreachable("Invalid nir_atomic_op");
1343 }
1344
1345 /** Returns nir_op_vec<num_components> or nir_op_mov if num_components == 1
1346 *
1347 * This is subtly different from nir_op_is_vec() which returns false for
1348 * nir_op_mov. Returning nir_op_mov from nir_op_vec() when num_components == 1
1349 * makes sense under the assumption that the num_components of the resulting
1350 * nir_def will same as what is passed in here because a single-component mov
1351 * is effectively a vec1. However, if alu->def.num_components > 1, nir_op_mov
1352 * has different semantics from nir_op_vec* so so code which detects "is this
1353 * a vec?" typically needs to handle nir_op_mov separate from nir_op_vecN.
1354 *
1355 * In the unlikely case where you can handle nir_op_vecN and nir_op_mov
1356 * together, use nir_op_is_vec_or_mov().
1357 */
1358 nir_op
1359 nir_op_vec(unsigned num_components);
1360
1361 /** Returns true if this op is one of nir_op_vec*
1362 *
1363 * Returns false for nir_op_mov. See nir_op_vec() for more details.
1364 */
1365 bool
1366 nir_op_is_vec(nir_op op);
1367
1368 static inline bool
nir_op_is_vec_or_mov(nir_op op)1369 nir_op_is_vec_or_mov(nir_op op)
1370 {
1371 return op == nir_op_mov || nir_op_is_vec(op);
1372 }
1373
1374 static inline bool
nir_is_float_control_signed_zero_preserve(unsigned execution_mode,unsigned bit_size)1375 nir_is_float_control_signed_zero_preserve(unsigned execution_mode, unsigned bit_size)
1376 {
1377 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_PRESERVE_FP16) ||
1378 (32 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_PRESERVE_FP32) ||
1379 (64 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_PRESERVE_FP64);
1380 }
1381
1382 static inline bool
nir_is_float_control_inf_preserve(unsigned execution_mode,unsigned bit_size)1383 nir_is_float_control_inf_preserve(unsigned execution_mode, unsigned bit_size)
1384 {
1385 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_INF_PRESERVE_FP16) ||
1386 (32 == bit_size && execution_mode & FLOAT_CONTROLS_INF_PRESERVE_FP32) ||
1387 (64 == bit_size && execution_mode & FLOAT_CONTROLS_INF_PRESERVE_FP64);
1388 }
1389
1390 static inline bool
nir_is_float_control_nan_preserve(unsigned execution_mode,unsigned bit_size)1391 nir_is_float_control_nan_preserve(unsigned execution_mode, unsigned bit_size)
1392 {
1393 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_NAN_PRESERVE_FP16) ||
1394 (32 == bit_size && execution_mode & FLOAT_CONTROLS_NAN_PRESERVE_FP32) ||
1395 (64 == bit_size && execution_mode & FLOAT_CONTROLS_NAN_PRESERVE_FP64);
1396 }
1397
1398 static inline bool
nir_is_float_control_signed_zero_inf_nan_preserve(unsigned execution_mode,unsigned bit_size)1399 nir_is_float_control_signed_zero_inf_nan_preserve(unsigned execution_mode, unsigned bit_size)
1400 {
1401 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16) ||
1402 (32 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32) ||
1403 (64 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
1404 }
1405
1406 static inline bool
nir_is_denorm_flush_to_zero(unsigned execution_mode,unsigned bit_size)1407 nir_is_denorm_flush_to_zero(unsigned execution_mode, unsigned bit_size)
1408 {
1409 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16) ||
1410 (32 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32) ||
1411 (64 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
1412 }
1413
1414 static inline bool
nir_is_denorm_preserve(unsigned execution_mode,unsigned bit_size)1415 nir_is_denorm_preserve(unsigned execution_mode, unsigned bit_size)
1416 {
1417 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP16) ||
1418 (32 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP32) ||
1419 (64 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP64);
1420 }
1421
1422 static inline bool
nir_is_rounding_mode_rtne(unsigned execution_mode,unsigned bit_size)1423 nir_is_rounding_mode_rtne(unsigned execution_mode, unsigned bit_size)
1424 {
1425 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16) ||
1426 (32 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32) ||
1427 (64 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
1428 }
1429
1430 static inline bool
nir_is_rounding_mode_rtz(unsigned execution_mode,unsigned bit_size)1431 nir_is_rounding_mode_rtz(unsigned execution_mode, unsigned bit_size)
1432 {
1433 return (16 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16) ||
1434 (32 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32) ||
1435 (64 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64);
1436 }
1437
1438 static inline bool
nir_has_any_rounding_mode_rtz(unsigned execution_mode)1439 nir_has_any_rounding_mode_rtz(unsigned execution_mode)
1440 {
1441 return (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16) ||
1442 (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32) ||
1443 (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64);
1444 }
1445
1446 static inline bool
nir_has_any_rounding_mode_rtne(unsigned execution_mode)1447 nir_has_any_rounding_mode_rtne(unsigned execution_mode)
1448 {
1449 return (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16) ||
1450 (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32) ||
1451 (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
1452 }
1453
1454 static inline nir_rounding_mode
nir_get_rounding_mode_from_float_controls(unsigned execution_mode,nir_alu_type type)1455 nir_get_rounding_mode_from_float_controls(unsigned execution_mode,
1456 nir_alu_type type)
1457 {
1458 if (nir_alu_type_get_base_type(type) != nir_type_float)
1459 return nir_rounding_mode_undef;
1460
1461 unsigned bit_size = nir_alu_type_get_type_size(type);
1462
1463 if (nir_is_rounding_mode_rtz(execution_mode, bit_size))
1464 return nir_rounding_mode_rtz;
1465 if (nir_is_rounding_mode_rtne(execution_mode, bit_size))
1466 return nir_rounding_mode_rtne;
1467 return nir_rounding_mode_undef;
1468 }
1469
1470 static inline bool
nir_has_any_rounding_mode_enabled(unsigned execution_mode)1471 nir_has_any_rounding_mode_enabled(unsigned execution_mode)
1472 {
1473 bool result =
1474 nir_has_any_rounding_mode_rtne(execution_mode) ||
1475 nir_has_any_rounding_mode_rtz(execution_mode);
1476 return result;
1477 }
1478
1479 typedef enum {
1480 /**
1481 * Operation where the first two sources are commutative.
1482 *
1483 * For 2-source operations, this just mathematical commutativity. Some
1484 * 3-source operations, like ffma, are only commutative in the first two
1485 * sources.
1486 */
1487 NIR_OP_IS_2SRC_COMMUTATIVE = (1 << 0),
1488
1489 /**
1490 * Operation is associative
1491 */
1492 NIR_OP_IS_ASSOCIATIVE = (1 << 1),
1493
1494 /**
1495 * Operation where src[0] is used to select src[1] on true or src[2] false.
1496 * src[0] may be Boolean, or it may be another type used in an implicit
1497 * comparison.
1498 */
1499 NIR_OP_IS_SELECTION = (1 << 2),
1500
1501 /**
1502 * Operation where a screen-space derivative is taken of src[0]. Must not be
1503 * moved into non-uniform control flow.
1504 */
1505 NIR_OP_IS_DERIVATIVE = (1 << 3),
1506 } nir_op_algebraic_property;
1507
1508 /* vec16 is the widest ALU op in NIR, making the max number of input of ALU
1509 * instructions to be the same as NIR_MAX_VEC_COMPONENTS.
1510 */
1511 #define NIR_ALU_MAX_INPUTS NIR_MAX_VEC_COMPONENTS
1512
1513 /***/
1514 typedef struct nir_op_info {
1515 /** Name of the NIR ALU opcode */
1516 const char *name;
1517
1518 /** Number of inputs (sources) */
1519 uint8_t num_inputs;
1520
1521 /**
1522 * The number of components in the output
1523 *
1524 * If non-zero, this is the size of the output and input sizes are
1525 * explicitly given; swizzle and writemask are still in effect, but if
1526 * the output component is masked out, then the input component may
1527 * still be in use.
1528 *
1529 * If zero, the opcode acts in the standard, per-component manner; the
1530 * operation is performed on each component (except the ones that are
1531 * masked out) with the input being taken from the input swizzle for
1532 * that component.
1533 *
1534 * The size of some of the inputs may be given (i.e. non-zero) even
1535 * though output_size is zero; in that case, the inputs with a zero
1536 * size act per-component, while the inputs with non-zero size don't.
1537 */
1538 uint8_t output_size;
1539
1540 /**
1541 * The type of vector that the instruction outputs. Note that the
1542 * staurate modifier is only allowed on outputs with the float type.
1543 */
1544 nir_alu_type output_type;
1545
1546 /**
1547 * The number of components in each input
1548 *
1549 * See nir_op_infos::output_size for more detail about the relationship
1550 * between input and output sizes.
1551 */
1552 uint8_t input_sizes[NIR_ALU_MAX_INPUTS];
1553
1554 /**
1555 * The type of vector that each input takes.
1556 */
1557 nir_alu_type input_types[NIR_ALU_MAX_INPUTS];
1558
1559 /** Algebraic properties of this opcode */
1560 nir_op_algebraic_property algebraic_properties;
1561
1562 /** Whether this represents a numeric conversion opcode */
1563 bool is_conversion;
1564 } nir_op_info;
1565
1566 /** Metadata for each nir_op, indexed by opcode */
1567 extern const nir_op_info nir_op_infos[nir_num_opcodes];
1568
1569 static inline bool
nir_op_is_selection(nir_op op)1570 nir_op_is_selection(nir_op op)
1571 {
1572 return (nir_op_infos[op].algebraic_properties & NIR_OP_IS_SELECTION) != 0;
1573 }
1574
1575 static inline bool
nir_op_is_derivative(nir_op op)1576 nir_op_is_derivative(nir_op op)
1577 {
1578 return (nir_op_infos[op].algebraic_properties & NIR_OP_IS_DERIVATIVE) != 0;
1579 }
1580
1581 /***/
1582 typedef struct nir_alu_instr {
1583 /** Base instruction */
1584 nir_instr instr;
1585
1586 /** Opcode */
1587 nir_op op;
1588
1589 /** Indicates that this ALU instruction generates an exact value
1590 *
1591 * This is kind of a mixture of GLSL "precise" and "invariant" and not
1592 * really equivalent to either. This indicates that the value generated by
1593 * this operation is high-precision and any code transformations that touch
1594 * it must ensure that the resulting value is bit-for-bit identical to the
1595 * original.
1596 */
1597 bool exact : 1;
1598
1599 /**
1600 * Indicates that this instruction doese not cause signed integer wrapping
1601 * to occur, in the form of overflow or underflow.
1602 */
1603 bool no_signed_wrap : 1;
1604
1605 /**
1606 * Indicates that this instruction does not cause unsigned integer wrapping
1607 * to occur, in the form of overflow or underflow.
1608 */
1609 bool no_unsigned_wrap : 1;
1610
1611 /**
1612 * The float controls bit float_controls2 cares about. That is,
1613 * NAN/INF/SIGNED_ZERO_PRESERVE only. Allow{Contract,Reassoc,Transform} are
1614 * still handled through the exact bit, and the other float controls bits
1615 * (rounding mode and denorm handling) remain in the execution mode only.
1616 */
1617 uint32_t fp_fast_math : 9;
1618
1619 /** Destination */
1620 nir_def def;
1621
1622 /** Sources
1623 *
1624 * The size of the array is given by :c:member:`nir_op_info.num_inputs`.
1625 */
1626 nir_alu_src src[];
1627 } nir_alu_instr;
1628
1629 static inline bool
nir_alu_instr_is_signed_zero_preserve(nir_alu_instr * alu)1630 nir_alu_instr_is_signed_zero_preserve(nir_alu_instr *alu)
1631 {
1632 return nir_is_float_control_signed_zero_preserve(alu->fp_fast_math, alu->def.bit_size);
1633 }
1634
1635 static inline bool
nir_alu_instr_is_inf_preserve(nir_alu_instr * alu)1636 nir_alu_instr_is_inf_preserve(nir_alu_instr *alu)
1637 {
1638 return nir_is_float_control_inf_preserve(alu->fp_fast_math, alu->def.bit_size);
1639 }
1640
1641 static inline bool
nir_alu_instr_is_nan_preserve(nir_alu_instr * alu)1642 nir_alu_instr_is_nan_preserve(nir_alu_instr *alu)
1643 {
1644 return nir_is_float_control_nan_preserve(alu->fp_fast_math, alu->def.bit_size);
1645 }
1646
1647 static inline bool
nir_alu_instr_is_signed_zero_inf_nan_preserve(nir_alu_instr * alu)1648 nir_alu_instr_is_signed_zero_inf_nan_preserve(nir_alu_instr *alu)
1649 {
1650 return nir_is_float_control_signed_zero_inf_nan_preserve(alu->fp_fast_math, alu->def.bit_size);
1651 }
1652
1653 void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src);
1654
1655 nir_component_mask_t
1656 nir_alu_instr_src_read_mask(const nir_alu_instr *instr, unsigned src);
1657 /**
1658 * Get the number of channels used for a source
1659 */
1660 unsigned
1661 nir_ssa_alu_instr_src_components(const nir_alu_instr *instr, unsigned src);
1662
1663 /* is this source channel used? */
1664 static inline bool
nir_alu_instr_channel_used(const nir_alu_instr * instr,unsigned src,unsigned channel)1665 nir_alu_instr_channel_used(const nir_alu_instr *instr, unsigned src,
1666 unsigned channel)
1667 {
1668 return channel < nir_ssa_alu_instr_src_components(instr, src);
1669 }
1670
1671 bool
1672 nir_alu_instr_is_comparison(const nir_alu_instr *instr);
1673
1674 bool nir_const_value_negative_equal(nir_const_value c1, nir_const_value c2,
1675 nir_alu_type full_type);
1676
1677 bool nir_alu_srcs_equal(const nir_alu_instr *alu1, const nir_alu_instr *alu2,
1678 unsigned src1, unsigned src2);
1679
1680 bool nir_alu_srcs_negative_equal(const nir_alu_instr *alu1,
1681 const nir_alu_instr *alu2,
1682 unsigned src1, unsigned src2);
1683
1684 bool nir_alu_src_is_trivial_ssa(const nir_alu_instr *alu, unsigned srcn);
1685
1686 typedef enum {
1687 nir_deref_type_var,
1688 nir_deref_type_array,
1689 nir_deref_type_array_wildcard,
1690 nir_deref_type_ptr_as_array,
1691 nir_deref_type_struct,
1692 nir_deref_type_cast,
1693 } nir_deref_type;
1694
1695 typedef struct {
1696 nir_instr instr;
1697
1698 /** The type of this deref instruction */
1699 nir_deref_type deref_type;
1700
1701 /** Bitmask what modes the underlying variable might be
1702 *
1703 * For OpenCL-style generic pointers, we may not know exactly what mode it
1704 * is at any given point in time in the compile process. This bitfield
1705 * contains the set of modes which it MAY be.
1706 *
1707 * Generally, this field should not be accessed directly. Use one of the
1708 * nir_deref_mode_ helpers instead.
1709 */
1710 nir_variable_mode modes;
1711
1712 /** The dereferenced type of the resulting pointer value */
1713 const struct glsl_type *type;
1714
1715 union {
1716 /** Variable being dereferenced if deref_type is a deref_var */
1717 nir_variable *var;
1718
1719 /** Parent deref if deref_type is not deref_var */
1720 nir_src parent;
1721 };
1722
1723 /** Additional deref parameters */
1724 union {
1725 struct {
1726 nir_src index;
1727 bool in_bounds;
1728 } arr;
1729
1730 struct {
1731 unsigned index;
1732 } strct;
1733
1734 struct {
1735 unsigned ptr_stride;
1736 unsigned align_mul;
1737 unsigned align_offset;
1738 } cast;
1739 };
1740
1741 /** Destination to store the resulting "pointer" */
1742 nir_def def;
1743 } nir_deref_instr;
1744
1745 /**
1746 * Returns true if the cast is trivial, i.e. the source and destination type is
1747 * the same.
1748 */
1749 bool nir_deref_cast_is_trivial(nir_deref_instr *cast);
1750
1751 /** Returns true if deref might have one of the given modes
1752 *
1753 * For multi-mode derefs, this returns true if any of the possible modes for
1754 * the deref to have any of the specified modes. This function returning true
1755 * does NOT mean that the deref definitely has one of those modes. It simply
1756 * means that, with the best information we have at the time, it might.
1757 */
1758 static inline bool
nir_deref_mode_may_be(const nir_deref_instr * deref,nir_variable_mode modes)1759 nir_deref_mode_may_be(const nir_deref_instr *deref, nir_variable_mode modes)
1760 {
1761 assert(!(modes & ~nir_var_all));
1762 assert(deref->modes != 0);
1763 return deref->modes & modes;
1764 }
1765
1766 /** Returns true if deref must have one of the given modes
1767 *
1768 * For multi-mode derefs, this returns true if NIR can prove that the given
1769 * deref has one of the specified modes. This function returning false does
1770 * NOT mean that deref doesn't have one of the given mode. It very well may
1771 * have one of those modes, we just don't have enough information to prove
1772 * that it does for sure.
1773 */
1774 static inline bool
nir_deref_mode_must_be(const nir_deref_instr * deref,nir_variable_mode modes)1775 nir_deref_mode_must_be(const nir_deref_instr *deref, nir_variable_mode modes)
1776 {
1777 assert(!(modes & ~nir_var_all));
1778 assert(deref->modes != 0);
1779 return !(deref->modes & ~modes);
1780 }
1781
1782 /** Returns true if deref has the given mode
1783 *
1784 * This returns true if the deref has exactly the mode specified. If the
1785 * deref may have that mode but may also have a different mode (i.e. modes has
1786 * multiple bits set), this will assert-fail.
1787 *
1788 * If you're confused about which nir_deref_mode_ helper to use, use this one
1789 * or nir_deref_mode_is_one_of below.
1790 */
1791 static inline bool
nir_deref_mode_is(const nir_deref_instr * deref,nir_variable_mode mode)1792 nir_deref_mode_is(const nir_deref_instr *deref, nir_variable_mode mode)
1793 {
1794 assert(util_bitcount(mode) == 1 && (mode & nir_var_all));
1795 assert(deref->modes != 0);
1796
1797 /* This is only for "simple" cases so, if modes might interact with this
1798 * deref then the deref has to have a single mode.
1799 */
1800 if (nir_deref_mode_may_be(deref, mode)) {
1801 assert(util_bitcount(deref->modes) == 1);
1802 assert(deref->modes == mode);
1803 }
1804
1805 return deref->modes == mode;
1806 }
1807
1808 /** Returns true if deref has one of the given modes
1809 *
1810 * This returns true if the deref has exactly one possible mode and that mode
1811 * is one of the modes specified. If the deref may have one of those modes
1812 * but may also have a different mode (i.e. modes has multiple bits set), this
1813 * will assert-fail.
1814 */
1815 static inline bool
nir_deref_mode_is_one_of(const nir_deref_instr * deref,nir_variable_mode modes)1816 nir_deref_mode_is_one_of(const nir_deref_instr *deref, nir_variable_mode modes)
1817 {
1818 /* This is only for "simple" cases so, if modes might interact with this
1819 * deref then the deref has to have a single mode.
1820 */
1821 if (nir_deref_mode_may_be(deref, modes)) {
1822 assert(util_bitcount(deref->modes) == 1);
1823 assert(nir_deref_mode_must_be(deref, modes));
1824 }
1825
1826 return nir_deref_mode_may_be(deref, modes);
1827 }
1828
1829 /** Returns true if deref's possible modes lie in the given set of modes
1830 *
1831 * This returns true if the deref's modes lie in the given set of modes. If
1832 * the deref's modes overlap with the specified modes but aren't entirely
1833 * contained in the specified set of modes, this will assert-fail. In
1834 * particular, if this is used in a generic pointers scenario, the specified
1835 * modes has to contain all or none of the possible generic pointer modes.
1836 *
1837 * This is intended mostly for mass-lowering of derefs which might have
1838 * generic pointers.
1839 */
1840 static inline bool
nir_deref_mode_is_in_set(const nir_deref_instr * deref,nir_variable_mode modes)1841 nir_deref_mode_is_in_set(const nir_deref_instr *deref, nir_variable_mode modes)
1842 {
1843 if (nir_deref_mode_may_be(deref, modes))
1844 assert(nir_deref_mode_must_be(deref, modes));
1845
1846 return nir_deref_mode_may_be(deref, modes);
1847 }
1848
1849 static inline nir_deref_instr *nir_src_as_deref(nir_src src);
1850
1851 static inline nir_deref_instr *
nir_deref_instr_parent(const nir_deref_instr * instr)1852 nir_deref_instr_parent(const nir_deref_instr *instr)
1853 {
1854 if (instr->deref_type == nir_deref_type_var)
1855 return NULL;
1856 else
1857 return nir_src_as_deref(instr->parent);
1858 }
1859
1860 static inline nir_variable *
nir_deref_instr_get_variable(const nir_deref_instr * instr)1861 nir_deref_instr_get_variable(const nir_deref_instr *instr)
1862 {
1863 while (instr->deref_type != nir_deref_type_var) {
1864 if (instr->deref_type == nir_deref_type_cast)
1865 return NULL;
1866
1867 instr = nir_deref_instr_parent(instr);
1868 }
1869
1870 return instr->var;
1871 }
1872
1873 bool nir_deref_instr_has_indirect(nir_deref_instr *instr);
1874 bool nir_deref_instr_is_known_out_of_bounds(nir_deref_instr *instr);
1875
1876 typedef enum {
1877 nir_deref_instr_has_complex_use_allow_memcpy_src = (1 << 0),
1878 nir_deref_instr_has_complex_use_allow_memcpy_dst = (1 << 1),
1879 nir_deref_instr_has_complex_use_allow_atomics = (1 << 2),
1880 } nir_deref_instr_has_complex_use_options;
1881
1882 bool nir_deref_instr_has_complex_use(nir_deref_instr *instr,
1883 nir_deref_instr_has_complex_use_options opts);
1884
1885 bool nir_deref_instr_remove_if_unused(nir_deref_instr *instr);
1886
1887 unsigned nir_deref_instr_array_stride(nir_deref_instr *instr);
1888
1889 typedef struct {
1890 nir_instr instr;
1891
1892 struct nir_function *callee;
1893
1894 unsigned num_params;
1895 nir_src params[];
1896 } nir_call_instr;
1897
1898 #include "nir_intrinsics.h"
1899
1900 #define NIR_INTRINSIC_MAX_CONST_INDEX 8
1901
1902 /** Represents an intrinsic
1903 *
1904 * An intrinsic is an instruction type for handling things that are
1905 * more-or-less regular operations but don't just consume and produce SSA
1906 * values like ALU operations do. Intrinsics are not for things that have
1907 * special semantic meaning such as phi nodes and parallel copies.
1908 * Examples of intrinsics include variable load/store operations, system
1909 * value loads, and the like. Even though texturing more-or-less falls
1910 * under this category, texturing is its own instruction type because
1911 * trying to represent texturing with intrinsics would lead to a
1912 * combinatorial explosion of intrinsic opcodes.
1913 *
1914 * By having a single instruction type for handling a lot of different
1915 * cases, optimization passes can look for intrinsics and, for the most
1916 * part, completely ignore them. Each intrinsic type also has a few
1917 * possible flags that govern whether or not they can be reordered or
1918 * eliminated. That way passes like dead code elimination can still work
1919 * on intrisics without understanding the meaning of each.
1920 *
1921 * Each intrinsic has some number of constant indices, some number of
1922 * variables, and some number of sources. What these sources, variables,
1923 * and indices mean depends on the intrinsic and is documented with the
1924 * intrinsic declaration in nir_intrinsics.h. Intrinsics and texture
1925 * instructions are the only types of instruction that can operate on
1926 * variables.
1927 */
1928 typedef struct {
1929 nir_instr instr;
1930
1931 nir_intrinsic_op intrinsic;
1932
1933 nir_def def;
1934
1935 /** number of components if this is a vectorized intrinsic
1936 *
1937 * Similarly to ALU operations, some intrinsics are vectorized.
1938 * An intrinsic is vectorized if nir_intrinsic_infos.dest_components == 0.
1939 * For vectorized intrinsics, the num_components field specifies the
1940 * number of destination components and the number of source components
1941 * for all sources with nir_intrinsic_infos.src_components[i] == 0.
1942 */
1943 uint8_t num_components;
1944
1945 int const_index[NIR_INTRINSIC_MAX_CONST_INDEX];
1946
1947 /* a variable name associated with this instr; cannot be modified or freed */
1948 const char *name;
1949
1950 nir_src src[];
1951 } nir_intrinsic_instr;
1952
1953 static inline nir_variable *
nir_intrinsic_get_var(const nir_intrinsic_instr * intrin,unsigned i)1954 nir_intrinsic_get_var(const nir_intrinsic_instr *intrin, unsigned i)
1955 {
1956 return nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[i]));
1957 }
1958
1959 typedef enum {
1960 /* Memory ordering. */
1961 NIR_MEMORY_ACQUIRE = 1 << 0,
1962 NIR_MEMORY_RELEASE = 1 << 1,
1963 NIR_MEMORY_ACQ_REL = NIR_MEMORY_ACQUIRE | NIR_MEMORY_RELEASE,
1964
1965 /* Memory visibility operations. */
1966 NIR_MEMORY_MAKE_AVAILABLE = 1 << 2,
1967 NIR_MEMORY_MAKE_VISIBLE = 1 << 3,
1968 } nir_memory_semantics;
1969
1970 /**
1971 * NIR intrinsics semantic flags
1972 *
1973 * information about what the compiler can do with the intrinsics.
1974 *
1975 * :c:member:`nir_intrinsic_info.flags`
1976 */
1977 typedef enum {
1978 /**
1979 * whether the intrinsic can be safely eliminated if none of its output
1980 * value is not being used.
1981 */
1982 NIR_INTRINSIC_CAN_ELIMINATE = (1 << 0),
1983
1984 /**
1985 * Whether the intrinsic can be reordered with respect to any other
1986 * intrinsic, i.e. whether the only reordering dependencies of the
1987 * intrinsic are due to the register reads/writes.
1988 */
1989 NIR_INTRINSIC_CAN_REORDER = (1 << 1),
1990 } nir_intrinsic_semantic_flag;
1991
1992 /**
1993 * Maximum valid value for a nir align_mul value (in intrinsics or derefs).
1994 *
1995 * Offsets can be signed, so this is the largest power of two in int32_t.
1996 */
1997 #define NIR_ALIGN_MUL_MAX 0x40000000
1998
1999 typedef struct nir_io_semantics {
2000 unsigned location : 7; /* gl_vert_attrib, gl_varying_slot, or gl_frag_result */
2001 unsigned num_slots : 6; /* max 32, may be pessimistic with const indexing */
2002 unsigned dual_source_blend_index : 1;
2003 unsigned fb_fetch_output : 1; /* for GL_KHR_blend_equation_advanced */
2004 unsigned gs_streams : 8; /* xxyyzzww: 2-bit stream index for each component */
2005 unsigned medium_precision : 1; /* GLSL mediump qualifier */
2006 unsigned per_view : 1;
2007 unsigned high_16bits : 1; /* whether accessing low or high half of the slot */
2008 unsigned invariant : 1; /* The variable has the invariant flag set */
2009 unsigned high_dvec2 : 1; /* whether accessing the high half of dvec3/dvec4 */
2010 /* CLIP_DISTn, LAYER, VIEWPORT, and TESS_LEVEL_* have up to 3 uses:
2011 * - an output consumed by the next stage
2012 * - a system value output affecting fixed-func hardware, e.g. the clipper
2013 * - a transform feedback output written to memory
2014 * The following fields disable the first two. Transform feedback is disabled
2015 * by transform feedback info.
2016 */
2017 unsigned no_varying : 1; /* whether this output isn't consumed by the next stage */
2018 unsigned no_sysval_output : 1; /* whether this system value output has no
2019 effect due to current pipeline states */
2020 unsigned interp_explicit_strict : 1; /* preserve original vertex order */
2021 unsigned _pad : 1;
2022 } nir_io_semantics;
2023
2024 /* Transform feedback info for 2 outputs. nir_intrinsic_store_output contains
2025 * this structure twice to support up to 4 outputs. The structure is limited
2026 * to 32 bits because it's stored in nir_intrinsic_instr::const_index[].
2027 */
2028 typedef struct nir_io_xfb {
2029 struct {
2030 /* start_component is equal to the index of out[]; add 2 for io_xfb2 */
2031 /* start_component is not relative to nir_intrinsic_component */
2032 /* get the stream index from nir_io_semantics */
2033 uint8_t num_components : 4; /* max 4; if this is 0, xfb is disabled */
2034 uint8_t buffer : 4; /* buffer index, max 3 */
2035 uint8_t offset; /* transform feedback buffer offset in dwords,
2036 max (1K - 4) bytes */
2037 } out[2];
2038 } nir_io_xfb;
2039
2040 unsigned
2041 nir_instr_xfb_write_mask(nir_intrinsic_instr *instr);
2042
2043 #define NIR_INTRINSIC_MAX_INPUTS 11
2044
2045 typedef struct {
2046 const char *name;
2047
2048 /** number of register/SSA inputs */
2049 uint8_t num_srcs;
2050
2051 /** number of components of each input register
2052 *
2053 * If this value is 0, the number of components is given by the
2054 * num_components field of nir_intrinsic_instr. If this value is -1, the
2055 * intrinsic consumes however many components are provided and it is not
2056 * validated at all.
2057 */
2058 int8_t src_components[NIR_INTRINSIC_MAX_INPUTS];
2059
2060 bool has_dest;
2061
2062 /** number of components of the output register
2063 *
2064 * If this value is 0, the number of components is given by the
2065 * num_components field of nir_intrinsic_instr.
2066 */
2067 uint8_t dest_components;
2068
2069 /** bitfield of legal bit sizes */
2070 uint8_t dest_bit_sizes;
2071
2072 /** source which the destination bit size must match
2073 *
2074 * Some intrinsics, such as subgroup intrinsics, are data manipulation
2075 * intrinsics and they have similar bit-size rules to ALU ops. This enables
2076 * validation to validate a bit more and enables auto-generated builder code
2077 * to properly determine destination bit sizes automatically.
2078 */
2079 int8_t bit_size_src;
2080
2081 /** the number of constant indices used by the intrinsic */
2082 uint8_t num_indices;
2083
2084 /** list of indices */
2085 uint8_t indices[NIR_INTRINSIC_MAX_CONST_INDEX];
2086
2087 /** indicates the usage of intr->const_index[n] */
2088 uint8_t index_map[NIR_INTRINSIC_NUM_INDEX_FLAGS];
2089
2090 /** semantic flags for calls to this intrinsic */
2091 nir_intrinsic_semantic_flag flags;
2092 } nir_intrinsic_info;
2093
2094 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
2095
2096 unsigned
2097 nir_intrinsic_src_components(const nir_intrinsic_instr *intr, unsigned srcn);
2098
2099 unsigned
2100 nir_intrinsic_dest_components(nir_intrinsic_instr *intr);
2101
2102 nir_alu_type
2103 nir_intrinsic_instr_src_type(const nir_intrinsic_instr *intrin, unsigned src);
2104
2105 nir_alu_type
2106 nir_intrinsic_instr_dest_type(const nir_intrinsic_instr *intrin);
2107
2108 /**
2109 * Helper to copy const_index[] from src to dst, without assuming they
2110 * match in order.
2111 */
2112 void nir_intrinsic_copy_const_indices(nir_intrinsic_instr *dst, nir_intrinsic_instr *src);
2113
2114 #include "nir_intrinsics_indices.h"
2115
2116 static inline void
nir_intrinsic_set_align(nir_intrinsic_instr * intrin,unsigned align_mul,unsigned align_offset)2117 nir_intrinsic_set_align(nir_intrinsic_instr *intrin,
2118 unsigned align_mul, unsigned align_offset)
2119 {
2120 assert(util_is_power_of_two_nonzero(align_mul));
2121 assert(align_offset < align_mul);
2122 nir_intrinsic_set_align_mul(intrin, align_mul);
2123 nir_intrinsic_set_align_offset(intrin, align_offset);
2124 }
2125
2126 /** Returns a simple alignment for an align_mul/offset pair
2127 *
2128 * This helper converts from the full mul+offset alignment scheme used by
2129 * most NIR intrinsics to a simple alignment. The returned value is the
2130 * largest power of two which divides both align_mul and align_offset.
2131 * For any offset X which satisfies the complex alignment described by
2132 * align_mul/offset, X % align == 0.
2133 */
2134 static inline uint32_t
nir_combined_align(uint32_t align_mul,uint32_t align_offset)2135 nir_combined_align(uint32_t align_mul, uint32_t align_offset)
2136 {
2137 assert(util_is_power_of_two_nonzero(align_mul));
2138 assert(align_offset < align_mul);
2139 return align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
2140 }
2141
2142 /** Returns a simple alignment for a load/store intrinsic offset
2143 *
2144 * Instead of the full mul+offset alignment scheme provided by the ALIGN_MUL
2145 * and ALIGN_OFFSET parameters, this helper takes both into account and
2146 * provides a single simple alignment parameter. The offset X is guaranteed
2147 * to satisfy X % align == 0.
2148 */
2149 static inline unsigned
nir_intrinsic_align(const nir_intrinsic_instr * intrin)2150 nir_intrinsic_align(const nir_intrinsic_instr *intrin)
2151 {
2152 return nir_combined_align(nir_intrinsic_align_mul(intrin),
2153 nir_intrinsic_align_offset(intrin));
2154 }
2155
2156 static inline bool
nir_intrinsic_has_align(const nir_intrinsic_instr * intrin)2157 nir_intrinsic_has_align(const nir_intrinsic_instr *intrin)
2158 {
2159 return nir_intrinsic_has_align_mul(intrin) &&
2160 nir_intrinsic_has_align_offset(intrin);
2161 }
2162
2163 unsigned
2164 nir_image_intrinsic_coord_components(const nir_intrinsic_instr *instr);
2165
2166 /* Converts a image_deref_* intrinsic into a image_* one */
2167 void nir_rewrite_image_intrinsic(nir_intrinsic_instr *instr,
2168 nir_def *handle, bool bindless);
2169
2170 /* Determine if an intrinsic can be arbitrarily reordered and eliminated. */
2171 static inline bool
nir_intrinsic_can_reorder(nir_intrinsic_instr * instr)2172 nir_intrinsic_can_reorder(nir_intrinsic_instr *instr)
2173 {
2174 if (nir_intrinsic_has_access(instr) &&
2175 nir_intrinsic_access(instr) & ACCESS_VOLATILE)
2176 return false;
2177
2178 if (instr->intrinsic == nir_intrinsic_load_deref) {
2179 nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
2180 return nir_deref_mode_is_in_set(deref, nir_var_read_only_modes) ||
2181 (nir_intrinsic_access(instr) & ACCESS_CAN_REORDER);
2182 } else if (instr->intrinsic == nir_intrinsic_load_ssbo ||
2183 instr->intrinsic == nir_intrinsic_bindless_image_load ||
2184 instr->intrinsic == nir_intrinsic_image_deref_load ||
2185 instr->intrinsic == nir_intrinsic_image_load ||
2186 instr->intrinsic == nir_intrinsic_ald_nv ||
2187 instr->intrinsic == nir_intrinsic_load_sysval_nv) {
2188 return nir_intrinsic_access(instr) & ACCESS_CAN_REORDER;
2189 } else {
2190 const nir_intrinsic_info *info =
2191 &nir_intrinsic_infos[instr->intrinsic];
2192 return (info->flags & NIR_INTRINSIC_CAN_ELIMINATE) &&
2193 (info->flags & NIR_INTRINSIC_CAN_REORDER);
2194 }
2195 }
2196
2197 bool nir_intrinsic_writes_external_memory(const nir_intrinsic_instr *instr);
2198
2199 static inline bool
nir_intrinsic_is_ray_query(nir_intrinsic_op intrinsic)2200 nir_intrinsic_is_ray_query(nir_intrinsic_op intrinsic)
2201 {
2202 switch (intrinsic) {
2203 case nir_intrinsic_rq_confirm_intersection:
2204 case nir_intrinsic_rq_generate_intersection:
2205 case nir_intrinsic_rq_initialize:
2206 case nir_intrinsic_rq_load:
2207 case nir_intrinsic_rq_proceed:
2208 case nir_intrinsic_rq_terminate:
2209 return true;
2210 default:
2211 return false;
2212 }
2213 }
2214
2215 /** Texture instruction source type */
2216 typedef enum nir_tex_src_type {
2217 /** Texture coordinate
2218 *
2219 * Must have :c:member:`nir_tex_instr.coord_components` components.
2220 */
2221 nir_tex_src_coord,
2222
2223 /** Projector
2224 *
2225 * The texture coordinate (except for the array component, if any) is
2226 * divided by this value before LOD computation and sampling.
2227 *
2228 * Must be a float scalar.
2229 */
2230 nir_tex_src_projector,
2231
2232 /** Shadow comparator
2233 *
2234 * For shadow sampling, the fetched texel values are compared against the
2235 * shadow comparator using the compare op specified by the sampler object
2236 * and converted to 1.0 if the comparison succeeds and 0.0 if it fails.
2237 * Interpolation happens after this conversion so the actual result may be
2238 * anywhere in the range [0.0, 1.0].
2239 *
2240 * Only valid if :c:member:`nir_tex_instr.is_shadow` and must be a float
2241 * scalar.
2242 */
2243 nir_tex_src_comparator,
2244
2245 /** Coordinate offset
2246 *
2247 * An integer value that is added to the texel address before sampling.
2248 * This is only allowed with operations that take an explicit LOD as it is
2249 * applied in integer texel space after LOD selection and not normalized
2250 * coordinate space.
2251 */
2252 nir_tex_src_offset,
2253
2254 /** LOD bias
2255 *
2256 * This value is added to the computed LOD before mip-mapping.
2257 */
2258 nir_tex_src_bias,
2259
2260 /** Explicit LOD */
2261 nir_tex_src_lod,
2262
2263 /** Min LOD
2264 *
2265 * The computed LOD is clamped to be at least as large as min_lod before
2266 * mip-mapping.
2267 */
2268 nir_tex_src_min_lod,
2269
2270 /** MSAA sample index */
2271 nir_tex_src_ms_index,
2272
2273 /** Intel-specific MSAA compression data */
2274 nir_tex_src_ms_mcs_intel,
2275
2276 /** Explicit horizontal (X-major) coordinate derivative */
2277 nir_tex_src_ddx,
2278
2279 /** Explicit vertical (Y-major) coordinate derivative */
2280 nir_tex_src_ddy,
2281
2282 /** Texture variable dereference */
2283 nir_tex_src_texture_deref,
2284
2285 /** Sampler variable dereference */
2286 nir_tex_src_sampler_deref,
2287
2288 /** Texture index offset
2289 *
2290 * This is added to :c:member:`nir_tex_instr.texture_index`. Unless
2291 * :c:member:`nir_tex_instr.texture_non_uniform` is set, this is guaranteed
2292 * to be dynamically uniform.
2293 */
2294 nir_tex_src_texture_offset,
2295
2296 /** Dynamically uniform sampler index offset
2297 *
2298 * This is added to :c:member:`nir_tex_instr.sampler_index`. Unless
2299 * :c:member:`nir_tex_instr.sampler_non_uniform` is set, this is guaranteed to be
2300 * dynamically uniform. This should not be present until GLSL ES 3.20, GLSL
2301 * 4.00, or ARB_gpu_shader5, because in ES 3.10 and GL 3.30 samplers said
2302 * "When aggregated into arrays within a shader, samplers can only be indexed
2303 * with a constant integral expression."
2304 */
2305 nir_tex_src_sampler_offset,
2306
2307 /** Bindless texture handle
2308 *
2309 * This is, unfortunately, a bit overloaded at the moment. There are
2310 * generally two types of bindless handles:
2311 *
2312 * 1. For GL_ARB_bindless bindless handles. These are part of the
2313 * GL/Gallium-level API and are always a 64-bit integer.
2314 *
2315 * 2. HW-specific handles. GL_ARB_bindless handles may be lowered to
2316 * these. Also, these are used by many Vulkan drivers to implement
2317 * descriptor sets, especially for UPDATE_AFTER_BIND descriptors.
2318 * The details of hardware handles (bit size, format, etc.) is
2319 * HW-specific.
2320 *
2321 * Because of this overloading and the resulting ambiguity, we currently
2322 * don't validate anything for these.
2323 */
2324 nir_tex_src_texture_handle,
2325
2326 /** Bindless sampler handle
2327 *
2328 * See nir_tex_src_texture_handle,
2329 */
2330 nir_tex_src_sampler_handle,
2331
2332 /** Tex src intrinsic
2333 *
2334 * This is an intrinsic used before function inlining i.e. before we know
2335 * if a bindless value has been given as function param for use as a tex
2336 * src.
2337 */
2338 nir_tex_src_sampler_deref_intrinsic,
2339 nir_tex_src_texture_deref_intrinsic,
2340
2341 /** Plane index for multi-plane YCbCr textures */
2342 nir_tex_src_plane,
2343
2344 /**
2345 * Backend-specific vec4 tex src argument.
2346 *
2347 * Can be used to have NIR optimization (copy propagation, lower_vec_to_regs)
2348 * apply to the packing of the tex srcs. This lowering must only happen
2349 * after nir_lower_tex().
2350 *
2351 * The nir_tex_instr_src_type() of this argument is float, so no lowering
2352 * will happen if nir_lower_int_to_float is used.
2353 */
2354 nir_tex_src_backend1,
2355
2356 /** Second backend-specific vec4 tex src argument, see nir_tex_src_backend1. */
2357 nir_tex_src_backend2,
2358
2359 nir_num_tex_src_types
2360 } nir_tex_src_type;
2361
2362 /** A texture instruction source */
2363 typedef struct nir_tex_src {
2364 /** Base source */
2365 nir_src src;
2366
2367 /** Type of this source */
2368 nir_tex_src_type src_type;
2369 } nir_tex_src;
2370
2371 /** Texture instruction opcode */
2372 typedef enum nir_texop {
2373 /** Regular texture look-up */
2374 nir_texop_tex,
2375 /** Texture look-up with LOD bias */
2376 nir_texop_txb,
2377 /** Texture look-up with explicit LOD */
2378 nir_texop_txl,
2379 /** Texture look-up with partial derivatives */
2380 nir_texop_txd,
2381 /** Texel fetch with explicit LOD */
2382 nir_texop_txf,
2383 /** Multisample texture fetch */
2384 nir_texop_txf_ms,
2385 /** Multisample texture fetch from framebuffer */
2386 nir_texop_txf_ms_fb,
2387 /** Multisample compression value fetch */
2388 nir_texop_txf_ms_mcs_intel,
2389 /** Texture size */
2390 nir_texop_txs,
2391 /** Texture lod query */
2392 nir_texop_lod,
2393 /** Texture gather */
2394 nir_texop_tg4,
2395 /** Texture levels query */
2396 nir_texop_query_levels,
2397 /** Texture samples query */
2398 nir_texop_texture_samples,
2399 /** Query whether all samples are definitely identical. */
2400 nir_texop_samples_identical,
2401 /** Regular texture look-up, eligible for pre-dispatch */
2402 nir_texop_tex_prefetch,
2403 /** Multisample fragment color texture fetch */
2404 nir_texop_fragment_fetch_amd,
2405 /** Multisample fragment mask texture fetch */
2406 nir_texop_fragment_mask_fetch_amd,
2407 /** Returns a buffer or image descriptor. */
2408 nir_texop_descriptor_amd,
2409 /** Returns a sampler descriptor. */
2410 nir_texop_sampler_descriptor_amd,
2411 /** Returns the sampler's LOD bias */
2412 nir_texop_lod_bias_agx,
2413 /** Returns a bool indicating that the sampler uses a custom border colour */
2414 nir_texop_has_custom_border_color_agx,
2415 /** Returns the sampler's custom border colour (if has_custom_border_agx) */
2416 nir_texop_custom_border_color_agx,
2417 /** Maps to TXQ.DIMENSION */
2418 nir_texop_hdr_dim_nv,
2419 /** Maps to TXQ.TEXTURE_TYPE */
2420 nir_texop_tex_type_nv,
2421 } nir_texop;
2422
2423 /** Represents a texture instruction */
2424 typedef struct nir_tex_instr {
2425 /** Base instruction */
2426 nir_instr instr;
2427
2428 /** Dimensionality of the texture operation
2429 *
2430 * This will typically match the dimensionality of the texture deref type
2431 * if a nir_tex_src_texture_deref is present. However, it may not if
2432 * texture lowering has occurred.
2433 */
2434 enum glsl_sampler_dim sampler_dim;
2435
2436 /** ALU type of the destination
2437 *
2438 * This is the canonical sampled type for this texture operation and may
2439 * not exactly match the sampled type of the deref type when a
2440 * nir_tex_src_texture_deref is present. For OpenCL, the sampled type of
2441 * the texture deref will be GLSL_TYPE_VOID and this is allowed to be
2442 * anything. With SPIR-V, the signedness of integer types is allowed to
2443 * differ. For all APIs, the bit size may differ if the driver has done
2444 * any sort of mediump or similar lowering since texture types always have
2445 * 32-bit sampled types.
2446 */
2447 nir_alu_type dest_type;
2448
2449 /** Texture opcode */
2450 nir_texop op;
2451
2452 /** Destination */
2453 nir_def def;
2454
2455 /** Array of sources
2456 *
2457 * This array has :c:member:`nir_tex_instr.num_srcs` elements
2458 */
2459 nir_tex_src *src;
2460
2461 /** Number of sources */
2462 unsigned num_srcs;
2463
2464 /** Number of components in the coordinate, if any */
2465 unsigned coord_components;
2466
2467 /** True if the texture instruction acts on an array texture */
2468 bool is_array;
2469
2470 /** True if the texture instruction performs a shadow comparison
2471 *
2472 * If this is true, the texture instruction must have a
2473 * nir_tex_src_comparator.
2474 */
2475 bool is_shadow;
2476
2477 /**
2478 * If is_shadow is true, whether this is the old-style shadow that outputs
2479 * 4 components or the new-style shadow that outputs 1 component.
2480 */
2481 bool is_new_style_shadow;
2482
2483 /**
2484 * True if this texture instruction should return a sparse residency code.
2485 * The code is in the last component of the result.
2486 */
2487 bool is_sparse;
2488
2489 /** nir_texop_tg4 component selector
2490 *
2491 * This determines which RGBA component is gathered.
2492 */
2493 unsigned component : 2;
2494
2495 /** Validation needs to know this for gradient component count */
2496 unsigned array_is_lowered_cube : 1;
2497
2498 /** True if this tg4 instruction has an implicit LOD or LOD bias, instead of using level 0 */
2499 unsigned is_gather_implicit_lod : 1;
2500
2501 /** Gather offsets */
2502 int8_t tg4_offsets[4][2];
2503
2504 /** True if the texture index or handle is not dynamically uniform */
2505 bool texture_non_uniform;
2506
2507 /** True if the sampler index or handle is not dynamically uniform.
2508 *
2509 * This may be set when VK_EXT_descriptor_indexing is supported and the
2510 * appropriate capability is enabled.
2511 *
2512 * This should always be false in GLSL (GLSL ES 3.20 says "When aggregated
2513 * into arrays within a shader, opaque types can only be indexed with a
2514 * dynamically uniform integral expression", and GLSL 4.60 says "When
2515 * aggregated into arrays within a shader, [texture, sampler, and
2516 * samplerShadow] types can only be indexed with a dynamically uniform
2517 * expression, or texture lookup will result in undefined values.").
2518 */
2519 bool sampler_non_uniform;
2520
2521 /** The texture index
2522 *
2523 * If this texture instruction has a nir_tex_src_texture_offset source,
2524 * then the texture index is given by texture_index + texture_offset.
2525 */
2526 unsigned texture_index;
2527
2528 /** The sampler index
2529 *
2530 * The following operations do not require a sampler and, as such, this
2531 * field should be ignored:
2532 *
2533 * - nir_texop_txf
2534 * - nir_texop_txf_ms
2535 * - nir_texop_txs
2536 * - nir_texop_query_levels
2537 * - nir_texop_texture_samples
2538 * - nir_texop_samples_identical
2539 *
2540 * If this texture instruction has a nir_tex_src_sampler_offset source,
2541 * then the sampler index is given by sampler_index + sampler_offset.
2542 */
2543 unsigned sampler_index;
2544
2545 /* Back-end specific flags, intended to be used in combination with
2546 * nir_tex_src_backend1/2 to provide additional hw-specific information
2547 * to the back-end compiler.
2548 */
2549 uint32_t backend_flags;
2550 } nir_tex_instr;
2551
2552 /**
2553 * Returns true if the texture operation requires a sampler as a general rule
2554 *
2555 * Note that the specific hw/driver backend could require to a sampler
2556 * object/configuration packet in any case, for some other reason.
2557 *
2558 * See also :c:member:`nir_tex_instr.sampler_index`.
2559 */
2560 bool nir_tex_instr_need_sampler(const nir_tex_instr *instr);
2561
2562 /** Returns the number of components returned by this nir_tex_instr
2563 *
2564 * Useful for code building texture instructions when you don't want to think
2565 * about how many components a particular texture op returns. This does not
2566 * include the sparse residency code.
2567 */
2568 unsigned
2569 nir_tex_instr_result_size(const nir_tex_instr *instr);
2570
2571 /**
2572 * Returns the destination size of this nir_tex_instr including the sparse
2573 * residency code, if any.
2574 */
2575 static inline unsigned
nir_tex_instr_dest_size(const nir_tex_instr * instr)2576 nir_tex_instr_dest_size(const nir_tex_instr *instr)
2577 {
2578 /* One more component is needed for the residency code. */
2579 return nir_tex_instr_result_size(instr) + instr->is_sparse;
2580 }
2581
2582 /**
2583 * Returns true if this texture operation queries something about the texture
2584 * rather than actually sampling it.
2585 */
2586 bool
2587 nir_tex_instr_is_query(const nir_tex_instr *instr);
2588
2589 /** Returns true if this texture instruction does implicit derivatives
2590 *
2591 * This is important as there are extra control-flow rules around derivatives
2592 * and texture instructions which perform them implicitly.
2593 */
2594 bool
2595 nir_tex_instr_has_implicit_derivative(const nir_tex_instr *instr);
2596
2597 /** Returns the ALU type of the given texture instruction source */
2598 nir_alu_type
2599 nir_tex_instr_src_type(const nir_tex_instr *instr, unsigned src);
2600
2601 /**
2602 * Returns the number of components required by the given texture instruction
2603 * source
2604 */
2605 unsigned
2606 nir_tex_instr_src_size(const nir_tex_instr *instr, unsigned src);
2607
2608 /**
2609 * Returns the index of the texture instruction source with the given
2610 * nir_tex_src_type or -1 if no such source exists.
2611 */
2612 static inline int
nir_tex_instr_src_index(const nir_tex_instr * instr,nir_tex_src_type type)2613 nir_tex_instr_src_index(const nir_tex_instr *instr, nir_tex_src_type type)
2614 {
2615 for (unsigned i = 0; i < instr->num_srcs; i++)
2616 if (instr->src[i].src_type == type)
2617 return (int)i;
2618
2619 return -1;
2620 }
2621
2622 /** Adds a source to a texture instruction */
2623 void nir_tex_instr_add_src(nir_tex_instr *tex,
2624 nir_tex_src_type src_type,
2625 nir_def *src);
2626
2627 /** Removes a source from a texture instruction */
2628 void nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx);
2629
2630 bool nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr *tex);
2631
2632 typedef struct {
2633 nir_instr instr;
2634
2635 nir_def def;
2636
2637 nir_const_value value[];
2638 } nir_load_const_instr;
2639
2640 typedef enum {
2641 /** Return from a function
2642 *
2643 * This instruction is a classic function return. It jumps to
2644 * nir_function_impl::end_block. No return value is provided in this
2645 * instruction. Instead, the function is expected to write any return
2646 * data to a deref passed in from the caller.
2647 */
2648 nir_jump_return,
2649
2650 /** Immediately exit the current shader
2651 *
2652 * This instruction is roughly the equivalent of C's "exit()" in that it
2653 * immediately terminates the current shader invocation. From a CFG
2654 * perspective, it looks like a jump to nir_function_impl::end_block but
2655 * it actually jumps to the end block of the shader entrypoint. A halt
2656 * instruction in the shader entrypoint itself is semantically identical
2657 * to a return.
2658 *
2659 * For shaders with built-in I/O, any outputs written prior to a halt
2660 * instruction remain written and any outputs not written prior to the
2661 * halt have undefined values. It does NOT cause an implicit discard of
2662 * written results. If one wants discard results in a fragment shader,
2663 * for instance, a discard or demote intrinsic is required.
2664 */
2665 nir_jump_halt,
2666
2667 /** Break out of the inner-most loop
2668 *
2669 * This has the same semantics as C's "break" statement.
2670 */
2671 nir_jump_break,
2672
2673 /** Jump back to the top of the inner-most loop
2674 *
2675 * This has the same semantics as C's "continue" statement assuming that a
2676 * NIR loop is implemented as "while (1) { body }".
2677 */
2678 nir_jump_continue,
2679
2680 /** Jumps for unstructured CFG.
2681 *
2682 * As within an unstructured CFG we can't rely on block ordering we need to
2683 * place explicit jumps at the end of every block.
2684 */
2685 nir_jump_goto,
2686 nir_jump_goto_if,
2687 } nir_jump_type;
2688
2689 typedef struct {
2690 nir_instr instr;
2691 nir_jump_type type;
2692 nir_src condition;
2693 struct nir_block *target;
2694 struct nir_block *else_target;
2695 } nir_jump_instr;
2696
2697 /* creates a new SSA variable in an undefined state */
2698
2699 typedef struct {
2700 nir_instr instr;
2701 nir_def def;
2702 } nir_undef_instr;
2703
2704 typedef struct {
2705 struct exec_node node;
2706
2707 /* The predecessor block corresponding to this source */
2708 struct nir_block *pred;
2709
2710 nir_src src;
2711 } nir_phi_src;
2712
2713 #define nir_foreach_phi_src(phi_src, phi) \
2714 foreach_list_typed(nir_phi_src, phi_src, node, &(phi)->srcs)
2715 #define nir_foreach_phi_src_safe(phi_src, phi) \
2716 foreach_list_typed_safe(nir_phi_src, phi_src, node, &(phi)->srcs)
2717
2718 typedef struct {
2719 nir_instr instr;
2720
2721 /** list of nir_phi_src */
2722 struct exec_list srcs;
2723
2724 nir_def def;
2725 } nir_phi_instr;
2726
2727 static inline nir_phi_src *
nir_phi_get_src_from_block(nir_phi_instr * phi,struct nir_block * block)2728 nir_phi_get_src_from_block(nir_phi_instr *phi, struct nir_block *block)
2729 {
2730 nir_foreach_phi_src(src, phi) {
2731 if (src->pred == block)
2732 return src;
2733 }
2734
2735 assert(!"Block is not a predecessor of phi.");
2736 return NULL;
2737 }
2738
2739 typedef struct {
2740 struct exec_node node;
2741 bool src_is_reg;
2742 bool dest_is_reg;
2743 nir_src src;
2744 union {
2745 nir_def def;
2746 nir_src reg;
2747 } dest;
2748 } nir_parallel_copy_entry;
2749
2750 #define nir_foreach_parallel_copy_entry(entry, pcopy) \
2751 foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
2752
2753 typedef struct {
2754 nir_instr instr;
2755
2756 /* A list of nir_parallel_copy_entrys. The sources of all of the
2757 * entries are copied to the corresponding destinations "in parallel".
2758 * In other words, if we have two entries: a -> b and b -> a, the values
2759 * get swapped.
2760 */
2761 struct exec_list entries;
2762 } nir_parallel_copy_instr;
2763
2764 typedef enum nir_debug_info_type {
2765 nir_debug_info_src_loc,
2766 nir_debug_info_string,
2767 } nir_debug_info_type;
2768
2769 typedef enum nir_debug_info_source {
2770 nir_debug_info_spirv,
2771 nir_debug_info_nir,
2772 } nir_debug_info_source;
2773
2774 typedef struct nir_debug_info_instr {
2775 nir_instr instr;
2776
2777 nir_debug_info_type type;
2778
2779 union {
2780 struct {
2781 nir_src filename;
2782 /* 0 if only the spirv_offset is available. */
2783 uint32_t line;
2784 uint32_t column;
2785
2786 uint32_t spirv_offset;
2787
2788 nir_debug_info_source source;
2789 } src_loc;
2790
2791 uint16_t string_length;
2792 };
2793
2794 nir_def def;
2795
2796 char string[];
2797 } nir_debug_info_instr;
2798
2799 NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr,
2800 type, nir_instr_type_alu)
2801 NIR_DEFINE_CAST(nir_instr_as_deref, nir_instr, nir_deref_instr, instr,
2802 type, nir_instr_type_deref)
2803 NIR_DEFINE_CAST(nir_instr_as_call, nir_instr, nir_call_instr, instr,
2804 type, nir_instr_type_call)
2805 NIR_DEFINE_CAST(nir_instr_as_jump, nir_instr, nir_jump_instr, instr,
2806 type, nir_instr_type_jump)
2807 NIR_DEFINE_CAST(nir_instr_as_tex, nir_instr, nir_tex_instr, instr,
2808 type, nir_instr_type_tex)
2809 NIR_DEFINE_CAST(nir_instr_as_intrinsic, nir_instr, nir_intrinsic_instr, instr,
2810 type, nir_instr_type_intrinsic)
2811 NIR_DEFINE_CAST(nir_instr_as_load_const, nir_instr, nir_load_const_instr, instr,
2812 type, nir_instr_type_load_const)
2813 NIR_DEFINE_CAST(nir_instr_as_undef, nir_instr, nir_undef_instr, instr,
2814 type, nir_instr_type_undef)
2815 NIR_DEFINE_CAST(nir_instr_as_phi, nir_instr, nir_phi_instr, instr,
2816 type, nir_instr_type_phi)
2817 NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
2818 nir_parallel_copy_instr, instr,
2819 type, nir_instr_type_parallel_copy)
2820 NIR_DEFINE_CAST(nir_instr_as_debug_info, nir_instr,
2821 nir_debug_info_instr, instr,
2822 type, nir_instr_type_debug_info)
2823
2824 #define NIR_DEFINE_SRC_AS_CONST(type, suffix) \
2825 static inline type \
2826 nir_src_comp_as_##suffix(nir_src src, unsigned comp) \
2827 { \
2828 assert(nir_src_is_const(src)); \
2829 nir_load_const_instr *load = \
2830 nir_instr_as_load_const(src.ssa->parent_instr); \
2831 assert(comp < load->def.num_components); \
2832 return nir_const_value_as_##suffix(load->value[comp], \
2833 load->def.bit_size); \
2834 } \
2835 \
2836 static inline type \
2837 nir_src_as_##suffix(nir_src src) \
2838 { \
2839 assert(nir_src_num_components(src) == 1); \
2840 return nir_src_comp_as_##suffix(src, 0); \
2841 }
2842
2843 NIR_DEFINE_SRC_AS_CONST(int64_t, int)
2844 NIR_DEFINE_SRC_AS_CONST(uint64_t, uint)
2845 NIR_DEFINE_SRC_AS_CONST(bool, bool)
2846 NIR_DEFINE_SRC_AS_CONST(double, float)
2847
2848 #undef NIR_DEFINE_SRC_AS_CONST
2849
2850 typedef struct {
2851 nir_def *def;
2852 unsigned comp;
2853 } nir_scalar;
2854
2855 static inline bool
nir_scalar_is_const(nir_scalar s)2856 nir_scalar_is_const(nir_scalar s)
2857 {
2858 return s.def->parent_instr->type == nir_instr_type_load_const;
2859 }
2860
2861 static inline bool
nir_scalar_is_undef(nir_scalar s)2862 nir_scalar_is_undef(nir_scalar s)
2863 {
2864 return s.def->parent_instr->type == nir_instr_type_undef;
2865 }
2866
2867 static inline nir_const_value
nir_scalar_as_const_value(nir_scalar s)2868 nir_scalar_as_const_value(nir_scalar s)
2869 {
2870 assert(s.comp < s.def->num_components);
2871 nir_load_const_instr *load = nir_instr_as_load_const(s.def->parent_instr);
2872 return load->value[s.comp];
2873 }
2874
2875 #define NIR_DEFINE_SCALAR_AS_CONST(type, suffix) \
2876 static inline type \
2877 nir_scalar_as_##suffix(nir_scalar s) \
2878 { \
2879 return nir_const_value_as_##suffix( \
2880 nir_scalar_as_const_value(s), s.def->bit_size); \
2881 }
2882
NIR_DEFINE_SCALAR_AS_CONST(int64_t,int)2883 NIR_DEFINE_SCALAR_AS_CONST(int64_t, int)
2884 NIR_DEFINE_SCALAR_AS_CONST(uint64_t, uint)
2885 NIR_DEFINE_SCALAR_AS_CONST(bool, bool)
2886 NIR_DEFINE_SCALAR_AS_CONST(double, float)
2887
2888 #undef NIR_DEFINE_SCALAR_AS_CONST
2889
2890 static inline bool
2891 nir_scalar_is_alu(nir_scalar s)
2892 {
2893 return s.def->parent_instr->type == nir_instr_type_alu;
2894 }
2895
2896 static inline nir_op
nir_scalar_alu_op(nir_scalar s)2897 nir_scalar_alu_op(nir_scalar s)
2898 {
2899 return nir_instr_as_alu(s.def->parent_instr)->op;
2900 }
2901
2902 static inline bool
nir_scalar_is_intrinsic(nir_scalar s)2903 nir_scalar_is_intrinsic(nir_scalar s)
2904 {
2905 return s.def->parent_instr->type == nir_instr_type_intrinsic;
2906 }
2907
2908 static inline nir_intrinsic_op
nir_scalar_intrinsic_op(nir_scalar s)2909 nir_scalar_intrinsic_op(nir_scalar s)
2910 {
2911 return nir_instr_as_intrinsic(s.def->parent_instr)->intrinsic;
2912 }
2913
2914 static inline nir_scalar
nir_scalar_chase_alu_src(nir_scalar s,unsigned alu_src_idx)2915 nir_scalar_chase_alu_src(nir_scalar s, unsigned alu_src_idx)
2916 {
2917 nir_scalar out = { NULL, 0 };
2918
2919 nir_alu_instr *alu = nir_instr_as_alu(s.def->parent_instr);
2920 assert(alu_src_idx < nir_op_infos[alu->op].num_inputs);
2921
2922 /* Our component must be written */
2923 assert(s.comp < s.def->num_components);
2924
2925 out.def = alu->src[alu_src_idx].src.ssa;
2926
2927 if (nir_op_infos[alu->op].input_sizes[alu_src_idx] == 0) {
2928 /* The ALU src is unsized so the source component follows the
2929 * destination component.
2930 */
2931 out.comp = alu->src[alu_src_idx].swizzle[s.comp];
2932 } else {
2933 /* This is a sized source so all source components work together to
2934 * produce all the destination components. Since we need to return a
2935 * scalar, this only works if the source is a scalar.
2936 */
2937 assert(nir_op_infos[alu->op].input_sizes[alu_src_idx] == 1);
2938 out.comp = alu->src[alu_src_idx].swizzle[0];
2939 }
2940 assert(out.comp < out.def->num_components);
2941
2942 return out;
2943 }
2944
2945 nir_scalar nir_scalar_chase_movs(nir_scalar s);
2946
2947 static inline nir_scalar
nir_get_scalar(nir_def * def,unsigned channel)2948 nir_get_scalar(nir_def *def, unsigned channel)
2949 {
2950 nir_scalar s = { def, channel };
2951 return s;
2952 }
2953
2954 /** Returns a nir_scalar where we've followed the bit-exact mov/vec use chain to the original definition */
2955 static inline nir_scalar
nir_scalar_resolved(nir_def * def,unsigned channel)2956 nir_scalar_resolved(nir_def *def, unsigned channel)
2957 {
2958 return nir_scalar_chase_movs(nir_get_scalar(def, channel));
2959 }
2960
2961 static inline bool
nir_scalar_equal(nir_scalar s1,nir_scalar s2)2962 nir_scalar_equal(nir_scalar s1, nir_scalar s2)
2963 {
2964 return s1.def == s2.def && s1.comp == s2.comp;
2965 }
2966
2967 static inline uint64_t
nir_alu_src_as_uint(nir_alu_src src)2968 nir_alu_src_as_uint(nir_alu_src src)
2969 {
2970 nir_scalar scalar = nir_get_scalar(src.src.ssa, src.swizzle[0]);
2971 return nir_scalar_as_uint(scalar);
2972 }
2973
2974 typedef struct {
2975 bool success;
2976
2977 nir_variable *var;
2978 unsigned desc_set;
2979 unsigned binding;
2980 unsigned num_indices;
2981 nir_src indices[4];
2982 bool read_first_invocation;
2983 } nir_binding;
2984
2985 nir_binding nir_chase_binding(nir_src rsrc);
2986 nir_variable *nir_get_binding_variable(struct nir_shader *shader, nir_binding binding);
2987
2988 /*
2989 * Control flow
2990 *
2991 * Control flow consists of a tree of control flow nodes, which include
2992 * if-statements and loops. The leaves of the tree are basic blocks, lists of
2993 * instructions that always run start-to-finish. Each basic block also keeps
2994 * track of its successors (blocks which may run immediately after the current
2995 * block) and predecessors (blocks which could have run immediately before the
2996 * current block). Each function also has a start block and an end block which
2997 * all return statements point to (which is always empty). Together, all the
2998 * blocks with their predecessors and successors make up the control flow
2999 * graph (CFG) of the function. There are helpers that modify the tree of
3000 * control flow nodes while modifying the CFG appropriately; these should be
3001 * used instead of modifying the tree directly.
3002 */
3003
3004 typedef enum {
3005 nir_cf_node_block,
3006 nir_cf_node_if,
3007 nir_cf_node_loop,
3008 nir_cf_node_function
3009 } nir_cf_node_type;
3010
3011 typedef struct nir_cf_node {
3012 struct exec_node node;
3013 nir_cf_node_type type;
3014 struct nir_cf_node *parent;
3015 } nir_cf_node;
3016
3017 typedef struct nir_block {
3018 nir_cf_node cf_node;
3019
3020 /** list of nir_instr */
3021 struct exec_list instr_list;
3022
3023 /** generic block index; generated by nir_index_blocks */
3024 unsigned index;
3025
3026 /* This indicates whether the block or any parent block is executed
3027 * conditionally and whether the condition uses a divergent value.
3028 */
3029 bool divergent;
3030
3031 /*
3032 * Each block can only have up to 2 successors, so we put them in a simple
3033 * array - no need for anything more complicated.
3034 */
3035 struct nir_block *successors[2];
3036
3037 /* Set of nir_block predecessors in the CFG */
3038 struct set *predecessors;
3039
3040 /*
3041 * this node's immediate dominator in the dominance tree - set to NULL for
3042 * the start block and any unreachable blocks.
3043 */
3044 struct nir_block *imm_dom;
3045
3046 /* This node's children in the dominance tree */
3047 unsigned num_dom_children;
3048 struct nir_block **dom_children;
3049
3050 /* Set of nir_blocks on the dominance frontier of this block */
3051 struct set *dom_frontier;
3052
3053 /*
3054 * These two indices have the property that dom_{pre,post}_index for each
3055 * child of this block in the dominance tree will always be between
3056 * dom_pre_index and dom_post_index for this block, which makes testing if
3057 * a given block is dominated by another block an O(1) operation.
3058 */
3059 uint32_t dom_pre_index, dom_post_index;
3060
3061 /**
3062 * Value just before the first nir_instr->index in the block, but after
3063 * end_ip that of any predecessor block.
3064 */
3065 uint32_t start_ip;
3066 /**
3067 * Value just after the last nir_instr->index in the block, but before the
3068 * start_ip of any successor block.
3069 */
3070 uint32_t end_ip;
3071
3072 /* SSA def live in and out for this block; used for liveness analysis.
3073 * Indexed by ssa_def->index
3074 */
3075 BITSET_WORD *live_in;
3076 BITSET_WORD *live_out;
3077 } nir_block;
3078
3079 static inline bool
nir_block_is_reachable(nir_block * b)3080 nir_block_is_reachable(nir_block *b)
3081 {
3082 /* See also nir_block_dominates */
3083 return b->dom_post_index != 0;
3084 }
3085
3086 static inline nir_instr *
nir_block_first_instr(nir_block * block)3087 nir_block_first_instr(nir_block *block)
3088 {
3089 struct exec_node *head = exec_list_get_head(&block->instr_list);
3090 return exec_node_data(nir_instr, head, node);
3091 }
3092
3093 static inline nir_instr *
nir_block_last_instr(nir_block * block)3094 nir_block_last_instr(nir_block *block)
3095 {
3096 struct exec_node *tail = exec_list_get_tail(&block->instr_list);
3097 return exec_node_data(nir_instr, tail, node);
3098 }
3099
3100 static inline bool
nir_block_ends_in_jump(nir_block * block)3101 nir_block_ends_in_jump(nir_block *block)
3102 {
3103 return !exec_list_is_empty(&block->instr_list) &&
3104 nir_block_last_instr(block)->type == nir_instr_type_jump;
3105 }
3106
3107 static inline bool
nir_block_ends_in_return_or_halt(nir_block * block)3108 nir_block_ends_in_return_or_halt(nir_block *block)
3109 {
3110 if (exec_list_is_empty(&block->instr_list))
3111 return false;
3112
3113 nir_instr *instr = nir_block_last_instr(block);
3114 if (instr->type != nir_instr_type_jump)
3115 return false;
3116
3117 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
3118 return jump_instr->type == nir_jump_return ||
3119 jump_instr->type == nir_jump_halt;
3120 }
3121
3122 static inline bool
nir_block_ends_in_break(nir_block * block)3123 nir_block_ends_in_break(nir_block *block)
3124 {
3125 if (exec_list_is_empty(&block->instr_list))
3126 return false;
3127
3128 nir_instr *instr = nir_block_last_instr(block);
3129 return instr->type == nir_instr_type_jump &&
3130 nir_instr_as_jump(instr)->type == nir_jump_break;
3131 }
3132
3133 bool nir_block_contains_work(nir_block *block);
3134
3135 #define nir_foreach_instr(instr, block) \
3136 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
3137 #define nir_foreach_instr_reverse(instr, block) \
3138 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
3139 #define nir_foreach_instr_safe(instr, block) \
3140 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
3141 #define nir_foreach_instr_reverse_safe(instr, block) \
3142 foreach_list_typed_reverse_safe(nir_instr, instr, node, &(block)->instr_list)
3143
3144 /* Phis come first in the block */
3145 static inline nir_phi_instr *
nir_first_phi_in_block(nir_block * block)3146 nir_first_phi_in_block(nir_block *block)
3147 {
3148 nir_foreach_instr(instr, block) {
3149 if (instr->type == nir_instr_type_phi)
3150 return nir_instr_as_phi(instr);
3151 else
3152 return NULL;
3153 }
3154
3155 return NULL;
3156 }
3157
3158 static inline nir_phi_instr *
nir_next_phi(nir_phi_instr * phi)3159 nir_next_phi(nir_phi_instr *phi)
3160 {
3161 nir_instr *next = nir_instr_next(&phi->instr);
3162
3163 if (next && next->type == nir_instr_type_phi)
3164 return nir_instr_as_phi(next);
3165 else
3166 return NULL;
3167 }
3168
3169 #define nir_foreach_phi(instr, block) \
3170 for (nir_phi_instr *instr = nir_first_phi_in_block(block); instr != NULL; \
3171 instr = nir_next_phi(instr))
3172
3173 #define nir_foreach_phi_safe(instr, block) \
3174 for (nir_phi_instr *instr = nir_first_phi_in_block(block), \
3175 *__next = instr ? nir_next_phi(instr) : NULL; \
3176 instr != NULL; \
3177 instr = __next, __next = instr ? nir_next_phi(instr) : NULL)
3178
3179 static inline nir_phi_instr *
nir_block_last_phi_instr(nir_block * block)3180 nir_block_last_phi_instr(nir_block *block)
3181 {
3182 nir_phi_instr *last_phi = NULL;
3183 nir_foreach_phi(instr, block)
3184 last_phi = instr;
3185
3186 return last_phi;
3187 }
3188
3189 typedef enum {
3190 nir_selection_control_none = 0x0,
3191
3192 /**
3193 * Defined by SPIR-V spec 3.22 "Selection Control".
3194 * The application prefers to remove control flow.
3195 */
3196 nir_selection_control_flatten = 0x1,
3197
3198 /**
3199 * Defined by SPIR-V spec 3.22 "Selection Control".
3200 * The application prefers to keep control flow.
3201 */
3202 nir_selection_control_dont_flatten = 0x2,
3203
3204 /**
3205 * May be applied by the compiler stack when it knows
3206 * that a branch is divergent, and:
3207 * - either both the if and else are always taken
3208 * - the if or else is empty and the other is always taken
3209 */
3210 nir_selection_control_divergent_always_taken = 0x3,
3211 } nir_selection_control;
3212
3213 typedef struct nir_if {
3214 nir_cf_node cf_node;
3215 nir_src condition;
3216 nir_selection_control control;
3217
3218 /** list of nir_cf_node */
3219 struct exec_list then_list;
3220
3221 /** list of nir_cf_node */
3222 struct exec_list else_list;
3223 } nir_if;
3224
3225 typedef struct {
3226 nir_if *nif;
3227
3228 /** Condition instruction that contains the induction variable */
3229 nir_instr *conditional_instr;
3230
3231 /** Block within ::nif that has the break instruction. */
3232 nir_block *break_block;
3233
3234 /** Last block for the then- or else-path that does not contain the break. */
3235 nir_block *continue_from_block;
3236
3237 /** True when ::break_block is in the else-path of ::nif. */
3238 bool continue_from_then;
3239 bool induction_rhs;
3240
3241 /* This is true if the terminators exact trip count is unknown. For
3242 * example:
3243 *
3244 * for (int i = 0; i < imin(x, 4); i++)
3245 * ...
3246 *
3247 * Here loop analysis would have set a max_trip_count of 4 however we dont
3248 * know for sure that this is the exact trip count.
3249 */
3250 bool exact_trip_count_unknown;
3251
3252 struct list_head loop_terminator_link;
3253 } nir_loop_terminator;
3254
3255 typedef struct {
3256 /* Induction variable. */
3257 nir_def *def;
3258
3259 /* Init statement with only uniform. */
3260 nir_src *init_src;
3261
3262 /* Update statement with only uniform. */
3263 nir_alu_src *update_src;
3264 } nir_loop_induction_variable;
3265
3266 typedef struct {
3267 /* Estimated cost (in number of instructions) of the loop */
3268 unsigned instr_cost;
3269
3270 /* Contains fp64 ops that will be lowered */
3271 bool has_soft_fp64;
3272
3273 /* Guessed trip count based on array indexing */
3274 unsigned guessed_trip_count;
3275
3276 /* Maximum number of times the loop is run (if known) */
3277 unsigned max_trip_count;
3278
3279 /* Do we know the exact number of times the loop will be run */
3280 bool exact_trip_count_known;
3281
3282 /* Unroll the loop regardless of its size */
3283 bool force_unroll;
3284
3285 /* Does the loop contain complex loop terminators, continues or other
3286 * complex behaviours? If this is true we can't rely on
3287 * loop_terminator_list to be complete or accurate.
3288 */
3289 bool complex_loop;
3290
3291 nir_loop_terminator *limiting_terminator;
3292
3293 /* A list of loop_terminators terminating this loop. */
3294 struct list_head loop_terminator_list;
3295
3296 /* array of induction variables for this loop */
3297 nir_loop_induction_variable *induction_vars;
3298 unsigned num_induction_vars;
3299 } nir_loop_info;
3300
3301 typedef enum {
3302 nir_loop_control_none = 0x0,
3303 nir_loop_control_unroll = 0x1,
3304 nir_loop_control_dont_unroll = 0x2,
3305 } nir_loop_control;
3306
3307 typedef struct {
3308 nir_cf_node cf_node;
3309
3310 /** list of nir_cf_node */
3311 struct exec_list body;
3312
3313 /** (optional) list of nir_cf_node */
3314 struct exec_list continue_list;
3315
3316 nir_loop_info *info;
3317 nir_loop_control control;
3318 bool partially_unrolled;
3319 bool divergent;
3320 } nir_loop;
3321
3322 /**
3323 * Various bits of metadata that can may be created or required by
3324 * optimization and analysis passes
3325 */
3326 typedef enum {
3327 nir_metadata_none = 0x0,
3328
3329 /** Indicates that nir_block::index values are valid.
3330 *
3331 * The start block has index 0 and they increase through a natural walk of
3332 * the CFG. nir_function_impl::num_blocks is the number of blocks and
3333 * every block index is in the range [0, nir_function_impl::num_blocks].
3334 *
3335 * A pass can preserve this metadata type if it doesn't touch the CFG.
3336 */
3337 nir_metadata_block_index = 0x1,
3338
3339 /** Indicates that block dominance information is valid
3340 *
3341 * This includes:
3342 *
3343 * - nir_block::num_dom_children
3344 * - nir_block::dom_children
3345 * - nir_block::dom_frontier
3346 * - nir_block::dom_pre_index
3347 * - nir_block::dom_post_index
3348 *
3349 * A pass can preserve this metadata type if it doesn't touch the CFG.
3350 */
3351 nir_metadata_dominance = 0x2,
3352
3353 /** Indicates that SSA def data-flow liveness information is valid
3354 *
3355 * This includes:
3356 *
3357 * - nir_block::live_in
3358 * - nir_block::live_out
3359 *
3360 * A pass can preserve this metadata type if it never adds or removes any
3361 * SSA defs or uses of SSA defs (most passes shouldn't preserve this
3362 * metadata type).
3363 */
3364 nir_metadata_live_defs = 0x4,
3365
3366 /** A dummy metadata value to track when a pass forgot to call
3367 * nir_metadata_preserve.
3368 *
3369 * A pass should always clear this value even if it doesn't make any
3370 * progress to indicate that it thought about preserving metadata.
3371 */
3372 nir_metadata_not_properly_reset = 0x8,
3373
3374 /** Indicates that loop analysis information is valid.
3375 *
3376 * This includes everything pointed to by nir_loop::info.
3377 *
3378 * A pass can preserve this metadata type if it is guaranteed to not affect
3379 * any loop metadata. However, since loop metadata includes things like
3380 * loop counts which depend on arithmetic in the loop, this is very hard to
3381 * determine. Most passes shouldn't preserve this metadata type.
3382 */
3383 nir_metadata_loop_analysis = 0x10,
3384
3385 /** Indicates that nir_instr::index values are valid.
3386 *
3387 * The start instruction has index 0 and they increase through a natural
3388 * walk of instructions in blocks in the CFG. The indices my have holes
3389 * after passes such as DCE.
3390 *
3391 * A pass can preserve this metadata type if it never adds or moves any
3392 * instructions (most passes shouldn't preserve this metadata type), but
3393 * can preserve it if it only removes instructions.
3394 */
3395 nir_metadata_instr_index = 0x20,
3396
3397 /** All control flow metadata
3398 *
3399 * This includes all metadata preserved by a pass that preserves control flow
3400 * but modifies instructions. For example, a pass using
3401 * nir_shader_instructions_pass will typically preserve this if it does not
3402 * insert control flow.
3403 *
3404 * This is the most common metadata set to preserve, so it has its own alias.
3405 */
3406 nir_metadata_control_flow = nir_metadata_block_index |
3407 nir_metadata_dominance,
3408
3409 /** All metadata
3410 *
3411 * This includes all nir_metadata flags except not_properly_reset. Passes
3412 * which do not change the shader in any way should call
3413 *
3414 * nir_metadata_preserve(impl, nir_metadata_all);
3415 */
3416 nir_metadata_all = ~nir_metadata_not_properly_reset,
3417 } nir_metadata;
3418 MESA_DEFINE_CPP_ENUM_BITFIELD_OPERATORS(nir_metadata)
3419
3420 typedef struct {
3421 nir_cf_node cf_node;
3422
3423 /** pointer to the function of which this is an implementation */
3424 struct nir_function *function;
3425
3426 /**
3427 * For entrypoints, a pointer to a nir_function_impl which runs before
3428 * it, once per draw or dispatch, communicating via store_preamble and
3429 * load_preamble intrinsics. If NULL then there is no preamble.
3430 */
3431 struct nir_function *preamble;
3432
3433 /** list of nir_cf_node */
3434 struct exec_list body;
3435
3436 nir_block *end_block;
3437
3438 /** list for all local variables in the function */
3439 struct exec_list locals;
3440
3441 /** next available SSA value index */
3442 unsigned ssa_alloc;
3443
3444 /* total number of basic blocks, only valid when block_index_dirty = false */
3445 unsigned num_blocks;
3446
3447 /** True if this nir_function_impl uses structured control-flow
3448 *
3449 * Structured nir_function_impls have different validation rules.
3450 */
3451 bool structured;
3452
3453 nir_metadata valid_metadata;
3454 } nir_function_impl;
3455
3456 #define nir_foreach_function_temp_variable(var, impl) \
3457 foreach_list_typed(nir_variable, var, node, &(impl)->locals)
3458
3459 #define nir_foreach_function_temp_variable_safe(var, impl) \
3460 foreach_list_typed_safe(nir_variable, var, node, &(impl)->locals)
3461
3462 ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
nir_start_block(nir_function_impl * impl)3463 nir_start_block(nir_function_impl *impl)
3464 {
3465 return (nir_block *)impl->body.head_sentinel.next;
3466 }
3467
3468 ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
nir_impl_last_block(nir_function_impl * impl)3469 nir_impl_last_block(nir_function_impl *impl)
3470 {
3471 return (nir_block *)impl->body.tail_sentinel.prev;
3472 }
3473
3474 static inline nir_cf_node *
nir_cf_node_next(nir_cf_node * node)3475 nir_cf_node_next(nir_cf_node *node)
3476 {
3477 struct exec_node *next = exec_node_get_next(&node->node);
3478 if (exec_node_is_tail_sentinel(next))
3479 return NULL;
3480 else
3481 return exec_node_data(nir_cf_node, next, node);
3482 }
3483
3484 static inline nir_cf_node *
nir_cf_node_prev(nir_cf_node * node)3485 nir_cf_node_prev(nir_cf_node *node)
3486 {
3487 struct exec_node *prev = exec_node_get_prev(&node->node);
3488 if (exec_node_is_head_sentinel(prev))
3489 return NULL;
3490 else
3491 return exec_node_data(nir_cf_node, prev, node);
3492 }
3493
3494 static inline bool
nir_cf_node_is_first(const nir_cf_node * node)3495 nir_cf_node_is_first(const nir_cf_node *node)
3496 {
3497 return exec_node_is_head_sentinel(node->node.prev);
3498 }
3499
3500 static inline bool
nir_cf_node_is_last(const nir_cf_node * node)3501 nir_cf_node_is_last(const nir_cf_node *node)
3502 {
3503 return exec_node_is_tail_sentinel(node->node.next);
3504 }
3505
NIR_DEFINE_CAST(nir_cf_node_as_block,nir_cf_node,nir_block,cf_node,type,nir_cf_node_block)3506 NIR_DEFINE_CAST(nir_cf_node_as_block, nir_cf_node, nir_block, cf_node,
3507 type, nir_cf_node_block)
3508 NIR_DEFINE_CAST(nir_cf_node_as_if, nir_cf_node, nir_if, cf_node,
3509 type, nir_cf_node_if)
3510 NIR_DEFINE_CAST(nir_cf_node_as_loop, nir_cf_node, nir_loop, cf_node,
3511 type, nir_cf_node_loop)
3512 NIR_DEFINE_CAST(nir_cf_node_as_function, nir_cf_node,
3513 nir_function_impl, cf_node, type, nir_cf_node_function)
3514
3515 static inline nir_block *
3516 nir_if_first_then_block(nir_if *if_stmt)
3517 {
3518 struct exec_node *head = exec_list_get_head(&if_stmt->then_list);
3519 return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
3520 }
3521
3522 static inline nir_block *
nir_if_last_then_block(nir_if * if_stmt)3523 nir_if_last_then_block(nir_if *if_stmt)
3524 {
3525 struct exec_node *tail = exec_list_get_tail(&if_stmt->then_list);
3526 return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
3527 }
3528
3529 static inline nir_block *
nir_if_first_else_block(nir_if * if_stmt)3530 nir_if_first_else_block(nir_if *if_stmt)
3531 {
3532 struct exec_node *head = exec_list_get_head(&if_stmt->else_list);
3533 return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
3534 }
3535
3536 static inline nir_block *
nir_if_last_else_block(nir_if * if_stmt)3537 nir_if_last_else_block(nir_if *if_stmt)
3538 {
3539 struct exec_node *tail = exec_list_get_tail(&if_stmt->else_list);
3540 return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
3541 }
3542
3543 static inline nir_block *
nir_loop_first_block(nir_loop * loop)3544 nir_loop_first_block(nir_loop *loop)
3545 {
3546 struct exec_node *head = exec_list_get_head(&loop->body);
3547 return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
3548 }
3549
3550 static inline nir_block *
nir_loop_last_block(nir_loop * loop)3551 nir_loop_last_block(nir_loop *loop)
3552 {
3553 struct exec_node *tail = exec_list_get_tail(&loop->body);
3554 return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
3555 }
3556
3557 static inline bool
nir_loop_has_continue_construct(const nir_loop * loop)3558 nir_loop_has_continue_construct(const nir_loop *loop)
3559 {
3560 return !exec_list_is_empty(&loop->continue_list);
3561 }
3562
3563 static inline nir_block *
nir_loop_first_continue_block(nir_loop * loop)3564 nir_loop_first_continue_block(nir_loop *loop)
3565 {
3566 assert(nir_loop_has_continue_construct(loop));
3567 struct exec_node *head = exec_list_get_head(&loop->continue_list);
3568 return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
3569 }
3570
3571 static inline nir_block *
nir_loop_last_continue_block(nir_loop * loop)3572 nir_loop_last_continue_block(nir_loop *loop)
3573 {
3574 assert(nir_loop_has_continue_construct(loop));
3575 struct exec_node *tail = exec_list_get_tail(&loop->continue_list);
3576 return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
3577 }
3578
3579 /**
3580 * Return the target block of a nir_jump_continue statement
3581 */
3582 static inline nir_block *
nir_loop_continue_target(nir_loop * loop)3583 nir_loop_continue_target(nir_loop *loop)
3584 {
3585 if (nir_loop_has_continue_construct(loop))
3586 return nir_loop_first_continue_block(loop);
3587 else
3588 return nir_loop_first_block(loop);
3589 }
3590
3591 /**
3592 * Return true if this list of cf_nodes contains a single empty block.
3593 */
3594 static inline bool
nir_cf_list_is_empty_block(struct exec_list * cf_list)3595 nir_cf_list_is_empty_block(struct exec_list *cf_list)
3596 {
3597 if (exec_list_is_singular(cf_list)) {
3598 struct exec_node *head = exec_list_get_head(cf_list);
3599 nir_block *block =
3600 nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
3601 return exec_list_is_empty(&block->instr_list);
3602 }
3603 return false;
3604 }
3605
3606 typedef struct {
3607 uint8_t num_components;
3608 uint8_t bit_size;
3609
3610 /* True if this paramater is actually the function return variable */
3611 bool is_return;
3612
3613 /* The type of the function param */
3614 const struct glsl_type *type;
3615 } nir_parameter;
3616
3617 typedef struct nir_function {
3618 struct exec_node node;
3619
3620 const char *name;
3621 struct nir_shader *shader;
3622
3623 unsigned num_params;
3624 nir_parameter *params;
3625
3626 /** The implementation of this function.
3627 *
3628 * If the function is only declared and not implemented, this is NULL.
3629 *
3630 * Unless setting to NULL or NIR_SERIALIZE_FUNC_HAS_IMPL, set with
3631 * nir_function_set_impl to maintain IR invariants.
3632 */
3633 nir_function_impl *impl;
3634
3635 bool is_entrypoint;
3636 /* from SPIR-V linkage, only for libraries */
3637 bool is_exported;
3638 bool is_preamble;
3639 /* from SPIR-V function control */
3640 bool should_inline;
3641 bool dont_inline; /* from SPIR-V */
3642
3643 /**
3644 * Is this function a subroutine type declaration
3645 * e.g. subroutine void type1(float arg1);
3646 */
3647 bool is_subroutine;
3648
3649 /**
3650 * Is this function associated to a subroutine type
3651 * e.g. subroutine (type1, type2) function_name { function_body };
3652 * would have num_subroutine_types 2,
3653 * and pointers to the type1 and type2 types.
3654 */
3655 int num_subroutine_types;
3656 const struct glsl_type **subroutine_types;
3657
3658 int subroutine_index;
3659 } nir_function;
3660
3661 typedef enum {
3662 nir_lower_imul64 = (1 << 0),
3663 nir_lower_isign64 = (1 << 1),
3664 /** Lower all int64 modulus and division opcodes */
3665 nir_lower_divmod64 = (1 << 2),
3666 /** Lower all 64-bit umul_high and imul_high opcodes */
3667 nir_lower_imul_high64 = (1 << 3),
3668 nir_lower_bcsel64 = (1 << 4),
3669 nir_lower_icmp64 = (1 << 5),
3670 nir_lower_iadd64 = (1 << 6),
3671 nir_lower_iabs64 = (1 << 7),
3672 nir_lower_ineg64 = (1 << 8),
3673 nir_lower_logic64 = (1 << 9),
3674 nir_lower_minmax64 = (1 << 10),
3675 nir_lower_shift64 = (1 << 11),
3676 nir_lower_imul_2x32_64 = (1 << 12),
3677 nir_lower_extract64 = (1 << 13),
3678 nir_lower_ufind_msb64 = (1 << 14),
3679 nir_lower_bit_count64 = (1 << 15),
3680 nir_lower_subgroup_shuffle64 = (1 << 16),
3681 nir_lower_scan_reduce_bitwise64 = (1 << 17),
3682 nir_lower_scan_reduce_iadd64 = (1 << 18),
3683 nir_lower_vote_ieq64 = (1 << 19),
3684 nir_lower_usub_sat64 = (1 << 20),
3685 nir_lower_iadd_sat64 = (1 << 21),
3686 nir_lower_find_lsb64 = (1 << 22),
3687 nir_lower_conv64 = (1 << 23),
3688 nir_lower_uadd_sat64 = (1 << 24),
3689 nir_lower_iadd3_64 = (1 << 25),
3690 } nir_lower_int64_options;
3691
3692 typedef enum {
3693 nir_lower_drcp = (1 << 0),
3694 nir_lower_dsqrt = (1 << 1),
3695 nir_lower_drsq = (1 << 2),
3696 nir_lower_dtrunc = (1 << 3),
3697 nir_lower_dfloor = (1 << 4),
3698 nir_lower_dceil = (1 << 5),
3699 nir_lower_dfract = (1 << 6),
3700 nir_lower_dround_even = (1 << 7),
3701 nir_lower_dmod = (1 << 8),
3702 nir_lower_dsub = (1 << 9),
3703 nir_lower_ddiv = (1 << 10),
3704 nir_lower_dsign = (1 << 11),
3705 nir_lower_dminmax = (1 << 12),
3706 nir_lower_dsat = (1 << 13),
3707 nir_lower_fp64_full_software = (1 << 14),
3708 } nir_lower_doubles_options;
3709
3710 typedef enum {
3711 nir_divergence_single_prim_per_subgroup = (1 << 0),
3712 nir_divergence_single_patch_per_tcs_subgroup = (1 << 1),
3713 nir_divergence_single_patch_per_tes_subgroup = (1 << 2),
3714 nir_divergence_view_index_uniform = (1 << 3),
3715 nir_divergence_single_frag_shading_rate_per_subgroup = (1 << 4),
3716 nir_divergence_multiple_workgroup_per_compute_subgroup = (1 << 5),
3717 nir_divergence_shader_record_ptr_uniform = (1 << 6),
3718 nir_divergence_uniform_load_tears = (1 << 7),
3719 } nir_divergence_options;
3720
3721 typedef enum {
3722 /**
3723 * Whether a fragment shader can interpolate the same input multiple times
3724 * with different modes (smooth, noperspective) and locations (pixel,
3725 * centroid, sample, at_offset, at_sample), excluding the flat mode.
3726 *
3727 * This matches AMD GPU flexibility and limitations and is a superset of
3728 * the GL4 requirement that each input can be interpolated at its specified
3729 * location, and then also as centroid, at_offset, and at_sample.
3730 */
3731 nir_io_has_flexible_input_interpolation_except_flat = BITFIELD_BIT(0),
3732
3733 /**
3734 * nir_opt_varyings compacts (relocates) components of varyings by
3735 * rewriting their locations completely, effectively moving components of
3736 * varyings between slots. This option forces nir_opt_varyings to make
3737 * VARYING_SLOT_POS unused by moving its contents to VARn if the consumer
3738 * is not FS. If this option is not set and POS is unused, it moves
3739 * components of VARn to POS until it's fully used.
3740 */
3741 nir_io_dont_use_pos_for_non_fs_varyings = BITFIELD_BIT(1),
3742
3743 nir_io_16bit_input_output_support = BITFIELD_BIT(2),
3744
3745 /**
3746 * Implement mediump inputs and outputs as normal 32-bit IO.
3747 * Causes the mediump flag to be not set for IO semantics, essentially
3748 * destroying any mediump-related IO information in the shader.
3749 */
3750 nir_io_mediump_is_32bit = BITFIELD_BIT(3),
3751
3752 /**
3753 * Whether nir_opt_vectorize_io should ignore FS inputs.
3754 */
3755 nir_io_prefer_scalar_fs_inputs = BITFIELD_BIT(4),
3756
3757 /**
3758 * Whether interpolated fragment shader vec4 slots can use load_input for
3759 * a subset of its components to skip interpolation for those components.
3760 * The result of such load_input is a value from a random (not necessarily
3761 * provoking) vertex. If a value from the provoking vertex is required,
3762 * the vec4 slot should have no load_interpolated_input instructions.
3763 *
3764 * This exposes the AMD capability that allows packing flat inputs with
3765 * interpolated inputs in a limited number of cases. Normally, flat
3766 * components must be in a separate vec4 slot to get the value from
3767 * the provoking vertex. If the compiler can prove that all per-vertex
3768 * values are equal (convergent, i.e. the provoking vertex doesn't matter),
3769 * it can put such flat components into any interpolated vec4 slot.
3770 *
3771 * It should also be set if the hw can mix flat and interpolated components
3772 * in the same vec4 slot.
3773 *
3774 * This causes nir_opt_varyings to skip interpolation for all varyings
3775 * that are convergent, and enables better compaction and inter-shader code
3776 * motion for convergent varyings.
3777 */
3778 nir_io_mix_convergent_flat_with_interpolated = BITFIELD_BIT(5),
3779
3780 /**
3781 * Whether src_type and dest_type of IO intrinsics are irrelevant and
3782 * should be ignored by nir_opt_vectorize_io. All drivers that always treat
3783 * load_input and store_output as untyped and load_interpolated_input as
3784 * float##bit_size should set this.
3785 */
3786 nir_io_vectorizer_ignores_types = BITFIELD_BIT(6),
3787
3788 /* Options affecting the GLSL compiler are below. */
3789
3790 /**
3791 * Lower load_deref/store_deref to load_input/store_output/etc. intrinsics.
3792 * This is only affects GLSL compilation.
3793 */
3794 nir_io_glsl_lower_derefs = BITFIELD_BIT(16),
3795
3796 /**
3797 * Run nir_opt_varyings in the GLSL linker. If false, optimize varyings
3798 * the old way and lower IO later.
3799 *
3800 * nir_io_lower_to_intrinsics must be set for this to take effect.
3801 *
3802 * TODO: remove this and default to enabled once we are sure that this
3803 * codepath is solid.
3804 */
3805 nir_io_glsl_opt_varyings = BITFIELD_BIT(17),
3806 } nir_io_options;
3807
3808 typedef enum {
3809 nir_lower_packing_op_pack_64_2x32,
3810 nir_lower_packing_op_unpack_64_2x32,
3811 nir_lower_packing_op_pack_64_4x16,
3812 nir_lower_packing_op_unpack_64_4x16,
3813 nir_lower_packing_op_pack_32_2x16,
3814 nir_lower_packing_op_unpack_32_2x16,
3815 nir_lower_packing_op_pack_32_4x8,
3816 nir_lower_packing_op_unpack_32_4x8,
3817 nir_lower_packing_num_ops,
3818 } nir_lower_packing_op;
3819
3820 /** An instruction filtering callback
3821 *
3822 * Returns true if the instruction should be processed and false otherwise.
3823 */
3824 typedef bool (*nir_instr_filter_cb)(const nir_instr *, const void *);
3825
3826 /** A vectorization width callback
3827 *
3828 * Returns the maximum vectorization width per instruction.
3829 * 0, if the instruction must not be modified.
3830 *
3831 * The vectorization width must be a power of 2.
3832 */
3833 typedef uint8_t (*nir_vectorize_cb)(const nir_instr *, const void *);
3834
3835 typedef struct nir_shader_compiler_options {
3836 bool lower_fdiv;
3837 bool lower_ffma16;
3838 bool lower_ffma32;
3839 bool lower_ffma64;
3840 bool fuse_ffma16;
3841 bool fuse_ffma32;
3842 bool fuse_ffma64;
3843 bool lower_flrp16;
3844 bool lower_flrp32;
3845 /** Lowers flrp when it does not support doubles */
3846 bool lower_flrp64;
3847 bool lower_fpow;
3848 bool lower_fsat;
3849 bool lower_fsqrt;
3850 bool lower_sincos;
3851 bool lower_fmod;
3852 /** Lowers ibitfield_extract/ubitfield_extract. */
3853 bool lower_bitfield_extract;
3854 /** Lowers bitfield_insert. */
3855 bool lower_bitfield_insert;
3856 /** Lowers bitfield_reverse to shifts. */
3857 bool lower_bitfield_reverse;
3858 /** Lowers bit_count to shifts. */
3859 bool lower_bit_count;
3860 /** Lowers ifind_msb. */
3861 bool lower_ifind_msb;
3862 /** Lowers ufind_msb. */
3863 bool lower_ufind_msb;
3864 /** Lowers find_lsb to ufind_msb and logic ops */
3865 bool lower_find_lsb;
3866 bool lower_uadd_carry;
3867 bool lower_usub_borrow;
3868 /** Lowers imul_high/umul_high to 16-bit multiplies and carry operations. */
3869 bool lower_mul_high;
3870 /** lowers fneg to fmul(x, -1.0). Driver must call nir_opt_algebraic_late() */
3871 bool lower_fneg;
3872 /** lowers ineg to isub. Driver must call nir_opt_algebraic_late(). */
3873 bool lower_ineg;
3874 /** lowers fisnormal to alu ops. */
3875 bool lower_fisnormal;
3876
3877 /* lower {slt,sge,seq,sne} to {flt,fge,feq,fneu} + b2f: */
3878 bool lower_scmp;
3879
3880 /* lower b/fall_equalN/b/fany_nequalN (ex:fany_nequal4 to sne+fdot4+fsat) */
3881 bool lower_vector_cmp;
3882
3883 /** enable rules to avoid bit ops */
3884 bool lower_bitops;
3885
3886 /** enables rules to lower isign to imin+imax */
3887 bool lower_isign;
3888
3889 /** enables rules to lower fsign to fsub and flt */
3890 bool lower_fsign;
3891
3892 /** enables rules to lower iabs to ineg+imax */
3893 bool lower_iabs;
3894
3895 /** enable rules that avoid generating umax from signed integer ops */
3896 bool lower_umax;
3897
3898 /** enable rules that avoid generating umin from signed integer ops */
3899 bool lower_umin;
3900
3901 /* lower fmin/fmax with signed zero preserve to fmin/fmax with
3902 * no_signed_zero, for backends whose fmin/fmax implementations do not
3903 * implement IEEE-754-2019 semantics for signed zero.
3904 */
3905 bool lower_fminmax_signed_zero;
3906
3907 /* lower fdph to fdot4 */
3908 bool lower_fdph;
3909
3910 /** lower fdot to fmul and fsum/fadd. */
3911 bool lower_fdot;
3912
3913 /* Does the native fdot instruction replicate its result for four
3914 * components? If so, then opt_algebraic_late will turn all fdotN
3915 * instructions into fdotN_replicated instructions.
3916 */
3917 bool fdot_replicates;
3918
3919 /** lowers ffloor to fsub+ffract: */
3920 bool lower_ffloor;
3921
3922 /** lowers ffract to fsub+ffloor: */
3923 bool lower_ffract;
3924
3925 /** lowers fceil to fneg+ffloor+fneg: */
3926 bool lower_fceil;
3927
3928 bool lower_ftrunc;
3929
3930 /** Lowers fround_even to ffract+feq+csel.
3931 *
3932 * Not correct in that it doesn't correctly handle the "_even" part of the
3933 * rounding, but good enough for DX9 array indexing handling on DX9-class
3934 * hardware.
3935 */
3936 bool lower_fround_even;
3937
3938 bool lower_ldexp;
3939
3940 bool lower_pack_half_2x16;
3941 bool lower_pack_unorm_2x16;
3942 bool lower_pack_snorm_2x16;
3943 bool lower_pack_unorm_4x8;
3944 bool lower_pack_snorm_4x8;
3945 bool lower_pack_64_2x32;
3946 bool lower_pack_64_4x16;
3947 bool lower_pack_32_2x16;
3948 bool lower_pack_64_2x32_split;
3949 bool lower_pack_32_2x16_split;
3950 bool lower_unpack_half_2x16;
3951 bool lower_unpack_unorm_2x16;
3952 bool lower_unpack_snorm_2x16;
3953 bool lower_unpack_unorm_4x8;
3954 bool lower_unpack_snorm_4x8;
3955 bool lower_unpack_64_2x32_split;
3956 bool lower_unpack_32_2x16_split;
3957
3958 bool lower_pack_split;
3959
3960 bool lower_extract_byte;
3961 bool lower_extract_word;
3962 bool lower_insert_byte;
3963 bool lower_insert_word;
3964
3965 bool lower_all_io_to_temps;
3966 bool lower_all_io_to_elements;
3967
3968 /* Indicates that the driver only has zero-based vertex id */
3969 bool vertex_id_zero_based;
3970
3971 /**
3972 * If enabled, gl_BaseVertex will be lowered as:
3973 * is_indexed_draw (~0/0) & firstvertex
3974 */
3975 bool lower_base_vertex;
3976
3977 /**
3978 * If enabled, gl_HelperInvocation will be lowered as:
3979 *
3980 * !((1 << sample_id) & sample_mask_in))
3981 *
3982 * This depends on some possibly hw implementation details, which may
3983 * not be true for all hw. In particular that the FS is only executed
3984 * for covered samples or for helper invocations. So, do not blindly
3985 * enable this option.
3986 *
3987 * Note: See also issue #22 in ARB_shader_image_load_store
3988 */
3989 bool lower_helper_invocation;
3990
3991 /**
3992 * Convert gl_SampleMaskIn to gl_HelperInvocation as follows:
3993 *
3994 * gl_SampleMaskIn == 0 ---> gl_HelperInvocation
3995 * gl_SampleMaskIn != 0 ---> !gl_HelperInvocation
3996 */
3997 bool optimize_sample_mask_in;
3998
3999 /**
4000 * Optimize boolean reductions of quad broadcasts. This should only be enabled if
4001 * nir_intrinsic_reduce supports INCLUDE_HELPERS.
4002 */
4003 bool optimize_quad_vote_to_reduce;
4004
4005 bool lower_cs_local_index_to_id;
4006 bool lower_cs_local_id_to_index;
4007
4008 /* Prevents lowering global_invocation_id to be in terms of workgroup_id */
4009 bool has_cs_global_id;
4010
4011 bool lower_device_index_to_zero;
4012
4013 /* Set if nir_lower_pntc_ytransform() should invert gl_PointCoord.
4014 * Either when frame buffer is flipped or GL_POINT_SPRITE_COORD_ORIGIN
4015 * is GL_LOWER_LEFT.
4016 */
4017 bool lower_wpos_pntc;
4018
4019 /**
4020 * Set if nir_op_[iu]hadd and nir_op_[iu]rhadd instructions should be
4021 * lowered to simple arithmetic.
4022 *
4023 * If this flag is set, the lowering will be applied to all bit-sizes of
4024 * these instructions.
4025 *
4026 * :c:member:`lower_hadd64`
4027 */
4028 bool lower_hadd;
4029
4030 /**
4031 * Set if only 64-bit nir_op_[iu]hadd and nir_op_[iu]rhadd instructions
4032 * should be lowered to simple arithmetic.
4033 *
4034 * If this flag is set, the lowering will be applied to only 64-bit
4035 * versions of these instructions.
4036 *
4037 * :c:member:`lower_hadd`
4038 */
4039 bool lower_hadd64;
4040
4041 /**
4042 * Set if nir_op_uadd_sat should be lowered to simple arithmetic.
4043 *
4044 * If this flag is set, the lowering will be applied to all bit-sizes of
4045 * these instructions.
4046 */
4047 bool lower_uadd_sat;
4048
4049 /**
4050 * Set if nir_op_usub_sat should be lowered to simple arithmetic.
4051 *
4052 * If this flag is set, the lowering will be applied to all bit-sizes of
4053 * these instructions.
4054 */
4055 bool lower_usub_sat;
4056
4057 /**
4058 * Set if nir_op_iadd_sat and nir_op_isub_sat should be lowered to simple
4059 * arithmetic.
4060 *
4061 * If this flag is set, the lowering will be applied to all bit-sizes of
4062 * these instructions.
4063 */
4064 bool lower_iadd_sat;
4065
4066 /**
4067 * Set if imul_32x16 and umul_32x16 should be lowered to simple
4068 * arithmetic.
4069 */
4070 bool lower_mul_32x16;
4071
4072 /**
4073 * Should IO be re-vectorized? Some scalar ISAs still operate on vec4's
4074 * for IO purposes and would prefer loads/stores be vectorized.
4075 */
4076 bool vectorize_io;
4077 bool vectorize_tess_levels;
4078 bool lower_to_scalar;
4079 nir_instr_filter_cb lower_to_scalar_filter;
4080
4081 /**
4082 * Disables potentially harmful algebraic transformations for architectures
4083 * with SIMD-within-a-register semantics.
4084 *
4085 * Note, to actually vectorize 16bit instructions, use nir_opt_vectorize()
4086 * with a suitable callback function.
4087 */
4088 bool vectorize_vec2_16bit;
4089
4090 /**
4091 * Should the linker unify inputs_read/outputs_written between adjacent
4092 * shader stages which are linked into a single program?
4093 */
4094 bool unify_interfaces;
4095
4096 /**
4097 * Should nir_lower_io() create load_interpolated_input intrinsics?
4098 *
4099 * If not, it generates regular load_input intrinsics and interpolation
4100 * information must be inferred from the list of input nir_variables.
4101 */
4102 bool use_interpolated_input_intrinsics;
4103
4104 /**
4105 * Whether nir_lower_io() will lower interpolateAt functions to
4106 * load_interpolated_input intrinsics.
4107 *
4108 * Unlike use_interpolated_input_intrinsics this will only lower these
4109 * functions and leave input load intrinsics untouched.
4110 */
4111 bool lower_interpolate_at;
4112
4113 /* Lowers when 32x32->64 bit multiplication is not supported */
4114 bool lower_mul_2x32_64;
4115
4116 /* Indicates that urol and uror are supported */
4117 bool has_rotate8;
4118 bool has_rotate16;
4119 bool has_rotate32;
4120
4121 /** Backend supports shfr */
4122 bool has_shfr32;
4123
4124 /** Backend supports ternary addition */
4125 bool has_iadd3;
4126
4127 /**
4128 * Backend supports imul24, and would like to use it (when possible)
4129 * for address/offset calculation. If true, driver should call
4130 * nir_lower_amul(). (If not set, amul will automatically be lowered
4131 * to imul.)
4132 */
4133 bool has_imul24;
4134
4135 /** Backend supports umul24, if not set umul24 will automatically be lowered
4136 * to imul with masked inputs */
4137 bool has_umul24;
4138
4139 /** Backend supports 32-bit imad */
4140 bool has_imad32;
4141
4142 /** Backend supports umad24, if not set umad24 will automatically be lowered
4143 * to imul with masked inputs and iadd */
4144 bool has_umad24;
4145
4146 /* Backend supports fused comapre against zero and csel */
4147 bool has_fused_comp_and_csel;
4148
4149 /* Backend supports fneo, fequ, fltu, fgeu. */
4150 bool has_fneo_fcmpu;
4151
4152 /* Backend supports ford and funord. */
4153 bool has_ford_funord;
4154
4155 /** Backend supports fsub, if not set fsub will automatically be lowered to
4156 * fadd(x, fneg(y)). If true, driver should call nir_opt_algebraic_late(). */
4157 bool has_fsub;
4158
4159 /** Backend supports isub, if not set isub will automatically be lowered to
4160 * iadd(x, ineg(y)). If true, driver should call nir_opt_algebraic_late(). */
4161 bool has_isub;
4162
4163 /** Backend supports pack_32_4x8 or pack_32_4x8_split. */
4164 bool has_pack_32_4x8;
4165
4166 /** Backend supports nir_load_texture_scale and prefers it over txs for nir
4167 * lowerings. */
4168 bool has_texture_scaling;
4169
4170 /** Backend supports sdot_4x8_iadd. */
4171 bool has_sdot_4x8;
4172
4173 /** Backend supports udot_4x8_uadd. */
4174 bool has_udot_4x8;
4175
4176 /** Backend supports sudot_4x8_iadd. */
4177 bool has_sudot_4x8;
4178
4179 /** Backend supports sdot_4x8_iadd_sat. */
4180 bool has_sdot_4x8_sat;
4181
4182 /** Backend supports udot_4x8_uadd_sat. */
4183 bool has_udot_4x8_sat;
4184
4185 /** Backend supports sudot_4x8_iadd_sat. */
4186 bool has_sudot_4x8_sat;
4187
4188 /** Backend supports sdot_2x16 and udot_2x16 opcodes. */
4189 bool has_dot_2x16;
4190
4191 /** Backend supports fmulz (and ffmaz if lower_ffma32=false) */
4192 bool has_fmulz;
4193
4194 /**
4195 * Backend supports fmulz (and ffmaz if lower_ffma32=false) but only if
4196 * FLOAT_CONTROLS_DENORM_PRESERVE_FP32 is not set
4197 */
4198 bool has_fmulz_no_denorms;
4199
4200 /** Backend supports 32bit ufind_msb_rev and ifind_msb_rev. */
4201 bool has_find_msb_rev;
4202
4203 /** Backend supports pack_half_2x16_rtz_split. */
4204 bool has_pack_half_2x16_rtz;
4205
4206 /** Backend supports bitz/bitnz. */
4207 bool has_bit_test;
4208
4209 /** Backend supports ubfe/ibfe. */
4210 bool has_bfe;
4211
4212 /** Backend supports bfm. */
4213 bool has_bfm;
4214
4215 /** Backend supports bfi. */
4216 bool has_bfi;
4217
4218 /** Backend supports bitfield_select. */
4219 bool has_bitfield_select;
4220
4221 /** Backend supports uclz. */
4222 bool has_uclz;
4223
4224 /** Backend support msad_u4x8. */
4225 bool has_msad;
4226
4227 /**
4228 * Is this the Intel vec4 backend?
4229 *
4230 * Used to inhibit algebraic optimizations that are known to be harmful on
4231 * the Intel vec4 backend. This is generally applicable to any
4232 * optimization that might cause more immediate values to be used in
4233 * 3-source (e.g., ffma and flrp) instructions.
4234 */
4235 bool intel_vec4;
4236
4237 /**
4238 * For most Intel GPUs, all ternary operations such as FMA and BFE cannot
4239 * have immediates, so two to three instructions may eventually be needed.
4240 */
4241 bool avoid_ternary_with_two_constants;
4242
4243 /** Whether 8-bit ALU is supported. */
4244 bool support_8bit_alu;
4245
4246 /** Whether 16-bit ALU is supported. */
4247 bool support_16bit_alu;
4248
4249 unsigned max_unroll_iterations;
4250 unsigned max_unroll_iterations_aggressive;
4251 unsigned max_unroll_iterations_fp64;
4252
4253 bool lower_uniforms_to_ubo;
4254
4255 /* If the precision is ignored, backends that don't handle
4256 * different precisions when passing data between stages and use
4257 * vectorized IO can pack more varyings when linking. */
4258 bool linker_ignore_precision;
4259
4260 /* Specifies if indirect sampler array access will trigger forced loop
4261 * unrolling.
4262 */
4263 bool force_indirect_unrolling_sampler;
4264
4265 /* Some older drivers don't support GLSL versions with the concept of flat
4266 * varyings and also don't support integers. This setting helps us avoid
4267 * marking varyings as flat and potentially having them changed to ints via
4268 * varying packing.
4269 */
4270 bool no_integers;
4271
4272 /**
4273 * Specifies which type of indirectly accessed variables should force
4274 * loop unrolling.
4275 */
4276 nir_variable_mode force_indirect_unrolling;
4277
4278 bool driver_functions;
4279
4280 nir_lower_int64_options lower_int64_options;
4281 nir_lower_doubles_options lower_doubles_options;
4282 nir_divergence_options divergence_analysis_options;
4283
4284 /**
4285 * The masks of shader stages that support indirect indexing with
4286 * load_input and store_output intrinsics. It's used by
4287 * nir_lower_io_passes.
4288 */
4289 uint8_t support_indirect_inputs;
4290 uint8_t support_indirect_outputs;
4291
4292 /**
4293 * Remove varying loaded from uniform, let fragment shader load the
4294 * uniform directly. GPU passing varying by memory can benifit from it
4295 * for sure; but GPU passing varying by on chip resource may not.
4296 * Because it saves on chip resource but may increase memory pressure when
4297 * fragment task is far more than vertex one, so better left it disabled.
4298 */
4299 bool lower_varying_from_uniform;
4300
4301 /** store the variable offset into the instrinsic range_base instead
4302 * of adding it to the image index.
4303 */
4304 bool lower_image_offset_to_range_base;
4305
4306 /** store the variable offset into the instrinsic range_base instead
4307 * of adding it to the atomic source
4308 */
4309 bool lower_atomic_offset_to_range_base;
4310
4311 /** Don't convert medium-precision casts (e.g. f2fmp) into concrete
4312 * type casts (e.g. f2f16).
4313 */
4314 bool preserve_mediump;
4315
4316 /** lowers fquantize2f16 to alu ops. */
4317 bool lower_fquantize2f16;
4318
4319 /** Lower f2f16 to f2f16_rtz when execution mode is not rtne. */
4320 bool force_f2f16_rtz;
4321
4322 /** Lower VARYING_SLOT_LAYER in FS to SYSTEM_VALUE_LAYER_ID. */
4323 bool lower_layer_fs_input_to_sysval;
4324
4325 /** clip/cull distance and tess level arrays use compact semantics */
4326 bool compact_arrays;
4327
4328 /**
4329 * Whether discard gets emitted as nir_intrinsic_demote.
4330 * Otherwise, nir_intrinsic_terminate is being used.
4331 */
4332 bool discard_is_demote;
4333
4334 /**
4335 * Whether the new-style derivative intrinsics are supported. If false,
4336 * legacy ALU derivative ops will be emitted. This transitional option will
4337 * be removed once all drivers are converted to derivative intrinsics.
4338 */
4339 bool has_ddx_intrinsics;
4340
4341 /** Whether derivative intrinsics must be scalarized. */
4342 bool scalarize_ddx;
4343
4344 /** Options determining lowering and behavior of inputs and outputs. */
4345 nir_io_options io_options;
4346
4347 /**
4348 * Bit mask of nir_lower_packing_op to skip lowering some nir ops in
4349 * nir_lower_packing().
4350 */
4351 unsigned skip_lower_packing_ops;
4352
4353 /** Driver callback where drivers can define how to lower mediump.
4354 * Used by nir_lower_io_passes.
4355 */
4356 void (*lower_mediump_io)(struct nir_shader *nir);
4357
4358 /**
4359 * Return the maximum cost of an expression that's written to a shader
4360 * output that can be moved into the next shader to remove that output.
4361 *
4362 * Currently only uniform expressions are moved. A uniform expression is
4363 * any ALU expression sourcing only constants, uniforms, and UBO loads.
4364 *
4365 * Set to NULL or return 0 if you only want to propagate constants from
4366 * outputs to inputs.
4367 *
4368 * Drivers can set the maximum cost based on the types of consecutive
4369 * shaders or shader SHA1s.
4370 *
4371 * Drivers should also set "varying_estimate_instr_cost".
4372 */
4373 unsigned (*varying_expression_max_cost)(struct nir_shader *consumer,
4374 struct nir_shader *producer);
4375
4376 /**
4377 * Return the cost of an instruction that could be moved into the next
4378 * shader. If the cost of all instructions in an expression is <=
4379 * varying_expression_max_cost(), the instruction is moved.
4380 */
4381 unsigned (*varying_estimate_instr_cost)(struct nir_instr *instr);
4382 } nir_shader_compiler_options;
4383
4384 typedef struct nir_shader {
4385 gc_ctx *gctx;
4386
4387 /** list of uniforms (nir_variable) */
4388 struct exec_list variables;
4389
4390 /** Set of driver-specific options for the shader.
4391 *
4392 * The memory for the options is expected to be kept in a single static
4393 * copy by the driver.
4394 */
4395 const struct nir_shader_compiler_options *options;
4396
4397 /** Various bits of compile-time information about a given shader */
4398 struct shader_info info;
4399
4400 /** list of nir_function */
4401 struct exec_list functions;
4402
4403 /**
4404 * The size of the variable space for load_input_*, load_uniform_*, etc.
4405 * intrinsics. This is in back-end specific units which is likely one of
4406 * bytes, dwords, or vec4s depending on context and back-end.
4407 */
4408 unsigned num_inputs, num_uniforms, num_outputs;
4409
4410 /** Size in bytes of required implicitly bound global memory */
4411 unsigned global_mem_size;
4412
4413 /** Size in bytes of required scratch space */
4414 unsigned scratch_size;
4415
4416 /** Constant data associated with this shader.
4417 *
4418 * Constant data is loaded through load_constant intrinsics (as compared to
4419 * the NIR load_const instructions which have the constant value inlined
4420 * into them). This is usually generated by nir_opt_large_constants (so
4421 * shaders don't have to load_const into a temporary array when they want
4422 * to indirect on a const array).
4423 */
4424 void *constant_data;
4425 /** Size of the constant data associated with the shader, in bytes */
4426 unsigned constant_data_size;
4427
4428 struct nir_xfb_info *xfb_info;
4429
4430 unsigned printf_info_count;
4431 u_printf_info *printf_info;
4432 } nir_shader;
4433
4434 #define nir_foreach_function(func, shader) \
4435 foreach_list_typed(nir_function, func, node, &(shader)->functions)
4436
4437 #define nir_foreach_function_safe(func, shader) \
4438 foreach_list_typed_safe(nir_function, func, node, &(shader)->functions)
4439
4440 static inline nir_function *
nir_foreach_function_with_impl_first(const nir_shader * shader)4441 nir_foreach_function_with_impl_first(const nir_shader *shader)
4442 {
4443 foreach_list_typed(nir_function, func, node, &shader->functions) {
4444 if (func->impl != NULL)
4445 return func;
4446 }
4447
4448 return NULL;
4449 }
4450
4451 static inline nir_function_impl *
nir_foreach_function_with_impl_next(nir_function ** it)4452 nir_foreach_function_with_impl_next(nir_function **it)
4453 {
4454 foreach_list_typed_from(nir_function, func, node, _, (*it)->node.next) {
4455 if (func->impl != NULL) {
4456 *it = func;
4457 return func->impl;
4458 }
4459 }
4460
4461 return NULL;
4462 }
4463
4464 #define nir_foreach_function_with_impl(it, impl_it, shader) \
4465 for (nir_function *it = nir_foreach_function_with_impl_first(shader); \
4466 it != NULL; \
4467 it = NULL) \
4468 \
4469 for (nir_function_impl *impl_it = it->impl; \
4470 impl_it != NULL; \
4471 impl_it = nir_foreach_function_with_impl_next(&it))
4472
4473 /* Equivalent to
4474 *
4475 * nir_foreach_function(func, shader) {
4476 * if (func->impl != NULL) {
4477 * ...
4478 * }
4479 * }
4480 *
4481 * Carefully written to ensure break/continue work in the user code.
4482 */
4483
4484 #define nir_foreach_function_impl(it, shader) \
4485 nir_foreach_function_with_impl(_func_##it, it, shader)
4486
4487 static inline nir_function_impl *
nir_shader_get_entrypoint(const nir_shader * shader)4488 nir_shader_get_entrypoint(const nir_shader *shader)
4489 {
4490 nir_function *func = NULL;
4491
4492 nir_foreach_function(function, shader) {
4493 assert(func == NULL);
4494 if (function->is_entrypoint) {
4495 func = function;
4496 #ifndef NDEBUG
4497 break;
4498 #endif
4499 }
4500 }
4501
4502 if (!func)
4503 return NULL;
4504
4505 assert(func->num_params == 0);
4506 assert(func->impl);
4507 return func->impl;
4508 }
4509
4510 static inline nir_function *
nir_shader_get_function_for_name(const nir_shader * shader,const char * name)4511 nir_shader_get_function_for_name(const nir_shader *shader, const char *name)
4512 {
4513 nir_foreach_function(func, shader) {
4514 if (func->name && strcmp(func->name, name) == 0)
4515 return func;
4516 }
4517
4518 return NULL;
4519 }
4520
4521 /*
4522 * After all functions are forcibly inlined, these passes remove redundant
4523 * functions from a shader and library respectively.
4524 */
4525 void nir_remove_non_entrypoints(nir_shader *shader);
4526 void nir_remove_non_exported(nir_shader *shader);
4527
4528 nir_shader *nir_shader_create(void *mem_ctx,
4529 gl_shader_stage stage,
4530 const nir_shader_compiler_options *options,
4531 shader_info *si);
4532
4533 /** Adds a variable to the appropriate list in nir_shader */
4534 void nir_shader_add_variable(nir_shader *shader, nir_variable *var);
4535
4536 static inline void
nir_function_impl_add_variable(nir_function_impl * impl,nir_variable * var)4537 nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)
4538 {
4539 assert(var->data.mode == nir_var_function_temp);
4540 exec_list_push_tail(&impl->locals, &var->node);
4541 }
4542
4543 /** creates a variable, sets a few defaults, and adds it to the list */
4544 nir_variable *nir_variable_create(nir_shader *shader,
4545 nir_variable_mode mode,
4546 const struct glsl_type *type,
4547 const char *name);
4548 /** creates a local variable and adds it to the list */
4549 nir_variable *nir_local_variable_create(nir_function_impl *impl,
4550 const struct glsl_type *type,
4551 const char *name);
4552
4553 /** Creates a uniform builtin state variable. */
4554 nir_variable *
4555 nir_state_variable_create(nir_shader *shader,
4556 const struct glsl_type *type,
4557 const char *name,
4558 const gl_state_index16 tokens[STATE_LENGTH]);
4559
4560 /* Gets the variable for the given mode and location, creating it (with the given
4561 * type) if necessary.
4562 */
4563 nir_variable *
4564 nir_get_variable_with_location(nir_shader *shader, nir_variable_mode mode, int location,
4565 const struct glsl_type *type);
4566
4567 /* Creates a variable for the given mode and location.
4568 */
4569 nir_variable *
4570 nir_create_variable_with_location(nir_shader *shader, nir_variable_mode mode, int location,
4571 const struct glsl_type *type);
4572
4573 nir_variable *nir_find_variable_with_location(nir_shader *shader,
4574 nir_variable_mode mode,
4575 unsigned location);
4576
4577 nir_variable *nir_find_variable_with_driver_location(nir_shader *shader,
4578 nir_variable_mode mode,
4579 unsigned location);
4580
4581 nir_variable *nir_find_state_variable(nir_shader *s,
4582 gl_state_index16 tokens[STATE_LENGTH]);
4583
4584 nir_variable *nir_find_sampler_variable_with_tex_index(nir_shader *shader,
4585 unsigned texture_index);
4586
4587 void nir_sort_variables_with_modes(nir_shader *shader,
4588 int (*compar)(const nir_variable *,
4589 const nir_variable *),
4590 nir_variable_mode modes);
4591
4592 /** creates a function and adds it to the shader's list of functions */
4593 nir_function *nir_function_create(nir_shader *shader, const char *name);
4594
4595 static inline void
nir_function_set_impl(nir_function * func,nir_function_impl * impl)4596 nir_function_set_impl(nir_function *func, nir_function_impl *impl)
4597 {
4598 func->impl = impl;
4599 impl->function = func;
4600 }
4601
4602 nir_function_impl *nir_function_impl_create(nir_function *func);
4603 /** creates a function_impl that isn't tied to any particular function */
4604 nir_function_impl *nir_function_impl_create_bare(nir_shader *shader);
4605
4606 nir_block *nir_block_create(nir_shader *shader);
4607 nir_if *nir_if_create(nir_shader *shader);
4608 nir_loop *nir_loop_create(nir_shader *shader);
4609
4610 nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
4611
4612 /** requests that the given pieces of metadata be generated */
4613 void nir_metadata_require(nir_function_impl *impl, nir_metadata required, ...);
4614 /** dirties all but the preserved metadata */
4615 void nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved);
4616 /** Preserves all metadata for the given shader */
4617 void nir_shader_preserve_all_metadata(nir_shader *shader);
4618
4619 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
4620 nir_alu_instr *nir_alu_instr_create(nir_shader *shader, nir_op op);
4621
4622 nir_deref_instr *nir_deref_instr_create(nir_shader *shader,
4623 nir_deref_type deref_type);
4624
4625 nir_jump_instr *nir_jump_instr_create(nir_shader *shader, nir_jump_type type);
4626
4627 nir_load_const_instr *nir_load_const_instr_create(nir_shader *shader,
4628 unsigned num_components,
4629 unsigned bit_size);
4630
4631 nir_intrinsic_instr *nir_intrinsic_instr_create(nir_shader *shader,
4632 nir_intrinsic_op op);
4633
4634 nir_call_instr *nir_call_instr_create(nir_shader *shader,
4635 nir_function *callee);
4636
4637 /** Creates a NIR texture instruction */
4638 nir_tex_instr *nir_tex_instr_create(nir_shader *shader, unsigned num_srcs);
4639
4640 nir_phi_instr *nir_phi_instr_create(nir_shader *shader);
4641 nir_phi_src *nir_phi_instr_add_src(nir_phi_instr *instr,
4642 nir_block *pred, nir_def *src);
4643
4644 nir_parallel_copy_instr *nir_parallel_copy_instr_create(nir_shader *shader);
4645
4646 nir_debug_info_instr *nir_debug_info_instr_create(nir_shader *shader,
4647 nir_debug_info_type type,
4648 uint32_t string_length);
4649
4650 nir_undef_instr *nir_undef_instr_create(nir_shader *shader,
4651 unsigned num_components,
4652 unsigned bit_size);
4653
4654 nir_const_value nir_alu_binop_identity(nir_op binop, unsigned bit_size);
4655
4656 /**
4657 * NIR Cursors and Instruction Insertion API
4658 * @{
4659 *
4660 * A tiny struct representing a point to insert/extract instructions or
4661 * control flow nodes. Helps reduce the combinatorial explosion of possible
4662 * points to insert/extract.
4663 *
4664 * \sa nir_control_flow.h
4665 */
4666 typedef enum {
4667 nir_cursor_before_block,
4668 nir_cursor_after_block,
4669 nir_cursor_before_instr,
4670 nir_cursor_after_instr,
4671 } nir_cursor_option;
4672
4673 typedef struct {
4674 nir_cursor_option option;
4675 union {
4676 nir_block *block;
4677 nir_instr *instr;
4678 };
4679 } nir_cursor;
4680
4681 static inline nir_block *
nir_cursor_current_block(nir_cursor cursor)4682 nir_cursor_current_block(nir_cursor cursor)
4683 {
4684 if (cursor.option == nir_cursor_before_instr ||
4685 cursor.option == nir_cursor_after_instr) {
4686 return cursor.instr->block;
4687 } else {
4688 return cursor.block;
4689 }
4690 }
4691
4692 bool nir_cursors_equal(nir_cursor a, nir_cursor b);
4693
4694 static inline nir_cursor
nir_before_block(nir_block * block)4695 nir_before_block(nir_block *block)
4696 {
4697 nir_cursor cursor;
4698 cursor.option = nir_cursor_before_block;
4699 cursor.block = block;
4700 return cursor;
4701 }
4702
4703 static inline nir_cursor
nir_after_block(nir_block * block)4704 nir_after_block(nir_block *block)
4705 {
4706 nir_cursor cursor;
4707 cursor.option = nir_cursor_after_block;
4708 cursor.block = block;
4709 return cursor;
4710 }
4711
4712 static inline nir_cursor
nir_before_instr(nir_instr * instr)4713 nir_before_instr(nir_instr *instr)
4714 {
4715 nir_cursor cursor;
4716 cursor.option = nir_cursor_before_instr;
4717 cursor.instr = instr;
4718 return cursor;
4719 }
4720
4721 static inline nir_cursor
nir_after_instr(nir_instr * instr)4722 nir_after_instr(nir_instr *instr)
4723 {
4724 nir_cursor cursor;
4725 cursor.option = nir_cursor_after_instr;
4726 cursor.instr = instr;
4727 return cursor;
4728 }
4729
4730 static inline nir_cursor
nir_before_block_after_phis(nir_block * block)4731 nir_before_block_after_phis(nir_block *block)
4732 {
4733 nir_phi_instr *last_phi = nir_block_last_phi_instr(block);
4734 if (last_phi)
4735 return nir_after_instr(&last_phi->instr);
4736 else
4737 return nir_before_block(block);
4738 }
4739
4740 static inline nir_cursor
nir_after_block_before_jump(nir_block * block)4741 nir_after_block_before_jump(nir_block *block)
4742 {
4743 nir_instr *last_instr = nir_block_last_instr(block);
4744 if (last_instr && last_instr->type == nir_instr_type_jump) {
4745 return nir_before_instr(last_instr);
4746 } else {
4747 return nir_after_block(block);
4748 }
4749 }
4750
4751 static inline nir_cursor
nir_before_src(nir_src * src)4752 nir_before_src(nir_src *src)
4753 {
4754 if (nir_src_is_if(src)) {
4755 nir_block *prev_block =
4756 nir_cf_node_as_block(nir_cf_node_prev(&nir_src_parent_if(src)->cf_node));
4757 return nir_after_block(prev_block);
4758 } else if (nir_src_parent_instr(src)->type == nir_instr_type_phi) {
4759 #ifndef NDEBUG
4760 nir_phi_instr *cond_phi = nir_instr_as_phi(nir_src_parent_instr(src));
4761 bool found = false;
4762 nir_foreach_phi_src(phi_src, cond_phi) {
4763 if (phi_src->src.ssa == src->ssa) {
4764 found = true;
4765 break;
4766 }
4767 }
4768 assert(found);
4769 #endif
4770 /* The list_entry() macro is a generic container-of macro, it just happens
4771 * to have a more specific name.
4772 */
4773 nir_phi_src *phi_src = list_entry(src, nir_phi_src, src);
4774 return nir_after_block_before_jump(phi_src->pred);
4775 } else {
4776 return nir_before_instr(nir_src_parent_instr(src));
4777 }
4778 }
4779
4780 static inline nir_cursor
nir_before_cf_node(nir_cf_node * node)4781 nir_before_cf_node(nir_cf_node *node)
4782 {
4783 if (node->type == nir_cf_node_block)
4784 return nir_before_block(nir_cf_node_as_block(node));
4785
4786 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node)));
4787 }
4788
4789 static inline nir_cursor
nir_after_cf_node(nir_cf_node * node)4790 nir_after_cf_node(nir_cf_node *node)
4791 {
4792 if (node->type == nir_cf_node_block)
4793 return nir_after_block(nir_cf_node_as_block(node));
4794
4795 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node)));
4796 }
4797
4798 static inline nir_cursor
nir_after_phis(nir_block * block)4799 nir_after_phis(nir_block *block)
4800 {
4801 nir_foreach_instr(instr, block) {
4802 if (instr->type != nir_instr_type_phi)
4803 return nir_before_instr(instr);
4804 }
4805 return nir_after_block(block);
4806 }
4807
4808 static inline nir_cursor
nir_after_instr_and_phis(nir_instr * instr)4809 nir_after_instr_and_phis(nir_instr *instr)
4810 {
4811 if (instr->type == nir_instr_type_phi)
4812 return nir_after_phis(instr->block);
4813 else
4814 return nir_after_instr(instr);
4815 }
4816
4817 static inline nir_cursor
nir_after_cf_node_and_phis(nir_cf_node * node)4818 nir_after_cf_node_and_phis(nir_cf_node *node)
4819 {
4820 if (node->type == nir_cf_node_block)
4821 return nir_after_block(nir_cf_node_as_block(node));
4822
4823 nir_block *block = nir_cf_node_as_block(nir_cf_node_next(node));
4824
4825 return nir_after_phis(block);
4826 }
4827
4828 static inline nir_cursor
nir_before_cf_list(struct exec_list * cf_list)4829 nir_before_cf_list(struct exec_list *cf_list)
4830 {
4831 nir_cf_node *first_node = exec_node_data(nir_cf_node,
4832 exec_list_get_head(cf_list), node);
4833 return nir_before_cf_node(first_node);
4834 }
4835
4836 static inline nir_cursor
nir_after_cf_list(struct exec_list * cf_list)4837 nir_after_cf_list(struct exec_list *cf_list)
4838 {
4839 nir_cf_node *last_node = exec_node_data(nir_cf_node,
4840 exec_list_get_tail(cf_list), node);
4841 return nir_after_cf_node(last_node);
4842 }
4843
4844 static inline nir_cursor
nir_before_impl(nir_function_impl * impl)4845 nir_before_impl(nir_function_impl *impl)
4846 {
4847 return nir_before_cf_list(&impl->body);
4848 }
4849
4850 static inline nir_cursor
nir_after_impl(nir_function_impl * impl)4851 nir_after_impl(nir_function_impl *impl)
4852 {
4853 return nir_after_cf_list(&impl->body);
4854 }
4855
4856 /**
4857 * Insert a NIR instruction at the given cursor.
4858 *
4859 * Note: This does not update the cursor.
4860 */
4861 void nir_instr_insert(nir_cursor cursor, nir_instr *instr);
4862
4863 bool nir_instr_move(nir_cursor cursor, nir_instr *instr);
4864
4865 static inline void
nir_instr_insert_before(nir_instr * instr,nir_instr * before)4866 nir_instr_insert_before(nir_instr *instr, nir_instr *before)
4867 {
4868 nir_instr_insert(nir_before_instr(instr), before);
4869 }
4870
4871 static inline void
nir_instr_insert_after(nir_instr * instr,nir_instr * after)4872 nir_instr_insert_after(nir_instr *instr, nir_instr *after)
4873 {
4874 nir_instr_insert(nir_after_instr(instr), after);
4875 }
4876
4877 static inline void
nir_instr_insert_before_block(nir_block * block,nir_instr * before)4878 nir_instr_insert_before_block(nir_block *block, nir_instr *before)
4879 {
4880 nir_instr_insert(nir_before_block(block), before);
4881 }
4882
4883 static inline void
nir_instr_insert_after_block(nir_block * block,nir_instr * after)4884 nir_instr_insert_after_block(nir_block *block, nir_instr *after)
4885 {
4886 nir_instr_insert(nir_after_block(block), after);
4887 }
4888
4889 static inline void
nir_instr_insert_before_cf(nir_cf_node * node,nir_instr * before)4890 nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
4891 {
4892 nir_instr_insert(nir_before_cf_node(node), before);
4893 }
4894
4895 static inline void
nir_instr_insert_after_cf(nir_cf_node * node,nir_instr * after)4896 nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
4897 {
4898 nir_instr_insert(nir_after_cf_node(node), after);
4899 }
4900
4901 static inline void
nir_instr_insert_before_cf_list(struct exec_list * list,nir_instr * before)4902 nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
4903 {
4904 nir_instr_insert(nir_before_cf_list(list), before);
4905 }
4906
4907 static inline void
nir_instr_insert_after_cf_list(struct exec_list * list,nir_instr * after)4908 nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
4909 {
4910 nir_instr_insert(nir_after_cf_list(list), after);
4911 }
4912
4913 void nir_instr_remove_v(nir_instr *instr);
4914 void nir_instr_free(nir_instr *instr);
4915 void nir_instr_free_list(struct exec_list *list);
4916
4917 static inline nir_cursor
nir_instr_remove(nir_instr * instr)4918 nir_instr_remove(nir_instr *instr)
4919 {
4920 nir_cursor cursor;
4921 nir_instr *prev = nir_instr_prev(instr);
4922 if (prev) {
4923 cursor = nir_after_instr(prev);
4924 } else {
4925 cursor = nir_before_block(instr->block);
4926 }
4927 nir_instr_remove_v(instr);
4928 return cursor;
4929 }
4930
4931 nir_cursor nir_instr_free_and_dce(nir_instr *instr);
4932
4933 /** @} */
4934
4935 nir_def *nir_instr_def(nir_instr *instr);
4936
4937 typedef bool (*nir_foreach_def_cb)(nir_def *def, void *state);
4938 typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
4939 static inline bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
4940 bool nir_foreach_phi_src_leaving_block(nir_block *instr,
4941 nir_foreach_src_cb cb,
4942 void *state);
4943
4944 nir_const_value *nir_src_as_const_value(nir_src src);
4945
4946 #define NIR_SRC_AS_(name, c_type, type_enum, cast_macro) \
4947 static inline c_type * \
4948 nir_src_as_##name(nir_src src) \
4949 { \
4950 return src.ssa->parent_instr->type == type_enum \
4951 ? cast_macro(src.ssa->parent_instr) \
4952 : NULL; \
4953 }
4954
4955 NIR_SRC_AS_(alu_instr, nir_alu_instr, nir_instr_type_alu, nir_instr_as_alu)
4956 NIR_SRC_AS_(intrinsic, nir_intrinsic_instr,
4957 nir_instr_type_intrinsic, nir_instr_as_intrinsic)
4958 NIR_SRC_AS_(deref, nir_deref_instr, nir_instr_type_deref, nir_instr_as_deref)
4959 NIR_SRC_AS_(debug_info, nir_debug_info_instr, nir_instr_type_debug_info, nir_instr_as_debug_info)
4960
4961 const char *nir_src_as_string(nir_src src);
4962
4963 bool nir_src_is_always_uniform(nir_src src);
4964 bool nir_srcs_equal(nir_src src1, nir_src src2);
4965 bool nir_instrs_equal(const nir_instr *instr1, const nir_instr *instr2);
4966 nir_block *nir_src_get_block(nir_src *src);
4967
4968 static inline void
nir_src_rewrite(nir_src * src,nir_def * new_ssa)4969 nir_src_rewrite(nir_src *src, nir_def *new_ssa)
4970 {
4971 assert(src->ssa);
4972 assert(nir_src_is_if(src) ? (nir_src_parent_if(src) != NULL) : (nir_src_parent_instr(src) != NULL));
4973 list_del(&src->use_link);
4974 src->ssa = new_ssa;
4975 list_addtail(&src->use_link, &new_ssa->uses);
4976 }
4977
4978 /** Initialize a nir_src
4979 *
4980 * This is almost never the helper you want to use. This helper assumes that
4981 * the source is uninitialized garbage and blasts over it without doing any
4982 * tear-down the existing source, including removing it from uses lists.
4983 * Using this helper on a source that currently exists in any uses list will
4984 * result in linked list corruption. It also assumes that the instruction is
4985 * currently live in the IR and adds the source to the uses list for the given
4986 * nir_def as part of setup.
4987 *
4988 * This is pretty much only useful for adding sources to extant instructions
4989 * or manipulating parallel copy instructions as part of out-of-SSA.
4990 *
4991 * When in doubt, use nir_src_rewrite() instead.
4992 */
4993 void nir_instr_init_src(nir_instr *instr, nir_src *src, nir_def *def);
4994
4995 /** Clear a nir_src
4996 *
4997 * This helper clears a nir_src by removing it from any uses lists and
4998 * resetting its contents to NIR_SRC_INIT. This is typically used as a
4999 * precursor to removing the source from the instruction by adjusting a
5000 * num_srcs parameter somewhere or overwriting it with nir_instr_move_src().
5001 */
5002 void nir_instr_clear_src(nir_instr *instr, nir_src *src);
5003
5004 void nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src);
5005
5006 void nir_def_init(nir_instr *instr, nir_def *def,
5007 unsigned num_components, unsigned bit_size);
5008 static inline void
nir_def_init_for_type(nir_instr * instr,nir_def * def,const struct glsl_type * type)5009 nir_def_init_for_type(nir_instr *instr, nir_def *def,
5010 const struct glsl_type *type)
5011 {
5012 assert(glsl_type_is_vector_or_scalar(type));
5013 nir_def_init(instr, def, glsl_get_components(type),
5014 glsl_get_bit_size(type));
5015 }
5016 void nir_def_rewrite_uses(nir_def *def, nir_def *new_ssa);
5017 void nir_def_rewrite_uses_src(nir_def *def, nir_src new_src);
5018 void nir_def_rewrite_uses_after(nir_def *def, nir_def *new_ssa,
5019 nir_instr *after_me);
5020
5021 static inline void
nir_def_replace(nir_def * def,nir_def * new_ssa)5022 nir_def_replace(nir_def *def, nir_def *new_ssa)
5023 {
5024 nir_def_rewrite_uses(def, new_ssa);
5025 nir_instr_remove(def->parent_instr);
5026 }
5027
5028 nir_component_mask_t nir_src_components_read(const nir_src *src);
5029 nir_component_mask_t nir_def_components_read(const nir_def *def);
5030 bool nir_def_all_uses_are_fsat(const nir_def *def);
5031
5032 static inline bool
nir_def_is_unused(nir_def * ssa)5033 nir_def_is_unused(nir_def *ssa)
5034 {
5035 return list_is_empty(&ssa->uses);
5036 }
5037
5038 /** Sorts unstructured blocks
5039 *
5040 * NIR requires that unstructured blocks be sorted in reverse post
5041 * depth-first-search order. This is the standard ordering used in the
5042 * compiler literature which guarantees dominance. In particular, reverse
5043 * post-DFS order guarantees that dominators occur in the list before the
5044 * blocks they dominate.
5045 *
5046 * NOTE: This function also implicitly deletes any unreachable blocks.
5047 */
5048 void nir_sort_unstructured_blocks(nir_function_impl *impl);
5049
5050 /** Returns the next block
5051 *
5052 * For structured control-flow, this follows the same order as
5053 * nir_block_cf_tree_next(). For unstructured control-flow the blocks are in
5054 * reverse post-DFS order. (See nir_sort_unstructured_blocks() above.)
5055 */
5056 nir_block *nir_block_unstructured_next(nir_block *block);
5057 nir_block *nir_unstructured_start_block(nir_function_impl *impl);
5058
5059 #define nir_foreach_block_unstructured(block, impl) \
5060 for (nir_block *block = nir_unstructured_start_block(impl); block != NULL; \
5061 block = nir_block_unstructured_next(block))
5062
5063 #define nir_foreach_block_unstructured_safe(block, impl) \
5064 for (nir_block *block = nir_unstructured_start_block(impl), \
5065 *next = nir_block_unstructured_next(block); \
5066 block != NULL; \
5067 block = next, next = nir_block_unstructured_next(block))
5068
5069 /*
5070 * finds the next basic block in source-code order, returns NULL if there is
5071 * none
5072 */
5073
5074 nir_block *nir_block_cf_tree_next(nir_block *block);
5075
5076 /* Performs the opposite of nir_block_cf_tree_next() */
5077
5078 nir_block *nir_block_cf_tree_prev(nir_block *block);
5079
5080 /* Gets the first block in a CF node in source-code order */
5081
5082 nir_block *nir_cf_node_cf_tree_first(nir_cf_node *node);
5083
5084 /* Gets the last block in a CF node in source-code order */
5085
5086 nir_block *nir_cf_node_cf_tree_last(nir_cf_node *node);
5087
5088 /* Gets the next block after a CF node in source-code order */
5089
5090 nir_block *nir_cf_node_cf_tree_next(nir_cf_node *node);
5091
5092 /* Gets the block before a CF node in source-code order */
5093
5094 nir_block *nir_cf_node_cf_tree_prev(nir_cf_node *node);
5095
5096 /* Macros for loops that visit blocks in source-code order */
5097
5098 #define nir_foreach_block(block, impl) \
5099 for (nir_block *block = nir_start_block(impl); block != NULL; \
5100 block = nir_block_cf_tree_next(block))
5101
5102 #define nir_foreach_block_safe(block, impl) \
5103 for (nir_block *block = nir_start_block(impl), \
5104 *next = nir_block_cf_tree_next(block); \
5105 block != NULL; \
5106 block = next, next = nir_block_cf_tree_next(block))
5107
5108 #define nir_foreach_block_reverse(block, impl) \
5109 for (nir_block *block = nir_impl_last_block(impl); block != NULL; \
5110 block = nir_block_cf_tree_prev(block))
5111
5112 #define nir_foreach_block_reverse_safe(block, impl) \
5113 for (nir_block *block = nir_impl_last_block(impl), \
5114 *prev = nir_block_cf_tree_prev(block); \
5115 block != NULL; \
5116 block = prev, prev = nir_block_cf_tree_prev(block))
5117
5118 #define nir_foreach_block_in_cf_node(block, node) \
5119 for (nir_block *block = nir_cf_node_cf_tree_first(node); \
5120 block != nir_cf_node_cf_tree_next(node); \
5121 block = nir_block_cf_tree_next(block))
5122
5123 #define nir_foreach_block_in_cf_node_safe(block, node) \
5124 for (nir_block *block = nir_cf_node_cf_tree_first(node), \
5125 *next = nir_block_cf_tree_next(block); \
5126 block != nir_cf_node_cf_tree_next(node); \
5127 block = next, next = nir_block_cf_tree_next(block))
5128
5129 #define nir_foreach_block_in_cf_node_reverse(block, node) \
5130 for (nir_block *block = nir_cf_node_cf_tree_last(node); \
5131 block != nir_cf_node_cf_tree_prev(node); \
5132 block = nir_block_cf_tree_prev(block))
5133
5134 #define nir_foreach_block_in_cf_node_reverse_safe(block, node) \
5135 for (nir_block *block = nir_cf_node_cf_tree_last(node), \
5136 *prev = nir_block_cf_tree_prev(block); \
5137 block != nir_cf_node_cf_tree_prev(node); \
5138 block = prev, prev = nir_block_cf_tree_prev(block))
5139
5140 /* If the following CF node is an if, this function returns that if.
5141 * Otherwise, it returns NULL.
5142 */
5143 nir_if *nir_block_get_following_if(nir_block *block);
5144
5145 nir_loop *nir_block_get_following_loop(nir_block *block);
5146
5147 nir_block **nir_block_get_predecessors_sorted(const nir_block *block, void *mem_ctx);
5148
5149 void nir_index_ssa_defs(nir_function_impl *impl);
5150 unsigned nir_index_instrs(nir_function_impl *impl);
5151
5152 void nir_index_blocks(nir_function_impl *impl);
5153
5154 void nir_shader_clear_pass_flags(nir_shader *shader);
5155
5156 unsigned nir_shader_index_vars(nir_shader *shader, nir_variable_mode modes);
5157 unsigned nir_function_impl_index_vars(nir_function_impl *impl);
5158
5159 void nir_print_shader(nir_shader *shader, FILE *fp);
5160 void nir_print_shader_annotated(nir_shader *shader, FILE *fp, struct hash_table *errors);
5161 void nir_print_instr(const nir_instr *instr, FILE *fp);
5162 void nir_print_deref(const nir_deref_instr *deref, FILE *fp);
5163 void nir_log_shader_annotated_tagged(enum mesa_log_level level, const char *tag, nir_shader *shader, struct hash_table *annotations);
5164 #define nir_log_shadere(s) nir_log_shader_annotated_tagged(MESA_LOG_ERROR, (MESA_LOG_TAG), (s), NULL)
5165 #define nir_log_shaderw(s) nir_log_shader_annotated_tagged(MESA_LOG_WARN, (MESA_LOG_TAG), (s), NULL)
5166 #define nir_log_shaderi(s) nir_log_shader_annotated_tagged(MESA_LOG_INFO, (MESA_LOG_TAG), (s), NULL)
5167 #define nir_log_shader_annotated(s, annotations) nir_log_shader_annotated_tagged(MESA_LOG_ERROR, (MESA_LOG_TAG), (s), annotations)
5168
5169 char *nir_shader_as_str(nir_shader *nir, void *mem_ctx);
5170 char *nir_shader_as_str_annotated(nir_shader *nir, struct hash_table *annotations, void *mem_ctx);
5171 char *nir_instr_as_str(const nir_instr *instr, void *mem_ctx);
5172
5173 char *nir_shader_gather_debug_info(nir_shader *shader, const char *filename);
5174
5175 /** Shallow clone of a single instruction. */
5176 nir_instr *nir_instr_clone(nir_shader *s, const nir_instr *orig);
5177
5178 /** Clone a single instruction, including a remap table to rewrite sources. */
5179 nir_instr *nir_instr_clone_deep(nir_shader *s, const nir_instr *orig,
5180 struct hash_table *remap_table);
5181
5182 /** Shallow clone of a single ALU instruction. */
5183 nir_alu_instr *nir_alu_instr_clone(nir_shader *s, const nir_alu_instr *orig);
5184
5185 nir_shader *nir_shader_clone(void *mem_ctx, const nir_shader *s);
5186 nir_function *nir_function_clone(nir_shader *ns, const nir_function *fxn);
5187 nir_function_impl *nir_function_impl_clone(nir_shader *shader,
5188 const nir_function_impl *fi);
5189 nir_constant *nir_constant_clone(const nir_constant *c, nir_variable *var);
5190 nir_variable *nir_variable_clone(const nir_variable *c, nir_shader *shader);
5191
5192 void nir_shader_replace(nir_shader *dest, nir_shader *src);
5193
5194 void nir_shader_serialize_deserialize(nir_shader *s);
5195
5196 #ifndef NDEBUG
5197 void nir_validate_shader(nir_shader *shader, const char *when);
5198 void nir_validate_ssa_dominance(nir_shader *shader, const char *when);
5199 void nir_metadata_set_validation_flag(nir_shader *shader);
5200 void nir_metadata_check_validation_flag(nir_shader *shader);
5201
5202 static inline bool
should_skip_nir(const char * name)5203 should_skip_nir(const char *name)
5204 {
5205 static const char *list = NULL;
5206 if (!list) {
5207 /* Comma separated list of names to skip. */
5208 list = getenv("NIR_SKIP");
5209 if (!list)
5210 list = "";
5211 }
5212
5213 if (!list[0])
5214 return false;
5215
5216 return comma_separated_list_contains(list, name);
5217 }
5218
5219 static inline bool
should_print_nir(nir_shader * shader)5220 should_print_nir(nir_shader *shader)
5221 {
5222 if ((shader->info.internal && !NIR_DEBUG(PRINT_INTERNAL)) ||
5223 shader->info.stage < 0 ||
5224 shader->info.stage > MESA_SHADER_KERNEL)
5225 return false;
5226
5227 return unlikely(nir_debug_print_shader[shader->info.stage]);
5228 }
5229 #else
5230 static inline void
nir_validate_shader(nir_shader * shader,const char * when)5231 nir_validate_shader(nir_shader *shader, const char *when)
5232 {
5233 (void)shader;
5234 (void)when;
5235 }
5236 static inline void
nir_validate_ssa_dominance(nir_shader * shader,const char * when)5237 nir_validate_ssa_dominance(nir_shader *shader, const char *when)
5238 {
5239 (void)shader;
5240 (void)when;
5241 }
5242 static inline void
nir_metadata_set_validation_flag(nir_shader * shader)5243 nir_metadata_set_validation_flag(nir_shader *shader)
5244 {
5245 (void)shader;
5246 }
5247 static inline void
nir_metadata_check_validation_flag(nir_shader * shader)5248 nir_metadata_check_validation_flag(nir_shader *shader)
5249 {
5250 (void)shader;
5251 }
5252 static inline bool
should_skip_nir(UNUSED const char * pass_name)5253 should_skip_nir(UNUSED const char *pass_name)
5254 {
5255 return false;
5256 }
5257 static inline bool
should_print_nir(UNUSED nir_shader * shader)5258 should_print_nir(UNUSED nir_shader *shader)
5259 {
5260 return false;
5261 }
5262 #endif /* NDEBUG */
5263
5264 #define _PASS(pass, nir, do_pass) \
5265 do { \
5266 if (should_skip_nir(#pass)) { \
5267 printf("skipping %s\n", #pass); \
5268 break; \
5269 } \
5270 do_pass if (NIR_DEBUG(CLONE)) \
5271 { \
5272 nir_shader *_clone = nir_shader_clone(ralloc_parent(nir), nir);\
5273 nir_shader_replace(nir, _clone); \
5274 } \
5275 if (NIR_DEBUG(SERIALIZE)) { \
5276 nir_shader_serialize_deserialize(nir); \
5277 } \
5278 } while (0)
5279
5280 #define NIR_PASS(progress, nir, pass, ...) _PASS(pass, nir, { \
5281 nir_metadata_set_validation_flag(nir); \
5282 if (should_print_nir(nir)) \
5283 printf("%s\n", #pass); \
5284 if (pass(nir, ##__VA_ARGS__)) { \
5285 nir_validate_shader(nir, "after " #pass " in " __FILE__); \
5286 UNUSED bool _; \
5287 progress = true; \
5288 if (should_print_nir(nir)) \
5289 nir_print_shader(nir, stdout); \
5290 nir_metadata_check_validation_flag(nir); \
5291 } \
5292 })
5293
5294 #define NIR_PASS_V(nir, pass, ...) _PASS(pass, nir, { \
5295 if (should_print_nir(nir)) \
5296 printf("%s\n", #pass); \
5297 pass(nir, ##__VA_ARGS__); \
5298 nir_validate_shader(nir, "after " #pass " in " __FILE__); \
5299 if (should_print_nir(nir)) \
5300 nir_print_shader(nir, stdout); \
5301 })
5302
5303 #define _NIR_LOOP_PASS(progress, idempotent, skip, nir, pass, ...) \
5304 do { \
5305 bool nir_loop_pass_progress = false; \
5306 if (!_mesa_set_search(skip, (void (*)())&pass)) \
5307 NIR_PASS(nir_loop_pass_progress, nir, pass, ##__VA_ARGS__); \
5308 if (nir_loop_pass_progress) \
5309 _mesa_set_clear(skip, NULL); \
5310 if (idempotent || !nir_loop_pass_progress) \
5311 _mesa_set_add(skip, (void (*)())&pass); \
5312 UNUSED bool _ = false; \
5313 progress |= nir_loop_pass_progress; \
5314 } while (0)
5315
5316 /* Helper to skip a pass if no different passes have made progress since it was
5317 * previously run. Note that two passes are considered the same if they have
5318 * the same function pointer, even if they used different options.
5319 *
5320 * The usage of this is mostly identical to NIR_PASS. "skip" is a "struct set *"
5321 * (created by _mesa_pointer_set_create) which the macro uses to keep track of
5322 * already run passes.
5323 *
5324 * Example:
5325 * bool progress = true;
5326 * struct set *skip = _mesa_pointer_set_create(NULL);
5327 * while (progress) {
5328 * progress = false;
5329 * NIR_LOOP_PASS(progress, skip, nir, pass1);
5330 * NIR_LOOP_PASS_NOT_IDEMPOTENT(progress, skip, nir, nir_opt_algebraic);
5331 * NIR_LOOP_PASS(progress, skip, nir, pass2);
5332 * ...
5333 * }
5334 * _mesa_set_destroy(skip, NULL);
5335 *
5336 * You shouldn't mix usage of this with the NIR_PASS set of helpers, without
5337 * using a new "skip" in-between.
5338 */
5339 #define NIR_LOOP_PASS(progress, skip, nir, pass, ...) \
5340 _NIR_LOOP_PASS(progress, true, skip, nir, pass, ##__VA_ARGS__)
5341
5342 /* Like NIR_LOOP_PASS, but use this for passes which may make further progress
5343 * when repeated.
5344 */
5345 #define NIR_LOOP_PASS_NOT_IDEMPOTENT(progress, skip, nir, pass, ...) \
5346 _NIR_LOOP_PASS(progress, false, skip, nir, pass, ##__VA_ARGS__)
5347
5348 #define NIR_SKIP(name) should_skip_nir(#name)
5349
5350 /** An instruction filtering callback with writemask
5351 *
5352 * Returns true if the instruction should be processed with the associated
5353 * writemask and false otherwise.
5354 */
5355 typedef bool (*nir_instr_writemask_filter_cb)(const nir_instr *,
5356 unsigned writemask, const void *);
5357
5358 /** A simple instruction lowering callback
5359 *
5360 * Many instruction lowering passes can be written as a simple function which
5361 * takes an instruction as its input and returns a sequence of instructions
5362 * that implement the consumed instruction. This function type represents
5363 * such a lowering function. When called, a function with this prototype
5364 * should either return NULL indicating that no lowering needs to be done or
5365 * emit a sequence of instructions using the provided builder (whose cursor
5366 * will already be placed after the instruction to be lowered) and return the
5367 * resulting nir_def.
5368 */
5369 typedef nir_def *(*nir_lower_instr_cb)(struct nir_builder *,
5370 nir_instr *, void *);
5371
5372 /**
5373 * Special return value for nir_lower_instr_cb when some progress occurred
5374 * (like changing an input to the instr) that didn't result in a replacement
5375 * SSA def being generated.
5376 */
5377 #define NIR_LOWER_INSTR_PROGRESS ((nir_def *)(uintptr_t)1)
5378
5379 /**
5380 * Special return value for nir_lower_instr_cb when some progress occurred
5381 * that should remove the current instruction that doesn't create an output
5382 * (like a store)
5383 */
5384
5385 #define NIR_LOWER_INSTR_PROGRESS_REPLACE ((nir_def *)(uintptr_t)2)
5386
5387 /** Iterate over all the instructions in a nir_function_impl and lower them
5388 * using the provided callbacks
5389 *
5390 * This function implements the guts of a standard lowering pass for you. It
5391 * iterates over all of the instructions in a nir_function_impl and calls the
5392 * filter callback on each one. If the filter callback returns true, it then
5393 * calls the lowering call back on the instruction. (Splitting it this way
5394 * allows us to avoid some save/restore work for instructions we know won't be
5395 * lowered.) If the instruction is dead after the lowering is complete, it
5396 * will be removed. If new instructions are added, the lowering callback will
5397 * also be called on them in case multiple lowerings are required.
5398 *
5399 * If the callback indicates that the original instruction is replaced (either
5400 * through a new SSA def or NIR_LOWER_INSTR_PROGRESS_REPLACE), then the
5401 * instruction is removed along with any now-dead SSA defs it used.
5402 *
5403 * The metadata for the nir_function_impl will also be updated. If any blocks
5404 * are added (they cannot be removed), dominance and block indices will be
5405 * invalidated.
5406 */
5407 bool nir_function_impl_lower_instructions(nir_function_impl *impl,
5408 nir_instr_filter_cb filter,
5409 nir_lower_instr_cb lower,
5410 void *cb_data);
5411 bool nir_shader_lower_instructions(nir_shader *shader,
5412 nir_instr_filter_cb filter,
5413 nir_lower_instr_cb lower,
5414 void *cb_data);
5415
5416 void nir_calc_dominance_impl(nir_function_impl *impl);
5417 void nir_calc_dominance(nir_shader *shader);
5418
5419 nir_block *nir_dominance_lca(nir_block *b1, nir_block *b2);
5420 bool nir_block_dominates(nir_block *parent, nir_block *child);
5421 bool nir_block_is_unreachable(nir_block *block);
5422
5423 void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
5424 void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
5425
5426 void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
5427 void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
5428
5429 void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
5430 void nir_dump_cfg(nir_shader *shader, FILE *fp);
5431
5432 void nir_gs_count_vertices_and_primitives(const nir_shader *shader,
5433 int *out_vtxcnt,
5434 int *out_prmcnt,
5435 int *out_decomposed_prmcnt,
5436 unsigned num_streams);
5437
5438 typedef enum {
5439 nir_group_all,
5440 nir_group_same_resource_only,
5441 } nir_load_grouping;
5442
5443 void nir_group_loads(nir_shader *shader, nir_load_grouping grouping,
5444 unsigned max_distance);
5445
5446 bool nir_shrink_vec_array_vars(nir_shader *shader, nir_variable_mode modes);
5447 bool nir_split_array_vars(nir_shader *shader, nir_variable_mode modes);
5448 bool nir_split_var_copies(nir_shader *shader);
5449 bool nir_split_per_member_structs(nir_shader *shader);
5450 bool nir_split_struct_vars(nir_shader *shader, nir_variable_mode modes);
5451
5452 bool nir_lower_returns_impl(nir_function_impl *impl);
5453 bool nir_lower_returns(nir_shader *shader);
5454
5455 void nir_inline_function_impl(struct nir_builder *b,
5456 const nir_function_impl *impl,
5457 nir_def **params,
5458 struct hash_table *shader_var_remap);
5459 bool nir_inline_functions(nir_shader *shader);
5460 void nir_cleanup_functions(nir_shader *shader);
5461 bool nir_link_shader_functions(nir_shader *shader,
5462 const nir_shader *link_shader);
5463
5464 void nir_find_inlinable_uniforms(nir_shader *shader);
5465 void nir_inline_uniforms(nir_shader *shader, unsigned num_uniforms,
5466 const uint32_t *uniform_values,
5467 const uint16_t *uniform_dw_offsets);
5468 bool nir_collect_src_uniforms(const nir_src *src, int component,
5469 uint32_t *uni_offsets, uint8_t *num_offsets,
5470 unsigned max_num_bo, unsigned max_offset);
5471 void nir_add_inlinable_uniforms(const nir_src *cond, nir_loop_info *info,
5472 uint32_t *uni_offsets, uint8_t *num_offsets,
5473 unsigned max_num_bo, unsigned max_offset);
5474
5475 bool nir_propagate_invariant(nir_shader *shader, bool invariant_prim);
5476
5477 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, nir_shader *shader);
5478 void nir_lower_deref_copy_instr(struct nir_builder *b,
5479 nir_intrinsic_instr *copy);
5480 bool nir_lower_var_copies(nir_shader *shader);
5481
5482 bool nir_opt_memcpy(nir_shader *shader);
5483 bool nir_lower_memcpy(nir_shader *shader);
5484
5485 void nir_fixup_deref_modes(nir_shader *shader);
5486 void nir_fixup_deref_types(nir_shader *shader);
5487
5488 bool nir_lower_global_vars_to_local(nir_shader *shader);
5489
5490 typedef enum {
5491 nir_lower_direct_array_deref_of_vec_load = (1 << 0),
5492 nir_lower_indirect_array_deref_of_vec_load = (1 << 1),
5493 nir_lower_direct_array_deref_of_vec_store = (1 << 2),
5494 nir_lower_indirect_array_deref_of_vec_store = (1 << 3),
5495 } nir_lower_array_deref_of_vec_options;
5496
5497 bool nir_lower_array_deref_of_vec(nir_shader *shader, nir_variable_mode modes,
5498 bool (*filter)(nir_variable *),
5499 nir_lower_array_deref_of_vec_options options);
5500
5501 bool nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes,
5502 uint32_t max_lower_array_len);
5503
5504 bool nir_lower_indirect_var_derefs(nir_shader *shader,
5505 const struct set *vars);
5506
5507 bool nir_lower_locals_to_regs(nir_shader *shader, uint8_t bool_bitsize);
5508
5509 bool nir_lower_io_to_temporaries(nir_shader *shader,
5510 nir_function_impl *entrypoint,
5511 bool outputs, bool inputs);
5512
5513 bool nir_lower_vars_to_scratch(nir_shader *shader,
5514 nir_variable_mode modes,
5515 int size_threshold,
5516 glsl_type_size_align_func variable_size_align,
5517 glsl_type_size_align_func scratch_layout_size_align);
5518
5519 void nir_lower_clip_halfz(nir_shader *shader);
5520
5521 void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);
5522
5523 void nir_gather_types(nir_function_impl *impl,
5524 BITSET_WORD *float_types,
5525 BITSET_WORD *int_types);
5526
5527 void nir_assign_var_locations(nir_shader *shader, nir_variable_mode mode,
5528 unsigned *size,
5529 int (*type_size)(const struct glsl_type *, bool));
5530
5531 /* Some helpers to do very simple linking */
5532 bool nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer);
5533 bool nir_remove_unused_io_vars(nir_shader *shader, nir_variable_mode mode,
5534 uint64_t *used_by_other_stage,
5535 uint64_t *used_by_other_stage_patches);
5536 void nir_compact_varyings(nir_shader *producer, nir_shader *consumer,
5537 bool default_to_smooth_interp);
5538 void nir_link_xfb_varyings(nir_shader *producer, nir_shader *consumer);
5539 bool nir_link_opt_varyings(nir_shader *producer, nir_shader *consumer);
5540 void nir_link_varying_precision(nir_shader *producer, nir_shader *consumer);
5541 nir_variable *nir_clone_uniform_variable(nir_shader *nir,
5542 nir_variable *uniform, bool spirv);
5543 nir_deref_instr *nir_clone_deref_instr(struct nir_builder *b,
5544 nir_variable *var,
5545 nir_deref_instr *deref);
5546
5547
5548 /* Return status from nir_opt_varyings. */
5549 typedef enum {
5550 /* Whether the IR changed such that NIR optimizations should be run, such
5551 * as due to removal of loads and stores. IO semantic changes such as
5552 * compaction don't count as IR changes because they don't affect NIR
5553 * optimizations.
5554 */
5555 nir_progress_producer = BITFIELD_BIT(0),
5556 nir_progress_consumer = BITFIELD_BIT(1),
5557 } nir_opt_varyings_progress;
5558
5559 nir_opt_varyings_progress
5560 nir_opt_varyings(nir_shader *producer, nir_shader *consumer, bool spirv,
5561 unsigned max_uniform_components, unsigned max_ubos_per_stage);
5562
5563 bool nir_slot_is_sysval_output(gl_varying_slot slot,
5564 gl_shader_stage next_shader);
5565 bool nir_slot_is_varying(gl_varying_slot slot);
5566 bool nir_slot_is_sysval_output_and_varying(gl_varying_slot slot,
5567 gl_shader_stage next_shader);
5568 bool nir_remove_varying(nir_intrinsic_instr *intr, gl_shader_stage next_shader);
5569 bool nir_remove_sysval_output(nir_intrinsic_instr *intr);
5570
5571 bool nir_lower_amul(nir_shader *shader,
5572 int (*type_size)(const struct glsl_type *, bool));
5573
5574 bool nir_lower_ubo_vec4(nir_shader *shader);
5575
5576 void nir_sort_variables_by_location(nir_shader *shader, nir_variable_mode mode);
5577 void nir_assign_io_var_locations(nir_shader *shader,
5578 nir_variable_mode mode,
5579 unsigned *size,
5580 gl_shader_stage stage);
5581
5582 typedef enum {
5583 /* If set, this causes all 64-bit IO operations to be lowered on-the-fly
5584 * to 32-bit operations. This is only valid for nir_var_shader_in/out
5585 * modes.
5586 *
5587 * Note that this destroys dual-slot information i.e. whether an input
5588 * occupies the low or high half of dvec4. Instead, it adds an offset of 1
5589 * to the load (which is ambiguous) and expects driver locations of inputs
5590 * to be final, which prevents any further optimizations.
5591 *
5592 * TODO: remove this in favor of nir_lower_io_lower_64bit_to_32_new.
5593 */
5594 nir_lower_io_lower_64bit_to_32 = (1 << 0),
5595
5596 /* If set, this causes the subset of 64-bit IO operations involving floats to be lowered on-the-fly
5597 * to 32-bit operations. This is only valid for nir_var_shader_in/out
5598 * modes.
5599 */
5600 nir_lower_io_lower_64bit_float_to_32 = (1 << 1),
5601
5602 /* This causes all 64-bit IO operations to be lowered to 32-bit operations.
5603 * This is only valid for nir_var_shader_in/out modes.
5604 *
5605 * Only VS inputs: Dual slot information is preserved as nir_io_semantics::
5606 * high_dvec2 and gathered into shader_info::dual_slot_inputs, so that
5607 * the shader can be arbitrarily optimized and the low or high half of
5608 * dvec4 can be DCE'd independently without affecting the other half.
5609 */
5610 nir_lower_io_lower_64bit_to_32_new = (1 << 2),
5611 } nir_lower_io_options;
5612 bool nir_lower_io(nir_shader *shader,
5613 nir_variable_mode modes,
5614 int (*type_size)(const struct glsl_type *, bool),
5615 nir_lower_io_options);
5616
5617 bool nir_io_add_const_offset_to_base(nir_shader *nir, nir_variable_mode modes);
5618 bool nir_lower_color_inputs(nir_shader *nir);
5619 void nir_lower_io_passes(nir_shader *nir, bool renumber_vs_inputs);
5620 bool nir_io_add_intrinsic_xfb_info(nir_shader *nir);
5621
5622 bool
5623 nir_lower_vars_to_explicit_types(nir_shader *shader,
5624 nir_variable_mode modes,
5625 glsl_type_size_align_func type_info);
5626 void
5627 nir_gather_explicit_io_initializers(nir_shader *shader,
5628 void *dst, size_t dst_size,
5629 nir_variable_mode mode);
5630
5631 bool nir_lower_vec3_to_vec4(nir_shader *shader, nir_variable_mode modes);
5632
5633 typedef enum {
5634 /**
5635 * An address format which is a simple 32-bit global GPU address.
5636 */
5637 nir_address_format_32bit_global,
5638
5639 /**
5640 * An address format which is a simple 64-bit global GPU address.
5641 */
5642 nir_address_format_64bit_global,
5643
5644 /**
5645 * An address format which is a 64-bit global GPU address encoded as a
5646 * 2x32-bit vector.
5647 */
5648 nir_address_format_2x32bit_global,
5649
5650 /**
5651 * An address format which is a 64-bit global base address and a 32-bit
5652 * offset.
5653 *
5654 * This is identical to 64bit_bounded_global except that bounds checking
5655 * is not applied when lowering to global access. Even though the size is
5656 * never used for an actual bounds check, it needs to be valid so we can
5657 * lower deref_buffer_array_length properly.
5658 */
5659 nir_address_format_64bit_global_32bit_offset,
5660
5661 /**
5662 * An address format which is a bounds-checked 64-bit global GPU address.
5663 *
5664 * The address is comprised as a 32-bit vec4 where .xy are a uint64_t base
5665 * address stored with the low bits in .x and high bits in .y, .z is a
5666 * size, and .w is an offset. When the final I/O operation is lowered, .w
5667 * is checked against .z and the operation is predicated on the result.
5668 */
5669 nir_address_format_64bit_bounded_global,
5670
5671 /**
5672 * An address format which is comprised of a vec2 where the first
5673 * component is a buffer index and the second is an offset.
5674 */
5675 nir_address_format_32bit_index_offset,
5676
5677 /**
5678 * An address format which is a 64-bit value, where the high 32 bits
5679 * are a buffer index, and the low 32 bits are an offset.
5680 */
5681 nir_address_format_32bit_index_offset_pack64,
5682
5683 /**
5684 * An address format which is comprised of a vec3 where the first two
5685 * components specify the buffer and the third is an offset.
5686 */
5687 nir_address_format_vec2_index_32bit_offset,
5688
5689 /**
5690 * An address format which represents generic pointers with a 62-bit
5691 * pointer and a 2-bit enum in the top two bits. The top two bits have
5692 * the following meanings:
5693 *
5694 * - 0x0: Global memory
5695 * - 0x1: Shared memory
5696 * - 0x2: Scratch memory
5697 * - 0x3: Global memory
5698 *
5699 * The redundancy between 0x0 and 0x3 is because of Intel sign-extension of
5700 * addresses. Valid global memory addresses may naturally have either 0 or
5701 * ~0 as their high bits.
5702 *
5703 * Shared and scratch pointers are represented as 32-bit offsets with the
5704 * top 32 bits only being used for the enum. This allows us to avoid
5705 * 64-bit address calculations in a bunch of cases.
5706 */
5707 nir_address_format_62bit_generic,
5708
5709 /**
5710 * An address format which is a simple 32-bit offset.
5711 */
5712 nir_address_format_32bit_offset,
5713
5714 /**
5715 * An address format which is a simple 32-bit offset cast to 64-bit.
5716 */
5717 nir_address_format_32bit_offset_as_64bit,
5718
5719 /**
5720 * An address format representing a purely logical addressing model. In
5721 * this model, all deref chains must be complete from the dereference
5722 * operation to the variable. Cast derefs are not allowed. These
5723 * addresses will be 32-bit scalars but the format is immaterial because
5724 * you can always chase the chain.
5725 */
5726 nir_address_format_logical,
5727 } nir_address_format;
5728
5729 unsigned
5730 nir_address_format_bit_size(nir_address_format addr_format);
5731
5732 unsigned
5733 nir_address_format_num_components(nir_address_format addr_format);
5734
5735 static inline const struct glsl_type *
nir_address_format_to_glsl_type(nir_address_format addr_format)5736 nir_address_format_to_glsl_type(nir_address_format addr_format)
5737 {
5738 unsigned bit_size = nir_address_format_bit_size(addr_format);
5739 assert(bit_size == 32 || bit_size == 64);
5740 return glsl_vector_type(bit_size == 32 ? GLSL_TYPE_UINT : GLSL_TYPE_UINT64,
5741 nir_address_format_num_components(addr_format));
5742 }
5743
5744 const nir_const_value *nir_address_format_null_value(nir_address_format addr_format);
5745
5746 nir_def *nir_build_addr_iadd(struct nir_builder *b, nir_def *addr,
5747 nir_address_format addr_format,
5748 nir_variable_mode modes,
5749 nir_def *offset);
5750
5751 nir_def *nir_build_addr_iadd_imm(struct nir_builder *b, nir_def *addr,
5752 nir_address_format addr_format,
5753 nir_variable_mode modes,
5754 int64_t offset);
5755
5756 nir_def *nir_build_addr_ieq(struct nir_builder *b, nir_def *addr0, nir_def *addr1,
5757 nir_address_format addr_format);
5758
5759 nir_def *nir_build_addr_isub(struct nir_builder *b, nir_def *addr0, nir_def *addr1,
5760 nir_address_format addr_format);
5761
5762 nir_def *nir_explicit_io_address_from_deref(struct nir_builder *b,
5763 nir_deref_instr *deref,
5764 nir_def *base_addr,
5765 nir_address_format addr_format);
5766
5767 bool nir_get_explicit_deref_align(nir_deref_instr *deref,
5768 bool default_to_type_align,
5769 uint32_t *align_mul,
5770 uint32_t *align_offset);
5771
5772 void nir_lower_explicit_io_instr(struct nir_builder *b,
5773 nir_intrinsic_instr *io_instr,
5774 nir_def *addr,
5775 nir_address_format addr_format);
5776
5777 bool nir_lower_explicit_io(nir_shader *shader,
5778 nir_variable_mode modes,
5779 nir_address_format);
5780
5781 typedef struct {
5782 uint8_t num_components;
5783 uint8_t bit_size;
5784 uint16_t align;
5785 } nir_mem_access_size_align;
5786
5787 /* clang-format off */
5788 typedef nir_mem_access_size_align
5789 (*nir_lower_mem_access_bit_sizes_cb)(nir_intrinsic_op intrin,
5790 uint8_t bytes,
5791 uint8_t bit_size,
5792 uint32_t align_mul,
5793 uint32_t align_offset,
5794 bool offset_is_const,
5795 const void *cb_data);
5796 /* clang-format on */
5797
5798 typedef struct {
5799 nir_lower_mem_access_bit_sizes_cb callback;
5800 nir_variable_mode modes;
5801 bool may_lower_unaligned_stores_to_atomics;
5802 void *cb_data;
5803 } nir_lower_mem_access_bit_sizes_options;
5804
5805 bool nir_lower_mem_access_bit_sizes(nir_shader *shader,
5806 const nir_lower_mem_access_bit_sizes_options *options);
5807
5808 typedef struct {
5809 /* Lower load_ubo to be robust. Out-of-bounds loads will return UNDEFINED
5810 * values (not necessarily zero).
5811 */
5812 bool lower_ubo;
5813
5814 /* Lower load_ssbo/store_ssbo/ssbo_atomic(_swap) to be robust. Out-of-bounds
5815 * loads and atomics will return UNDEFINED values (not necessarily zero).
5816 * Out-of-bounds stores and atomics CORRUPT the contents of the SSBO.
5817 *
5818 * This suffices for robustBufferAccess but not robustBufferAccess2.
5819 */
5820 bool lower_ssbo;
5821
5822 /* Lower all image_load/image_store/image_atomic(_swap) instructions to be
5823 * robust. Out-of-bounds loads will return ZERO.
5824 *
5825 * This suffices for robustImageAccess but not robustImageAccess2.
5826 */
5827 bool lower_image;
5828
5829 /* Lower all buffer image instructions as above. Implied by lower_image. */
5830 bool lower_buffer_image;
5831
5832 /* Lower image_atomic(_swap) for all dimensions. Implied by lower_image. */
5833 bool lower_image_atomic;
5834
5835 /* Vulkan's robustBufferAccess feature is only concerned with buffers that
5836 * are bound through descriptor sets, so shared memory is not included, but
5837 * it may be useful to enable this for debugging.
5838 */
5839 bool lower_shared;
5840 } nir_lower_robust_access_options;
5841
5842 bool nir_lower_robust_access(nir_shader *s,
5843 const nir_lower_robust_access_options *opts);
5844
5845 /* clang-format off */
5846 typedef bool (*nir_should_vectorize_mem_func)(unsigned align_mul,
5847 unsigned align_offset,
5848 unsigned bit_size,
5849 unsigned num_components,
5850 nir_intrinsic_instr *low,
5851 nir_intrinsic_instr *high,
5852 void *data);
5853 /* clang-format on */
5854
5855 typedef struct {
5856 nir_should_vectorize_mem_func callback;
5857 nir_variable_mode modes;
5858 nir_variable_mode robust_modes;
5859 void *cb_data;
5860 bool has_shared2_amd;
5861 } nir_load_store_vectorize_options;
5862
5863 bool nir_opt_load_store_vectorize(nir_shader *shader, const nir_load_store_vectorize_options *options);
5864 bool nir_opt_load_store_update_alignments(nir_shader *shader);
5865
5866 typedef bool (*nir_lower_shader_calls_should_remat_func)(nir_instr *instr, void *data);
5867
5868 typedef struct nir_lower_shader_calls_options {
5869 /* Address format used for load/store operations on the call stack. */
5870 nir_address_format address_format;
5871
5872 /* Stack alignment */
5873 unsigned stack_alignment;
5874
5875 /* Put loads from the stack as close as possible from where they're needed.
5876 * You might want to disable combined_loads for best effects.
5877 */
5878 bool localized_loads;
5879
5880 /* If this function pointer is not NULL, lower_shader_calls will run
5881 * nir_opt_load_store_vectorize for stack load/store operations. Otherwise
5882 * the optimizaion is not run.
5883 */
5884 nir_should_vectorize_mem_func vectorizer_callback;
5885
5886 /* Data passed to vectorizer_callback */
5887 void *vectorizer_data;
5888
5889 /* If this function pointer is not NULL, lower_shader_calls will call this
5890 * function on instructions that require spill/fill/rematerialization of
5891 * their value. If this function returns true, lower_shader_calls will
5892 * ensure that the instruction is rematerialized, adding the sources of the
5893 * instruction to be spilled/filled.
5894 */
5895 nir_lower_shader_calls_should_remat_func should_remat_callback;
5896
5897 /* Data passed to should_remat_callback */
5898 void *should_remat_data;
5899 } nir_lower_shader_calls_options;
5900
5901 bool
5902 nir_lower_shader_calls(nir_shader *shader,
5903 const nir_lower_shader_calls_options *options,
5904 nir_shader ***resume_shaders_out,
5905 uint32_t *num_resume_shaders_out,
5906 void *mem_ctx);
5907
5908 int nir_get_io_offset_src_number(const nir_intrinsic_instr *instr);
5909 int nir_get_io_arrayed_index_src_number(const nir_intrinsic_instr *instr);
5910
5911 nir_src *nir_get_io_offset_src(nir_intrinsic_instr *instr);
5912 nir_src *nir_get_io_arrayed_index_src(nir_intrinsic_instr *instr);
5913 nir_src *nir_get_shader_call_payload_src(nir_intrinsic_instr *call);
5914
5915 bool nir_is_arrayed_io(const nir_variable *var, gl_shader_stage stage);
5916
5917 bool nir_lower_reg_intrinsics_to_ssa_impl(nir_function_impl *impl);
5918 bool nir_lower_reg_intrinsics_to_ssa(nir_shader *shader);
5919 bool nir_lower_vars_to_ssa(nir_shader *shader);
5920
5921 bool nir_remove_dead_derefs(nir_shader *shader);
5922 bool nir_remove_dead_derefs_impl(nir_function_impl *impl);
5923
5924 typedef struct nir_remove_dead_variables_options {
5925 bool (*can_remove_var)(nir_variable *var, void *data);
5926 void *can_remove_var_data;
5927 } nir_remove_dead_variables_options;
5928
5929 bool nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
5930 const nir_remove_dead_variables_options *options);
5931
5932 bool nir_lower_variable_initializers(nir_shader *shader,
5933 nir_variable_mode modes);
5934 bool nir_zero_initialize_shared_memory(nir_shader *shader,
5935 const unsigned shared_size,
5936 const unsigned chunk_size);
5937 bool nir_clear_shared_memory(nir_shader *shader,
5938 const unsigned shared_size,
5939 const unsigned chunk_size);
5940
5941 bool nir_move_vec_src_uses_to_dest(nir_shader *shader, bool skip_const_srcs);
5942 bool nir_lower_vec_to_regs(nir_shader *shader, nir_instr_writemask_filter_cb cb,
5943 const void *_data);
5944 bool nir_lower_alpha_test(nir_shader *shader, enum compare_func func,
5945 bool alpha_to_one,
5946 const gl_state_index16 *alpha_ref_state_tokens);
5947 bool nir_lower_alu(nir_shader *shader);
5948
5949 bool nir_lower_flrp(nir_shader *shader, unsigned lowering_mask,
5950 bool always_precise);
5951
5952 bool nir_scale_fdiv(nir_shader *shader);
5953
5954 bool nir_lower_alu_to_scalar(nir_shader *shader, nir_instr_filter_cb cb, const void *data);
5955 bool nir_lower_alu_width(nir_shader *shader, nir_vectorize_cb cb, const void *data);
5956 bool nir_lower_alu_vec8_16_srcs(nir_shader *shader);
5957 bool nir_lower_bool_to_bitsize(nir_shader *shader);
5958 bool nir_lower_bool_to_float(nir_shader *shader, bool has_fcsel_ne);
5959 bool nir_lower_bool_to_int32(nir_shader *shader);
5960 bool nir_opt_simplify_convert_alu_types(nir_shader *shader);
5961 bool nir_lower_const_arrays_to_uniforms(nir_shader *shader,
5962 unsigned max_uniform_components);
5963 bool nir_lower_convert_alu_types(nir_shader *shader,
5964 bool (*should_lower)(nir_intrinsic_instr *));
5965 bool nir_lower_constant_convert_alu_types(nir_shader *shader);
5966 bool nir_lower_alu_conversion_to_intrinsic(nir_shader *shader);
5967 bool nir_lower_int_to_float(nir_shader *shader);
5968 bool nir_lower_load_const_to_scalar(nir_shader *shader);
5969 bool nir_lower_read_invocation_to_scalar(nir_shader *shader);
5970 bool nir_lower_phis_to_scalar(nir_shader *shader, bool lower_all);
5971 void nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer);
5972 bool nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
5973 bool outputs_only);
5974 bool nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask, nir_instr_filter_cb filter, void *filter_data);
5975 bool nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask);
5976 bool nir_lower_io_to_vector(nir_shader *shader, nir_variable_mode mask);
5977 bool nir_vectorize_tess_levels(nir_shader *shader);
5978 nir_shader *nir_create_passthrough_tcs_impl(const nir_shader_compiler_options *options,
5979 unsigned *locations, unsigned num_locations,
5980 uint8_t patch_vertices);
5981 nir_shader *nir_create_passthrough_tcs(const nir_shader_compiler_options *options,
5982 const nir_shader *vs, uint8_t patch_vertices);
5983 nir_shader *nir_create_passthrough_gs(const nir_shader_compiler_options *options,
5984 const nir_shader *prev_stage,
5985 enum mesa_prim primitive_type,
5986 enum mesa_prim output_primitive_type,
5987 bool emulate_edgeflags,
5988 bool force_line_strip_out);
5989
5990 bool nir_lower_fragcolor(nir_shader *shader, unsigned max_cbufs);
5991 bool nir_lower_fragcoord_wtrans(nir_shader *shader);
5992 bool nir_lower_frag_coord_to_pixel_coord(nir_shader *shader);
5993 bool nir_lower_viewport_transform(nir_shader *shader);
5994 bool nir_lower_uniforms_to_ubo(nir_shader *shader, bool dword_packed, bool load_vec4);
5995
5996 bool nir_lower_is_helper_invocation(nir_shader *shader);
5997
5998 bool nir_lower_single_sampled(nir_shader *shader);
5999
6000 typedef struct nir_lower_subgroups_options {
6001 /* In addition to the boolean lowering options below, this optional callback
6002 * will filter instructions for lowering if non-NULL. The data passed will be
6003 * this options struct itself.
6004 */
6005 nir_instr_filter_cb filter;
6006
6007 uint8_t subgroup_size;
6008 uint8_t ballot_bit_size;
6009 uint8_t ballot_components;
6010 bool lower_to_scalar : 1;
6011 bool lower_vote_trivial : 1;
6012 bool lower_vote_eq : 1;
6013 bool lower_vote_bool_eq : 1;
6014 bool lower_first_invocation_to_ballot : 1;
6015 bool lower_read_first_invocation : 1;
6016 bool lower_subgroup_masks : 1;
6017 bool lower_relative_shuffle : 1;
6018 bool lower_shuffle_to_32bit : 1;
6019 bool lower_shuffle_to_swizzle_amd : 1;
6020 bool lower_shuffle : 1;
6021 bool lower_quad : 1;
6022 bool lower_quad_broadcast_dynamic : 1;
6023 bool lower_quad_broadcast_dynamic_to_const : 1;
6024 bool lower_elect : 1;
6025 bool lower_read_invocation_to_cond : 1;
6026 bool lower_rotate_to_shuffle : 1;
6027 bool lower_ballot_bit_count_to_mbcnt_amd : 1;
6028 bool lower_inverse_ballot : 1;
6029 bool lower_reduce : 1;
6030 bool lower_boolean_reduce : 1;
6031 bool lower_boolean_shuffle : 1;
6032 } nir_lower_subgroups_options;
6033
6034 bool nir_lower_subgroups(nir_shader *shader,
6035 const nir_lower_subgroups_options *options);
6036
6037 bool nir_lower_system_values(nir_shader *shader);
6038
6039 nir_def *
6040 nir_build_lowered_load_helper_invocation(struct nir_builder *b);
6041
6042 typedef struct nir_lower_compute_system_values_options {
6043 bool has_base_global_invocation_id : 1;
6044 bool has_base_workgroup_id : 1;
6045 bool has_global_size : 1;
6046 bool shuffle_local_ids_for_quad_derivatives : 1;
6047 bool lower_local_invocation_index : 1;
6048 bool lower_cs_local_id_to_index : 1;
6049 bool lower_workgroup_id_to_index : 1;
6050 /* At shader execution time, check if WorkGroupId should be 1D
6051 * and compute it quickly. Fall back to slow computation if not.
6052 */
6053 bool shortcut_1d_workgroup_id : 1;
6054 uint32_t num_workgroups[3]; /* Compile-time-known dispatch sizes, or 0 if unknown. */
6055 } nir_lower_compute_system_values_options;
6056
6057 bool nir_lower_compute_system_values(nir_shader *shader,
6058 const nir_lower_compute_system_values_options *options);
6059
6060 struct nir_lower_sysvals_to_varyings_options {
6061 bool frag_coord : 1;
6062 bool front_face : 1;
6063 bool point_coord : 1;
6064 };
6065
6066 bool
6067 nir_lower_sysvals_to_varyings(nir_shader *shader,
6068 const struct nir_lower_sysvals_to_varyings_options *options);
6069
6070 /***/
6071 enum ENUM_PACKED nir_lower_tex_packing {
6072 /** No packing */
6073 nir_lower_tex_packing_none = 0,
6074 /**
6075 * The sampler returns up to 2 32-bit words of half floats or 16-bit signed
6076 * or unsigned ints based on the sampler type
6077 */
6078 nir_lower_tex_packing_16,
6079 /** The sampler returns 1 32-bit word of 4x8 unorm */
6080 nir_lower_tex_packing_8,
6081 };
6082
6083 /***/
6084 typedef struct nir_lower_tex_options {
6085 /**
6086 * bitmask of (1 << GLSL_SAMPLER_DIM_x) to control for which
6087 * sampler types a texture projector is lowered.
6088 */
6089 unsigned lower_txp;
6090
6091 /**
6092 * If true, lower texture projector for any array sampler dims
6093 */
6094 bool lower_txp_array;
6095
6096 /**
6097 * If true, lower away nir_tex_src_offset for all texelfetch instructions.
6098 */
6099 bool lower_txf_offset;
6100
6101 /**
6102 * If true, lower away nir_tex_src_offset for all rect textures.
6103 */
6104 bool lower_rect_offset;
6105
6106 /**
6107 * If not NULL, this filter will return true for tex instructions that
6108 * should lower away nir_tex_src_offset.
6109 */
6110 nir_instr_filter_cb lower_offset_filter;
6111
6112 /**
6113 * If true, lower rect textures to 2D, using txs to fetch the
6114 * texture dimensions and dividing the texture coords by the
6115 * texture dims to normalize.
6116 */
6117 bool lower_rect;
6118
6119 /**
6120 * If true, lower 1D textures to 2D. This requires the GL/VK driver to map 1D
6121 * textures to 2D textures with height=1.
6122 *
6123 * lower_1d_shadow does this lowering for shadow textures only.
6124 */
6125 bool lower_1d;
6126 bool lower_1d_shadow;
6127
6128 /**
6129 * If true, convert yuv to rgb.
6130 */
6131 unsigned lower_y_uv_external;
6132 unsigned lower_y_vu_external;
6133 unsigned lower_y_u_v_external;
6134 unsigned lower_yx_xuxv_external;
6135 unsigned lower_yx_xvxu_external;
6136 unsigned lower_xy_uxvx_external;
6137 unsigned lower_xy_vxux_external;
6138 unsigned lower_ayuv_external;
6139 unsigned lower_xyuv_external;
6140 unsigned lower_yuv_external;
6141 unsigned lower_yu_yv_external;
6142 unsigned lower_yv_yu_external;
6143 unsigned lower_y41x_external;
6144 unsigned bt709_external;
6145 unsigned bt2020_external;
6146 unsigned yuv_full_range_external;
6147
6148 /**
6149 * To emulate certain texture wrap modes, this can be used
6150 * to saturate the specified tex coord to [0.0, 1.0]. The
6151 * bits are according to sampler #, ie. if, for example:
6152 *
6153 * (conf->saturate_s & (1 << n))
6154 *
6155 * is true, then the s coord for sampler n is saturated.
6156 *
6157 * Note that clamping must happen *after* projector lowering
6158 * so any projected texture sample instruction with a clamped
6159 * coordinate gets automatically lowered, regardless of the
6160 * 'lower_txp' setting.
6161 */
6162 unsigned saturate_s;
6163 unsigned saturate_t;
6164 unsigned saturate_r;
6165
6166 /* Bitmask of textures that need swizzling.
6167 *
6168 * If (swizzle_result & (1 << texture_index)), then the swizzle in
6169 * swizzles[texture_index] is applied to the result of the texturing
6170 * operation.
6171 */
6172 unsigned swizzle_result;
6173
6174 /* A swizzle for each texture. Values 0-3 represent x, y, z, or w swizzles
6175 * while 4 and 5 represent 0 and 1 respectively.
6176 *
6177 * Indexed by texture-id.
6178 */
6179 uint8_t swizzles[32][4];
6180
6181 /* Can be used to scale sampled values in range required by the
6182 * format.
6183 *
6184 * Indexed by texture-id.
6185 */
6186 float scale_factors[32];
6187
6188 /**
6189 * Bitmap of textures that need srgb to linear conversion. If
6190 * (lower_srgb & (1 << texture_index)) then the rgb (xyz) components
6191 * of the texture are lowered to linear.
6192 */
6193 unsigned lower_srgb;
6194
6195 /**
6196 * If true, lower nir_texop_txd on cube maps with nir_texop_txl.
6197 */
6198 bool lower_txd_cube_map;
6199
6200 /**
6201 * If true, lower nir_texop_txd on 3D surfaces with nir_texop_txl.
6202 */
6203 bool lower_txd_3d;
6204
6205 /**
6206 * If true, lower nir_texop_txd any array surfaces with nir_texop_txl.
6207 */
6208 bool lower_txd_array;
6209
6210 /**
6211 * If true, lower nir_texop_txd on shadow samplers (except cube maps)
6212 * with nir_texop_txl. Notice that cube map shadow samplers are lowered
6213 * with lower_txd_cube_map.
6214 */
6215 bool lower_txd_shadow;
6216
6217 /**
6218 * If true, lower nir_texop_txd on all samplers to a nir_texop_txl.
6219 * Implies lower_txd_cube_map and lower_txd_shadow.
6220 */
6221 bool lower_txd;
6222
6223 /**
6224 * If true, lower nir_texop_txd when it uses min_lod.
6225 */
6226 bool lower_txd_clamp;
6227
6228 /**
6229 * If true, lower nir_texop_txb that try to use shadow compare and min_lod
6230 * at the same time to a nir_texop_lod, some math, and nir_texop_tex.
6231 */
6232 bool lower_txb_shadow_clamp;
6233
6234 /**
6235 * If true, lower nir_texop_txd on shadow samplers when it uses min_lod
6236 * with nir_texop_txl. This includes cube maps.
6237 */
6238 bool lower_txd_shadow_clamp;
6239
6240 /**
6241 * If true, lower nir_texop_txd on when it uses both offset and min_lod
6242 * with nir_texop_txl. This includes cube maps.
6243 */
6244 bool lower_txd_offset_clamp;
6245
6246 /**
6247 * If true, lower nir_texop_txd with min_lod to a nir_texop_txl if the
6248 * sampler is bindless.
6249 */
6250 bool lower_txd_clamp_bindless_sampler;
6251
6252 /**
6253 * If true, lower nir_texop_txd with min_lod to a nir_texop_txl if the
6254 * sampler index is not statically determinable to be less than 16.
6255 */
6256 bool lower_txd_clamp_if_sampler_index_not_lt_16;
6257
6258 /**
6259 * If true, lower nir_texop_txs with a non-0-lod into nir_texop_txs with
6260 * 0-lod followed by a nir_ishr.
6261 */
6262 bool lower_txs_lod;
6263
6264 /**
6265 * If true, lower nir_texop_txs for cube arrays to a nir_texop_txs with a
6266 * 2D array type followed by a nir_idiv by 6.
6267 */
6268 bool lower_txs_cube_array;
6269
6270 /**
6271 * If true, apply a .bagr swizzle on tg4 results to handle Broadcom's
6272 * mixed-up tg4 locations.
6273 */
6274 bool lower_tg4_broadcom_swizzle;
6275
6276 /**
6277 * If true, lowers tg4 with 4 constant offsets to 4 tg4 calls
6278 */
6279 bool lower_tg4_offsets;
6280
6281 /**
6282 * Lower txf_ms to fragment_mask_fetch and fragment_fetch and samples_identical to
6283 * fragment_mask_fetch.
6284 */
6285 bool lower_to_fragment_fetch_amd;
6286
6287 /**
6288 * To lower packed sampler return formats. This will be called for all
6289 * tex instructions.
6290 */
6291 enum nir_lower_tex_packing (*lower_tex_packing_cb)(const nir_tex_instr *tex, const void *data);
6292 const void *lower_tex_packing_data;
6293
6294 /**
6295 * If true, lower nir_texop_lod to return -FLT_MAX if the sum of the
6296 * absolute values of derivatives is 0 for all coordinates.
6297 */
6298 bool lower_lod_zero_width;
6299
6300 /* Turns nir_op_tex and other ops with an implicit derivative, in stages
6301 * without implicit derivatives (like the vertex shader) to have an explicit
6302 * LOD with a value of 0.
6303 */
6304 bool lower_invalid_implicit_lod;
6305
6306 /* If true, texture_index (sampler_index) will be zero if a texture_offset
6307 * (sampler_offset) source is present. This is convenient for backends that
6308 * support indirect indexing of textures (samplers) but not offsetting it.
6309 */
6310 bool lower_index_to_offset;
6311
6312 /**
6313 * Payload data to be sent to callback / filter functions.
6314 */
6315 void *callback_data;
6316 } nir_lower_tex_options;
6317
6318 /** Lowers complex texture instructions to simpler ones */
6319 bool nir_lower_tex(nir_shader *shader,
6320 const nir_lower_tex_options *options);
6321
6322 typedef struct nir_lower_tex_shadow_swizzle {
6323 unsigned swizzle_r : 3;
6324 unsigned swizzle_g : 3;
6325 unsigned swizzle_b : 3;
6326 unsigned swizzle_a : 3;
6327 } nir_lower_tex_shadow_swizzle;
6328
6329 bool
6330 nir_lower_tex_shadow(nir_shader *s,
6331 unsigned n_states,
6332 enum compare_func *compare_func,
6333 nir_lower_tex_shadow_swizzle *tex_swizzles);
6334
6335 typedef struct nir_lower_image_options {
6336 /**
6337 * If true, lower cube size operations.
6338 */
6339 bool lower_cube_size;
6340
6341 /**
6342 * Lower multi sample image load and samples_identical to use fragment_mask_load.
6343 */
6344 bool lower_to_fragment_mask_load_amd;
6345
6346 /**
6347 * Lower image_samples to a constant in case the driver doesn't support multisampled
6348 * images.
6349 */
6350 bool lower_image_samples_to_one;
6351 } nir_lower_image_options;
6352
6353 bool nir_lower_image(nir_shader *nir,
6354 const nir_lower_image_options *options);
6355
6356 bool
6357 nir_lower_image_atomics_to_global(nir_shader *s);
6358
6359 bool nir_lower_readonly_images_to_tex(nir_shader *shader, bool per_variable);
6360
6361 enum nir_lower_non_uniform_access_type {
6362 nir_lower_non_uniform_ubo_access = (1 << 0),
6363 nir_lower_non_uniform_ssbo_access = (1 << 1),
6364 nir_lower_non_uniform_texture_access = (1 << 2),
6365 nir_lower_non_uniform_image_access = (1 << 3),
6366 nir_lower_non_uniform_get_ssbo_size = (1 << 4),
6367 };
6368
6369 /* Given the nir_src used for the resource, return the channels which might be non-uniform. */
6370 typedef nir_component_mask_t (*nir_lower_non_uniform_access_callback)(const nir_src *, void *);
6371
6372 typedef struct nir_lower_non_uniform_access_options {
6373 enum nir_lower_non_uniform_access_type types;
6374 nir_lower_non_uniform_access_callback callback;
6375 void *callback_data;
6376 } nir_lower_non_uniform_access_options;
6377
6378 bool nir_has_non_uniform_access(nir_shader *shader, enum nir_lower_non_uniform_access_type types);
6379 bool nir_opt_non_uniform_access(nir_shader *shader);
6380 bool nir_lower_non_uniform_access(nir_shader *shader,
6381 const nir_lower_non_uniform_access_options *options);
6382
6383 typedef struct {
6384 /* Whether 16-bit floating point arithmetic should be allowed in 8-bit
6385 * division lowering
6386 */
6387 bool allow_fp16;
6388 } nir_lower_idiv_options;
6389
6390 bool nir_lower_idiv(nir_shader *shader, const nir_lower_idiv_options *options);
6391
6392 typedef struct nir_input_attachment_options {
6393 bool use_fragcoord_sysval;
6394 bool use_layer_id_sysval;
6395 bool use_view_id_for_layer;
6396 uint32_t unscaled_input_attachment_ir3;
6397 } nir_input_attachment_options;
6398
6399 bool nir_lower_input_attachments(nir_shader *shader,
6400 const nir_input_attachment_options *options);
6401
6402 bool nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables,
6403 bool use_vars,
6404 bool use_clipdist_array,
6405 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH]);
6406 bool nir_lower_clip_gs(nir_shader *shader, unsigned ucp_enables,
6407 bool use_clipdist_array,
6408 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH]);
6409 bool nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables,
6410 bool use_clipdist_array);
6411
6412 bool nir_lower_clip_cull_distance_to_vec4s(nir_shader *shader);
6413 bool nir_lower_clip_cull_distance_arrays(nir_shader *nir);
6414 bool nir_lower_clip_disable(nir_shader *shader, unsigned clip_plane_enable);
6415
6416 bool nir_lower_point_size_mov(nir_shader *shader,
6417 const gl_state_index16 *pointsize_state_tokens);
6418
6419 bool nir_lower_frexp(nir_shader *nir);
6420
6421 bool nir_lower_two_sided_color(nir_shader *shader, bool face_sysval);
6422
6423 bool nir_lower_clamp_color_outputs(nir_shader *shader);
6424
6425 bool nir_lower_flatshade(nir_shader *shader);
6426
6427 bool nir_lower_passthrough_edgeflags(nir_shader *shader);
6428 bool nir_lower_patch_vertices(nir_shader *nir, unsigned static_count,
6429 const gl_state_index16 *uniform_state_tokens);
6430
6431 typedef struct nir_lower_wpos_ytransform_options {
6432 gl_state_index16 state_tokens[STATE_LENGTH];
6433 bool fs_coord_origin_upper_left : 1;
6434 bool fs_coord_origin_lower_left : 1;
6435 bool fs_coord_pixel_center_integer : 1;
6436 bool fs_coord_pixel_center_half_integer : 1;
6437 } nir_lower_wpos_ytransform_options;
6438
6439 bool nir_lower_wpos_ytransform(nir_shader *shader,
6440 const nir_lower_wpos_ytransform_options *options);
6441 bool nir_lower_wpos_center(nir_shader *shader);
6442
6443 bool nir_lower_pntc_ytransform(nir_shader *shader,
6444 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH]);
6445
6446 bool nir_lower_wrmasks(nir_shader *shader, nir_instr_filter_cb cb, const void *data);
6447
6448 bool nir_lower_fb_read(nir_shader *shader);
6449
6450 typedef struct nir_lower_drawpixels_options {
6451 gl_state_index16 texcoord_state_tokens[STATE_LENGTH];
6452 gl_state_index16 scale_state_tokens[STATE_LENGTH];
6453 gl_state_index16 bias_state_tokens[STATE_LENGTH];
6454 unsigned drawpix_sampler;
6455 unsigned pixelmap_sampler;
6456 bool pixel_maps : 1;
6457 bool scale_and_bias : 1;
6458 } nir_lower_drawpixels_options;
6459
6460 bool nir_lower_drawpixels(nir_shader *shader,
6461 const nir_lower_drawpixels_options *options);
6462
6463 typedef struct nir_lower_bitmap_options {
6464 unsigned sampler;
6465 bool swizzle_xxxx;
6466 } nir_lower_bitmap_options;
6467
6468 bool nir_lower_bitmap(nir_shader *shader, const nir_lower_bitmap_options *options);
6469
6470 bool nir_lower_atomics_to_ssbo(nir_shader *shader, unsigned offset_align_state);
6471
6472 typedef enum {
6473 nir_lower_gs_intrinsics_per_stream = 1 << 0,
6474 nir_lower_gs_intrinsics_count_primitives = 1 << 1,
6475 nir_lower_gs_intrinsics_count_vertices_per_primitive = 1 << 2,
6476 nir_lower_gs_intrinsics_overwrite_incomplete = 1 << 3,
6477 nir_lower_gs_intrinsics_always_end_primitive = 1 << 4,
6478 nir_lower_gs_intrinsics_count_decomposed_primitives = 1 << 5,
6479 } nir_lower_gs_intrinsics_flags;
6480
6481 bool nir_lower_gs_intrinsics(nir_shader *shader, nir_lower_gs_intrinsics_flags options);
6482
6483 bool nir_lower_tess_coord_z(nir_shader *shader, bool triangles);
6484
6485 typedef struct {
6486 bool payload_to_shared_for_atomics : 1;
6487 bool payload_to_shared_for_small_types : 1;
6488 uint32_t payload_offset_in_bytes;
6489 } nir_lower_task_shader_options;
6490
6491 bool nir_lower_task_shader(nir_shader *shader, nir_lower_task_shader_options options);
6492
6493 typedef unsigned (*nir_lower_bit_size_callback)(const nir_instr *, void *);
6494
6495 bool nir_lower_bit_size(nir_shader *shader,
6496 nir_lower_bit_size_callback callback,
6497 void *callback_data);
6498 bool nir_lower_64bit_phis(nir_shader *shader);
6499
6500 bool nir_split_64bit_vec3_and_vec4(nir_shader *shader);
6501
6502 nir_lower_int64_options nir_lower_int64_op_to_options_mask(nir_op opcode);
6503 bool nir_lower_int64(nir_shader *shader);
6504 bool nir_lower_int64_float_conversions(nir_shader *shader);
6505
6506 nir_lower_doubles_options nir_lower_doubles_op_to_options_mask(nir_op opcode);
6507 bool nir_lower_doubles(nir_shader *shader, const nir_shader *softfp64,
6508 nir_lower_doubles_options options);
6509 bool nir_lower_pack(nir_shader *shader);
6510
6511 bool nir_recompute_io_bases(nir_shader *nir, nir_variable_mode modes);
6512 bool nir_lower_mediump_vars(nir_shader *nir, nir_variable_mode modes);
6513 bool nir_lower_mediump_io(nir_shader *nir, nir_variable_mode modes,
6514 uint64_t varying_mask, bool use_16bit_slots);
6515 bool nir_force_mediump_io(nir_shader *nir, nir_variable_mode modes,
6516 nir_alu_type types);
6517 bool nir_unpack_16bit_varying_slots(nir_shader *nir, nir_variable_mode modes);
6518
6519 struct nir_opt_tex_srcs_options {
6520 unsigned sampler_dims;
6521 unsigned src_types;
6522 };
6523
6524 struct nir_opt_16bit_tex_image_options {
6525 nir_rounding_mode rounding_mode;
6526 nir_alu_type opt_tex_dest_types;
6527 nir_alu_type opt_image_dest_types;
6528 bool integer_dest_saturates;
6529 bool opt_image_store_data;
6530 bool opt_image_srcs;
6531 unsigned opt_srcs_options_count;
6532 struct nir_opt_tex_srcs_options *opt_srcs_options;
6533 };
6534
6535 bool nir_opt_16bit_tex_image(nir_shader *nir,
6536 struct nir_opt_16bit_tex_image_options *options);
6537
6538 typedef struct {
6539 bool legalize_type; /* whether this src should be legalized */
6540 uint8_t bit_size; /* bit_size to enforce */
6541 nir_tex_src_type match_src; /* if bit_size is 0, match bit size of this */
6542 } nir_tex_src_type_constraint, nir_tex_src_type_constraints[nir_num_tex_src_types];
6543
6544 bool nir_legalize_16bit_sampler_srcs(nir_shader *nir,
6545 nir_tex_src_type_constraints constraints);
6546
6547 bool nir_lower_point_size(nir_shader *shader, float min, float max);
6548
6549 void nir_lower_texcoord_replace(nir_shader *s, unsigned coord_replace,
6550 bool point_coord_is_sysval, bool yinvert);
6551
6552 void nir_lower_texcoord_replace_late(nir_shader *s, unsigned coord_replace,
6553 bool point_coord_is_sysval);
6554
6555 typedef enum {
6556 nir_lower_interpolation_at_sample = (1 << 1),
6557 nir_lower_interpolation_at_offset = (1 << 2),
6558 nir_lower_interpolation_centroid = (1 << 3),
6559 nir_lower_interpolation_pixel = (1 << 4),
6560 nir_lower_interpolation_sample = (1 << 5),
6561 } nir_lower_interpolation_options;
6562
6563 bool nir_lower_interpolation(nir_shader *shader,
6564 nir_lower_interpolation_options options);
6565
6566 typedef enum {
6567 nir_lower_discard_if_to_cf = (1 << 0),
6568 nir_lower_demote_if_to_cf = (1 << 1),
6569 nir_lower_terminate_if_to_cf = (1 << 2),
6570 } nir_lower_discard_if_options;
6571
6572 bool nir_lower_discard_if(nir_shader *shader, nir_lower_discard_if_options options);
6573
6574 bool nir_lower_terminate_to_demote(nir_shader *nir);
6575
6576 bool nir_lower_memory_model(nir_shader *shader);
6577
6578 bool nir_lower_goto_ifs(nir_shader *shader);
6579 bool nir_lower_continue_constructs(nir_shader *shader);
6580
6581 bool nir_shader_uses_view_index(nir_shader *shader);
6582 bool nir_can_lower_multiview(nir_shader *shader);
6583 bool nir_lower_multiview(nir_shader *shader, uint32_t view_mask);
6584
6585 bool nir_lower_view_index_to_device_index(nir_shader *shader);
6586
6587 typedef enum {
6588 nir_lower_fp16_rtz = (1 << 0),
6589 nir_lower_fp16_rtne = (1 << 1),
6590 nir_lower_fp16_ru = (1 << 2),
6591 nir_lower_fp16_rd = (1 << 3),
6592 nir_lower_fp16_all = 0xf,
6593 nir_lower_fp16_split_fp64 = (1 << 4),
6594 } nir_lower_fp16_cast_options;
6595 bool nir_lower_fp16_casts(nir_shader *shader, nir_lower_fp16_cast_options options);
6596 bool nir_normalize_cubemap_coords(nir_shader *shader);
6597
6598 bool nir_shader_supports_implicit_lod(nir_shader *shader);
6599
6600 void nir_live_defs_impl(nir_function_impl *impl);
6601
6602 const BITSET_WORD *nir_get_live_defs(nir_cursor cursor, void *mem_ctx);
6603
6604 void nir_loop_analyze_impl(nir_function_impl *impl,
6605 nir_variable_mode indirect_mask,
6606 bool force_unroll_sampler_indirect);
6607
6608 bool nir_defs_interfere(nir_def *a, nir_def *b);
6609
6610 bool nir_repair_ssa_impl(nir_function_impl *impl);
6611 bool nir_repair_ssa(nir_shader *shader);
6612
6613 void nir_convert_loop_to_lcssa(nir_loop *loop);
6614 bool nir_convert_to_lcssa(nir_shader *shader, bool skip_invariants, bool skip_bool_invariants);
6615 void nir_divergence_analysis(nir_shader *shader);
6616 void nir_vertex_divergence_analysis(nir_shader *shader);
6617 bool nir_update_instr_divergence(nir_shader *shader, nir_instr *instr);
6618 bool nir_has_divergent_loop(nir_shader *shader);
6619
6620 void
6621 nir_rewrite_uses_to_load_reg(struct nir_builder *b, nir_def *old,
6622 nir_def *reg);
6623
6624 /* If phi_webs_only is true, only convert SSA values involved in phi nodes to
6625 * registers. If false, convert all values (even those not involved in a phi
6626 * node) to registers.
6627 */
6628 bool nir_convert_from_ssa(nir_shader *shader,
6629 bool phi_webs_only);
6630
6631 bool nir_lower_phis_to_regs_block(nir_block *block);
6632 bool nir_lower_ssa_defs_to_regs_block(nir_block *block);
6633
6634 bool nir_rematerialize_deref_in_use_blocks(nir_deref_instr *instr);
6635 bool nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl);
6636
6637 bool nir_lower_samplers(nir_shader *shader);
6638 bool nir_lower_cl_images(nir_shader *shader, bool lower_image_derefs, bool lower_sampler_derefs);
6639 bool nir_dedup_inline_samplers(nir_shader *shader);
6640
6641 typedef struct nir_lower_ssbo_options {
6642 bool native_loads;
6643 bool native_offset;
6644 } nir_lower_ssbo_options;
6645
6646 bool nir_lower_ssbo(nir_shader *shader, const nir_lower_ssbo_options *opts);
6647
6648 bool nir_lower_helper_writes(nir_shader *shader, bool lower_plain_stores);
6649
6650 typedef struct nir_lower_printf_options {
6651 unsigned max_buffer_size;
6652 unsigned ptr_bit_size;
6653 bool use_printf_base_identifier;
6654 } nir_lower_printf_options;
6655
6656 bool nir_lower_printf(nir_shader *nir, const nir_lower_printf_options *options);
6657
6658 /* This is here for unit tests. */
6659 bool nir_opt_comparison_pre_impl(nir_function_impl *impl);
6660
6661 bool nir_opt_comparison_pre(nir_shader *shader);
6662
6663 typedef struct nir_opt_access_options {
6664 bool is_vulkan;
6665 } nir_opt_access_options;
6666
6667 bool nir_opt_access(nir_shader *shader, const nir_opt_access_options *options);
6668 bool nir_opt_algebraic(nir_shader *shader);
6669 bool nir_opt_algebraic_before_ffma(nir_shader *shader);
6670 bool nir_opt_algebraic_before_lower_int64(nir_shader *shader);
6671 bool nir_opt_algebraic_late(nir_shader *shader);
6672 bool nir_opt_algebraic_distribute_src_mods(nir_shader *shader);
6673 bool nir_opt_constant_folding(nir_shader *shader);
6674
6675 /* Try to combine a and b into a. Return true if combination was possible,
6676 * which will result in b being removed by the pass. Return false if
6677 * combination wasn't possible.
6678 */
6679 typedef bool (*nir_combine_barrier_cb)(
6680 nir_intrinsic_instr *a, nir_intrinsic_instr *b, void *data);
6681
6682 bool nir_opt_combine_barriers(nir_shader *shader,
6683 nir_combine_barrier_cb combine_cb,
6684 void *data);
6685 bool nir_opt_barrier_modes(nir_shader *shader);
6686
6687 bool nir_opt_combine_stores(nir_shader *shader, nir_variable_mode modes);
6688
6689 bool nir_copy_prop_impl(nir_function_impl *impl);
6690 bool nir_copy_prop(nir_shader *shader);
6691
6692 bool nir_opt_copy_prop_vars(nir_shader *shader);
6693
6694 bool nir_opt_cse(nir_shader *shader);
6695
6696 bool nir_opt_dce(nir_shader *shader);
6697
6698 bool nir_opt_dead_cf(nir_shader *shader);
6699
6700 bool nir_opt_dead_write_vars(nir_shader *shader);
6701
6702 bool nir_opt_deref_impl(nir_function_impl *impl);
6703 bool nir_opt_deref(nir_shader *shader);
6704
6705 bool nir_opt_find_array_copies(nir_shader *shader);
6706
6707 bool nir_opt_fragdepth(nir_shader *shader);
6708
6709 bool nir_opt_gcm(nir_shader *shader, bool value_number);
6710
6711 bool nir_opt_generate_bfi(nir_shader *shader);
6712
6713 bool nir_opt_idiv_const(nir_shader *shader, unsigned min_bit_size);
6714
6715 bool nir_opt_mqsad(nir_shader *shader);
6716
6717 typedef enum {
6718 nir_opt_if_optimize_phi_true_false = (1 << 0),
6719 nir_opt_if_avoid_64bit_phis = (1 << 1),
6720 } nir_opt_if_options;
6721
6722 bool nir_opt_if(nir_shader *shader, nir_opt_if_options options);
6723
6724 bool nir_opt_intrinsics(nir_shader *shader);
6725
6726 bool nir_opt_large_constants(nir_shader *shader,
6727 glsl_type_size_align_func size_align,
6728 unsigned threshold);
6729
6730 bool nir_opt_licm(nir_shader *shader);
6731 bool nir_opt_loop(nir_shader *shader);
6732
6733 bool nir_opt_loop_unroll(nir_shader *shader);
6734
6735 typedef enum {
6736 nir_move_const_undef = (1 << 0),
6737 nir_move_load_ubo = (1 << 1),
6738 nir_move_load_input = (1 << 2),
6739 nir_move_comparisons = (1 << 3),
6740 nir_move_copies = (1 << 4),
6741 nir_move_load_ssbo = (1 << 5),
6742 nir_move_load_uniform = (1 << 6),
6743 nir_move_alu = (1 << 7),
6744 } nir_move_options;
6745
6746 bool nir_can_move_instr(nir_instr *instr, nir_move_options options);
6747
6748 bool nir_opt_sink(nir_shader *shader, nir_move_options options);
6749
6750 bool nir_opt_move(nir_shader *shader, nir_move_options options);
6751
6752 typedef struct {
6753 /** nir_load_uniform max base offset */
6754 uint32_t uniform_max;
6755
6756 /** nir_load_ubo_vec4 max base offset */
6757 uint32_t ubo_vec4_max;
6758
6759 /** nir_var_mem_shared max base offset */
6760 uint32_t shared_max;
6761
6762 /** nir_load/store_buffer_amd max base offset */
6763 uint32_t buffer_max;
6764
6765 /**
6766 * Callback to get the max base offset for instructions for which the
6767 * corresponding value above is zero.
6768 */
6769 uint32_t (*max_offset_cb)(nir_intrinsic_instr *intr, const void *data);
6770
6771 /** Data to pass to max_offset_cb. */
6772 const void *max_offset_data;
6773
6774 /**
6775 * Allow the offset calculation to wrap. If false, constant additions that
6776 * might wrap will not be folded into the offset.
6777 */
6778 bool allow_offset_wrap;
6779 } nir_opt_offsets_options;
6780
6781 bool nir_opt_offsets(nir_shader *shader, const nir_opt_offsets_options *options);
6782
6783 bool nir_opt_peephole_select(nir_shader *shader, unsigned limit,
6784 bool indirect_load_ok, bool expensive_alu_ok);
6785
6786 bool nir_opt_reassociate_bfi(nir_shader *shader);
6787
6788 bool nir_opt_rematerialize_compares(nir_shader *shader);
6789
6790 bool nir_opt_remove_phis(nir_shader *shader);
6791 bool nir_opt_remove_phis_block(nir_block *block);
6792
6793 bool nir_opt_phi_precision(nir_shader *shader);
6794
6795 bool nir_opt_shrink_stores(nir_shader *shader, bool shrink_image_store);
6796
6797 bool nir_opt_shrink_vectors(nir_shader *shader, bool shrink_start);
6798
6799 bool nir_opt_undef(nir_shader *shader);
6800
6801 bool nir_lower_undef_to_zero(nir_shader *shader);
6802
6803 bool nir_opt_uniform_atomics(nir_shader *shader, bool fs_atomics_predicated);
6804
6805 bool nir_opt_uniform_subgroup(nir_shader *shader,
6806 const nir_lower_subgroups_options *);
6807
6808 bool nir_opt_vectorize(nir_shader *shader, nir_vectorize_cb filter,
6809 void *data);
6810 bool nir_opt_vectorize_io(nir_shader *shader, nir_variable_mode modes);
6811
6812 bool nir_opt_conditional_discard(nir_shader *shader);
6813 bool nir_opt_move_discards_to_top(nir_shader *shader);
6814
6815 bool nir_opt_ray_queries(nir_shader *shader);
6816
6817 bool nir_opt_ray_query_ranges(nir_shader *shader);
6818
6819 bool nir_opt_reuse_constants(nir_shader *shader);
6820
6821 void nir_sweep(nir_shader *shader);
6822
6823 void nir_remap_dual_slot_attributes(nir_shader *shader,
6824 uint64_t *dual_slot_inputs);
6825 uint64_t nir_get_single_slot_attribs_mask(uint64_t attribs, uint64_t dual_slot);
6826
6827 nir_intrinsic_op nir_intrinsic_from_system_value(gl_system_value val);
6828 gl_system_value nir_system_value_from_intrinsic(nir_intrinsic_op intrin);
6829
6830 static inline bool
nir_variable_is_in_ubo(const nir_variable * var)6831 nir_variable_is_in_ubo(const nir_variable *var)
6832 {
6833 return (var->data.mode == nir_var_mem_ubo &&
6834 var->interface_type != NULL);
6835 }
6836
6837 static inline bool
nir_variable_is_in_ssbo(const nir_variable * var)6838 nir_variable_is_in_ssbo(const nir_variable *var)
6839 {
6840 return (var->data.mode == nir_var_mem_ssbo &&
6841 var->interface_type != NULL);
6842 }
6843
6844 static inline bool
nir_variable_is_in_block(const nir_variable * var)6845 nir_variable_is_in_block(const nir_variable *var)
6846 {
6847 return nir_variable_is_in_ubo(var) || nir_variable_is_in_ssbo(var);
6848 }
6849
6850 static inline unsigned
nir_variable_count_slots(const nir_variable * var,const struct glsl_type * type)6851 nir_variable_count_slots(const nir_variable *var, const struct glsl_type *type)
6852 {
6853 return var->data.compact ? DIV_ROUND_UP(var->data.location_frac + glsl_get_length(type), 4) : glsl_count_attribute_slots(type, false);
6854 }
6855
6856 static inline unsigned
nir_deref_count_slots(nir_deref_instr * deref,nir_variable * var)6857 nir_deref_count_slots(nir_deref_instr *deref, nir_variable *var)
6858 {
6859 if (var->data.compact) {
6860 switch (deref->deref_type) {
6861 case nir_deref_type_array:
6862 return 1;
6863 case nir_deref_type_var:
6864 return nir_variable_count_slots(var, deref->type);
6865 default:
6866 unreachable("illegal deref type");
6867 }
6868 }
6869 return glsl_count_attribute_slots(deref->type, false);
6870 }
6871
6872 /* See default_ub_config in nir_range_analysis.c for documentation. */
6873 typedef struct nir_unsigned_upper_bound_config {
6874 unsigned min_subgroup_size;
6875 unsigned max_subgroup_size;
6876 unsigned max_workgroup_invocations;
6877 unsigned max_workgroup_count[3];
6878 unsigned max_workgroup_size[3];
6879
6880 uint32_t vertex_attrib_max[32];
6881 } nir_unsigned_upper_bound_config;
6882
6883 uint32_t
6884 nir_unsigned_upper_bound(nir_shader *shader, struct hash_table *range_ht,
6885 nir_scalar scalar,
6886 const nir_unsigned_upper_bound_config *config);
6887
6888 bool
6889 nir_addition_might_overflow(nir_shader *shader, struct hash_table *range_ht,
6890 nir_scalar ssa, unsigned const_val,
6891 const nir_unsigned_upper_bound_config *config);
6892
6893 typedef struct {
6894 /* True if gl_DrawID is considered uniform, i.e. if the preamble is run
6895 * at least once per "internal" draw rather than per user-visible draw.
6896 */
6897 bool drawid_uniform;
6898
6899 /* True if the subgroup size is uniform. */
6900 bool subgroup_size_uniform;
6901
6902 /* True if load_workgroup_size is supported in the preamble. */
6903 bool load_workgroup_size_allowed;
6904
6905 /* size/align for load/store_preamble. */
6906 void (*def_size)(nir_def *def, unsigned *size, unsigned *align);
6907
6908 /* Total available size for load/store_preamble storage, in units
6909 * determined by def_size.
6910 */
6911 unsigned preamble_storage_size;
6912
6913 /* Give the cost for an instruction. nir_opt_preamble will prioritize
6914 * instructions with higher costs. Instructions with cost 0 may still be
6915 * lifted, but only when required to lift other instructions with non-0
6916 * cost (e.g. a load_const source of an expression).
6917 */
6918 float (*instr_cost_cb)(nir_instr *instr, const void *data);
6919
6920 /* Give the cost of rewriting the instruction to use load_preamble. This
6921 * may happen from inserting move instructions, etc. If the benefit doesn't
6922 * exceed the cost here then we won't rewrite it.
6923 */
6924 float (*rewrite_cost_cb)(nir_def *def, const void *data);
6925
6926 /* Instructions whose definitions should not be rewritten. These could
6927 * still be moved to the preamble, but they shouldn't be the root of a
6928 * replacement expression. Instructions with cost 0 and derefs are
6929 * automatically included by the pass.
6930 */
6931 nir_instr_filter_cb avoid_instr_cb;
6932
6933 const void *cb_data;
6934 } nir_opt_preamble_options;
6935
6936 bool
6937 nir_opt_preamble(nir_shader *shader,
6938 const nir_opt_preamble_options *options,
6939 unsigned *size);
6940
6941 nir_function_impl *nir_shader_get_preamble(nir_shader *shader);
6942
6943 bool nir_lower_point_smooth(nir_shader *shader);
6944 bool nir_lower_poly_line_smooth(nir_shader *shader, unsigned num_smooth_aa_sample);
6945
6946 bool nir_mod_analysis(nir_scalar val, nir_alu_type val_type, unsigned div, unsigned *mod);
6947
6948 bool
6949 nir_remove_tex_shadow(nir_shader *shader, unsigned textures_bitmask);
6950
6951 void
6952 nir_trivialize_registers(nir_shader *s);
6953
6954 unsigned
6955 nir_static_workgroup_size(const nir_shader *s);
6956
6957 static inline nir_intrinsic_instr *
nir_reg_get_decl(nir_def * reg)6958 nir_reg_get_decl(nir_def *reg)
6959 {
6960 assert(reg->parent_instr->type == nir_instr_type_intrinsic);
6961 nir_intrinsic_instr *decl = nir_instr_as_intrinsic(reg->parent_instr);
6962 assert(decl->intrinsic == nir_intrinsic_decl_reg);
6963
6964 return decl;
6965 }
6966
6967 static inline nir_intrinsic_instr *
nir_next_decl_reg(nir_intrinsic_instr * prev,nir_function_impl * impl)6968 nir_next_decl_reg(nir_intrinsic_instr *prev, nir_function_impl *impl)
6969 {
6970 nir_instr *start;
6971 if (prev != NULL)
6972 start = nir_instr_next(&prev->instr);
6973 else if (impl != NULL)
6974 start = nir_block_first_instr(nir_start_block(impl));
6975 else
6976 return NULL;
6977
6978 for (nir_instr *instr = start; instr; instr = nir_instr_next(instr)) {
6979 if (instr->type != nir_instr_type_intrinsic)
6980 continue;
6981
6982 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
6983 if (intrin->intrinsic == nir_intrinsic_decl_reg)
6984 return intrin;
6985 }
6986
6987 return NULL;
6988 }
6989
6990 #define nir_foreach_reg_decl(reg, impl) \
6991 for (nir_intrinsic_instr *reg = nir_next_decl_reg(NULL, impl); \
6992 reg; reg = nir_next_decl_reg(reg, NULL))
6993
6994 #define nir_foreach_reg_decl_safe(reg, impl) \
6995 for (nir_intrinsic_instr *reg = nir_next_decl_reg(NULL, impl), \
6996 *next_ = nir_next_decl_reg(reg, NULL); \
6997 reg; reg = next_, next_ = nir_next_decl_reg(next_, NULL))
6998
6999 static inline nir_cursor
nir_after_reg_decls(nir_function_impl * impl)7000 nir_after_reg_decls(nir_function_impl *impl)
7001 {
7002 nir_intrinsic_instr *last_reg_decl = NULL;
7003 nir_foreach_reg_decl(reg_decl, impl)
7004 last_reg_decl = reg_decl;
7005
7006 if (last_reg_decl != NULL)
7007 return nir_after_instr(&last_reg_decl->instr);
7008 return nir_before_impl(impl);
7009 }
7010
7011 static inline bool
nir_is_load_reg(nir_intrinsic_instr * intr)7012 nir_is_load_reg(nir_intrinsic_instr *intr)
7013 {
7014 return intr->intrinsic == nir_intrinsic_load_reg ||
7015 intr->intrinsic == nir_intrinsic_load_reg_indirect;
7016 }
7017
7018 static inline bool
nir_is_store_reg(nir_intrinsic_instr * intr)7019 nir_is_store_reg(nir_intrinsic_instr *intr)
7020 {
7021 return intr->intrinsic == nir_intrinsic_store_reg ||
7022 intr->intrinsic == nir_intrinsic_store_reg_indirect;
7023 }
7024
7025 #define nir_foreach_reg_load(load, reg) \
7026 assert(reg->intrinsic == nir_intrinsic_decl_reg); \
7027 \
7028 nir_foreach_use(load, ®->def) \
7029 if (nir_is_load_reg(nir_instr_as_intrinsic(nir_src_parent_instr(load))))
7030
7031 #define nir_foreach_reg_load_safe(load, reg) \
7032 assert(reg->intrinsic == nir_intrinsic_decl_reg); \
7033 \
7034 nir_foreach_use_safe(load, ®->def) \
7035 if (nir_is_load_reg(nir_instr_as_intrinsic(nir_src_parent_instr(load))))
7036
7037 #define nir_foreach_reg_store(store, reg) \
7038 assert(reg->intrinsic == nir_intrinsic_decl_reg); \
7039 \
7040 nir_foreach_use(store, ®->def) \
7041 if (nir_is_store_reg(nir_instr_as_intrinsic(nir_src_parent_instr(store))))
7042
7043 #define nir_foreach_reg_store_safe(store, reg) \
7044 assert(reg->intrinsic == nir_intrinsic_decl_reg); \
7045 \
7046 nir_foreach_use_safe(store, ®->def) \
7047 if (nir_is_store_reg(nir_instr_as_intrinsic(nir_src_parent_instr(store))))
7048
7049 static inline nir_intrinsic_instr *
nir_load_reg_for_def(const nir_def * def)7050 nir_load_reg_for_def(const nir_def *def)
7051 {
7052 if (def->parent_instr->type != nir_instr_type_intrinsic)
7053 return NULL;
7054
7055 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(def->parent_instr);
7056 if (!nir_is_load_reg(intr))
7057 return NULL;
7058
7059 return intr;
7060 }
7061
7062 static inline nir_intrinsic_instr *
nir_store_reg_for_def(const nir_def * def)7063 nir_store_reg_for_def(const nir_def *def)
7064 {
7065 /* Look for the trivial store: single use of our destination by a
7066 * store_register intrinsic.
7067 */
7068 if (!list_is_singular(&def->uses))
7069 return NULL;
7070
7071 nir_src *src = list_first_entry(&def->uses, nir_src, use_link);
7072 if (nir_src_is_if(src))
7073 return NULL;
7074
7075 nir_instr *parent = nir_src_parent_instr(src);
7076 if (parent->type != nir_instr_type_intrinsic)
7077 return NULL;
7078
7079 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(parent);
7080 if (!nir_is_store_reg(intr))
7081 return NULL;
7082
7083 /* The first value is data. Third is indirect index, ignore that one. */
7084 if (&intr->src[0] != src)
7085 return NULL;
7086
7087 return intr;
7088 }
7089
7090 struct nir_use_dominance_state;
7091
7092 struct nir_use_dominance_state *
7093 nir_calc_use_dominance_impl(nir_function_impl *impl, bool post_dominance);
7094
7095 nir_instr *
7096 nir_get_immediate_use_dominator(struct nir_use_dominance_state *state,
7097 nir_instr *instr);
7098 nir_instr *nir_use_dominance_lca(struct nir_use_dominance_state *state,
7099 nir_instr *i1, nir_instr *i2);
7100 bool nir_instr_dominates_use(struct nir_use_dominance_state *state,
7101 nir_instr *parent, nir_instr *child);
7102 void nir_print_use_dominators(struct nir_use_dominance_state *state,
7103 nir_instr **instructions,
7104 unsigned num_instructions);
7105
7106 #include "nir_inline_helpers.h"
7107
7108 #ifdef __cplusplus
7109 } /* extern "C" */
7110 #endif
7111
7112 #endif /* NIR_H */
7113