nir: Add more helpers for working with const values
[mesa.git] / src / compiler / nir / nir.h
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 (cwabbott0@gmail.com)
25 *
26 */
27
28 #ifndef NIR_H
29 #define NIR_H
30
31 #include "util/hash_table.h"
32 #include "compiler/glsl/list.h"
33 #include "GL/gl.h" /* GLenum */
34 #include "util/list.h"
35 #include "util/ralloc.h"
36 #include "util/set.h"
37 #include "util/bitscan.h"
38 #include "util/bitset.h"
39 #include "util/macros.h"
40 #include "compiler/nir_types.h"
41 #include "compiler/shader_enums.h"
42 #include "compiler/shader_info.h"
43 #include <stdio.h>
44
45 #ifndef NDEBUG
46 #include "util/debug.h"
47 #endif /* NDEBUG */
48
49 #include "nir_opcodes.h"
50
51 #if defined(_WIN32) && !defined(snprintf)
52 #define snprintf _snprintf
53 #endif
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 #define NIR_FALSE 0u
60 #define NIR_TRUE (~0u)
61 #define NIR_MAX_VEC_COMPONENTS 4
62 #define NIR_MAX_MATRIX_COLUMNS 4
63 typedef uint8_t nir_component_mask_t;
64
65 /** Defines a cast function
66 *
67 * This macro defines a cast function from in_type to out_type where
68 * out_type is some structure type that contains a field of type out_type.
69 *
70 * Note that you have to be a bit careful as the generated cast function
71 * destroys constness.
72 */
73 #define NIR_DEFINE_CAST(name, in_type, out_type, field, \
74 type_field, type_value) \
75 static inline out_type * \
76 name(const in_type *parent) \
77 { \
78 assert(parent && parent->type_field == type_value); \
79 return exec_node_data(out_type, parent, field); \
80 }
81
82 struct nir_function;
83 struct nir_shader;
84 struct nir_instr;
85 struct nir_builder;
86
87
88 /**
89 * Description of built-in state associated with a uniform
90 *
91 * \sa nir_variable::state_slots
92 */
93 typedef struct {
94 gl_state_index16 tokens[STATE_LENGTH];
95 int swizzle;
96 } nir_state_slot;
97
98 typedef enum {
99 nir_var_shader_in = (1 << 0),
100 nir_var_shader_out = (1 << 1),
101 nir_var_shader_temp = (1 << 2),
102 nir_var_function_temp = (1 << 3),
103 nir_var_uniform = (1 << 4),
104 nir_var_mem_ubo = (1 << 5),
105 nir_var_system_value = (1 << 6),
106 nir_var_mem_ssbo = (1 << 7),
107 nir_var_mem_shared = (1 << 8),
108 nir_var_mem_global = (1 << 9),
109 nir_var_all = ~0,
110 } nir_variable_mode;
111
112 /**
113 * Rounding modes.
114 */
115 typedef enum {
116 nir_rounding_mode_undef = 0,
117 nir_rounding_mode_rtne = 1, /* round to nearest even */
118 nir_rounding_mode_ru = 2, /* round up */
119 nir_rounding_mode_rd = 3, /* round down */
120 nir_rounding_mode_rtz = 4, /* round towards zero */
121 } nir_rounding_mode;
122
123 typedef union {
124 bool b;
125 float f32;
126 double f64;
127 int8_t i8;
128 uint8_t u8;
129 int16_t i16;
130 uint16_t u16;
131 int32_t i32;
132 uint32_t u32;
133 int64_t i64;
134 uint64_t u64;
135 } nir_const_value;
136
137 #define nir_const_value_to_array(arr, c, components, m) \
138 { \
139 for (unsigned i = 0; i < components; ++i) \
140 arr[i] = c[i].m; \
141 } while (false)
142
143 static inline nir_const_value
144 nir_const_value_for_raw_uint(uint64_t x, unsigned bit_size)
145 {
146 nir_const_value v;
147 memset(&v, 0, sizeof(v));
148
149 switch (bit_size) {
150 case 1: v.b = x; break;
151 case 8: v.u8 = x; break;
152 case 16: v.u16 = x; break;
153 case 32: v.u32 = x; break;
154 case 64: v.u64 = x; break;
155 default:
156 unreachable("Invalid bit size");
157 }
158
159 return v;
160 }
161
162 static inline nir_const_value
163 nir_const_value_for_int(int64_t i, unsigned bit_size)
164 {
165 nir_const_value v;
166 memset(&v, 0, sizeof(v));
167
168 assert(bit_size <= 64);
169 if (bit_size < 64) {
170 assert(i >= (-(1ll << (bit_size - 1))));
171 assert(i < (1ll << (bit_size - 1)));
172 }
173
174 return nir_const_value_for_raw_uint(i, bit_size);
175 }
176
177 static inline nir_const_value
178 nir_const_value_for_uint(uint64_t u, unsigned bit_size)
179 {
180 nir_const_value v;
181 memset(&v, 0, sizeof(v));
182
183 assert(bit_size <= 64);
184 if (bit_size < 64)
185 assert(u < (1ull << bit_size));
186
187 return nir_const_value_for_raw_uint(u, bit_size);
188 }
189
190 static inline nir_const_value
191 nir_const_value_for_bool(bool b, unsigned bit_size)
192 {
193 /* Booleans use a 0/-1 convention */
194 return nir_const_value_for_int(-(int)b, bit_size);
195 }
196
197 /* This one isn't inline because it requires half-float conversion */
198 nir_const_value nir_const_value_for_float(double b, unsigned bit_size);
199
200 static inline int64_t
201 nir_const_value_as_int(nir_const_value value, unsigned bit_size)
202 {
203 switch (bit_size) {
204 /* int1_t uses 0/-1 convention */
205 case 1: return -(int)value.b;
206 case 8: return value.i8;
207 case 16: return value.i16;
208 case 32: return value.i32;
209 case 64: return value.i64;
210 default:
211 unreachable("Invalid bit size");
212 }
213 }
214
215 static inline int64_t
216 nir_const_value_as_uint(nir_const_value value, unsigned bit_size)
217 {
218 switch (bit_size) {
219 case 1: return value.b;
220 case 8: return value.u8;
221 case 16: return value.u16;
222 case 32: return value.u32;
223 case 64: return value.u64;
224 default:
225 unreachable("Invalid bit size");
226 }
227 }
228
229 static inline bool
230 nir_const_value_as_bool(nir_const_value value, unsigned bit_size)
231 {
232 int64_t i = nir_const_value_as_int(value, bit_size);
233
234 /* Booleans of any size use 0/-1 convention */
235 assert(i == 0 || i == -1);
236
237 return i;
238 }
239
240 /* This one isn't inline because it requires half-float conversion */
241 double nir_const_value_as_float(nir_const_value value, unsigned bit_size);
242
243 typedef struct nir_constant {
244 /**
245 * Value of the constant.
246 *
247 * The field used to back the values supplied by the constant is determined
248 * by the type associated with the \c nir_variable. Constants may be
249 * scalars, vectors, or matrices.
250 */
251 nir_const_value values[NIR_MAX_VEC_COMPONENTS];
252
253 /* we could get this from the var->type but makes clone *much* easier to
254 * not have to care about the type.
255 */
256 unsigned num_elements;
257
258 /* Array elements / Structure Fields */
259 struct nir_constant **elements;
260 } nir_constant;
261
262 /**
263 * \brief Layout qualifiers for gl_FragDepth.
264 *
265 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
266 * with a layout qualifier.
267 */
268 typedef enum {
269 nir_depth_layout_none, /**< No depth layout is specified. */
270 nir_depth_layout_any,
271 nir_depth_layout_greater,
272 nir_depth_layout_less,
273 nir_depth_layout_unchanged
274 } nir_depth_layout;
275
276 /**
277 * Enum keeping track of how a variable was declared.
278 */
279 typedef enum {
280 /**
281 * Normal declaration.
282 */
283 nir_var_declared_normally = 0,
284
285 /**
286 * Variable is implicitly generated by the compiler and should not be
287 * visible via the API.
288 */
289 nir_var_hidden,
290 } nir_var_declaration_type;
291
292 /**
293 * Either a uniform, global variable, shader input, or shader output. Based on
294 * ir_variable - it should be easy to translate between the two.
295 */
296
297 typedef struct nir_variable {
298 struct exec_node node;
299
300 /**
301 * Declared type of the variable
302 */
303 const struct glsl_type *type;
304
305 /**
306 * Declared name of the variable
307 */
308 char *name;
309
310 struct nir_variable_data {
311 /**
312 * Storage class of the variable.
313 *
314 * \sa nir_variable_mode
315 */
316 nir_variable_mode mode;
317
318 /**
319 * Is the variable read-only?
320 *
321 * This is set for variables declared as \c const, shader inputs,
322 * and uniforms.
323 */
324 unsigned read_only:1;
325 unsigned centroid:1;
326 unsigned sample:1;
327 unsigned patch:1;
328 unsigned invariant:1;
329
330 /**
331 * Can this variable be coalesced with another?
332 *
333 * This is set by nir_lower_io_to_temporaries to say that any
334 * copies involving this variable should stay put. Propagating it can
335 * duplicate the resulting load/store, which is not wanted, and may
336 * result in a load/store of the variable with an indirect offset which
337 * the backend may not be able to handle.
338 */
339 unsigned cannot_coalesce:1;
340
341 /**
342 * When separate shader programs are enabled, only input/outputs between
343 * the stages of a multi-stage separate program can be safely removed
344 * from the shader interface. Other input/outputs must remains active.
345 *
346 * This is also used to make sure xfb varyings that are unused by the
347 * fragment shader are not removed.
348 */
349 unsigned always_active_io:1;
350
351 /**
352 * Interpolation mode for shader inputs / outputs
353 *
354 * \sa glsl_interp_mode
355 */
356 unsigned interpolation:2;
357
358 /**
359 * If non-zero, then this variable may be packed along with other variables
360 * into a single varying slot, so this offset should be applied when
361 * accessing components. For example, an offset of 1 means that the x
362 * component of this variable is actually stored in component y of the
363 * location specified by \c location.
364 */
365 unsigned location_frac:2;
366
367 /**
368 * If true, this variable represents an array of scalars that should
369 * be tightly packed. In other words, consecutive array elements
370 * should be stored one component apart, rather than one slot apart.
371 */
372 unsigned compact:1;
373
374 /**
375 * Whether this is a fragment shader output implicitly initialized with
376 * the previous contents of the specified render target at the
377 * framebuffer location corresponding to this shader invocation.
378 */
379 unsigned fb_fetch_output:1;
380
381 /**
382 * Non-zero if this variable is considered bindless as defined by
383 * ARB_bindless_texture.
384 */
385 unsigned bindless:1;
386
387 /**
388 * Was an explicit binding set in the shader?
389 */
390 unsigned explicit_binding:1;
391
392 /**
393 * Was a transfer feedback buffer set in the shader?
394 */
395 unsigned explicit_xfb_buffer:1;
396
397 /**
398 * Was a transfer feedback stride set in the shader?
399 */
400 unsigned explicit_xfb_stride:1;
401
402 /**
403 * Was an explicit offset set in the shader?
404 */
405 unsigned explicit_offset:1;
406
407 /**
408 * \brief Layout qualifier for gl_FragDepth.
409 *
410 * This is not equal to \c ir_depth_layout_none if and only if this
411 * variable is \c gl_FragDepth and a layout qualifier is specified.
412 */
413 nir_depth_layout depth_layout;
414
415 /**
416 * Storage location of the base of this variable
417 *
418 * The precise meaning of this field depends on the nature of the variable.
419 *
420 * - Vertex shader input: one of the values from \c gl_vert_attrib.
421 * - Vertex shader output: one of the values from \c gl_varying_slot.
422 * - Geometry shader input: one of the values from \c gl_varying_slot.
423 * - Geometry shader output: one of the values from \c gl_varying_slot.
424 * - Fragment shader input: one of the values from \c gl_varying_slot.
425 * - Fragment shader output: one of the values from \c gl_frag_result.
426 * - Uniforms: Per-stage uniform slot number for default uniform block.
427 * - Uniforms: Index within the uniform block definition for UBO members.
428 * - Non-UBO Uniforms: uniform slot number.
429 * - Other: This field is not currently used.
430 *
431 * If the variable is a uniform, shader input, or shader output, and the
432 * slot has not been assigned, the value will be -1.
433 */
434 int location;
435
436 /**
437 * The actual location of the variable in the IR. Only valid for inputs
438 * and outputs.
439 */
440 unsigned int driver_location;
441
442 /**
443 * Vertex stream output identifier.
444 *
445 * For packed outputs, bit 31 is set and bits [2*i+1,2*i] indicate the
446 * stream of the i-th component.
447 */
448 unsigned stream;
449
450 /**
451 * output index for dual source blending.
452 */
453 int index;
454
455 /**
456 * Descriptor set binding for sampler or UBO.
457 */
458 int descriptor_set;
459
460 /**
461 * Initial binding point for a sampler or UBO.
462 *
463 * For array types, this represents the binding point for the first element.
464 */
465 int binding;
466
467 /**
468 * Location an atomic counter or transform feedback is stored at.
469 */
470 unsigned offset;
471
472 /**
473 * Transform feedback buffer.
474 */
475 unsigned xfb_buffer;
476
477 /**
478 * Transform feedback stride.
479 */
480 unsigned xfb_stride;
481
482 /**
483 * How the variable was declared. See nir_var_declaration_type.
484 *
485 * This is used to detect variables generated by the compiler, so should
486 * not be visible via the API.
487 */
488 unsigned how_declared:2;
489
490 /**
491 * ARB_shader_image_load_store qualifiers.
492 */
493 struct {
494 enum gl_access_qualifier access;
495
496 /** Image internal format if specified explicitly, otherwise GL_NONE. */
497 GLenum format;
498 } image;
499 } data;
500
501 /**
502 * Built-in state that backs this uniform
503 *
504 * Once set at variable creation, \c state_slots must remain invariant.
505 * This is because, ideally, this array would be shared by all clones of
506 * this variable in the IR tree. In other words, we'd really like for it
507 * to be a fly-weight.
508 *
509 * If the variable is not a uniform, \c num_state_slots will be zero and
510 * \c state_slots will be \c NULL.
511 */
512 /*@{*/
513 unsigned num_state_slots; /**< Number of state slots used */
514 nir_state_slot *state_slots; /**< State descriptors. */
515 /*@}*/
516
517 /**
518 * Constant expression assigned in the initializer of the variable
519 *
520 * This field should only be used temporarily by creators of NIR shaders
521 * and then lower_constant_initializers can be used to get rid of them.
522 * Most of the rest of NIR ignores this field or asserts that it's NULL.
523 */
524 nir_constant *constant_initializer;
525
526 /**
527 * For variables that are in an interface block or are an instance of an
528 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
529 *
530 * \sa ir_variable::location
531 */
532 const struct glsl_type *interface_type;
533
534 /**
535 * Description of per-member data for per-member struct variables
536 *
537 * This is used for variables which are actually an amalgamation of
538 * multiple entities such as a struct of built-in values or a struct of
539 * inputs each with their own layout specifier. This is only allowed on
540 * variables with a struct or array of array of struct type.
541 */
542 unsigned num_members;
543 struct nir_variable_data *members;
544 } nir_variable;
545
546 #define nir_foreach_variable(var, var_list) \
547 foreach_list_typed(nir_variable, var, node, var_list)
548
549 #define nir_foreach_variable_safe(var, var_list) \
550 foreach_list_typed_safe(nir_variable, var, node, var_list)
551
552 static inline bool
553 nir_variable_is_global(const nir_variable *var)
554 {
555 return var->data.mode != nir_var_function_temp;
556 }
557
558 typedef struct nir_register {
559 struct exec_node node;
560
561 unsigned num_components; /** < number of vector components */
562 unsigned num_array_elems; /** < size of array (0 for no array) */
563
564 /* The bit-size of each channel; must be one of 8, 16, 32, or 64 */
565 uint8_t bit_size;
566
567 /** generic register index. */
568 unsigned index;
569
570 /** only for debug purposes, can be NULL */
571 const char *name;
572
573 /** set of nir_srcs where this register is used (read from) */
574 struct list_head uses;
575
576 /** set of nir_dests where this register is defined (written to) */
577 struct list_head defs;
578
579 /** set of nir_ifs where this register is used as a condition */
580 struct list_head if_uses;
581 } nir_register;
582
583 #define nir_foreach_register(reg, reg_list) \
584 foreach_list_typed(nir_register, reg, node, reg_list)
585 #define nir_foreach_register_safe(reg, reg_list) \
586 foreach_list_typed_safe(nir_register, reg, node, reg_list)
587
588 typedef enum PACKED {
589 nir_instr_type_alu,
590 nir_instr_type_deref,
591 nir_instr_type_call,
592 nir_instr_type_tex,
593 nir_instr_type_intrinsic,
594 nir_instr_type_load_const,
595 nir_instr_type_jump,
596 nir_instr_type_ssa_undef,
597 nir_instr_type_phi,
598 nir_instr_type_parallel_copy,
599 } nir_instr_type;
600
601 typedef struct nir_instr {
602 struct exec_node node;
603 struct nir_block *block;
604 nir_instr_type type;
605
606 /* A temporary for optimization and analysis passes to use for storing
607 * flags. For instance, DCE uses this to store the "dead/live" info.
608 */
609 uint8_t pass_flags;
610
611 /** generic instruction index. */
612 unsigned index;
613 } nir_instr;
614
615 static inline nir_instr *
616 nir_instr_next(nir_instr *instr)
617 {
618 struct exec_node *next = exec_node_get_next(&instr->node);
619 if (exec_node_is_tail_sentinel(next))
620 return NULL;
621 else
622 return exec_node_data(nir_instr, next, node);
623 }
624
625 static inline nir_instr *
626 nir_instr_prev(nir_instr *instr)
627 {
628 struct exec_node *prev = exec_node_get_prev(&instr->node);
629 if (exec_node_is_head_sentinel(prev))
630 return NULL;
631 else
632 return exec_node_data(nir_instr, prev, node);
633 }
634
635 static inline bool
636 nir_instr_is_first(const nir_instr *instr)
637 {
638 return exec_node_is_head_sentinel(exec_node_get_prev_const(&instr->node));
639 }
640
641 static inline bool
642 nir_instr_is_last(const nir_instr *instr)
643 {
644 return exec_node_is_tail_sentinel(exec_node_get_next_const(&instr->node));
645 }
646
647 typedef struct nir_ssa_def {
648 /** for debugging only, can be NULL */
649 const char* name;
650
651 /** generic SSA definition index. */
652 unsigned index;
653
654 /** Index into the live_in and live_out bitfields */
655 unsigned live_index;
656
657 /** Instruction which produces this SSA value. */
658 nir_instr *parent_instr;
659
660 /** set of nir_instrs where this register is used (read from) */
661 struct list_head uses;
662
663 /** set of nir_ifs where this register is used as a condition */
664 struct list_head if_uses;
665
666 uint8_t num_components;
667
668 /* The bit-size of each channel; must be one of 8, 16, 32, or 64 */
669 uint8_t bit_size;
670 } nir_ssa_def;
671
672 struct nir_src;
673
674 typedef struct {
675 nir_register *reg;
676 struct nir_src *indirect; /** < NULL for no indirect offset */
677 unsigned base_offset;
678
679 /* TODO use-def chain goes here */
680 } nir_reg_src;
681
682 typedef struct {
683 nir_instr *parent_instr;
684 struct list_head def_link;
685
686 nir_register *reg;
687 struct nir_src *indirect; /** < NULL for no indirect offset */
688 unsigned base_offset;
689
690 /* TODO def-use chain goes here */
691 } nir_reg_dest;
692
693 struct nir_if;
694
695 typedef struct nir_src {
696 union {
697 /** Instruction that consumes this value as a source. */
698 nir_instr *parent_instr;
699 struct nir_if *parent_if;
700 };
701
702 struct list_head use_link;
703
704 union {
705 nir_reg_src reg;
706 nir_ssa_def *ssa;
707 };
708
709 bool is_ssa;
710 } nir_src;
711
712 static inline nir_src
713 nir_src_init(void)
714 {
715 nir_src src = { { NULL } };
716 return src;
717 }
718
719 #define NIR_SRC_INIT nir_src_init()
720
721 #define nir_foreach_use(src, reg_or_ssa_def) \
722 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
723
724 #define nir_foreach_use_safe(src, reg_or_ssa_def) \
725 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
726
727 #define nir_foreach_if_use(src, reg_or_ssa_def) \
728 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
729
730 #define nir_foreach_if_use_safe(src, reg_or_ssa_def) \
731 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
732
733 typedef struct {
734 union {
735 nir_reg_dest reg;
736 nir_ssa_def ssa;
737 };
738
739 bool is_ssa;
740 } nir_dest;
741
742 static inline nir_dest
743 nir_dest_init(void)
744 {
745 nir_dest dest = { { { NULL } } };
746 return dest;
747 }
748
749 #define NIR_DEST_INIT nir_dest_init()
750
751 #define nir_foreach_def(dest, reg) \
752 list_for_each_entry(nir_dest, dest, &(reg)->defs, reg.def_link)
753
754 #define nir_foreach_def_safe(dest, reg) \
755 list_for_each_entry_safe(nir_dest, dest, &(reg)->defs, reg.def_link)
756
757 static inline nir_src
758 nir_src_for_ssa(nir_ssa_def *def)
759 {
760 nir_src src = NIR_SRC_INIT;
761
762 src.is_ssa = true;
763 src.ssa = def;
764
765 return src;
766 }
767
768 static inline nir_src
769 nir_src_for_reg(nir_register *reg)
770 {
771 nir_src src = NIR_SRC_INIT;
772
773 src.is_ssa = false;
774 src.reg.reg = reg;
775 src.reg.indirect = NULL;
776 src.reg.base_offset = 0;
777
778 return src;
779 }
780
781 static inline nir_dest
782 nir_dest_for_reg(nir_register *reg)
783 {
784 nir_dest dest = NIR_DEST_INIT;
785
786 dest.reg.reg = reg;
787
788 return dest;
789 }
790
791 static inline unsigned
792 nir_src_bit_size(nir_src src)
793 {
794 return src.is_ssa ? src.ssa->bit_size : src.reg.reg->bit_size;
795 }
796
797 static inline unsigned
798 nir_src_num_components(nir_src src)
799 {
800 return src.is_ssa ? src.ssa->num_components : src.reg.reg->num_components;
801 }
802
803 static inline bool
804 nir_src_is_const(nir_src src)
805 {
806 return src.is_ssa &&
807 src.ssa->parent_instr->type == nir_instr_type_load_const;
808 }
809
810 int64_t nir_src_as_int(nir_src src);
811 uint64_t nir_src_as_uint(nir_src src);
812 bool nir_src_as_bool(nir_src src);
813 double nir_src_as_float(nir_src src);
814 int64_t nir_src_comp_as_int(nir_src src, unsigned component);
815 uint64_t nir_src_comp_as_uint(nir_src src, unsigned component);
816 bool nir_src_comp_as_bool(nir_src src, unsigned component);
817 double nir_src_comp_as_float(nir_src src, unsigned component);
818
819 static inline unsigned
820 nir_dest_bit_size(nir_dest dest)
821 {
822 return dest.is_ssa ? dest.ssa.bit_size : dest.reg.reg->bit_size;
823 }
824
825 static inline unsigned
826 nir_dest_num_components(nir_dest dest)
827 {
828 return dest.is_ssa ? dest.ssa.num_components : dest.reg.reg->num_components;
829 }
830
831 void nir_src_copy(nir_src *dest, const nir_src *src, void *instr_or_if);
832 void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr);
833
834 typedef struct {
835 nir_src src;
836
837 /**
838 * \name input modifiers
839 */
840 /*@{*/
841 /**
842 * For inputs interpreted as floating point, flips the sign bit. For
843 * inputs interpreted as integers, performs the two's complement negation.
844 */
845 bool negate;
846
847 /**
848 * Clears the sign bit for floating point values, and computes the integer
849 * absolute value for integers. Note that the negate modifier acts after
850 * the absolute value modifier, therefore if both are set then all inputs
851 * will become negative.
852 */
853 bool abs;
854 /*@}*/
855
856 /**
857 * For each input component, says which component of the register it is
858 * chosen from. Note that which elements of the swizzle are used and which
859 * are ignored are based on the write mask for most opcodes - for example,
860 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
861 * a swizzle of {2, x, 1, 0} where x means "don't care."
862 */
863 uint8_t swizzle[NIR_MAX_VEC_COMPONENTS];
864 } nir_alu_src;
865
866 typedef struct {
867 nir_dest dest;
868
869 /**
870 * \name saturate output modifier
871 *
872 * Only valid for opcodes that output floating-point numbers. Clamps the
873 * output to between 0.0 and 1.0 inclusive.
874 */
875
876 bool saturate;
877
878 unsigned write_mask : NIR_MAX_VEC_COMPONENTS; /* ignored if dest.is_ssa is true */
879 } nir_alu_dest;
880
881 /** NIR sized and unsized types
882 *
883 * The values in this enum are carefully chosen so that the sized type is
884 * just the unsized type OR the number of bits.
885 */
886 typedef enum {
887 nir_type_invalid = 0, /* Not a valid type */
888 nir_type_int = 2,
889 nir_type_uint = 4,
890 nir_type_bool = 6,
891 nir_type_float = 128,
892 nir_type_bool1 = 1 | nir_type_bool,
893 nir_type_bool32 = 32 | nir_type_bool,
894 nir_type_int1 = 1 | nir_type_int,
895 nir_type_int8 = 8 | nir_type_int,
896 nir_type_int16 = 16 | nir_type_int,
897 nir_type_int32 = 32 | nir_type_int,
898 nir_type_int64 = 64 | nir_type_int,
899 nir_type_uint1 = 1 | nir_type_uint,
900 nir_type_uint8 = 8 | nir_type_uint,
901 nir_type_uint16 = 16 | nir_type_uint,
902 nir_type_uint32 = 32 | nir_type_uint,
903 nir_type_uint64 = 64 | nir_type_uint,
904 nir_type_float16 = 16 | nir_type_float,
905 nir_type_float32 = 32 | nir_type_float,
906 nir_type_float64 = 64 | nir_type_float,
907 } nir_alu_type;
908
909 #define NIR_ALU_TYPE_SIZE_MASK 0x79
910 #define NIR_ALU_TYPE_BASE_TYPE_MASK 0x86
911
912 static inline unsigned
913 nir_alu_type_get_type_size(nir_alu_type type)
914 {
915 return type & NIR_ALU_TYPE_SIZE_MASK;
916 }
917
918 static inline unsigned
919 nir_alu_type_get_base_type(nir_alu_type type)
920 {
921 return type & NIR_ALU_TYPE_BASE_TYPE_MASK;
922 }
923
924 static inline nir_alu_type
925 nir_get_nir_type_for_glsl_base_type(enum glsl_base_type base_type)
926 {
927 switch (base_type) {
928 case GLSL_TYPE_BOOL:
929 return nir_type_bool1;
930 break;
931 case GLSL_TYPE_UINT:
932 return nir_type_uint32;
933 break;
934 case GLSL_TYPE_INT:
935 return nir_type_int32;
936 break;
937 case GLSL_TYPE_UINT16:
938 return nir_type_uint16;
939 break;
940 case GLSL_TYPE_INT16:
941 return nir_type_int16;
942 break;
943 case GLSL_TYPE_UINT8:
944 return nir_type_uint8;
945 case GLSL_TYPE_INT8:
946 return nir_type_int8;
947 case GLSL_TYPE_UINT64:
948 return nir_type_uint64;
949 break;
950 case GLSL_TYPE_INT64:
951 return nir_type_int64;
952 break;
953 case GLSL_TYPE_FLOAT:
954 return nir_type_float32;
955 break;
956 case GLSL_TYPE_FLOAT16:
957 return nir_type_float16;
958 break;
959 case GLSL_TYPE_DOUBLE:
960 return nir_type_float64;
961 break;
962
963 case GLSL_TYPE_SAMPLER:
964 case GLSL_TYPE_IMAGE:
965 case GLSL_TYPE_ATOMIC_UINT:
966 case GLSL_TYPE_STRUCT:
967 case GLSL_TYPE_INTERFACE:
968 case GLSL_TYPE_ARRAY:
969 case GLSL_TYPE_VOID:
970 case GLSL_TYPE_SUBROUTINE:
971 case GLSL_TYPE_FUNCTION:
972 case GLSL_TYPE_ERROR:
973 return nir_type_invalid;
974 }
975
976 unreachable("unknown type");
977 }
978
979 static inline nir_alu_type
980 nir_get_nir_type_for_glsl_type(const struct glsl_type *type)
981 {
982 return nir_get_nir_type_for_glsl_base_type(glsl_get_base_type(type));
983 }
984
985 nir_op nir_type_conversion_op(nir_alu_type src, nir_alu_type dst,
986 nir_rounding_mode rnd);
987
988 static inline nir_op
989 nir_op_vec(unsigned components)
990 {
991 switch (components) {
992 case 1: return nir_op_mov;
993 case 2: return nir_op_vec2;
994 case 3: return nir_op_vec3;
995 case 4: return nir_op_vec4;
996 default: unreachable("bad component count");
997 }
998 }
999
1000 typedef enum {
1001 /**
1002 * Operation where the first two sources are commutative.
1003 *
1004 * For 2-source operations, this just mathematical commutativity. Some
1005 * 3-source operations, like ffma, are only commutative in the first two
1006 * sources.
1007 */
1008 NIR_OP_IS_2SRC_COMMUTATIVE = (1 << 0),
1009 NIR_OP_IS_ASSOCIATIVE = (1 << 1),
1010 } nir_op_algebraic_property;
1011
1012 typedef struct {
1013 const char *name;
1014
1015 unsigned num_inputs;
1016
1017 /**
1018 * The number of components in the output
1019 *
1020 * If non-zero, this is the size of the output and input sizes are
1021 * explicitly given; swizzle and writemask are still in effect, but if
1022 * the output component is masked out, then the input component may
1023 * still be in use.
1024 *
1025 * If zero, the opcode acts in the standard, per-component manner; the
1026 * operation is performed on each component (except the ones that are
1027 * masked out) with the input being taken from the input swizzle for
1028 * that component.
1029 *
1030 * The size of some of the inputs may be given (i.e. non-zero) even
1031 * though output_size is zero; in that case, the inputs with a zero
1032 * size act per-component, while the inputs with non-zero size don't.
1033 */
1034 unsigned output_size;
1035
1036 /**
1037 * The type of vector that the instruction outputs. Note that the
1038 * staurate modifier is only allowed on outputs with the float type.
1039 */
1040
1041 nir_alu_type output_type;
1042
1043 /**
1044 * The number of components in each input
1045 */
1046 unsigned input_sizes[NIR_MAX_VEC_COMPONENTS];
1047
1048 /**
1049 * The type of vector that each input takes. Note that negate and
1050 * absolute value are only allowed on inputs with int or float type and
1051 * behave differently on the two.
1052 */
1053 nir_alu_type input_types[NIR_MAX_VEC_COMPONENTS];
1054
1055 nir_op_algebraic_property algebraic_properties;
1056
1057 /* Whether this represents a numeric conversion opcode */
1058 bool is_conversion;
1059 } nir_op_info;
1060
1061 extern const nir_op_info nir_op_infos[nir_num_opcodes];
1062
1063 typedef struct nir_alu_instr {
1064 nir_instr instr;
1065 nir_op op;
1066
1067 /** Indicates that this ALU instruction generates an exact value
1068 *
1069 * This is kind of a mixture of GLSL "precise" and "invariant" and not
1070 * really equivalent to either. This indicates that the value generated by
1071 * this operation is high-precision and any code transformations that touch
1072 * it must ensure that the resulting value is bit-for-bit identical to the
1073 * original.
1074 */
1075 bool exact:1;
1076
1077 /**
1078 * Indicates that this instruction do not cause wrapping to occur, in the
1079 * form of overflow or underflow.
1080 */
1081 bool no_signed_wrap:1;
1082 bool no_unsigned_wrap:1;
1083
1084 nir_alu_dest dest;
1085 nir_alu_src src[];
1086 } nir_alu_instr;
1087
1088 void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
1089 nir_alu_instr *instr);
1090 void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
1091 nir_alu_instr *instr);
1092
1093 /* is this source channel used? */
1094 static inline bool
1095 nir_alu_instr_channel_used(const nir_alu_instr *instr, unsigned src,
1096 unsigned channel)
1097 {
1098 if (nir_op_infos[instr->op].input_sizes[src] > 0)
1099 return channel < nir_op_infos[instr->op].input_sizes[src];
1100
1101 return (instr->dest.write_mask >> channel) & 1;
1102 }
1103
1104 static inline nir_component_mask_t
1105 nir_alu_instr_src_read_mask(const nir_alu_instr *instr, unsigned src)
1106 {
1107 nir_component_mask_t read_mask = 0;
1108 for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; c++) {
1109 if (!nir_alu_instr_channel_used(instr, src, c))
1110 continue;
1111
1112 read_mask |= (1 << instr->src[src].swizzle[c]);
1113 }
1114 return read_mask;
1115 }
1116
1117 /**
1118 * Get the number of channels used for a source
1119 */
1120 static inline unsigned
1121 nir_ssa_alu_instr_src_components(const nir_alu_instr *instr, unsigned src)
1122 {
1123 if (nir_op_infos[instr->op].input_sizes[src] > 0)
1124 return nir_op_infos[instr->op].input_sizes[src];
1125
1126 return nir_dest_num_components(instr->dest.dest);
1127 }
1128
1129 bool nir_const_value_negative_equal(nir_const_value c1, nir_const_value c2,
1130 nir_alu_type full_type);
1131
1132 bool nir_alu_srcs_equal(const nir_alu_instr *alu1, const nir_alu_instr *alu2,
1133 unsigned src1, unsigned src2);
1134
1135 bool nir_alu_srcs_negative_equal(const nir_alu_instr *alu1,
1136 const nir_alu_instr *alu2,
1137 unsigned src1, unsigned src2);
1138
1139 typedef enum {
1140 nir_deref_type_var,
1141 nir_deref_type_array,
1142 nir_deref_type_array_wildcard,
1143 nir_deref_type_ptr_as_array,
1144 nir_deref_type_struct,
1145 nir_deref_type_cast,
1146 } nir_deref_type;
1147
1148 typedef struct {
1149 nir_instr instr;
1150
1151 /** The type of this deref instruction */
1152 nir_deref_type deref_type;
1153
1154 /** The mode of the underlying variable */
1155 nir_variable_mode mode;
1156
1157 /** The dereferenced type of the resulting pointer value */
1158 const struct glsl_type *type;
1159
1160 union {
1161 /** Variable being dereferenced if deref_type is a deref_var */
1162 nir_variable *var;
1163
1164 /** Parent deref if deref_type is not deref_var */
1165 nir_src parent;
1166 };
1167
1168 /** Additional deref parameters */
1169 union {
1170 struct {
1171 nir_src index;
1172 } arr;
1173
1174 struct {
1175 unsigned index;
1176 } strct;
1177
1178 struct {
1179 unsigned ptr_stride;
1180 } cast;
1181 };
1182
1183 /** Destination to store the resulting "pointer" */
1184 nir_dest dest;
1185 } nir_deref_instr;
1186
1187 static inline nir_deref_instr *nir_src_as_deref(nir_src src);
1188
1189 static inline nir_deref_instr *
1190 nir_deref_instr_parent(const nir_deref_instr *instr)
1191 {
1192 if (instr->deref_type == nir_deref_type_var)
1193 return NULL;
1194 else
1195 return nir_src_as_deref(instr->parent);
1196 }
1197
1198 static inline nir_variable *
1199 nir_deref_instr_get_variable(const nir_deref_instr *instr)
1200 {
1201 while (instr->deref_type != nir_deref_type_var) {
1202 if (instr->deref_type == nir_deref_type_cast)
1203 return NULL;
1204
1205 instr = nir_deref_instr_parent(instr);
1206 }
1207
1208 return instr->var;
1209 }
1210
1211 bool nir_deref_instr_has_indirect(nir_deref_instr *instr);
1212 bool nir_deref_instr_has_complex_use(nir_deref_instr *instr);
1213
1214 bool nir_deref_instr_remove_if_unused(nir_deref_instr *instr);
1215
1216 unsigned nir_deref_instr_ptr_as_array_stride(nir_deref_instr *instr);
1217
1218 typedef struct {
1219 nir_instr instr;
1220
1221 struct nir_function *callee;
1222
1223 unsigned num_params;
1224 nir_src params[];
1225 } nir_call_instr;
1226
1227 #include "nir_intrinsics.h"
1228
1229 #define NIR_INTRINSIC_MAX_CONST_INDEX 4
1230
1231 /** Represents an intrinsic
1232 *
1233 * An intrinsic is an instruction type for handling things that are
1234 * more-or-less regular operations but don't just consume and produce SSA
1235 * values like ALU operations do. Intrinsics are not for things that have
1236 * special semantic meaning such as phi nodes and parallel copies.
1237 * Examples of intrinsics include variable load/store operations, system
1238 * value loads, and the like. Even though texturing more-or-less falls
1239 * under this category, texturing is its own instruction type because
1240 * trying to represent texturing with intrinsics would lead to a
1241 * combinatorial explosion of intrinsic opcodes.
1242 *
1243 * By having a single instruction type for handling a lot of different
1244 * cases, optimization passes can look for intrinsics and, for the most
1245 * part, completely ignore them. Each intrinsic type also has a few
1246 * possible flags that govern whether or not they can be reordered or
1247 * eliminated. That way passes like dead code elimination can still work
1248 * on intrisics without understanding the meaning of each.
1249 *
1250 * Each intrinsic has some number of constant indices, some number of
1251 * variables, and some number of sources. What these sources, variables,
1252 * and indices mean depends on the intrinsic and is documented with the
1253 * intrinsic declaration in nir_intrinsics.h. Intrinsics and texture
1254 * instructions are the only types of instruction that can operate on
1255 * variables.
1256 */
1257 typedef struct {
1258 nir_instr instr;
1259
1260 nir_intrinsic_op intrinsic;
1261
1262 nir_dest dest;
1263
1264 /** number of components if this is a vectorized intrinsic
1265 *
1266 * Similarly to ALU operations, some intrinsics are vectorized.
1267 * An intrinsic is vectorized if nir_intrinsic_infos.dest_components == 0.
1268 * For vectorized intrinsics, the num_components field specifies the
1269 * number of destination components and the number of source components
1270 * for all sources with nir_intrinsic_infos.src_components[i] == 0.
1271 */
1272 uint8_t num_components;
1273
1274 int const_index[NIR_INTRINSIC_MAX_CONST_INDEX];
1275
1276 nir_src src[];
1277 } nir_intrinsic_instr;
1278
1279 static inline nir_variable *
1280 nir_intrinsic_get_var(nir_intrinsic_instr *intrin, unsigned i)
1281 {
1282 return nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[i]));
1283 }
1284
1285 /**
1286 * \name NIR intrinsics semantic flags
1287 *
1288 * information about what the compiler can do with the intrinsics.
1289 *
1290 * \sa nir_intrinsic_info::flags
1291 */
1292 typedef enum {
1293 /**
1294 * whether the intrinsic can be safely eliminated if none of its output
1295 * value is not being used.
1296 */
1297 NIR_INTRINSIC_CAN_ELIMINATE = (1 << 0),
1298
1299 /**
1300 * Whether the intrinsic can be reordered with respect to any other
1301 * intrinsic, i.e. whether the only reordering dependencies of the
1302 * intrinsic are due to the register reads/writes.
1303 */
1304 NIR_INTRINSIC_CAN_REORDER = (1 << 1),
1305 } nir_intrinsic_semantic_flag;
1306
1307 /**
1308 * \name NIR intrinsics const-index flag
1309 *
1310 * Indicates the usage of a const_index slot.
1311 *
1312 * \sa nir_intrinsic_info::index_map
1313 */
1314 typedef enum {
1315 /**
1316 * Generally instructions that take a offset src argument, can encode
1317 * a constant 'base' value which is added to the offset.
1318 */
1319 NIR_INTRINSIC_BASE = 1,
1320
1321 /**
1322 * For store instructions, a writemask for the store.
1323 */
1324 NIR_INTRINSIC_WRMASK,
1325
1326 /**
1327 * The stream-id for GS emit_vertex/end_primitive intrinsics.
1328 */
1329 NIR_INTRINSIC_STREAM_ID,
1330
1331 /**
1332 * The clip-plane id for load_user_clip_plane intrinsic.
1333 */
1334 NIR_INTRINSIC_UCP_ID,
1335
1336 /**
1337 * The amount of data, starting from BASE, that this instruction may
1338 * access. This is used to provide bounds if the offset is not constant.
1339 */
1340 NIR_INTRINSIC_RANGE,
1341
1342 /**
1343 * The Vulkan descriptor set for vulkan_resource_index intrinsic.
1344 */
1345 NIR_INTRINSIC_DESC_SET,
1346
1347 /**
1348 * The Vulkan descriptor set binding for vulkan_resource_index intrinsic.
1349 */
1350 NIR_INTRINSIC_BINDING,
1351
1352 /**
1353 * Component offset.
1354 */
1355 NIR_INTRINSIC_COMPONENT,
1356
1357 /**
1358 * Interpolation mode (only meaningful for FS inputs).
1359 */
1360 NIR_INTRINSIC_INTERP_MODE,
1361
1362 /**
1363 * A binary nir_op to use when performing a reduction or scan operation
1364 */
1365 NIR_INTRINSIC_REDUCTION_OP,
1366
1367 /**
1368 * Cluster size for reduction operations
1369 */
1370 NIR_INTRINSIC_CLUSTER_SIZE,
1371
1372 /**
1373 * Parameter index for a load_param intrinsic
1374 */
1375 NIR_INTRINSIC_PARAM_IDX,
1376
1377 /**
1378 * Image dimensionality for image intrinsics
1379 *
1380 * One of GLSL_SAMPLER_DIM_*
1381 */
1382 NIR_INTRINSIC_IMAGE_DIM,
1383
1384 /**
1385 * Non-zero if we are accessing an array image
1386 */
1387 NIR_INTRINSIC_IMAGE_ARRAY,
1388
1389 /**
1390 * Image format for image intrinsics
1391 */
1392 NIR_INTRINSIC_FORMAT,
1393
1394 /**
1395 * Access qualifiers for image and memory access intrinsics
1396 */
1397 NIR_INTRINSIC_ACCESS,
1398
1399 /**
1400 * Alignment for offsets and addresses
1401 *
1402 * These two parameters, specify an alignment in terms of a multiplier and
1403 * an offset. The offset or address parameter X of the intrinsic is
1404 * guaranteed to satisfy the following:
1405 *
1406 * (X - align_offset) % align_mul == 0
1407 */
1408 NIR_INTRINSIC_ALIGN_MUL,
1409 NIR_INTRINSIC_ALIGN_OFFSET,
1410
1411 /**
1412 * The Vulkan descriptor type for a vulkan_resource_[re]index intrinsic.
1413 */
1414 NIR_INTRINSIC_DESC_TYPE,
1415
1416 /**
1417 * The nir_alu_type of a uniform/input/output
1418 */
1419 NIR_INTRINSIC_TYPE,
1420
1421 /**
1422 * The swizzle mask for the instructions
1423 * SwizzleInvocationsAMD and SwizzleInvocationsMaskedAMD
1424 */
1425 NIR_INTRINSIC_SWIZZLE_MASK,
1426
1427 /* Separate source/dest access flags for copies */
1428 NIR_INTRINSIC_SRC_ACCESS = 21,
1429 NIR_INTRINSIC_DST_ACCESS = 22,
1430
1431 NIR_INTRINSIC_NUM_INDEX_FLAGS,
1432
1433 } nir_intrinsic_index_flag;
1434
1435 #define NIR_INTRINSIC_MAX_INPUTS 5
1436
1437 typedef struct {
1438 const char *name;
1439
1440 unsigned num_srcs; /** < number of register/SSA inputs */
1441
1442 /** number of components of each input register
1443 *
1444 * If this value is 0, the number of components is given by the
1445 * num_components field of nir_intrinsic_instr. If this value is -1, the
1446 * intrinsic consumes however many components are provided and it is not
1447 * validated at all.
1448 */
1449 int src_components[NIR_INTRINSIC_MAX_INPUTS];
1450
1451 bool has_dest;
1452
1453 /** number of components of the output register
1454 *
1455 * If this value is 0, the number of components is given by the
1456 * num_components field of nir_intrinsic_instr.
1457 */
1458 unsigned dest_components;
1459
1460 /** bitfield of legal bit sizes */
1461 unsigned dest_bit_sizes;
1462
1463 /** the number of constant indices used by the intrinsic */
1464 unsigned num_indices;
1465
1466 /** indicates the usage of intr->const_index[n] */
1467 unsigned index_map[NIR_INTRINSIC_NUM_INDEX_FLAGS];
1468
1469 /** semantic flags for calls to this intrinsic */
1470 nir_intrinsic_semantic_flag flags;
1471 } nir_intrinsic_info;
1472
1473 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
1474
1475 static inline unsigned
1476 nir_intrinsic_src_components(nir_intrinsic_instr *intr, unsigned srcn)
1477 {
1478 const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
1479 assert(srcn < info->num_srcs);
1480 if (info->src_components[srcn] > 0)
1481 return info->src_components[srcn];
1482 else if (info->src_components[srcn] == 0)
1483 return intr->num_components;
1484 else
1485 return nir_src_num_components(intr->src[srcn]);
1486 }
1487
1488 static inline unsigned
1489 nir_intrinsic_dest_components(nir_intrinsic_instr *intr)
1490 {
1491 const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
1492 if (!info->has_dest)
1493 return 0;
1494 else if (info->dest_components)
1495 return info->dest_components;
1496 else
1497 return intr->num_components;
1498 }
1499
1500 #define INTRINSIC_IDX_ACCESSORS(name, flag, type) \
1501 static inline type \
1502 nir_intrinsic_##name(const nir_intrinsic_instr *instr) \
1503 { \
1504 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
1505 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
1506 return (type)instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1]; \
1507 } \
1508 static inline void \
1509 nir_intrinsic_set_##name(nir_intrinsic_instr *instr, type val) \
1510 { \
1511 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
1512 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
1513 instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1] = val; \
1514 }
1515
1516 INTRINSIC_IDX_ACCESSORS(write_mask, WRMASK, unsigned)
1517 INTRINSIC_IDX_ACCESSORS(base, BASE, int)
1518 INTRINSIC_IDX_ACCESSORS(stream_id, STREAM_ID, unsigned)
1519 INTRINSIC_IDX_ACCESSORS(ucp_id, UCP_ID, unsigned)
1520 INTRINSIC_IDX_ACCESSORS(range, RANGE, unsigned)
1521 INTRINSIC_IDX_ACCESSORS(desc_set, DESC_SET, unsigned)
1522 INTRINSIC_IDX_ACCESSORS(binding, BINDING, unsigned)
1523 INTRINSIC_IDX_ACCESSORS(component, COMPONENT, unsigned)
1524 INTRINSIC_IDX_ACCESSORS(interp_mode, INTERP_MODE, unsigned)
1525 INTRINSIC_IDX_ACCESSORS(reduction_op, REDUCTION_OP, unsigned)
1526 INTRINSIC_IDX_ACCESSORS(cluster_size, CLUSTER_SIZE, unsigned)
1527 INTRINSIC_IDX_ACCESSORS(param_idx, PARAM_IDX, unsigned)
1528 INTRINSIC_IDX_ACCESSORS(image_dim, IMAGE_DIM, enum glsl_sampler_dim)
1529 INTRINSIC_IDX_ACCESSORS(image_array, IMAGE_ARRAY, bool)
1530 INTRINSIC_IDX_ACCESSORS(access, ACCESS, enum gl_access_qualifier)
1531 INTRINSIC_IDX_ACCESSORS(src_access, SRC_ACCESS, enum gl_access_qualifier)
1532 INTRINSIC_IDX_ACCESSORS(dst_access, DST_ACCESS, enum gl_access_qualifier)
1533 INTRINSIC_IDX_ACCESSORS(format, FORMAT, unsigned)
1534 INTRINSIC_IDX_ACCESSORS(align_mul, ALIGN_MUL, unsigned)
1535 INTRINSIC_IDX_ACCESSORS(align_offset, ALIGN_OFFSET, unsigned)
1536 INTRINSIC_IDX_ACCESSORS(desc_type, DESC_TYPE, unsigned)
1537 INTRINSIC_IDX_ACCESSORS(type, TYPE, nir_alu_type)
1538 INTRINSIC_IDX_ACCESSORS(swizzle_mask, SWIZZLE_MASK, unsigned)
1539
1540 static inline void
1541 nir_intrinsic_set_align(nir_intrinsic_instr *intrin,
1542 unsigned align_mul, unsigned align_offset)
1543 {
1544 assert(util_is_power_of_two_nonzero(align_mul));
1545 assert(align_offset < align_mul);
1546 nir_intrinsic_set_align_mul(intrin, align_mul);
1547 nir_intrinsic_set_align_offset(intrin, align_offset);
1548 }
1549
1550 /** Returns a simple alignment for a load/store intrinsic offset
1551 *
1552 * Instead of the full mul+offset alignment scheme provided by the ALIGN_MUL
1553 * and ALIGN_OFFSET parameters, this helper takes both into account and
1554 * provides a single simple alignment parameter. The offset X is guaranteed
1555 * to satisfy X % align == 0.
1556 */
1557 static inline unsigned
1558 nir_intrinsic_align(const nir_intrinsic_instr *intrin)
1559 {
1560 const unsigned align_mul = nir_intrinsic_align_mul(intrin);
1561 const unsigned align_offset = nir_intrinsic_align_offset(intrin);
1562 assert(align_offset < align_mul);
1563 return align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
1564 }
1565
1566 /* Converts a image_deref_* intrinsic into a image_* one */
1567 void nir_rewrite_image_intrinsic(nir_intrinsic_instr *instr,
1568 nir_ssa_def *handle, bool bindless);
1569
1570 /* Determine if an intrinsic can be arbitrarily reordered and eliminated. */
1571 static inline bool
1572 nir_intrinsic_can_reorder(nir_intrinsic_instr *instr)
1573 {
1574 if (instr->intrinsic == nir_intrinsic_load_deref ||
1575 instr->intrinsic == nir_intrinsic_load_ssbo ||
1576 instr->intrinsic == nir_intrinsic_bindless_image_load ||
1577 instr->intrinsic == nir_intrinsic_image_deref_load ||
1578 instr->intrinsic == nir_intrinsic_image_load) {
1579 return nir_intrinsic_access(instr) & ACCESS_CAN_REORDER;
1580 } else {
1581 const nir_intrinsic_info *info =
1582 &nir_intrinsic_infos[instr->intrinsic];
1583 return (info->flags & NIR_INTRINSIC_CAN_ELIMINATE) &&
1584 (info->flags & NIR_INTRINSIC_CAN_REORDER);
1585 }
1586 }
1587
1588 /**
1589 * \group texture information
1590 *
1591 * This gives semantic information about textures which is useful to the
1592 * frontend, the backend, and lowering passes, but not the optimizer.
1593 */
1594
1595 typedef enum {
1596 nir_tex_src_coord,
1597 nir_tex_src_projector,
1598 nir_tex_src_comparator, /* shadow comparator */
1599 nir_tex_src_offset,
1600 nir_tex_src_bias,
1601 nir_tex_src_lod,
1602 nir_tex_src_min_lod,
1603 nir_tex_src_ms_index, /* MSAA sample index */
1604 nir_tex_src_ms_mcs, /* MSAA compression value */
1605 nir_tex_src_ddx,
1606 nir_tex_src_ddy,
1607 nir_tex_src_texture_deref, /* < deref pointing to the texture */
1608 nir_tex_src_sampler_deref, /* < deref pointing to the sampler */
1609 nir_tex_src_texture_offset, /* < dynamically uniform indirect offset */
1610 nir_tex_src_sampler_offset, /* < dynamically uniform indirect offset */
1611 nir_tex_src_texture_handle, /* < bindless texture handle */
1612 nir_tex_src_sampler_handle, /* < bindless sampler handle */
1613 nir_tex_src_plane, /* < selects plane for planar textures */
1614 nir_num_tex_src_types
1615 } nir_tex_src_type;
1616
1617 typedef struct {
1618 nir_src src;
1619 nir_tex_src_type src_type;
1620 } nir_tex_src;
1621
1622 typedef enum {
1623 nir_texop_tex, /**< Regular texture look-up */
1624 nir_texop_txb, /**< Texture look-up with LOD bias */
1625 nir_texop_txl, /**< Texture look-up with explicit LOD */
1626 nir_texop_txd, /**< Texture look-up with partial derivatives */
1627 nir_texop_txf, /**< Texel fetch with explicit LOD */
1628 nir_texop_txf_ms, /**< Multisample texture fetch */
1629 nir_texop_txf_ms_fb, /**< Multisample texture fetch from framebuffer */
1630 nir_texop_txf_ms_mcs, /**< Multisample compression value fetch */
1631 nir_texop_txs, /**< Texture size */
1632 nir_texop_lod, /**< Texture lod query */
1633 nir_texop_tg4, /**< Texture gather */
1634 nir_texop_query_levels, /**< Texture levels query */
1635 nir_texop_texture_samples, /**< Texture samples query */
1636 nir_texop_samples_identical, /**< Query whether all samples are definitely
1637 * identical.
1638 */
1639 } nir_texop;
1640
1641 typedef struct {
1642 nir_instr instr;
1643
1644 enum glsl_sampler_dim sampler_dim;
1645 nir_alu_type dest_type;
1646
1647 nir_texop op;
1648 nir_dest dest;
1649 nir_tex_src *src;
1650 unsigned num_srcs, coord_components;
1651 bool is_array, is_shadow;
1652
1653 /**
1654 * If is_shadow is true, whether this is the old-style shadow that outputs 4
1655 * components or the new-style shadow that outputs 1 component.
1656 */
1657 bool is_new_style_shadow;
1658
1659 /* gather component selector */
1660 unsigned component : 2;
1661
1662 /* gather offsets */
1663 int8_t tg4_offsets[4][2];
1664
1665 /* True if the texture index or handle is not dynamically uniform */
1666 bool texture_non_uniform;
1667
1668 /* True if the sampler index or handle is not dynamically uniform */
1669 bool sampler_non_uniform;
1670
1671 /** The texture index
1672 *
1673 * If this texture instruction has a nir_tex_src_texture_offset source,
1674 * then the texture index is given by texture_index + texture_offset.
1675 */
1676 unsigned texture_index;
1677
1678 /** The size of the texture array or 0 if it's not an array */
1679 unsigned texture_array_size;
1680
1681 /** The sampler index
1682 *
1683 * The following operations do not require a sampler and, as such, this
1684 * field should be ignored:
1685 * - nir_texop_txf
1686 * - nir_texop_txf_ms
1687 * - nir_texop_txs
1688 * - nir_texop_lod
1689 * - nir_texop_query_levels
1690 * - nir_texop_texture_samples
1691 * - nir_texop_samples_identical
1692 *
1693 * If this texture instruction has a nir_tex_src_sampler_offset source,
1694 * then the sampler index is given by sampler_index + sampler_offset.
1695 */
1696 unsigned sampler_index;
1697 } nir_tex_instr;
1698
1699 static inline unsigned
1700 nir_tex_instr_dest_size(const nir_tex_instr *instr)
1701 {
1702 switch (instr->op) {
1703 case nir_texop_txs: {
1704 unsigned ret;
1705 switch (instr->sampler_dim) {
1706 case GLSL_SAMPLER_DIM_1D:
1707 case GLSL_SAMPLER_DIM_BUF:
1708 ret = 1;
1709 break;
1710 case GLSL_SAMPLER_DIM_2D:
1711 case GLSL_SAMPLER_DIM_CUBE:
1712 case GLSL_SAMPLER_DIM_MS:
1713 case GLSL_SAMPLER_DIM_RECT:
1714 case GLSL_SAMPLER_DIM_EXTERNAL:
1715 case GLSL_SAMPLER_DIM_SUBPASS:
1716 ret = 2;
1717 break;
1718 case GLSL_SAMPLER_DIM_3D:
1719 ret = 3;
1720 break;
1721 default:
1722 unreachable("not reached");
1723 }
1724 if (instr->is_array)
1725 ret++;
1726 return ret;
1727 }
1728
1729 case nir_texop_lod:
1730 return 2;
1731
1732 case nir_texop_texture_samples:
1733 case nir_texop_query_levels:
1734 case nir_texop_samples_identical:
1735 return 1;
1736
1737 default:
1738 if (instr->is_shadow && instr->is_new_style_shadow)
1739 return 1;
1740
1741 return 4;
1742 }
1743 }
1744
1745 /* Returns true if this texture operation queries something about the texture
1746 * rather than actually sampling it.
1747 */
1748 static inline bool
1749 nir_tex_instr_is_query(const nir_tex_instr *instr)
1750 {
1751 switch (instr->op) {
1752 case nir_texop_txs:
1753 case nir_texop_lod:
1754 case nir_texop_texture_samples:
1755 case nir_texop_query_levels:
1756 case nir_texop_txf_ms_mcs:
1757 return true;
1758 case nir_texop_tex:
1759 case nir_texop_txb:
1760 case nir_texop_txl:
1761 case nir_texop_txd:
1762 case nir_texop_txf:
1763 case nir_texop_txf_ms:
1764 case nir_texop_txf_ms_fb:
1765 case nir_texop_tg4:
1766 return false;
1767 default:
1768 unreachable("Invalid texture opcode");
1769 }
1770 }
1771
1772 static inline bool
1773 nir_alu_instr_is_comparison(const nir_alu_instr *instr)
1774 {
1775 switch (instr->op) {
1776 case nir_op_flt:
1777 case nir_op_fge:
1778 case nir_op_feq:
1779 case nir_op_fne:
1780 case nir_op_ilt:
1781 case nir_op_ult:
1782 case nir_op_ige:
1783 case nir_op_uge:
1784 case nir_op_ieq:
1785 case nir_op_ine:
1786 case nir_op_i2b1:
1787 case nir_op_f2b1:
1788 case nir_op_inot:
1789 return true;
1790 default:
1791 return false;
1792 }
1793 }
1794
1795 static inline nir_alu_type
1796 nir_tex_instr_src_type(const nir_tex_instr *instr, unsigned src)
1797 {
1798 switch (instr->src[src].src_type) {
1799 case nir_tex_src_coord:
1800 switch (instr->op) {
1801 case nir_texop_txf:
1802 case nir_texop_txf_ms:
1803 case nir_texop_txf_ms_fb:
1804 case nir_texop_txf_ms_mcs:
1805 case nir_texop_samples_identical:
1806 return nir_type_int;
1807
1808 default:
1809 return nir_type_float;
1810 }
1811
1812 case nir_tex_src_lod:
1813 switch (instr->op) {
1814 case nir_texop_txs:
1815 case nir_texop_txf:
1816 return nir_type_int;
1817
1818 default:
1819 return nir_type_float;
1820 }
1821
1822 case nir_tex_src_projector:
1823 case nir_tex_src_comparator:
1824 case nir_tex_src_bias:
1825 case nir_tex_src_ddx:
1826 case nir_tex_src_ddy:
1827 return nir_type_float;
1828
1829 case nir_tex_src_offset:
1830 case nir_tex_src_ms_index:
1831 case nir_tex_src_texture_offset:
1832 case nir_tex_src_sampler_offset:
1833 return nir_type_int;
1834
1835 default:
1836 unreachable("Invalid texture source type");
1837 }
1838 }
1839
1840 static inline unsigned
1841 nir_tex_instr_src_size(const nir_tex_instr *instr, unsigned src)
1842 {
1843 if (instr->src[src].src_type == nir_tex_src_coord)
1844 return instr->coord_components;
1845
1846 /* The MCS value is expected to be a vec4 returned by a txf_ms_mcs */
1847 if (instr->src[src].src_type == nir_tex_src_ms_mcs)
1848 return 4;
1849
1850 if (instr->src[src].src_type == nir_tex_src_ddx ||
1851 instr->src[src].src_type == nir_tex_src_ddy) {
1852 if (instr->is_array)
1853 return instr->coord_components - 1;
1854 else
1855 return instr->coord_components;
1856 }
1857
1858 /* Usual APIs don't allow cube + offset, but we allow it, with 2 coords for
1859 * the offset, since a cube maps to a single face.
1860 */
1861 if (instr->src[src].src_type == nir_tex_src_offset) {
1862 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE)
1863 return 2;
1864 else if (instr->is_array)
1865 return instr->coord_components - 1;
1866 else
1867 return instr->coord_components;
1868 }
1869
1870 return 1;
1871 }
1872
1873 static inline int
1874 nir_tex_instr_src_index(const nir_tex_instr *instr, nir_tex_src_type type)
1875 {
1876 for (unsigned i = 0; i < instr->num_srcs; i++)
1877 if (instr->src[i].src_type == type)
1878 return (int) i;
1879
1880 return -1;
1881 }
1882
1883 void nir_tex_instr_add_src(nir_tex_instr *tex,
1884 nir_tex_src_type src_type,
1885 nir_src src);
1886
1887 void nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx);
1888
1889 bool nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr *tex);
1890
1891 typedef struct {
1892 nir_instr instr;
1893
1894 nir_ssa_def def;
1895
1896 nir_const_value value[];
1897 } nir_load_const_instr;
1898
1899 #define nir_const_load_to_arr(arr, l, m) \
1900 { \
1901 nir_const_value_to_array(arr, l->value, l->def.num_components, m); \
1902 } while (false);
1903
1904 typedef enum {
1905 nir_jump_return,
1906 nir_jump_break,
1907 nir_jump_continue,
1908 } nir_jump_type;
1909
1910 typedef struct {
1911 nir_instr instr;
1912 nir_jump_type type;
1913 } nir_jump_instr;
1914
1915 /* creates a new SSA variable in an undefined state */
1916
1917 typedef struct {
1918 nir_instr instr;
1919 nir_ssa_def def;
1920 } nir_ssa_undef_instr;
1921
1922 typedef struct {
1923 struct exec_node node;
1924
1925 /* The predecessor block corresponding to this source */
1926 struct nir_block *pred;
1927
1928 nir_src src;
1929 } nir_phi_src;
1930
1931 #define nir_foreach_phi_src(phi_src, phi) \
1932 foreach_list_typed(nir_phi_src, phi_src, node, &(phi)->srcs)
1933 #define nir_foreach_phi_src_safe(phi_src, phi) \
1934 foreach_list_typed_safe(nir_phi_src, phi_src, node, &(phi)->srcs)
1935
1936 typedef struct {
1937 nir_instr instr;
1938
1939 struct exec_list srcs; /** < list of nir_phi_src */
1940
1941 nir_dest dest;
1942 } nir_phi_instr;
1943
1944 typedef struct {
1945 struct exec_node node;
1946 nir_src src;
1947 nir_dest dest;
1948 } nir_parallel_copy_entry;
1949
1950 #define nir_foreach_parallel_copy_entry(entry, pcopy) \
1951 foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
1952
1953 typedef struct {
1954 nir_instr instr;
1955
1956 /* A list of nir_parallel_copy_entrys. The sources of all of the
1957 * entries are copied to the corresponding destinations "in parallel".
1958 * In other words, if we have two entries: a -> b and b -> a, the values
1959 * get swapped.
1960 */
1961 struct exec_list entries;
1962 } nir_parallel_copy_instr;
1963
1964 NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr,
1965 type, nir_instr_type_alu)
1966 NIR_DEFINE_CAST(nir_instr_as_deref, nir_instr, nir_deref_instr, instr,
1967 type, nir_instr_type_deref)
1968 NIR_DEFINE_CAST(nir_instr_as_call, nir_instr, nir_call_instr, instr,
1969 type, nir_instr_type_call)
1970 NIR_DEFINE_CAST(nir_instr_as_jump, nir_instr, nir_jump_instr, instr,
1971 type, nir_instr_type_jump)
1972 NIR_DEFINE_CAST(nir_instr_as_tex, nir_instr, nir_tex_instr, instr,
1973 type, nir_instr_type_tex)
1974 NIR_DEFINE_CAST(nir_instr_as_intrinsic, nir_instr, nir_intrinsic_instr, instr,
1975 type, nir_instr_type_intrinsic)
1976 NIR_DEFINE_CAST(nir_instr_as_load_const, nir_instr, nir_load_const_instr, instr,
1977 type, nir_instr_type_load_const)
1978 NIR_DEFINE_CAST(nir_instr_as_ssa_undef, nir_instr, nir_ssa_undef_instr, instr,
1979 type, nir_instr_type_ssa_undef)
1980 NIR_DEFINE_CAST(nir_instr_as_phi, nir_instr, nir_phi_instr, instr,
1981 type, nir_instr_type_phi)
1982 NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
1983 nir_parallel_copy_instr, instr,
1984 type, nir_instr_type_parallel_copy)
1985
1986 /*
1987 * Control flow
1988 *
1989 * Control flow consists of a tree of control flow nodes, which include
1990 * if-statements and loops. The leaves of the tree are basic blocks, lists of
1991 * instructions that always run start-to-finish. Each basic block also keeps
1992 * track of its successors (blocks which may run immediately after the current
1993 * block) and predecessors (blocks which could have run immediately before the
1994 * current block). Each function also has a start block and an end block which
1995 * all return statements point to (which is always empty). Together, all the
1996 * blocks with their predecessors and successors make up the control flow
1997 * graph (CFG) of the function. There are helpers that modify the tree of
1998 * control flow nodes while modifying the CFG appropriately; these should be
1999 * used instead of modifying the tree directly.
2000 */
2001
2002 typedef enum {
2003 nir_cf_node_block,
2004 nir_cf_node_if,
2005 nir_cf_node_loop,
2006 nir_cf_node_function
2007 } nir_cf_node_type;
2008
2009 typedef struct nir_cf_node {
2010 struct exec_node node;
2011 nir_cf_node_type type;
2012 struct nir_cf_node *parent;
2013 } nir_cf_node;
2014
2015 typedef struct nir_block {
2016 nir_cf_node cf_node;
2017
2018 struct exec_list instr_list; /** < list of nir_instr */
2019
2020 /** generic block index; generated by nir_index_blocks */
2021 unsigned index;
2022
2023 /*
2024 * Each block can only have up to 2 successors, so we put them in a simple
2025 * array - no need for anything more complicated.
2026 */
2027 struct nir_block *successors[2];
2028
2029 /* Set of nir_block predecessors in the CFG */
2030 struct set *predecessors;
2031
2032 /*
2033 * this node's immediate dominator in the dominance tree - set to NULL for
2034 * the start block.
2035 */
2036 struct nir_block *imm_dom;
2037
2038 /* This node's children in the dominance tree */
2039 unsigned num_dom_children;
2040 struct nir_block **dom_children;
2041
2042 /* Set of nir_blocks on the dominance frontier of this block */
2043 struct set *dom_frontier;
2044
2045 /*
2046 * These two indices have the property that dom_{pre,post}_index for each
2047 * child of this block in the dominance tree will always be between
2048 * dom_pre_index and dom_post_index for this block, which makes testing if
2049 * a given block is dominated by another block an O(1) operation.
2050 */
2051 unsigned dom_pre_index, dom_post_index;
2052
2053 /* live in and out for this block; used for liveness analysis */
2054 BITSET_WORD *live_in;
2055 BITSET_WORD *live_out;
2056 } nir_block;
2057
2058 static inline nir_instr *
2059 nir_block_first_instr(nir_block *block)
2060 {
2061 struct exec_node *head = exec_list_get_head(&block->instr_list);
2062 return exec_node_data(nir_instr, head, node);
2063 }
2064
2065 static inline nir_instr *
2066 nir_block_last_instr(nir_block *block)
2067 {
2068 struct exec_node *tail = exec_list_get_tail(&block->instr_list);
2069 return exec_node_data(nir_instr, tail, node);
2070 }
2071
2072 static inline bool
2073 nir_block_ends_in_jump(nir_block *block)
2074 {
2075 return !exec_list_is_empty(&block->instr_list) &&
2076 nir_block_last_instr(block)->type == nir_instr_type_jump;
2077 }
2078
2079 #define nir_foreach_instr(instr, block) \
2080 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
2081 #define nir_foreach_instr_reverse(instr, block) \
2082 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
2083 #define nir_foreach_instr_safe(instr, block) \
2084 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
2085 #define nir_foreach_instr_reverse_safe(instr, block) \
2086 foreach_list_typed_reverse_safe(nir_instr, instr, node, &(block)->instr_list)
2087
2088 typedef enum {
2089 nir_selection_control_none = 0x0,
2090 nir_selection_control_flatten = 0x1,
2091 nir_selection_control_dont_flatten = 0x2,
2092 } nir_selection_control;
2093
2094 typedef struct nir_if {
2095 nir_cf_node cf_node;
2096 nir_src condition;
2097 nir_selection_control control;
2098
2099 struct exec_list then_list; /** < list of nir_cf_node */
2100 struct exec_list else_list; /** < list of nir_cf_node */
2101 } nir_if;
2102
2103 typedef struct {
2104 nir_if *nif;
2105
2106 /** Instruction that generates nif::condition. */
2107 nir_instr *conditional_instr;
2108
2109 /** Block within ::nif that has the break instruction. */
2110 nir_block *break_block;
2111
2112 /** Last block for the then- or else-path that does not contain the break. */
2113 nir_block *continue_from_block;
2114
2115 /** True when ::break_block is in the else-path of ::nif. */
2116 bool continue_from_then;
2117 bool induction_rhs;
2118
2119 /* This is true if the terminators exact trip count is unknown. For
2120 * example:
2121 *
2122 * for (int i = 0; i < imin(x, 4); i++)
2123 * ...
2124 *
2125 * Here loop analysis would have set a max_trip_count of 4 however we dont
2126 * know for sure that this is the exact trip count.
2127 */
2128 bool exact_trip_count_unknown;
2129
2130 struct list_head loop_terminator_link;
2131 } nir_loop_terminator;
2132
2133 typedef struct {
2134 /* Estimated cost (in number of instructions) of the loop */
2135 unsigned instr_cost;
2136
2137 /* Guessed trip count based on array indexing */
2138 unsigned guessed_trip_count;
2139
2140 /* Maximum number of times the loop is run (if known) */
2141 unsigned max_trip_count;
2142
2143 /* Do we know the exact number of times the loop will be run */
2144 bool exact_trip_count_known;
2145
2146 /* Unroll the loop regardless of its size */
2147 bool force_unroll;
2148
2149 /* Does the loop contain complex loop terminators, continues or other
2150 * complex behaviours? If this is true we can't rely on
2151 * loop_terminator_list to be complete or accurate.
2152 */
2153 bool complex_loop;
2154
2155 nir_loop_terminator *limiting_terminator;
2156
2157 /* A list of loop_terminators terminating this loop. */
2158 struct list_head loop_terminator_list;
2159 } nir_loop_info;
2160
2161 typedef enum {
2162 nir_loop_control_none = 0x0,
2163 nir_loop_control_unroll = 0x1,
2164 nir_loop_control_dont_unroll = 0x2,
2165 } nir_loop_control;
2166
2167 typedef struct {
2168 nir_cf_node cf_node;
2169
2170 struct exec_list body; /** < list of nir_cf_node */
2171
2172 nir_loop_info *info;
2173 nir_loop_control control;
2174 bool partially_unrolled;
2175 } nir_loop;
2176
2177 /**
2178 * Various bits of metadata that can may be created or required by
2179 * optimization and analysis passes
2180 */
2181 typedef enum {
2182 nir_metadata_none = 0x0,
2183 nir_metadata_block_index = 0x1,
2184 nir_metadata_dominance = 0x2,
2185 nir_metadata_live_ssa_defs = 0x4,
2186 nir_metadata_not_properly_reset = 0x8,
2187 nir_metadata_loop_analysis = 0x10,
2188 } nir_metadata;
2189
2190 typedef struct {
2191 nir_cf_node cf_node;
2192
2193 /** pointer to the function of which this is an implementation */
2194 struct nir_function *function;
2195
2196 struct exec_list body; /** < list of nir_cf_node */
2197
2198 nir_block *end_block;
2199
2200 /** list for all local variables in the function */
2201 struct exec_list locals;
2202
2203 /** list of local registers in the function */
2204 struct exec_list registers;
2205
2206 /** next available local register index */
2207 unsigned reg_alloc;
2208
2209 /** next available SSA value index */
2210 unsigned ssa_alloc;
2211
2212 /* total number of basic blocks, only valid when block_index_dirty = false */
2213 unsigned num_blocks;
2214
2215 nir_metadata valid_metadata;
2216 } nir_function_impl;
2217
2218 ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
2219 nir_start_block(nir_function_impl *impl)
2220 {
2221 return (nir_block *) impl->body.head_sentinel.next;
2222 }
2223
2224 ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
2225 nir_impl_last_block(nir_function_impl *impl)
2226 {
2227 return (nir_block *) impl->body.tail_sentinel.prev;
2228 }
2229
2230 static inline nir_cf_node *
2231 nir_cf_node_next(nir_cf_node *node)
2232 {
2233 struct exec_node *next = exec_node_get_next(&node->node);
2234 if (exec_node_is_tail_sentinel(next))
2235 return NULL;
2236 else
2237 return exec_node_data(nir_cf_node, next, node);
2238 }
2239
2240 static inline nir_cf_node *
2241 nir_cf_node_prev(nir_cf_node *node)
2242 {
2243 struct exec_node *prev = exec_node_get_prev(&node->node);
2244 if (exec_node_is_head_sentinel(prev))
2245 return NULL;
2246 else
2247 return exec_node_data(nir_cf_node, prev, node);
2248 }
2249
2250 static inline bool
2251 nir_cf_node_is_first(const nir_cf_node *node)
2252 {
2253 return exec_node_is_head_sentinel(node->node.prev);
2254 }
2255
2256 static inline bool
2257 nir_cf_node_is_last(const nir_cf_node *node)
2258 {
2259 return exec_node_is_tail_sentinel(node->node.next);
2260 }
2261
2262 NIR_DEFINE_CAST(nir_cf_node_as_block, nir_cf_node, nir_block, cf_node,
2263 type, nir_cf_node_block)
2264 NIR_DEFINE_CAST(nir_cf_node_as_if, nir_cf_node, nir_if, cf_node,
2265 type, nir_cf_node_if)
2266 NIR_DEFINE_CAST(nir_cf_node_as_loop, nir_cf_node, nir_loop, cf_node,
2267 type, nir_cf_node_loop)
2268 NIR_DEFINE_CAST(nir_cf_node_as_function, nir_cf_node,
2269 nir_function_impl, cf_node, type, nir_cf_node_function)
2270
2271 static inline nir_block *
2272 nir_if_first_then_block(nir_if *if_stmt)
2273 {
2274 struct exec_node *head = exec_list_get_head(&if_stmt->then_list);
2275 return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
2276 }
2277
2278 static inline nir_block *
2279 nir_if_last_then_block(nir_if *if_stmt)
2280 {
2281 struct exec_node *tail = exec_list_get_tail(&if_stmt->then_list);
2282 return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
2283 }
2284
2285 static inline nir_block *
2286 nir_if_first_else_block(nir_if *if_stmt)
2287 {
2288 struct exec_node *head = exec_list_get_head(&if_stmt->else_list);
2289 return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
2290 }
2291
2292 static inline nir_block *
2293 nir_if_last_else_block(nir_if *if_stmt)
2294 {
2295 struct exec_node *tail = exec_list_get_tail(&if_stmt->else_list);
2296 return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
2297 }
2298
2299 static inline nir_block *
2300 nir_loop_first_block(nir_loop *loop)
2301 {
2302 struct exec_node *head = exec_list_get_head(&loop->body);
2303 return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
2304 }
2305
2306 static inline nir_block *
2307 nir_loop_last_block(nir_loop *loop)
2308 {
2309 struct exec_node *tail = exec_list_get_tail(&loop->body);
2310 return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
2311 }
2312
2313 /**
2314 * Return true if this list of cf_nodes contains a single empty block.
2315 */
2316 static inline bool
2317 nir_cf_list_is_empty_block(struct exec_list *cf_list)
2318 {
2319 if (exec_list_is_singular(cf_list)) {
2320 struct exec_node *head = exec_list_get_head(cf_list);
2321 nir_block *block =
2322 nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
2323 return exec_list_is_empty(&block->instr_list);
2324 }
2325 return false;
2326 }
2327
2328 typedef struct {
2329 uint8_t num_components;
2330 uint8_t bit_size;
2331 } nir_parameter;
2332
2333 typedef struct nir_function {
2334 struct exec_node node;
2335
2336 const char *name;
2337 struct nir_shader *shader;
2338
2339 unsigned num_params;
2340 nir_parameter *params;
2341
2342 /** The implementation of this function.
2343 *
2344 * If the function is only declared and not implemented, this is NULL.
2345 */
2346 nir_function_impl *impl;
2347
2348 bool is_entrypoint;
2349 } nir_function;
2350
2351 typedef enum {
2352 nir_lower_imul64 = (1 << 0),
2353 nir_lower_isign64 = (1 << 1),
2354 /** Lower all int64 modulus and division opcodes */
2355 nir_lower_divmod64 = (1 << 2),
2356 /** Lower all 64-bit umul_high and imul_high opcodes */
2357 nir_lower_imul_high64 = (1 << 3),
2358 nir_lower_mov64 = (1 << 4),
2359 nir_lower_icmp64 = (1 << 5),
2360 nir_lower_iadd64 = (1 << 6),
2361 nir_lower_iabs64 = (1 << 7),
2362 nir_lower_ineg64 = (1 << 8),
2363 nir_lower_logic64 = (1 << 9),
2364 nir_lower_minmax64 = (1 << 10),
2365 nir_lower_shift64 = (1 << 11),
2366 nir_lower_imul_2x32_64 = (1 << 12),
2367 } nir_lower_int64_options;
2368
2369 typedef enum {
2370 nir_lower_drcp = (1 << 0),
2371 nir_lower_dsqrt = (1 << 1),
2372 nir_lower_drsq = (1 << 2),
2373 nir_lower_dtrunc = (1 << 3),
2374 nir_lower_dfloor = (1 << 4),
2375 nir_lower_dceil = (1 << 5),
2376 nir_lower_dfract = (1 << 6),
2377 nir_lower_dround_even = (1 << 7),
2378 nir_lower_dmod = (1 << 8),
2379 nir_lower_fp64_full_software = (1 << 9),
2380 } nir_lower_doubles_options;
2381
2382 typedef struct nir_shader_compiler_options {
2383 bool lower_fdiv;
2384 bool lower_ffma;
2385 bool fuse_ffma;
2386 bool lower_flrp16;
2387 bool lower_flrp32;
2388 /** Lowers flrp when it does not support doubles */
2389 bool lower_flrp64;
2390 bool lower_fpow;
2391 bool lower_fsat;
2392 bool lower_fsqrt;
2393 bool lower_fmod;
2394 /** Lowers ibitfield_extract/ubitfield_extract to ibfe/ubfe. */
2395 bool lower_bitfield_extract;
2396 /** Lowers ibitfield_extract/ubitfield_extract to compares, shifts. */
2397 bool lower_bitfield_extract_to_shifts;
2398 /** Lowers bitfield_insert to bfi/bfm */
2399 bool lower_bitfield_insert;
2400 /** Lowers bitfield_insert to compares, and shifts. */
2401 bool lower_bitfield_insert_to_shifts;
2402 /** Lowers bitfield_insert to bfm/bitfield_select. */
2403 bool lower_bitfield_insert_to_bitfield_select;
2404 /** Lowers bitfield_reverse to shifts. */
2405 bool lower_bitfield_reverse;
2406 /** Lowers bit_count to shifts. */
2407 bool lower_bit_count;
2408 /** Lowers ifind_msb to compare and ufind_msb */
2409 bool lower_ifind_msb;
2410 /** Lowers find_lsb to ufind_msb and logic ops */
2411 bool lower_find_lsb;
2412 bool lower_uadd_carry;
2413 bool lower_usub_borrow;
2414 /** Lowers imul_high/umul_high to 16-bit multiplies and carry operations. */
2415 bool lower_mul_high;
2416 /** lowers fneg and ineg to fsub and isub. */
2417 bool lower_negate;
2418 /** lowers fsub and isub to fadd+fneg and iadd+ineg. */
2419 bool lower_sub;
2420
2421 /* lower {slt,sge,seq,sne} to {flt,fge,feq,fne} + b2f: */
2422 bool lower_scmp;
2423
2424 /** enables rules to lower idiv by power-of-two: */
2425 bool lower_idiv;
2426
2427 /** enable rules to avoid bit shifts */
2428 bool lower_bitshift;
2429
2430 /** enables rules to lower isign to imin+imax */
2431 bool lower_isign;
2432
2433 /** enables rules to lower fsign to fsub and flt */
2434 bool lower_fsign;
2435
2436 /* Does the native fdot instruction replicate its result for four
2437 * components? If so, then opt_algebraic_late will turn all fdotN
2438 * instructions into fdot_replicatedN instructions.
2439 */
2440 bool fdot_replicates;
2441
2442 /** lowers ffloor to fsub+ffract: */
2443 bool lower_ffloor;
2444
2445 /** lowers ffract to fsub+ffloor: */
2446 bool lower_ffract;
2447
2448 /** lowers fceil to fneg+ffloor+fneg: */
2449 bool lower_fceil;
2450
2451 bool lower_ftrunc;
2452
2453 bool lower_ldexp;
2454
2455 bool lower_pack_half_2x16;
2456 bool lower_pack_unorm_2x16;
2457 bool lower_pack_snorm_2x16;
2458 bool lower_pack_unorm_4x8;
2459 bool lower_pack_snorm_4x8;
2460 bool lower_unpack_half_2x16;
2461 bool lower_unpack_unorm_2x16;
2462 bool lower_unpack_snorm_2x16;
2463 bool lower_unpack_unorm_4x8;
2464 bool lower_unpack_snorm_4x8;
2465
2466 bool lower_extract_byte;
2467 bool lower_extract_word;
2468
2469 bool lower_all_io_to_temps;
2470 bool lower_all_io_to_elements;
2471
2472 /* Indicates that the driver only has zero-based vertex id */
2473 bool vertex_id_zero_based;
2474
2475 /**
2476 * If enabled, gl_BaseVertex will be lowered as:
2477 * is_indexed_draw (~0/0) & firstvertex
2478 */
2479 bool lower_base_vertex;
2480
2481 /**
2482 * If enabled, gl_HelperInvocation will be lowered as:
2483 *
2484 * !((1 << sample_id) & sample_mask_in))
2485 *
2486 * This depends on some possibly hw implementation details, which may
2487 * not be true for all hw. In particular that the FS is only executed
2488 * for covered samples or for helper invocations. So, do not blindly
2489 * enable this option.
2490 *
2491 * Note: See also issue #22 in ARB_shader_image_load_store
2492 */
2493 bool lower_helper_invocation;
2494
2495 /**
2496 * Convert gl_SampleMaskIn to gl_HelperInvocation as follows:
2497 *
2498 * gl_SampleMaskIn == 0 ---> gl_HelperInvocation
2499 * gl_SampleMaskIn != 0 ---> !gl_HelperInvocation
2500 */
2501 bool optimize_sample_mask_in;
2502
2503 bool lower_cs_local_index_from_id;
2504 bool lower_cs_local_id_from_index;
2505
2506 bool lower_device_index_to_zero;
2507
2508 /* Set if nir_lower_wpos_ytransform() should also invert gl_PointCoord. */
2509 bool lower_wpos_pntc;
2510
2511 bool lower_hadd;
2512 bool lower_add_sat;
2513
2514 /**
2515 * Should IO be re-vectorized? Some scalar ISAs still operate on vec4's
2516 * for IO purposes and would prefer loads/stores be vectorized.
2517 */
2518 bool vectorize_io;
2519
2520 /**
2521 * Should nir_lower_io() create load_interpolated_input intrinsics?
2522 *
2523 * If not, it generates regular load_input intrinsics and interpolation
2524 * information must be inferred from the list of input nir_variables.
2525 */
2526 bool use_interpolated_input_intrinsics;
2527
2528 /* Lowers when 32x32->64 bit multiplication is not supported */
2529 bool lower_mul_2x32_64;
2530
2531 /* Lowers when rotate instruction is not supported */
2532 bool lower_rotate;
2533
2534 unsigned max_unroll_iterations;
2535
2536 nir_lower_int64_options lower_int64_options;
2537 nir_lower_doubles_options lower_doubles_options;
2538 } nir_shader_compiler_options;
2539
2540 typedef struct nir_shader {
2541 /** list of uniforms (nir_variable) */
2542 struct exec_list uniforms;
2543
2544 /** list of inputs (nir_variable) */
2545 struct exec_list inputs;
2546
2547 /** list of outputs (nir_variable) */
2548 struct exec_list outputs;
2549
2550 /** list of shared compute variables (nir_variable) */
2551 struct exec_list shared;
2552
2553 /** Set of driver-specific options for the shader.
2554 *
2555 * The memory for the options is expected to be kept in a single static
2556 * copy by the driver.
2557 */
2558 const struct nir_shader_compiler_options *options;
2559
2560 /** Various bits of compile-time information about a given shader */
2561 struct shader_info info;
2562
2563 /** list of global variables in the shader (nir_variable) */
2564 struct exec_list globals;
2565
2566 /** list of system value variables in the shader (nir_variable) */
2567 struct exec_list system_values;
2568
2569 struct exec_list functions; /** < list of nir_function */
2570
2571 /**
2572 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
2573 * access plus one
2574 */
2575 unsigned num_inputs, num_uniforms, num_outputs, num_shared;
2576
2577 /** Size in bytes of required scratch space */
2578 unsigned scratch_size;
2579
2580 /** Constant data associated with this shader.
2581 *
2582 * Constant data is loaded through load_constant intrinsics. See also
2583 * nir_opt_large_constants.
2584 */
2585 void *constant_data;
2586 unsigned constant_data_size;
2587 } nir_shader;
2588
2589 #define nir_foreach_function(func, shader) \
2590 foreach_list_typed(nir_function, func, node, &(shader)->functions)
2591
2592 static inline nir_function_impl *
2593 nir_shader_get_entrypoint(nir_shader *shader)
2594 {
2595 nir_function *func = NULL;
2596
2597 nir_foreach_function(function, shader) {
2598 assert(func == NULL);
2599 if (function->is_entrypoint) {
2600 func = function;
2601 #ifndef NDEBUG
2602 break;
2603 #endif
2604 }
2605 }
2606
2607 if (!func)
2608 return NULL;
2609
2610 assert(func->num_params == 0);
2611 assert(func->impl);
2612 return func->impl;
2613 }
2614
2615 nir_shader *nir_shader_create(void *mem_ctx,
2616 gl_shader_stage stage,
2617 const nir_shader_compiler_options *options,
2618 shader_info *si);
2619
2620 nir_register *nir_local_reg_create(nir_function_impl *impl);
2621
2622 void nir_reg_remove(nir_register *reg);
2623
2624 /** Adds a variable to the appropriate list in nir_shader */
2625 void nir_shader_add_variable(nir_shader *shader, nir_variable *var);
2626
2627 static inline void
2628 nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)
2629 {
2630 assert(var->data.mode == nir_var_function_temp);
2631 exec_list_push_tail(&impl->locals, &var->node);
2632 }
2633
2634 /** creates a variable, sets a few defaults, and adds it to the list */
2635 nir_variable *nir_variable_create(nir_shader *shader,
2636 nir_variable_mode mode,
2637 const struct glsl_type *type,
2638 const char *name);
2639 /** creates a local variable and adds it to the list */
2640 nir_variable *nir_local_variable_create(nir_function_impl *impl,
2641 const struct glsl_type *type,
2642 const char *name);
2643
2644 /** creates a function and adds it to the shader's list of functions */
2645 nir_function *nir_function_create(nir_shader *shader, const char *name);
2646
2647 nir_function_impl *nir_function_impl_create(nir_function *func);
2648 /** creates a function_impl that isn't tied to any particular function */
2649 nir_function_impl *nir_function_impl_create_bare(nir_shader *shader);
2650
2651 nir_block *nir_block_create(nir_shader *shader);
2652 nir_if *nir_if_create(nir_shader *shader);
2653 nir_loop *nir_loop_create(nir_shader *shader);
2654
2655 nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
2656
2657 /** requests that the given pieces of metadata be generated */
2658 void nir_metadata_require(nir_function_impl *impl, nir_metadata required, ...);
2659 /** dirties all but the preserved metadata */
2660 void nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved);
2661
2662 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
2663 nir_alu_instr *nir_alu_instr_create(nir_shader *shader, nir_op op);
2664
2665 nir_deref_instr *nir_deref_instr_create(nir_shader *shader,
2666 nir_deref_type deref_type);
2667
2668 nir_jump_instr *nir_jump_instr_create(nir_shader *shader, nir_jump_type type);
2669
2670 nir_load_const_instr *nir_load_const_instr_create(nir_shader *shader,
2671 unsigned num_components,
2672 unsigned bit_size);
2673
2674 nir_intrinsic_instr *nir_intrinsic_instr_create(nir_shader *shader,
2675 nir_intrinsic_op op);
2676
2677 nir_call_instr *nir_call_instr_create(nir_shader *shader,
2678 nir_function *callee);
2679
2680 nir_tex_instr *nir_tex_instr_create(nir_shader *shader, unsigned num_srcs);
2681
2682 nir_phi_instr *nir_phi_instr_create(nir_shader *shader);
2683
2684 nir_parallel_copy_instr *nir_parallel_copy_instr_create(nir_shader *shader);
2685
2686 nir_ssa_undef_instr *nir_ssa_undef_instr_create(nir_shader *shader,
2687 unsigned num_components,
2688 unsigned bit_size);
2689
2690 nir_const_value nir_alu_binop_identity(nir_op binop, unsigned bit_size);
2691
2692 /**
2693 * NIR Cursors and Instruction Insertion API
2694 * @{
2695 *
2696 * A tiny struct representing a point to insert/extract instructions or
2697 * control flow nodes. Helps reduce the combinatorial explosion of possible
2698 * points to insert/extract.
2699 *
2700 * \sa nir_control_flow.h
2701 */
2702 typedef enum {
2703 nir_cursor_before_block,
2704 nir_cursor_after_block,
2705 nir_cursor_before_instr,
2706 nir_cursor_after_instr,
2707 } nir_cursor_option;
2708
2709 typedef struct {
2710 nir_cursor_option option;
2711 union {
2712 nir_block *block;
2713 nir_instr *instr;
2714 };
2715 } nir_cursor;
2716
2717 static inline nir_block *
2718 nir_cursor_current_block(nir_cursor cursor)
2719 {
2720 if (cursor.option == nir_cursor_before_instr ||
2721 cursor.option == nir_cursor_after_instr) {
2722 return cursor.instr->block;
2723 } else {
2724 return cursor.block;
2725 }
2726 }
2727
2728 bool nir_cursors_equal(nir_cursor a, nir_cursor b);
2729
2730 static inline nir_cursor
2731 nir_before_block(nir_block *block)
2732 {
2733 nir_cursor cursor;
2734 cursor.option = nir_cursor_before_block;
2735 cursor.block = block;
2736 return cursor;
2737 }
2738
2739 static inline nir_cursor
2740 nir_after_block(nir_block *block)
2741 {
2742 nir_cursor cursor;
2743 cursor.option = nir_cursor_after_block;
2744 cursor.block = block;
2745 return cursor;
2746 }
2747
2748 static inline nir_cursor
2749 nir_before_instr(nir_instr *instr)
2750 {
2751 nir_cursor cursor;
2752 cursor.option = nir_cursor_before_instr;
2753 cursor.instr = instr;
2754 return cursor;
2755 }
2756
2757 static inline nir_cursor
2758 nir_after_instr(nir_instr *instr)
2759 {
2760 nir_cursor cursor;
2761 cursor.option = nir_cursor_after_instr;
2762 cursor.instr = instr;
2763 return cursor;
2764 }
2765
2766 static inline nir_cursor
2767 nir_after_block_before_jump(nir_block *block)
2768 {
2769 nir_instr *last_instr = nir_block_last_instr(block);
2770 if (last_instr && last_instr->type == nir_instr_type_jump) {
2771 return nir_before_instr(last_instr);
2772 } else {
2773 return nir_after_block(block);
2774 }
2775 }
2776
2777 static inline nir_cursor
2778 nir_before_src(nir_src *src, bool is_if_condition)
2779 {
2780 if (is_if_condition) {
2781 nir_block *prev_block =
2782 nir_cf_node_as_block(nir_cf_node_prev(&src->parent_if->cf_node));
2783 assert(!nir_block_ends_in_jump(prev_block));
2784 return nir_after_block(prev_block);
2785 } else if (src->parent_instr->type == nir_instr_type_phi) {
2786 #ifndef NDEBUG
2787 nir_phi_instr *cond_phi = nir_instr_as_phi(src->parent_instr);
2788 bool found = false;
2789 nir_foreach_phi_src(phi_src, cond_phi) {
2790 if (phi_src->src.ssa == src->ssa) {
2791 found = true;
2792 break;
2793 }
2794 }
2795 assert(found);
2796 #endif
2797 /* The LIST_ENTRY macro is a generic container-of macro, it just happens
2798 * to have a more specific name.
2799 */
2800 nir_phi_src *phi_src = LIST_ENTRY(nir_phi_src, src, src);
2801 return nir_after_block_before_jump(phi_src->pred);
2802 } else {
2803 return nir_before_instr(src->parent_instr);
2804 }
2805 }
2806
2807 static inline nir_cursor
2808 nir_before_cf_node(nir_cf_node *node)
2809 {
2810 if (node->type == nir_cf_node_block)
2811 return nir_before_block(nir_cf_node_as_block(node));
2812
2813 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node)));
2814 }
2815
2816 static inline nir_cursor
2817 nir_after_cf_node(nir_cf_node *node)
2818 {
2819 if (node->type == nir_cf_node_block)
2820 return nir_after_block(nir_cf_node_as_block(node));
2821
2822 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node)));
2823 }
2824
2825 static inline nir_cursor
2826 nir_after_phis(nir_block *block)
2827 {
2828 nir_foreach_instr(instr, block) {
2829 if (instr->type != nir_instr_type_phi)
2830 return nir_before_instr(instr);
2831 }
2832 return nir_after_block(block);
2833 }
2834
2835 static inline nir_cursor
2836 nir_after_cf_node_and_phis(nir_cf_node *node)
2837 {
2838 if (node->type == nir_cf_node_block)
2839 return nir_after_block(nir_cf_node_as_block(node));
2840
2841 nir_block *block = nir_cf_node_as_block(nir_cf_node_next(node));
2842
2843 return nir_after_phis(block);
2844 }
2845
2846 static inline nir_cursor
2847 nir_before_cf_list(struct exec_list *cf_list)
2848 {
2849 nir_cf_node *first_node = exec_node_data(nir_cf_node,
2850 exec_list_get_head(cf_list), node);
2851 return nir_before_cf_node(first_node);
2852 }
2853
2854 static inline nir_cursor
2855 nir_after_cf_list(struct exec_list *cf_list)
2856 {
2857 nir_cf_node *last_node = exec_node_data(nir_cf_node,
2858 exec_list_get_tail(cf_list), node);
2859 return nir_after_cf_node(last_node);
2860 }
2861
2862 /**
2863 * Insert a NIR instruction at the given cursor.
2864 *
2865 * Note: This does not update the cursor.
2866 */
2867 void nir_instr_insert(nir_cursor cursor, nir_instr *instr);
2868
2869 static inline void
2870 nir_instr_insert_before(nir_instr *instr, nir_instr *before)
2871 {
2872 nir_instr_insert(nir_before_instr(instr), before);
2873 }
2874
2875 static inline void
2876 nir_instr_insert_after(nir_instr *instr, nir_instr *after)
2877 {
2878 nir_instr_insert(nir_after_instr(instr), after);
2879 }
2880
2881 static inline void
2882 nir_instr_insert_before_block(nir_block *block, nir_instr *before)
2883 {
2884 nir_instr_insert(nir_before_block(block), before);
2885 }
2886
2887 static inline void
2888 nir_instr_insert_after_block(nir_block *block, nir_instr *after)
2889 {
2890 nir_instr_insert(nir_after_block(block), after);
2891 }
2892
2893 static inline void
2894 nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
2895 {
2896 nir_instr_insert(nir_before_cf_node(node), before);
2897 }
2898
2899 static inline void
2900 nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
2901 {
2902 nir_instr_insert(nir_after_cf_node(node), after);
2903 }
2904
2905 static inline void
2906 nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
2907 {
2908 nir_instr_insert(nir_before_cf_list(list), before);
2909 }
2910
2911 static inline void
2912 nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
2913 {
2914 nir_instr_insert(nir_after_cf_list(list), after);
2915 }
2916
2917 void nir_instr_remove_v(nir_instr *instr);
2918
2919 static inline nir_cursor
2920 nir_instr_remove(nir_instr *instr)
2921 {
2922 nir_cursor cursor;
2923 nir_instr *prev = nir_instr_prev(instr);
2924 if (prev) {
2925 cursor = nir_after_instr(prev);
2926 } else {
2927 cursor = nir_before_block(instr->block);
2928 }
2929 nir_instr_remove_v(instr);
2930 return cursor;
2931 }
2932
2933 /** @} */
2934
2935 typedef bool (*nir_foreach_ssa_def_cb)(nir_ssa_def *def, void *state);
2936 typedef bool (*nir_foreach_dest_cb)(nir_dest *dest, void *state);
2937 typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
2938 bool nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb,
2939 void *state);
2940 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
2941 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
2942
2943 nir_const_value *nir_src_as_const_value(nir_src src);
2944
2945 #define NIR_SRC_AS_(name, c_type, type_enum, cast_macro) \
2946 static inline c_type * \
2947 nir_src_as_ ## name (nir_src src) \
2948 { \
2949 return src.is_ssa && src.ssa->parent_instr->type == type_enum \
2950 ? cast_macro(src.ssa->parent_instr) : NULL; \
2951 }
2952
2953 NIR_SRC_AS_(alu_instr, nir_alu_instr, nir_instr_type_alu, nir_instr_as_alu)
2954 NIR_SRC_AS_(intrinsic, nir_intrinsic_instr,
2955 nir_instr_type_intrinsic, nir_instr_as_intrinsic)
2956 NIR_SRC_AS_(deref, nir_deref_instr, nir_instr_type_deref, nir_instr_as_deref)
2957
2958 bool nir_src_is_dynamically_uniform(nir_src src);
2959 bool nir_srcs_equal(nir_src src1, nir_src src2);
2960 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
2961 void nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src);
2962 void nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src);
2963 void nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest,
2964 nir_dest new_dest);
2965
2966 void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
2967 unsigned num_components, unsigned bit_size,
2968 const char *name);
2969 void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
2970 unsigned num_components, unsigned bit_size,
2971 const char *name);
2972 static inline void
2973 nir_ssa_dest_init_for_type(nir_instr *instr, nir_dest *dest,
2974 const struct glsl_type *type,
2975 const char *name)
2976 {
2977 assert(glsl_type_is_vector_or_scalar(type));
2978 nir_ssa_dest_init(instr, dest, glsl_get_components(type),
2979 glsl_get_bit_size(type), name);
2980 }
2981 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src);
2982 void nir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_src new_src,
2983 nir_instr *after_me);
2984
2985 nir_component_mask_t nir_ssa_def_components_read(const nir_ssa_def *def);
2986
2987 /*
2988 * finds the next basic block in source-code order, returns NULL if there is
2989 * none
2990 */
2991
2992 nir_block *nir_block_cf_tree_next(nir_block *block);
2993
2994 /* Performs the opposite of nir_block_cf_tree_next() */
2995
2996 nir_block *nir_block_cf_tree_prev(nir_block *block);
2997
2998 /* Gets the first block in a CF node in source-code order */
2999
3000 nir_block *nir_cf_node_cf_tree_first(nir_cf_node *node);
3001
3002 /* Gets the last block in a CF node in source-code order */
3003
3004 nir_block *nir_cf_node_cf_tree_last(nir_cf_node *node);
3005
3006 /* Gets the next block after a CF node in source-code order */
3007
3008 nir_block *nir_cf_node_cf_tree_next(nir_cf_node *node);
3009
3010 /* Macros for loops that visit blocks in source-code order */
3011
3012 #define nir_foreach_block(block, impl) \
3013 for (nir_block *block = nir_start_block(impl); block != NULL; \
3014 block = nir_block_cf_tree_next(block))
3015
3016 #define nir_foreach_block_safe(block, impl) \
3017 for (nir_block *block = nir_start_block(impl), \
3018 *next = nir_block_cf_tree_next(block); \
3019 block != NULL; \
3020 block = next, next = nir_block_cf_tree_next(block))
3021
3022 #define nir_foreach_block_reverse(block, impl) \
3023 for (nir_block *block = nir_impl_last_block(impl); block != NULL; \
3024 block = nir_block_cf_tree_prev(block))
3025
3026 #define nir_foreach_block_reverse_safe(block, impl) \
3027 for (nir_block *block = nir_impl_last_block(impl), \
3028 *prev = nir_block_cf_tree_prev(block); \
3029 block != NULL; \
3030 block = prev, prev = nir_block_cf_tree_prev(block))
3031
3032 #define nir_foreach_block_in_cf_node(block, node) \
3033 for (nir_block *block = nir_cf_node_cf_tree_first(node); \
3034 block != nir_cf_node_cf_tree_next(node); \
3035 block = nir_block_cf_tree_next(block))
3036
3037 /* If the following CF node is an if, this function returns that if.
3038 * Otherwise, it returns NULL.
3039 */
3040 nir_if *nir_block_get_following_if(nir_block *block);
3041
3042 nir_loop *nir_block_get_following_loop(nir_block *block);
3043
3044 void nir_index_local_regs(nir_function_impl *impl);
3045 void nir_index_ssa_defs(nir_function_impl *impl);
3046 unsigned nir_index_instrs(nir_function_impl *impl);
3047
3048 void nir_index_blocks(nir_function_impl *impl);
3049
3050 void nir_print_shader(nir_shader *shader, FILE *fp);
3051 void nir_print_shader_annotated(nir_shader *shader, FILE *fp, struct hash_table *errors);
3052 void nir_print_instr(const nir_instr *instr, FILE *fp);
3053 void nir_print_deref(const nir_deref_instr *deref, FILE *fp);
3054
3055 /** Shallow clone of a single ALU instruction. */
3056 nir_alu_instr *nir_alu_instr_clone(nir_shader *s, const nir_alu_instr *orig);
3057
3058 nir_shader *nir_shader_clone(void *mem_ctx, const nir_shader *s);
3059 nir_function_impl *nir_function_impl_clone(nir_shader *shader,
3060 const nir_function_impl *fi);
3061 nir_constant *nir_constant_clone(const nir_constant *c, nir_variable *var);
3062 nir_variable *nir_variable_clone(const nir_variable *c, nir_shader *shader);
3063
3064 void nir_shader_replace(nir_shader *dest, nir_shader *src);
3065
3066 void nir_shader_serialize_deserialize(nir_shader *s);
3067
3068 #ifndef NDEBUG
3069 void nir_validate_shader(nir_shader *shader, const char *when);
3070 void nir_metadata_set_validation_flag(nir_shader *shader);
3071 void nir_metadata_check_validation_flag(nir_shader *shader);
3072
3073 static inline bool
3074 should_skip_nir(const char *name)
3075 {
3076 static const char *list = NULL;
3077 if (!list) {
3078 /* Comma separated list of names to skip. */
3079 list = getenv("NIR_SKIP");
3080 if (!list)
3081 list = "";
3082 }
3083
3084 if (!list[0])
3085 return false;
3086
3087 return comma_separated_list_contains(list, name);
3088 }
3089
3090 static inline bool
3091 should_clone_nir(void)
3092 {
3093 static int should_clone = -1;
3094 if (should_clone < 0)
3095 should_clone = env_var_as_boolean("NIR_TEST_CLONE", false);
3096
3097 return should_clone;
3098 }
3099
3100 static inline bool
3101 should_serialize_deserialize_nir(void)
3102 {
3103 static int test_serialize = -1;
3104 if (test_serialize < 0)
3105 test_serialize = env_var_as_boolean("NIR_TEST_SERIALIZE", false);
3106
3107 return test_serialize;
3108 }
3109
3110 static inline bool
3111 should_print_nir(void)
3112 {
3113 static int should_print = -1;
3114 if (should_print < 0)
3115 should_print = env_var_as_boolean("NIR_PRINT", false);
3116
3117 return should_print;
3118 }
3119 #else
3120 static inline void nir_validate_shader(nir_shader *shader, const char *when) { (void) shader; (void)when; }
3121 static inline void nir_metadata_set_validation_flag(nir_shader *shader) { (void) shader; }
3122 static inline void nir_metadata_check_validation_flag(nir_shader *shader) { (void) shader; }
3123 static inline bool should_skip_nir(UNUSED const char *pass_name) { return false; }
3124 static inline bool should_clone_nir(void) { return false; }
3125 static inline bool should_serialize_deserialize_nir(void) { return false; }
3126 static inline bool should_print_nir(void) { return false; }
3127 #endif /* NDEBUG */
3128
3129 #define _PASS(pass, nir, do_pass) do { \
3130 if (should_skip_nir(#pass)) { \
3131 printf("skipping %s\n", #pass); \
3132 break; \
3133 } \
3134 do_pass \
3135 nir_validate_shader(nir, "after " #pass); \
3136 if (should_clone_nir()) { \
3137 nir_shader *clone = nir_shader_clone(ralloc_parent(nir), nir); \
3138 nir_shader_replace(nir, clone); \
3139 } \
3140 if (should_serialize_deserialize_nir()) { \
3141 nir_shader_serialize_deserialize(nir); \
3142 } \
3143 } while (0)
3144
3145 #define NIR_PASS(progress, nir, pass, ...) _PASS(pass, nir, \
3146 nir_metadata_set_validation_flag(nir); \
3147 if (should_print_nir()) \
3148 printf("%s\n", #pass); \
3149 if (pass(nir, ##__VA_ARGS__)) { \
3150 progress = true; \
3151 if (should_print_nir()) \
3152 nir_print_shader(nir, stdout); \
3153 nir_metadata_check_validation_flag(nir); \
3154 } \
3155 )
3156
3157 #define NIR_PASS_V(nir, pass, ...) _PASS(pass, nir, \
3158 if (should_print_nir()) \
3159 printf("%s\n", #pass); \
3160 pass(nir, ##__VA_ARGS__); \
3161 if (should_print_nir()) \
3162 nir_print_shader(nir, stdout); \
3163 )
3164
3165 #define NIR_SKIP(name) should_skip_nir(#name)
3166
3167 void nir_calc_dominance_impl(nir_function_impl *impl);
3168 void nir_calc_dominance(nir_shader *shader);
3169
3170 nir_block *nir_dominance_lca(nir_block *b1, nir_block *b2);
3171 bool nir_block_dominates(nir_block *parent, nir_block *child);
3172
3173 void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
3174 void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
3175
3176 void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
3177 void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
3178
3179 void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
3180 void nir_dump_cfg(nir_shader *shader, FILE *fp);
3181
3182 int nir_gs_count_vertices(const nir_shader *shader);
3183
3184 bool nir_shrink_vec_array_vars(nir_shader *shader, nir_variable_mode modes);
3185 bool nir_split_array_vars(nir_shader *shader, nir_variable_mode modes);
3186 bool nir_split_var_copies(nir_shader *shader);
3187 bool nir_split_per_member_structs(nir_shader *shader);
3188 bool nir_split_struct_vars(nir_shader *shader, nir_variable_mode modes);
3189
3190 bool nir_lower_returns_impl(nir_function_impl *impl);
3191 bool nir_lower_returns(nir_shader *shader);
3192
3193 void nir_inline_function_impl(struct nir_builder *b,
3194 const nir_function_impl *impl,
3195 nir_ssa_def **params);
3196 bool nir_inline_functions(nir_shader *shader);
3197
3198 bool nir_propagate_invariant(nir_shader *shader);
3199
3200 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, nir_shader *shader);
3201 void nir_lower_deref_copy_instr(struct nir_builder *b,
3202 nir_intrinsic_instr *copy);
3203 bool nir_lower_var_copies(nir_shader *shader);
3204
3205 void nir_fixup_deref_modes(nir_shader *shader);
3206
3207 bool nir_lower_global_vars_to_local(nir_shader *shader);
3208
3209 typedef enum {
3210 nir_lower_direct_array_deref_of_vec_load = (1 << 0),
3211 nir_lower_indirect_array_deref_of_vec_load = (1 << 1),
3212 nir_lower_direct_array_deref_of_vec_store = (1 << 2),
3213 nir_lower_indirect_array_deref_of_vec_store = (1 << 3),
3214 } nir_lower_array_deref_of_vec_options;
3215
3216 bool nir_lower_array_deref_of_vec(nir_shader *shader, nir_variable_mode modes,
3217 nir_lower_array_deref_of_vec_options options);
3218
3219 bool nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes);
3220
3221 bool nir_lower_locals_to_regs(nir_shader *shader);
3222
3223 void nir_lower_io_to_temporaries(nir_shader *shader,
3224 nir_function_impl *entrypoint,
3225 bool outputs, bool inputs);
3226
3227 bool nir_lower_vars_to_scratch(nir_shader *shader,
3228 nir_variable_mode modes,
3229 int size_threshold,
3230 glsl_type_size_align_func size_align);
3231
3232 void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);
3233
3234 void nir_gather_ssa_types(nir_function_impl *impl,
3235 BITSET_WORD *float_types,
3236 BITSET_WORD *int_types);
3237
3238 void nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
3239 int (*type_size)(const struct glsl_type *, bool));
3240
3241 /* Some helpers to do very simple linking */
3242 bool nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer);
3243 bool nir_remove_unused_io_vars(nir_shader *shader, struct exec_list *var_list,
3244 uint64_t *used_by_other_stage,
3245 uint64_t *used_by_other_stage_patches);
3246 void nir_compact_varyings(nir_shader *producer, nir_shader *consumer,
3247 bool default_to_smooth_interp);
3248 void nir_link_xfb_varyings(nir_shader *producer, nir_shader *consumer);
3249 bool nir_link_opt_varyings(nir_shader *producer, nir_shader *consumer);
3250
3251
3252 void nir_assign_io_var_locations(struct exec_list *var_list,
3253 unsigned *size,
3254 gl_shader_stage stage);
3255
3256 typedef enum {
3257 /* If set, this forces all non-flat fragment shader inputs to be
3258 * interpolated as if with the "sample" qualifier. This requires
3259 * nir_shader_compiler_options::use_interpolated_input_intrinsics.
3260 */
3261 nir_lower_io_force_sample_interpolation = (1 << 1),
3262 } nir_lower_io_options;
3263 bool nir_lower_io(nir_shader *shader,
3264 nir_variable_mode modes,
3265 int (*type_size)(const struct glsl_type *, bool),
3266 nir_lower_io_options);
3267
3268 bool nir_io_add_const_offset_to_base(nir_shader *nir, nir_variable_mode mode);
3269
3270 typedef enum {
3271 /**
3272 * An address format which is a simple 32-bit global GPU address.
3273 */
3274 nir_address_format_32bit_global,
3275
3276 /**
3277 * An address format which is a simple 64-bit global GPU address.
3278 */
3279 nir_address_format_64bit_global,
3280
3281 /**
3282 * An address format which is a bounds-checked 64-bit global GPU address.
3283 *
3284 * The address is comprised as a 32-bit vec4 where .xy are a uint64_t base
3285 * address stored with the low bits in .x and high bits in .y, .z is a
3286 * size, and .w is an offset. When the final I/O operation is lowered, .w
3287 * is checked against .z and the operation is predicated on the result.
3288 */
3289 nir_address_format_64bit_bounded_global,
3290
3291 /**
3292 * An address format which is comprised of a vec2 where the first
3293 * component is a buffer index and the second is an offset.
3294 */
3295 nir_address_format_32bit_index_offset,
3296
3297 /**
3298 * An address format which is a simple 32-bit offset.
3299 */
3300 nir_address_format_32bit_offset,
3301
3302 /**
3303 * An address format representing a purely logical addressing model. In
3304 * this model, all deref chains must be complete from the dereference
3305 * operation to the variable. Cast derefs are not allowed. These
3306 * addresses will be 32-bit scalars but the format is immaterial because
3307 * you can always chase the chain.
3308 */
3309 nir_address_format_logical,
3310 } nir_address_format;
3311
3312 static inline unsigned
3313 nir_address_format_bit_size(nir_address_format addr_format)
3314 {
3315 switch (addr_format) {
3316 case nir_address_format_32bit_global: return 32;
3317 case nir_address_format_64bit_global: return 64;
3318 case nir_address_format_64bit_bounded_global: return 32;
3319 case nir_address_format_32bit_index_offset: return 32;
3320 case nir_address_format_32bit_offset: return 32;
3321 case nir_address_format_logical: return 32;
3322 }
3323 unreachable("Invalid address format");
3324 }
3325
3326 static inline unsigned
3327 nir_address_format_num_components(nir_address_format addr_format)
3328 {
3329 switch (addr_format) {
3330 case nir_address_format_32bit_global: return 1;
3331 case nir_address_format_64bit_global: return 1;
3332 case nir_address_format_64bit_bounded_global: return 4;
3333 case nir_address_format_32bit_index_offset: return 2;
3334 case nir_address_format_32bit_offset: return 1;
3335 case nir_address_format_logical: return 1;
3336 }
3337 unreachable("Invalid address format");
3338 }
3339
3340 static inline const struct glsl_type *
3341 nir_address_format_to_glsl_type(nir_address_format addr_format)
3342 {
3343 unsigned bit_size = nir_address_format_bit_size(addr_format);
3344 assert(bit_size == 32 || bit_size == 64);
3345 return glsl_vector_type(bit_size == 32 ? GLSL_TYPE_UINT : GLSL_TYPE_UINT64,
3346 nir_address_format_num_components(addr_format));
3347 }
3348
3349 const nir_const_value *nir_address_format_null_value(nir_address_format addr_format);
3350
3351 nir_ssa_def *nir_build_addr_ieq(struct nir_builder *b, nir_ssa_def *addr0, nir_ssa_def *addr1,
3352 nir_address_format addr_format);
3353
3354 nir_ssa_def *nir_build_addr_isub(struct nir_builder *b, nir_ssa_def *addr0, nir_ssa_def *addr1,
3355 nir_address_format addr_format);
3356
3357 nir_ssa_def * nir_explicit_io_address_from_deref(struct nir_builder *b,
3358 nir_deref_instr *deref,
3359 nir_ssa_def *base_addr,
3360 nir_address_format addr_format);
3361 void nir_lower_explicit_io_instr(struct nir_builder *b,
3362 nir_intrinsic_instr *io_instr,
3363 nir_ssa_def *addr,
3364 nir_address_format addr_format);
3365
3366 bool nir_lower_explicit_io(nir_shader *shader,
3367 nir_variable_mode modes,
3368 nir_address_format);
3369
3370 nir_src *nir_get_io_offset_src(nir_intrinsic_instr *instr);
3371 nir_src *nir_get_io_vertex_index_src(nir_intrinsic_instr *instr);
3372
3373 bool nir_is_per_vertex_io(const nir_variable *var, gl_shader_stage stage);
3374
3375 bool nir_lower_regs_to_ssa_impl(nir_function_impl *impl);
3376 bool nir_lower_regs_to_ssa(nir_shader *shader);
3377 bool nir_lower_vars_to_ssa(nir_shader *shader);
3378
3379 bool nir_remove_dead_derefs(nir_shader *shader);
3380 bool nir_remove_dead_derefs_impl(nir_function_impl *impl);
3381 bool nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes);
3382 bool nir_lower_constant_initializers(nir_shader *shader,
3383 nir_variable_mode modes);
3384
3385 bool nir_move_load_const(nir_shader *shader);
3386 bool nir_move_vec_src_uses_to_dest(nir_shader *shader);
3387 bool nir_lower_vec_to_movs(nir_shader *shader);
3388 void nir_lower_alpha_test(nir_shader *shader, enum compare_func func,
3389 bool alpha_to_one);
3390 bool nir_lower_alu(nir_shader *shader);
3391
3392 bool nir_lower_flrp(nir_shader *shader, unsigned lowering_mask,
3393 bool always_precise, bool have_ffma);
3394
3395 bool nir_lower_alu_to_scalar(nir_shader *shader, BITSET_WORD *lower_set);
3396 bool nir_lower_bool_to_float(nir_shader *shader);
3397 bool nir_lower_bool_to_int32(nir_shader *shader);
3398 bool nir_lower_int_to_float(nir_shader *shader);
3399 bool nir_lower_load_const_to_scalar(nir_shader *shader);
3400 bool nir_lower_read_invocation_to_scalar(nir_shader *shader);
3401 bool nir_lower_phis_to_scalar(nir_shader *shader);
3402 void nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer);
3403 void nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
3404 bool outputs_only);
3405 void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask);
3406 void nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask);
3407 bool nir_lower_io_to_vector(nir_shader *shader, nir_variable_mode mask);
3408
3409 void nir_lower_fragcoord_wtrans(nir_shader *shader);
3410 void nir_lower_viewport_transform(nir_shader *shader);
3411 bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
3412
3413 typedef struct nir_lower_subgroups_options {
3414 uint8_t subgroup_size;
3415 uint8_t ballot_bit_size;
3416 bool lower_to_scalar:1;
3417 bool lower_vote_trivial:1;
3418 bool lower_vote_eq_to_ballot:1;
3419 bool lower_subgroup_masks:1;
3420 bool lower_shuffle:1;
3421 bool lower_shuffle_to_32bit:1;
3422 bool lower_quad:1;
3423 } nir_lower_subgroups_options;
3424
3425 bool nir_lower_subgroups(nir_shader *shader,
3426 const nir_lower_subgroups_options *options);
3427
3428 bool nir_lower_system_values(nir_shader *shader);
3429
3430 enum PACKED nir_lower_tex_packing {
3431 nir_lower_tex_packing_none = 0,
3432 /* The sampler returns up to 2 32-bit words of half floats or 16-bit signed
3433 * or unsigned ints based on the sampler type
3434 */
3435 nir_lower_tex_packing_16,
3436 /* The sampler returns 1 32-bit word of 4x8 unorm */
3437 nir_lower_tex_packing_8,
3438 };
3439
3440 typedef struct nir_lower_tex_options {
3441 /**
3442 * bitmask of (1 << GLSL_SAMPLER_DIM_x) to control for which
3443 * sampler types a texture projector is lowered.
3444 */
3445 unsigned lower_txp;
3446
3447 /**
3448 * If true, lower away nir_tex_src_offset for all texelfetch instructions.
3449 */
3450 bool lower_txf_offset;
3451
3452 /**
3453 * If true, lower away nir_tex_src_offset for all rect textures.
3454 */
3455 bool lower_rect_offset;
3456
3457 /**
3458 * If true, lower rect textures to 2D, using txs to fetch the
3459 * texture dimensions and dividing the texture coords by the
3460 * texture dims to normalize.
3461 */
3462 bool lower_rect;
3463
3464 /**
3465 * If true, convert yuv to rgb.
3466 */
3467 unsigned lower_y_uv_external;
3468 unsigned lower_y_u_v_external;
3469 unsigned lower_yx_xuxv_external;
3470 unsigned lower_xy_uxvx_external;
3471 unsigned lower_ayuv_external;
3472 unsigned lower_xyuv_external;
3473
3474 /**
3475 * To emulate certain texture wrap modes, this can be used
3476 * to saturate the specified tex coord to [0.0, 1.0]. The
3477 * bits are according to sampler #, ie. if, for example:
3478 *
3479 * (conf->saturate_s & (1 << n))
3480 *
3481 * is true, then the s coord for sampler n is saturated.
3482 *
3483 * Note that clamping must happen *after* projector lowering
3484 * so any projected texture sample instruction with a clamped
3485 * coordinate gets automatically lowered, regardless of the
3486 * 'lower_txp' setting.
3487 */
3488 unsigned saturate_s;
3489 unsigned saturate_t;
3490 unsigned saturate_r;
3491
3492 /* Bitmask of textures that need swizzling.
3493 *
3494 * If (swizzle_result & (1 << texture_index)), then the swizzle in
3495 * swizzles[texture_index] is applied to the result of the texturing
3496 * operation.
3497 */
3498 unsigned swizzle_result;
3499
3500 /* A swizzle for each texture. Values 0-3 represent x, y, z, or w swizzles
3501 * while 4 and 5 represent 0 and 1 respectively.
3502 */
3503 uint8_t swizzles[32][4];
3504
3505 /* Can be used to scale sampled values in range required by the format. */
3506 float scale_factors[32];
3507
3508 /**
3509 * Bitmap of textures that need srgb to linear conversion. If
3510 * (lower_srgb & (1 << texture_index)) then the rgb (xyz) components
3511 * of the texture are lowered to linear.
3512 */
3513 unsigned lower_srgb;
3514
3515 /**
3516 * If true, lower nir_texop_tex on shaders that doesn't support implicit
3517 * LODs to nir_texop_txl.
3518 */
3519 bool lower_tex_without_implicit_lod;
3520
3521 /**
3522 * If true, lower nir_texop_txd on cube maps with nir_texop_txl.
3523 */
3524 bool lower_txd_cube_map;
3525
3526 /**
3527 * If true, lower nir_texop_txd on 3D surfaces with nir_texop_txl.
3528 */
3529 bool lower_txd_3d;
3530
3531 /**
3532 * If true, lower nir_texop_txd on shadow samplers (except cube maps)
3533 * with nir_texop_txl. Notice that cube map shadow samplers are lowered
3534 * with lower_txd_cube_map.
3535 */
3536 bool lower_txd_shadow;
3537
3538 /**
3539 * If true, lower nir_texop_txd on all samplers to a nir_texop_txl.
3540 * Implies lower_txd_cube_map and lower_txd_shadow.
3541 */
3542 bool lower_txd;
3543
3544 /**
3545 * If true, lower nir_texop_txb that try to use shadow compare and min_lod
3546 * at the same time to a nir_texop_lod, some math, and nir_texop_tex.
3547 */
3548 bool lower_txb_shadow_clamp;
3549
3550 /**
3551 * If true, lower nir_texop_txd on shadow samplers when it uses min_lod
3552 * with nir_texop_txl. This includes cube maps.
3553 */
3554 bool lower_txd_shadow_clamp;
3555
3556 /**
3557 * If true, lower nir_texop_txd on when it uses both offset and min_lod
3558 * with nir_texop_txl. This includes cube maps.
3559 */
3560 bool lower_txd_offset_clamp;
3561
3562 /**
3563 * If true, lower nir_texop_txd with min_lod to a nir_texop_txl if the
3564 * sampler is bindless.
3565 */
3566 bool lower_txd_clamp_bindless_sampler;
3567
3568 /**
3569 * If true, lower nir_texop_txd with min_lod to a nir_texop_txl if the
3570 * sampler index is not statically determinable to be less than 16.
3571 */
3572 bool lower_txd_clamp_if_sampler_index_not_lt_16;
3573
3574 /**
3575 * If true, lower nir_texop_txs with a non-0-lod into nir_texop_txs with
3576 * 0-lod followed by a nir_ishr.
3577 */
3578 bool lower_txs_lod;
3579
3580 /**
3581 * If true, apply a .bagr swizzle on tg4 results to handle Broadcom's
3582 * mixed-up tg4 locations.
3583 */
3584 bool lower_tg4_broadcom_swizzle;
3585
3586 /**
3587 * If true, lowers tg4 with 4 constant offsets to 4 tg4 calls
3588 */
3589 bool lower_tg4_offsets;
3590
3591 enum nir_lower_tex_packing lower_tex_packing[32];
3592 } nir_lower_tex_options;
3593
3594 bool nir_lower_tex(nir_shader *shader,
3595 const nir_lower_tex_options *options);
3596
3597 enum nir_lower_non_uniform_access_type {
3598 nir_lower_non_uniform_ubo_access = (1 << 0),
3599 nir_lower_non_uniform_ssbo_access = (1 << 1),
3600 nir_lower_non_uniform_texture_access = (1 << 2),
3601 nir_lower_non_uniform_image_access = (1 << 3),
3602 };
3603
3604 bool nir_lower_non_uniform_access(nir_shader *shader,
3605 enum nir_lower_non_uniform_access_type);
3606
3607 bool nir_lower_idiv(nir_shader *shader);
3608
3609 bool nir_lower_input_attachments(nir_shader *shader, bool use_fragcoord_sysval);
3610
3611 bool nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables, bool use_vars);
3612 bool nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables);
3613 bool nir_lower_clip_cull_distance_arrays(nir_shader *nir);
3614
3615 bool nir_lower_frexp(nir_shader *nir);
3616
3617 void nir_lower_two_sided_color(nir_shader *shader);
3618
3619 bool nir_lower_clamp_color_outputs(nir_shader *shader);
3620
3621 void nir_lower_passthrough_edgeflags(nir_shader *shader);
3622 bool nir_lower_patch_vertices(nir_shader *nir, unsigned static_count,
3623 const gl_state_index16 *uniform_state_tokens);
3624
3625 typedef struct nir_lower_wpos_ytransform_options {
3626 gl_state_index16 state_tokens[STATE_LENGTH];
3627 bool fs_coord_origin_upper_left :1;
3628 bool fs_coord_origin_lower_left :1;
3629 bool fs_coord_pixel_center_integer :1;
3630 bool fs_coord_pixel_center_half_integer :1;
3631 } nir_lower_wpos_ytransform_options;
3632
3633 bool nir_lower_wpos_ytransform(nir_shader *shader,
3634 const nir_lower_wpos_ytransform_options *options);
3635 bool nir_lower_wpos_center(nir_shader *shader, const bool for_sample_shading);
3636
3637 bool nir_lower_fb_read(nir_shader *shader);
3638
3639 typedef struct nir_lower_drawpixels_options {
3640 gl_state_index16 texcoord_state_tokens[STATE_LENGTH];
3641 gl_state_index16 scale_state_tokens[STATE_LENGTH];
3642 gl_state_index16 bias_state_tokens[STATE_LENGTH];
3643 unsigned drawpix_sampler;
3644 unsigned pixelmap_sampler;
3645 bool pixel_maps :1;
3646 bool scale_and_bias :1;
3647 } nir_lower_drawpixels_options;
3648
3649 void nir_lower_drawpixels(nir_shader *shader,
3650 const nir_lower_drawpixels_options *options);
3651
3652 typedef struct nir_lower_bitmap_options {
3653 unsigned sampler;
3654 bool swizzle_xxxx;
3655 } nir_lower_bitmap_options;
3656
3657 void nir_lower_bitmap(nir_shader *shader, const nir_lower_bitmap_options *options);
3658
3659 bool nir_lower_atomics_to_ssbo(nir_shader *shader, unsigned ssbo_offset);
3660
3661 typedef enum {
3662 nir_lower_int_source_mods = 1 << 0,
3663 nir_lower_float_source_mods = 1 << 1,
3664 nir_lower_triop_abs = 1 << 2,
3665 nir_lower_all_source_mods = (1 << 3) - 1
3666 } nir_lower_to_source_mods_flags;
3667
3668
3669 bool nir_lower_to_source_mods(nir_shader *shader, nir_lower_to_source_mods_flags options);
3670
3671 bool nir_lower_gs_intrinsics(nir_shader *shader);
3672
3673 typedef unsigned (*nir_lower_bit_size_callback)(const nir_alu_instr *, void *);
3674
3675 bool nir_lower_bit_size(nir_shader *shader,
3676 nir_lower_bit_size_callback callback,
3677 void *callback_data);
3678
3679 nir_lower_int64_options nir_lower_int64_op_to_options_mask(nir_op opcode);
3680 bool nir_lower_int64(nir_shader *shader, nir_lower_int64_options options);
3681
3682 nir_lower_doubles_options nir_lower_doubles_op_to_options_mask(nir_op opcode);
3683 bool nir_lower_doubles(nir_shader *shader, const nir_shader *softfp64,
3684 nir_lower_doubles_options options);
3685 bool nir_lower_pack(nir_shader *shader);
3686
3687 typedef enum {
3688 nir_lower_interpolation_at_sample = (1 << 1),
3689 nir_lower_interpolation_at_offset = (1 << 2),
3690 nir_lower_interpolation_centroid = (1 << 3),
3691 nir_lower_interpolation_pixel = (1 << 4),
3692 nir_lower_interpolation_sample = (1 << 5),
3693 } nir_lower_interpolation_options;
3694
3695 bool nir_lower_interpolation(nir_shader *shader,
3696 nir_lower_interpolation_options options);
3697
3698 bool nir_normalize_cubemap_coords(nir_shader *shader);
3699
3700 void nir_live_ssa_defs_impl(nir_function_impl *impl);
3701
3702 void nir_loop_analyze_impl(nir_function_impl *impl,
3703 nir_variable_mode indirect_mask);
3704
3705 bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
3706
3707 bool nir_repair_ssa_impl(nir_function_impl *impl);
3708 bool nir_repair_ssa(nir_shader *shader);
3709
3710 void nir_convert_loop_to_lcssa(nir_loop *loop);
3711
3712 /* If phi_webs_only is true, only convert SSA values involved in phi nodes to
3713 * registers. If false, convert all values (even those not involved in a phi
3714 * node) to registers.
3715 */
3716 bool nir_convert_from_ssa(nir_shader *shader, bool phi_webs_only);
3717
3718 bool nir_lower_phis_to_regs_block(nir_block *block);
3719 bool nir_lower_ssa_defs_to_regs_block(nir_block *block);
3720 bool nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl);
3721
3722 /* This is here for unit tests. */
3723 bool nir_opt_comparison_pre_impl(nir_function_impl *impl);
3724
3725 bool nir_opt_comparison_pre(nir_shader *shader);
3726
3727 bool nir_opt_algebraic(nir_shader *shader);
3728 bool nir_opt_algebraic_before_ffma(nir_shader *shader);
3729 bool nir_opt_algebraic_late(nir_shader *shader);
3730 bool nir_opt_constant_folding(nir_shader *shader);
3731
3732 bool nir_opt_combine_stores(nir_shader *shader, nir_variable_mode modes);
3733
3734 bool nir_copy_prop(nir_shader *shader);
3735
3736 bool nir_opt_copy_prop_vars(nir_shader *shader);
3737
3738 bool nir_opt_cse(nir_shader *shader);
3739
3740 bool nir_opt_dce(nir_shader *shader);
3741
3742 bool nir_opt_dead_cf(nir_shader *shader);
3743
3744 bool nir_opt_dead_write_vars(nir_shader *shader);
3745
3746 bool nir_opt_deref_impl(nir_function_impl *impl);
3747 bool nir_opt_deref(nir_shader *shader);
3748
3749 bool nir_opt_find_array_copies(nir_shader *shader);
3750
3751 bool nir_opt_gcm(nir_shader *shader, bool value_number);
3752
3753 bool nir_opt_idiv_const(nir_shader *shader, unsigned min_bit_size);
3754
3755 bool nir_opt_if(nir_shader *shader, bool aggressive_last_continue);
3756
3757 bool nir_opt_intrinsics(nir_shader *shader);
3758
3759 bool nir_opt_large_constants(nir_shader *shader,
3760 glsl_type_size_align_func size_align,
3761 unsigned threshold);
3762
3763 bool nir_opt_loop_unroll(nir_shader *shader, nir_variable_mode indirect_mask);
3764
3765 bool nir_opt_move_comparisons(nir_shader *shader);
3766
3767 bool nir_opt_move_load_ubo(nir_shader *shader);
3768
3769 bool nir_opt_peephole_select(nir_shader *shader, unsigned limit,
3770 bool indirect_load_ok, bool expensive_alu_ok);
3771
3772 bool nir_opt_rematerialize_compares(nir_shader *shader);
3773
3774 bool nir_opt_remove_phis(nir_shader *shader);
3775
3776 bool nir_opt_shrink_load(nir_shader *shader);
3777
3778 bool nir_opt_trivial_continues(nir_shader *shader);
3779
3780 bool nir_opt_undef(nir_shader *shader);
3781
3782 bool nir_opt_vectorize(nir_shader *shader);
3783
3784 bool nir_opt_conditional_discard(nir_shader *shader);
3785
3786 void nir_strip(nir_shader *shader);
3787
3788 void nir_sweep(nir_shader *shader);
3789
3790 void nir_remap_dual_slot_attributes(nir_shader *shader,
3791 uint64_t *dual_slot_inputs);
3792 uint64_t nir_get_single_slot_attribs_mask(uint64_t attribs, uint64_t dual_slot);
3793
3794 nir_intrinsic_op nir_intrinsic_from_system_value(gl_system_value val);
3795 gl_system_value nir_system_value_from_intrinsic(nir_intrinsic_op intrin);
3796
3797 bool nir_lower_sincos(nir_shader *shader);
3798
3799 static inline bool
3800 nir_variable_is_in_ubo(const nir_variable *var)
3801 {
3802 return (var->data.mode == nir_var_mem_ubo &&
3803 var->interface_type != NULL);
3804 }
3805
3806 static inline bool
3807 nir_variable_is_in_ssbo(const nir_variable *var)
3808 {
3809 return (var->data.mode == nir_var_mem_ssbo &&
3810 var->interface_type != NULL);
3811 }
3812
3813 static inline bool
3814 nir_variable_is_in_block(const nir_variable *var)
3815 {
3816 return nir_variable_is_in_ubo(var) || nir_variable_is_in_ssbo(var);
3817 }
3818
3819 #ifdef __cplusplus
3820 } /* extern "C" */
3821 #endif
3822
3823 #endif /* NIR_H */