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