2 * Copyright © 2014 Connor Abbott
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:
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
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
24 * Connor Abbott (cwabbott0@gmail.com)
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"
36 #include "util/bitset.h"
37 #include "compiler/nir_types.h"
38 #include "compiler/shader_enums.h"
41 #include "nir_opcodes.h"
48 struct gl_shader_program
;
51 #define NIR_TRUE (~0u)
53 /** Defines a cast function
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.
58 * Note that you have to be a bit careful as the generated cast function
61 #define NIR_DEFINE_CAST(name, in_type, out_type, field) \
62 static inline out_type * \
63 name(const in_type *parent) \
65 return exec_node_data(out_type, parent, field); \
74 * Description of built-in state associated with a uniform
76 * \sa nir_variable::state_slots
90 nir_var_shader_storage
,
96 * Data stored in an nir_constant
98 union nir_constant_data
{
105 typedef struct nir_constant
{
107 * Value of the constant.
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.
113 union nir_constant_data value
;
115 /* we could get this from the var->type but makes clone *much* easier to
116 * not have to care about the type.
118 unsigned num_elements
;
120 /* Array elements / Structure Fields */
121 struct nir_constant
**elements
;
125 * \brief Layout qualifiers for gl_FragDepth.
127 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
128 * with a layout qualifier.
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
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.
143 typedef struct nir_variable
{
144 struct exec_node node
;
147 * Declared type of the variable
149 const struct glsl_type
*type
;
152 * Declared name of the variable
156 struct nir_variable_data
{
159 * Is the variable read-only?
161 * This is set for variables declared as \c const, shader inputs,
164 unsigned read_only
:1;
168 unsigned invariant
:1;
171 * Storage class of the variable.
173 * \sa nir_variable_mode
175 nir_variable_mode mode
:4;
178 * Interpolation mode for shader inputs / outputs
180 * \sa glsl_interp_qualifier
182 unsigned interpolation
:2;
185 * \name ARB_fragment_coord_conventions
188 unsigned origin_upper_left
:1;
189 unsigned pixel_center_integer
:1;
193 * Was the location explicitly set in the shader?
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
199 unsigned explicit_location
:1;
200 unsigned explicit_index
:1;
203 * Was an initial binding explicitly set in the shader?
205 * If so, constant_initializer contains an integer nir_constant
206 * representing the initial binding point.
208 unsigned explicit_binding
:1;
211 * Does this variable have an initializer?
213 * This is used by the linker to cross-validiate initializers of global
216 unsigned has_initializer
:1;
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.
225 unsigned location_frac
:2;
228 * \brief Layout qualifier for gl_FragDepth.
230 * This is not equal to \c ir_depth_layout_none if and only if this
231 * variable is \c gl_FragDepth and a layout qualifier is specified.
233 nir_depth_layout depth_layout
;
236 * Storage location of the base of this variable
238 * The precise meaning of this field depends on the nature of the variable.
240 * - Vertex shader input: one of the values from \c gl_vert_attrib.
241 * - Vertex shader output: one of the values from \c gl_varying_slot.
242 * - Geometry shader input: one of the values from \c gl_varying_slot.
243 * - Geometry shader output: one of the values from \c gl_varying_slot.
244 * - Fragment shader input: one of the values from \c gl_varying_slot.
245 * - Fragment shader output: one of the values from \c gl_frag_result.
246 * - Uniforms: Per-stage uniform slot number for default uniform block.
247 * - Uniforms: Index within the uniform block definition for UBO members.
248 * - Non-UBO Uniforms: uniform slot number.
249 * - Other: This field is not currently used.
251 * If the variable is a uniform, shader input, or shader output, and the
252 * slot has not been assigned, the value will be -1.
257 * The actual location of the variable in the IR. Only valid for inputs
260 unsigned int driver_location
;
263 * output index for dual source blending.
268 * Initial binding point for a sampler or UBO.
270 * For array types, this represents the binding point for the first element.
275 * Location an atomic counter is stored at.
280 * ARB_shader_image_load_store qualifiers.
283 bool read_only
; /**< "readonly" qualifier. */
284 bool write_only
; /**< "writeonly" qualifier. */
289 /** Image internal format if specified explicitly, otherwise GL_NONE. */
294 * Highest element accessed with a constant expression array index
296 * Not used for non-array variables.
298 unsigned max_array_access
;
303 * Built-in state that backs this uniform
305 * Once set at variable creation, \c state_slots must remain invariant.
306 * This is because, ideally, this array would be shared by all clones of
307 * this variable in the IR tree. In other words, we'd really like for it
308 * to be a fly-weight.
310 * If the variable is not a uniform, \c num_state_slots will be zero and
311 * \c state_slots will be \c NULL.
314 unsigned num_state_slots
; /**< Number of state slots used */
315 nir_state_slot
*state_slots
; /**< State descriptors. */
319 * Constant expression assigned in the initializer of the variable
321 nir_constant
*constant_initializer
;
324 * For variables that are in an interface block or are an instance of an
325 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
327 * \sa ir_variable::location
329 const struct glsl_type
*interface_type
;
332 #define nir_foreach_variable(var, var_list) \
333 foreach_list_typed(nir_variable, var, node, var_list)
336 nir_variable_is_global(const nir_variable
*var
)
338 return var
->data
.mode
!= nir_var_local
&& var
->data
.mode
!= nir_var_param
;
341 typedef struct nir_register
{
342 struct exec_node node
;
344 unsigned num_components
; /** < number of vector components */
345 unsigned num_array_elems
; /** < size of array (0 for no array) */
347 /** generic register index. */
350 /** only for debug purposes, can be NULL */
353 /** whether this register is local (per-function) or global (per-shader) */
357 * If this flag is set to true, then accessing channels >= num_components
358 * is well-defined, and simply spills over to the next array element. This
359 * is useful for backends that can do per-component accessing, in
360 * particular scalar backends. By setting this flag and making
361 * num_components equal to 1, structures can be packed tightly into
362 * registers and then registers can be accessed per-component to get to
363 * each structure member, even if it crosses vec4 boundaries.
367 /** set of nir_src's where this register is used (read from) */
368 struct list_head uses
;
370 /** set of nir_dest's where this register is defined (written to) */
371 struct list_head defs
;
373 /** set of nir_if's where this register is used as a condition */
374 struct list_head if_uses
;
381 nir_instr_type_intrinsic
,
382 nir_instr_type_load_const
,
384 nir_instr_type_ssa_undef
,
386 nir_instr_type_parallel_copy
,
389 typedef struct nir_instr
{
390 struct exec_node node
;
392 struct nir_block
*block
;
394 /** generic instruction index. */
397 /* A temporary for optimization and analysis passes to use for storing
398 * flags. For instance, DCE uses this to store the "dead/live" info.
403 static inline nir_instr
*
404 nir_instr_next(nir_instr
*instr
)
406 struct exec_node
*next
= exec_node_get_next(&instr
->node
);
407 if (exec_node_is_tail_sentinel(next
))
410 return exec_node_data(nir_instr
, next
, node
);
413 static inline nir_instr
*
414 nir_instr_prev(nir_instr
*instr
)
416 struct exec_node
*prev
= exec_node_get_prev(&instr
->node
);
417 if (exec_node_is_head_sentinel(prev
))
420 return exec_node_data(nir_instr
, prev
, node
);
424 nir_instr_is_first(nir_instr
*instr
)
426 return exec_node_is_head_sentinel(exec_node_get_prev(&instr
->node
));
430 nir_instr_is_last(nir_instr
*instr
)
432 return exec_node_is_tail_sentinel(exec_node_get_next(&instr
->node
));
435 typedef struct nir_ssa_def
{
436 /** for debugging only, can be NULL */
439 /** generic SSA definition index. */
442 /** Index into the live_in and live_out bitfields */
445 nir_instr
*parent_instr
;
447 /** set of nir_instr's where this register is used (read from) */
448 struct list_head uses
;
450 /** set of nir_if's where this register is used as a condition */
451 struct list_head if_uses
;
453 uint8_t num_components
;
460 struct nir_src
*indirect
; /** < NULL for no indirect offset */
461 unsigned base_offset
;
463 /* TODO use-def chain goes here */
467 nir_instr
*parent_instr
;
468 struct list_head def_link
;
471 struct nir_src
*indirect
; /** < NULL for no indirect offset */
472 unsigned base_offset
;
474 /* TODO def-use chain goes here */
479 typedef struct nir_src
{
481 nir_instr
*parent_instr
;
482 struct nir_if
*parent_if
;
485 struct list_head use_link
;
495 #define NIR_SRC_INIT (nir_src) { { NULL } }
497 #define nir_foreach_use(reg_or_ssa_def, src) \
498 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
500 #define nir_foreach_use_safe(reg_or_ssa_def, src) \
501 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
503 #define nir_foreach_if_use(reg_or_ssa_def, src) \
504 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
506 #define nir_foreach_if_use_safe(reg_or_ssa_def, src) \
507 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
518 #define NIR_DEST_INIT (nir_dest) { { { NULL } } }
520 #define nir_foreach_def(reg, dest) \
521 list_for_each_entry(nir_dest, dest, &(reg)->defs, reg.def_link)
523 #define nir_foreach_def_safe(reg, dest) \
524 list_for_each_entry_safe(nir_dest, dest, &(reg)->defs, reg.def_link)
526 static inline nir_src
527 nir_src_for_ssa(nir_ssa_def
*def
)
529 nir_src src
= NIR_SRC_INIT
;
537 static inline nir_src
538 nir_src_for_reg(nir_register
*reg
)
540 nir_src src
= NIR_SRC_INIT
;
544 src
.reg
.indirect
= NULL
;
545 src
.reg
.base_offset
= 0;
550 static inline nir_dest
551 nir_dest_for_reg(nir_register
*reg
)
553 nir_dest dest
= NIR_DEST_INIT
;
560 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *instr_or_if
);
561 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
);
567 * \name input modifiers
571 * For inputs interpreted as floating point, flips the sign bit. For
572 * inputs interpreted as integers, performs the two's complement negation.
577 * Clears the sign bit for floating point values, and computes the integer
578 * absolute value for integers. Note that the negate modifier acts after
579 * the absolute value modifier, therefore if both are set then all inputs
580 * will become negative.
586 * For each input component, says which component of the register it is
587 * chosen from. Note that which elements of the swizzle are used and which
588 * are ignored are based on the write mask for most opcodes - for example,
589 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
590 * a swizzle of {2, x, 1, 0} where x means "don't care."
599 * \name saturate output modifier
601 * Only valid for opcodes that output floating-point numbers. Clamps the
602 * output to between 0.0 and 1.0 inclusive.
607 unsigned write_mask
: 4; /* ignored if dest.is_ssa is true */
611 nir_type_invalid
= 0, /* Not a valid type */
619 NIR_OP_IS_COMMUTATIVE
= (1 << 0),
620 NIR_OP_IS_ASSOCIATIVE
= (1 << 1),
621 } nir_op_algebraic_property
;
629 * The number of components in the output
631 * If non-zero, this is the size of the output and input sizes are
632 * explicitly given; swizzle and writemask are still in effect, but if
633 * the output component is masked out, then the input component may
636 * If zero, the opcode acts in the standard, per-component manner; the
637 * operation is performed on each component (except the ones that are
638 * masked out) with the input being taken from the input swizzle for
641 * The size of some of the inputs may be given (i.e. non-zero) even
642 * though output_size is zero; in that case, the inputs with a zero
643 * size act per-component, while the inputs with non-zero size don't.
645 unsigned output_size
;
648 * The type of vector that the instruction outputs. Note that the
649 * staurate modifier is only allowed on outputs with the float type.
652 nir_alu_type output_type
;
655 * The number of components in each input
657 unsigned input_sizes
[4];
660 * The type of vector that each input takes. Note that negate and
661 * absolute value are only allowed on inputs with int or float type and
662 * behave differently on the two.
664 nir_alu_type input_types
[4];
666 nir_op_algebraic_property algebraic_properties
;
669 extern const nir_op_info nir_op_infos
[nir_num_opcodes
];
671 typedef struct nir_alu_instr
{
678 void nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
679 nir_alu_instr
*instr
);
680 void nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
681 nir_alu_instr
*instr
);
683 /* is this source channel used? */
685 nir_alu_instr_channel_used(nir_alu_instr
*instr
, unsigned src
, unsigned channel
)
687 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
688 return channel
< nir_op_infos
[instr
->op
].input_sizes
[src
];
690 return (instr
->dest
.write_mask
>> channel
) & 1;
694 * For instructions whose destinations are SSA, get the number of channels
697 static inline unsigned
698 nir_ssa_alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
700 assert(instr
->dest
.dest
.is_ssa
);
702 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
703 return nir_op_infos
[instr
->op
].input_sizes
[src
];
705 return instr
->dest
.dest
.ssa
.num_components
;
710 nir_deref_type_array
,
711 nir_deref_type_struct
714 typedef struct nir_deref
{
715 nir_deref_type deref_type
;
716 struct nir_deref
*child
;
717 const struct glsl_type
*type
;
726 /* This enum describes how the array is referenced. If the deref is
727 * direct then the base_offset is used. If the deref is indirect then then
728 * offset is given by base_offset + indirect. If the deref is a wildcard
729 * then the deref refers to all of the elements of the array at the same
730 * time. Wildcard dereferences are only ever allowed in copy_var
731 * intrinsics and the source and destination derefs must have matching
735 nir_deref_array_type_direct
,
736 nir_deref_array_type_indirect
,
737 nir_deref_array_type_wildcard
,
738 } nir_deref_array_type
;
743 nir_deref_array_type deref_array_type
;
744 unsigned base_offset
;
754 NIR_DEFINE_CAST(nir_deref_as_var
, nir_deref
, nir_deref_var
, deref
)
755 NIR_DEFINE_CAST(nir_deref_as_array
, nir_deref
, nir_deref_array
, deref
)
756 NIR_DEFINE_CAST(nir_deref_as_struct
, nir_deref
, nir_deref_struct
, deref
)
758 /* Returns the last deref in the chain. */
759 static inline nir_deref
*
760 nir_deref_tail(nir_deref
*deref
)
763 deref
= deref
->child
;
771 nir_deref_var
**params
;
772 nir_deref_var
*return_deref
;
774 struct nir_function
*callee
;
777 #define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
778 num_variables, num_indices, idx0, idx1, idx2, flags) \
779 nir_intrinsic_##name,
781 #define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
784 #include "nir_intrinsics.h"
785 nir_num_intrinsics
= nir_last_intrinsic
+ 1
789 #undef LAST_INTRINSIC
791 #define NIR_INTRINSIC_MAX_CONST_INDEX 3
793 /** Represents an intrinsic
795 * An intrinsic is an instruction type for handling things that are
796 * more-or-less regular operations but don't just consume and produce SSA
797 * values like ALU operations do. Intrinsics are not for things that have
798 * special semantic meaning such as phi nodes and parallel copies.
799 * Examples of intrinsics include variable load/store operations, system
800 * value loads, and the like. Even though texturing more-or-less falls
801 * under this category, texturing is its own instruction type because
802 * trying to represent texturing with intrinsics would lead to a
803 * combinatorial explosion of intrinsic opcodes.
805 * By having a single instruction type for handling a lot of different
806 * cases, optimization passes can look for intrinsics and, for the most
807 * part, completely ignore them. Each intrinsic type also has a few
808 * possible flags that govern whether or not they can be reordered or
809 * eliminated. That way passes like dead code elimination can still work
810 * on intrisics without understanding the meaning of each.
812 * Each intrinsic has some number of constant indices, some number of
813 * variables, and some number of sources. What these sources, variables,
814 * and indices mean depends on the intrinsic and is documented with the
815 * intrinsic declaration in nir_intrinsics.h. Intrinsics and texture
816 * instructions are the only types of instruction that can operate on
822 nir_intrinsic_op intrinsic
;
826 /** number of components if this is a vectorized intrinsic
828 * Similarly to ALU operations, some intrinsics are vectorized.
829 * An intrinsic is vectorized if nir_intrinsic_infos.dest_components == 0.
830 * For vectorized intrinsics, the num_components field specifies the
831 * number of destination components and the number of source components
832 * for all sources with nir_intrinsic_infos.src_components[i] == 0.
834 uint8_t num_components
;
836 int const_index
[NIR_INTRINSIC_MAX_CONST_INDEX
];
838 nir_deref_var
*variables
[2];
841 } nir_intrinsic_instr
;
844 * \name NIR intrinsics semantic flags
846 * information about what the compiler can do with the intrinsics.
848 * \sa nir_intrinsic_info::flags
852 * whether the intrinsic can be safely eliminated if none of its output
853 * value is not being used.
855 NIR_INTRINSIC_CAN_ELIMINATE
= (1 << 0),
858 * Whether the intrinsic can be reordered with respect to any other
859 * intrinsic, i.e. whether the only reordering dependencies of the
860 * intrinsic are due to the register reads/writes.
862 NIR_INTRINSIC_CAN_REORDER
= (1 << 1),
863 } nir_intrinsic_semantic_flag
;
866 * \name NIR intrinsics const-index flag
868 * Indicates the usage of a const_index slot.
870 * \sa nir_intrinsic_info::index_map
874 * Generally instructions that take a offset src argument, can encode
875 * a constant 'base' value which is added to the offset.
877 NIR_INTRINSIC_BASE
= 1,
880 * For store instructions, a writemask for the store.
882 NIR_INTRINSIC_WRMASK
= 2,
885 * The stream-id for GS emit_vertex/end_primitive intrinsics.
887 NIR_INTRINSIC_STREAM_ID
= 3,
890 * The clip-plane id for load_user_clip_plane intrinsic.
892 NIR_INTRINSIC_UCP_ID
= 4,
894 NIR_INTRINSIC_NUM_INDEX_FLAGS
,
896 } nir_intrinsic_index_flag
;
898 #define NIR_INTRINSIC_MAX_INPUTS 4
903 unsigned num_srcs
; /** < number of register/SSA inputs */
905 /** number of components of each input register
907 * If this value is 0, the number of components is given by the
908 * num_components field of nir_intrinsic_instr.
910 unsigned src_components
[NIR_INTRINSIC_MAX_INPUTS
];
914 /** number of components of the output register
916 * If this value is 0, the number of components is given by the
917 * num_components field of nir_intrinsic_instr.
919 unsigned dest_components
;
921 /** the number of inputs/outputs that are variables */
922 unsigned num_variables
;
924 /** the number of constant indices used by the intrinsic */
925 unsigned num_indices
;
927 /** indicates the usage of intr->const_index[n] */
928 unsigned index_map
[NIR_INTRINSIC_NUM_INDEX_FLAGS
];
930 /** semantic flags for calls to this intrinsic */
931 nir_intrinsic_semantic_flag flags
;
932 } nir_intrinsic_info
;
934 extern const nir_intrinsic_info nir_intrinsic_infos
[nir_num_intrinsics
];
937 #define INTRINSIC_IDX_ACCESSORS(name, flag, type) \
939 nir_intrinsic_##name(nir_intrinsic_instr *instr) \
941 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
942 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
943 return instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1]; \
946 nir_intrinsic_set_##name(nir_intrinsic_instr *instr, type val) \
948 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
949 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
950 instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1] = val; \
953 INTRINSIC_IDX_ACCESSORS(write_mask
, WRMASK
, unsigned)
954 INTRINSIC_IDX_ACCESSORS(base
, BASE
, int)
955 INTRINSIC_IDX_ACCESSORS(stream_id
, STREAM_ID
, unsigned)
956 INTRINSIC_IDX_ACCESSORS(ucp_id
, UCP_ID
, unsigned)
959 * \group texture information
961 * This gives semantic information about textures which is useful to the
962 * frontend, the backend, and lowering passes, but not the optimizer.
967 nir_tex_src_projector
,
968 nir_tex_src_comparitor
, /* shadow comparitor */
972 nir_tex_src_ms_index
, /* MSAA sample index */
975 nir_tex_src_texture_offset
, /* < dynamically uniform indirect offset */
976 nir_tex_src_sampler_offset
, /* < dynamically uniform indirect offset */
977 nir_num_tex_src_types
982 nir_tex_src_type src_type
;
986 nir_texop_tex
, /**< Regular texture look-up */
987 nir_texop_txb
, /**< Texture look-up with LOD bias */
988 nir_texop_txl
, /**< Texture look-up with explicit LOD */
989 nir_texop_txd
, /**< Texture look-up with partial derivatvies */
990 nir_texop_txf
, /**< Texel fetch with explicit LOD */
991 nir_texop_txf_ms
, /**< Multisample texture fetch */
992 nir_texop_txs
, /**< Texture size */
993 nir_texop_lod
, /**< Texture lod query */
994 nir_texop_tg4
, /**< Texture gather */
995 nir_texop_query_levels
, /**< Texture levels query */
996 nir_texop_texture_samples
, /**< Texture samples query */
997 nir_texop_samples_identical
, /**< Query whether all samples are definitely
1005 enum glsl_sampler_dim sampler_dim
;
1006 nir_alu_type dest_type
;
1011 unsigned num_srcs
, coord_components
;
1012 bool is_array
, is_shadow
;
1015 * If is_shadow is true, whether this is the old-style shadow that outputs 4
1016 * components or the new-style shadow that outputs 1 component.
1018 bool is_new_style_shadow
;
1020 /* gather component selector */
1021 unsigned component
: 2;
1023 /** The texture index
1025 * If this texture instruction has a nir_tex_src_texture_offset source,
1026 * then the texture index is given by texture_index + texture_offset.
1028 unsigned texture_index
;
1030 /** The size of the texture array or 0 if it's not an array */
1031 unsigned texture_array_size
;
1033 /** The texture deref
1035 * If this is null, use texture_index instead.
1037 nir_deref_var
*texture
;
1039 /** The sampler index
1041 * The following operations do not require a sampler and, as such, this
1042 * field should be ignored:
1044 * - nir_texop_txf_ms
1048 * - nir_texop_query_levels
1049 * - nir_texop_texture_samples
1050 * - nir_texop_samples_identical
1052 * If this texture instruction has a nir_tex_src_sampler_offset source,
1053 * then the sampler index is given by sampler_index + sampler_offset.
1055 unsigned sampler_index
;
1057 /** The sampler deref
1059 * If this is null, use sampler_index instead.
1061 nir_deref_var
*sampler
;
1064 static inline unsigned
1065 nir_tex_instr_dest_size(nir_tex_instr
*instr
)
1067 switch (instr
->op
) {
1068 case nir_texop_txs
: {
1070 switch (instr
->sampler_dim
) {
1071 case GLSL_SAMPLER_DIM_1D
:
1072 case GLSL_SAMPLER_DIM_BUF
:
1075 case GLSL_SAMPLER_DIM_2D
:
1076 case GLSL_SAMPLER_DIM_CUBE
:
1077 case GLSL_SAMPLER_DIM_MS
:
1078 case GLSL_SAMPLER_DIM_RECT
:
1079 case GLSL_SAMPLER_DIM_EXTERNAL
:
1082 case GLSL_SAMPLER_DIM_3D
:
1086 unreachable("not reached");
1088 if (instr
->is_array
)
1096 case nir_texop_texture_samples
:
1097 case nir_texop_query_levels
:
1098 case nir_texop_samples_identical
:
1102 if (instr
->is_shadow
&& instr
->is_new_style_shadow
)
1109 /* Returns true if this texture operation queries something about the texture
1110 * rather than actually sampling it.
1113 nir_tex_instr_is_query(nir_tex_instr
*instr
)
1115 switch (instr
->op
) {
1118 case nir_texop_texture_samples
:
1119 case nir_texop_query_levels
:
1126 case nir_texop_txf_ms
:
1130 unreachable("Invalid texture opcode");
1134 static inline unsigned
1135 nir_tex_instr_src_size(nir_tex_instr
*instr
, unsigned src
)
1137 if (instr
->src
[src
].src_type
== nir_tex_src_coord
)
1138 return instr
->coord_components
;
1141 if (instr
->src
[src
].src_type
== nir_tex_src_offset
||
1142 instr
->src
[src
].src_type
== nir_tex_src_ddx
||
1143 instr
->src
[src
].src_type
== nir_tex_src_ddy
) {
1144 if (instr
->is_array
)
1145 return instr
->coord_components
- 1;
1147 return instr
->coord_components
;
1154 nir_tex_instr_src_index(nir_tex_instr
*instr
, nir_tex_src_type type
)
1156 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
1157 if (instr
->src
[i
].src_type
== type
)
1174 nir_const_value value
;
1177 } nir_load_const_instr
;
1190 /* creates a new SSA variable in an undefined state */
1195 } nir_ssa_undef_instr
;
1198 struct exec_node node
;
1200 /* The predecessor block corresponding to this source */
1201 struct nir_block
*pred
;
1206 #define nir_foreach_phi_src(phi, entry) \
1207 foreach_list_typed(nir_phi_src, entry, node, &(phi)->srcs)
1208 #define nir_foreach_phi_src_safe(phi, entry) \
1209 foreach_list_typed_safe(nir_phi_src, entry, node, &(phi)->srcs)
1214 struct exec_list srcs
; /** < list of nir_phi_src */
1220 struct exec_node node
;
1223 } nir_parallel_copy_entry
;
1225 #define nir_foreach_parallel_copy_entry(pcopy, entry) \
1226 foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
1231 /* A list of nir_parallel_copy_entry's. The sources of all of the
1232 * entries are copied to the corresponding destinations "in parallel".
1233 * In other words, if we have two entries: a -> b and b -> a, the values
1236 struct exec_list entries
;
1237 } nir_parallel_copy_instr
;
1239 NIR_DEFINE_CAST(nir_instr_as_alu
, nir_instr
, nir_alu_instr
, instr
)
1240 NIR_DEFINE_CAST(nir_instr_as_call
, nir_instr
, nir_call_instr
, instr
)
1241 NIR_DEFINE_CAST(nir_instr_as_jump
, nir_instr
, nir_jump_instr
, instr
)
1242 NIR_DEFINE_CAST(nir_instr_as_tex
, nir_instr
, nir_tex_instr
, instr
)
1243 NIR_DEFINE_CAST(nir_instr_as_intrinsic
, nir_instr
, nir_intrinsic_instr
, instr
)
1244 NIR_DEFINE_CAST(nir_instr_as_load_const
, nir_instr
, nir_load_const_instr
, instr
)
1245 NIR_DEFINE_CAST(nir_instr_as_ssa_undef
, nir_instr
, nir_ssa_undef_instr
, instr
)
1246 NIR_DEFINE_CAST(nir_instr_as_phi
, nir_instr
, nir_phi_instr
, instr
)
1247 NIR_DEFINE_CAST(nir_instr_as_parallel_copy
, nir_instr
,
1248 nir_parallel_copy_instr
, instr
)
1253 * Control flow consists of a tree of control flow nodes, which include
1254 * if-statements and loops. The leaves of the tree are basic blocks, lists of
1255 * instructions that always run start-to-finish. Each basic block also keeps
1256 * track of its successors (blocks which may run immediately after the current
1257 * block) and predecessors (blocks which could have run immediately before the
1258 * current block). Each function also has a start block and an end block which
1259 * all return statements point to (which is always empty). Together, all the
1260 * blocks with their predecessors and successors make up the control flow
1261 * graph (CFG) of the function. There are helpers that modify the tree of
1262 * control flow nodes while modifying the CFG appropriately; these should be
1263 * used instead of modifying the tree directly.
1270 nir_cf_node_function
1273 typedef struct nir_cf_node
{
1274 struct exec_node node
;
1275 nir_cf_node_type type
;
1276 struct nir_cf_node
*parent
;
1279 typedef struct nir_block
{
1280 nir_cf_node cf_node
;
1282 struct exec_list instr_list
; /** < list of nir_instr */
1284 /** generic block index; generated by nir_index_blocks */
1288 * Each block can only have up to 2 successors, so we put them in a simple
1289 * array - no need for anything more complicated.
1291 struct nir_block
*successors
[2];
1293 /* Set of nir_block predecessors in the CFG */
1294 struct set
*predecessors
;
1297 * this node's immediate dominator in the dominance tree - set to NULL for
1300 struct nir_block
*imm_dom
;
1302 /* This node's children in the dominance tree */
1303 unsigned num_dom_children
;
1304 struct nir_block
**dom_children
;
1306 /* Set of nir_block's on the dominance frontier of this block */
1307 struct set
*dom_frontier
;
1310 * These two indices have the property that dom_{pre,post}_index for each
1311 * child of this block in the dominance tree will always be between
1312 * dom_pre_index and dom_post_index for this block, which makes testing if
1313 * a given block is dominated by another block an O(1) operation.
1315 unsigned dom_pre_index
, dom_post_index
;
1317 /* live in and out for this block; used for liveness analysis */
1318 BITSET_WORD
*live_in
;
1319 BITSET_WORD
*live_out
;
1322 static inline nir_instr
*
1323 nir_block_first_instr(nir_block
*block
)
1325 struct exec_node
*head
= exec_list_get_head(&block
->instr_list
);
1326 return exec_node_data(nir_instr
, head
, node
);
1329 static inline nir_instr
*
1330 nir_block_last_instr(nir_block
*block
)
1332 struct exec_node
*tail
= exec_list_get_tail(&block
->instr_list
);
1333 return exec_node_data(nir_instr
, tail
, node
);
1336 #define nir_foreach_instr(block, instr) \
1337 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
1338 #define nir_foreach_instr_reverse(block, instr) \
1339 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
1340 #define nir_foreach_instr_safe(block, instr) \
1341 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
1342 #define nir_foreach_instr_reverse_safe(block, instr) \
1343 foreach_list_typed_reverse_safe(nir_instr, instr, node, &(block)->instr_list)
1345 typedef struct nir_if
{
1346 nir_cf_node cf_node
;
1349 struct exec_list then_list
; /** < list of nir_cf_node */
1350 struct exec_list else_list
; /** < list of nir_cf_node */
1353 static inline nir_cf_node
*
1354 nir_if_first_then_node(nir_if
*if_stmt
)
1356 struct exec_node
*head
= exec_list_get_head(&if_stmt
->then_list
);
1357 return exec_node_data(nir_cf_node
, head
, node
);
1360 static inline nir_cf_node
*
1361 nir_if_last_then_node(nir_if
*if_stmt
)
1363 struct exec_node
*tail
= exec_list_get_tail(&if_stmt
->then_list
);
1364 return exec_node_data(nir_cf_node
, tail
, node
);
1367 static inline nir_cf_node
*
1368 nir_if_first_else_node(nir_if
*if_stmt
)
1370 struct exec_node
*head
= exec_list_get_head(&if_stmt
->else_list
);
1371 return exec_node_data(nir_cf_node
, head
, node
);
1374 static inline nir_cf_node
*
1375 nir_if_last_else_node(nir_if
*if_stmt
)
1377 struct exec_node
*tail
= exec_list_get_tail(&if_stmt
->else_list
);
1378 return exec_node_data(nir_cf_node
, tail
, node
);
1382 nir_cf_node cf_node
;
1384 struct exec_list body
; /** < list of nir_cf_node */
1387 static inline nir_cf_node
*
1388 nir_loop_first_cf_node(nir_loop
*loop
)
1390 return exec_node_data(nir_cf_node
, exec_list_get_head(&loop
->body
), node
);
1393 static inline nir_cf_node
*
1394 nir_loop_last_cf_node(nir_loop
*loop
)
1396 return exec_node_data(nir_cf_node
, exec_list_get_tail(&loop
->body
), node
);
1400 * Various bits of metadata that can may be created or required by
1401 * optimization and analysis passes
1404 nir_metadata_none
= 0x0,
1405 nir_metadata_block_index
= 0x1,
1406 nir_metadata_dominance
= 0x2,
1407 nir_metadata_live_ssa_defs
= 0x4,
1408 nir_metadata_not_properly_reset
= 0x8,
1412 nir_cf_node cf_node
;
1414 /** pointer to the function of which this is an implementation */
1415 struct nir_function
*function
;
1417 struct exec_list body
; /** < list of nir_cf_node */
1419 nir_block
*end_block
;
1421 /** list for all local variables in the function */
1422 struct exec_list locals
;
1424 /** array of variables used as parameters */
1425 unsigned num_params
;
1426 nir_variable
**params
;
1428 /** variable used to hold the result of the function */
1429 nir_variable
*return_var
;
1431 /** list of local registers in the function */
1432 struct exec_list registers
;
1434 /** next available local register index */
1437 /** next available SSA value index */
1440 /* total number of basic blocks, only valid when block_index_dirty = false */
1441 unsigned num_blocks
;
1443 nir_metadata valid_metadata
;
1444 } nir_function_impl
;
1446 static inline nir_block
*
1447 nir_start_block(nir_function_impl
*impl
)
1449 return (nir_block
*) exec_list_get_head(&impl
->body
);
1452 static inline nir_cf_node
*
1453 nir_cf_node_next(nir_cf_node
*node
)
1455 struct exec_node
*next
= exec_node_get_next(&node
->node
);
1456 if (exec_node_is_tail_sentinel(next
))
1459 return exec_node_data(nir_cf_node
, next
, node
);
1462 static inline nir_cf_node
*
1463 nir_cf_node_prev(nir_cf_node
*node
)
1465 struct exec_node
*prev
= exec_node_get_prev(&node
->node
);
1466 if (exec_node_is_head_sentinel(prev
))
1469 return exec_node_data(nir_cf_node
, prev
, node
);
1473 nir_cf_node_is_first(const nir_cf_node
*node
)
1475 return exec_node_is_head_sentinel(node
->node
.prev
);
1479 nir_cf_node_is_last(const nir_cf_node
*node
)
1481 return exec_node_is_tail_sentinel(node
->node
.next
);
1484 NIR_DEFINE_CAST(nir_cf_node_as_block
, nir_cf_node
, nir_block
, cf_node
)
1485 NIR_DEFINE_CAST(nir_cf_node_as_if
, nir_cf_node
, nir_if
, cf_node
)
1486 NIR_DEFINE_CAST(nir_cf_node_as_loop
, nir_cf_node
, nir_loop
, cf_node
)
1487 NIR_DEFINE_CAST(nir_cf_node_as_function
, nir_cf_node
, nir_function_impl
, cf_node
)
1492 nir_parameter_inout
,
1493 } nir_parameter_type
;
1496 nir_parameter_type param_type
;
1497 const struct glsl_type
*type
;
1500 typedef struct nir_function
{
1501 struct exec_node node
;
1504 struct nir_shader
*shader
;
1506 unsigned num_params
;
1507 nir_parameter
*params
;
1508 const struct glsl_type
*return_type
;
1510 /** The implementation of this function.
1512 * If the function is only declared and not implemented, this is NULL.
1514 nir_function_impl
*impl
;
1517 typedef struct nir_shader_compiler_options
{
1525 bool lower_bitfield_extract
;
1526 bool lower_bitfield_insert
;
1527 bool lower_uadd_carry
;
1528 bool lower_usub_borrow
;
1529 /** lowers fneg and ineg to fsub and isub. */
1531 /** lowers fsub and isub to fadd+fneg and iadd+ineg. */
1534 /* lower {slt,sge,seq,sne} to {flt,fge,feq,fne} + b2f: */
1537 /* Does the native fdot instruction replicate its result for four
1538 * components? If so, then opt_algebraic_late will turn all fdotN
1539 * instructions into fdot_replicatedN instructions.
1541 bool fdot_replicates
;
1543 /** lowers ffract to fsub+ffloor: */
1546 bool lower_pack_half_2x16
;
1547 bool lower_pack_unorm_2x16
;
1548 bool lower_pack_snorm_2x16
;
1549 bool lower_pack_unorm_4x8
;
1550 bool lower_pack_snorm_4x8
;
1551 bool lower_unpack_half_2x16
;
1552 bool lower_unpack_unorm_2x16
;
1553 bool lower_unpack_snorm_2x16
;
1554 bool lower_unpack_unorm_4x8
;
1555 bool lower_unpack_snorm_4x8
;
1557 bool lower_extract_byte
;
1558 bool lower_extract_word
;
1561 * Does the driver support real 32-bit integers? (Otherwise, integers
1562 * are simulated by floats.)
1564 bool native_integers
;
1565 } nir_shader_compiler_options
;
1567 typedef struct nir_shader_info
{
1570 /* Descriptive name provided by the client; may be NULL */
1573 /* Number of textures used by this shader */
1574 unsigned num_textures
;
1575 /* Number of uniform buffers used by this shader */
1577 /* Number of atomic buffers used by this shader */
1579 /* Number of shader storage buffers used by this shader */
1581 /* Number of images used by this shader */
1582 unsigned num_images
;
1584 /* Which inputs are actually read */
1585 uint64_t inputs_read
;
1586 /* Which outputs are actually written */
1587 uint64_t outputs_written
;
1588 /* Which system values are actually read */
1589 uint64_t system_values_read
;
1591 /* Which patch inputs are actually read */
1592 uint32_t patch_inputs_read
;
1593 /* Which patch outputs are actually written */
1594 uint32_t patch_outputs_written
;
1596 /* Whether or not this shader ever uses textureGather() */
1597 bool uses_texture_gather
;
1599 /* Whether or not this shader uses the gl_ClipDistance output */
1600 bool uses_clip_distance_out
;
1602 /* Whether or not separate shader objects were used */
1603 bool separate_shader
;
1605 /** Was this shader linked with any transform feedback varyings? */
1606 bool has_transform_feedback_varyings
;
1610 /** The number of vertices recieves per input primitive */
1611 unsigned vertices_in
;
1613 /** The output primitive type (GL enum value) */
1614 unsigned output_primitive
;
1616 /** The maximum number of vertices the geometry shader might write. */
1617 unsigned vertices_out
;
1619 /** 1 .. MAX_GEOMETRY_SHADER_INVOCATIONS */
1620 unsigned invocations
;
1622 /** Whether or not this shader uses EndPrimitive */
1623 bool uses_end_primitive
;
1625 /** Whether or not this shader uses non-zero streams */
1633 * Whether early fragment tests are enabled as defined by
1634 * ARB_shader_image_load_store.
1636 bool early_fragment_tests
;
1638 /** gl_FragDepth layout for ARB_conservative_depth. */
1639 enum gl_frag_depth_layout depth_layout
;
1643 unsigned local_size
[3];
1647 /** The number of vertices in the TCS output patch. */
1648 unsigned vertices_out
;
1653 typedef struct nir_shader
{
1654 /** list of uniforms (nir_variable) */
1655 struct exec_list uniforms
;
1657 /** list of inputs (nir_variable) */
1658 struct exec_list inputs
;
1660 /** list of outputs (nir_variable) */
1661 struct exec_list outputs
;
1663 /** Set of driver-specific options for the shader.
1665 * The memory for the options is expected to be kept in a single static
1666 * copy by the driver.
1668 const struct nir_shader_compiler_options
*options
;
1670 /** Various bits of compile-time information about a given shader */
1671 struct nir_shader_info info
;
1673 /** list of global variables in the shader (nir_variable) */
1674 struct exec_list globals
;
1676 /** list of system value variables in the shader (nir_variable) */
1677 struct exec_list system_values
;
1679 struct exec_list functions
; /** < list of nir_function */
1681 /** list of global register in the shader */
1682 struct exec_list registers
;
1684 /** next available global register index */
1688 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
1691 unsigned num_inputs
, num_uniforms
, num_outputs
;
1693 /** The shader stage, such as MESA_SHADER_VERTEX. */
1694 gl_shader_stage stage
;
1697 #define nir_foreach_function(shader, func) \
1698 foreach_list_typed(nir_function, func, node, &(shader)->functions)
1700 nir_shader
*nir_shader_create(void *mem_ctx
,
1701 gl_shader_stage stage
,
1702 const nir_shader_compiler_options
*options
);
1704 /** creates a register, including assigning it an index and adding it to the list */
1705 nir_register
*nir_global_reg_create(nir_shader
*shader
);
1707 nir_register
*nir_local_reg_create(nir_function_impl
*impl
);
1709 void nir_reg_remove(nir_register
*reg
);
1711 /** Adds a variable to the appropreate list in nir_shader */
1712 void nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
);
1715 nir_function_impl_add_variable(nir_function_impl
*impl
, nir_variable
*var
)
1717 assert(var
->data
.mode
== nir_var_local
);
1718 exec_list_push_tail(&impl
->locals
, &var
->node
);
1721 /** creates a variable, sets a few defaults, and adds it to the list */
1722 nir_variable
*nir_variable_create(nir_shader
*shader
,
1723 nir_variable_mode mode
,
1724 const struct glsl_type
*type
,
1726 /** creates a local variable and adds it to the list */
1727 nir_variable
*nir_local_variable_create(nir_function_impl
*impl
,
1728 const struct glsl_type
*type
,
1731 /** creates a function and adds it to the shader's list of functions */
1732 nir_function
*nir_function_create(nir_shader
*shader
, const char *name
);
1734 nir_function_impl
*nir_function_impl_create(nir_function
*func
);
1735 /** creates a function_impl that isn't tied to any particular function */
1736 nir_function_impl
*nir_function_impl_create_bare(nir_shader
*shader
);
1738 nir_block
*nir_block_create(nir_shader
*shader
);
1739 nir_if
*nir_if_create(nir_shader
*shader
);
1740 nir_loop
*nir_loop_create(nir_shader
*shader
);
1742 nir_function_impl
*nir_cf_node_get_function(nir_cf_node
*node
);
1744 /** requests that the given pieces of metadata be generated */
1745 void nir_metadata_require(nir_function_impl
*impl
, nir_metadata required
);
1746 /** dirties all but the preserved metadata */
1747 void nir_metadata_preserve(nir_function_impl
*impl
, nir_metadata preserved
);
1749 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
1750 nir_alu_instr
*nir_alu_instr_create(nir_shader
*shader
, nir_op op
);
1752 nir_jump_instr
*nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
);
1754 nir_load_const_instr
*nir_load_const_instr_create(nir_shader
*shader
,
1755 unsigned num_components
);
1757 nir_intrinsic_instr
*nir_intrinsic_instr_create(nir_shader
*shader
,
1758 nir_intrinsic_op op
);
1760 nir_call_instr
*nir_call_instr_create(nir_shader
*shader
,
1761 nir_function
*callee
);
1763 nir_tex_instr
*nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
);
1765 nir_phi_instr
*nir_phi_instr_create(nir_shader
*shader
);
1767 nir_parallel_copy_instr
*nir_parallel_copy_instr_create(nir_shader
*shader
);
1769 nir_ssa_undef_instr
*nir_ssa_undef_instr_create(nir_shader
*shader
,
1770 unsigned num_components
);
1772 nir_deref_var
*nir_deref_var_create(void *mem_ctx
, nir_variable
*var
);
1773 nir_deref_array
*nir_deref_array_create(void *mem_ctx
);
1774 nir_deref_struct
*nir_deref_struct_create(void *mem_ctx
, unsigned field_index
);
1776 nir_deref
*nir_copy_deref(void *mem_ctx
, nir_deref
*deref
);
1778 nir_load_const_instr
*
1779 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
);
1782 * NIR Cursors and Instruction Insertion API
1785 * A tiny struct representing a point to insert/extract instructions or
1786 * control flow nodes. Helps reduce the combinatorial explosion of possible
1787 * points to insert/extract.
1789 * \sa nir_control_flow.h
1792 nir_cursor_before_block
,
1793 nir_cursor_after_block
,
1794 nir_cursor_before_instr
,
1795 nir_cursor_after_instr
,
1796 } nir_cursor_option
;
1799 nir_cursor_option option
;
1806 static inline nir_cursor
1807 nir_before_block(nir_block
*block
)
1810 cursor
.option
= nir_cursor_before_block
;
1811 cursor
.block
= block
;
1815 static inline nir_cursor
1816 nir_after_block(nir_block
*block
)
1819 cursor
.option
= nir_cursor_after_block
;
1820 cursor
.block
= block
;
1824 static inline nir_cursor
1825 nir_before_instr(nir_instr
*instr
)
1828 cursor
.option
= nir_cursor_before_instr
;
1829 cursor
.instr
= instr
;
1833 static inline nir_cursor
1834 nir_after_instr(nir_instr
*instr
)
1837 cursor
.option
= nir_cursor_after_instr
;
1838 cursor
.instr
= instr
;
1842 static inline nir_cursor
1843 nir_after_block_before_jump(nir_block
*block
)
1845 nir_instr
*last_instr
= nir_block_last_instr(block
);
1846 if (last_instr
&& last_instr
->type
== nir_instr_type_jump
) {
1847 return nir_before_instr(last_instr
);
1849 return nir_after_block(block
);
1853 static inline nir_cursor
1854 nir_before_cf_node(nir_cf_node
*node
)
1856 if (node
->type
== nir_cf_node_block
)
1857 return nir_before_block(nir_cf_node_as_block(node
));
1859 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node
)));
1862 static inline nir_cursor
1863 nir_after_cf_node(nir_cf_node
*node
)
1865 if (node
->type
== nir_cf_node_block
)
1866 return nir_after_block(nir_cf_node_as_block(node
));
1868 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node
)));
1871 static inline nir_cursor
1872 nir_before_cf_list(struct exec_list
*cf_list
)
1874 nir_cf_node
*first_node
= exec_node_data(nir_cf_node
,
1875 exec_list_get_head(cf_list
), node
);
1876 return nir_before_cf_node(first_node
);
1879 static inline nir_cursor
1880 nir_after_cf_list(struct exec_list
*cf_list
)
1882 nir_cf_node
*last_node
= exec_node_data(nir_cf_node
,
1883 exec_list_get_tail(cf_list
), node
);
1884 return nir_after_cf_node(last_node
);
1888 * Insert a NIR instruction at the given cursor.
1890 * Note: This does not update the cursor.
1892 void nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
);
1895 nir_instr_insert_before(nir_instr
*instr
, nir_instr
*before
)
1897 nir_instr_insert(nir_before_instr(instr
), before
);
1901 nir_instr_insert_after(nir_instr
*instr
, nir_instr
*after
)
1903 nir_instr_insert(nir_after_instr(instr
), after
);
1907 nir_instr_insert_before_block(nir_block
*block
, nir_instr
*before
)
1909 nir_instr_insert(nir_before_block(block
), before
);
1913 nir_instr_insert_after_block(nir_block
*block
, nir_instr
*after
)
1915 nir_instr_insert(nir_after_block(block
), after
);
1919 nir_instr_insert_before_cf(nir_cf_node
*node
, nir_instr
*before
)
1921 nir_instr_insert(nir_before_cf_node(node
), before
);
1925 nir_instr_insert_after_cf(nir_cf_node
*node
, nir_instr
*after
)
1927 nir_instr_insert(nir_after_cf_node(node
), after
);
1931 nir_instr_insert_before_cf_list(struct exec_list
*list
, nir_instr
*before
)
1933 nir_instr_insert(nir_before_cf_list(list
), before
);
1937 nir_instr_insert_after_cf_list(struct exec_list
*list
, nir_instr
*after
)
1939 nir_instr_insert(nir_after_cf_list(list
), after
);
1942 void nir_instr_remove(nir_instr
*instr
);
1946 typedef bool (*nir_foreach_ssa_def_cb
)(nir_ssa_def
*def
, void *state
);
1947 typedef bool (*nir_foreach_dest_cb
)(nir_dest
*dest
, void *state
);
1948 typedef bool (*nir_foreach_src_cb
)(nir_src
*src
, void *state
);
1949 bool nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
,
1951 bool nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
);
1952 bool nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
);
1954 nir_const_value
*nir_src_as_const_value(nir_src src
);
1955 bool nir_src_is_dynamically_uniform(nir_src src
);
1956 bool nir_srcs_equal(nir_src src1
, nir_src src2
);
1957 void nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
);
1958 void nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
);
1959 void nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
);
1960 void nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
,
1963 void nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1964 unsigned num_components
, const char *name
);
1965 void nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1966 unsigned num_components
, const char *name
);
1967 void nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
);
1968 void nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1969 nir_instr
*after_me
);
1971 /* visits basic blocks in source-code order */
1972 typedef bool (*nir_foreach_block_cb
)(nir_block
*block
, void *state
);
1973 bool nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1975 bool nir_foreach_block_reverse(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1977 bool nir_foreach_block_in_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1980 /* If the following CF node is an if, this function returns that if.
1981 * Otherwise, it returns NULL.
1983 nir_if
*nir_block_get_following_if(nir_block
*block
);
1985 nir_loop
*nir_block_get_following_loop(nir_block
*block
);
1987 void nir_index_local_regs(nir_function_impl
*impl
);
1988 void nir_index_global_regs(nir_shader
*shader
);
1989 void nir_index_ssa_defs(nir_function_impl
*impl
);
1990 unsigned nir_index_instrs(nir_function_impl
*impl
);
1992 void nir_index_blocks(nir_function_impl
*impl
);
1994 void nir_print_shader(nir_shader
*shader
, FILE *fp
);
1995 void nir_print_instr(const nir_instr
*instr
, FILE *fp
);
1997 nir_shader
* nir_shader_clone(void *mem_ctx
, const nir_shader
*s
);
1998 nir_function_impl
*nir_function_impl_clone(const nir_function_impl
*fi
);
2001 void nir_validate_shader(nir_shader
*shader
);
2002 void nir_metadata_set_validation_flag(nir_shader
*shader
);
2003 void nir_metadata_check_validation_flag(nir_shader
*shader
);
2005 #include "util/debug.h"
2007 should_clone_nir(void)
2009 static int should_clone
= -1;
2010 if (should_clone
< 0)
2011 should_clone
= env_var_as_boolean("NIR_TEST_CLONE", false);
2013 return should_clone
;
2016 static inline void nir_validate_shader(nir_shader
*shader
) { (void) shader
; }
2017 static inline void nir_metadata_set_validation_flag(nir_shader
*shader
) { (void) shader
; }
2018 static inline void nir_metadata_check_validation_flag(nir_shader
*shader
) { (void) shader
; }
2019 static inline bool should_clone_nir(void) { return false; }
2022 #define _PASS(nir, do_pass) do { \
2024 nir_validate_shader(nir); \
2025 if (should_clone_nir()) { \
2026 nir_shader *clone = nir_shader_clone(ralloc_parent(nir), nir); \
2032 #define NIR_PASS(progress, nir, pass, ...) _PASS(nir, \
2033 nir_metadata_set_validation_flag(nir); \
2034 if (pass(nir, ##__VA_ARGS__)) { \
2036 nir_metadata_check_validation_flag(nir); \
2040 #define NIR_PASS_V(nir, pass, ...) _PASS(nir, \
2041 pass(nir, ##__VA_ARGS__); \
2044 void nir_calc_dominance_impl(nir_function_impl
*impl
);
2045 void nir_calc_dominance(nir_shader
*shader
);
2047 nir_block
*nir_dominance_lca(nir_block
*b1
, nir_block
*b2
);
2048 bool nir_block_dominates(nir_block
*parent
, nir_block
*child
);
2050 void nir_dump_dom_tree_impl(nir_function_impl
*impl
, FILE *fp
);
2051 void nir_dump_dom_tree(nir_shader
*shader
, FILE *fp
);
2053 void nir_dump_dom_frontier_impl(nir_function_impl
*impl
, FILE *fp
);
2054 void nir_dump_dom_frontier(nir_shader
*shader
, FILE *fp
);
2056 void nir_dump_cfg_impl(nir_function_impl
*impl
, FILE *fp
);
2057 void nir_dump_cfg(nir_shader
*shader
, FILE *fp
);
2059 int nir_gs_count_vertices(const nir_shader
*shader
);
2061 bool nir_split_var_copies(nir_shader
*shader
);
2063 void nir_lower_var_copy_instr(nir_intrinsic_instr
*copy
, void *mem_ctx
);
2064 void nir_lower_var_copies(nir_shader
*shader
);
2066 bool nir_lower_global_vars_to_local(nir_shader
*shader
);
2068 bool nir_lower_indirect_derefs(nir_shader
*shader
, uint32_t mode_mask
);
2070 bool nir_lower_locals_to_regs(nir_shader
*shader
);
2072 void nir_lower_outputs_to_temporaries(nir_shader
*shader
);
2074 void nir_assign_var_locations(struct exec_list
*var_list
,
2076 int (*type_size
)(const struct glsl_type
*));
2078 void nir_lower_io(nir_shader
*shader
,
2079 nir_variable_mode mode
,
2080 int (*type_size
)(const struct glsl_type
*));
2081 nir_src
*nir_get_io_offset_src(nir_intrinsic_instr
*instr
);
2082 nir_src
*nir_get_io_vertex_index_src(nir_intrinsic_instr
*instr
);
2084 void nir_lower_vars_to_ssa(nir_shader
*shader
);
2086 bool nir_remove_dead_variables(nir_shader
*shader
);
2088 void nir_move_vec_src_uses_to_dest(nir_shader
*shader
);
2089 bool nir_lower_vec_to_movs(nir_shader
*shader
);
2090 void nir_lower_alu_to_scalar(nir_shader
*shader
);
2091 void nir_lower_load_const_to_scalar(nir_shader
*shader
);
2093 void nir_lower_phis_to_scalar(nir_shader
*shader
);
2095 void nir_lower_samplers(nir_shader
*shader
,
2096 const struct gl_shader_program
*shader_program
);
2098 bool nir_lower_system_values(nir_shader
*shader
);
2100 typedef struct nir_lower_tex_options
{
2102 * bitmask of (1 << GLSL_SAMPLER_DIM_x) to control for which
2103 * sampler types a texture projector is lowered.
2108 * If true, lower rect textures to 2D, using txs to fetch the
2109 * texture dimensions and dividing the texture coords by the
2110 * texture dims to normalize.
2115 * To emulate certain texture wrap modes, this can be used
2116 * to saturate the specified tex coord to [0.0, 1.0]. The
2117 * bits are according to sampler #, ie. if, for example:
2119 * (conf->saturate_s & (1 << n))
2121 * is true, then the s coord for sampler n is saturated.
2123 * Note that clamping must happen *after* projector lowering
2124 * so any projected texture sample instruction with a clamped
2125 * coordinate gets automatically lowered, regardless of the
2126 * 'lower_txp' setting.
2128 unsigned saturate_s
;
2129 unsigned saturate_t
;
2130 unsigned saturate_r
;
2132 /* Bitmask of textures that need swizzling.
2134 * If (swizzle_result & (1 << texture_index)), then the swizzle in
2135 * swizzles[texture_index] is applied to the result of the texturing
2138 unsigned swizzle_result
;
2140 /* A swizzle for each texture. Values 0-3 represent x, y, z, or w swizzles
2141 * while 4 and 5 represent 0 and 1 respectively.
2143 uint8_t swizzles
[32][4];
2144 } nir_lower_tex_options
;
2146 bool nir_lower_tex(nir_shader
*shader
,
2147 const nir_lower_tex_options
*options
);
2149 void nir_lower_idiv(nir_shader
*shader
);
2151 void nir_lower_clip_vs(nir_shader
*shader
, unsigned ucp_enables
);
2152 void nir_lower_clip_fs(nir_shader
*shader
, unsigned ucp_enables
);
2154 void nir_lower_two_sided_color(nir_shader
*shader
);
2156 void nir_lower_atomics(nir_shader
*shader
,
2157 const struct gl_shader_program
*shader_program
);
2158 void nir_lower_to_source_mods(nir_shader
*shader
);
2160 bool nir_lower_gs_intrinsics(nir_shader
*shader
);
2162 bool nir_normalize_cubemap_coords(nir_shader
*shader
);
2164 void nir_live_ssa_defs_impl(nir_function_impl
*impl
);
2165 bool nir_ssa_defs_interfere(nir_ssa_def
*a
, nir_ssa_def
*b
);
2167 void nir_convert_to_ssa_impl(nir_function_impl
*impl
);
2168 void nir_convert_to_ssa(nir_shader
*shader
);
2170 /* If phi_webs_only is true, only convert SSA values involved in phi nodes to
2171 * registers. If false, convert all values (even those not involved in a phi
2172 * node) to registers.
2174 void nir_convert_from_ssa(nir_shader
*shader
, bool phi_webs_only
);
2176 bool nir_opt_algebraic(nir_shader
*shader
);
2177 bool nir_opt_algebraic_late(nir_shader
*shader
);
2178 bool nir_opt_constant_folding(nir_shader
*shader
);
2180 bool nir_opt_global_to_local(nir_shader
*shader
);
2182 bool nir_copy_prop(nir_shader
*shader
);
2184 bool nir_opt_cse(nir_shader
*shader
);
2186 bool nir_opt_dce(nir_shader
*shader
);
2188 bool nir_opt_dead_cf(nir_shader
*shader
);
2190 void nir_opt_gcm(nir_shader
*shader
);
2192 bool nir_opt_peephole_select(nir_shader
*shader
);
2194 bool nir_opt_remove_phis(nir_shader
*shader
);
2196 bool nir_opt_undef(nir_shader
*shader
);
2198 void nir_sweep(nir_shader
*shader
);
2200 nir_intrinsic_op
nir_intrinsic_from_system_value(gl_system_value val
);
2201 gl_system_value
nir_system_value_from_intrinsic(nir_intrinsic_op intrin
);