a122f12d079fd2a5b85d1dac93343d471b2d4849
[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 #pragma once
29
30 #include "util/hash_table.h"
31 #include "compiler/glsl/list.h"
32 #include "GL/gl.h" /* GLenum */
33 #include "util/list.h"
34 #include "util/ralloc.h"
35 #include "util/set.h"
36 #include "util/bitset.h"
37 #include "util/macros.h"
38 #include "compiler/nir_types.h"
39 #include "compiler/shader_enums.h"
40 #include <stdio.h>
41
42 #include "nir_opcodes.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 struct gl_program;
49 struct gl_shader_program;
50
51 #define NIR_FALSE 0u
52 #define NIR_TRUE (~0u)
53
54 /** Defines a cast function
55 *
56 * This macro defines a cast function from in_type to out_type where
57 * out_type is some structure type that contains a field of type out_type.
58 *
59 * Note that you have to be a bit careful as the generated cast function
60 * destroys constness.
61 */
62 #define NIR_DEFINE_CAST(name, in_type, out_type, field, \
63 type_field, type_value) \
64 static inline out_type * \
65 name(const in_type *parent) \
66 { \
67 assert(parent && parent->type_field == type_value); \
68 return exec_node_data(out_type, parent, field); \
69 }
70
71 struct nir_function;
72 struct nir_shader;
73 struct nir_instr;
74
75
76 /**
77 * Description of built-in state associated with a uniform
78 *
79 * \sa nir_variable::state_slots
80 */
81 typedef struct {
82 int tokens[5];
83 int swizzle;
84 } nir_state_slot;
85
86 typedef enum {
87 nir_var_shader_in = (1 << 0),
88 nir_var_shader_out = (1 << 1),
89 nir_var_global = (1 << 2),
90 nir_var_local = (1 << 3),
91 nir_var_uniform = (1 << 4),
92 nir_var_shader_storage = (1 << 5),
93 nir_var_system_value = (1 << 6),
94 nir_var_param = (1 << 7),
95 nir_var_shared = (1 << 8),
96 nir_var_all = ~0,
97 } nir_variable_mode;
98
99 /**
100 * Data stored in an nir_constant
101 */
102 union nir_constant_data {
103 unsigned u[16];
104 int i[16];
105 float f[16];
106 bool b[16];
107 double d[16];
108 };
109
110 typedef struct nir_constant {
111 /**
112 * Value of the constant.
113 *
114 * The field used to back the values supplied by the constant is determined
115 * by the type associated with the \c nir_variable. Constants may be
116 * scalars, vectors, or matrices.
117 */
118 union nir_constant_data value;
119
120 /* we could get this from the var->type but makes clone *much* easier to
121 * not have to care about the type.
122 */
123 unsigned num_elements;
124
125 /* Array elements / Structure Fields */
126 struct nir_constant **elements;
127 } nir_constant;
128
129 /**
130 * \brief Layout qualifiers for gl_FragDepth.
131 *
132 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
133 * with a layout qualifier.
134 */
135 typedef enum {
136 nir_depth_layout_none, /**< No depth layout is specified. */
137 nir_depth_layout_any,
138 nir_depth_layout_greater,
139 nir_depth_layout_less,
140 nir_depth_layout_unchanged
141 } nir_depth_layout;
142
143 /**
144 * Either a uniform, global variable, shader input, or shader output. Based on
145 * ir_variable - it should be easy to translate between the two.
146 */
147
148 typedef struct nir_variable {
149 struct exec_node node;
150
151 /**
152 * Declared type of the variable
153 */
154 const struct glsl_type *type;
155
156 /**
157 * Declared name of the variable
158 */
159 char *name;
160
161 struct nir_variable_data {
162 /**
163 * Storage class of the variable.
164 *
165 * \sa nir_variable_mode
166 */
167 nir_variable_mode mode;
168
169 /**
170 * Is the variable read-only?
171 *
172 * This is set for variables declared as \c const, shader inputs,
173 * and uniforms.
174 */
175 unsigned read_only:1;
176 unsigned centroid:1;
177 unsigned sample:1;
178 unsigned patch:1;
179 unsigned invariant:1;
180
181 /**
182 * Interpolation mode for shader inputs / outputs
183 *
184 * \sa glsl_interp_mode
185 */
186 unsigned interpolation:2;
187
188 /**
189 * \name ARB_fragment_coord_conventions
190 * @{
191 */
192 unsigned origin_upper_left:1;
193 unsigned pixel_center_integer:1;
194 /*@}*/
195
196 /**
197 * Was the location explicitly set in the shader?
198 *
199 * If the location is explicitly set in the shader, it \b cannot be changed
200 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
201 * no effect).
202 */
203 unsigned explicit_location:1;
204 unsigned explicit_index:1;
205
206 /**
207 * Was an initial binding explicitly set in the shader?
208 *
209 * If so, constant_initializer contains an integer nir_constant
210 * representing the initial binding point.
211 */
212 unsigned explicit_binding:1;
213
214 /**
215 * Does this variable have an initializer?
216 *
217 * This is used by the linker to cross-validiate initializers of global
218 * variables.
219 */
220 unsigned has_initializer:1;
221
222 /**
223 * If non-zero, then this variable may be packed along with other variables
224 * into a single varying slot, so this offset should be applied when
225 * accessing components. For example, an offset of 1 means that the x
226 * component of this variable is actually stored in component y of the
227 * location specified by \c location.
228 */
229 unsigned location_frac:2;
230
231 /**
232 * Whether this is a fragment shader output implicitly initialized with
233 * the previous contents of the specified render target at the
234 * framebuffer location corresponding to this shader invocation.
235 */
236 unsigned fb_fetch_output:1;
237
238 /**
239 * \brief Layout qualifier for gl_FragDepth.
240 *
241 * This is not equal to \c ir_depth_layout_none if and only if this
242 * variable is \c gl_FragDepth and a layout qualifier is specified.
243 */
244 nir_depth_layout depth_layout;
245
246 /**
247 * Storage location of the base of this variable
248 *
249 * The precise meaning of this field depends on the nature of the variable.
250 *
251 * - Vertex shader input: one of the values from \c gl_vert_attrib.
252 * - Vertex shader output: one of the values from \c gl_varying_slot.
253 * - Geometry shader input: one of the values from \c gl_varying_slot.
254 * - Geometry shader output: one of the values from \c gl_varying_slot.
255 * - Fragment shader input: one of the values from \c gl_varying_slot.
256 * - Fragment shader output: one of the values from \c gl_frag_result.
257 * - Uniforms: Per-stage uniform slot number for default uniform block.
258 * - Uniforms: Index within the uniform block definition for UBO members.
259 * - Non-UBO Uniforms: uniform slot number.
260 * - Other: This field is not currently used.
261 *
262 * If the variable is a uniform, shader input, or shader output, and the
263 * slot has not been assigned, the value will be -1.
264 */
265 int location;
266
267 /**
268 * The actual location of the variable in the IR. Only valid for inputs
269 * and outputs.
270 */
271 unsigned int driver_location;
272
273 /**
274 * output index for dual source blending.
275 */
276 int index;
277
278 /**
279 * Descriptor set binding for sampler or UBO.
280 */
281 int descriptor_set;
282
283 /**
284 * Initial binding point for a sampler or UBO.
285 *
286 * For array types, this represents the binding point for the first element.
287 */
288 int binding;
289
290 /**
291 * Location an atomic counter is stored at.
292 */
293 unsigned offset;
294
295 /**
296 * ARB_shader_image_load_store qualifiers.
297 */
298 struct {
299 bool read_only; /**< "readonly" qualifier. */
300 bool write_only; /**< "writeonly" qualifier. */
301 bool coherent;
302 bool _volatile;
303 bool restrict_flag;
304
305 /** Image internal format if specified explicitly, otherwise GL_NONE. */
306 GLenum format;
307 } image;
308
309 /**
310 * Highest element accessed with a constant expression array index
311 *
312 * Not used for non-array variables.
313 */
314 unsigned max_array_access;
315
316 } data;
317
318 /**
319 * Built-in state that backs this uniform
320 *
321 * Once set at variable creation, \c state_slots must remain invariant.
322 * This is because, ideally, this array would be shared by all clones of
323 * this variable in the IR tree. In other words, we'd really like for it
324 * to be a fly-weight.
325 *
326 * If the variable is not a uniform, \c num_state_slots will be zero and
327 * \c state_slots will be \c NULL.
328 */
329 /*@{*/
330 unsigned num_state_slots; /**< Number of state slots used */
331 nir_state_slot *state_slots; /**< State descriptors. */
332 /*@}*/
333
334 /**
335 * Constant expression assigned in the initializer of the variable
336 */
337 nir_constant *constant_initializer;
338
339 /**
340 * For variables that are in an interface block or are an instance of an
341 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
342 *
343 * \sa ir_variable::location
344 */
345 const struct glsl_type *interface_type;
346 } nir_variable;
347
348 #define nir_foreach_variable(var, var_list) \
349 foreach_list_typed(nir_variable, var, node, var_list)
350
351 #define nir_foreach_variable_safe(var, var_list) \
352 foreach_list_typed_safe(nir_variable, var, node, var_list)
353
354 static inline bool
355 nir_variable_is_global(const nir_variable *var)
356 {
357 return var->data.mode != nir_var_local && var->data.mode != nir_var_param;
358 }
359
360 typedef struct nir_register {
361 struct exec_node node;
362
363 unsigned num_components; /** < number of vector components */
364 unsigned num_array_elems; /** < size of array (0 for no array) */
365
366 /* The bit-size of each channel; must be one of 8, 16, 32, or 64 */
367 uint8_t bit_size;
368
369 /** generic register index. */
370 unsigned index;
371
372 /** only for debug purposes, can be NULL */
373 const char *name;
374
375 /** whether this register is local (per-function) or global (per-shader) */
376 bool is_global;
377
378 /**
379 * If this flag is set to true, then accessing channels >= num_components
380 * is well-defined, and simply spills over to the next array element. This
381 * is useful for backends that can do per-component accessing, in
382 * particular scalar backends. By setting this flag and making
383 * num_components equal to 1, structures can be packed tightly into
384 * registers and then registers can be accessed per-component to get to
385 * each structure member, even if it crosses vec4 boundaries.
386 */
387 bool is_packed;
388
389 /** set of nir_src's where this register is used (read from) */
390 struct list_head uses;
391
392 /** set of nir_dest's where this register is defined (written to) */
393 struct list_head defs;
394
395 /** set of nir_if's where this register is used as a condition */
396 struct list_head if_uses;
397 } nir_register;
398
399 typedef enum {
400 nir_instr_type_alu,
401 nir_instr_type_call,
402 nir_instr_type_tex,
403 nir_instr_type_intrinsic,
404 nir_instr_type_load_const,
405 nir_instr_type_jump,
406 nir_instr_type_ssa_undef,
407 nir_instr_type_phi,
408 nir_instr_type_parallel_copy,
409 } nir_instr_type;
410
411 typedef struct nir_instr {
412 struct exec_node node;
413 nir_instr_type type;
414 struct nir_block *block;
415
416 /** generic instruction index. */
417 unsigned index;
418
419 /* A temporary for optimization and analysis passes to use for storing
420 * flags. For instance, DCE uses this to store the "dead/live" info.
421 */
422 uint8_t pass_flags;
423 } nir_instr;
424
425 static inline nir_instr *
426 nir_instr_next(nir_instr *instr)
427 {
428 struct exec_node *next = exec_node_get_next(&instr->node);
429 if (exec_node_is_tail_sentinel(next))
430 return NULL;
431 else
432 return exec_node_data(nir_instr, next, node);
433 }
434
435 static inline nir_instr *
436 nir_instr_prev(nir_instr *instr)
437 {
438 struct exec_node *prev = exec_node_get_prev(&instr->node);
439 if (exec_node_is_head_sentinel(prev))
440 return NULL;
441 else
442 return exec_node_data(nir_instr, prev, node);
443 }
444
445 static inline bool
446 nir_instr_is_first(nir_instr *instr)
447 {
448 return exec_node_is_head_sentinel(exec_node_get_prev(&instr->node));
449 }
450
451 static inline bool
452 nir_instr_is_last(nir_instr *instr)
453 {
454 return exec_node_is_tail_sentinel(exec_node_get_next(&instr->node));
455 }
456
457 typedef struct nir_ssa_def {
458 /** for debugging only, can be NULL */
459 const char* name;
460
461 /** generic SSA definition index. */
462 unsigned index;
463
464 /** Index into the live_in and live_out bitfields */
465 unsigned live_index;
466
467 nir_instr *parent_instr;
468
469 /** set of nir_instr's where this register is used (read from) */
470 struct list_head uses;
471
472 /** set of nir_if's where this register is used as a condition */
473 struct list_head if_uses;
474
475 uint8_t num_components;
476
477 /* The bit-size of each channel; must be one of 8, 16, 32, or 64 */
478 uint8_t bit_size;
479 } nir_ssa_def;
480
481 struct nir_src;
482
483 typedef struct {
484 nir_register *reg;
485 struct nir_src *indirect; /** < NULL for no indirect offset */
486 unsigned base_offset;
487
488 /* TODO use-def chain goes here */
489 } nir_reg_src;
490
491 typedef struct {
492 nir_instr *parent_instr;
493 struct list_head def_link;
494
495 nir_register *reg;
496 struct nir_src *indirect; /** < NULL for no indirect offset */
497 unsigned base_offset;
498
499 /* TODO def-use chain goes here */
500 } nir_reg_dest;
501
502 struct nir_if;
503
504 typedef struct nir_src {
505 union {
506 nir_instr *parent_instr;
507 struct nir_if *parent_if;
508 };
509
510 struct list_head use_link;
511
512 union {
513 nir_reg_src reg;
514 nir_ssa_def *ssa;
515 };
516
517 bool is_ssa;
518 } nir_src;
519
520 static inline nir_src
521 nir_src_init(void)
522 {
523 nir_src src = { { NULL } };
524 return src;
525 }
526
527 #define NIR_SRC_INIT nir_src_init()
528
529 #define nir_foreach_use(src, reg_or_ssa_def) \
530 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
531
532 #define nir_foreach_use_safe(src, reg_or_ssa_def) \
533 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
534
535 #define nir_foreach_if_use(src, reg_or_ssa_def) \
536 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
537
538 #define nir_foreach_if_use_safe(src, reg_or_ssa_def) \
539 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
540
541 typedef struct {
542 union {
543 nir_reg_dest reg;
544 nir_ssa_def ssa;
545 };
546
547 bool is_ssa;
548 } nir_dest;
549
550 static inline nir_dest
551 nir_dest_init(void)
552 {
553 nir_dest dest = { { { NULL } } };
554 return dest;
555 }
556
557 #define NIR_DEST_INIT nir_dest_init()
558
559 #define nir_foreach_def(dest, reg) \
560 list_for_each_entry(nir_dest, dest, &(reg)->defs, reg.def_link)
561
562 #define nir_foreach_def_safe(dest, reg) \
563 list_for_each_entry_safe(nir_dest, dest, &(reg)->defs, reg.def_link)
564
565 static inline nir_src
566 nir_src_for_ssa(nir_ssa_def *def)
567 {
568 nir_src src = NIR_SRC_INIT;
569
570 src.is_ssa = true;
571 src.ssa = def;
572
573 return src;
574 }
575
576 static inline nir_src
577 nir_src_for_reg(nir_register *reg)
578 {
579 nir_src src = NIR_SRC_INIT;
580
581 src.is_ssa = false;
582 src.reg.reg = reg;
583 src.reg.indirect = NULL;
584 src.reg.base_offset = 0;
585
586 return src;
587 }
588
589 static inline nir_dest
590 nir_dest_for_reg(nir_register *reg)
591 {
592 nir_dest dest = NIR_DEST_INIT;
593
594 dest.reg.reg = reg;
595
596 return dest;
597 }
598
599 static inline unsigned
600 nir_src_bit_size(nir_src src)
601 {
602 return src.is_ssa ? src.ssa->bit_size : src.reg.reg->bit_size;
603 }
604
605 static inline unsigned
606 nir_dest_bit_size(nir_dest dest)
607 {
608 return dest.is_ssa ? dest.ssa.bit_size : dest.reg.reg->bit_size;
609 }
610
611 void nir_src_copy(nir_src *dest, const nir_src *src, void *instr_or_if);
612 void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr);
613
614 typedef struct {
615 nir_src src;
616
617 /**
618 * \name input modifiers
619 */
620 /*@{*/
621 /**
622 * For inputs interpreted as floating point, flips the sign bit. For
623 * inputs interpreted as integers, performs the two's complement negation.
624 */
625 bool negate;
626
627 /**
628 * Clears the sign bit for floating point values, and computes the integer
629 * absolute value for integers. Note that the negate modifier acts after
630 * the absolute value modifier, therefore if both are set then all inputs
631 * will become negative.
632 */
633 bool abs;
634 /*@}*/
635
636 /**
637 * For each input component, says which component of the register it is
638 * chosen from. Note that which elements of the swizzle are used and which
639 * are ignored are based on the write mask for most opcodes - for example,
640 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
641 * a swizzle of {2, x, 1, 0} where x means "don't care."
642 */
643 uint8_t swizzle[4];
644 } nir_alu_src;
645
646 typedef struct {
647 nir_dest dest;
648
649 /**
650 * \name saturate output modifier
651 *
652 * Only valid for opcodes that output floating-point numbers. Clamps the
653 * output to between 0.0 and 1.0 inclusive.
654 */
655
656 bool saturate;
657
658 unsigned write_mask : 4; /* ignored if dest.is_ssa is true */
659 } nir_alu_dest;
660
661 typedef enum {
662 nir_type_invalid = 0, /* Not a valid type */
663 nir_type_float,
664 nir_type_int,
665 nir_type_uint,
666 nir_type_bool,
667 nir_type_bool32 = 32 | nir_type_bool,
668 nir_type_int8 = 8 | nir_type_int,
669 nir_type_int16 = 16 | nir_type_int,
670 nir_type_int32 = 32 | nir_type_int,
671 nir_type_int64 = 64 | nir_type_int,
672 nir_type_uint8 = 8 | nir_type_uint,
673 nir_type_uint16 = 16 | nir_type_uint,
674 nir_type_uint32 = 32 | nir_type_uint,
675 nir_type_uint64 = 64 | nir_type_uint,
676 nir_type_float16 = 16 | nir_type_float,
677 nir_type_float32 = 32 | nir_type_float,
678 nir_type_float64 = 64 | nir_type_float,
679 } nir_alu_type;
680
681 #define NIR_ALU_TYPE_SIZE_MASK 0xfffffff8
682 #define NIR_ALU_TYPE_BASE_TYPE_MASK 0x00000007
683
684 static inline unsigned
685 nir_alu_type_get_type_size(nir_alu_type type)
686 {
687 return type & NIR_ALU_TYPE_SIZE_MASK;
688 }
689
690 static inline unsigned
691 nir_alu_type_get_base_type(nir_alu_type type)
692 {
693 return type & NIR_ALU_TYPE_BASE_TYPE_MASK;
694 }
695
696 typedef enum {
697 NIR_OP_IS_COMMUTATIVE = (1 << 0),
698 NIR_OP_IS_ASSOCIATIVE = (1 << 1),
699 } nir_op_algebraic_property;
700
701 typedef struct {
702 const char *name;
703
704 unsigned num_inputs;
705
706 /**
707 * The number of components in the output
708 *
709 * If non-zero, this is the size of the output and input sizes are
710 * explicitly given; swizzle and writemask are still in effect, but if
711 * the output component is masked out, then the input component may
712 * still be in use.
713 *
714 * If zero, the opcode acts in the standard, per-component manner; the
715 * operation is performed on each component (except the ones that are
716 * masked out) with the input being taken from the input swizzle for
717 * that component.
718 *
719 * The size of some of the inputs may be given (i.e. non-zero) even
720 * though output_size is zero; in that case, the inputs with a zero
721 * size act per-component, while the inputs with non-zero size don't.
722 */
723 unsigned output_size;
724
725 /**
726 * The type of vector that the instruction outputs. Note that the
727 * staurate modifier is only allowed on outputs with the float type.
728 */
729
730 nir_alu_type output_type;
731
732 /**
733 * The number of components in each input
734 */
735 unsigned input_sizes[4];
736
737 /**
738 * The type of vector that each input takes. Note that negate and
739 * absolute value are only allowed on inputs with int or float type and
740 * behave differently on the two.
741 */
742 nir_alu_type input_types[4];
743
744 nir_op_algebraic_property algebraic_properties;
745 } nir_op_info;
746
747 extern const nir_op_info nir_op_infos[nir_num_opcodes];
748
749 typedef struct nir_alu_instr {
750 nir_instr instr;
751 nir_op op;
752
753 /** Indicates that this ALU instruction generates an exact value
754 *
755 * This is kind of a mixture of GLSL "precise" and "invariant" and not
756 * really equivalent to either. This indicates that the value generated by
757 * this operation is high-precision and any code transformations that touch
758 * it must ensure that the resulting value is bit-for-bit identical to the
759 * original.
760 */
761 bool exact;
762
763 nir_alu_dest dest;
764 nir_alu_src src[];
765 } nir_alu_instr;
766
767 void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
768 nir_alu_instr *instr);
769 void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
770 nir_alu_instr *instr);
771
772 /* is this source channel used? */
773 static inline bool
774 nir_alu_instr_channel_used(nir_alu_instr *instr, unsigned src, unsigned channel)
775 {
776 if (nir_op_infos[instr->op].input_sizes[src] > 0)
777 return channel < nir_op_infos[instr->op].input_sizes[src];
778
779 return (instr->dest.write_mask >> channel) & 1;
780 }
781
782 /*
783 * For instructions whose destinations are SSA, get the number of channels
784 * used for a source
785 */
786 static inline unsigned
787 nir_ssa_alu_instr_src_components(const nir_alu_instr *instr, unsigned src)
788 {
789 assert(instr->dest.dest.is_ssa);
790
791 if (nir_op_infos[instr->op].input_sizes[src] > 0)
792 return nir_op_infos[instr->op].input_sizes[src];
793
794 return instr->dest.dest.ssa.num_components;
795 }
796
797 bool nir_alu_srcs_equal(const nir_alu_instr *alu1, const nir_alu_instr *alu2,
798 unsigned src1, unsigned src2);
799
800 typedef enum {
801 nir_deref_type_var,
802 nir_deref_type_array,
803 nir_deref_type_struct
804 } nir_deref_type;
805
806 typedef struct nir_deref {
807 nir_deref_type deref_type;
808 struct nir_deref *child;
809 const struct glsl_type *type;
810 } nir_deref;
811
812 typedef struct {
813 nir_deref deref;
814
815 nir_variable *var;
816 } nir_deref_var;
817
818 /* This enum describes how the array is referenced. If the deref is
819 * direct then the base_offset is used. If the deref is indirect then
820 * offset is given by base_offset + indirect. If the deref is a wildcard
821 * then the deref refers to all of the elements of the array at the same
822 * time. Wildcard dereferences are only ever allowed in copy_var
823 * intrinsics and the source and destination derefs must have matching
824 * wildcards.
825 */
826 typedef enum {
827 nir_deref_array_type_direct,
828 nir_deref_array_type_indirect,
829 nir_deref_array_type_wildcard,
830 } nir_deref_array_type;
831
832 typedef struct {
833 nir_deref deref;
834
835 nir_deref_array_type deref_array_type;
836 unsigned base_offset;
837 nir_src indirect;
838 } nir_deref_array;
839
840 typedef struct {
841 nir_deref deref;
842
843 unsigned index;
844 } nir_deref_struct;
845
846 NIR_DEFINE_CAST(nir_deref_as_var, nir_deref, nir_deref_var, deref,
847 deref_type, nir_deref_type_var)
848 NIR_DEFINE_CAST(nir_deref_as_array, nir_deref, nir_deref_array, deref,
849 deref_type, nir_deref_type_array)
850 NIR_DEFINE_CAST(nir_deref_as_struct, nir_deref, nir_deref_struct, deref,
851 deref_type, nir_deref_type_struct)
852
853 /* Returns the last deref in the chain. */
854 static inline nir_deref *
855 nir_deref_tail(nir_deref *deref)
856 {
857 while (deref->child)
858 deref = deref->child;
859 return deref;
860 }
861
862 typedef struct {
863 nir_instr instr;
864
865 unsigned num_params;
866 nir_deref_var **params;
867 nir_deref_var *return_deref;
868
869 struct nir_function *callee;
870 } nir_call_instr;
871
872 #define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
873 num_variables, num_indices, idx0, idx1, idx2, flags) \
874 nir_intrinsic_##name,
875
876 #define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
877
878 typedef enum {
879 #include "nir_intrinsics.h"
880 nir_num_intrinsics = nir_last_intrinsic + 1
881 } nir_intrinsic_op;
882
883 #define NIR_INTRINSIC_MAX_CONST_INDEX 3
884
885 /** Represents an intrinsic
886 *
887 * An intrinsic is an instruction type for handling things that are
888 * more-or-less regular operations but don't just consume and produce SSA
889 * values like ALU operations do. Intrinsics are not for things that have
890 * special semantic meaning such as phi nodes and parallel copies.
891 * Examples of intrinsics include variable load/store operations, system
892 * value loads, and the like. Even though texturing more-or-less falls
893 * under this category, texturing is its own instruction type because
894 * trying to represent texturing with intrinsics would lead to a
895 * combinatorial explosion of intrinsic opcodes.
896 *
897 * By having a single instruction type for handling a lot of different
898 * cases, optimization passes can look for intrinsics and, for the most
899 * part, completely ignore them. Each intrinsic type also has a few
900 * possible flags that govern whether or not they can be reordered or
901 * eliminated. That way passes like dead code elimination can still work
902 * on intrisics without understanding the meaning of each.
903 *
904 * Each intrinsic has some number of constant indices, some number of
905 * variables, and some number of sources. What these sources, variables,
906 * and indices mean depends on the intrinsic and is documented with the
907 * intrinsic declaration in nir_intrinsics.h. Intrinsics and texture
908 * instructions are the only types of instruction that can operate on
909 * variables.
910 */
911 typedef struct {
912 nir_instr instr;
913
914 nir_intrinsic_op intrinsic;
915
916 nir_dest dest;
917
918 /** number of components if this is a vectorized intrinsic
919 *
920 * Similarly to ALU operations, some intrinsics are vectorized.
921 * An intrinsic is vectorized if nir_intrinsic_infos.dest_components == 0.
922 * For vectorized intrinsics, the num_components field specifies the
923 * number of destination components and the number of source components
924 * for all sources with nir_intrinsic_infos.src_components[i] == 0.
925 */
926 uint8_t num_components;
927
928 int const_index[NIR_INTRINSIC_MAX_CONST_INDEX];
929
930 nir_deref_var *variables[2];
931
932 nir_src src[];
933 } nir_intrinsic_instr;
934
935 /**
936 * \name NIR intrinsics semantic flags
937 *
938 * information about what the compiler can do with the intrinsics.
939 *
940 * \sa nir_intrinsic_info::flags
941 */
942 typedef enum {
943 /**
944 * whether the intrinsic can be safely eliminated if none of its output
945 * value is not being used.
946 */
947 NIR_INTRINSIC_CAN_ELIMINATE = (1 << 0),
948
949 /**
950 * Whether the intrinsic can be reordered with respect to any other
951 * intrinsic, i.e. whether the only reordering dependencies of the
952 * intrinsic are due to the register reads/writes.
953 */
954 NIR_INTRINSIC_CAN_REORDER = (1 << 1),
955 } nir_intrinsic_semantic_flag;
956
957 /**
958 * \name NIR intrinsics const-index flag
959 *
960 * Indicates the usage of a const_index slot.
961 *
962 * \sa nir_intrinsic_info::index_map
963 */
964 typedef enum {
965 /**
966 * Generally instructions that take a offset src argument, can encode
967 * a constant 'base' value which is added to the offset.
968 */
969 NIR_INTRINSIC_BASE = 1,
970
971 /**
972 * For store instructions, a writemask for the store.
973 */
974 NIR_INTRINSIC_WRMASK = 2,
975
976 /**
977 * The stream-id for GS emit_vertex/end_primitive intrinsics.
978 */
979 NIR_INTRINSIC_STREAM_ID = 3,
980
981 /**
982 * The clip-plane id for load_user_clip_plane intrinsic.
983 */
984 NIR_INTRINSIC_UCP_ID = 4,
985
986 /**
987 * The amount of data, starting from BASE, that this instruction may
988 * access. This is used to provide bounds if the offset is not constant.
989 */
990 NIR_INTRINSIC_RANGE = 5,
991
992 /**
993 * The Vulkan descriptor set for vulkan_resource_index intrinsic.
994 */
995 NIR_INTRINSIC_DESC_SET = 6,
996
997 /**
998 * The Vulkan descriptor set binding for vulkan_resource_index intrinsic.
999 */
1000 NIR_INTRINSIC_BINDING = 7,
1001
1002 /**
1003 * Component offset.
1004 */
1005 NIR_INTRINSIC_COMPONENT = 8,
1006
1007 /**
1008 * Interpolation mode (only meaningful for FS inputs).
1009 */
1010 NIR_INTRINSIC_INTERP_MODE = 9,
1011
1012 NIR_INTRINSIC_NUM_INDEX_FLAGS,
1013
1014 } nir_intrinsic_index_flag;
1015
1016 #define NIR_INTRINSIC_MAX_INPUTS 4
1017
1018 typedef struct {
1019 const char *name;
1020
1021 unsigned num_srcs; /** < number of register/SSA inputs */
1022
1023 /** number of components of each input register
1024 *
1025 * If this value is 0, the number of components is given by the
1026 * num_components field of nir_intrinsic_instr.
1027 */
1028 unsigned src_components[NIR_INTRINSIC_MAX_INPUTS];
1029
1030 bool has_dest;
1031
1032 /** number of components of the output register
1033 *
1034 * If this value is 0, the number of components is given by the
1035 * num_components field of nir_intrinsic_instr.
1036 */
1037 unsigned dest_components;
1038
1039 /** the number of inputs/outputs that are variables */
1040 unsigned num_variables;
1041
1042 /** the number of constant indices used by the intrinsic */
1043 unsigned num_indices;
1044
1045 /** indicates the usage of intr->const_index[n] */
1046 unsigned index_map[NIR_INTRINSIC_NUM_INDEX_FLAGS];
1047
1048 /** semantic flags for calls to this intrinsic */
1049 nir_intrinsic_semantic_flag flags;
1050 } nir_intrinsic_info;
1051
1052 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
1053
1054
1055 #define INTRINSIC_IDX_ACCESSORS(name, flag, type) \
1056 static inline type \
1057 nir_intrinsic_##name(nir_intrinsic_instr *instr) \
1058 { \
1059 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
1060 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
1061 return instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1]; \
1062 } \
1063 static inline void \
1064 nir_intrinsic_set_##name(nir_intrinsic_instr *instr, type val) \
1065 { \
1066 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
1067 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
1068 instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1] = val; \
1069 }
1070
1071 INTRINSIC_IDX_ACCESSORS(write_mask, WRMASK, unsigned)
1072 INTRINSIC_IDX_ACCESSORS(base, BASE, int)
1073 INTRINSIC_IDX_ACCESSORS(stream_id, STREAM_ID, unsigned)
1074 INTRINSIC_IDX_ACCESSORS(ucp_id, UCP_ID, unsigned)
1075 INTRINSIC_IDX_ACCESSORS(range, RANGE, unsigned)
1076 INTRINSIC_IDX_ACCESSORS(desc_set, DESC_SET, unsigned)
1077 INTRINSIC_IDX_ACCESSORS(binding, BINDING, unsigned)
1078 INTRINSIC_IDX_ACCESSORS(component, COMPONENT, unsigned)
1079 INTRINSIC_IDX_ACCESSORS(interp_mode, INTERP_MODE, unsigned)
1080
1081 /**
1082 * \group texture information
1083 *
1084 * This gives semantic information about textures which is useful to the
1085 * frontend, the backend, and lowering passes, but not the optimizer.
1086 */
1087
1088 typedef enum {
1089 nir_tex_src_coord,
1090 nir_tex_src_projector,
1091 nir_tex_src_comparitor, /* shadow comparitor */
1092 nir_tex_src_offset,
1093 nir_tex_src_bias,
1094 nir_tex_src_lod,
1095 nir_tex_src_ms_index, /* MSAA sample index */
1096 nir_tex_src_ms_mcs, /* MSAA compression value */
1097 nir_tex_src_ddx,
1098 nir_tex_src_ddy,
1099 nir_tex_src_texture_offset, /* < dynamically uniform indirect offset */
1100 nir_tex_src_sampler_offset, /* < dynamically uniform indirect offset */
1101 nir_tex_src_plane, /* < selects plane for planar textures */
1102 nir_num_tex_src_types
1103 } nir_tex_src_type;
1104
1105 typedef struct {
1106 nir_src src;
1107 nir_tex_src_type src_type;
1108 } nir_tex_src;
1109
1110 typedef enum {
1111 nir_texop_tex, /**< Regular texture look-up */
1112 nir_texop_txb, /**< Texture look-up with LOD bias */
1113 nir_texop_txl, /**< Texture look-up with explicit LOD */
1114 nir_texop_txd, /**< Texture look-up with partial derivatvies */
1115 nir_texop_txf, /**< Texel fetch with explicit LOD */
1116 nir_texop_txf_ms, /**< Multisample texture fetch */
1117 nir_texop_txf_ms_mcs, /**< Multisample compression value fetch */
1118 nir_texop_txs, /**< Texture size */
1119 nir_texop_lod, /**< Texture lod query */
1120 nir_texop_tg4, /**< Texture gather */
1121 nir_texop_query_levels, /**< Texture levels query */
1122 nir_texop_texture_samples, /**< Texture samples query */
1123 nir_texop_samples_identical, /**< Query whether all samples are definitely
1124 * identical.
1125 */
1126 } nir_texop;
1127
1128 typedef struct {
1129 nir_instr instr;
1130
1131 enum glsl_sampler_dim sampler_dim;
1132 nir_alu_type dest_type;
1133
1134 nir_texop op;
1135 nir_dest dest;
1136 nir_tex_src *src;
1137 unsigned num_srcs, coord_components;
1138 bool is_array, is_shadow;
1139
1140 /**
1141 * If is_shadow is true, whether this is the old-style shadow that outputs 4
1142 * components or the new-style shadow that outputs 1 component.
1143 */
1144 bool is_new_style_shadow;
1145
1146 /* gather component selector */
1147 unsigned component : 2;
1148
1149 /** The texture index
1150 *
1151 * If this texture instruction has a nir_tex_src_texture_offset source,
1152 * then the texture index is given by texture_index + texture_offset.
1153 */
1154 unsigned texture_index;
1155
1156 /** The size of the texture array or 0 if it's not an array */
1157 unsigned texture_array_size;
1158
1159 /** The texture deref
1160 *
1161 * If this is null, use texture_index instead.
1162 */
1163 nir_deref_var *texture;
1164
1165 /** The sampler index
1166 *
1167 * The following operations do not require a sampler and, as such, this
1168 * field should be ignored:
1169 * - nir_texop_txf
1170 * - nir_texop_txf_ms
1171 * - nir_texop_txs
1172 * - nir_texop_lod
1173 * - nir_texop_tg4
1174 * - nir_texop_query_levels
1175 * - nir_texop_texture_samples
1176 * - nir_texop_samples_identical
1177 *
1178 * If this texture instruction has a nir_tex_src_sampler_offset source,
1179 * then the sampler index is given by sampler_index + sampler_offset.
1180 */
1181 unsigned sampler_index;
1182
1183 /** The sampler deref
1184 *
1185 * If this is null, use sampler_index instead.
1186 */
1187 nir_deref_var *sampler;
1188 } nir_tex_instr;
1189
1190 static inline unsigned
1191 nir_tex_instr_dest_size(nir_tex_instr *instr)
1192 {
1193 switch (instr->op) {
1194 case nir_texop_txs: {
1195 unsigned ret;
1196 switch (instr->sampler_dim) {
1197 case GLSL_SAMPLER_DIM_1D:
1198 case GLSL_SAMPLER_DIM_BUF:
1199 ret = 1;
1200 break;
1201 case GLSL_SAMPLER_DIM_2D:
1202 case GLSL_SAMPLER_DIM_CUBE:
1203 case GLSL_SAMPLER_DIM_MS:
1204 case GLSL_SAMPLER_DIM_RECT:
1205 case GLSL_SAMPLER_DIM_EXTERNAL:
1206 case GLSL_SAMPLER_DIM_SUBPASS:
1207 ret = 2;
1208 break;
1209 case GLSL_SAMPLER_DIM_3D:
1210 ret = 3;
1211 break;
1212 default:
1213 unreachable("not reached");
1214 }
1215 if (instr->is_array)
1216 ret++;
1217 return ret;
1218 }
1219
1220 case nir_texop_lod:
1221 return 2;
1222
1223 case nir_texop_texture_samples:
1224 case nir_texop_query_levels:
1225 case nir_texop_samples_identical:
1226 return 1;
1227
1228 default:
1229 if (instr->is_shadow && instr->is_new_style_shadow)
1230 return 1;
1231
1232 return 4;
1233 }
1234 }
1235
1236 /* Returns true if this texture operation queries something about the texture
1237 * rather than actually sampling it.
1238 */
1239 static inline bool
1240 nir_tex_instr_is_query(nir_tex_instr *instr)
1241 {
1242 switch (instr->op) {
1243 case nir_texop_txs:
1244 case nir_texop_lod:
1245 case nir_texop_texture_samples:
1246 case nir_texop_query_levels:
1247 case nir_texop_txf_ms_mcs:
1248 return true;
1249 case nir_texop_tex:
1250 case nir_texop_txb:
1251 case nir_texop_txl:
1252 case nir_texop_txd:
1253 case nir_texop_txf:
1254 case nir_texop_txf_ms:
1255 case nir_texop_tg4:
1256 return false;
1257 default:
1258 unreachable("Invalid texture opcode");
1259 }
1260 }
1261
1262 static inline nir_alu_type
1263 nir_tex_instr_src_type(nir_tex_instr *instr, unsigned src)
1264 {
1265 switch (instr->src[src].src_type) {
1266 case nir_tex_src_coord:
1267 switch (instr->op) {
1268 case nir_texop_txf:
1269 case nir_texop_txf_ms:
1270 case nir_texop_txf_ms_mcs:
1271 case nir_texop_samples_identical:
1272 return nir_type_int;
1273
1274 default:
1275 return nir_type_float;
1276 }
1277
1278 case nir_tex_src_lod:
1279 switch (instr->op) {
1280 case nir_texop_txs:
1281 case nir_texop_txf:
1282 return nir_type_int;
1283
1284 default:
1285 return nir_type_float;
1286 }
1287
1288 case nir_tex_src_projector:
1289 case nir_tex_src_comparitor:
1290 case nir_tex_src_bias:
1291 case nir_tex_src_ddx:
1292 case nir_tex_src_ddy:
1293 return nir_type_float;
1294
1295 case nir_tex_src_offset:
1296 case nir_tex_src_ms_index:
1297 case nir_tex_src_texture_offset:
1298 case nir_tex_src_sampler_offset:
1299 return nir_type_int;
1300
1301 default:
1302 unreachable("Invalid texture source type");
1303 }
1304 }
1305
1306 static inline unsigned
1307 nir_tex_instr_src_size(nir_tex_instr *instr, unsigned src)
1308 {
1309 if (instr->src[src].src_type == nir_tex_src_coord)
1310 return instr->coord_components;
1311
1312 /* The MCS value is expected to be a vec4 returned by a txf_ms_mcs */
1313 if (instr->src[src].src_type == nir_tex_src_ms_mcs)
1314 return 4;
1315
1316 if (instr->src[src].src_type == nir_tex_src_offset ||
1317 instr->src[src].src_type == nir_tex_src_ddx ||
1318 instr->src[src].src_type == nir_tex_src_ddy) {
1319 if (instr->is_array)
1320 return instr->coord_components - 1;
1321 else
1322 return instr->coord_components;
1323 }
1324
1325 return 1;
1326 }
1327
1328 static inline int
1329 nir_tex_instr_src_index(nir_tex_instr *instr, nir_tex_src_type type)
1330 {
1331 for (unsigned i = 0; i < instr->num_srcs; i++)
1332 if (instr->src[i].src_type == type)
1333 return (int) i;
1334
1335 return -1;
1336 }
1337
1338 void nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx);
1339
1340 typedef union {
1341 float f32[4];
1342 double f64[4];
1343 int32_t i32[4];
1344 uint32_t u32[4];
1345 int64_t i64[4];
1346 uint64_t u64[4];
1347 } nir_const_value;
1348
1349 typedef struct {
1350 nir_instr instr;
1351
1352 nir_const_value value;
1353
1354 nir_ssa_def def;
1355 } nir_load_const_instr;
1356
1357 typedef enum {
1358 nir_jump_return,
1359 nir_jump_break,
1360 nir_jump_continue,
1361 } nir_jump_type;
1362
1363 typedef struct {
1364 nir_instr instr;
1365 nir_jump_type type;
1366 } nir_jump_instr;
1367
1368 /* creates a new SSA variable in an undefined state */
1369
1370 typedef struct {
1371 nir_instr instr;
1372 nir_ssa_def def;
1373 } nir_ssa_undef_instr;
1374
1375 typedef struct {
1376 struct exec_node node;
1377
1378 /* The predecessor block corresponding to this source */
1379 struct nir_block *pred;
1380
1381 nir_src src;
1382 } nir_phi_src;
1383
1384 #define nir_foreach_phi_src(phi_src, phi) \
1385 foreach_list_typed(nir_phi_src, phi_src, node, &(phi)->srcs)
1386 #define nir_foreach_phi_src_safe(phi_src, phi) \
1387 foreach_list_typed_safe(nir_phi_src, phi_src, node, &(phi)->srcs)
1388
1389 typedef struct {
1390 nir_instr instr;
1391
1392 struct exec_list srcs; /** < list of nir_phi_src */
1393
1394 nir_dest dest;
1395 } nir_phi_instr;
1396
1397 typedef struct {
1398 struct exec_node node;
1399 nir_src src;
1400 nir_dest dest;
1401 } nir_parallel_copy_entry;
1402
1403 #define nir_foreach_parallel_copy_entry(entry, pcopy) \
1404 foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
1405
1406 typedef struct {
1407 nir_instr instr;
1408
1409 /* A list of nir_parallel_copy_entry's. The sources of all of the
1410 * entries are copied to the corresponding destinations "in parallel".
1411 * In other words, if we have two entries: a -> b and b -> a, the values
1412 * get swapped.
1413 */
1414 struct exec_list entries;
1415 } nir_parallel_copy_instr;
1416
1417 NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr,
1418 type, nir_instr_type_alu)
1419 NIR_DEFINE_CAST(nir_instr_as_call, nir_instr, nir_call_instr, instr,
1420 type, nir_instr_type_call)
1421 NIR_DEFINE_CAST(nir_instr_as_jump, nir_instr, nir_jump_instr, instr,
1422 type, nir_instr_type_jump)
1423 NIR_DEFINE_CAST(nir_instr_as_tex, nir_instr, nir_tex_instr, instr,
1424 type, nir_instr_type_tex)
1425 NIR_DEFINE_CAST(nir_instr_as_intrinsic, nir_instr, nir_intrinsic_instr, instr,
1426 type, nir_instr_type_intrinsic)
1427 NIR_DEFINE_CAST(nir_instr_as_load_const, nir_instr, nir_load_const_instr, instr,
1428 type, nir_instr_type_load_const)
1429 NIR_DEFINE_CAST(nir_instr_as_ssa_undef, nir_instr, nir_ssa_undef_instr, instr,
1430 type, nir_instr_type_ssa_undef)
1431 NIR_DEFINE_CAST(nir_instr_as_phi, nir_instr, nir_phi_instr, instr,
1432 type, nir_instr_type_phi)
1433 NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
1434 nir_parallel_copy_instr, instr,
1435 type, nir_instr_type_parallel_copy)
1436
1437 /*
1438 * Control flow
1439 *
1440 * Control flow consists of a tree of control flow nodes, which include
1441 * if-statements and loops. The leaves of the tree are basic blocks, lists of
1442 * instructions that always run start-to-finish. Each basic block also keeps
1443 * track of its successors (blocks which may run immediately after the current
1444 * block) and predecessors (blocks which could have run immediately before the
1445 * current block). Each function also has a start block and an end block which
1446 * all return statements point to (which is always empty). Together, all the
1447 * blocks with their predecessors and successors make up the control flow
1448 * graph (CFG) of the function. There are helpers that modify the tree of
1449 * control flow nodes while modifying the CFG appropriately; these should be
1450 * used instead of modifying the tree directly.
1451 */
1452
1453 typedef enum {
1454 nir_cf_node_block,
1455 nir_cf_node_if,
1456 nir_cf_node_loop,
1457 nir_cf_node_function
1458 } nir_cf_node_type;
1459
1460 typedef struct nir_cf_node {
1461 struct exec_node node;
1462 nir_cf_node_type type;
1463 struct nir_cf_node *parent;
1464 } nir_cf_node;
1465
1466 typedef struct nir_block {
1467 nir_cf_node cf_node;
1468
1469 struct exec_list instr_list; /** < list of nir_instr */
1470
1471 /** generic block index; generated by nir_index_blocks */
1472 unsigned index;
1473
1474 /*
1475 * Each block can only have up to 2 successors, so we put them in a simple
1476 * array - no need for anything more complicated.
1477 */
1478 struct nir_block *successors[2];
1479
1480 /* Set of nir_block predecessors in the CFG */
1481 struct set *predecessors;
1482
1483 /*
1484 * this node's immediate dominator in the dominance tree - set to NULL for
1485 * the start block.
1486 */
1487 struct nir_block *imm_dom;
1488
1489 /* This node's children in the dominance tree */
1490 unsigned num_dom_children;
1491 struct nir_block **dom_children;
1492
1493 /* Set of nir_block's on the dominance frontier of this block */
1494 struct set *dom_frontier;
1495
1496 /*
1497 * These two indices have the property that dom_{pre,post}_index for each
1498 * child of this block in the dominance tree will always be between
1499 * dom_pre_index and dom_post_index for this block, which makes testing if
1500 * a given block is dominated by another block an O(1) operation.
1501 */
1502 unsigned dom_pre_index, dom_post_index;
1503
1504 /* live in and out for this block; used for liveness analysis */
1505 BITSET_WORD *live_in;
1506 BITSET_WORD *live_out;
1507 } nir_block;
1508
1509 static inline nir_instr *
1510 nir_block_first_instr(nir_block *block)
1511 {
1512 struct exec_node *head = exec_list_get_head(&block->instr_list);
1513 return exec_node_data(nir_instr, head, node);
1514 }
1515
1516 static inline nir_instr *
1517 nir_block_last_instr(nir_block *block)
1518 {
1519 struct exec_node *tail = exec_list_get_tail(&block->instr_list);
1520 return exec_node_data(nir_instr, tail, node);
1521 }
1522
1523 #define nir_foreach_instr(instr, block) \
1524 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
1525 #define nir_foreach_instr_reverse(instr, block) \
1526 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
1527 #define nir_foreach_instr_safe(instr, block) \
1528 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
1529 #define nir_foreach_instr_reverse_safe(instr, block) \
1530 foreach_list_typed_reverse_safe(nir_instr, instr, node, &(block)->instr_list)
1531
1532 typedef struct nir_if {
1533 nir_cf_node cf_node;
1534 nir_src condition;
1535
1536 struct exec_list then_list; /** < list of nir_cf_node */
1537 struct exec_list else_list; /** < list of nir_cf_node */
1538 } nir_if;
1539
1540 static inline nir_cf_node *
1541 nir_if_first_then_node(nir_if *if_stmt)
1542 {
1543 struct exec_node *head = exec_list_get_head(&if_stmt->then_list);
1544 return exec_node_data(nir_cf_node, head, node);
1545 }
1546
1547 static inline nir_cf_node *
1548 nir_if_last_then_node(nir_if *if_stmt)
1549 {
1550 struct exec_node *tail = exec_list_get_tail(&if_stmt->then_list);
1551 return exec_node_data(nir_cf_node, tail, node);
1552 }
1553
1554 static inline nir_cf_node *
1555 nir_if_first_else_node(nir_if *if_stmt)
1556 {
1557 struct exec_node *head = exec_list_get_head(&if_stmt->else_list);
1558 return exec_node_data(nir_cf_node, head, node);
1559 }
1560
1561 static inline nir_cf_node *
1562 nir_if_last_else_node(nir_if *if_stmt)
1563 {
1564 struct exec_node *tail = exec_list_get_tail(&if_stmt->else_list);
1565 return exec_node_data(nir_cf_node, tail, node);
1566 }
1567
1568 typedef struct {
1569 nir_cf_node cf_node;
1570
1571 struct exec_list body; /** < list of nir_cf_node */
1572 } nir_loop;
1573
1574 static inline nir_cf_node *
1575 nir_loop_first_cf_node(nir_loop *loop)
1576 {
1577 return exec_node_data(nir_cf_node, exec_list_get_head(&loop->body), node);
1578 }
1579
1580 static inline nir_cf_node *
1581 nir_loop_last_cf_node(nir_loop *loop)
1582 {
1583 return exec_node_data(nir_cf_node, exec_list_get_tail(&loop->body), node);
1584 }
1585
1586 /**
1587 * Various bits of metadata that can may be created or required by
1588 * optimization and analysis passes
1589 */
1590 typedef enum {
1591 nir_metadata_none = 0x0,
1592 nir_metadata_block_index = 0x1,
1593 nir_metadata_dominance = 0x2,
1594 nir_metadata_live_ssa_defs = 0x4,
1595 nir_metadata_not_properly_reset = 0x8,
1596 } nir_metadata;
1597
1598 typedef struct {
1599 nir_cf_node cf_node;
1600
1601 /** pointer to the function of which this is an implementation */
1602 struct nir_function *function;
1603
1604 struct exec_list body; /** < list of nir_cf_node */
1605
1606 nir_block *end_block;
1607
1608 /** list for all local variables in the function */
1609 struct exec_list locals;
1610
1611 /** array of variables used as parameters */
1612 unsigned num_params;
1613 nir_variable **params;
1614
1615 /** variable used to hold the result of the function */
1616 nir_variable *return_var;
1617
1618 /** list of local registers in the function */
1619 struct exec_list registers;
1620
1621 /** next available local register index */
1622 unsigned reg_alloc;
1623
1624 /** next available SSA value index */
1625 unsigned ssa_alloc;
1626
1627 /* total number of basic blocks, only valid when block_index_dirty = false */
1628 unsigned num_blocks;
1629
1630 nir_metadata valid_metadata;
1631 } nir_function_impl;
1632
1633 ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
1634 nir_start_block(nir_function_impl *impl)
1635 {
1636 return (nir_block *) impl->body.head_sentinel.next;
1637 }
1638
1639 ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
1640 nir_impl_last_block(nir_function_impl *impl)
1641 {
1642 return (nir_block *) impl->body.tail_sentinel.prev;
1643 }
1644
1645 static inline nir_cf_node *
1646 nir_cf_node_next(nir_cf_node *node)
1647 {
1648 struct exec_node *next = exec_node_get_next(&node->node);
1649 if (exec_node_is_tail_sentinel(next))
1650 return NULL;
1651 else
1652 return exec_node_data(nir_cf_node, next, node);
1653 }
1654
1655 static inline nir_cf_node *
1656 nir_cf_node_prev(nir_cf_node *node)
1657 {
1658 struct exec_node *prev = exec_node_get_prev(&node->node);
1659 if (exec_node_is_head_sentinel(prev))
1660 return NULL;
1661 else
1662 return exec_node_data(nir_cf_node, prev, node);
1663 }
1664
1665 static inline bool
1666 nir_cf_node_is_first(const nir_cf_node *node)
1667 {
1668 return exec_node_is_head_sentinel(node->node.prev);
1669 }
1670
1671 static inline bool
1672 nir_cf_node_is_last(const nir_cf_node *node)
1673 {
1674 return exec_node_is_tail_sentinel(node->node.next);
1675 }
1676
1677 NIR_DEFINE_CAST(nir_cf_node_as_block, nir_cf_node, nir_block, cf_node,
1678 type, nir_cf_node_block)
1679 NIR_DEFINE_CAST(nir_cf_node_as_if, nir_cf_node, nir_if, cf_node,
1680 type, nir_cf_node_if)
1681 NIR_DEFINE_CAST(nir_cf_node_as_loop, nir_cf_node, nir_loop, cf_node,
1682 type, nir_cf_node_loop)
1683 NIR_DEFINE_CAST(nir_cf_node_as_function, nir_cf_node,
1684 nir_function_impl, cf_node, type, nir_cf_node_function)
1685
1686 typedef enum {
1687 nir_parameter_in,
1688 nir_parameter_out,
1689 nir_parameter_inout,
1690 } nir_parameter_type;
1691
1692 typedef struct {
1693 nir_parameter_type param_type;
1694 const struct glsl_type *type;
1695 } nir_parameter;
1696
1697 typedef struct nir_function {
1698 struct exec_node node;
1699
1700 const char *name;
1701 struct nir_shader *shader;
1702
1703 unsigned num_params;
1704 nir_parameter *params;
1705 const struct glsl_type *return_type;
1706
1707 /** The implementation of this function.
1708 *
1709 * If the function is only declared and not implemented, this is NULL.
1710 */
1711 nir_function_impl *impl;
1712 } nir_function;
1713
1714 typedef struct nir_shader_compiler_options {
1715 bool lower_fdiv;
1716 bool lower_ffma;
1717 bool fuse_ffma;
1718 bool lower_flrp32;
1719 /** Lowers flrp when it does not support doubles */
1720 bool lower_flrp64;
1721 bool lower_fpow;
1722 bool lower_fsat;
1723 bool lower_fsqrt;
1724 bool lower_fmod32;
1725 bool lower_fmod64;
1726 bool lower_bitfield_extract;
1727 bool lower_bitfield_insert;
1728 bool lower_uadd_carry;
1729 bool lower_usub_borrow;
1730 /** lowers fneg and ineg to fsub and isub. */
1731 bool lower_negate;
1732 /** lowers fsub and isub to fadd+fneg and iadd+ineg. */
1733 bool lower_sub;
1734
1735 /* lower {slt,sge,seq,sne} to {flt,fge,feq,fne} + b2f: */
1736 bool lower_scmp;
1737
1738 /** enables rules to lower idiv by power-of-two: */
1739 bool lower_idiv;
1740
1741 /* Does the native fdot instruction replicate its result for four
1742 * components? If so, then opt_algebraic_late will turn all fdotN
1743 * instructions into fdot_replicatedN instructions.
1744 */
1745 bool fdot_replicates;
1746
1747 /** lowers ffract to fsub+ffloor: */
1748 bool lower_ffract;
1749
1750 bool lower_pack_half_2x16;
1751 bool lower_pack_unorm_2x16;
1752 bool lower_pack_snorm_2x16;
1753 bool lower_pack_unorm_4x8;
1754 bool lower_pack_snorm_4x8;
1755 bool lower_unpack_half_2x16;
1756 bool lower_unpack_unorm_2x16;
1757 bool lower_unpack_snorm_2x16;
1758 bool lower_unpack_unorm_4x8;
1759 bool lower_unpack_snorm_4x8;
1760
1761 bool lower_extract_byte;
1762 bool lower_extract_word;
1763
1764 /**
1765 * Does the driver support real 32-bit integers? (Otherwise, integers
1766 * are simulated by floats.)
1767 */
1768 bool native_integers;
1769
1770 /* Indicates that the driver only has zero-based vertex id */
1771 bool vertex_id_zero_based;
1772
1773 bool lower_cs_local_index_from_id;
1774
1775 /**
1776 * Should nir_lower_io() create load_interpolated_input intrinsics?
1777 *
1778 * If not, it generates regular load_input intrinsics and interpolation
1779 * information must be inferred from the list of input nir_variables.
1780 */
1781 bool use_interpolated_input_intrinsics;
1782 } nir_shader_compiler_options;
1783
1784 typedef struct nir_shader_info {
1785 const char *name;
1786
1787 /* Descriptive name provided by the client; may be NULL */
1788 const char *label;
1789
1790 /* Number of textures used by this shader */
1791 unsigned num_textures;
1792 /* Number of uniform buffers used by this shader */
1793 unsigned num_ubos;
1794 /* Number of atomic buffers used by this shader */
1795 unsigned num_abos;
1796 /* Number of shader storage buffers used by this shader */
1797 unsigned num_ssbos;
1798 /* Number of images used by this shader */
1799 unsigned num_images;
1800
1801 /* Which inputs are actually read */
1802 uint64_t inputs_read;
1803 /* Which inputs are actually read and are double */
1804 uint64_t double_inputs_read;
1805 /* Which outputs are actually written */
1806 uint64_t outputs_written;
1807 /* Which outputs are actually read */
1808 uint64_t outputs_read;
1809 /* Which system values are actually read */
1810 uint64_t system_values_read;
1811
1812 /* Which patch inputs are actually read */
1813 uint32_t patch_inputs_read;
1814 /* Which patch outputs are actually written */
1815 uint32_t patch_outputs_written;
1816
1817 /* Whether or not this shader ever uses textureGather() */
1818 bool uses_texture_gather;
1819
1820 /* Whether or not this shader uses the gl_ClipDistance output */
1821 bool uses_clip_distance_out;
1822
1823 /* Whether or not separate shader objects were used */
1824 bool separate_shader;
1825
1826 /** Was this shader linked with any transform feedback varyings? */
1827 bool has_transform_feedback_varyings;
1828
1829 union {
1830 struct {
1831 /** The number of vertices recieves per input primitive */
1832 unsigned vertices_in;
1833
1834 /** The output primitive type (GL enum value) */
1835 unsigned output_primitive;
1836
1837 /** The maximum number of vertices the geometry shader might write. */
1838 unsigned vertices_out;
1839
1840 /** 1 .. MAX_GEOMETRY_SHADER_INVOCATIONS */
1841 unsigned invocations;
1842
1843 /** Whether or not this shader uses EndPrimitive */
1844 bool uses_end_primitive;
1845
1846 /** Whether or not this shader uses non-zero streams */
1847 bool uses_streams;
1848 } gs;
1849
1850 struct {
1851 bool uses_discard;
1852
1853 /**
1854 * Whether any inputs are declared with the "sample" qualifier.
1855 */
1856 bool uses_sample_qualifier;
1857
1858 /**
1859 * Whether early fragment tests are enabled as defined by
1860 * ARB_shader_image_load_store.
1861 */
1862 bool early_fragment_tests;
1863
1864 /** gl_FragDepth layout for ARB_conservative_depth. */
1865 enum gl_frag_depth_layout depth_layout;
1866 } fs;
1867
1868 struct {
1869 unsigned local_size[3];
1870 } cs;
1871
1872 struct {
1873 /** The number of vertices in the TCS output patch. */
1874 unsigned vertices_out;
1875 } tcs;
1876 };
1877 } nir_shader_info;
1878
1879 typedef struct nir_shader {
1880 /** list of uniforms (nir_variable) */
1881 struct exec_list uniforms;
1882
1883 /** list of inputs (nir_variable) */
1884 struct exec_list inputs;
1885
1886 /** list of outputs (nir_variable) */
1887 struct exec_list outputs;
1888
1889 /** list of shared compute variables (nir_variable) */
1890 struct exec_list shared;
1891
1892 /** Set of driver-specific options for the shader.
1893 *
1894 * The memory for the options is expected to be kept in a single static
1895 * copy by the driver.
1896 */
1897 const struct nir_shader_compiler_options *options;
1898
1899 /** Various bits of compile-time information about a given shader */
1900 struct nir_shader_info info;
1901
1902 /** list of global variables in the shader (nir_variable) */
1903 struct exec_list globals;
1904
1905 /** list of system value variables in the shader (nir_variable) */
1906 struct exec_list system_values;
1907
1908 struct exec_list functions; /** < list of nir_function */
1909
1910 /** list of global register in the shader */
1911 struct exec_list registers;
1912
1913 /** next available global register index */
1914 unsigned reg_alloc;
1915
1916 /**
1917 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
1918 * access plus one
1919 */
1920 unsigned num_inputs, num_uniforms, num_outputs, num_shared;
1921
1922 /** The shader stage, such as MESA_SHADER_VERTEX. */
1923 gl_shader_stage stage;
1924 } nir_shader;
1925
1926 static inline nir_function_impl *
1927 nir_shader_get_entrypoint(nir_shader *shader)
1928 {
1929 assert(exec_list_length(&shader->functions) == 1);
1930 struct exec_node *func_node = exec_list_get_head(&shader->functions);
1931 nir_function *func = exec_node_data(nir_function, func_node, node);
1932 assert(func->return_type == glsl_void_type());
1933 assert(func->num_params == 0);
1934 assert(func->impl);
1935 return func->impl;
1936 }
1937
1938 #define nir_foreach_function(func, shader) \
1939 foreach_list_typed(nir_function, func, node, &(shader)->functions)
1940
1941 nir_shader *nir_shader_create(void *mem_ctx,
1942 gl_shader_stage stage,
1943 const nir_shader_compiler_options *options);
1944
1945 /** creates a register, including assigning it an index and adding it to the list */
1946 nir_register *nir_global_reg_create(nir_shader *shader);
1947
1948 nir_register *nir_local_reg_create(nir_function_impl *impl);
1949
1950 void nir_reg_remove(nir_register *reg);
1951
1952 /** Adds a variable to the appropreate list in nir_shader */
1953 void nir_shader_add_variable(nir_shader *shader, nir_variable *var);
1954
1955 static inline void
1956 nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)
1957 {
1958 assert(var->data.mode == nir_var_local);
1959 exec_list_push_tail(&impl->locals, &var->node);
1960 }
1961
1962 /** creates a variable, sets a few defaults, and adds it to the list */
1963 nir_variable *nir_variable_create(nir_shader *shader,
1964 nir_variable_mode mode,
1965 const struct glsl_type *type,
1966 const char *name);
1967 /** creates a local variable and adds it to the list */
1968 nir_variable *nir_local_variable_create(nir_function_impl *impl,
1969 const struct glsl_type *type,
1970 const char *name);
1971
1972 /** creates a function and adds it to the shader's list of functions */
1973 nir_function *nir_function_create(nir_shader *shader, const char *name);
1974
1975 nir_function_impl *nir_function_impl_create(nir_function *func);
1976 /** creates a function_impl that isn't tied to any particular function */
1977 nir_function_impl *nir_function_impl_create_bare(nir_shader *shader);
1978
1979 nir_block *nir_block_create(nir_shader *shader);
1980 nir_if *nir_if_create(nir_shader *shader);
1981 nir_loop *nir_loop_create(nir_shader *shader);
1982
1983 nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
1984
1985 /** requests that the given pieces of metadata be generated */
1986 void nir_metadata_require(nir_function_impl *impl, nir_metadata required);
1987 /** dirties all but the preserved metadata */
1988 void nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved);
1989
1990 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
1991 nir_alu_instr *nir_alu_instr_create(nir_shader *shader, nir_op op);
1992
1993 nir_jump_instr *nir_jump_instr_create(nir_shader *shader, nir_jump_type type);
1994
1995 nir_load_const_instr *nir_load_const_instr_create(nir_shader *shader,
1996 unsigned num_components,
1997 unsigned bit_size);
1998
1999 nir_intrinsic_instr *nir_intrinsic_instr_create(nir_shader *shader,
2000 nir_intrinsic_op op);
2001
2002 nir_call_instr *nir_call_instr_create(nir_shader *shader,
2003 nir_function *callee);
2004
2005 nir_tex_instr *nir_tex_instr_create(nir_shader *shader, unsigned num_srcs);
2006
2007 nir_phi_instr *nir_phi_instr_create(nir_shader *shader);
2008
2009 nir_parallel_copy_instr *nir_parallel_copy_instr_create(nir_shader *shader);
2010
2011 nir_ssa_undef_instr *nir_ssa_undef_instr_create(nir_shader *shader,
2012 unsigned num_components,
2013 unsigned bit_size);
2014
2015 nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
2016 nir_deref_array *nir_deref_array_create(void *mem_ctx);
2017 nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
2018
2019 nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
2020
2021 typedef bool (*nir_deref_foreach_leaf_cb)(nir_deref_var *deref, void *state);
2022 bool nir_deref_foreach_leaf(nir_deref_var *deref,
2023 nir_deref_foreach_leaf_cb cb, void *state);
2024
2025 nir_load_const_instr *
2026 nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref);
2027
2028 /**
2029 * NIR Cursors and Instruction Insertion API
2030 * @{
2031 *
2032 * A tiny struct representing a point to insert/extract instructions or
2033 * control flow nodes. Helps reduce the combinatorial explosion of possible
2034 * points to insert/extract.
2035 *
2036 * \sa nir_control_flow.h
2037 */
2038 typedef enum {
2039 nir_cursor_before_block,
2040 nir_cursor_after_block,
2041 nir_cursor_before_instr,
2042 nir_cursor_after_instr,
2043 } nir_cursor_option;
2044
2045 typedef struct {
2046 nir_cursor_option option;
2047 union {
2048 nir_block *block;
2049 nir_instr *instr;
2050 };
2051 } nir_cursor;
2052
2053 static inline nir_block *
2054 nir_cursor_current_block(nir_cursor cursor)
2055 {
2056 if (cursor.option == nir_cursor_before_instr ||
2057 cursor.option == nir_cursor_after_instr) {
2058 return cursor.instr->block;
2059 } else {
2060 return cursor.block;
2061 }
2062 }
2063
2064 bool nir_cursors_equal(nir_cursor a, nir_cursor b);
2065
2066 static inline nir_cursor
2067 nir_before_block(nir_block *block)
2068 {
2069 nir_cursor cursor;
2070 cursor.option = nir_cursor_before_block;
2071 cursor.block = block;
2072 return cursor;
2073 }
2074
2075 static inline nir_cursor
2076 nir_after_block(nir_block *block)
2077 {
2078 nir_cursor cursor;
2079 cursor.option = nir_cursor_after_block;
2080 cursor.block = block;
2081 return cursor;
2082 }
2083
2084 static inline nir_cursor
2085 nir_before_instr(nir_instr *instr)
2086 {
2087 nir_cursor cursor;
2088 cursor.option = nir_cursor_before_instr;
2089 cursor.instr = instr;
2090 return cursor;
2091 }
2092
2093 static inline nir_cursor
2094 nir_after_instr(nir_instr *instr)
2095 {
2096 nir_cursor cursor;
2097 cursor.option = nir_cursor_after_instr;
2098 cursor.instr = instr;
2099 return cursor;
2100 }
2101
2102 static inline nir_cursor
2103 nir_after_block_before_jump(nir_block *block)
2104 {
2105 nir_instr *last_instr = nir_block_last_instr(block);
2106 if (last_instr && last_instr->type == nir_instr_type_jump) {
2107 return nir_before_instr(last_instr);
2108 } else {
2109 return nir_after_block(block);
2110 }
2111 }
2112
2113 static inline nir_cursor
2114 nir_before_cf_node(nir_cf_node *node)
2115 {
2116 if (node->type == nir_cf_node_block)
2117 return nir_before_block(nir_cf_node_as_block(node));
2118
2119 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node)));
2120 }
2121
2122 static inline nir_cursor
2123 nir_after_cf_node(nir_cf_node *node)
2124 {
2125 if (node->type == nir_cf_node_block)
2126 return nir_after_block(nir_cf_node_as_block(node));
2127
2128 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node)));
2129 }
2130
2131 static inline nir_cursor
2132 nir_after_phis(nir_block *block)
2133 {
2134 nir_foreach_instr(instr, block) {
2135 if (instr->type != nir_instr_type_phi)
2136 return nir_before_instr(instr);
2137 }
2138 return nir_after_block(block);
2139 }
2140
2141 static inline nir_cursor
2142 nir_after_cf_node_and_phis(nir_cf_node *node)
2143 {
2144 if (node->type == nir_cf_node_block)
2145 return nir_after_block(nir_cf_node_as_block(node));
2146
2147 nir_block *block = nir_cf_node_as_block(nir_cf_node_next(node));
2148 assert(block->cf_node.type == nir_cf_node_block);
2149
2150 return nir_after_phis(block);
2151 }
2152
2153 static inline nir_cursor
2154 nir_before_cf_list(struct exec_list *cf_list)
2155 {
2156 nir_cf_node *first_node = exec_node_data(nir_cf_node,
2157 exec_list_get_head(cf_list), node);
2158 return nir_before_cf_node(first_node);
2159 }
2160
2161 static inline nir_cursor
2162 nir_after_cf_list(struct exec_list *cf_list)
2163 {
2164 nir_cf_node *last_node = exec_node_data(nir_cf_node,
2165 exec_list_get_tail(cf_list), node);
2166 return nir_after_cf_node(last_node);
2167 }
2168
2169 /**
2170 * Insert a NIR instruction at the given cursor.
2171 *
2172 * Note: This does not update the cursor.
2173 */
2174 void nir_instr_insert(nir_cursor cursor, nir_instr *instr);
2175
2176 static inline void
2177 nir_instr_insert_before(nir_instr *instr, nir_instr *before)
2178 {
2179 nir_instr_insert(nir_before_instr(instr), before);
2180 }
2181
2182 static inline void
2183 nir_instr_insert_after(nir_instr *instr, nir_instr *after)
2184 {
2185 nir_instr_insert(nir_after_instr(instr), after);
2186 }
2187
2188 static inline void
2189 nir_instr_insert_before_block(nir_block *block, nir_instr *before)
2190 {
2191 nir_instr_insert(nir_before_block(block), before);
2192 }
2193
2194 static inline void
2195 nir_instr_insert_after_block(nir_block *block, nir_instr *after)
2196 {
2197 nir_instr_insert(nir_after_block(block), after);
2198 }
2199
2200 static inline void
2201 nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
2202 {
2203 nir_instr_insert(nir_before_cf_node(node), before);
2204 }
2205
2206 static inline void
2207 nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
2208 {
2209 nir_instr_insert(nir_after_cf_node(node), after);
2210 }
2211
2212 static inline void
2213 nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
2214 {
2215 nir_instr_insert(nir_before_cf_list(list), before);
2216 }
2217
2218 static inline void
2219 nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
2220 {
2221 nir_instr_insert(nir_after_cf_list(list), after);
2222 }
2223
2224 void nir_instr_remove(nir_instr *instr);
2225
2226 /** @} */
2227
2228 typedef bool (*nir_foreach_ssa_def_cb)(nir_ssa_def *def, void *state);
2229 typedef bool (*nir_foreach_dest_cb)(nir_dest *dest, void *state);
2230 typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
2231 bool nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb,
2232 void *state);
2233 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
2234 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
2235
2236 nir_const_value *nir_src_as_const_value(nir_src src);
2237 bool nir_src_is_dynamically_uniform(nir_src src);
2238 bool nir_srcs_equal(nir_src src1, nir_src src2);
2239 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
2240 void nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src);
2241 void nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src);
2242 void nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest,
2243 nir_dest new_dest);
2244
2245 void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
2246 unsigned num_components, unsigned bit_size,
2247 const char *name);
2248 void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
2249 unsigned num_components, unsigned bit_size,
2250 const char *name);
2251 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src);
2252 void nir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_src new_src,
2253 nir_instr *after_me);
2254
2255 uint8_t nir_ssa_def_components_read(nir_ssa_def *def);
2256
2257 /*
2258 * finds the next basic block in source-code order, returns NULL if there is
2259 * none
2260 */
2261
2262 nir_block *nir_block_cf_tree_next(nir_block *block);
2263
2264 /* Performs the opposite of nir_block_cf_tree_next() */
2265
2266 nir_block *nir_block_cf_tree_prev(nir_block *block);
2267
2268 /* Gets the first block in a CF node in source-code order */
2269
2270 nir_block *nir_cf_node_cf_tree_first(nir_cf_node *node);
2271
2272 /* Gets the last block in a CF node in source-code order */
2273
2274 nir_block *nir_cf_node_cf_tree_last(nir_cf_node *node);
2275
2276 /* Gets the next block after a CF node in source-code order */
2277
2278 nir_block *nir_cf_node_cf_tree_next(nir_cf_node *node);
2279
2280 /* Macros for loops that visit blocks in source-code order */
2281
2282 #define nir_foreach_block(block, impl) \
2283 for (nir_block *block = nir_start_block(impl); block != NULL; \
2284 block = nir_block_cf_tree_next(block))
2285
2286 #define nir_foreach_block_safe(block, impl) \
2287 for (nir_block *block = nir_start_block(impl), \
2288 *next = nir_block_cf_tree_next(block); \
2289 block != NULL; \
2290 block = next, next = nir_block_cf_tree_next(block))
2291
2292 #define nir_foreach_block_reverse(block, impl) \
2293 for (nir_block *block = nir_impl_last_block(impl); block != NULL; \
2294 block = nir_block_cf_tree_prev(block))
2295
2296 #define nir_foreach_block_reverse_safe(block, impl) \
2297 for (nir_block *block = nir_impl_last_block(impl), \
2298 *prev = nir_block_cf_tree_prev(block); \
2299 block != NULL; \
2300 block = prev, prev = nir_block_cf_tree_prev(block))
2301
2302 #define nir_foreach_block_in_cf_node(block, node) \
2303 for (nir_block *block = nir_cf_node_cf_tree_first(node); \
2304 block != nir_cf_node_cf_tree_next(node); \
2305 block = nir_block_cf_tree_next(block))
2306
2307 /* If the following CF node is an if, this function returns that if.
2308 * Otherwise, it returns NULL.
2309 */
2310 nir_if *nir_block_get_following_if(nir_block *block);
2311
2312 nir_loop *nir_block_get_following_loop(nir_block *block);
2313
2314 void nir_index_local_regs(nir_function_impl *impl);
2315 void nir_index_global_regs(nir_shader *shader);
2316 void nir_index_ssa_defs(nir_function_impl *impl);
2317 unsigned nir_index_instrs(nir_function_impl *impl);
2318
2319 void nir_index_blocks(nir_function_impl *impl);
2320
2321 void nir_print_shader(nir_shader *shader, FILE *fp);
2322 void nir_print_shader_annotated(nir_shader *shader, FILE *fp, struct hash_table *errors);
2323 void nir_print_instr(const nir_instr *instr, FILE *fp);
2324
2325 nir_shader *nir_shader_clone(void *mem_ctx, const nir_shader *s);
2326 nir_function_impl *nir_function_impl_clone(const nir_function_impl *fi);
2327 nir_constant *nir_constant_clone(const nir_constant *c, nir_variable *var);
2328 nir_variable *nir_variable_clone(const nir_variable *c, nir_shader *shader);
2329
2330 #ifdef DEBUG
2331 void nir_validate_shader(nir_shader *shader);
2332 void nir_metadata_set_validation_flag(nir_shader *shader);
2333 void nir_metadata_check_validation_flag(nir_shader *shader);
2334
2335 #include "util/debug.h"
2336 static inline bool
2337 should_clone_nir(void)
2338 {
2339 static int should_clone = -1;
2340 if (should_clone < 0)
2341 should_clone = env_var_as_boolean("NIR_TEST_CLONE", false);
2342
2343 return should_clone;
2344 }
2345 #else
2346 static inline void nir_validate_shader(nir_shader *shader) { (void) shader; }
2347 static inline void nir_metadata_set_validation_flag(nir_shader *shader) { (void) shader; }
2348 static inline void nir_metadata_check_validation_flag(nir_shader *shader) { (void) shader; }
2349 static inline bool should_clone_nir(void) { return false; }
2350 #endif /* DEBUG */
2351
2352 #define _PASS(nir, do_pass) do { \
2353 do_pass \
2354 nir_validate_shader(nir); \
2355 if (should_clone_nir()) { \
2356 nir_shader *clone = nir_shader_clone(ralloc_parent(nir), nir); \
2357 ralloc_free(nir); \
2358 nir = clone; \
2359 } \
2360 } while (0)
2361
2362 #define NIR_PASS(progress, nir, pass, ...) _PASS(nir, \
2363 nir_metadata_set_validation_flag(nir); \
2364 if (pass(nir, ##__VA_ARGS__)) { \
2365 progress = true; \
2366 nir_metadata_check_validation_flag(nir); \
2367 } \
2368 )
2369
2370 #define NIR_PASS_V(nir, pass, ...) _PASS(nir, \
2371 pass(nir, ##__VA_ARGS__); \
2372 )
2373
2374 void nir_calc_dominance_impl(nir_function_impl *impl);
2375 void nir_calc_dominance(nir_shader *shader);
2376
2377 nir_block *nir_dominance_lca(nir_block *b1, nir_block *b2);
2378 bool nir_block_dominates(nir_block *parent, nir_block *child);
2379
2380 void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
2381 void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
2382
2383 void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
2384 void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
2385
2386 void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
2387 void nir_dump_cfg(nir_shader *shader, FILE *fp);
2388
2389 int nir_gs_count_vertices(const nir_shader *shader);
2390
2391 bool nir_split_var_copies(nir_shader *shader);
2392
2393 bool nir_lower_returns_impl(nir_function_impl *impl);
2394 bool nir_lower_returns(nir_shader *shader);
2395
2396 bool nir_inline_functions(nir_shader *shader);
2397
2398 bool nir_propagate_invariant(nir_shader *shader);
2399
2400 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);
2401 void nir_lower_var_copies(nir_shader *shader);
2402
2403 bool nir_lower_global_vars_to_local(nir_shader *shader);
2404
2405 bool nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes);
2406
2407 bool nir_lower_locals_to_regs(nir_shader *shader);
2408
2409 void nir_lower_io_to_temporaries(nir_shader *shader,
2410 nir_function_impl *entrypoint,
2411 bool outputs, bool inputs);
2412
2413 void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);
2414
2415 void nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
2416 unsigned base_offset,
2417 int (*type_size)(const struct glsl_type *));
2418
2419 typedef enum {
2420 /* If set, this forces all non-flat fragment shader inputs to be
2421 * interpolated as if with the "sample" qualifier. This requires
2422 * nir_shader_compiler_options::use_interpolated_input_intrinsics.
2423 */
2424 nir_lower_io_force_sample_interpolation = (1 << 1),
2425 } nir_lower_io_options;
2426 void nir_lower_io(nir_shader *shader,
2427 nir_variable_mode modes,
2428 int (*type_size)(const struct glsl_type *),
2429 nir_lower_io_options);
2430 nir_src *nir_get_io_offset_src(nir_intrinsic_instr *instr);
2431 nir_src *nir_get_io_vertex_index_src(nir_intrinsic_instr *instr);
2432
2433 void nir_lower_io_types(nir_shader *shader);
2434 void nir_lower_vars_to_ssa(nir_shader *shader);
2435
2436 bool nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes);
2437
2438 void nir_move_vec_src_uses_to_dest(nir_shader *shader);
2439 bool nir_lower_vec_to_movs(nir_shader *shader);
2440 bool nir_lower_alu_to_scalar(nir_shader *shader);
2441 void nir_lower_load_const_to_scalar(nir_shader *shader);
2442
2443 bool nir_lower_phis_to_scalar(nir_shader *shader);
2444 void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask);
2445
2446 void nir_lower_samplers(nir_shader *shader,
2447 const struct gl_shader_program *shader_program);
2448
2449 bool nir_lower_system_values(nir_shader *shader);
2450
2451 typedef struct nir_lower_tex_options {
2452 /**
2453 * bitmask of (1 << GLSL_SAMPLER_DIM_x) to control for which
2454 * sampler types a texture projector is lowered.
2455 */
2456 unsigned lower_txp;
2457
2458 /**
2459 * If true, lower away nir_tex_src_offset for all texelfetch instructions.
2460 */
2461 bool lower_txf_offset;
2462
2463 /**
2464 * If true, lower away nir_tex_src_offset for all rect textures.
2465 */
2466 bool lower_rect_offset;
2467
2468 /**
2469 * If true, lower rect textures to 2D, using txs to fetch the
2470 * texture dimensions and dividing the texture coords by the
2471 * texture dims to normalize.
2472 */
2473 bool lower_rect;
2474
2475 /**
2476 * If true, convert yuv to rgb.
2477 */
2478 unsigned lower_y_uv_external;
2479 unsigned lower_y_u_v_external;
2480 unsigned lower_yx_xuxv_external;
2481
2482 /**
2483 * To emulate certain texture wrap modes, this can be used
2484 * to saturate the specified tex coord to [0.0, 1.0]. The
2485 * bits are according to sampler #, ie. if, for example:
2486 *
2487 * (conf->saturate_s & (1 << n))
2488 *
2489 * is true, then the s coord for sampler n is saturated.
2490 *
2491 * Note that clamping must happen *after* projector lowering
2492 * so any projected texture sample instruction with a clamped
2493 * coordinate gets automatically lowered, regardless of the
2494 * 'lower_txp' setting.
2495 */
2496 unsigned saturate_s;
2497 unsigned saturate_t;
2498 unsigned saturate_r;
2499
2500 /* Bitmask of textures that need swizzling.
2501 *
2502 * If (swizzle_result & (1 << texture_index)), then the swizzle in
2503 * swizzles[texture_index] is applied to the result of the texturing
2504 * operation.
2505 */
2506 unsigned swizzle_result;
2507
2508 /* A swizzle for each texture. Values 0-3 represent x, y, z, or w swizzles
2509 * while 4 and 5 represent 0 and 1 respectively.
2510 */
2511 uint8_t swizzles[32][4];
2512
2513 /**
2514 * Bitmap of textures that need srgb to linear conversion. If
2515 * (lower_srgb & (1 << texture_index)) then the rgb (xyz) components
2516 * of the texture are lowered to linear.
2517 */
2518 unsigned lower_srgb;
2519 } nir_lower_tex_options;
2520
2521 bool nir_lower_tex(nir_shader *shader,
2522 const nir_lower_tex_options *options);
2523
2524 bool nir_lower_idiv(nir_shader *shader);
2525
2526 void nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables);
2527 void nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables);
2528
2529 void nir_lower_two_sided_color(nir_shader *shader);
2530
2531 void nir_lower_clamp_color_outputs(nir_shader *shader);
2532
2533 void nir_lower_passthrough_edgeflags(nir_shader *shader);
2534
2535 typedef struct nir_lower_wpos_ytransform_options {
2536 int state_tokens[5];
2537 bool fs_coord_origin_upper_left :1;
2538 bool fs_coord_origin_lower_left :1;
2539 bool fs_coord_pixel_center_integer :1;
2540 bool fs_coord_pixel_center_half_integer :1;
2541 } nir_lower_wpos_ytransform_options;
2542
2543 bool nir_lower_wpos_ytransform(nir_shader *shader,
2544 const nir_lower_wpos_ytransform_options *options);
2545 bool nir_lower_wpos_center(nir_shader *shader);
2546
2547 typedef struct nir_lower_drawpixels_options {
2548 int texcoord_state_tokens[5];
2549 int scale_state_tokens[5];
2550 int bias_state_tokens[5];
2551 unsigned drawpix_sampler;
2552 unsigned pixelmap_sampler;
2553 bool pixel_maps :1;
2554 bool scale_and_bias :1;
2555 } nir_lower_drawpixels_options;
2556
2557 void nir_lower_drawpixels(nir_shader *shader,
2558 const nir_lower_drawpixels_options *options);
2559
2560 typedef struct nir_lower_bitmap_options {
2561 unsigned sampler;
2562 bool swizzle_xxxx;
2563 } nir_lower_bitmap_options;
2564
2565 void nir_lower_bitmap(nir_shader *shader, const nir_lower_bitmap_options *options);
2566
2567 void nir_lower_atomics(nir_shader *shader,
2568 const struct gl_shader_program *shader_program);
2569 void nir_lower_to_source_mods(nir_shader *shader);
2570
2571 bool nir_lower_gs_intrinsics(nir_shader *shader);
2572
2573 typedef enum {
2574 nir_lower_drcp = (1 << 0),
2575 nir_lower_dsqrt = (1 << 1),
2576 nir_lower_drsq = (1 << 2),
2577 nir_lower_dtrunc = (1 << 3),
2578 nir_lower_dfloor = (1 << 4),
2579 nir_lower_dceil = (1 << 5),
2580 nir_lower_dfract = (1 << 6),
2581 nir_lower_dround_even = (1 << 7),
2582 nir_lower_dmod = (1 << 8)
2583 } nir_lower_doubles_options;
2584
2585 void nir_lower_doubles(nir_shader *shader, nir_lower_doubles_options options);
2586 void nir_lower_double_pack(nir_shader *shader);
2587
2588 bool nir_normalize_cubemap_coords(nir_shader *shader);
2589
2590 void nir_live_ssa_defs_impl(nir_function_impl *impl);
2591 bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
2592
2593 void nir_convert_to_ssa_impl(nir_function_impl *impl);
2594 void nir_convert_to_ssa(nir_shader *shader);
2595
2596 bool nir_repair_ssa_impl(nir_function_impl *impl);
2597 bool nir_repair_ssa(nir_shader *shader);
2598
2599 /* If phi_webs_only is true, only convert SSA values involved in phi nodes to
2600 * registers. If false, convert all values (even those not involved in a phi
2601 * node) to registers.
2602 */
2603 void nir_convert_from_ssa(nir_shader *shader, bool phi_webs_only);
2604
2605 bool nir_opt_algebraic(nir_shader *shader);
2606 bool nir_opt_algebraic_late(nir_shader *shader);
2607 bool nir_opt_constant_folding(nir_shader *shader);
2608
2609 bool nir_opt_global_to_local(nir_shader *shader);
2610
2611 bool nir_copy_prop(nir_shader *shader);
2612
2613 bool nir_opt_cse(nir_shader *shader);
2614
2615 bool nir_opt_dce(nir_shader *shader);
2616
2617 bool nir_opt_dead_cf(nir_shader *shader);
2618
2619 bool nir_opt_gcm(nir_shader *shader, bool value_number);
2620
2621 bool nir_opt_peephole_select(nir_shader *shader, unsigned limit);
2622
2623 bool nir_opt_remove_phis(nir_shader *shader);
2624
2625 bool nir_opt_undef(nir_shader *shader);
2626
2627 void nir_sweep(nir_shader *shader);
2628
2629 nir_intrinsic_op nir_intrinsic_from_system_value(gl_system_value val);
2630 gl_system_value nir_system_value_from_intrinsic(nir_intrinsic_op intrin);
2631
2632 #ifdef __cplusplus
2633 } /* extern "C" */
2634 #endif