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