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
->shared_size
= 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
:
118 case nir_var_mem_constant
:
121 case nir_var_mem_global
:
122 assert(!"nir_shader_add_variable cannot be used for global memory");
125 case nir_var_mem_push_const
:
126 assert(!"nir_var_push_constant is not supposed to be used for variables");
130 assert(!"invalid mode");
134 exec_list_push_tail(&shader
->variables
, &var
->node
);
138 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
139 const struct glsl_type
*type
, const char *name
)
141 nir_variable
*var
= rzalloc(shader
, nir_variable
);
142 var
->name
= ralloc_strdup(var
, name
);
144 var
->data
.mode
= mode
;
145 var
->data
.how_declared
= nir_var_declared_normally
;
147 if ((mode
== nir_var_shader_in
&&
148 shader
->info
.stage
!= MESA_SHADER_VERTEX
&&
149 shader
->info
.stage
!= MESA_SHADER_KERNEL
) ||
150 (mode
== nir_var_shader_out
&&
151 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
152 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
154 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
155 var
->data
.read_only
= true;
157 nir_shader_add_variable(shader
, var
);
163 nir_local_variable_create(nir_function_impl
*impl
,
164 const struct glsl_type
*type
, const char *name
)
166 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
167 var
->name
= ralloc_strdup(var
, name
);
169 var
->data
.mode
= nir_var_function_temp
;
171 nir_function_impl_add_variable(impl
, var
);
177 nir_find_variable_with_location(nir_shader
*shader
,
178 nir_variable_mode mode
,
181 assert(util_bitcount(mode
) == 1 && mode
!= nir_var_function_temp
);
182 nir_foreach_variable_with_modes(var
, shader
, mode
) {
183 if (var
->data
.location
== location
)
190 nir_find_variable_with_driver_location(nir_shader
*shader
,
191 nir_variable_mode mode
,
194 assert(util_bitcount(mode
) == 1 && mode
!= nir_var_function_temp
);
195 nir_foreach_variable_with_modes(var
, shader
, mode
) {
196 if (var
->data
.driver_location
== location
)
203 nir_function_create(nir_shader
*shader
, const char *name
)
205 nir_function
*func
= ralloc(shader
, nir_function
);
207 exec_list_push_tail(&shader
->functions
, &func
->node
);
209 func
->name
= ralloc_strdup(func
, name
);
210 func
->shader
= shader
;
211 func
->num_params
= 0;
214 func
->is_entrypoint
= false;
219 /* NOTE: if the instruction you are copying a src to is already added
220 * to the IR, use nir_instr_rewrite_src() instead.
222 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
224 dest
->is_ssa
= src
->is_ssa
;
226 dest
->ssa
= src
->ssa
;
228 dest
->reg
.base_offset
= src
->reg
.base_offset
;
229 dest
->reg
.reg
= src
->reg
.reg
;
230 if (src
->reg
.indirect
) {
231 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
232 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
234 dest
->reg
.indirect
= NULL
;
239 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
241 /* Copying an SSA definition makes no sense whatsoever. */
242 assert(!src
->is_ssa
);
244 dest
->is_ssa
= false;
246 dest
->reg
.base_offset
= src
->reg
.base_offset
;
247 dest
->reg
.reg
= src
->reg
.reg
;
248 if (src
->reg
.indirect
) {
249 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
250 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
252 dest
->reg
.indirect
= NULL
;
257 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
258 nir_alu_instr
*instr
)
260 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
261 dest
->abs
= src
->abs
;
262 dest
->negate
= src
->negate
;
263 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
264 dest
->swizzle
[i
] = src
->swizzle
[i
];
268 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
269 nir_alu_instr
*instr
)
271 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
272 dest
->write_mask
= src
->write_mask
;
273 dest
->saturate
= src
->saturate
;
278 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
280 exec_node_init(&node
->node
);
286 nir_function_impl_create_bare(nir_shader
*shader
)
288 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
290 impl
->function
= NULL
;
292 cf_init(&impl
->cf_node
, nir_cf_node_function
);
294 exec_list_make_empty(&impl
->body
);
295 exec_list_make_empty(&impl
->registers
);
296 exec_list_make_empty(&impl
->locals
);
299 impl
->valid_metadata
= nir_metadata_none
;
300 impl
->structured
= true;
302 /* create start & end blocks */
303 nir_block
*start_block
= nir_block_create(shader
);
304 nir_block
*end_block
= nir_block_create(shader
);
305 start_block
->cf_node
.parent
= &impl
->cf_node
;
306 end_block
->cf_node
.parent
= &impl
->cf_node
;
307 impl
->end_block
= end_block
;
309 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
311 start_block
->successors
[0] = end_block
;
312 _mesa_set_add(end_block
->predecessors
, start_block
);
317 nir_function_impl_create(nir_function
*function
)
319 assert(function
->impl
== NULL
);
321 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
323 function
->impl
= impl
;
324 impl
->function
= function
;
330 nir_block_create(nir_shader
*shader
)
332 nir_block
*block
= rzalloc(shader
, nir_block
);
334 cf_init(&block
->cf_node
, nir_cf_node_block
);
336 block
->successors
[0] = block
->successors
[1] = NULL
;
337 block
->predecessors
= _mesa_pointer_set_create(block
);
338 block
->imm_dom
= NULL
;
339 /* XXX maybe it would be worth it to defer allocation? This
340 * way it doesn't get allocated for shader refs that never run
341 * nir_calc_dominance? For example, state-tracker creates an
342 * initial IR, clones that, runs appropriate lowering pass, passes
343 * to driver which does common lowering/opt, and then stores ref
344 * which is later used to do state specific lowering and futher
345 * opt. Do any of the references not need dominance metadata?
347 block
->dom_frontier
= _mesa_pointer_set_create(block
);
349 exec_list_make_empty(&block
->instr_list
);
355 src_init(nir_src
*src
)
359 src
->reg
.indirect
= NULL
;
360 src
->reg
.base_offset
= 0;
364 nir_if_create(nir_shader
*shader
)
366 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
368 if_stmt
->control
= nir_selection_control_none
;
370 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
371 src_init(&if_stmt
->condition
);
373 nir_block
*then
= nir_block_create(shader
);
374 exec_list_make_empty(&if_stmt
->then_list
);
375 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
376 then
->cf_node
.parent
= &if_stmt
->cf_node
;
378 nir_block
*else_stmt
= nir_block_create(shader
);
379 exec_list_make_empty(&if_stmt
->else_list
);
380 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
381 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
387 nir_loop_create(nir_shader
*shader
)
389 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
391 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
393 nir_block
*body
= nir_block_create(shader
);
394 exec_list_make_empty(&loop
->body
);
395 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
396 body
->cf_node
.parent
= &loop
->cf_node
;
398 body
->successors
[0] = body
;
399 _mesa_set_add(body
->predecessors
, body
);
405 instr_init(nir_instr
*instr
, nir_instr_type type
)
409 exec_node_init(&instr
->node
);
413 dest_init(nir_dest
*dest
)
415 dest
->is_ssa
= false;
416 dest
->reg
.reg
= NULL
;
417 dest
->reg
.indirect
= NULL
;
418 dest
->reg
.base_offset
= 0;
422 alu_dest_init(nir_alu_dest
*dest
)
424 dest_init(&dest
->dest
);
425 dest
->saturate
= false;
426 dest
->write_mask
= 0xf;
430 alu_src_init(nir_alu_src
*src
)
433 src
->abs
= src
->negate
= false;
434 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
439 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
441 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
442 /* TODO: don't use rzalloc */
443 nir_alu_instr
*instr
=
445 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
447 instr_init(&instr
->instr
, nir_instr_type_alu
);
449 alu_dest_init(&instr
->dest
);
450 for (unsigned i
= 0; i
< num_srcs
; i
++)
451 alu_src_init(&instr
->src
[i
]);
457 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
459 nir_deref_instr
*instr
=
460 rzalloc_size(shader
, sizeof(nir_deref_instr
));
462 instr_init(&instr
->instr
, nir_instr_type_deref
);
464 instr
->deref_type
= deref_type
;
465 if (deref_type
!= nir_deref_type_var
)
466 src_init(&instr
->parent
);
468 if (deref_type
== nir_deref_type_array
||
469 deref_type
== nir_deref_type_ptr_as_array
)
470 src_init(&instr
->arr
.index
);
472 dest_init(&instr
->dest
);
478 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
480 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
481 instr_init(&instr
->instr
, nir_instr_type_jump
);
482 src_init(&instr
->condition
);
484 instr
->target
= NULL
;
485 instr
->else_target
= NULL
;
489 nir_load_const_instr
*
490 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
493 nir_load_const_instr
*instr
=
494 rzalloc_size(shader
, sizeof(*instr
) + num_components
* sizeof(*instr
->value
));
495 instr_init(&instr
->instr
, nir_instr_type_load_const
);
497 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
502 nir_intrinsic_instr
*
503 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
505 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
506 /* TODO: don't use rzalloc */
507 nir_intrinsic_instr
*instr
=
509 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
511 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
512 instr
->intrinsic
= op
;
514 if (nir_intrinsic_infos
[op
].has_dest
)
515 dest_init(&instr
->dest
);
517 for (unsigned i
= 0; i
< num_srcs
; i
++)
518 src_init(&instr
->src
[i
]);
524 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
526 const unsigned num_params
= callee
->num_params
;
527 nir_call_instr
*instr
=
528 rzalloc_size(shader
, sizeof(*instr
) +
529 num_params
* sizeof(instr
->params
[0]));
531 instr_init(&instr
->instr
, nir_instr_type_call
);
532 instr
->callee
= callee
;
533 instr
->num_params
= num_params
;
534 for (unsigned i
= 0; i
< num_params
; i
++)
535 src_init(&instr
->params
[i
]);
540 static int8_t default_tg4_offsets
[4][2] =
549 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
551 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
552 instr_init(&instr
->instr
, nir_instr_type_tex
);
554 dest_init(&instr
->dest
);
556 instr
->num_srcs
= num_srcs
;
557 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
558 for (unsigned i
= 0; i
< num_srcs
; i
++)
559 src_init(&instr
->src
[i
].src
);
561 instr
->texture_index
= 0;
562 instr
->sampler_index
= 0;
563 memcpy(instr
->tg4_offsets
, default_tg4_offsets
, sizeof(instr
->tg4_offsets
));
569 nir_tex_instr_add_src(nir_tex_instr
*tex
,
570 nir_tex_src_type src_type
,
573 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
576 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
577 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
578 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
582 ralloc_free(tex
->src
);
585 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
586 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
591 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
593 assert(src_idx
< tex
->num_srcs
);
595 /* First rewrite the source to NIR_SRC_INIT */
596 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
598 /* Now, move all of the other sources down */
599 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
600 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
601 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
607 nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr
*tex
)
609 if (tex
->op
!= nir_texop_tg4
)
611 return memcmp(tex
->tg4_offsets
, default_tg4_offsets
,
612 sizeof(tex
->tg4_offsets
)) != 0;
616 nir_phi_instr_create(nir_shader
*shader
)
618 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
619 instr_init(&instr
->instr
, nir_instr_type_phi
);
621 dest_init(&instr
->dest
);
622 exec_list_make_empty(&instr
->srcs
);
626 nir_parallel_copy_instr
*
627 nir_parallel_copy_instr_create(nir_shader
*shader
)
629 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
630 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
632 exec_list_make_empty(&instr
->entries
);
637 nir_ssa_undef_instr
*
638 nir_ssa_undef_instr_create(nir_shader
*shader
,
639 unsigned num_components
,
642 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
643 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
645 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
650 static nir_const_value
651 const_value_float(double d
, unsigned bit_size
)
654 memset(&v
, 0, sizeof(v
));
656 case 16: v
.u16
= _mesa_float_to_half(d
); break;
657 case 32: v
.f32
= d
; break;
658 case 64: v
.f64
= d
; break;
660 unreachable("Invalid bit size");
665 static nir_const_value
666 const_value_int(int64_t i
, unsigned bit_size
)
669 memset(&v
, 0, sizeof(v
));
671 case 1: v
.b
= i
& 1; break;
672 case 8: v
.i8
= i
; break;
673 case 16: v
.i16
= i
; break;
674 case 32: v
.i32
= i
; break;
675 case 64: v
.i64
= i
; break;
677 unreachable("Invalid bit size");
683 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
685 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
686 const int64_t min_int
= -max_int
- 1;
689 return const_value_int(0, bit_size
);
691 return const_value_float(0, bit_size
);
693 return const_value_int(1, bit_size
);
695 return const_value_float(1, bit_size
);
697 return const_value_int(max_int
, bit_size
);
699 return const_value_int(~0ull, bit_size
);
701 return const_value_float(INFINITY
, bit_size
);
703 return const_value_int(min_int
, bit_size
);
705 return const_value_int(0, bit_size
);
707 return const_value_float(-INFINITY
, bit_size
);
709 return const_value_int(~0ull, bit_size
);
711 return const_value_int(0, bit_size
);
713 return const_value_int(0, bit_size
);
715 unreachable("Invalid reduction operation");
720 nir_cf_node_get_function(nir_cf_node
*node
)
722 while (node
->type
!= nir_cf_node_function
) {
726 return nir_cf_node_as_function(node
);
729 /* Reduces a cursor by trying to convert everything to after and trying to
730 * go up to block granularity when possible.
733 reduce_cursor(nir_cursor cursor
)
735 switch (cursor
.option
) {
736 case nir_cursor_before_block
:
737 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
738 /* Empty block. After is as good as before. */
739 cursor
.option
= nir_cursor_after_block
;
743 case nir_cursor_after_block
:
746 case nir_cursor_before_instr
: {
747 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
749 /* Before this instruction is after the previous */
750 cursor
.instr
= prev_instr
;
751 cursor
.option
= nir_cursor_after_instr
;
753 /* No previous instruction. Switch to before block */
754 cursor
.block
= cursor
.instr
->block
;
755 cursor
.option
= nir_cursor_before_block
;
757 return reduce_cursor(cursor
);
760 case nir_cursor_after_instr
:
761 if (nir_instr_next(cursor
.instr
) == NULL
) {
762 /* This is the last instruction, switch to after block */
763 cursor
.option
= nir_cursor_after_block
;
764 cursor
.block
= cursor
.instr
->block
;
769 unreachable("Inavlid cursor option");
774 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
776 /* Reduced cursors should be unique */
777 a
= reduce_cursor(a
);
778 b
= reduce_cursor(b
);
780 return a
.block
== b
.block
&& a
.option
== b
.option
;
784 add_use_cb(nir_src
*src
, void *state
)
786 nir_instr
*instr
= state
;
788 src
->parent_instr
= instr
;
789 list_addtail(&src
->use_link
,
790 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
796 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
798 nir_instr
*instr
= state
;
800 if (instr
->block
&& def
->index
== UINT_MAX
) {
801 nir_function_impl
*impl
=
802 nir_cf_node_get_function(&instr
->block
->cf_node
);
804 def
->index
= impl
->ssa_alloc
++;
806 impl
->valid_metadata
&= ~nir_metadata_live_ssa_defs
;
813 add_reg_def_cb(nir_dest
*dest
, void *state
)
815 nir_instr
*instr
= state
;
818 dest
->reg
.parent_instr
= instr
;
819 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
826 add_defs_uses(nir_instr
*instr
)
828 nir_foreach_src(instr
, add_use_cb
, instr
);
829 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
830 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
834 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
836 switch (cursor
.option
) {
837 case nir_cursor_before_block
:
838 /* Only allow inserting jumps into empty blocks. */
839 if (instr
->type
== nir_instr_type_jump
)
840 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
842 instr
->block
= cursor
.block
;
843 add_defs_uses(instr
);
844 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
846 case nir_cursor_after_block
: {
847 /* Inserting instructions after a jump is illegal. */
848 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
849 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
852 instr
->block
= cursor
.block
;
853 add_defs_uses(instr
);
854 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
857 case nir_cursor_before_instr
:
858 assert(instr
->type
!= nir_instr_type_jump
);
859 instr
->block
= cursor
.instr
->block
;
860 add_defs_uses(instr
);
861 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
863 case nir_cursor_after_instr
:
864 /* Inserting instructions after a jump is illegal. */
865 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
867 /* Only allow inserting jumps at the end of the block. */
868 if (instr
->type
== nir_instr_type_jump
)
869 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
871 instr
->block
= cursor
.instr
->block
;
872 add_defs_uses(instr
);
873 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
877 if (instr
->type
== nir_instr_type_jump
)
878 nir_handle_add_jump(instr
->block
);
882 src_is_valid(const nir_src
*src
)
884 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
888 remove_use_cb(nir_src
*src
, void *state
)
892 if (src_is_valid(src
))
893 list_del(&src
->use_link
);
899 remove_def_cb(nir_dest
*dest
, void *state
)
904 list_del(&dest
->reg
.def_link
);
910 remove_defs_uses(nir_instr
*instr
)
912 nir_foreach_dest(instr
, remove_def_cb
, instr
);
913 nir_foreach_src(instr
, remove_use_cb
, instr
);
916 void nir_instr_remove_v(nir_instr
*instr
)
918 remove_defs_uses(instr
);
919 exec_node_remove(&instr
->node
);
921 if (instr
->type
== nir_instr_type_jump
) {
922 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
923 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
930 nir_index_local_regs(nir_function_impl
*impl
)
933 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
934 reg
->index
= index
++;
936 impl
->reg_alloc
= index
;
940 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
942 return cb(&instr
->dest
.dest
, state
);
946 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
948 return cb(&instr
->dest
, state
);
952 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
955 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
956 return cb(&instr
->dest
, state
);
962 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
965 return cb(&instr
->dest
, state
);
969 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
971 return cb(&instr
->dest
, state
);
975 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
976 nir_foreach_dest_cb cb
, void *state
)
978 nir_foreach_parallel_copy_entry(entry
, instr
) {
979 if (!cb(&entry
->dest
, state
))
987 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
989 switch (instr
->type
) {
990 case nir_instr_type_alu
:
991 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
992 case nir_instr_type_deref
:
993 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
994 case nir_instr_type_intrinsic
:
995 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
996 case nir_instr_type_tex
:
997 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
998 case nir_instr_type_phi
:
999 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1000 case nir_instr_type_parallel_copy
:
1001 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1004 case nir_instr_type_load_const
:
1005 case nir_instr_type_ssa_undef
:
1006 case nir_instr_type_call
:
1007 case nir_instr_type_jump
:
1011 unreachable("Invalid instruction type");
1018 struct foreach_ssa_def_state
{
1019 nir_foreach_ssa_def_cb cb
;
1024 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1026 struct foreach_ssa_def_state
*state
= void_state
;
1029 return state
->cb(&dest
->ssa
, state
->client_state
);
1035 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1037 switch (instr
->type
) {
1038 case nir_instr_type_alu
:
1039 case nir_instr_type_deref
:
1040 case nir_instr_type_tex
:
1041 case nir_instr_type_intrinsic
:
1042 case nir_instr_type_phi
:
1043 case nir_instr_type_parallel_copy
: {
1044 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1045 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1048 case nir_instr_type_load_const
:
1049 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1050 case nir_instr_type_ssa_undef
:
1051 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1052 case nir_instr_type_call
:
1053 case nir_instr_type_jump
:
1056 unreachable("Invalid instruction type");
1061 nir_instr_ssa_def(nir_instr
*instr
)
1063 switch (instr
->type
) {
1064 case nir_instr_type_alu
:
1065 assert(nir_instr_as_alu(instr
)->dest
.dest
.is_ssa
);
1066 return &nir_instr_as_alu(instr
)->dest
.dest
.ssa
;
1068 case nir_instr_type_deref
:
1069 assert(nir_instr_as_deref(instr
)->dest
.is_ssa
);
1070 return &nir_instr_as_deref(instr
)->dest
.ssa
;
1072 case nir_instr_type_tex
:
1073 assert(nir_instr_as_tex(instr
)->dest
.is_ssa
);
1074 return &nir_instr_as_tex(instr
)->dest
.ssa
;
1076 case nir_instr_type_intrinsic
: {
1077 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
1078 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
) {
1079 assert(intrin
->dest
.is_ssa
);
1080 return &intrin
->dest
.ssa
;
1086 case nir_instr_type_phi
:
1087 assert(nir_instr_as_phi(instr
)->dest
.is_ssa
);
1088 return &nir_instr_as_phi(instr
)->dest
.ssa
;
1090 case nir_instr_type_parallel_copy
:
1091 unreachable("Parallel copies are unsupported by this function");
1093 case nir_instr_type_load_const
:
1094 return &nir_instr_as_load_const(instr
)->def
;
1096 case nir_instr_type_ssa_undef
:
1097 return &nir_instr_as_ssa_undef(instr
)->def
;
1099 case nir_instr_type_call
:
1100 case nir_instr_type_jump
:
1104 unreachable("Invalid instruction type");
1108 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1110 if (!cb(src
, state
))
1112 if (!src
->is_ssa
&& src
->reg
.indirect
)
1113 return cb(src
->reg
.indirect
, state
);
1118 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1120 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1121 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1128 visit_deref_instr_src(nir_deref_instr
*instr
,
1129 nir_foreach_src_cb cb
, void *state
)
1131 if (instr
->deref_type
!= nir_deref_type_var
) {
1132 if (!visit_src(&instr
->parent
, cb
, state
))
1136 if (instr
->deref_type
== nir_deref_type_array
||
1137 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1138 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1146 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1148 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1149 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1157 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1160 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1161 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1162 if (!visit_src(&instr
->src
[i
], cb
, state
))
1170 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1172 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1173 if (!visit_src(&instr
->params
[i
], cb
, state
))
1181 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1183 nir_foreach_phi_src(src
, instr
) {
1184 if (!visit_src(&src
->src
, cb
, state
))
1192 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1193 nir_foreach_src_cb cb
, void *state
)
1195 nir_foreach_parallel_copy_entry(entry
, instr
) {
1196 if (!visit_src(&entry
->src
, cb
, state
))
1204 visit_jump_src(nir_jump_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1206 if (instr
->type
!= nir_jump_goto_if
)
1209 return visit_src(&instr
->condition
, cb
, state
);
1214 nir_foreach_src_cb cb
;
1215 } visit_dest_indirect_state
;
1218 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1220 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1222 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1223 return state
->cb(dest
->reg
.indirect
, state
->state
);
1229 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1231 switch (instr
->type
) {
1232 case nir_instr_type_alu
:
1233 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1236 case nir_instr_type_deref
:
1237 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1240 case nir_instr_type_intrinsic
:
1241 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1244 case nir_instr_type_tex
:
1245 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1248 case nir_instr_type_call
:
1249 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1252 case nir_instr_type_load_const
:
1253 /* Constant load instructions have no regular sources */
1255 case nir_instr_type_phi
:
1256 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1259 case nir_instr_type_parallel_copy
:
1260 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1264 case nir_instr_type_jump
:
1265 return visit_jump_src(nir_instr_as_jump(instr
), cb
, state
);
1266 case nir_instr_type_ssa_undef
:
1270 unreachable("Invalid instruction type");
1274 visit_dest_indirect_state dest_state
;
1275 dest_state
.state
= state
;
1277 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1281 nir_foreach_phi_src_leaving_block(nir_block
*block
,
1282 nir_foreach_src_cb cb
,
1285 for (unsigned i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
1286 if (block
->successors
[i
] == NULL
)
1289 nir_foreach_instr(instr
, block
->successors
[i
]) {
1290 if (instr
->type
!= nir_instr_type_phi
)
1293 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
1294 nir_foreach_phi_src(phi_src
, phi
) {
1295 if (phi_src
->pred
== block
) {
1296 if (!cb(&phi_src
->src
, state
))
1307 nir_const_value_for_float(double f
, unsigned bit_size
)
1310 memset(&v
, 0, sizeof(v
));
1314 v
.u16
= _mesa_float_to_half(f
);
1323 unreachable("Invalid bit size");
1330 nir_const_value_as_float(nir_const_value value
, unsigned bit_size
)
1333 case 16: return _mesa_half_to_float(value
.u16
);
1334 case 32: return value
.f32
;
1335 case 64: return value
.f64
;
1337 unreachable("Invalid bit size");
1342 nir_src_as_const_value(nir_src src
)
1347 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1350 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1356 * Returns true if the source is known to be dynamically uniform. Otherwise it
1357 * returns false which means it may or may not be dynamically uniform but it
1358 * can't be determined.
1361 nir_src_is_dynamically_uniform(nir_src src
)
1366 /* Constants are trivially dynamically uniform */
1367 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1370 /* As are uniform variables */
1371 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1372 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1373 if (intr
->intrinsic
== nir_intrinsic_load_uniform
&&
1374 nir_src_is_dynamically_uniform(intr
->src
[0]))
1378 /* Operating together dynamically uniform expressions produces a
1379 * dynamically uniform result
1381 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
1382 nir_alu_instr
*alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
1383 for (int i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
1384 if (!nir_src_is_dynamically_uniform(alu
->src
[i
].src
))
1391 /* XXX: this could have many more tests, such as when a sampler function is
1392 * called with dynamically uniform arguments.
1398 src_remove_all_uses(nir_src
*src
)
1400 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1401 if (!src_is_valid(src
))
1404 list_del(&src
->use_link
);
1409 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1411 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1412 if (!src_is_valid(src
))
1416 src
->parent_instr
= parent_instr
;
1418 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1420 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1423 src
->parent_if
= parent_if
;
1425 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1427 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1433 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1435 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1437 src_remove_all_uses(src
);
1439 src_add_all_uses(src
, instr
, NULL
);
1443 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1445 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1447 src_remove_all_uses(dest
);
1448 src_remove_all_uses(src
);
1450 *src
= NIR_SRC_INIT
;
1451 src_add_all_uses(dest
, dest_instr
, NULL
);
1455 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1457 nir_src
*src
= &if_stmt
->condition
;
1458 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1460 src_remove_all_uses(src
);
1462 src_add_all_uses(src
, NULL
, if_stmt
);
1466 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1469 /* We can only overwrite an SSA destination if it has no uses. */
1470 assert(list_is_empty(&dest
->ssa
.uses
) && list_is_empty(&dest
->ssa
.if_uses
));
1472 list_del(&dest
->reg
.def_link
);
1473 if (dest
->reg
.indirect
)
1474 src_remove_all_uses(dest
->reg
.indirect
);
1477 /* We can't re-write with an SSA def */
1478 assert(!new_dest
.is_ssa
);
1480 nir_dest_copy(dest
, &new_dest
, instr
);
1482 dest
->reg
.parent_instr
= instr
;
1483 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1485 if (dest
->reg
.indirect
)
1486 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1489 /* note: does *not* take ownership of 'name' */
1491 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1492 unsigned num_components
,
1493 unsigned bit_size
, const char *name
)
1495 def
->name
= ralloc_strdup(instr
, name
);
1496 def
->live_index
= UINT_MAX
; /* Something clearly OOB */
1497 def
->parent_instr
= instr
;
1498 list_inithead(&def
->uses
);
1499 list_inithead(&def
->if_uses
);
1500 def
->num_components
= num_components
;
1501 def
->bit_size
= bit_size
;
1502 def
->divergent
= true; /* This is the safer default */
1505 nir_function_impl
*impl
=
1506 nir_cf_node_get_function(&instr
->block
->cf_node
);
1508 def
->index
= impl
->ssa_alloc
++;
1510 impl
->valid_metadata
&= ~nir_metadata_live_ssa_defs
;
1512 def
->index
= UINT_MAX
;
1516 /* note: does *not* take ownership of 'name' */
1518 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1519 unsigned num_components
, unsigned bit_size
,
1522 dest
->is_ssa
= true;
1523 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1527 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1529 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1531 nir_foreach_use_safe(use_src
, def
)
1532 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1534 nir_foreach_if_use_safe(use_src
, def
)
1535 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1539 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1541 assert(start
->block
== end
->block
);
1543 if (between
->block
!= start
->block
)
1546 /* Search backwards looking for "between" */
1547 while (start
!= end
) {
1551 end
= nir_instr_prev(end
);
1558 /* Replaces all uses of the given SSA def with the given source but only if
1559 * the use comes after the after_me instruction. This can be useful if you
1560 * are emitting code to fix up the result of some instruction: you can freely
1561 * use the result in that code and then call rewrite_uses_after and pass the
1562 * last fixup instruction as after_me and it will replace all of the uses you
1563 * want without touching the fixup code.
1565 * This function assumes that after_me is in the same block as
1566 * def->parent_instr and that after_me comes after def->parent_instr.
1569 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1570 nir_instr
*after_me
)
1572 if (new_src
.is_ssa
&& def
== new_src
.ssa
)
1575 nir_foreach_use_safe(use_src
, def
) {
1576 assert(use_src
->parent_instr
!= def
->parent_instr
);
1577 /* Since def already dominates all of its uses, the only way a use can
1578 * not be dominated by after_me is if it is between def and after_me in
1579 * the instruction list.
1581 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1582 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1585 nir_foreach_if_use_safe(use_src
, def
)
1586 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1589 nir_component_mask_t
1590 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1592 nir_component_mask_t read_mask
= 0;
1593 nir_foreach_use(use
, def
) {
1594 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1595 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1596 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1597 int src_idx
= alu_src
- &alu
->src
[0];
1598 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1599 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1601 return (1 << def
->num_components
) - 1;
1605 if (!list_is_empty(&def
->if_uses
))
1612 nir_block_unstructured_next(nir_block
*block
)
1614 if (block
== NULL
) {
1615 /* nir_foreach_block_unstructured_safe() will call this function on a
1616 * NULL block after the last iteration, but it won't use the result so
1617 * just return NULL here.
1622 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1623 if (cf_next
== NULL
&& block
->cf_node
.parent
->type
== nir_cf_node_function
)
1626 if (cf_next
&& cf_next
->type
== nir_cf_node_block
)
1627 return nir_cf_node_as_block(cf_next
);
1629 return nir_block_cf_tree_next(block
);
1633 nir_unstructured_start_block(nir_function_impl
*impl
)
1635 return nir_start_block(impl
);
1639 nir_block_cf_tree_next(nir_block
*block
)
1641 if (block
== NULL
) {
1642 /* nir_foreach_block_safe() will call this function on a NULL block
1643 * after the last iteration, but it won't use the result so just return
1649 assert(nir_cf_node_get_function(&block
->cf_node
)->structured
);
1651 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1653 return nir_cf_node_cf_tree_first(cf_next
);
1655 nir_cf_node
*parent
= block
->cf_node
.parent
;
1657 switch (parent
->type
) {
1658 case nir_cf_node_if
: {
1659 /* Are we at the end of the if? Go to the beginning of the else */
1660 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1661 if (block
== nir_if_last_then_block(if_stmt
))
1662 return nir_if_first_else_block(if_stmt
);
1664 assert(block
== nir_if_last_else_block(if_stmt
));
1668 case nir_cf_node_loop
:
1669 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1671 case nir_cf_node_function
:
1675 unreachable("unknown cf node type");
1680 nir_block_cf_tree_prev(nir_block
*block
)
1682 if (block
== NULL
) {
1683 /* do this for consistency with nir_block_cf_tree_next() */
1687 assert(nir_cf_node_get_function(&block
->cf_node
)->structured
);
1689 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1691 return nir_cf_node_cf_tree_last(cf_prev
);
1693 nir_cf_node
*parent
= block
->cf_node
.parent
;
1695 switch (parent
->type
) {
1696 case nir_cf_node_if
: {
1697 /* Are we at the beginning of the else? Go to the end of the if */
1698 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1699 if (block
== nir_if_first_else_block(if_stmt
))
1700 return nir_if_last_then_block(if_stmt
);
1702 assert(block
== nir_if_first_then_block(if_stmt
));
1706 case nir_cf_node_loop
:
1707 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1709 case nir_cf_node_function
:
1713 unreachable("unknown cf node type");
1717 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1719 switch (node
->type
) {
1720 case nir_cf_node_function
: {
1721 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1722 return nir_start_block(impl
);
1725 case nir_cf_node_if
: {
1726 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1727 return nir_if_first_then_block(if_stmt
);
1730 case nir_cf_node_loop
: {
1731 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1732 return nir_loop_first_block(loop
);
1735 case nir_cf_node_block
: {
1736 return nir_cf_node_as_block(node
);
1740 unreachable("unknown node type");
1744 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1746 switch (node
->type
) {
1747 case nir_cf_node_function
: {
1748 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1749 return nir_impl_last_block(impl
);
1752 case nir_cf_node_if
: {
1753 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1754 return nir_if_last_else_block(if_stmt
);
1757 case nir_cf_node_loop
: {
1758 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1759 return nir_loop_last_block(loop
);
1762 case nir_cf_node_block
: {
1763 return nir_cf_node_as_block(node
);
1767 unreachable("unknown node type");
1771 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1773 if (node
->type
== nir_cf_node_block
)
1774 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1775 else if (node
->type
== nir_cf_node_function
)
1778 return nir_cf_node_as_block(nir_cf_node_next(node
));
1782 nir_block_get_following_if(nir_block
*block
)
1784 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1787 if (nir_cf_node_is_last(&block
->cf_node
))
1790 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1792 if (next_node
->type
!= nir_cf_node_if
)
1795 return nir_cf_node_as_if(next_node
);
1799 nir_block_get_following_loop(nir_block
*block
)
1801 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1804 if (nir_cf_node_is_last(&block
->cf_node
))
1807 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1809 if (next_node
->type
!= nir_cf_node_loop
)
1812 return nir_cf_node_as_loop(next_node
);
1816 nir_index_blocks(nir_function_impl
*impl
)
1820 if (impl
->valid_metadata
& nir_metadata_block_index
)
1823 nir_foreach_block_unstructured(block
, impl
) {
1824 block
->index
= index
++;
1827 /* The end_block isn't really part of the program, which is why its index
1830 impl
->num_blocks
= impl
->end_block
->index
= index
;
1834 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1836 unsigned *index
= (unsigned *) state
;
1837 def
->index
= (*index
)++;
1843 * The indices are applied top-to-bottom which has the very nice property
1844 * that, if A dominates B, then A->index <= B->index.
1847 nir_index_ssa_defs(nir_function_impl
*impl
)
1851 impl
->valid_metadata
&= ~nir_metadata_live_ssa_defs
;
1853 nir_foreach_block_unstructured(block
, impl
) {
1854 nir_foreach_instr(instr
, block
)
1855 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1858 impl
->ssa_alloc
= index
;
1862 * The indices are applied top-to-bottom which has the very nice property
1863 * that, if A dominates B, then A->index <= B->index.
1866 nir_index_instrs(nir_function_impl
*impl
)
1870 nir_foreach_block(block
, impl
) {
1871 nir_foreach_instr(instr
, block
)
1872 instr
->index
= index
++;
1879 nir_shader_index_vars(nir_shader
*shader
, nir_variable_mode modes
)
1882 nir_foreach_variable_with_modes(var
, shader
, modes
)
1883 var
->index
= count
++;
1888 nir_function_impl_index_vars(nir_function_impl
*impl
)
1891 nir_foreach_function_temp_variable(var
, impl
)
1892 var
->index
= count
++;
1897 cursor_next_instr(nir_cursor cursor
)
1899 switch (cursor
.option
) {
1900 case nir_cursor_before_block
:
1901 for (nir_block
*block
= cursor
.block
; block
;
1902 block
= nir_block_cf_tree_next(block
)) {
1903 nir_instr
*instr
= nir_block_first_instr(block
);
1909 case nir_cursor_after_block
:
1910 cursor
.block
= nir_block_cf_tree_next(cursor
.block
);
1911 if (cursor
.block
== NULL
)
1914 cursor
.option
= nir_cursor_before_block
;
1915 return cursor_next_instr(cursor
);
1917 case nir_cursor_before_instr
:
1918 return cursor
.instr
;
1920 case nir_cursor_after_instr
:
1921 if (nir_instr_next(cursor
.instr
))
1922 return nir_instr_next(cursor
.instr
);
1924 cursor
.option
= nir_cursor_after_block
;
1925 cursor
.block
= cursor
.instr
->block
;
1926 return cursor_next_instr(cursor
);
1929 unreachable("Inavlid cursor option");
1932 ASSERTED
static bool
1933 dest_is_ssa(nir_dest
*dest
, void *_state
)
1936 return dest
->is_ssa
;
1940 nir_function_impl_lower_instructions(nir_function_impl
*impl
,
1941 nir_instr_filter_cb filter
,
1942 nir_lower_instr_cb lower
,
1946 nir_builder_init(&b
, impl
);
1948 nir_metadata preserved
= nir_metadata_block_index
|
1949 nir_metadata_dominance
;
1951 bool progress
= false;
1952 nir_cursor iter
= nir_before_cf_list(&impl
->body
);
1954 while ((instr
= cursor_next_instr(iter
)) != NULL
) {
1955 if (filter
&& !filter(instr
, cb_data
)) {
1956 iter
= nir_after_instr(instr
);
1960 assert(nir_foreach_dest(instr
, dest_is_ssa
, NULL
));
1961 nir_ssa_def
*old_def
= nir_instr_ssa_def(instr
);
1962 if (old_def
== NULL
) {
1963 iter
= nir_after_instr(instr
);
1967 /* We're about to ask the callback to generate a replacement for instr.
1968 * Save off the uses from instr's SSA def so we know what uses to
1969 * rewrite later. If we use nir_ssa_def_rewrite_uses, it fails in the
1970 * case where the generated replacement code uses the result of instr
1971 * itself. If we use nir_ssa_def_rewrite_uses_after (which is the
1972 * normal solution to this problem), it doesn't work well if control-
1973 * flow is inserted as part of the replacement, doesn't handle cases
1974 * where the replacement is something consumed by instr, and suffers
1975 * from performance issues. This is the only way to 100% guarantee
1976 * that we rewrite the correct set efficiently.
1978 struct list_head old_uses
, old_if_uses
;
1979 list_replace(&old_def
->uses
, &old_uses
);
1980 list_inithead(&old_def
->uses
);
1981 list_replace(&old_def
->if_uses
, &old_if_uses
);
1982 list_inithead(&old_def
->if_uses
);
1984 b
.cursor
= nir_after_instr(instr
);
1985 nir_ssa_def
*new_def
= lower(&b
, instr
, cb_data
);
1986 if (new_def
&& new_def
!= NIR_LOWER_INSTR_PROGRESS
) {
1987 assert(old_def
!= NULL
);
1988 if (new_def
->parent_instr
->block
!= instr
->block
)
1989 preserved
= nir_metadata_none
;
1991 nir_src new_src
= nir_src_for_ssa(new_def
);
1992 list_for_each_entry_safe(nir_src
, use_src
, &old_uses
, use_link
)
1993 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1995 list_for_each_entry_safe(nir_src
, use_src
, &old_if_uses
, use_link
)
1996 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1998 if (list_is_empty(&old_def
->uses
) && list_is_empty(&old_def
->if_uses
)) {
1999 iter
= nir_instr_remove(instr
);
2001 iter
= nir_after_instr(instr
);
2005 /* We didn't end up lowering after all. Put the uses back */
2007 list_replace(&old_uses
, &old_def
->uses
);
2008 list_replace(&old_if_uses
, &old_def
->if_uses
);
2010 iter
= nir_after_instr(instr
);
2012 if (new_def
== NIR_LOWER_INSTR_PROGRESS
)
2018 nir_metadata_preserve(impl
, preserved
);
2020 nir_metadata_preserve(impl
, nir_metadata_all
);
2027 nir_shader_lower_instructions(nir_shader
*shader
,
2028 nir_instr_filter_cb filter
,
2029 nir_lower_instr_cb lower
,
2032 bool progress
= false;
2034 nir_foreach_function(function
, shader
) {
2035 if (function
->impl
&&
2036 nir_function_impl_lower_instructions(function
->impl
,
2037 filter
, lower
, cb_data
))
2045 nir_intrinsic_from_system_value(gl_system_value val
)
2048 case SYSTEM_VALUE_VERTEX_ID
:
2049 return nir_intrinsic_load_vertex_id
;
2050 case SYSTEM_VALUE_INSTANCE_ID
:
2051 return nir_intrinsic_load_instance_id
;
2052 case SYSTEM_VALUE_DRAW_ID
:
2053 return nir_intrinsic_load_draw_id
;
2054 case SYSTEM_VALUE_BASE_INSTANCE
:
2055 return nir_intrinsic_load_base_instance
;
2056 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
2057 return nir_intrinsic_load_vertex_id_zero_base
;
2058 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
2059 return nir_intrinsic_load_is_indexed_draw
;
2060 case SYSTEM_VALUE_FIRST_VERTEX
:
2061 return nir_intrinsic_load_first_vertex
;
2062 case SYSTEM_VALUE_BASE_VERTEX
:
2063 return nir_intrinsic_load_base_vertex
;
2064 case SYSTEM_VALUE_INVOCATION_ID
:
2065 return nir_intrinsic_load_invocation_id
;
2066 case SYSTEM_VALUE_FRAG_COORD
:
2067 return nir_intrinsic_load_frag_coord
;
2068 case SYSTEM_VALUE_POINT_COORD
:
2069 return nir_intrinsic_load_point_coord
;
2070 case SYSTEM_VALUE_LINE_COORD
:
2071 return nir_intrinsic_load_line_coord
;
2072 case SYSTEM_VALUE_FRONT_FACE
:
2073 return nir_intrinsic_load_front_face
;
2074 case SYSTEM_VALUE_SAMPLE_ID
:
2075 return nir_intrinsic_load_sample_id
;
2076 case SYSTEM_VALUE_SAMPLE_POS
:
2077 return nir_intrinsic_load_sample_pos
;
2078 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
2079 return nir_intrinsic_load_sample_mask_in
;
2080 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
2081 return nir_intrinsic_load_local_invocation_id
;
2082 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
2083 return nir_intrinsic_load_local_invocation_index
;
2084 case SYSTEM_VALUE_WORK_GROUP_ID
:
2085 return nir_intrinsic_load_work_group_id
;
2086 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
2087 return nir_intrinsic_load_num_work_groups
;
2088 case SYSTEM_VALUE_PRIMITIVE_ID
:
2089 return nir_intrinsic_load_primitive_id
;
2090 case SYSTEM_VALUE_TESS_COORD
:
2091 return nir_intrinsic_load_tess_coord
;
2092 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
2093 return nir_intrinsic_load_tess_level_outer
;
2094 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
2095 return nir_intrinsic_load_tess_level_inner
;
2096 case SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
:
2097 return nir_intrinsic_load_tess_level_outer_default
;
2098 case SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
:
2099 return nir_intrinsic_load_tess_level_inner_default
;
2100 case SYSTEM_VALUE_VERTICES_IN
:
2101 return nir_intrinsic_load_patch_vertices_in
;
2102 case SYSTEM_VALUE_HELPER_INVOCATION
:
2103 return nir_intrinsic_load_helper_invocation
;
2104 case SYSTEM_VALUE_COLOR0
:
2105 return nir_intrinsic_load_color0
;
2106 case SYSTEM_VALUE_COLOR1
:
2107 return nir_intrinsic_load_color1
;
2108 case SYSTEM_VALUE_VIEW_INDEX
:
2109 return nir_intrinsic_load_view_index
;
2110 case SYSTEM_VALUE_SUBGROUP_SIZE
:
2111 return nir_intrinsic_load_subgroup_size
;
2112 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
2113 return nir_intrinsic_load_subgroup_invocation
;
2114 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
2115 return nir_intrinsic_load_subgroup_eq_mask
;
2116 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
2117 return nir_intrinsic_load_subgroup_ge_mask
;
2118 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
2119 return nir_intrinsic_load_subgroup_gt_mask
;
2120 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
2121 return nir_intrinsic_load_subgroup_le_mask
;
2122 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
2123 return nir_intrinsic_load_subgroup_lt_mask
;
2124 case SYSTEM_VALUE_NUM_SUBGROUPS
:
2125 return nir_intrinsic_load_num_subgroups
;
2126 case SYSTEM_VALUE_SUBGROUP_ID
:
2127 return nir_intrinsic_load_subgroup_id
;
2128 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
2129 return nir_intrinsic_load_local_group_size
;
2130 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
2131 return nir_intrinsic_load_global_invocation_id
;
2132 case SYSTEM_VALUE_BASE_GLOBAL_INVOCATION_ID
:
2133 return nir_intrinsic_load_base_global_invocation_id
;
2134 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
:
2135 return nir_intrinsic_load_global_invocation_index
;
2136 case SYSTEM_VALUE_WORK_DIM
:
2137 return nir_intrinsic_load_work_dim
;
2138 case SYSTEM_VALUE_USER_DATA_AMD
:
2139 return nir_intrinsic_load_user_data_amd
;
2141 unreachable("system value does not directly correspond to intrinsic");
2146 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
2149 case nir_intrinsic_load_vertex_id
:
2150 return SYSTEM_VALUE_VERTEX_ID
;
2151 case nir_intrinsic_load_instance_id
:
2152 return SYSTEM_VALUE_INSTANCE_ID
;
2153 case nir_intrinsic_load_draw_id
:
2154 return SYSTEM_VALUE_DRAW_ID
;
2155 case nir_intrinsic_load_base_instance
:
2156 return SYSTEM_VALUE_BASE_INSTANCE
;
2157 case nir_intrinsic_load_vertex_id_zero_base
:
2158 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2159 case nir_intrinsic_load_first_vertex
:
2160 return SYSTEM_VALUE_FIRST_VERTEX
;
2161 case nir_intrinsic_load_is_indexed_draw
:
2162 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
2163 case nir_intrinsic_load_base_vertex
:
2164 return SYSTEM_VALUE_BASE_VERTEX
;
2165 case nir_intrinsic_load_invocation_id
:
2166 return SYSTEM_VALUE_INVOCATION_ID
;
2167 case nir_intrinsic_load_frag_coord
:
2168 return SYSTEM_VALUE_FRAG_COORD
;
2169 case nir_intrinsic_load_point_coord
:
2170 return SYSTEM_VALUE_POINT_COORD
;
2171 case nir_intrinsic_load_line_coord
:
2172 return SYSTEM_VALUE_LINE_COORD
;
2173 case nir_intrinsic_load_front_face
:
2174 return SYSTEM_VALUE_FRONT_FACE
;
2175 case nir_intrinsic_load_sample_id
:
2176 return SYSTEM_VALUE_SAMPLE_ID
;
2177 case nir_intrinsic_load_sample_pos
:
2178 return SYSTEM_VALUE_SAMPLE_POS
;
2179 case nir_intrinsic_load_sample_mask_in
:
2180 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
2181 case nir_intrinsic_load_local_invocation_id
:
2182 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
2183 case nir_intrinsic_load_local_invocation_index
:
2184 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
2185 case nir_intrinsic_load_num_work_groups
:
2186 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2187 case nir_intrinsic_load_work_group_id
:
2188 return SYSTEM_VALUE_WORK_GROUP_ID
;
2189 case nir_intrinsic_load_primitive_id
:
2190 return SYSTEM_VALUE_PRIMITIVE_ID
;
2191 case nir_intrinsic_load_tess_coord
:
2192 return SYSTEM_VALUE_TESS_COORD
;
2193 case nir_intrinsic_load_tess_level_outer
:
2194 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2195 case nir_intrinsic_load_tess_level_inner
:
2196 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
2197 case nir_intrinsic_load_tess_level_outer_default
:
2198 return SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
;
2199 case nir_intrinsic_load_tess_level_inner_default
:
2200 return SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
;
2201 case nir_intrinsic_load_patch_vertices_in
:
2202 return SYSTEM_VALUE_VERTICES_IN
;
2203 case nir_intrinsic_load_helper_invocation
:
2204 return SYSTEM_VALUE_HELPER_INVOCATION
;
2205 case nir_intrinsic_load_color0
:
2206 return SYSTEM_VALUE_COLOR0
;
2207 case nir_intrinsic_load_color1
:
2208 return SYSTEM_VALUE_COLOR1
;
2209 case nir_intrinsic_load_view_index
:
2210 return SYSTEM_VALUE_VIEW_INDEX
;
2211 case nir_intrinsic_load_subgroup_size
:
2212 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2213 case nir_intrinsic_load_subgroup_invocation
:
2214 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2215 case nir_intrinsic_load_subgroup_eq_mask
:
2216 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2217 case nir_intrinsic_load_subgroup_ge_mask
:
2218 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2219 case nir_intrinsic_load_subgroup_gt_mask
:
2220 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2221 case nir_intrinsic_load_subgroup_le_mask
:
2222 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2223 case nir_intrinsic_load_subgroup_lt_mask
:
2224 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2225 case nir_intrinsic_load_num_subgroups
:
2226 return SYSTEM_VALUE_NUM_SUBGROUPS
;
2227 case nir_intrinsic_load_subgroup_id
:
2228 return SYSTEM_VALUE_SUBGROUP_ID
;
2229 case nir_intrinsic_load_local_group_size
:
2230 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2231 case nir_intrinsic_load_global_invocation_id
:
2232 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
2233 case nir_intrinsic_load_base_global_invocation_id
:
2234 return SYSTEM_VALUE_BASE_GLOBAL_INVOCATION_ID
;
2235 case nir_intrinsic_load_global_invocation_index
:
2236 return SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
;
2237 case nir_intrinsic_load_work_dim
:
2238 return SYSTEM_VALUE_WORK_DIM
;
2239 case nir_intrinsic_load_user_data_amd
:
2240 return SYSTEM_VALUE_USER_DATA_AMD
;
2242 unreachable("intrinsic doesn't produce a system value");
2246 /* OpenGL utility method that remaps the location attributes if they are
2247 * doubles. Not needed for vulkan due the differences on the input location
2248 * count for doubles on vulkan vs OpenGL
2250 * The bitfield returned in dual_slot is one bit for each double input slot in
2251 * the original OpenGL single-slot input numbering. The mapping from old
2252 * locations to new locations is as follows:
2254 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
2257 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
2259 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
2262 nir_foreach_shader_in_variable(var
, shader
) {
2263 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
2264 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
2265 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
2269 nir_foreach_shader_in_variable(var
, shader
) {
2270 var
->data
.location
+=
2271 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
2275 /* Returns an attribute mask that has been re-compacted using the given
2279 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
2282 unsigned loc
= u_bit_scan64(&dual_slot
);
2283 /* mask of all bits up to and including loc */
2284 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
2285 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);
2291 nir_rewrite_image_intrinsic(nir_intrinsic_instr
*intrin
, nir_ssa_def
*src
,
2294 enum gl_access_qualifier access
= nir_intrinsic_access(intrin
);
2295 nir_alu_type type
= nir_type_invalid
;
2296 if (nir_intrinsic_infos
[intrin
->intrinsic
].index_map
[NIR_INTRINSIC_TYPE
])
2297 type
= nir_intrinsic_type(intrin
);
2299 switch (intrin
->intrinsic
) {
2301 case nir_intrinsic_image_deref_##op: \
2302 intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
2303 : nir_intrinsic_image_##op; \
2315 CASE(atomic_exchange
)
2316 CASE(atomic_comp_swap
)
2318 CASE(atomic_inc_wrap
)
2319 CASE(atomic_dec_wrap
)
2322 CASE(load_raw_intel
)
2323 CASE(store_raw_intel
)
2326 unreachable("Unhanded image intrinsic");
2329 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
2330 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2332 nir_intrinsic_set_image_dim(intrin
, glsl_get_sampler_dim(deref
->type
));
2333 nir_intrinsic_set_image_array(intrin
, glsl_sampler_type_is_array(deref
->type
));
2334 nir_intrinsic_set_access(intrin
, access
| var
->data
.access
);
2335 nir_intrinsic_set_format(intrin
, var
->data
.image
.format
);
2336 if (nir_intrinsic_infos
[intrin
->intrinsic
].index_map
[NIR_INTRINSIC_TYPE
])
2337 nir_intrinsic_set_type(intrin
, type
);
2339 nir_instr_rewrite_src(&intrin
->instr
, &intrin
->src
[0],
2340 nir_src_for_ssa(src
));
2344 nir_image_intrinsic_coord_components(const nir_intrinsic_instr
*instr
)
2346 enum glsl_sampler_dim dim
= nir_intrinsic_image_dim(instr
);
2347 int coords
= glsl_get_sampler_dim_coordinate_components(dim
);
2348 if (dim
== GLSL_SAMPLER_DIM_CUBE
)
2351 return coords
+ nir_intrinsic_image_array(instr
);