2 * Copyright © 2014 Intel Corporation
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)
29 #include "nir_builder.h"
30 #include "nir_control_flow_private.h"
31 #include "util/half_float.h"
35 #include "util/u_math.h"
37 #include "main/menums.h" /* BITFIELD64_MASK */
40 nir_shader_create(void *mem_ctx
,
41 gl_shader_stage stage
,
42 const nir_shader_compiler_options
*options
,
45 nir_shader
*shader
= rzalloc(mem_ctx
, nir_shader
);
47 exec_list_make_empty(&shader
->variables
);
49 shader
->options
= options
;
52 assert(si
->stage
== stage
);
55 shader
->info
.stage
= stage
;
58 exec_list_make_empty(&shader
->functions
);
60 shader
->num_inputs
= 0;
61 shader
->num_outputs
= 0;
62 shader
->num_uniforms
= 0;
63 shader
->num_shared
= 0;
69 reg_create(void *mem_ctx
, struct exec_list
*list
)
71 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
73 list_inithead(®
->uses
);
74 list_inithead(®
->defs
);
75 list_inithead(®
->if_uses
);
77 reg
->num_components
= 0;
79 reg
->num_array_elems
= 0;
82 exec_list_push_tail(list
, ®
->node
);
88 nir_local_reg_create(nir_function_impl
*impl
)
90 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
91 reg
->index
= impl
->reg_alloc
++;
97 nir_reg_remove(nir_register
*reg
)
99 exec_node_remove(®
->node
);
103 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
105 switch (var
->data
.mode
) {
106 case nir_var_function_temp
:
107 assert(!"nir_shader_add_variable cannot be used for local variables");
110 case nir_var_shader_temp
:
111 case nir_var_shader_in
:
112 case nir_var_shader_out
:
113 case nir_var_uniform
:
114 case nir_var_mem_ubo
:
115 case nir_var_mem_ssbo
:
116 case nir_var_mem_shared
:
117 case nir_var_system_value
:
120 case nir_var_mem_global
:
121 assert(!"nir_shader_add_variable cannot be used for global memory");
124 case nir_var_mem_push_const
:
125 assert(!"nir_var_push_constant is not supposed to be used for variables");
129 assert(!"invalid mode");
133 exec_list_push_tail(&shader
->variables
, &var
->node
);
137 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
138 const struct glsl_type
*type
, const char *name
)
140 nir_variable
*var
= rzalloc(shader
, nir_variable
);
141 var
->name
= ralloc_strdup(var
, name
);
143 var
->data
.mode
= mode
;
144 var
->data
.how_declared
= nir_var_declared_normally
;
146 if ((mode
== nir_var_shader_in
&&
147 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
148 (mode
== nir_var_shader_out
&&
149 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
150 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
152 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
153 var
->data
.read_only
= true;
155 nir_shader_add_variable(shader
, var
);
161 nir_local_variable_create(nir_function_impl
*impl
,
162 const struct glsl_type
*type
, const char *name
)
164 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
165 var
->name
= ralloc_strdup(var
, name
);
167 var
->data
.mode
= nir_var_function_temp
;
169 nir_function_impl_add_variable(impl
, var
);
175 nir_find_variable_with_location(nir_shader
*shader
,
176 nir_variable_mode mode
,
179 assert(util_bitcount(mode
) == 1 && mode
!= nir_var_function_temp
);
180 nir_foreach_variable_with_modes(var
, shader
, mode
) {
181 if (var
->data
.location
== location
)
188 nir_find_variable_with_driver_location(nir_shader
*shader
,
189 nir_variable_mode mode
,
192 assert(util_bitcount(mode
) == 1 && mode
!= nir_var_function_temp
);
193 nir_foreach_variable_with_modes(var
, shader
, mode
) {
194 if (var
->data
.driver_location
== location
)
201 nir_function_create(nir_shader
*shader
, const char *name
)
203 nir_function
*func
= ralloc(shader
, nir_function
);
205 exec_list_push_tail(&shader
->functions
, &func
->node
);
207 func
->name
= ralloc_strdup(func
, name
);
208 func
->shader
= shader
;
209 func
->num_params
= 0;
212 func
->is_entrypoint
= false;
217 /* NOTE: if the instruction you are copying a src to is already added
218 * to the IR, use nir_instr_rewrite_src() instead.
220 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
222 dest
->is_ssa
= src
->is_ssa
;
224 dest
->ssa
= src
->ssa
;
226 dest
->reg
.base_offset
= src
->reg
.base_offset
;
227 dest
->reg
.reg
= src
->reg
.reg
;
228 if (src
->reg
.indirect
) {
229 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
230 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
232 dest
->reg
.indirect
= NULL
;
237 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
239 /* Copying an SSA definition makes no sense whatsoever. */
240 assert(!src
->is_ssa
);
242 dest
->is_ssa
= false;
244 dest
->reg
.base_offset
= src
->reg
.base_offset
;
245 dest
->reg
.reg
= src
->reg
.reg
;
246 if (src
->reg
.indirect
) {
247 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
248 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
250 dest
->reg
.indirect
= NULL
;
255 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
256 nir_alu_instr
*instr
)
258 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
259 dest
->abs
= src
->abs
;
260 dest
->negate
= src
->negate
;
261 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
262 dest
->swizzle
[i
] = src
->swizzle
[i
];
266 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
267 nir_alu_instr
*instr
)
269 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
270 dest
->write_mask
= src
->write_mask
;
271 dest
->saturate
= src
->saturate
;
276 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
278 exec_node_init(&node
->node
);
284 nir_function_impl_create_bare(nir_shader
*shader
)
286 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
288 impl
->function
= NULL
;
290 cf_init(&impl
->cf_node
, nir_cf_node_function
);
292 exec_list_make_empty(&impl
->body
);
293 exec_list_make_empty(&impl
->registers
);
294 exec_list_make_empty(&impl
->locals
);
297 impl
->valid_metadata
= nir_metadata_none
;
298 impl
->structured
= true;
300 /* create start & end blocks */
301 nir_block
*start_block
= nir_block_create(shader
);
302 nir_block
*end_block
= nir_block_create(shader
);
303 start_block
->cf_node
.parent
= &impl
->cf_node
;
304 end_block
->cf_node
.parent
= &impl
->cf_node
;
305 impl
->end_block
= end_block
;
307 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
309 start_block
->successors
[0] = end_block
;
310 _mesa_set_add(end_block
->predecessors
, start_block
);
315 nir_function_impl_create(nir_function
*function
)
317 assert(function
->impl
== NULL
);
319 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
321 function
->impl
= impl
;
322 impl
->function
= function
;
328 nir_block_create(nir_shader
*shader
)
330 nir_block
*block
= rzalloc(shader
, nir_block
);
332 cf_init(&block
->cf_node
, nir_cf_node_block
);
334 block
->successors
[0] = block
->successors
[1] = NULL
;
335 block
->predecessors
= _mesa_pointer_set_create(block
);
336 block
->imm_dom
= NULL
;
337 /* XXX maybe it would be worth it to defer allocation? This
338 * way it doesn't get allocated for shader refs that never run
339 * nir_calc_dominance? For example, state-tracker creates an
340 * initial IR, clones that, runs appropriate lowering pass, passes
341 * to driver which does common lowering/opt, and then stores ref
342 * which is later used to do state specific lowering and futher
343 * opt. Do any of the references not need dominance metadata?
345 block
->dom_frontier
= _mesa_pointer_set_create(block
);
347 exec_list_make_empty(&block
->instr_list
);
353 src_init(nir_src
*src
)
357 src
->reg
.indirect
= NULL
;
358 src
->reg
.base_offset
= 0;
362 nir_if_create(nir_shader
*shader
)
364 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
366 if_stmt
->control
= nir_selection_control_none
;
368 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
369 src_init(&if_stmt
->condition
);
371 nir_block
*then
= nir_block_create(shader
);
372 exec_list_make_empty(&if_stmt
->then_list
);
373 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
374 then
->cf_node
.parent
= &if_stmt
->cf_node
;
376 nir_block
*else_stmt
= nir_block_create(shader
);
377 exec_list_make_empty(&if_stmt
->else_list
);
378 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
379 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
385 nir_loop_create(nir_shader
*shader
)
387 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
389 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
391 nir_block
*body
= nir_block_create(shader
);
392 exec_list_make_empty(&loop
->body
);
393 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
394 body
->cf_node
.parent
= &loop
->cf_node
;
396 body
->successors
[0] = body
;
397 _mesa_set_add(body
->predecessors
, body
);
403 instr_init(nir_instr
*instr
, nir_instr_type type
)
407 exec_node_init(&instr
->node
);
411 dest_init(nir_dest
*dest
)
413 dest
->is_ssa
= false;
414 dest
->reg
.reg
= NULL
;
415 dest
->reg
.indirect
= NULL
;
416 dest
->reg
.base_offset
= 0;
420 alu_dest_init(nir_alu_dest
*dest
)
422 dest_init(&dest
->dest
);
423 dest
->saturate
= false;
424 dest
->write_mask
= 0xf;
428 alu_src_init(nir_alu_src
*src
)
431 src
->abs
= src
->negate
= false;
432 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
437 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
439 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
440 /* TODO: don't use rzalloc */
441 nir_alu_instr
*instr
=
443 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
445 instr_init(&instr
->instr
, nir_instr_type_alu
);
447 alu_dest_init(&instr
->dest
);
448 for (unsigned i
= 0; i
< num_srcs
; i
++)
449 alu_src_init(&instr
->src
[i
]);
455 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
457 nir_deref_instr
*instr
=
458 rzalloc_size(shader
, sizeof(nir_deref_instr
));
460 instr_init(&instr
->instr
, nir_instr_type_deref
);
462 instr
->deref_type
= deref_type
;
463 if (deref_type
!= nir_deref_type_var
)
464 src_init(&instr
->parent
);
466 if (deref_type
== nir_deref_type_array
||
467 deref_type
== nir_deref_type_ptr_as_array
)
468 src_init(&instr
->arr
.index
);
470 dest_init(&instr
->dest
);
476 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
478 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
479 instr_init(&instr
->instr
, nir_instr_type_jump
);
480 src_init(&instr
->condition
);
482 instr
->target
= NULL
;
483 instr
->else_target
= NULL
;
487 nir_load_const_instr
*
488 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
491 nir_load_const_instr
*instr
=
492 rzalloc_size(shader
, sizeof(*instr
) + num_components
* sizeof(*instr
->value
));
493 instr_init(&instr
->instr
, nir_instr_type_load_const
);
495 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
500 nir_intrinsic_instr
*
501 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
503 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
504 /* TODO: don't use rzalloc */
505 nir_intrinsic_instr
*instr
=
507 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
509 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
510 instr
->intrinsic
= op
;
512 if (nir_intrinsic_infos
[op
].has_dest
)
513 dest_init(&instr
->dest
);
515 for (unsigned i
= 0; i
< num_srcs
; i
++)
516 src_init(&instr
->src
[i
]);
522 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
524 const unsigned num_params
= callee
->num_params
;
525 nir_call_instr
*instr
=
526 rzalloc_size(shader
, sizeof(*instr
) +
527 num_params
* sizeof(instr
->params
[0]));
529 instr_init(&instr
->instr
, nir_instr_type_call
);
530 instr
->callee
= callee
;
531 instr
->num_params
= num_params
;
532 for (unsigned i
= 0; i
< num_params
; i
++)
533 src_init(&instr
->params
[i
]);
538 static int8_t default_tg4_offsets
[4][2] =
547 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
549 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
550 instr_init(&instr
->instr
, nir_instr_type_tex
);
552 dest_init(&instr
->dest
);
554 instr
->num_srcs
= num_srcs
;
555 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
556 for (unsigned i
= 0; i
< num_srcs
; i
++)
557 src_init(&instr
->src
[i
].src
);
559 instr
->texture_index
= 0;
560 instr
->sampler_index
= 0;
561 memcpy(instr
->tg4_offsets
, default_tg4_offsets
, sizeof(instr
->tg4_offsets
));
567 nir_tex_instr_add_src(nir_tex_instr
*tex
,
568 nir_tex_src_type src_type
,
571 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
574 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
575 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
576 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
580 ralloc_free(tex
->src
);
583 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
584 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
589 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
591 assert(src_idx
< tex
->num_srcs
);
593 /* First rewrite the source to NIR_SRC_INIT */
594 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
596 /* Now, move all of the other sources down */
597 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
598 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
599 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
605 nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr
*tex
)
607 if (tex
->op
!= nir_texop_tg4
)
609 return memcmp(tex
->tg4_offsets
, default_tg4_offsets
,
610 sizeof(tex
->tg4_offsets
)) != 0;
614 nir_phi_instr_create(nir_shader
*shader
)
616 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
617 instr_init(&instr
->instr
, nir_instr_type_phi
);
619 dest_init(&instr
->dest
);
620 exec_list_make_empty(&instr
->srcs
);
624 nir_parallel_copy_instr
*
625 nir_parallel_copy_instr_create(nir_shader
*shader
)
627 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
628 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
630 exec_list_make_empty(&instr
->entries
);
635 nir_ssa_undef_instr
*
636 nir_ssa_undef_instr_create(nir_shader
*shader
,
637 unsigned num_components
,
640 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
641 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
643 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
648 static nir_const_value
649 const_value_float(double d
, unsigned bit_size
)
652 memset(&v
, 0, sizeof(v
));
654 case 16: v
.u16
= _mesa_float_to_half(d
); break;
655 case 32: v
.f32
= d
; break;
656 case 64: v
.f64
= d
; break;
658 unreachable("Invalid bit size");
663 static nir_const_value
664 const_value_int(int64_t i
, unsigned bit_size
)
667 memset(&v
, 0, sizeof(v
));
669 case 1: v
.b
= i
& 1; break;
670 case 8: v
.i8
= i
; break;
671 case 16: v
.i16
= i
; break;
672 case 32: v
.i32
= i
; break;
673 case 64: v
.i64
= i
; break;
675 unreachable("Invalid bit size");
681 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
683 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
684 const int64_t min_int
= -max_int
- 1;
687 return const_value_int(0, bit_size
);
689 return const_value_float(0, bit_size
);
691 return const_value_int(1, bit_size
);
693 return const_value_float(1, bit_size
);
695 return const_value_int(max_int
, bit_size
);
697 return const_value_int(~0ull, bit_size
);
699 return const_value_float(INFINITY
, bit_size
);
701 return const_value_int(min_int
, bit_size
);
703 return const_value_int(0, bit_size
);
705 return const_value_float(-INFINITY
, bit_size
);
707 return const_value_int(~0ull, bit_size
);
709 return const_value_int(0, bit_size
);
711 return const_value_int(0, bit_size
);
713 unreachable("Invalid reduction operation");
718 nir_cf_node_get_function(nir_cf_node
*node
)
720 while (node
->type
!= nir_cf_node_function
) {
724 return nir_cf_node_as_function(node
);
727 /* Reduces a cursor by trying to convert everything to after and trying to
728 * go up to block granularity when possible.
731 reduce_cursor(nir_cursor cursor
)
733 switch (cursor
.option
) {
734 case nir_cursor_before_block
:
735 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
736 /* Empty block. After is as good as before. */
737 cursor
.option
= nir_cursor_after_block
;
741 case nir_cursor_after_block
:
744 case nir_cursor_before_instr
: {
745 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
747 /* Before this instruction is after the previous */
748 cursor
.instr
= prev_instr
;
749 cursor
.option
= nir_cursor_after_instr
;
751 /* No previous instruction. Switch to before block */
752 cursor
.block
= cursor
.instr
->block
;
753 cursor
.option
= nir_cursor_before_block
;
755 return reduce_cursor(cursor
);
758 case nir_cursor_after_instr
:
759 if (nir_instr_next(cursor
.instr
) == NULL
) {
760 /* This is the last instruction, switch to after block */
761 cursor
.option
= nir_cursor_after_block
;
762 cursor
.block
= cursor
.instr
->block
;
767 unreachable("Inavlid cursor option");
772 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
774 /* Reduced cursors should be unique */
775 a
= reduce_cursor(a
);
776 b
= reduce_cursor(b
);
778 return a
.block
== b
.block
&& a
.option
== b
.option
;
782 add_use_cb(nir_src
*src
, void *state
)
784 nir_instr
*instr
= state
;
786 src
->parent_instr
= instr
;
787 list_addtail(&src
->use_link
,
788 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
794 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
796 nir_instr
*instr
= state
;
798 if (instr
->block
&& def
->index
== UINT_MAX
) {
799 nir_function_impl
*impl
=
800 nir_cf_node_get_function(&instr
->block
->cf_node
);
802 def
->index
= impl
->ssa_alloc
++;
809 add_reg_def_cb(nir_dest
*dest
, void *state
)
811 nir_instr
*instr
= state
;
814 dest
->reg
.parent_instr
= instr
;
815 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
822 add_defs_uses(nir_instr
*instr
)
824 nir_foreach_src(instr
, add_use_cb
, instr
);
825 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
826 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
830 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
832 switch (cursor
.option
) {
833 case nir_cursor_before_block
:
834 /* Only allow inserting jumps into empty blocks. */
835 if (instr
->type
== nir_instr_type_jump
)
836 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
838 instr
->block
= cursor
.block
;
839 add_defs_uses(instr
);
840 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
842 case nir_cursor_after_block
: {
843 /* Inserting instructions after a jump is illegal. */
844 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
845 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
848 instr
->block
= cursor
.block
;
849 add_defs_uses(instr
);
850 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
853 case nir_cursor_before_instr
:
854 assert(instr
->type
!= nir_instr_type_jump
);
855 instr
->block
= cursor
.instr
->block
;
856 add_defs_uses(instr
);
857 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
859 case nir_cursor_after_instr
:
860 /* Inserting instructions after a jump is illegal. */
861 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
863 /* Only allow inserting jumps at the end of the block. */
864 if (instr
->type
== nir_instr_type_jump
)
865 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
867 instr
->block
= cursor
.instr
->block
;
868 add_defs_uses(instr
);
869 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
873 if (instr
->type
== nir_instr_type_jump
)
874 nir_handle_add_jump(instr
->block
);
878 src_is_valid(const nir_src
*src
)
880 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
884 remove_use_cb(nir_src
*src
, void *state
)
888 if (src_is_valid(src
))
889 list_del(&src
->use_link
);
895 remove_def_cb(nir_dest
*dest
, void *state
)
900 list_del(&dest
->reg
.def_link
);
906 remove_defs_uses(nir_instr
*instr
)
908 nir_foreach_dest(instr
, remove_def_cb
, instr
);
909 nir_foreach_src(instr
, remove_use_cb
, instr
);
912 void nir_instr_remove_v(nir_instr
*instr
)
914 remove_defs_uses(instr
);
915 exec_node_remove(&instr
->node
);
917 if (instr
->type
== nir_instr_type_jump
) {
918 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
919 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
926 nir_index_local_regs(nir_function_impl
*impl
)
929 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
930 reg
->index
= index
++;
932 impl
->reg_alloc
= index
;
936 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
938 return cb(&instr
->dest
.dest
, state
);
942 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
944 return cb(&instr
->dest
, state
);
948 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
951 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
952 return cb(&instr
->dest
, state
);
958 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
961 return cb(&instr
->dest
, state
);
965 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
967 return cb(&instr
->dest
, state
);
971 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
972 nir_foreach_dest_cb cb
, void *state
)
974 nir_foreach_parallel_copy_entry(entry
, instr
) {
975 if (!cb(&entry
->dest
, state
))
983 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
985 switch (instr
->type
) {
986 case nir_instr_type_alu
:
987 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
988 case nir_instr_type_deref
:
989 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
990 case nir_instr_type_intrinsic
:
991 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
992 case nir_instr_type_tex
:
993 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
994 case nir_instr_type_phi
:
995 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
996 case nir_instr_type_parallel_copy
:
997 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1000 case nir_instr_type_load_const
:
1001 case nir_instr_type_ssa_undef
:
1002 case nir_instr_type_call
:
1003 case nir_instr_type_jump
:
1007 unreachable("Invalid instruction type");
1014 struct foreach_ssa_def_state
{
1015 nir_foreach_ssa_def_cb cb
;
1020 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1022 struct foreach_ssa_def_state
*state
= void_state
;
1025 return state
->cb(&dest
->ssa
, state
->client_state
);
1031 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1033 switch (instr
->type
) {
1034 case nir_instr_type_alu
:
1035 case nir_instr_type_deref
:
1036 case nir_instr_type_tex
:
1037 case nir_instr_type_intrinsic
:
1038 case nir_instr_type_phi
:
1039 case nir_instr_type_parallel_copy
: {
1040 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1041 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1044 case nir_instr_type_load_const
:
1045 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1046 case nir_instr_type_ssa_undef
:
1047 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1048 case nir_instr_type_call
:
1049 case nir_instr_type_jump
:
1052 unreachable("Invalid instruction type");
1057 nir_instr_ssa_def(nir_instr
*instr
)
1059 switch (instr
->type
) {
1060 case nir_instr_type_alu
:
1061 assert(nir_instr_as_alu(instr
)->dest
.dest
.is_ssa
);
1062 return &nir_instr_as_alu(instr
)->dest
.dest
.ssa
;
1064 case nir_instr_type_deref
:
1065 assert(nir_instr_as_deref(instr
)->dest
.is_ssa
);
1066 return &nir_instr_as_deref(instr
)->dest
.ssa
;
1068 case nir_instr_type_tex
:
1069 assert(nir_instr_as_tex(instr
)->dest
.is_ssa
);
1070 return &nir_instr_as_tex(instr
)->dest
.ssa
;
1072 case nir_instr_type_intrinsic
: {
1073 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
1074 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
) {
1075 assert(intrin
->dest
.is_ssa
);
1076 return &intrin
->dest
.ssa
;
1082 case nir_instr_type_phi
:
1083 assert(nir_instr_as_phi(instr
)->dest
.is_ssa
);
1084 return &nir_instr_as_phi(instr
)->dest
.ssa
;
1086 case nir_instr_type_parallel_copy
:
1087 unreachable("Parallel copies are unsupported by this function");
1089 case nir_instr_type_load_const
:
1090 return &nir_instr_as_load_const(instr
)->def
;
1092 case nir_instr_type_ssa_undef
:
1093 return &nir_instr_as_ssa_undef(instr
)->def
;
1095 case nir_instr_type_call
:
1096 case nir_instr_type_jump
:
1100 unreachable("Invalid instruction type");
1104 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1106 if (!cb(src
, state
))
1108 if (!src
->is_ssa
&& src
->reg
.indirect
)
1109 return cb(src
->reg
.indirect
, state
);
1114 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1116 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1117 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1124 visit_deref_instr_src(nir_deref_instr
*instr
,
1125 nir_foreach_src_cb cb
, void *state
)
1127 if (instr
->deref_type
!= nir_deref_type_var
) {
1128 if (!visit_src(&instr
->parent
, cb
, state
))
1132 if (instr
->deref_type
== nir_deref_type_array
||
1133 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1134 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1142 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1144 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1145 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1153 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1156 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1157 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1158 if (!visit_src(&instr
->src
[i
], cb
, state
))
1166 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1168 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1169 if (!visit_src(&instr
->params
[i
], cb
, state
))
1177 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1179 nir_foreach_phi_src(src
, instr
) {
1180 if (!visit_src(&src
->src
, cb
, state
))
1188 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1189 nir_foreach_src_cb cb
, void *state
)
1191 nir_foreach_parallel_copy_entry(entry
, instr
) {
1192 if (!visit_src(&entry
->src
, cb
, state
))
1200 visit_jump_src(nir_jump_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1202 if (instr
->type
!= nir_jump_goto_if
)
1205 return visit_src(&instr
->condition
, cb
, state
);
1210 nir_foreach_src_cb cb
;
1211 } visit_dest_indirect_state
;
1214 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1216 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1218 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1219 return state
->cb(dest
->reg
.indirect
, state
->state
);
1225 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1227 switch (instr
->type
) {
1228 case nir_instr_type_alu
:
1229 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1232 case nir_instr_type_deref
:
1233 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1236 case nir_instr_type_intrinsic
:
1237 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1240 case nir_instr_type_tex
:
1241 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1244 case nir_instr_type_call
:
1245 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1248 case nir_instr_type_load_const
:
1249 /* Constant load instructions have no regular sources */
1251 case nir_instr_type_phi
:
1252 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1255 case nir_instr_type_parallel_copy
:
1256 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1260 case nir_instr_type_jump
:
1261 return visit_jump_src(nir_instr_as_jump(instr
), cb
, state
);
1262 case nir_instr_type_ssa_undef
:
1266 unreachable("Invalid instruction type");
1270 visit_dest_indirect_state dest_state
;
1271 dest_state
.state
= state
;
1273 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1277 nir_foreach_phi_src_leaving_block(nir_block
*block
,
1278 nir_foreach_src_cb cb
,
1281 for (unsigned i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
1282 if (block
->successors
[i
] == NULL
)
1285 nir_foreach_instr(instr
, block
->successors
[i
]) {
1286 if (instr
->type
!= nir_instr_type_phi
)
1289 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
1290 nir_foreach_phi_src(phi_src
, phi
) {
1291 if (phi_src
->pred
== block
) {
1292 if (!cb(&phi_src
->src
, state
))
1303 nir_const_value_for_float(double f
, unsigned bit_size
)
1306 memset(&v
, 0, sizeof(v
));
1310 v
.u16
= _mesa_float_to_half(f
);
1319 unreachable("Invalid bit size");
1326 nir_const_value_as_float(nir_const_value value
, unsigned bit_size
)
1329 case 16: return _mesa_half_to_float(value
.u16
);
1330 case 32: return value
.f32
;
1331 case 64: return value
.f64
;
1333 unreachable("Invalid bit size");
1338 nir_src_as_const_value(nir_src src
)
1343 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1346 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1352 * Returns true if the source is known to be dynamically uniform. Otherwise it
1353 * returns false which means it may or may not be dynamically uniform but it
1354 * can't be determined.
1357 nir_src_is_dynamically_uniform(nir_src src
)
1362 /* Constants are trivially dynamically uniform */
1363 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1366 /* As are uniform variables */
1367 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1368 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1369 if (intr
->intrinsic
== nir_intrinsic_load_uniform
&&
1370 nir_src_is_dynamically_uniform(intr
->src
[0]))
1374 /* Operating together dynamically uniform expressions produces a
1375 * dynamically uniform result
1377 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
1378 nir_alu_instr
*alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
1379 for (int i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
1380 if (!nir_src_is_dynamically_uniform(alu
->src
[i
].src
))
1387 /* XXX: this could have many more tests, such as when a sampler function is
1388 * called with dynamically uniform arguments.
1394 src_remove_all_uses(nir_src
*src
)
1396 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1397 if (!src_is_valid(src
))
1400 list_del(&src
->use_link
);
1405 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1407 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1408 if (!src_is_valid(src
))
1412 src
->parent_instr
= parent_instr
;
1414 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1416 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1419 src
->parent_if
= parent_if
;
1421 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1423 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1429 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1431 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1433 src_remove_all_uses(src
);
1435 src_add_all_uses(src
, instr
, NULL
);
1439 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1441 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1443 src_remove_all_uses(dest
);
1444 src_remove_all_uses(src
);
1446 *src
= NIR_SRC_INIT
;
1447 src_add_all_uses(dest
, dest_instr
, NULL
);
1451 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1453 nir_src
*src
= &if_stmt
->condition
;
1454 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1456 src_remove_all_uses(src
);
1458 src_add_all_uses(src
, NULL
, if_stmt
);
1462 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1465 /* We can only overwrite an SSA destination if it has no uses. */
1466 assert(list_is_empty(&dest
->ssa
.uses
) && list_is_empty(&dest
->ssa
.if_uses
));
1468 list_del(&dest
->reg
.def_link
);
1469 if (dest
->reg
.indirect
)
1470 src_remove_all_uses(dest
->reg
.indirect
);
1473 /* We can't re-write with an SSA def */
1474 assert(!new_dest
.is_ssa
);
1476 nir_dest_copy(dest
, &new_dest
, instr
);
1478 dest
->reg
.parent_instr
= instr
;
1479 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1481 if (dest
->reg
.indirect
)
1482 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1485 /* note: does *not* take ownership of 'name' */
1487 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1488 unsigned num_components
,
1489 unsigned bit_size
, const char *name
)
1491 def
->name
= ralloc_strdup(instr
, name
);
1492 def
->parent_instr
= instr
;
1493 list_inithead(&def
->uses
);
1494 list_inithead(&def
->if_uses
);
1495 def
->num_components
= num_components
;
1496 def
->bit_size
= bit_size
;
1497 def
->divergent
= true; /* This is the safer default */
1500 nir_function_impl
*impl
=
1501 nir_cf_node_get_function(&instr
->block
->cf_node
);
1503 def
->index
= impl
->ssa_alloc
++;
1505 def
->index
= UINT_MAX
;
1509 /* note: does *not* take ownership of 'name' */
1511 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1512 unsigned num_components
, unsigned bit_size
,
1515 dest
->is_ssa
= true;
1516 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1520 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1522 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1524 nir_foreach_use_safe(use_src
, def
)
1525 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1527 nir_foreach_if_use_safe(use_src
, def
)
1528 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1532 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1534 assert(start
->block
== end
->block
);
1536 if (between
->block
!= start
->block
)
1539 /* Search backwards looking for "between" */
1540 while (start
!= end
) {
1544 end
= nir_instr_prev(end
);
1551 /* Replaces all uses of the given SSA def with the given source but only if
1552 * the use comes after the after_me instruction. This can be useful if you
1553 * are emitting code to fix up the result of some instruction: you can freely
1554 * use the result in that code and then call rewrite_uses_after and pass the
1555 * last fixup instruction as after_me and it will replace all of the uses you
1556 * want without touching the fixup code.
1558 * This function assumes that after_me is in the same block as
1559 * def->parent_instr and that after_me comes after def->parent_instr.
1562 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1563 nir_instr
*after_me
)
1565 if (new_src
.is_ssa
&& def
== new_src
.ssa
)
1568 nir_foreach_use_safe(use_src
, def
) {
1569 assert(use_src
->parent_instr
!= def
->parent_instr
);
1570 /* Since def already dominates all of its uses, the only way a use can
1571 * not be dominated by after_me is if it is between def and after_me in
1572 * the instruction list.
1574 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1575 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1578 nir_foreach_if_use_safe(use_src
, def
)
1579 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1582 nir_component_mask_t
1583 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1585 nir_component_mask_t read_mask
= 0;
1586 nir_foreach_use(use
, def
) {
1587 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1588 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1589 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1590 int src_idx
= alu_src
- &alu
->src
[0];
1591 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1592 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1594 return (1 << def
->num_components
) - 1;
1598 if (!list_is_empty(&def
->if_uses
))
1605 nir_block_unstructured_next(nir_block
*block
)
1607 if (block
== NULL
) {
1608 /* nir_foreach_block_unstructured_safe() will call this function on a
1609 * NULL block after the last iteration, but it won't use the result so
1610 * just return NULL here.
1615 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1616 if (cf_next
== NULL
&& block
->cf_node
.parent
->type
== nir_cf_node_function
)
1619 if (cf_next
&& cf_next
->type
== nir_cf_node_block
)
1620 return nir_cf_node_as_block(cf_next
);
1622 return nir_block_cf_tree_next(block
);
1626 nir_unstructured_start_block(nir_function_impl
*impl
)
1628 return nir_start_block(impl
);
1632 nir_block_cf_tree_next(nir_block
*block
)
1634 if (block
== NULL
) {
1635 /* nir_foreach_block_safe() will call this function on a NULL block
1636 * after the last iteration, but it won't use the result so just return
1642 assert(nir_cf_node_get_function(&block
->cf_node
)->structured
);
1644 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1646 return nir_cf_node_cf_tree_first(cf_next
);
1648 nir_cf_node
*parent
= block
->cf_node
.parent
;
1650 switch (parent
->type
) {
1651 case nir_cf_node_if
: {
1652 /* Are we at the end of the if? Go to the beginning of the else */
1653 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1654 if (block
== nir_if_last_then_block(if_stmt
))
1655 return nir_if_first_else_block(if_stmt
);
1657 assert(block
== nir_if_last_else_block(if_stmt
));
1661 case nir_cf_node_loop
:
1662 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1664 case nir_cf_node_function
:
1668 unreachable("unknown cf node type");
1673 nir_block_cf_tree_prev(nir_block
*block
)
1675 if (block
== NULL
) {
1676 /* do this for consistency with nir_block_cf_tree_next() */
1680 assert(nir_cf_node_get_function(&block
->cf_node
)->structured
);
1682 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1684 return nir_cf_node_cf_tree_last(cf_prev
);
1686 nir_cf_node
*parent
= block
->cf_node
.parent
;
1688 switch (parent
->type
) {
1689 case nir_cf_node_if
: {
1690 /* Are we at the beginning of the else? Go to the end of the if */
1691 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1692 if (block
== nir_if_first_else_block(if_stmt
))
1693 return nir_if_last_then_block(if_stmt
);
1695 assert(block
== nir_if_first_then_block(if_stmt
));
1699 case nir_cf_node_loop
:
1700 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1702 case nir_cf_node_function
:
1706 unreachable("unknown cf node type");
1710 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1712 switch (node
->type
) {
1713 case nir_cf_node_function
: {
1714 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1715 return nir_start_block(impl
);
1718 case nir_cf_node_if
: {
1719 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1720 return nir_if_first_then_block(if_stmt
);
1723 case nir_cf_node_loop
: {
1724 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1725 return nir_loop_first_block(loop
);
1728 case nir_cf_node_block
: {
1729 return nir_cf_node_as_block(node
);
1733 unreachable("unknown node type");
1737 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1739 switch (node
->type
) {
1740 case nir_cf_node_function
: {
1741 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1742 return nir_impl_last_block(impl
);
1745 case nir_cf_node_if
: {
1746 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1747 return nir_if_last_else_block(if_stmt
);
1750 case nir_cf_node_loop
: {
1751 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1752 return nir_loop_last_block(loop
);
1755 case nir_cf_node_block
: {
1756 return nir_cf_node_as_block(node
);
1760 unreachable("unknown node type");
1764 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1766 if (node
->type
== nir_cf_node_block
)
1767 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1768 else if (node
->type
== nir_cf_node_function
)
1771 return nir_cf_node_as_block(nir_cf_node_next(node
));
1775 nir_block_get_following_if(nir_block
*block
)
1777 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1780 if (nir_cf_node_is_last(&block
->cf_node
))
1783 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1785 if (next_node
->type
!= nir_cf_node_if
)
1788 return nir_cf_node_as_if(next_node
);
1792 nir_block_get_following_loop(nir_block
*block
)
1794 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1797 if (nir_cf_node_is_last(&block
->cf_node
))
1800 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1802 if (next_node
->type
!= nir_cf_node_loop
)
1805 return nir_cf_node_as_loop(next_node
);
1809 nir_index_blocks(nir_function_impl
*impl
)
1813 if (impl
->valid_metadata
& nir_metadata_block_index
)
1816 nir_foreach_block_unstructured(block
, impl
) {
1817 block
->index
= index
++;
1820 /* The end_block isn't really part of the program, which is why its index
1823 impl
->num_blocks
= impl
->end_block
->index
= index
;
1827 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1829 unsigned *index
= (unsigned *) state
;
1830 def
->index
= (*index
)++;
1836 * The indices are applied top-to-bottom which has the very nice property
1837 * that, if A dominates B, then A->index <= B->index.
1840 nir_index_ssa_defs(nir_function_impl
*impl
)
1844 nir_foreach_block_unstructured(block
, impl
) {
1845 nir_foreach_instr(instr
, block
)
1846 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1849 impl
->ssa_alloc
= index
;
1853 * The indices are applied top-to-bottom which has the very nice property
1854 * that, if A dominates B, then A->index <= B->index.
1857 nir_index_instrs(nir_function_impl
*impl
)
1861 nir_foreach_block(block
, impl
) {
1862 nir_foreach_instr(instr
, block
)
1863 instr
->index
= index
++;
1870 nir_shader_index_vars(nir_shader
*shader
, nir_variable_mode modes
)
1873 nir_foreach_variable_with_modes(var
, shader
, modes
)
1874 var
->index
= count
++;
1879 nir_function_impl_index_vars(nir_function_impl
*impl
)
1882 nir_foreach_function_temp_variable(var
, impl
)
1883 var
->index
= count
++;
1888 cursor_next_instr(nir_cursor cursor
)
1890 switch (cursor
.option
) {
1891 case nir_cursor_before_block
:
1892 for (nir_block
*block
= cursor
.block
; block
;
1893 block
= nir_block_cf_tree_next(block
)) {
1894 nir_instr
*instr
= nir_block_first_instr(block
);
1900 case nir_cursor_after_block
:
1901 cursor
.block
= nir_block_cf_tree_next(cursor
.block
);
1902 if (cursor
.block
== NULL
)
1905 cursor
.option
= nir_cursor_before_block
;
1906 return cursor_next_instr(cursor
);
1908 case nir_cursor_before_instr
:
1909 return cursor
.instr
;
1911 case nir_cursor_after_instr
:
1912 if (nir_instr_next(cursor
.instr
))
1913 return nir_instr_next(cursor
.instr
);
1915 cursor
.option
= nir_cursor_after_block
;
1916 cursor
.block
= cursor
.instr
->block
;
1917 return cursor_next_instr(cursor
);
1920 unreachable("Inavlid cursor option");
1923 ASSERTED
static bool
1924 dest_is_ssa(nir_dest
*dest
, void *_state
)
1927 return dest
->is_ssa
;
1931 nir_function_impl_lower_instructions(nir_function_impl
*impl
,
1932 nir_instr_filter_cb filter
,
1933 nir_lower_instr_cb lower
,
1937 nir_builder_init(&b
, impl
);
1939 nir_metadata preserved
= nir_metadata_block_index
|
1940 nir_metadata_dominance
;
1942 bool progress
= false;
1943 nir_cursor iter
= nir_before_cf_list(&impl
->body
);
1945 while ((instr
= cursor_next_instr(iter
)) != NULL
) {
1946 if (filter
&& !filter(instr
, cb_data
)) {
1947 iter
= nir_after_instr(instr
);
1951 assert(nir_foreach_dest(instr
, dest_is_ssa
, NULL
));
1952 nir_ssa_def
*old_def
= nir_instr_ssa_def(instr
);
1953 if (old_def
== NULL
) {
1954 iter
= nir_after_instr(instr
);
1958 /* We're about to ask the callback to generate a replacement for instr.
1959 * Save off the uses from instr's SSA def so we know what uses to
1960 * rewrite later. If we use nir_ssa_def_rewrite_uses, it fails in the
1961 * case where the generated replacement code uses the result of instr
1962 * itself. If we use nir_ssa_def_rewrite_uses_after (which is the
1963 * normal solution to this problem), it doesn't work well if control-
1964 * flow is inserted as part of the replacement, doesn't handle cases
1965 * where the replacement is something consumed by instr, and suffers
1966 * from performance issues. This is the only way to 100% guarantee
1967 * that we rewrite the correct set efficiently.
1969 struct list_head old_uses
, old_if_uses
;
1970 list_replace(&old_def
->uses
, &old_uses
);
1971 list_inithead(&old_def
->uses
);
1972 list_replace(&old_def
->if_uses
, &old_if_uses
);
1973 list_inithead(&old_def
->if_uses
);
1975 b
.cursor
= nir_after_instr(instr
);
1976 nir_ssa_def
*new_def
= lower(&b
, instr
, cb_data
);
1977 if (new_def
&& new_def
!= NIR_LOWER_INSTR_PROGRESS
) {
1978 assert(old_def
!= NULL
);
1979 if (new_def
->parent_instr
->block
!= instr
->block
)
1980 preserved
= nir_metadata_none
;
1982 nir_src new_src
= nir_src_for_ssa(new_def
);
1983 list_for_each_entry_safe(nir_src
, use_src
, &old_uses
, use_link
)
1984 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1986 list_for_each_entry_safe(nir_src
, use_src
, &old_if_uses
, use_link
)
1987 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1989 if (list_is_empty(&old_def
->uses
) && list_is_empty(&old_def
->if_uses
)) {
1990 iter
= nir_instr_remove(instr
);
1992 iter
= nir_after_instr(instr
);
1996 /* We didn't end up lowering after all. Put the uses back */
1998 list_replace(&old_uses
, &old_def
->uses
);
1999 list_replace(&old_if_uses
, &old_def
->if_uses
);
2001 iter
= nir_after_instr(instr
);
2003 if (new_def
== NIR_LOWER_INSTR_PROGRESS
)
2009 nir_metadata_preserve(impl
, preserved
);
2011 nir_metadata_preserve(impl
, nir_metadata_all
);
2018 nir_shader_lower_instructions(nir_shader
*shader
,
2019 nir_instr_filter_cb filter
,
2020 nir_lower_instr_cb lower
,
2023 bool progress
= false;
2025 nir_foreach_function(function
, shader
) {
2026 if (function
->impl
&&
2027 nir_function_impl_lower_instructions(function
->impl
,
2028 filter
, lower
, cb_data
))
2036 nir_intrinsic_from_system_value(gl_system_value val
)
2039 case SYSTEM_VALUE_VERTEX_ID
:
2040 return nir_intrinsic_load_vertex_id
;
2041 case SYSTEM_VALUE_INSTANCE_ID
:
2042 return nir_intrinsic_load_instance_id
;
2043 case SYSTEM_VALUE_DRAW_ID
:
2044 return nir_intrinsic_load_draw_id
;
2045 case SYSTEM_VALUE_BASE_INSTANCE
:
2046 return nir_intrinsic_load_base_instance
;
2047 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
2048 return nir_intrinsic_load_vertex_id_zero_base
;
2049 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
2050 return nir_intrinsic_load_is_indexed_draw
;
2051 case SYSTEM_VALUE_FIRST_VERTEX
:
2052 return nir_intrinsic_load_first_vertex
;
2053 case SYSTEM_VALUE_BASE_VERTEX
:
2054 return nir_intrinsic_load_base_vertex
;
2055 case SYSTEM_VALUE_INVOCATION_ID
:
2056 return nir_intrinsic_load_invocation_id
;
2057 case SYSTEM_VALUE_FRAG_COORD
:
2058 return nir_intrinsic_load_frag_coord
;
2059 case SYSTEM_VALUE_POINT_COORD
:
2060 return nir_intrinsic_load_point_coord
;
2061 case SYSTEM_VALUE_LINE_COORD
:
2062 return nir_intrinsic_load_line_coord
;
2063 case SYSTEM_VALUE_FRONT_FACE
:
2064 return nir_intrinsic_load_front_face
;
2065 case SYSTEM_VALUE_SAMPLE_ID
:
2066 return nir_intrinsic_load_sample_id
;
2067 case SYSTEM_VALUE_SAMPLE_POS
:
2068 return nir_intrinsic_load_sample_pos
;
2069 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
2070 return nir_intrinsic_load_sample_mask_in
;
2071 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
2072 return nir_intrinsic_load_local_invocation_id
;
2073 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
2074 return nir_intrinsic_load_local_invocation_index
;
2075 case SYSTEM_VALUE_WORK_GROUP_ID
:
2076 return nir_intrinsic_load_work_group_id
;
2077 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
2078 return nir_intrinsic_load_num_work_groups
;
2079 case SYSTEM_VALUE_PRIMITIVE_ID
:
2080 return nir_intrinsic_load_primitive_id
;
2081 case SYSTEM_VALUE_TESS_COORD
:
2082 return nir_intrinsic_load_tess_coord
;
2083 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
2084 return nir_intrinsic_load_tess_level_outer
;
2085 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
2086 return nir_intrinsic_load_tess_level_inner
;
2087 case SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
:
2088 return nir_intrinsic_load_tess_level_outer_default
;
2089 case SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
:
2090 return nir_intrinsic_load_tess_level_inner_default
;
2091 case SYSTEM_VALUE_VERTICES_IN
:
2092 return nir_intrinsic_load_patch_vertices_in
;
2093 case SYSTEM_VALUE_HELPER_INVOCATION
:
2094 return nir_intrinsic_load_helper_invocation
;
2095 case SYSTEM_VALUE_COLOR0
:
2096 return nir_intrinsic_load_color0
;
2097 case SYSTEM_VALUE_COLOR1
:
2098 return nir_intrinsic_load_color1
;
2099 case SYSTEM_VALUE_VIEW_INDEX
:
2100 return nir_intrinsic_load_view_index
;
2101 case SYSTEM_VALUE_SUBGROUP_SIZE
:
2102 return nir_intrinsic_load_subgroup_size
;
2103 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
2104 return nir_intrinsic_load_subgroup_invocation
;
2105 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
2106 return nir_intrinsic_load_subgroup_eq_mask
;
2107 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
2108 return nir_intrinsic_load_subgroup_ge_mask
;
2109 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
2110 return nir_intrinsic_load_subgroup_gt_mask
;
2111 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
2112 return nir_intrinsic_load_subgroup_le_mask
;
2113 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
2114 return nir_intrinsic_load_subgroup_lt_mask
;
2115 case SYSTEM_VALUE_NUM_SUBGROUPS
:
2116 return nir_intrinsic_load_num_subgroups
;
2117 case SYSTEM_VALUE_SUBGROUP_ID
:
2118 return nir_intrinsic_load_subgroup_id
;
2119 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
2120 return nir_intrinsic_load_local_group_size
;
2121 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
2122 return nir_intrinsic_load_global_invocation_id
;
2123 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
:
2124 return nir_intrinsic_load_global_invocation_index
;
2125 case SYSTEM_VALUE_WORK_DIM
:
2126 return nir_intrinsic_load_work_dim
;
2127 case SYSTEM_VALUE_USER_DATA_AMD
:
2128 return nir_intrinsic_load_user_data_amd
;
2130 unreachable("system value does not directly correspond to intrinsic");
2135 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
2138 case nir_intrinsic_load_vertex_id
:
2139 return SYSTEM_VALUE_VERTEX_ID
;
2140 case nir_intrinsic_load_instance_id
:
2141 return SYSTEM_VALUE_INSTANCE_ID
;
2142 case nir_intrinsic_load_draw_id
:
2143 return SYSTEM_VALUE_DRAW_ID
;
2144 case nir_intrinsic_load_base_instance
:
2145 return SYSTEM_VALUE_BASE_INSTANCE
;
2146 case nir_intrinsic_load_vertex_id_zero_base
:
2147 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2148 case nir_intrinsic_load_first_vertex
:
2149 return SYSTEM_VALUE_FIRST_VERTEX
;
2150 case nir_intrinsic_load_is_indexed_draw
:
2151 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
2152 case nir_intrinsic_load_base_vertex
:
2153 return SYSTEM_VALUE_BASE_VERTEX
;
2154 case nir_intrinsic_load_invocation_id
:
2155 return SYSTEM_VALUE_INVOCATION_ID
;
2156 case nir_intrinsic_load_frag_coord
:
2157 return SYSTEM_VALUE_FRAG_COORD
;
2158 case nir_intrinsic_load_point_coord
:
2159 return SYSTEM_VALUE_POINT_COORD
;
2160 case nir_intrinsic_load_line_coord
:
2161 return SYSTEM_VALUE_LINE_COORD
;
2162 case nir_intrinsic_load_front_face
:
2163 return SYSTEM_VALUE_FRONT_FACE
;
2164 case nir_intrinsic_load_sample_id
:
2165 return SYSTEM_VALUE_SAMPLE_ID
;
2166 case nir_intrinsic_load_sample_pos
:
2167 return SYSTEM_VALUE_SAMPLE_POS
;
2168 case nir_intrinsic_load_sample_mask_in
:
2169 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
2170 case nir_intrinsic_load_local_invocation_id
:
2171 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
2172 case nir_intrinsic_load_local_invocation_index
:
2173 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
2174 case nir_intrinsic_load_num_work_groups
:
2175 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2176 case nir_intrinsic_load_work_group_id
:
2177 return SYSTEM_VALUE_WORK_GROUP_ID
;
2178 case nir_intrinsic_load_primitive_id
:
2179 return SYSTEM_VALUE_PRIMITIVE_ID
;
2180 case nir_intrinsic_load_tess_coord
:
2181 return SYSTEM_VALUE_TESS_COORD
;
2182 case nir_intrinsic_load_tess_level_outer
:
2183 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2184 case nir_intrinsic_load_tess_level_inner
:
2185 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
2186 case nir_intrinsic_load_tess_level_outer_default
:
2187 return SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
;
2188 case nir_intrinsic_load_tess_level_inner_default
:
2189 return SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
;
2190 case nir_intrinsic_load_patch_vertices_in
:
2191 return SYSTEM_VALUE_VERTICES_IN
;
2192 case nir_intrinsic_load_helper_invocation
:
2193 return SYSTEM_VALUE_HELPER_INVOCATION
;
2194 case nir_intrinsic_load_color0
:
2195 return SYSTEM_VALUE_COLOR0
;
2196 case nir_intrinsic_load_color1
:
2197 return SYSTEM_VALUE_COLOR1
;
2198 case nir_intrinsic_load_view_index
:
2199 return SYSTEM_VALUE_VIEW_INDEX
;
2200 case nir_intrinsic_load_subgroup_size
:
2201 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2202 case nir_intrinsic_load_subgroup_invocation
:
2203 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2204 case nir_intrinsic_load_subgroup_eq_mask
:
2205 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2206 case nir_intrinsic_load_subgroup_ge_mask
:
2207 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2208 case nir_intrinsic_load_subgroup_gt_mask
:
2209 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2210 case nir_intrinsic_load_subgroup_le_mask
:
2211 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2212 case nir_intrinsic_load_subgroup_lt_mask
:
2213 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2214 case nir_intrinsic_load_num_subgroups
:
2215 return SYSTEM_VALUE_NUM_SUBGROUPS
;
2216 case nir_intrinsic_load_subgroup_id
:
2217 return SYSTEM_VALUE_SUBGROUP_ID
;
2218 case nir_intrinsic_load_local_group_size
:
2219 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2220 case nir_intrinsic_load_global_invocation_id
:
2221 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
2222 case nir_intrinsic_load_user_data_amd
:
2223 return SYSTEM_VALUE_USER_DATA_AMD
;
2225 unreachable("intrinsic doesn't produce a system value");
2229 /* OpenGL utility method that remaps the location attributes if they are
2230 * doubles. Not needed for vulkan due the differences on the input location
2231 * count for doubles on vulkan vs OpenGL
2233 * The bitfield returned in dual_slot is one bit for each double input slot in
2234 * the original OpenGL single-slot input numbering. The mapping from old
2235 * locations to new locations is as follows:
2237 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
2240 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
2242 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
2245 nir_foreach_shader_in_variable(var
, shader
) {
2246 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
2247 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
2248 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
2252 nir_foreach_shader_in_variable(var
, shader
) {
2253 var
->data
.location
+=
2254 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
2258 /* Returns an attribute mask that has been re-compacted using the given
2262 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
2265 unsigned loc
= u_bit_scan64(&dual_slot
);
2266 /* mask of all bits up to and including loc */
2267 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
2268 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);
2274 nir_rewrite_image_intrinsic(nir_intrinsic_instr
*intrin
, nir_ssa_def
*src
,
2277 enum gl_access_qualifier access
= nir_intrinsic_access(intrin
);
2279 switch (intrin
->intrinsic
) {
2281 case nir_intrinsic_image_deref_##op: \
2282 intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
2283 : nir_intrinsic_image_##op; \
2295 CASE(atomic_exchange
)
2296 CASE(atomic_comp_swap
)
2298 CASE(atomic_inc_wrap
)
2299 CASE(atomic_dec_wrap
)
2302 CASE(load_raw_intel
)
2303 CASE(store_raw_intel
)
2306 unreachable("Unhanded image intrinsic");
2309 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
2310 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2312 nir_intrinsic_set_image_dim(intrin
, glsl_get_sampler_dim(deref
->type
));
2313 nir_intrinsic_set_image_array(intrin
, glsl_sampler_type_is_array(deref
->type
));
2314 nir_intrinsic_set_access(intrin
, access
| var
->data
.access
);
2315 nir_intrinsic_set_format(intrin
, var
->data
.image
.format
);
2317 nir_instr_rewrite_src(&intrin
->instr
, &intrin
->src
[0],
2318 nir_src_for_ssa(src
));
2322 nir_image_intrinsic_coord_components(const nir_intrinsic_instr
*instr
)
2324 enum glsl_sampler_dim dim
= nir_intrinsic_image_dim(instr
);
2325 int coords
= glsl_get_sampler_dim_coordinate_components(dim
);
2326 if (dim
== GLSL_SAMPLER_DIM_CUBE
)
2329 return coords
+ nir_intrinsic_image_array(instr
);