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_control_flow_private.h"
30 #include "util/half_float.h"
34 #include "util/u_math.h"
36 #include "main/menums.h" /* BITFIELD64_MASK */
39 nir_shader_create(void *mem_ctx
,
40 gl_shader_stage stage
,
41 const nir_shader_compiler_options
*options
,
44 nir_shader
*shader
= rzalloc(mem_ctx
, nir_shader
);
46 exec_list_make_empty(&shader
->uniforms
);
47 exec_list_make_empty(&shader
->inputs
);
48 exec_list_make_empty(&shader
->outputs
);
49 exec_list_make_empty(&shader
->shared
);
51 shader
->options
= options
;
54 assert(si
->stage
== stage
);
57 shader
->info
.stage
= stage
;
60 exec_list_make_empty(&shader
->functions
);
61 exec_list_make_empty(&shader
->registers
);
62 exec_list_make_empty(&shader
->globals
);
63 exec_list_make_empty(&shader
->system_values
);
64 shader
->reg_alloc
= 0;
66 shader
->num_inputs
= 0;
67 shader
->num_outputs
= 0;
68 shader
->num_uniforms
= 0;
69 shader
->num_shared
= 0;
75 reg_create(void *mem_ctx
, struct exec_list
*list
)
77 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
79 list_inithead(®
->uses
);
80 list_inithead(®
->defs
);
81 list_inithead(®
->if_uses
);
83 reg
->num_components
= 0;
85 reg
->num_array_elems
= 0;
86 reg
->is_packed
= false;
89 exec_list_push_tail(list
, ®
->node
);
95 nir_global_reg_create(nir_shader
*shader
)
97 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
98 reg
->index
= shader
->reg_alloc
++;
99 reg
->is_global
= true;
105 nir_local_reg_create(nir_function_impl
*impl
)
107 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
108 reg
->index
= impl
->reg_alloc
++;
109 reg
->is_global
= false;
115 nir_reg_remove(nir_register
*reg
)
117 exec_node_remove(®
->node
);
121 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
123 switch (var
->data
.mode
) {
125 assert(!"invalid mode");
128 case nir_var_function_temp
:
129 assert(!"nir_shader_add_variable cannot be used for local variables");
132 case nir_var_shader_temp
:
133 exec_list_push_tail(&shader
->globals
, &var
->node
);
136 case nir_var_shader_in
:
137 exec_list_push_tail(&shader
->inputs
, &var
->node
);
140 case nir_var_shader_out
:
141 exec_list_push_tail(&shader
->outputs
, &var
->node
);
144 case nir_var_uniform
:
145 case nir_var_mem_ubo
:
146 case nir_var_mem_ssbo
:
147 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
150 case nir_var_mem_shared
:
151 assert(gl_shader_stage_is_compute(shader
->info
.stage
));
152 exec_list_push_tail(&shader
->shared
, &var
->node
);
155 case nir_var_mem_global
:
156 assert(!"nir_shader_add_variable cannot be used for global memory");
159 case nir_var_system_value
:
160 exec_list_push_tail(&shader
->system_values
, &var
->node
);
166 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
167 const struct glsl_type
*type
, const char *name
)
169 nir_variable
*var
= rzalloc(shader
, nir_variable
);
170 var
->name
= ralloc_strdup(var
, name
);
172 var
->data
.mode
= mode
;
173 var
->data
.how_declared
= nir_var_declared_normally
;
175 if ((mode
== nir_var_shader_in
&&
176 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
177 (mode
== nir_var_shader_out
&&
178 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
179 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
181 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
182 var
->data
.read_only
= true;
184 nir_shader_add_variable(shader
, var
);
190 nir_local_variable_create(nir_function_impl
*impl
,
191 const struct glsl_type
*type
, const char *name
)
193 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
194 var
->name
= ralloc_strdup(var
, name
);
196 var
->data
.mode
= nir_var_function_temp
;
198 nir_function_impl_add_variable(impl
, var
);
204 nir_function_create(nir_shader
*shader
, const char *name
)
206 nir_function
*func
= ralloc(shader
, nir_function
);
208 exec_list_push_tail(&shader
->functions
, &func
->node
);
210 func
->name
= ralloc_strdup(func
, name
);
211 func
->shader
= shader
;
212 func
->num_params
= 0;
215 func
->is_entrypoint
= false;
220 /* NOTE: if the instruction you are copying a src to is already added
221 * to the IR, use nir_instr_rewrite_src() instead.
223 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
225 dest
->is_ssa
= src
->is_ssa
;
227 dest
->ssa
= src
->ssa
;
229 dest
->reg
.base_offset
= src
->reg
.base_offset
;
230 dest
->reg
.reg
= src
->reg
.reg
;
231 if (src
->reg
.indirect
) {
232 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
233 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
235 dest
->reg
.indirect
= NULL
;
240 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
242 /* Copying an SSA definition makes no sense whatsoever. */
243 assert(!src
->is_ssa
);
245 dest
->is_ssa
= false;
247 dest
->reg
.base_offset
= src
->reg
.base_offset
;
248 dest
->reg
.reg
= src
->reg
.reg
;
249 if (src
->reg
.indirect
) {
250 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
251 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
253 dest
->reg
.indirect
= NULL
;
258 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
259 nir_alu_instr
*instr
)
261 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
262 dest
->abs
= src
->abs
;
263 dest
->negate
= src
->negate
;
264 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
265 dest
->swizzle
[i
] = src
->swizzle
[i
];
269 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
270 nir_alu_instr
*instr
)
272 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
273 dest
->write_mask
= src
->write_mask
;
274 dest
->saturate
= src
->saturate
;
279 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
281 exec_node_init(&node
->node
);
287 nir_function_impl_create_bare(nir_shader
*shader
)
289 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
291 impl
->function
= NULL
;
293 cf_init(&impl
->cf_node
, nir_cf_node_function
);
295 exec_list_make_empty(&impl
->body
);
296 exec_list_make_empty(&impl
->registers
);
297 exec_list_make_empty(&impl
->locals
);
300 impl
->valid_metadata
= nir_metadata_none
;
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
);
486 nir_load_const_instr
*
487 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
490 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
491 instr_init(&instr
->instr
, nir_instr_type_load_const
);
493 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
498 nir_intrinsic_instr
*
499 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
501 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
502 /* TODO: don't use rzalloc */
503 nir_intrinsic_instr
*instr
=
505 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
507 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
508 instr
->intrinsic
= op
;
510 if (nir_intrinsic_infos
[op
].has_dest
)
511 dest_init(&instr
->dest
);
513 for (unsigned i
= 0; i
< num_srcs
; i
++)
514 src_init(&instr
->src
[i
]);
520 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
522 const unsigned num_params
= callee
->num_params
;
523 nir_call_instr
*instr
=
524 rzalloc_size(shader
, sizeof(*instr
) +
525 num_params
* sizeof(instr
->params
[0]));
527 instr_init(&instr
->instr
, nir_instr_type_call
);
528 instr
->callee
= callee
;
529 instr
->num_params
= num_params
;
530 for (unsigned i
= 0; i
< num_params
; i
++)
531 src_init(&instr
->params
[i
]);
536 static int8_t default_tg4_offsets
[4][2] =
545 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
547 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
548 instr_init(&instr
->instr
, nir_instr_type_tex
);
550 dest_init(&instr
->dest
);
552 instr
->num_srcs
= num_srcs
;
553 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
554 for (unsigned i
= 0; i
< num_srcs
; i
++)
555 src_init(&instr
->src
[i
].src
);
557 instr
->texture_index
= 0;
558 instr
->texture_array_size
= 0;
559 instr
->sampler_index
= 0;
560 memcpy(instr
->tg4_offsets
, default_tg4_offsets
, sizeof(instr
->tg4_offsets
));
566 nir_tex_instr_add_src(nir_tex_instr
*tex
,
567 nir_tex_src_type src_type
,
570 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
573 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
574 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
575 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
579 ralloc_free(tex
->src
);
582 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
583 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
588 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
590 assert(src_idx
< tex
->num_srcs
);
592 /* First rewrite the source to NIR_SRC_INIT */
593 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
595 /* Now, move all of the other sources down */
596 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
597 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
598 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
604 nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr
*tex
)
606 if (tex
->op
!= nir_texop_tg4
)
608 return memcmp(tex
->tg4_offsets
, default_tg4_offsets
,
609 sizeof(tex
->tg4_offsets
)) != 0;
613 nir_phi_instr_create(nir_shader
*shader
)
615 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
616 instr_init(&instr
->instr
, nir_instr_type_phi
);
618 dest_init(&instr
->dest
);
619 exec_list_make_empty(&instr
->srcs
);
623 nir_parallel_copy_instr
*
624 nir_parallel_copy_instr_create(nir_shader
*shader
)
626 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
627 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
629 exec_list_make_empty(&instr
->entries
);
634 nir_ssa_undef_instr
*
635 nir_ssa_undef_instr_create(nir_shader
*shader
,
636 unsigned num_components
,
639 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
640 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
642 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
647 static nir_const_value
648 const_value_float(double d
, unsigned bit_size
)
652 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
653 case 32: v
.f32
[0] = d
; break;
654 case 64: v
.f64
[0] = d
; break;
656 unreachable("Invalid bit size");
661 static nir_const_value
662 const_value_int(int64_t i
, unsigned bit_size
)
666 case 1: v
.b
[0] = i
& 1; break;
667 case 8: v
.i8
[0] = i
; break;
668 case 16: v
.i16
[0] = i
; break;
669 case 32: v
.i32
[0] = i
; break;
670 case 64: v
.i64
[0] = i
; break;
672 unreachable("Invalid bit size");
678 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
680 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
681 const int64_t min_int
= -max_int
- 1;
684 return const_value_int(0, bit_size
);
686 return const_value_float(0, bit_size
);
688 return const_value_int(1, bit_size
);
690 return const_value_float(1, bit_size
);
692 return const_value_int(max_int
, bit_size
);
694 return const_value_int(~0ull, bit_size
);
696 return const_value_float(INFINITY
, bit_size
);
698 return const_value_int(min_int
, bit_size
);
700 return const_value_int(0, bit_size
);
702 return const_value_float(-INFINITY
, bit_size
);
704 return const_value_int(~0ull, bit_size
);
706 return const_value_int(0, bit_size
);
708 return const_value_int(0, bit_size
);
710 unreachable("Invalid reduction operation");
715 nir_cf_node_get_function(nir_cf_node
*node
)
717 while (node
->type
!= nir_cf_node_function
) {
721 return nir_cf_node_as_function(node
);
724 /* Reduces a cursor by trying to convert everything to after and trying to
725 * go up to block granularity when possible.
728 reduce_cursor(nir_cursor cursor
)
730 switch (cursor
.option
) {
731 case nir_cursor_before_block
:
732 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
733 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
734 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
735 /* Empty block. After is as good as before. */
736 cursor
.option
= nir_cursor_after_block
;
740 case nir_cursor_after_block
:
743 case nir_cursor_before_instr
: {
744 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
746 /* Before this instruction is after the previous */
747 cursor
.instr
= prev_instr
;
748 cursor
.option
= nir_cursor_after_instr
;
750 /* No previous instruction. Switch to before block */
751 cursor
.block
= cursor
.instr
->block
;
752 cursor
.option
= nir_cursor_before_block
;
754 return reduce_cursor(cursor
);
757 case nir_cursor_after_instr
:
758 if (nir_instr_next(cursor
.instr
) == NULL
) {
759 /* This is the last instruction, switch to after block */
760 cursor
.option
= nir_cursor_after_block
;
761 cursor
.block
= cursor
.instr
->block
;
766 unreachable("Inavlid cursor option");
771 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
773 /* Reduced cursors should be unique */
774 a
= reduce_cursor(a
);
775 b
= reduce_cursor(b
);
777 return a
.block
== b
.block
&& a
.option
== b
.option
;
781 add_use_cb(nir_src
*src
, void *state
)
783 nir_instr
*instr
= state
;
785 src
->parent_instr
= instr
;
786 list_addtail(&src
->use_link
,
787 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
793 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
795 nir_instr
*instr
= state
;
797 if (instr
->block
&& def
->index
== UINT_MAX
) {
798 nir_function_impl
*impl
=
799 nir_cf_node_get_function(&instr
->block
->cf_node
);
801 def
->index
= impl
->ssa_alloc
++;
808 add_reg_def_cb(nir_dest
*dest
, void *state
)
810 nir_instr
*instr
= state
;
813 dest
->reg
.parent_instr
= instr
;
814 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
821 add_defs_uses(nir_instr
*instr
)
823 nir_foreach_src(instr
, add_use_cb
, instr
);
824 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
825 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
829 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
831 switch (cursor
.option
) {
832 case nir_cursor_before_block
:
833 /* Only allow inserting jumps into empty blocks. */
834 if (instr
->type
== nir_instr_type_jump
)
835 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
837 instr
->block
= cursor
.block
;
838 add_defs_uses(instr
);
839 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
841 case nir_cursor_after_block
: {
842 /* Inserting instructions after a jump is illegal. */
843 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
844 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
847 instr
->block
= cursor
.block
;
848 add_defs_uses(instr
);
849 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
852 case nir_cursor_before_instr
:
853 assert(instr
->type
!= nir_instr_type_jump
);
854 instr
->block
= cursor
.instr
->block
;
855 add_defs_uses(instr
);
856 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
858 case nir_cursor_after_instr
:
859 /* Inserting instructions after a jump is illegal. */
860 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
862 /* Only allow inserting jumps at the end of the block. */
863 if (instr
->type
== nir_instr_type_jump
)
864 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
866 instr
->block
= cursor
.instr
->block
;
867 add_defs_uses(instr
);
868 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
872 if (instr
->type
== nir_instr_type_jump
)
873 nir_handle_add_jump(instr
->block
);
877 src_is_valid(const nir_src
*src
)
879 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
883 remove_use_cb(nir_src
*src
, void *state
)
887 if (src_is_valid(src
))
888 list_del(&src
->use_link
);
894 remove_def_cb(nir_dest
*dest
, void *state
)
899 list_del(&dest
->reg
.def_link
);
905 remove_defs_uses(nir_instr
*instr
)
907 nir_foreach_dest(instr
, remove_def_cb
, instr
);
908 nir_foreach_src(instr
, remove_use_cb
, instr
);
911 void nir_instr_remove_v(nir_instr
*instr
)
913 remove_defs_uses(instr
);
914 exec_node_remove(&instr
->node
);
916 if (instr
->type
== nir_instr_type_jump
) {
917 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
918 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
925 nir_index_local_regs(nir_function_impl
*impl
)
928 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
929 reg
->index
= index
++;
931 impl
->reg_alloc
= index
;
935 nir_index_global_regs(nir_shader
*shader
)
938 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
939 reg
->index
= index
++;
941 shader
->reg_alloc
= index
;
945 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
947 return cb(&instr
->dest
.dest
, state
);
951 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
953 return cb(&instr
->dest
, state
);
957 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
960 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
961 return cb(&instr
->dest
, state
);
967 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
970 return cb(&instr
->dest
, state
);
974 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
976 return cb(&instr
->dest
, state
);
980 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
981 nir_foreach_dest_cb cb
, void *state
)
983 nir_foreach_parallel_copy_entry(entry
, instr
) {
984 if (!cb(&entry
->dest
, state
))
992 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
994 switch (instr
->type
) {
995 case nir_instr_type_alu
:
996 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
997 case nir_instr_type_deref
:
998 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
999 case nir_instr_type_intrinsic
:
1000 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
1001 case nir_instr_type_tex
:
1002 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
1003 case nir_instr_type_phi
:
1004 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1005 case nir_instr_type_parallel_copy
:
1006 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1009 case nir_instr_type_load_const
:
1010 case nir_instr_type_ssa_undef
:
1011 case nir_instr_type_call
:
1012 case nir_instr_type_jump
:
1016 unreachable("Invalid instruction type");
1023 struct foreach_ssa_def_state
{
1024 nir_foreach_ssa_def_cb cb
;
1029 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1031 struct foreach_ssa_def_state
*state
= void_state
;
1034 return state
->cb(&dest
->ssa
, state
->client_state
);
1040 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1042 switch (instr
->type
) {
1043 case nir_instr_type_alu
:
1044 case nir_instr_type_deref
:
1045 case nir_instr_type_tex
:
1046 case nir_instr_type_intrinsic
:
1047 case nir_instr_type_phi
:
1048 case nir_instr_type_parallel_copy
: {
1049 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1050 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1053 case nir_instr_type_load_const
:
1054 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1055 case nir_instr_type_ssa_undef
:
1056 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1057 case nir_instr_type_call
:
1058 case nir_instr_type_jump
:
1061 unreachable("Invalid instruction type");
1066 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1068 if (!cb(src
, state
))
1070 if (!src
->is_ssa
&& src
->reg
.indirect
)
1071 return cb(src
->reg
.indirect
, state
);
1076 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1078 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1079 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1086 visit_deref_instr_src(nir_deref_instr
*instr
,
1087 nir_foreach_src_cb cb
, void *state
)
1089 if (instr
->deref_type
!= nir_deref_type_var
) {
1090 if (!visit_src(&instr
->parent
, cb
, state
))
1094 if (instr
->deref_type
== nir_deref_type_array
||
1095 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1096 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1104 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1106 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1107 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1115 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1118 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1119 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1120 if (!visit_src(&instr
->src
[i
], cb
, state
))
1128 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1130 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1131 if (!visit_src(&instr
->params
[i
], cb
, state
))
1139 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1141 nir_foreach_phi_src(src
, instr
) {
1142 if (!visit_src(&src
->src
, cb
, state
))
1150 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1151 nir_foreach_src_cb cb
, void *state
)
1153 nir_foreach_parallel_copy_entry(entry
, instr
) {
1154 if (!visit_src(&entry
->src
, cb
, state
))
1163 nir_foreach_src_cb cb
;
1164 } visit_dest_indirect_state
;
1167 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1169 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1171 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1172 return state
->cb(dest
->reg
.indirect
, state
->state
);
1178 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1180 switch (instr
->type
) {
1181 case nir_instr_type_alu
:
1182 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1185 case nir_instr_type_deref
:
1186 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1189 case nir_instr_type_intrinsic
:
1190 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1193 case nir_instr_type_tex
:
1194 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1197 case nir_instr_type_call
:
1198 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1201 case nir_instr_type_load_const
:
1202 /* Constant load instructions have no regular sources */
1204 case nir_instr_type_phi
:
1205 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1208 case nir_instr_type_parallel_copy
:
1209 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1213 case nir_instr_type_jump
:
1214 case nir_instr_type_ssa_undef
:
1218 unreachable("Invalid instruction type");
1222 visit_dest_indirect_state dest_state
;
1223 dest_state
.state
= state
;
1225 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1229 nir_src_comp_as_int(nir_src src
, unsigned comp
)
1231 assert(nir_src_is_const(src
));
1232 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1234 assert(comp
< load
->def
.num_components
);
1235 switch (load
->def
.bit_size
) {
1236 /* int1_t uses 0/-1 convention */
1237 case 1: return -(int)load
->value
.b
[comp
];
1238 case 8: return load
->value
.i8
[comp
];
1239 case 16: return load
->value
.i16
[comp
];
1240 case 32: return load
->value
.i32
[comp
];
1241 case 64: return load
->value
.i64
[comp
];
1243 unreachable("Invalid bit size");
1248 nir_src_comp_as_uint(nir_src src
, unsigned comp
)
1250 assert(nir_src_is_const(src
));
1251 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1253 assert(comp
< load
->def
.num_components
);
1254 switch (load
->def
.bit_size
) {
1255 case 1: return load
->value
.b
[comp
];
1256 case 8: return load
->value
.u8
[comp
];
1257 case 16: return load
->value
.u16
[comp
];
1258 case 32: return load
->value
.u32
[comp
];
1259 case 64: return load
->value
.u64
[comp
];
1261 unreachable("Invalid bit size");
1266 nir_src_comp_as_bool(nir_src src
, unsigned comp
)
1268 int64_t i
= nir_src_comp_as_int(src
, comp
);
1270 /* Booleans of any size use 0/-1 convention */
1271 assert(i
== 0 || i
== -1);
1277 nir_src_comp_as_float(nir_src src
, unsigned comp
)
1279 assert(nir_src_is_const(src
));
1280 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1282 assert(comp
< load
->def
.num_components
);
1283 switch (load
->def
.bit_size
) {
1284 case 16: return _mesa_half_to_float(load
->value
.u16
[comp
]);
1285 case 32: return load
->value
.f32
[comp
];
1286 case 64: return load
->value
.f64
[comp
];
1288 unreachable("Invalid bit size");
1293 nir_src_as_int(nir_src src
)
1295 assert(nir_src_num_components(src
) == 1);
1296 return nir_src_comp_as_int(src
, 0);
1300 nir_src_as_uint(nir_src src
)
1302 assert(nir_src_num_components(src
) == 1);
1303 return nir_src_comp_as_uint(src
, 0);
1307 nir_src_as_bool(nir_src src
)
1309 assert(nir_src_num_components(src
) == 1);
1310 return nir_src_comp_as_bool(src
, 0);
1314 nir_src_as_float(nir_src src
)
1316 assert(nir_src_num_components(src
) == 1);
1317 return nir_src_comp_as_float(src
, 0);
1321 nir_src_as_const_value(nir_src src
)
1326 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1329 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1331 return &load
->value
;
1335 * Returns true if the source is known to be dynamically uniform. Otherwise it
1336 * returns false which means it may or may not be dynamically uniform but it
1337 * can't be determined.
1340 nir_src_is_dynamically_uniform(nir_src src
)
1345 /* Constants are trivially dynamically uniform */
1346 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1349 /* As are uniform variables */
1350 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1351 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1353 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1357 /* XXX: this could have many more tests, such as when a sampler function is
1358 * called with dynamically uniform arguments.
1364 src_remove_all_uses(nir_src
*src
)
1366 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1367 if (!src_is_valid(src
))
1370 list_del(&src
->use_link
);
1375 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1377 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1378 if (!src_is_valid(src
))
1382 src
->parent_instr
= parent_instr
;
1384 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1386 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1389 src
->parent_if
= parent_if
;
1391 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1393 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1399 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1401 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1403 src_remove_all_uses(src
);
1405 src_add_all_uses(src
, instr
, NULL
);
1409 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1411 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1413 src_remove_all_uses(dest
);
1414 src_remove_all_uses(src
);
1416 *src
= NIR_SRC_INIT
;
1417 src_add_all_uses(dest
, dest_instr
, NULL
);
1421 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1423 nir_src
*src
= &if_stmt
->condition
;
1424 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1426 src_remove_all_uses(src
);
1428 src_add_all_uses(src
, NULL
, if_stmt
);
1432 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1435 /* We can only overwrite an SSA destination if it has no uses. */
1436 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1438 list_del(&dest
->reg
.def_link
);
1439 if (dest
->reg
.indirect
)
1440 src_remove_all_uses(dest
->reg
.indirect
);
1443 /* We can't re-write with an SSA def */
1444 assert(!new_dest
.is_ssa
);
1446 nir_dest_copy(dest
, &new_dest
, instr
);
1448 dest
->reg
.parent_instr
= instr
;
1449 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1451 if (dest
->reg
.indirect
)
1452 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1455 /* note: does *not* take ownership of 'name' */
1457 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1458 unsigned num_components
,
1459 unsigned bit_size
, const char *name
)
1461 def
->name
= ralloc_strdup(instr
, name
);
1462 def
->parent_instr
= instr
;
1463 list_inithead(&def
->uses
);
1464 list_inithead(&def
->if_uses
);
1465 def
->num_components
= num_components
;
1466 def
->bit_size
= bit_size
;
1469 nir_function_impl
*impl
=
1470 nir_cf_node_get_function(&instr
->block
->cf_node
);
1472 def
->index
= impl
->ssa_alloc
++;
1474 def
->index
= UINT_MAX
;
1478 /* note: does *not* take ownership of 'name' */
1480 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1481 unsigned num_components
, unsigned bit_size
,
1484 dest
->is_ssa
= true;
1485 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1489 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1491 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1493 nir_foreach_use_safe(use_src
, def
)
1494 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1496 nir_foreach_if_use_safe(use_src
, def
)
1497 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1501 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1503 assert(start
->block
== end
->block
);
1505 if (between
->block
!= start
->block
)
1508 /* Search backwards looking for "between" */
1509 while (start
!= end
) {
1513 end
= nir_instr_prev(end
);
1520 /* Replaces all uses of the given SSA def with the given source but only if
1521 * the use comes after the after_me instruction. This can be useful if you
1522 * are emitting code to fix up the result of some instruction: you can freely
1523 * use the result in that code and then call rewrite_uses_after and pass the
1524 * last fixup instruction as after_me and it will replace all of the uses you
1525 * want without touching the fixup code.
1527 * This function assumes that after_me is in the same block as
1528 * def->parent_instr and that after_me comes after def->parent_instr.
1531 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1532 nir_instr
*after_me
)
1534 if (new_src
.is_ssa
&& def
== new_src
.ssa
)
1537 nir_foreach_use_safe(use_src
, def
) {
1538 assert(use_src
->parent_instr
!= def
->parent_instr
);
1539 /* Since def already dominates all of its uses, the only way a use can
1540 * not be dominated by after_me is if it is between def and after_me in
1541 * the instruction list.
1543 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1544 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1547 nir_foreach_if_use_safe(use_src
, def
)
1548 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1551 nir_component_mask_t
1552 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1554 nir_component_mask_t read_mask
= 0;
1555 nir_foreach_use(use
, def
) {
1556 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1557 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1558 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1559 int src_idx
= alu_src
- &alu
->src
[0];
1560 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1561 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1563 return (1 << def
->num_components
) - 1;
1567 if (!list_empty(&def
->if_uses
))
1574 nir_block_cf_tree_next(nir_block
*block
)
1576 if (block
== NULL
) {
1577 /* nir_foreach_block_safe() will call this function on a NULL block
1578 * after the last iteration, but it won't use the result so just return
1584 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1586 return nir_cf_node_cf_tree_first(cf_next
);
1588 nir_cf_node
*parent
= block
->cf_node
.parent
;
1590 switch (parent
->type
) {
1591 case nir_cf_node_if
: {
1592 /* Are we at the end of the if? Go to the beginning of the else */
1593 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1594 if (block
== nir_if_last_then_block(if_stmt
))
1595 return nir_if_first_else_block(if_stmt
);
1597 assert(block
== nir_if_last_else_block(if_stmt
));
1601 case nir_cf_node_loop
:
1602 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1604 case nir_cf_node_function
:
1608 unreachable("unknown cf node type");
1613 nir_block_cf_tree_prev(nir_block
*block
)
1615 if (block
== NULL
) {
1616 /* do this for consistency with nir_block_cf_tree_next() */
1620 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1622 return nir_cf_node_cf_tree_last(cf_prev
);
1624 nir_cf_node
*parent
= block
->cf_node
.parent
;
1626 switch (parent
->type
) {
1627 case nir_cf_node_if
: {
1628 /* Are we at the beginning of the else? Go to the end of the if */
1629 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1630 if (block
== nir_if_first_else_block(if_stmt
))
1631 return nir_if_last_then_block(if_stmt
);
1633 assert(block
== nir_if_first_then_block(if_stmt
));
1637 case nir_cf_node_loop
:
1638 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1640 case nir_cf_node_function
:
1644 unreachable("unknown cf node type");
1648 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1650 switch (node
->type
) {
1651 case nir_cf_node_function
: {
1652 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1653 return nir_start_block(impl
);
1656 case nir_cf_node_if
: {
1657 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1658 return nir_if_first_then_block(if_stmt
);
1661 case nir_cf_node_loop
: {
1662 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1663 return nir_loop_first_block(loop
);
1666 case nir_cf_node_block
: {
1667 return nir_cf_node_as_block(node
);
1671 unreachable("unknown node type");
1675 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1677 switch (node
->type
) {
1678 case nir_cf_node_function
: {
1679 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1680 return nir_impl_last_block(impl
);
1683 case nir_cf_node_if
: {
1684 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1685 return nir_if_last_else_block(if_stmt
);
1688 case nir_cf_node_loop
: {
1689 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1690 return nir_loop_last_block(loop
);
1693 case nir_cf_node_block
: {
1694 return nir_cf_node_as_block(node
);
1698 unreachable("unknown node type");
1702 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1704 if (node
->type
== nir_cf_node_block
)
1705 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1706 else if (node
->type
== nir_cf_node_function
)
1709 return nir_cf_node_as_block(nir_cf_node_next(node
));
1713 nir_block_get_following_if(nir_block
*block
)
1715 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1718 if (nir_cf_node_is_last(&block
->cf_node
))
1721 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1723 if (next_node
->type
!= nir_cf_node_if
)
1726 return nir_cf_node_as_if(next_node
);
1730 nir_block_get_following_loop(nir_block
*block
)
1732 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1735 if (nir_cf_node_is_last(&block
->cf_node
))
1738 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1740 if (next_node
->type
!= nir_cf_node_loop
)
1743 return nir_cf_node_as_loop(next_node
);
1747 nir_index_blocks(nir_function_impl
*impl
)
1751 if (impl
->valid_metadata
& nir_metadata_block_index
)
1754 nir_foreach_block(block
, impl
) {
1755 block
->index
= index
++;
1758 /* The end_block isn't really part of the program, which is why its index
1761 impl
->num_blocks
= impl
->end_block
->index
= index
;
1765 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1767 unsigned *index
= (unsigned *) state
;
1768 def
->index
= (*index
)++;
1774 * The indices are applied top-to-bottom which has the very nice property
1775 * that, if A dominates B, then A->index <= B->index.
1778 nir_index_ssa_defs(nir_function_impl
*impl
)
1782 nir_foreach_block(block
, impl
) {
1783 nir_foreach_instr(instr
, block
)
1784 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1787 impl
->ssa_alloc
= index
;
1791 * The indices are applied top-to-bottom which has the very nice property
1792 * that, if A dominates B, then A->index <= B->index.
1795 nir_index_instrs(nir_function_impl
*impl
)
1799 nir_foreach_block(block
, impl
) {
1800 nir_foreach_instr(instr
, block
)
1801 instr
->index
= index
++;
1808 nir_intrinsic_from_system_value(gl_system_value val
)
1811 case SYSTEM_VALUE_VERTEX_ID
:
1812 return nir_intrinsic_load_vertex_id
;
1813 case SYSTEM_VALUE_INSTANCE_ID
:
1814 return nir_intrinsic_load_instance_id
;
1815 case SYSTEM_VALUE_DRAW_ID
:
1816 return nir_intrinsic_load_draw_id
;
1817 case SYSTEM_VALUE_BASE_INSTANCE
:
1818 return nir_intrinsic_load_base_instance
;
1819 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1820 return nir_intrinsic_load_vertex_id_zero_base
;
1821 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
1822 return nir_intrinsic_load_is_indexed_draw
;
1823 case SYSTEM_VALUE_FIRST_VERTEX
:
1824 return nir_intrinsic_load_first_vertex
;
1825 case SYSTEM_VALUE_BASE_VERTEX
:
1826 return nir_intrinsic_load_base_vertex
;
1827 case SYSTEM_VALUE_INVOCATION_ID
:
1828 return nir_intrinsic_load_invocation_id
;
1829 case SYSTEM_VALUE_FRAG_COORD
:
1830 return nir_intrinsic_load_frag_coord
;
1831 case SYSTEM_VALUE_FRONT_FACE
:
1832 return nir_intrinsic_load_front_face
;
1833 case SYSTEM_VALUE_SAMPLE_ID
:
1834 return nir_intrinsic_load_sample_id
;
1835 case SYSTEM_VALUE_SAMPLE_POS
:
1836 return nir_intrinsic_load_sample_pos
;
1837 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1838 return nir_intrinsic_load_sample_mask_in
;
1839 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1840 return nir_intrinsic_load_local_invocation_id
;
1841 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
1842 return nir_intrinsic_load_local_invocation_index
;
1843 case SYSTEM_VALUE_WORK_GROUP_ID
:
1844 return nir_intrinsic_load_work_group_id
;
1845 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1846 return nir_intrinsic_load_num_work_groups
;
1847 case SYSTEM_VALUE_PRIMITIVE_ID
:
1848 return nir_intrinsic_load_primitive_id
;
1849 case SYSTEM_VALUE_TESS_COORD
:
1850 return nir_intrinsic_load_tess_coord
;
1851 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1852 return nir_intrinsic_load_tess_level_outer
;
1853 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1854 return nir_intrinsic_load_tess_level_inner
;
1855 case SYSTEM_VALUE_VERTICES_IN
:
1856 return nir_intrinsic_load_patch_vertices_in
;
1857 case SYSTEM_VALUE_HELPER_INVOCATION
:
1858 return nir_intrinsic_load_helper_invocation
;
1859 case SYSTEM_VALUE_VIEW_INDEX
:
1860 return nir_intrinsic_load_view_index
;
1861 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1862 return nir_intrinsic_load_subgroup_size
;
1863 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1864 return nir_intrinsic_load_subgroup_invocation
;
1865 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1866 return nir_intrinsic_load_subgroup_eq_mask
;
1867 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1868 return nir_intrinsic_load_subgroup_ge_mask
;
1869 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1870 return nir_intrinsic_load_subgroup_gt_mask
;
1871 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1872 return nir_intrinsic_load_subgroup_le_mask
;
1873 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1874 return nir_intrinsic_load_subgroup_lt_mask
;
1875 case SYSTEM_VALUE_NUM_SUBGROUPS
:
1876 return nir_intrinsic_load_num_subgroups
;
1877 case SYSTEM_VALUE_SUBGROUP_ID
:
1878 return nir_intrinsic_load_subgroup_id
;
1879 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
1880 return nir_intrinsic_load_local_group_size
;
1881 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
1882 return nir_intrinsic_load_global_invocation_id
;
1883 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
:
1884 return nir_intrinsic_load_global_invocation_index
;
1885 case SYSTEM_VALUE_WORK_DIM
:
1886 return nir_intrinsic_load_work_dim
;
1888 unreachable("system value does not directly correspond to intrinsic");
1893 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1896 case nir_intrinsic_load_vertex_id
:
1897 return SYSTEM_VALUE_VERTEX_ID
;
1898 case nir_intrinsic_load_instance_id
:
1899 return SYSTEM_VALUE_INSTANCE_ID
;
1900 case nir_intrinsic_load_draw_id
:
1901 return SYSTEM_VALUE_DRAW_ID
;
1902 case nir_intrinsic_load_base_instance
:
1903 return SYSTEM_VALUE_BASE_INSTANCE
;
1904 case nir_intrinsic_load_vertex_id_zero_base
:
1905 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1906 case nir_intrinsic_load_first_vertex
:
1907 return SYSTEM_VALUE_FIRST_VERTEX
;
1908 case nir_intrinsic_load_is_indexed_draw
:
1909 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
1910 case nir_intrinsic_load_base_vertex
:
1911 return SYSTEM_VALUE_BASE_VERTEX
;
1912 case nir_intrinsic_load_invocation_id
:
1913 return SYSTEM_VALUE_INVOCATION_ID
;
1914 case nir_intrinsic_load_frag_coord
:
1915 return SYSTEM_VALUE_FRAG_COORD
;
1916 case nir_intrinsic_load_front_face
:
1917 return SYSTEM_VALUE_FRONT_FACE
;
1918 case nir_intrinsic_load_sample_id
:
1919 return SYSTEM_VALUE_SAMPLE_ID
;
1920 case nir_intrinsic_load_sample_pos
:
1921 return SYSTEM_VALUE_SAMPLE_POS
;
1922 case nir_intrinsic_load_sample_mask_in
:
1923 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1924 case nir_intrinsic_load_local_invocation_id
:
1925 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1926 case nir_intrinsic_load_local_invocation_index
:
1927 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1928 case nir_intrinsic_load_num_work_groups
:
1929 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1930 case nir_intrinsic_load_work_group_id
:
1931 return SYSTEM_VALUE_WORK_GROUP_ID
;
1932 case nir_intrinsic_load_primitive_id
:
1933 return SYSTEM_VALUE_PRIMITIVE_ID
;
1934 case nir_intrinsic_load_tess_coord
:
1935 return SYSTEM_VALUE_TESS_COORD
;
1936 case nir_intrinsic_load_tess_level_outer
:
1937 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1938 case nir_intrinsic_load_tess_level_inner
:
1939 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1940 case nir_intrinsic_load_patch_vertices_in
:
1941 return SYSTEM_VALUE_VERTICES_IN
;
1942 case nir_intrinsic_load_helper_invocation
:
1943 return SYSTEM_VALUE_HELPER_INVOCATION
;
1944 case nir_intrinsic_load_view_index
:
1945 return SYSTEM_VALUE_VIEW_INDEX
;
1946 case nir_intrinsic_load_subgroup_size
:
1947 return SYSTEM_VALUE_SUBGROUP_SIZE
;
1948 case nir_intrinsic_load_subgroup_invocation
:
1949 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1950 case nir_intrinsic_load_subgroup_eq_mask
:
1951 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
1952 case nir_intrinsic_load_subgroup_ge_mask
:
1953 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
1954 case nir_intrinsic_load_subgroup_gt_mask
:
1955 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
1956 case nir_intrinsic_load_subgroup_le_mask
:
1957 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
1958 case nir_intrinsic_load_subgroup_lt_mask
:
1959 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
1960 case nir_intrinsic_load_num_subgroups
:
1961 return SYSTEM_VALUE_NUM_SUBGROUPS
;
1962 case nir_intrinsic_load_subgroup_id
:
1963 return SYSTEM_VALUE_SUBGROUP_ID
;
1964 case nir_intrinsic_load_local_group_size
:
1965 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1966 case nir_intrinsic_load_global_invocation_id
:
1967 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1969 unreachable("intrinsic doesn't produce a system value");
1973 /* OpenGL utility method that remaps the location attributes if they are
1974 * doubles. Not needed for vulkan due the differences on the input location
1975 * count for doubles on vulkan vs OpenGL
1977 * The bitfield returned in dual_slot is one bit for each double input slot in
1978 * the original OpenGL single-slot input numbering. The mapping from old
1979 * locations to new locations is as follows:
1981 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
1984 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
1986 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
1989 nir_foreach_variable(var
, &shader
->inputs
) {
1990 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
1991 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
1992 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
1996 nir_foreach_variable(var
, &shader
->inputs
) {
1997 var
->data
.location
+=
1998 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
2002 /* Returns an attribute mask that has been re-compacted using the given
2006 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
2009 unsigned loc
= u_bit_scan64(&dual_slot
);
2010 /* mask of all bits up to and including loc */
2011 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
2012 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);