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"
33 #include "GL/gl.h" /* GLenum */
34 #include "util/ralloc.h"
35 #include "main/mtypes.h"
36 #include "main/bitset.h"
37 #include "nir_types.h"
45 #define NIR_TRUE (~0u)
47 struct nir_function_overload
;
53 * Description of built-in state associated with a uniform
55 * \sa nir_variable::state_slots
72 * Data stored in an nir_constant
74 union nir_constant_data
{
81 typedef struct nir_constant
{
83 * Value of the constant.
85 * The field used to back the values supplied by the constant is determined
86 * by the type associated with the \c ir_instruction. Constants may be
87 * scalars, vectors, or matrices.
89 union nir_constant_data value
;
91 /* Array elements / Structure Fields */
92 struct nir_constant
**elements
;
96 * \brief Layout qualifiers for gl_FragDepth.
98 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
99 * with a layout qualifier.
102 nir_depth_layout_none
, /**< No depth layout is specified. */
103 nir_depth_layout_any
,
104 nir_depth_layout_greater
,
105 nir_depth_layout_less
,
106 nir_depth_layout_unchanged
110 * Either a uniform, global variable, shader input, or shader output. Based on
111 * ir_variable - it should be easy to translate between the two.
115 struct exec_node node
;
118 * Declared type of the variable
120 const struct glsl_type
*type
;
123 * Declared name of the variable
128 * For variables which satisfy the is_interface_instance() predicate, this
129 * points to an array of integers such that if the ith member of the
130 * interface block is an array, max_ifc_array_access[i] is the maximum
131 * array element of that member that has been accessed. If the ith member
132 * of the interface block is not an array, max_ifc_array_access[i] is
135 * For variables whose type is not an interface block, this pointer is
138 unsigned *max_ifc_array_access
;
140 struct nir_variable_data
{
143 * Is the variable read-only?
145 * This is set for variables declared as \c const, shader inputs,
148 unsigned read_only
:1;
151 unsigned invariant
:1;
154 * Storage class of the variable.
156 * \sa nir_variable_mode
158 nir_variable_mode mode
:4;
161 * Interpolation mode for shader inputs / outputs
163 * \sa ir_variable_interpolation
165 unsigned interpolation
:2;
168 * \name ARB_fragment_coord_conventions
171 unsigned origin_upper_left
:1;
172 unsigned pixel_center_integer
:1;
176 * Was the location explicitly set in the shader?
178 * If the location is explicitly set in the shader, it \b cannot be changed
179 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
182 unsigned explicit_location
:1;
183 unsigned explicit_index
:1;
186 * Was an initial binding explicitly set in the shader?
188 * If so, constant_value contains an integer ir_constant representing the
189 * initial binding point.
191 unsigned explicit_binding
:1;
194 * Does this variable have an initializer?
196 * This is used by the linker to cross-validiate initializers of global
199 unsigned has_initializer
:1;
202 * Is this variable a generic output or input that has not yet been matched
203 * up to a variable in another stage of the pipeline?
205 * This is used by the linker as scratch storage while assigning locations
206 * to generic inputs and outputs.
208 unsigned is_unmatched_generic_inout
:1;
211 * If non-zero, then this variable may be packed along with other variables
212 * into a single varying slot, so this offset should be applied when
213 * accessing components. For example, an offset of 1 means that the x
214 * component of this variable is actually stored in component y of the
215 * location specified by \c location.
217 unsigned location_frac
:2;
220 * Non-zero if this variable was created by lowering a named interface
221 * block which was not an array.
223 * Note that this variable and \c from_named_ifc_block_array will never
226 unsigned from_named_ifc_block_nonarray
:1;
229 * Non-zero if this variable was created by lowering a named interface
230 * block which was an array.
232 * Note that this variable and \c from_named_ifc_block_nonarray will never
235 unsigned from_named_ifc_block_array
:1;
238 * \brief Layout qualifier for gl_FragDepth.
240 * This is not equal to \c ir_depth_layout_none if and only if this
241 * variable is \c gl_FragDepth and a layout qualifier is specified.
243 nir_depth_layout depth_layout
;
246 * Storage location of the base of this variable
248 * The precise meaning of this field depends on the nature of the variable.
250 * - Vertex shader input: one of the values from \c gl_vert_attrib.
251 * - Vertex shader output: one of the values from \c gl_varying_slot.
252 * - Geometry shader input: one of the values from \c gl_varying_slot.
253 * - Geometry shader output: one of the values from \c gl_varying_slot.
254 * - Fragment shader input: one of the values from \c gl_varying_slot.
255 * - Fragment shader output: one of the values from \c gl_frag_result.
256 * - Uniforms: Per-stage uniform slot number for default uniform block.
257 * - Uniforms: Index within the uniform block definition for UBO members.
258 * - Other: This field is not currently used.
260 * If the variable is a uniform, shader input, or shader output, and the
261 * slot has not been assigned, the value will be -1.
266 * The actual location of the variable in the IR. Only valid for inputs
269 unsigned int driver_location
;
272 * output index for dual source blending.
277 * Initial binding point for a sampler or UBO.
279 * For array types, this represents the binding point for the first element.
284 * Location an atomic counter is stored at.
287 unsigned buffer_index
;
292 * ARB_shader_image_load_store qualifiers.
295 bool read_only
; /**< "readonly" qualifier. */
296 bool write_only
; /**< "writeonly" qualifier. */
301 /** Image internal format if specified explicitly, otherwise GL_NONE. */
306 * Highest element accessed with a constant expression array index
308 * Not used for non-array variables.
310 unsigned max_array_access
;
315 * Built-in state that backs this uniform
317 * Once set at variable creation, \c state_slots must remain invariant.
318 * This is because, ideally, this array would be shared by all clones of
319 * this variable in the IR tree. In other words, we'd really like for it
320 * to be a fly-weight.
322 * If the variable is not a uniform, \c num_state_slots will be zero and
323 * \c state_slots will be \c NULL.
326 unsigned num_state_slots
; /**< Number of state slots used */
327 nir_state_slot
*state_slots
; /**< State descriptors. */
331 * Value assigned in the initializer of a variable declared "const"
333 nir_constant
*constant_value
;
336 * Constant expression assigned in the initializer of the variable
339 * This field and \c ::constant_value are distinct. Even if the two fields
340 * refer to constants with the same value, they must point to separate
343 nir_constant
*constant_initializer
;
346 * For variables that are in an interface block or are an instance of an
347 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
349 * \sa ir_variable::location
351 const struct glsl_type
*interface_type
;
355 struct exec_node node
;
357 unsigned num_components
; /** < number of vector components */
358 unsigned num_array_elems
; /** < size of array (0 for no array) */
360 /** generic register index. */
363 /** only for debug purposes, can be NULL */
366 /** whether this register is local (per-function) or global (per-shader) */
370 * If this flag is set to true, then accessing channels >= num_components
371 * is well-defined, and simply spills over to the next array element. This
372 * is useful for backends that can do per-component accessing, in
373 * particular scalar backends. By setting this flag and making
374 * num_components equal to 1, structures can be packed tightly into
375 * registers and then registers can be accessed per-component to get to
376 * each structure member, even if it crosses vec4 boundaries.
380 /** set of nir_instr's where this register is used (read from) */
383 /** set of nir_instr's where this register is defined (written to) */
386 /** set of ifs where this register is used as a condition */
393 nir_instr_type_texture
,
394 nir_instr_type_intrinsic
,
395 nir_instr_type_load_const
,
397 nir_instr_type_ssa_undef
,
399 nir_instr_type_parallel_copy
,
403 struct exec_node node
;
405 struct nir_block
*block
;
407 /* flag for dead code elimination (see nir_opt_dce.c) */
411 #define nir_instr_next(instr) \
412 exec_node_data(nir_instr, (instr)->node.next, node)
414 #define nir_instr_prev(instr) \
415 exec_node_data(nir_instr, (instr)->node.prev, node)
418 /** for debugging only, can be NULL */
421 /** generic SSA definition index. */
424 /** Index into the live_in and live_out bitfields */
427 nir_instr
*parent_instr
;
432 uint8_t num_components
;
439 struct nir_src
*indirect
; /** < NULL for no indirect offset */
440 unsigned base_offset
;
442 /* TODO use-def chain goes here */
447 struct nir_src
*indirect
; /** < NULL for no indirect offset */
448 unsigned base_offset
;
450 /* TODO def-use chain goes here */
453 typedef struct nir_src
{
471 nir_src
nir_src_copy(nir_src src
, void *mem_ctx
);
472 nir_dest
nir_dest_copy(nir_dest dest
, void *mem_ctx
);
478 * \name input modifiers
482 * For inputs interpreted as a floating point, flips the sign bit. For inputs
483 * interpreted as an integer, performs the two's complement negation.
488 * Clears the sign bit for floating point values, and computes the integer
489 * absolute value for integers. Note that the negate modifier acts after
490 * the absolute value modifier, therefore if both are set then all inputs
491 * will become negative.
497 * For each input component, says which component of the register it is
498 * chosen from. Note that which elements of the swizzle are used and which
499 * are ignored are based on the write mask for most opcodes - for example,
500 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
501 * a swizzle of {2, x, 1, 0} where x means "don't care."
510 * \name saturate output modifier
512 * Only valid for opcodes that output floating-point numbers. Clamps the
513 * output to between 0.0 and 1.0 inclusive.
518 unsigned write_mask
: 4; /* ignored if dest.is_ssa is true */
521 #define OPCODE(name, num_inputs, per_component, output_size, output_type, \
522 input_sizes, input_types) \
525 #define LAST_OPCODE(name) nir_last_opcode = nir_op_##name,
528 #include "nir_opcodes.h"
529 nir_num_opcodes
= nir_last_opcode
+ 1
548 * If true, the opcode acts in the standard, per-component manner; the
549 * operation is performed on each component (except the ones that are masked
550 * out) with the input being taken from the input swizzle for that component.
552 * If false, the size of the output and inputs are explicitly given; swizzle
553 * and writemask are still in effect, but if the output component is masked
554 * out, then the input component may still be in use.
556 * The size of some of the inputs may be given (i.e. non-zero) even though
557 * per_component is false; in that case, each component of the input acts
558 * per-component, while the rest of the inputs and the output are normal.
559 * For example, for conditional select the condition is per-component but
560 * everything else is normal.
565 * If per_component is false, the number of components in the output.
567 unsigned output_size
;
570 * The type of vector that the instruction outputs. Note that this
571 * determines whether the saturate modifier is allowed.
574 nir_alu_type output_type
;
577 * If per_component is false, the number of components in each input.
579 unsigned input_sizes
[4];
582 * The type of vector that each input takes. Note that negate is only
583 * allowed on inputs with int or float type, and behaves differently on the
584 * two, and absolute value is only allowed on float type inputs.
586 nir_alu_type input_types
[4];
589 extern const nir_op_info nir_op_infos
[nir_num_opcodes
];
591 typedef struct nir_alu_instr
{
600 /* is this source channel used? */
602 nir_alu_instr_channel_used(nir_alu_instr
*instr
, unsigned src
, unsigned channel
)
604 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
605 return channel
< nir_op_infos
[instr
->op
].input_sizes
[src
];
607 return (instr
->dest
.write_mask
>> channel
) & 1;
612 nir_deref_type_array
,
613 nir_deref_type_struct
616 typedef struct nir_deref
{
617 nir_deref_type deref_type
;
618 struct nir_deref
*child
;
619 const struct glsl_type
*type
;
628 /* This enum describes how the array is referenced. If the deref is
629 * direct then the base_offset is used. If the deref is indirect then then
630 * offset is given by base_offset + indirect. If the deref is a wildcard
631 * then the deref refers to all of the elements of the array at the same
632 * time. Wildcard dereferences are only ever allowed in copy_var
633 * intrinsics and the source and destination derefs must have matching
637 nir_deref_array_type_direct
,
638 nir_deref_array_type_indirect
,
639 nir_deref_array_type_wildcard
,
640 } nir_deref_array_type
;
645 nir_deref_array_type deref_array_type
;
646 unsigned base_offset
;
656 #define nir_deref_as_var(_deref) exec_node_data(nir_deref_var, _deref, deref)
657 #define nir_deref_as_array(_deref) \
658 exec_node_data(nir_deref_array, _deref, deref)
659 #define nir_deref_as_struct(_deref) \
660 exec_node_data(nir_deref_struct, _deref, deref)
666 nir_deref_var
**params
;
667 nir_deref_var
*return_deref
;
672 struct nir_function_overload
*callee
;
675 #define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
676 num_variables, num_indices, flags) \
677 nir_intrinsic_##name,
679 #define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
682 #include "nir_intrinsics.h"
683 nir_num_intrinsics
= nir_last_intrinsic
+ 1
687 #undef LAST_INTRINSIC
692 nir_intrinsic_op intrinsic
;
698 nir_deref_var
*variables
[2];
704 } nir_intrinsic_instr
;
707 * \name NIR intrinsics semantic flags
709 * information about what the compiler can do with the intrinsics.
711 * \sa nir_intrinsic_info::flags
715 * whether the intrinsic can be safely eliminated if none of its register
716 * outputs are being used.
718 #define NIR_INTRINSIC_CAN_ELIMINATE (1 << 0)
721 * Whether the intrinsic can be reordered with respect to any other intrinsic,
722 * i.e. whether the only reodering dependencies of the intrinsic are due to the
723 * register reads/writes.
725 #define NIR_INTRINSIC_CAN_REORDER (1 << 1)
728 #define NIR_INTRINSIC_MAX_INPUTS 4
733 unsigned num_srcs
; /** < number of register/SSA inputs */
735 /** number of components of each input register */
736 unsigned src_components
[NIR_INTRINSIC_MAX_INPUTS
];
740 /** number of components of each output register */
741 unsigned dest_components
;
743 /** the number of inputs/outputs that are variables */
744 unsigned num_variables
;
746 /** the number of constant indices used by the intrinsic */
747 unsigned num_indices
;
749 /** semantic flags for calls to this intrinsic */
751 } nir_intrinsic_info
;
753 extern const nir_intrinsic_info nir_intrinsic_infos
[nir_num_intrinsics
];
756 * \group texture information
758 * This gives semantic information about textures which is useful to the
759 * frontend, the backend, and lowering passes, but not the optimizer.
764 nir_tex_src_projector
,
765 nir_tex_src_comparitor
, /* shadow comparitor */
769 nir_tex_src_ms_index
, /* MSAA sample index */
772 nir_tex_src_sampler_index
, /* < dynamically uniform indirect index */
773 nir_num_texinput_types
777 nir_texop_tex
, /**< Regular texture look-up */
778 nir_texop_txb
, /**< Texture look-up with LOD bias */
779 nir_texop_txl
, /**< Texture look-up with explicit LOD */
780 nir_texop_txd
, /**< Texture look-up with partial derivatvies */
781 nir_texop_txf
, /**< Texel fetch with explicit LOD */
782 nir_texop_txf_ms
, /**< Multisample texture fetch */
783 nir_texop_txs
, /**< Texture size */
784 nir_texop_lod
, /**< Texture lod query */
785 nir_texop_tg4
, /**< Texture gather */
786 nir_texop_query_levels
/**< Texture levels query */
795 enum glsl_sampler_dim sampler_dim
;
796 nir_alu_type dest_type
;
801 nir_texinput_type src_type
[4];
802 unsigned num_srcs
, coord_components
;
803 bool is_array
, is_shadow
;
806 * If is_shadow is true, whether this is the old-style shadow that outputs 4
807 * components or the new-style shadow that outputs 1 component.
809 bool is_new_style_shadow
;
811 /* constant offset - must be 0 if the offset source is used */
814 /* gather component selector */
815 unsigned component
: 2;
817 unsigned sampler_index
;
818 nir_deref_var
*sampler
; /* if this is NULL, use sampler_index instead */
821 static inline unsigned
822 nir_tex_instr_dest_size(nir_tex_instr
*instr
)
824 if (instr
->op
== nir_texop_txs
) {
826 switch (instr
->sampler_dim
) {
827 case GLSL_SAMPLER_DIM_1D
:
828 case GLSL_SAMPLER_DIM_BUF
:
831 case GLSL_SAMPLER_DIM_2D
:
832 case GLSL_SAMPLER_DIM_CUBE
:
833 case GLSL_SAMPLER_DIM_MS
:
834 case GLSL_SAMPLER_DIM_RECT
:
835 case GLSL_SAMPLER_DIM_EXTERNAL
:
838 case GLSL_SAMPLER_DIM_3D
:
850 if (instr
->op
== nir_texop_query_levels
)
853 if (instr
->is_shadow
&& instr
->is_new_style_shadow
)
859 static inline unsigned
860 nir_tex_instr_src_size(nir_tex_instr
*instr
, unsigned src
)
862 if (instr
->src_type
[src
] == nir_tex_src_coord
)
863 return instr
->coord_components
;
866 if (instr
->src_type
[src
] == nir_tex_src_offset
||
867 instr
->src_type
[src
] == nir_tex_src_ddx
||
868 instr
->src_type
[src
] == nir_tex_src_ddy
) {
870 return instr
->coord_components
- 1;
872 return instr
->coord_components
;
879 nir_tex_instr_src_index(nir_tex_instr
*instr
, nir_texinput_type type
)
881 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
882 if (instr
->src_type
[i
] == type
)
900 nir_const_value value
;
901 nir_const_value
*array
;
904 unsigned num_components
;
907 * The number of constant array elements to be copied into the variable. If
908 * this != 0, then value.array holds the array of size array_elems;
909 * otherwise, value.value holds the single vector constant (the more common
910 * case, and the only case for SSA destinations).
912 unsigned array_elems
;
918 } nir_load_const_instr
;
931 /* creates a new SSA variable in an undefined state */
936 } nir_ssa_undef_instr
;
939 struct exec_node node
;
940 struct nir_block
*pred
;
947 struct exec_list srcs
;
952 struct exec_node node
;
955 } nir_parallel_copy_copy
;
960 /* Indicates that this is the parallel copy at the end of the block.
961 * When isolating phi nodes, we create 2 parallel copies in most blocks;
962 * this flag helps tell them apart.
966 struct exec_list copies
;
967 } nir_parallel_copy_instr
;
969 #define nir_instr_as_alu(_instr) exec_node_data(nir_alu_instr, _instr, instr)
970 #define nir_instr_as_call(_instr) exec_node_data(nir_call_instr, _instr, instr)
971 #define nir_instr_as_jump(_instr) exec_node_data(nir_jump_instr, _instr, instr)
972 #define nir_instr_as_texture(_instr) \
973 exec_node_data(nir_tex_instr, _instr, instr)
974 #define nir_instr_as_intrinsic(_instr) \
975 exec_node_data(nir_intrinsic_instr, _instr, instr)
976 #define nir_instr_as_load_const(_instr) \
977 exec_node_data(nir_load_const_instr, _instr, instr)
978 #define nir_instr_as_ssa_undef(_instr) \
979 exec_node_data(nir_ssa_undef_instr, _instr, instr)
980 #define nir_instr_as_phi(_instr) \
981 exec_node_data(nir_phi_instr, _instr, instr)
982 #define nir_instr_as_parallel_copy(_instr) \
983 exec_node_data(nir_parallel_copy_instr, _instr, instr)
989 * Control flow consists of a tree of control flow nodes, which include
990 * if-statements and loops. The leaves of the tree are basic blocks, lists of
991 * instructions that always run start-to-finish. Each basic block also keeps
992 * track of its successors (blocks which may run immediately after the current
993 * block) and predecessors (blocks which could have run immediately before the
994 * current block). Each function also has a start block and an end block which
995 * all return statements point to (which is always empty). Together, all the
996 * blocks with their predecessors and successors make up the control flow
997 * graph (CFG) of the function. There are helpers that modify the tree of
998 * control flow nodes while modifying the CFG appropriately; these should be
999 * used instead of modifying the tree directly.
1006 nir_cf_node_function
1009 typedef struct nir_cf_node
{
1010 struct exec_node node
;
1011 nir_cf_node_type type
;
1012 struct nir_cf_node
*parent
;
1015 typedef struct nir_block
{
1016 nir_cf_node cf_node
;
1017 struct exec_list instr_list
;
1022 * Each block can only have up to 2 successors, so we put them in a simple
1023 * array - no need for anything more complicated.
1025 struct nir_block
*successors
[2];
1027 struct set
*predecessors
;
1030 * this node's immediate dominator in the dominance tree - set to NULL for
1033 struct nir_block
*imm_dom
;
1035 /* This node's children in the dominance tree */
1036 unsigned num_dom_children
;
1037 struct nir_block
**dom_children
;
1039 struct set
*dom_frontier
;
1041 /* live in and out for this block; used for liveness analysis */
1042 BITSET_WORD
*live_in
;
1043 BITSET_WORD
*live_out
;
1046 #define nir_block_first_instr(block) \
1047 exec_node_data(nir_instr, exec_list_get_head(&(block)->instr_list), node)
1048 #define nir_block_last_instr(block) \
1049 exec_node_data(nir_instr, exec_list_get_tail(&(block)->instr_list), node)
1051 #define nir_foreach_instr(block, instr) \
1052 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
1053 #define nir_foreach_instr_reverse(block, instr) \
1054 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
1055 #define nir_foreach_instr_safe(block, instr) \
1056 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
1059 nir_cf_node cf_node
;
1061 struct exec_list then_list
;
1062 struct exec_list else_list
;
1065 #define nir_if_first_then_node(if) \
1066 exec_node_data(nir_cf_node, exec_list_get_head(&(if)->then_list), node)
1067 #define nir_if_last_then_node(if) \
1068 exec_node_data(nir_cf_node, exec_list_get_tail(&(if)->then_list), node)
1069 #define nir_if_first_else_node(if) \
1070 exec_node_data(nir_cf_node, exec_list_get_head(&(if)->else_list), node)
1071 #define nir_if_last_else_node(if) \
1072 exec_node_data(nir_cf_node, exec_list_get_tail(&(if)->else_list), node)
1075 nir_cf_node cf_node
;
1076 struct exec_list body
;
1079 #define nir_loop_first_cf_node(loop) \
1080 exec_node_data(nir_cf_node, exec_list_get_head(&(loop)->body), node)
1081 #define nir_loop_last_cf_node(loop) \
1082 exec_node_data(nir_cf_node, exec_list_get_tail(&(loop)->body), node)
1085 * Various bits of metadata that can may be created or required by
1086 * optimization and analysis passes
1089 nir_metadata_none
= 0x0,
1090 nir_metadata_block_index
= 0x1,
1091 nir_metadata_dominance
= 0x2,
1092 nir_metadata_live_variables
= 0x4,
1096 nir_cf_node cf_node
;
1098 /** pointer to the overload of which this is an implementation */
1099 struct nir_function_overload
*overload
;
1101 struct exec_list body
; /** < list of nir_cf_node */
1103 nir_block
*start_block
, *end_block
;
1105 /** list for all local variables in the function */
1106 struct exec_list locals
;
1108 /** array of variables used as parameters */
1109 unsigned num_params
;
1110 nir_variable
**params
;
1112 /** variable used to hold the result of the function */
1113 nir_variable
*return_var
;
1115 /** list of local registers in the function */
1116 struct exec_list registers
;
1118 /** next available local register index */
1121 /** next available SSA value index */
1124 /* total number of basic blocks, only valid when block_index_dirty = false */
1125 unsigned num_blocks
;
1127 nir_metadata valid_metadata
;
1128 } nir_function_impl
;
1130 #define nir_cf_node_next(_node) \
1131 exec_node_data(nir_cf_node, exec_node_get_next(&(_node)->node), node)
1133 #define nir_cf_node_prev(_node) \
1134 exec_node_data(nir_cf_node, exec_node_get_prev(&(_node)->node), node)
1136 #define nir_cf_node_is_first(_node) \
1137 exec_node_is_head_sentinel((_node)->node.prev)
1139 #define nir_cf_node_is_last(_node) \
1140 exec_node_is_tail_sentinel((_node)->node.next)
1142 #define nir_cf_node_as_block(node) \
1143 exec_node_data(nir_block, node, cf_node)
1145 #define nir_cf_node_as_if(node) \
1146 exec_node_data(nir_if, node, cf_node)
1148 #define nir_cf_node_as_loop(node) \
1149 exec_node_data(nir_loop, node, cf_node)
1151 #define nir_cf_node_as_function(node) \
1152 exec_node_data(nir_function_impl, node, cf_node)
1157 nir_parameter_inout
,
1158 } nir_parameter_type
;
1161 nir_parameter_type param_type
;
1162 const struct glsl_type
*type
;
1165 typedef struct nir_function_overload
{
1166 struct exec_node node
;
1168 unsigned num_params
;
1169 nir_parameter
*params
;
1170 const struct glsl_type
*return_type
;
1172 nir_function_impl
*impl
; /** < NULL if the overload is only declared yet */
1174 /** pointer to the function of which this is an overload */
1175 struct nir_function
*function
;
1176 } nir_function_overload
;
1178 typedef struct nir_function
{
1179 struct exec_node node
;
1181 struct exec_list overload_list
;
1183 struct nir_shader
*shader
;
1186 #define nir_function_first_overload(func) \
1187 exec_node_data(nir_function_overload, \
1188 exec_list_get_head(&(func)->overload_list), node)
1190 typedef struct nir_shader
{
1191 /** hash table of name -> uniform */
1192 struct hash_table
*uniforms
;
1194 /** hash table of name -> input */
1195 struct hash_table
*inputs
;
1197 /** hash table of name -> output */
1198 struct hash_table
*outputs
;
1200 /** list of global variables in the shader */
1201 struct exec_list globals
;
1203 struct exec_list system_values
;
1205 struct exec_list functions
;
1207 /** list of global registers in the shader */
1208 struct exec_list registers
;
1210 /** structures used in this shader */
1211 unsigned num_user_structures
;
1212 struct glsl_type
**user_structures
;
1214 /** next available global register index */
1218 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
1221 unsigned num_inputs
, num_uniforms
, num_outputs
;
1224 #define nir_foreach_overload(shader, overload) \
1225 foreach_list_typed(nir_function, func, node, &(shader)->functions) \
1226 foreach_list_typed(nir_function_overload, overload, node, \
1227 &(func)->overload_list)
1229 nir_shader
*nir_shader_create(void *mem_ctx
);
1231 /** creates a register, including assigning it an index and adding it to the list */
1232 nir_register
*nir_global_reg_create(nir_shader
*shader
);
1234 nir_register
*nir_local_reg_create(nir_function_impl
*impl
);
1236 void nir_reg_remove(nir_register
*reg
);
1238 /** creates a function and adds it to the shader's list of functions */
1239 nir_function
*nir_function_create(nir_shader
*shader
, const char *name
);
1241 /** creates a null function returning null */
1242 nir_function_overload
*nir_function_overload_create(nir_function
*func
);
1244 nir_function_impl
*nir_function_impl_create(nir_function_overload
*func
);
1246 nir_block
*nir_block_create(void *mem_ctx
);
1247 nir_if
*nir_if_create(void *mem_ctx
);
1248 nir_loop
*nir_loop_create(void *mem_ctx
);
1250 nir_function_impl
*nir_cf_node_get_function(nir_cf_node
*node
);
1252 /** puts a control flow node immediately after another control flow node */
1253 void nir_cf_node_insert_after(nir_cf_node
*node
, nir_cf_node
*after
);
1255 /** puts a control flow node immediately before another control flow node */
1256 void nir_cf_node_insert_before(nir_cf_node
*node
, nir_cf_node
*before
);
1258 /** puts a control flow node at the beginning of a list from an if, loop, or function */
1259 void nir_cf_node_insert_begin(struct exec_list
*list
, nir_cf_node
*node
);
1261 /** puts a control flow node at the end of a list from an if, loop, or function */
1262 void nir_cf_node_insert_end(struct exec_list
*list
, nir_cf_node
*node
);
1264 /** removes a control flow node, doing any cleanup necessary */
1265 void nir_cf_node_remove(nir_cf_node
*node
);
1267 /** requests that the given pieces of metadata be generated */
1268 void nir_metadata_require(nir_function_impl
*impl
, nir_metadata required
);
1269 /** dirties all but the preserved metadata */
1270 void nir_metadata_dirty(nir_function_impl
*impl
, nir_metadata preserved
);
1272 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
1273 nir_alu_instr
*nir_alu_instr_create(void *mem_ctx
, nir_op op
);
1275 nir_jump_instr
*nir_jump_instr_create(void *mem_ctx
, nir_jump_type type
);
1277 nir_load_const_instr
*nir_load_const_instr_create(void *mem_ctx
);
1279 nir_intrinsic_instr
*nir_intrinsic_instr_create(void *mem_ctx
,
1280 nir_intrinsic_op op
);
1282 nir_call_instr
*nir_call_instr_create(void *mem_ctx
,
1283 nir_function_overload
*callee
);
1285 nir_tex_instr
*nir_tex_instr_create(void *mem_ctx
, unsigned num_srcs
);
1287 nir_phi_instr
*nir_phi_instr_create(void *mem_ctx
);
1289 nir_parallel_copy_instr
*nir_parallel_copy_instr_create(void *mem_ctx
);
1291 nir_ssa_undef_instr
*nir_ssa_undef_instr_create(void *mem_ctx
);
1293 nir_deref_var
*nir_deref_var_create(void *mem_ctx
, nir_variable
*var
);
1294 nir_deref_array
*nir_deref_array_create(void *mem_ctx
);
1295 nir_deref_struct
*nir_deref_struct_create(void *mem_ctx
, unsigned field_index
);
1297 nir_deref
*nir_copy_deref(void *mem_ctx
, nir_deref
*deref
);
1299 void nir_instr_insert_before(nir_instr
*instr
, nir_instr
*before
);
1300 void nir_instr_insert_after(nir_instr
*instr
, nir_instr
*after
);
1302 void nir_instr_insert_before_block(nir_block
*block
, nir_instr
*before
);
1303 void nir_instr_insert_after_block(nir_block
*block
, nir_instr
*after
);
1305 void nir_instr_insert_before_cf(nir_cf_node
*node
, nir_instr
*before
);
1306 void nir_instr_insert_after_cf(nir_cf_node
*node
, nir_instr
*after
);
1308 void nir_instr_insert_before_cf_list(struct exec_list
*list
, nir_instr
*before
);
1309 void nir_instr_insert_after_cf_list(struct exec_list
*list
, nir_instr
*after
);
1311 void nir_instr_remove(nir_instr
*instr
);
1313 typedef bool (*nir_foreach_dest_cb
)(nir_dest
*dest
, void *state
);
1314 typedef bool (*nir_foreach_src_cb
)(nir_src
*src
, void *state
);
1315 bool nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
);
1316 bool nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
);
1318 bool nir_srcs_equal(nir_src src1
, nir_src src2
);
1319 void nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
);
1321 void nir_ssa_def_init(nir_function_impl
*impl
, nir_instr
*instr
,
1322 nir_ssa_def
*def
, unsigned num_components
,
1324 void nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
, void *mem_ctx
);
1326 /* visits basic blocks in source-code order */
1327 typedef bool (*nir_foreach_block_cb
)(nir_block
*block
, void *state
);
1328 bool nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1330 bool nir_foreach_block_reverse(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1333 /* If the following CF node is an if, this function returns that if.
1334 * Otherwise, it returns NULL.
1336 nir_if
*nir_block_following_if(nir_block
*block
);
1338 void nir_index_local_regs(nir_function_impl
*impl
);
1339 void nir_index_global_regs(nir_shader
*shader
);
1340 void nir_index_ssa_defs(nir_function_impl
*impl
);
1342 void nir_index_blocks(nir_function_impl
*impl
);
1344 void nir_print_shader(nir_shader
*shader
, FILE *fp
);
1346 void nir_validate_shader(nir_shader
*shader
);
1348 void nir_calc_dominance_impl(nir_function_impl
*impl
);
1349 void nir_calc_dominance(nir_shader
*shader
);
1351 void nir_dump_dom_tree_impl(nir_function_impl
*impl
, FILE *fp
);
1352 void nir_dump_dom_tree(nir_shader
*shader
, FILE *fp
);
1354 void nir_dump_dom_frontier_impl(nir_function_impl
*impl
, FILE *fp
);
1355 void nir_dump_dom_frontier(nir_shader
*shader
, FILE *fp
);
1357 void nir_dump_cfg_impl(nir_function_impl
*impl
, FILE *fp
);
1358 void nir_dump_cfg(nir_shader
*shader
, FILE *fp
);
1360 void nir_lower_variables_scalar(nir_shader
*shader
, bool lower_globals
,
1361 bool lower_io
, bool add_names
,
1362 bool native_integers
);
1364 void nir_remove_dead_variables(nir_shader
*shader
);
1366 void nir_lower_vec_to_movs(nir_shader
*shader
);
1368 void nir_lower_samplers(nir_shader
*shader
,
1369 struct gl_shader_program
*shader_program
,
1370 struct gl_program
*prog
);
1372 void nir_lower_system_values(nir_shader
*shader
);
1374 void nir_lower_atomics(nir_shader
*shader
);
1376 void nir_live_variables_impl(nir_function_impl
*impl
);
1377 bool nir_ssa_defs_interfere(nir_ssa_def
*a
, nir_ssa_def
*b
);
1379 void nir_convert_to_ssa_impl(nir_function_impl
*impl
);
1380 void nir_convert_to_ssa(nir_shader
*shader
);
1381 void nir_convert_from_ssa(nir_shader
*shader
);
1383 bool nir_opt_global_to_local(nir_shader
*shader
);
1385 bool nir_copy_prop_impl(nir_function_impl
*impl
);
1386 bool nir_copy_prop(nir_shader
*shader
);
1388 bool nir_opt_cse(nir_shader
*shader
);
1390 bool nir_opt_dce_impl(nir_function_impl
*impl
);
1391 bool nir_opt_dce(nir_shader
*shader
);
1393 bool nir_opt_peephole_select(nir_shader
*shader
);
1394 bool nir_opt_peephole_ffma(nir_shader
*shader
);