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");
129 assert(!"nir_shader_add_variable cannot be used for local variables");
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
:
147 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
151 assert(shader
->info
.stage
== MESA_SHADER_COMPUTE
);
152 exec_list_push_tail(&shader
->shared
, &var
->node
);
155 case nir_var_system_value
:
156 exec_list_push_tail(&shader
->system_values
, &var
->node
);
162 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
163 const struct glsl_type
*type
, const char *name
)
165 nir_variable
*var
= rzalloc(shader
, nir_variable
);
166 var
->name
= ralloc_strdup(var
, name
);
168 var
->data
.mode
= mode
;
169 var
->data
.how_declared
= nir_var_declared_normally
;
171 if ((mode
== nir_var_shader_in
&&
172 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
173 (mode
== nir_var_shader_out
&&
174 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
175 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
177 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
178 var
->data
.read_only
= true;
180 nir_shader_add_variable(shader
, var
);
186 nir_local_variable_create(nir_function_impl
*impl
,
187 const struct glsl_type
*type
, const char *name
)
189 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
190 var
->name
= ralloc_strdup(var
, name
);
192 var
->data
.mode
= nir_var_local
;
194 nir_function_impl_add_variable(impl
, var
);
200 nir_function_create(nir_shader
*shader
, const char *name
)
202 nir_function
*func
= ralloc(shader
, nir_function
);
204 exec_list_push_tail(&shader
->functions
, &func
->node
);
206 func
->name
= ralloc_strdup(func
, name
);
207 func
->shader
= shader
;
208 func
->num_params
= 0;
215 /* NOTE: if the instruction you are copying a src to is already added
216 * to the IR, use nir_instr_rewrite_src() instead.
218 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
220 dest
->is_ssa
= src
->is_ssa
;
222 dest
->ssa
= src
->ssa
;
224 dest
->reg
.base_offset
= src
->reg
.base_offset
;
225 dest
->reg
.reg
= src
->reg
.reg
;
226 if (src
->reg
.indirect
) {
227 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
228 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
230 dest
->reg
.indirect
= NULL
;
235 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
237 /* Copying an SSA definition makes no sense whatsoever. */
238 assert(!src
->is_ssa
);
240 dest
->is_ssa
= false;
242 dest
->reg
.base_offset
= src
->reg
.base_offset
;
243 dest
->reg
.reg
= src
->reg
.reg
;
244 if (src
->reg
.indirect
) {
245 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
246 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
248 dest
->reg
.indirect
= NULL
;
253 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
254 nir_alu_instr
*instr
)
256 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
257 dest
->abs
= src
->abs
;
258 dest
->negate
= src
->negate
;
259 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
260 dest
->swizzle
[i
] = src
->swizzle
[i
];
264 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
265 nir_alu_instr
*instr
)
267 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
268 dest
->write_mask
= src
->write_mask
;
269 dest
->saturate
= src
->saturate
;
274 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
276 exec_node_init(&node
->node
);
282 nir_function_impl_create_bare(nir_shader
*shader
)
284 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
286 impl
->function
= NULL
;
288 cf_init(&impl
->cf_node
, nir_cf_node_function
);
290 exec_list_make_empty(&impl
->body
);
291 exec_list_make_empty(&impl
->registers
);
292 exec_list_make_empty(&impl
->locals
);
295 impl
->valid_metadata
= nir_metadata_none
;
297 /* create start & end blocks */
298 nir_block
*start_block
= nir_block_create(shader
);
299 nir_block
*end_block
= nir_block_create(shader
);
300 start_block
->cf_node
.parent
= &impl
->cf_node
;
301 end_block
->cf_node
.parent
= &impl
->cf_node
;
302 impl
->end_block
= end_block
;
304 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
306 start_block
->successors
[0] = end_block
;
307 _mesa_set_add(end_block
->predecessors
, start_block
);
312 nir_function_impl_create(nir_function
*function
)
314 assert(function
->impl
== NULL
);
316 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
318 function
->impl
= impl
;
319 impl
->function
= function
;
325 nir_block_create(nir_shader
*shader
)
327 nir_block
*block
= rzalloc(shader
, nir_block
);
329 cf_init(&block
->cf_node
, nir_cf_node_block
);
331 block
->successors
[0] = block
->successors
[1] = NULL
;
332 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
333 _mesa_key_pointer_equal
);
334 block
->imm_dom
= NULL
;
335 /* XXX maybe it would be worth it to defer allocation? This
336 * way it doesn't get allocated for shader refs that never run
337 * nir_calc_dominance? For example, state-tracker creates an
338 * initial IR, clones that, runs appropriate lowering pass, passes
339 * to driver which does common lowering/opt, and then stores ref
340 * which is later used to do state specific lowering and futher
341 * opt. Do any of the references not need dominance metadata?
343 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
344 _mesa_key_pointer_equal
);
346 exec_list_make_empty(&block
->instr_list
);
352 src_init(nir_src
*src
)
356 src
->reg
.indirect
= NULL
;
357 src
->reg
.base_offset
= 0;
361 nir_if_create(nir_shader
*shader
)
363 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
365 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
366 src_init(&if_stmt
->condition
);
368 nir_block
*then
= nir_block_create(shader
);
369 exec_list_make_empty(&if_stmt
->then_list
);
370 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
371 then
->cf_node
.parent
= &if_stmt
->cf_node
;
373 nir_block
*else_stmt
= nir_block_create(shader
);
374 exec_list_make_empty(&if_stmt
->else_list
);
375 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
376 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
382 nir_loop_create(nir_shader
*shader
)
384 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
386 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
388 nir_block
*body
= nir_block_create(shader
);
389 exec_list_make_empty(&loop
->body
);
390 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
391 body
->cf_node
.parent
= &loop
->cf_node
;
393 body
->successors
[0] = body
;
394 _mesa_set_add(body
->predecessors
, body
);
400 instr_init(nir_instr
*instr
, nir_instr_type type
)
404 exec_node_init(&instr
->node
);
408 dest_init(nir_dest
*dest
)
410 dest
->is_ssa
= false;
411 dest
->reg
.reg
= NULL
;
412 dest
->reg
.indirect
= NULL
;
413 dest
->reg
.base_offset
= 0;
417 alu_dest_init(nir_alu_dest
*dest
)
419 dest_init(&dest
->dest
);
420 dest
->saturate
= false;
421 dest
->write_mask
= 0xf;
425 alu_src_init(nir_alu_src
*src
)
428 src
->abs
= src
->negate
= false;
429 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
434 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
436 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
437 /* TODO: don't use rzalloc */
438 nir_alu_instr
*instr
=
440 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
442 instr_init(&instr
->instr
, nir_instr_type_alu
);
444 alu_dest_init(&instr
->dest
);
445 for (unsigned i
= 0; i
< num_srcs
; i
++)
446 alu_src_init(&instr
->src
[i
]);
452 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
454 nir_deref_instr
*instr
=
455 rzalloc_size(shader
, sizeof(nir_deref_instr
));
457 instr_init(&instr
->instr
, nir_instr_type_deref
);
459 instr
->deref_type
= deref_type
;
460 if (deref_type
!= nir_deref_type_var
)
461 src_init(&instr
->parent
);
463 if (deref_type
== nir_deref_type_array
||
464 deref_type
== nir_deref_type_ptr_as_array
)
465 src_init(&instr
->arr
.index
);
467 dest_init(&instr
->dest
);
473 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
475 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
476 instr_init(&instr
->instr
, nir_instr_type_jump
);
481 nir_load_const_instr
*
482 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
485 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
486 instr_init(&instr
->instr
, nir_instr_type_load_const
);
488 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
493 nir_intrinsic_instr
*
494 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
496 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
497 /* TODO: don't use rzalloc */
498 nir_intrinsic_instr
*instr
=
500 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
502 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
503 instr
->intrinsic
= op
;
505 if (nir_intrinsic_infos
[op
].has_dest
)
506 dest_init(&instr
->dest
);
508 for (unsigned i
= 0; i
< num_srcs
; i
++)
509 src_init(&instr
->src
[i
]);
515 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
517 const unsigned num_params
= callee
->num_params
;
518 nir_call_instr
*instr
=
519 rzalloc_size(shader
, sizeof(*instr
) +
520 num_params
* sizeof(instr
->params
[0]));
522 instr_init(&instr
->instr
, nir_instr_type_call
);
523 instr
->callee
= callee
;
524 instr
->num_params
= num_params
;
525 for (unsigned i
= 0; i
< num_params
; i
++)
526 src_init(&instr
->params
[i
]);
532 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
534 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
535 instr_init(&instr
->instr
, nir_instr_type_tex
);
537 dest_init(&instr
->dest
);
539 instr
->num_srcs
= num_srcs
;
540 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
541 for (unsigned i
= 0; i
< num_srcs
; i
++)
542 src_init(&instr
->src
[i
].src
);
544 instr
->texture_index
= 0;
545 instr
->texture_array_size
= 0;
546 instr
->sampler_index
= 0;
552 nir_tex_instr_add_src(nir_tex_instr
*tex
,
553 nir_tex_src_type src_type
,
556 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
559 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
560 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
561 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
565 ralloc_free(tex
->src
);
568 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
569 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
574 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
576 assert(src_idx
< tex
->num_srcs
);
578 /* First rewrite the source to NIR_SRC_INIT */
579 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
581 /* Now, move all of the other sources down */
582 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
583 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
584 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
590 nir_phi_instr_create(nir_shader
*shader
)
592 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
593 instr_init(&instr
->instr
, nir_instr_type_phi
);
595 dest_init(&instr
->dest
);
596 exec_list_make_empty(&instr
->srcs
);
600 nir_parallel_copy_instr
*
601 nir_parallel_copy_instr_create(nir_shader
*shader
)
603 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
604 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
606 exec_list_make_empty(&instr
->entries
);
611 nir_ssa_undef_instr
*
612 nir_ssa_undef_instr_create(nir_shader
*shader
,
613 unsigned num_components
,
616 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
617 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
619 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
624 static nir_const_value
625 const_value_float(double d
, unsigned bit_size
)
629 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
630 case 32: v
.f32
[0] = d
; break;
631 case 64: v
.f64
[0] = d
; break;
633 unreachable("Invalid bit size");
638 static nir_const_value
639 const_value_int(int64_t i
, unsigned bit_size
)
643 case 1: v
.b
[0] = i
& 1; break;
644 case 8: v
.i8
[0] = i
; break;
645 case 16: v
.i16
[0] = i
; break;
646 case 32: v
.i32
[0] = i
; break;
647 case 64: v
.i64
[0] = i
; break;
649 unreachable("Invalid bit size");
655 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
657 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
658 const int64_t min_int
= -max_int
- 1;
661 return const_value_int(0, bit_size
);
663 return const_value_float(0, bit_size
);
665 return const_value_int(1, bit_size
);
667 return const_value_float(1, bit_size
);
669 return const_value_int(max_int
, bit_size
);
671 return const_value_int(~0ull, bit_size
);
673 return const_value_float(INFINITY
, bit_size
);
675 return const_value_int(min_int
, bit_size
);
677 return const_value_int(0, bit_size
);
679 return const_value_float(-INFINITY
, bit_size
);
681 return const_value_int(~0ull, bit_size
);
683 return const_value_int(0, bit_size
);
685 return const_value_int(0, bit_size
);
687 unreachable("Invalid reduction operation");
692 nir_cf_node_get_function(nir_cf_node
*node
)
694 while (node
->type
!= nir_cf_node_function
) {
698 return nir_cf_node_as_function(node
);
701 /* Reduces a cursor by trying to convert everything to after and trying to
702 * go up to block granularity when possible.
705 reduce_cursor(nir_cursor cursor
)
707 switch (cursor
.option
) {
708 case nir_cursor_before_block
:
709 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
710 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
711 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
712 /* Empty block. After is as good as before. */
713 cursor
.option
= nir_cursor_after_block
;
717 case nir_cursor_after_block
:
720 case nir_cursor_before_instr
: {
721 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
723 /* Before this instruction is after the previous */
724 cursor
.instr
= prev_instr
;
725 cursor
.option
= nir_cursor_after_instr
;
727 /* No previous instruction. Switch to before block */
728 cursor
.block
= cursor
.instr
->block
;
729 cursor
.option
= nir_cursor_before_block
;
731 return reduce_cursor(cursor
);
734 case nir_cursor_after_instr
:
735 if (nir_instr_next(cursor
.instr
) == NULL
) {
736 /* This is the last instruction, switch to after block */
737 cursor
.option
= nir_cursor_after_block
;
738 cursor
.block
= cursor
.instr
->block
;
743 unreachable("Inavlid cursor option");
748 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
750 /* Reduced cursors should be unique */
751 a
= reduce_cursor(a
);
752 b
= reduce_cursor(b
);
754 return a
.block
== b
.block
&& a
.option
== b
.option
;
758 add_use_cb(nir_src
*src
, void *state
)
760 nir_instr
*instr
= state
;
762 src
->parent_instr
= instr
;
763 list_addtail(&src
->use_link
,
764 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
770 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
772 nir_instr
*instr
= state
;
774 if (instr
->block
&& def
->index
== UINT_MAX
) {
775 nir_function_impl
*impl
=
776 nir_cf_node_get_function(&instr
->block
->cf_node
);
778 def
->index
= impl
->ssa_alloc
++;
785 add_reg_def_cb(nir_dest
*dest
, void *state
)
787 nir_instr
*instr
= state
;
790 dest
->reg
.parent_instr
= instr
;
791 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
798 add_defs_uses(nir_instr
*instr
)
800 nir_foreach_src(instr
, add_use_cb
, instr
);
801 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
802 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
806 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
808 switch (cursor
.option
) {
809 case nir_cursor_before_block
:
810 /* Only allow inserting jumps into empty blocks. */
811 if (instr
->type
== nir_instr_type_jump
)
812 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
814 instr
->block
= cursor
.block
;
815 add_defs_uses(instr
);
816 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
818 case nir_cursor_after_block
: {
819 /* Inserting instructions after a jump is illegal. */
820 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
821 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
824 instr
->block
= cursor
.block
;
825 add_defs_uses(instr
);
826 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
829 case nir_cursor_before_instr
:
830 assert(instr
->type
!= nir_instr_type_jump
);
831 instr
->block
= cursor
.instr
->block
;
832 add_defs_uses(instr
);
833 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
835 case nir_cursor_after_instr
:
836 /* Inserting instructions after a jump is illegal. */
837 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
839 /* Only allow inserting jumps at the end of the block. */
840 if (instr
->type
== nir_instr_type_jump
)
841 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
843 instr
->block
= cursor
.instr
->block
;
844 add_defs_uses(instr
);
845 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
849 if (instr
->type
== nir_instr_type_jump
)
850 nir_handle_add_jump(instr
->block
);
854 src_is_valid(const nir_src
*src
)
856 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
860 remove_use_cb(nir_src
*src
, void *state
)
864 if (src_is_valid(src
))
865 list_del(&src
->use_link
);
871 remove_def_cb(nir_dest
*dest
, void *state
)
876 list_del(&dest
->reg
.def_link
);
882 remove_defs_uses(nir_instr
*instr
)
884 nir_foreach_dest(instr
, remove_def_cb
, instr
);
885 nir_foreach_src(instr
, remove_use_cb
, instr
);
888 void nir_instr_remove_v(nir_instr
*instr
)
890 remove_defs_uses(instr
);
891 exec_node_remove(&instr
->node
);
893 if (instr
->type
== nir_instr_type_jump
) {
894 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
895 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
902 nir_index_local_regs(nir_function_impl
*impl
)
905 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
906 reg
->index
= index
++;
908 impl
->reg_alloc
= index
;
912 nir_index_global_regs(nir_shader
*shader
)
915 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
916 reg
->index
= index
++;
918 shader
->reg_alloc
= index
;
922 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
924 return cb(&instr
->dest
.dest
, state
);
928 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
930 return cb(&instr
->dest
, state
);
934 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
937 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
938 return cb(&instr
->dest
, state
);
944 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
947 return cb(&instr
->dest
, state
);
951 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
953 return cb(&instr
->dest
, state
);
957 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
958 nir_foreach_dest_cb cb
, void *state
)
960 nir_foreach_parallel_copy_entry(entry
, instr
) {
961 if (!cb(&entry
->dest
, state
))
969 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
971 switch (instr
->type
) {
972 case nir_instr_type_alu
:
973 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
974 case nir_instr_type_deref
:
975 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
976 case nir_instr_type_intrinsic
:
977 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
978 case nir_instr_type_tex
:
979 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
980 case nir_instr_type_phi
:
981 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
982 case nir_instr_type_parallel_copy
:
983 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
986 case nir_instr_type_load_const
:
987 case nir_instr_type_ssa_undef
:
988 case nir_instr_type_call
:
989 case nir_instr_type_jump
:
993 unreachable("Invalid instruction type");
1000 struct foreach_ssa_def_state
{
1001 nir_foreach_ssa_def_cb cb
;
1006 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1008 struct foreach_ssa_def_state
*state
= void_state
;
1011 return state
->cb(&dest
->ssa
, state
->client_state
);
1017 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1019 switch (instr
->type
) {
1020 case nir_instr_type_alu
:
1021 case nir_instr_type_deref
:
1022 case nir_instr_type_tex
:
1023 case nir_instr_type_intrinsic
:
1024 case nir_instr_type_phi
:
1025 case nir_instr_type_parallel_copy
: {
1026 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1027 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1030 case nir_instr_type_load_const
:
1031 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1032 case nir_instr_type_ssa_undef
:
1033 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1034 case nir_instr_type_call
:
1035 case nir_instr_type_jump
:
1038 unreachable("Invalid instruction type");
1043 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1045 if (!cb(src
, state
))
1047 if (!src
->is_ssa
&& src
->reg
.indirect
)
1048 return cb(src
->reg
.indirect
, state
);
1053 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1055 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1056 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1063 visit_deref_instr_src(nir_deref_instr
*instr
,
1064 nir_foreach_src_cb cb
, void *state
)
1066 if (instr
->deref_type
!= nir_deref_type_var
) {
1067 if (!visit_src(&instr
->parent
, cb
, state
))
1071 if (instr
->deref_type
== nir_deref_type_array
||
1072 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1073 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1081 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1083 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1084 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1092 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1095 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1096 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1097 if (!visit_src(&instr
->src
[i
], cb
, state
))
1105 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1107 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1108 if (!visit_src(&instr
->params
[i
], cb
, state
))
1116 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1118 nir_foreach_phi_src(src
, instr
) {
1119 if (!visit_src(&src
->src
, cb
, state
))
1127 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1128 nir_foreach_src_cb cb
, void *state
)
1130 nir_foreach_parallel_copy_entry(entry
, instr
) {
1131 if (!visit_src(&entry
->src
, cb
, state
))
1140 nir_foreach_src_cb cb
;
1141 } visit_dest_indirect_state
;
1144 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1146 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1148 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1149 return state
->cb(dest
->reg
.indirect
, state
->state
);
1155 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1157 switch (instr
->type
) {
1158 case nir_instr_type_alu
:
1159 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1162 case nir_instr_type_deref
:
1163 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1166 case nir_instr_type_intrinsic
:
1167 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1170 case nir_instr_type_tex
:
1171 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1174 case nir_instr_type_call
:
1175 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1178 case nir_instr_type_load_const
:
1179 /* Constant load instructions have no regular sources */
1181 case nir_instr_type_phi
:
1182 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1185 case nir_instr_type_parallel_copy
:
1186 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1190 case nir_instr_type_jump
:
1191 case nir_instr_type_ssa_undef
:
1195 unreachable("Invalid instruction type");
1199 visit_dest_indirect_state dest_state
;
1200 dest_state
.state
= state
;
1202 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1206 nir_src_comp_as_int(nir_src src
, unsigned comp
)
1208 assert(nir_src_is_const(src
));
1209 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1211 assert(comp
< load
->def
.num_components
);
1212 switch (load
->def
.bit_size
) {
1213 /* int1_t uses 0/-1 convention */
1214 case 1: return -(int)load
->value
.b
[comp
];
1215 case 8: return load
->value
.i8
[comp
];
1216 case 16: return load
->value
.i16
[comp
];
1217 case 32: return load
->value
.i32
[comp
];
1218 case 64: return load
->value
.i64
[comp
];
1220 unreachable("Invalid bit size");
1225 nir_src_comp_as_uint(nir_src src
, unsigned comp
)
1227 assert(nir_src_is_const(src
));
1228 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1230 assert(comp
< load
->def
.num_components
);
1231 switch (load
->def
.bit_size
) {
1232 case 1: return load
->value
.b
[comp
];
1233 case 8: return load
->value
.u8
[comp
];
1234 case 16: return load
->value
.u16
[comp
];
1235 case 32: return load
->value
.u32
[comp
];
1236 case 64: return load
->value
.u64
[comp
];
1238 unreachable("Invalid bit size");
1243 nir_src_comp_as_bool(nir_src src
, unsigned comp
)
1245 int64_t i
= nir_src_comp_as_int(src
, comp
);
1247 /* Booleans of any size use 0/-1 convention */
1248 assert(i
== 0 || i
== -1);
1254 nir_src_comp_as_float(nir_src src
, unsigned comp
)
1256 assert(nir_src_is_const(src
));
1257 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1259 assert(comp
< load
->def
.num_components
);
1260 switch (load
->def
.bit_size
) {
1261 case 16: return _mesa_half_to_float(load
->value
.u16
[comp
]);
1262 case 32: return load
->value
.f32
[comp
];
1263 case 64: return load
->value
.f64
[comp
];
1265 unreachable("Invalid bit size");
1270 nir_src_as_int(nir_src src
)
1272 assert(nir_src_num_components(src
) == 1);
1273 return nir_src_comp_as_int(src
, 0);
1277 nir_src_as_uint(nir_src src
)
1279 assert(nir_src_num_components(src
) == 1);
1280 return nir_src_comp_as_uint(src
, 0);
1284 nir_src_as_bool(nir_src src
)
1286 assert(nir_src_num_components(src
) == 1);
1287 return nir_src_comp_as_bool(src
, 0);
1291 nir_src_as_float(nir_src src
)
1293 assert(nir_src_num_components(src
) == 1);
1294 return nir_src_comp_as_float(src
, 0);
1298 nir_src_as_const_value(nir_src src
)
1303 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1306 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1308 return &load
->value
;
1312 * Returns true if the source is known to be dynamically uniform. Otherwise it
1313 * returns false which means it may or may not be dynamically uniform but it
1314 * can't be determined.
1317 nir_src_is_dynamically_uniform(nir_src src
)
1322 /* Constants are trivially dynamically uniform */
1323 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1326 /* As are uniform variables */
1327 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1328 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1330 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1334 /* XXX: this could have many more tests, such as when a sampler function is
1335 * called with dynamically uniform arguments.
1341 src_remove_all_uses(nir_src
*src
)
1343 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1344 if (!src_is_valid(src
))
1347 list_del(&src
->use_link
);
1352 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1354 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1355 if (!src_is_valid(src
))
1359 src
->parent_instr
= parent_instr
;
1361 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1363 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1366 src
->parent_if
= parent_if
;
1368 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1370 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1376 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1378 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1380 src_remove_all_uses(src
);
1382 src_add_all_uses(src
, instr
, NULL
);
1386 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1388 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1390 src_remove_all_uses(dest
);
1391 src_remove_all_uses(src
);
1393 *src
= NIR_SRC_INIT
;
1394 src_add_all_uses(dest
, dest_instr
, NULL
);
1398 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1400 nir_src
*src
= &if_stmt
->condition
;
1401 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1403 src_remove_all_uses(src
);
1405 src_add_all_uses(src
, NULL
, if_stmt
);
1409 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1412 /* We can only overwrite an SSA destination if it has no uses. */
1413 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1415 list_del(&dest
->reg
.def_link
);
1416 if (dest
->reg
.indirect
)
1417 src_remove_all_uses(dest
->reg
.indirect
);
1420 /* We can't re-write with an SSA def */
1421 assert(!new_dest
.is_ssa
);
1423 nir_dest_copy(dest
, &new_dest
, instr
);
1425 dest
->reg
.parent_instr
= instr
;
1426 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1428 if (dest
->reg
.indirect
)
1429 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1432 /* note: does *not* take ownership of 'name' */
1434 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1435 unsigned num_components
,
1436 unsigned bit_size
, const char *name
)
1438 def
->name
= ralloc_strdup(instr
, name
);
1439 def
->parent_instr
= instr
;
1440 list_inithead(&def
->uses
);
1441 list_inithead(&def
->if_uses
);
1442 def
->num_components
= num_components
;
1443 def
->bit_size
= bit_size
;
1446 nir_function_impl
*impl
=
1447 nir_cf_node_get_function(&instr
->block
->cf_node
);
1449 def
->index
= impl
->ssa_alloc
++;
1451 def
->index
= UINT_MAX
;
1455 /* note: does *not* take ownership of 'name' */
1457 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1458 unsigned num_components
, unsigned bit_size
,
1461 dest
->is_ssa
= true;
1462 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1466 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1468 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1470 nir_foreach_use_safe(use_src
, def
)
1471 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1473 nir_foreach_if_use_safe(use_src
, def
)
1474 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1478 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1480 assert(start
->block
== end
->block
);
1482 if (between
->block
!= start
->block
)
1485 /* Search backwards looking for "between" */
1486 while (start
!= end
) {
1490 end
= nir_instr_prev(end
);
1497 /* Replaces all uses of the given SSA def with the given source but only if
1498 * the use comes after the after_me instruction. This can be useful if you
1499 * are emitting code to fix up the result of some instruction: you can freely
1500 * use the result in that code and then call rewrite_uses_after and pass the
1501 * last fixup instruction as after_me and it will replace all of the uses you
1502 * want without touching the fixup code.
1504 * This function assumes that after_me is in the same block as
1505 * def->parent_instr and that after_me comes after def->parent_instr.
1508 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1509 nir_instr
*after_me
)
1511 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1513 nir_foreach_use_safe(use_src
, def
) {
1514 assert(use_src
->parent_instr
!= def
->parent_instr
);
1515 /* Since def already dominates all of its uses, the only way a use can
1516 * not be dominated by after_me is if it is between def and after_me in
1517 * the instruction list.
1519 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1520 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1523 nir_foreach_if_use_safe(use_src
, def
)
1524 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1527 nir_component_mask_t
1528 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1530 nir_component_mask_t read_mask
= 0;
1531 nir_foreach_use(use
, def
) {
1532 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1533 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1534 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1535 int src_idx
= alu_src
- &alu
->src
[0];
1536 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1537 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1539 return (1 << def
->num_components
) - 1;
1543 if (!list_empty(&def
->if_uses
))
1550 nir_block_cf_tree_next(nir_block
*block
)
1552 if (block
== NULL
) {
1553 /* nir_foreach_block_safe() will call this function on a NULL block
1554 * after the last iteration, but it won't use the result so just return
1560 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1562 return nir_cf_node_cf_tree_first(cf_next
);
1564 nir_cf_node
*parent
= block
->cf_node
.parent
;
1566 switch (parent
->type
) {
1567 case nir_cf_node_if
: {
1568 /* Are we at the end of the if? Go to the beginning of the else */
1569 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1570 if (block
== nir_if_last_then_block(if_stmt
))
1571 return nir_if_first_else_block(if_stmt
);
1573 assert(block
== nir_if_last_else_block(if_stmt
));
1577 case nir_cf_node_loop
:
1578 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1580 case nir_cf_node_function
:
1584 unreachable("unknown cf node type");
1589 nir_block_cf_tree_prev(nir_block
*block
)
1591 if (block
== NULL
) {
1592 /* do this for consistency with nir_block_cf_tree_next() */
1596 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1598 return nir_cf_node_cf_tree_last(cf_prev
);
1600 nir_cf_node
*parent
= block
->cf_node
.parent
;
1602 switch (parent
->type
) {
1603 case nir_cf_node_if
: {
1604 /* Are we at the beginning of the else? Go to the end of the if */
1605 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1606 if (block
== nir_if_first_else_block(if_stmt
))
1607 return nir_if_last_then_block(if_stmt
);
1609 assert(block
== nir_if_first_then_block(if_stmt
));
1613 case nir_cf_node_loop
:
1614 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1616 case nir_cf_node_function
:
1620 unreachable("unknown cf node type");
1624 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1626 switch (node
->type
) {
1627 case nir_cf_node_function
: {
1628 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1629 return nir_start_block(impl
);
1632 case nir_cf_node_if
: {
1633 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1634 return nir_if_first_then_block(if_stmt
);
1637 case nir_cf_node_loop
: {
1638 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1639 return nir_loop_first_block(loop
);
1642 case nir_cf_node_block
: {
1643 return nir_cf_node_as_block(node
);
1647 unreachable("unknown node type");
1651 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1653 switch (node
->type
) {
1654 case nir_cf_node_function
: {
1655 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1656 return nir_impl_last_block(impl
);
1659 case nir_cf_node_if
: {
1660 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1661 return nir_if_last_else_block(if_stmt
);
1664 case nir_cf_node_loop
: {
1665 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1666 return nir_loop_last_block(loop
);
1669 case nir_cf_node_block
: {
1670 return nir_cf_node_as_block(node
);
1674 unreachable("unknown node type");
1678 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1680 if (node
->type
== nir_cf_node_block
)
1681 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1682 else if (node
->type
== nir_cf_node_function
)
1685 return nir_cf_node_as_block(nir_cf_node_next(node
));
1689 nir_block_get_following_if(nir_block
*block
)
1691 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1694 if (nir_cf_node_is_last(&block
->cf_node
))
1697 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1699 if (next_node
->type
!= nir_cf_node_if
)
1702 return nir_cf_node_as_if(next_node
);
1706 nir_block_get_following_loop(nir_block
*block
)
1708 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1711 if (nir_cf_node_is_last(&block
->cf_node
))
1714 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1716 if (next_node
->type
!= nir_cf_node_loop
)
1719 return nir_cf_node_as_loop(next_node
);
1723 nir_index_blocks(nir_function_impl
*impl
)
1727 if (impl
->valid_metadata
& nir_metadata_block_index
)
1730 nir_foreach_block(block
, impl
) {
1731 block
->index
= index
++;
1734 /* The end_block isn't really part of the program, which is why its index
1737 impl
->num_blocks
= impl
->end_block
->index
= index
;
1741 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1743 unsigned *index
= (unsigned *) state
;
1744 def
->index
= (*index
)++;
1750 * The indices are applied top-to-bottom which has the very nice property
1751 * that, if A dominates B, then A->index <= B->index.
1754 nir_index_ssa_defs(nir_function_impl
*impl
)
1758 nir_foreach_block(block
, impl
) {
1759 nir_foreach_instr(instr
, block
)
1760 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1763 impl
->ssa_alloc
= index
;
1767 * The indices are applied top-to-bottom which has the very nice property
1768 * that, if A dominates B, then A->index <= B->index.
1771 nir_index_instrs(nir_function_impl
*impl
)
1775 nir_foreach_block(block
, impl
) {
1776 nir_foreach_instr(instr
, block
)
1777 instr
->index
= index
++;
1784 nir_intrinsic_from_system_value(gl_system_value val
)
1787 case SYSTEM_VALUE_VERTEX_ID
:
1788 return nir_intrinsic_load_vertex_id
;
1789 case SYSTEM_VALUE_INSTANCE_ID
:
1790 return nir_intrinsic_load_instance_id
;
1791 case SYSTEM_VALUE_DRAW_ID
:
1792 return nir_intrinsic_load_draw_id
;
1793 case SYSTEM_VALUE_BASE_INSTANCE
:
1794 return nir_intrinsic_load_base_instance
;
1795 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1796 return nir_intrinsic_load_vertex_id_zero_base
;
1797 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
1798 return nir_intrinsic_load_is_indexed_draw
;
1799 case SYSTEM_VALUE_FIRST_VERTEX
:
1800 return nir_intrinsic_load_first_vertex
;
1801 case SYSTEM_VALUE_BASE_VERTEX
:
1802 return nir_intrinsic_load_base_vertex
;
1803 case SYSTEM_VALUE_INVOCATION_ID
:
1804 return nir_intrinsic_load_invocation_id
;
1805 case SYSTEM_VALUE_FRAG_COORD
:
1806 return nir_intrinsic_load_frag_coord
;
1807 case SYSTEM_VALUE_FRONT_FACE
:
1808 return nir_intrinsic_load_front_face
;
1809 case SYSTEM_VALUE_SAMPLE_ID
:
1810 return nir_intrinsic_load_sample_id
;
1811 case SYSTEM_VALUE_SAMPLE_POS
:
1812 return nir_intrinsic_load_sample_pos
;
1813 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1814 return nir_intrinsic_load_sample_mask_in
;
1815 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1816 return nir_intrinsic_load_local_invocation_id
;
1817 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
1818 return nir_intrinsic_load_local_invocation_index
;
1819 case SYSTEM_VALUE_WORK_GROUP_ID
:
1820 return nir_intrinsic_load_work_group_id
;
1821 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1822 return nir_intrinsic_load_num_work_groups
;
1823 case SYSTEM_VALUE_PRIMITIVE_ID
:
1824 return nir_intrinsic_load_primitive_id
;
1825 case SYSTEM_VALUE_TESS_COORD
:
1826 return nir_intrinsic_load_tess_coord
;
1827 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1828 return nir_intrinsic_load_tess_level_outer
;
1829 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1830 return nir_intrinsic_load_tess_level_inner
;
1831 case SYSTEM_VALUE_VERTICES_IN
:
1832 return nir_intrinsic_load_patch_vertices_in
;
1833 case SYSTEM_VALUE_HELPER_INVOCATION
:
1834 return nir_intrinsic_load_helper_invocation
;
1835 case SYSTEM_VALUE_VIEW_INDEX
:
1836 return nir_intrinsic_load_view_index
;
1837 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1838 return nir_intrinsic_load_subgroup_size
;
1839 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1840 return nir_intrinsic_load_subgroup_invocation
;
1841 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1842 return nir_intrinsic_load_subgroup_eq_mask
;
1843 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1844 return nir_intrinsic_load_subgroup_ge_mask
;
1845 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1846 return nir_intrinsic_load_subgroup_gt_mask
;
1847 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1848 return nir_intrinsic_load_subgroup_le_mask
;
1849 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1850 return nir_intrinsic_load_subgroup_lt_mask
;
1851 case SYSTEM_VALUE_NUM_SUBGROUPS
:
1852 return nir_intrinsic_load_num_subgroups
;
1853 case SYSTEM_VALUE_SUBGROUP_ID
:
1854 return nir_intrinsic_load_subgroup_id
;
1855 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
1856 return nir_intrinsic_load_local_group_size
;
1857 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
1858 return nir_intrinsic_load_global_invocation_id
;
1859 case SYSTEM_VALUE_WORK_DIM
:
1860 return nir_intrinsic_load_work_dim
;
1862 unreachable("system value does not directly correspond to intrinsic");
1867 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1870 case nir_intrinsic_load_vertex_id
:
1871 return SYSTEM_VALUE_VERTEX_ID
;
1872 case nir_intrinsic_load_instance_id
:
1873 return SYSTEM_VALUE_INSTANCE_ID
;
1874 case nir_intrinsic_load_draw_id
:
1875 return SYSTEM_VALUE_DRAW_ID
;
1876 case nir_intrinsic_load_base_instance
:
1877 return SYSTEM_VALUE_BASE_INSTANCE
;
1878 case nir_intrinsic_load_vertex_id_zero_base
:
1879 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1880 case nir_intrinsic_load_first_vertex
:
1881 return SYSTEM_VALUE_FIRST_VERTEX
;
1882 case nir_intrinsic_load_is_indexed_draw
:
1883 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
1884 case nir_intrinsic_load_base_vertex
:
1885 return SYSTEM_VALUE_BASE_VERTEX
;
1886 case nir_intrinsic_load_invocation_id
:
1887 return SYSTEM_VALUE_INVOCATION_ID
;
1888 case nir_intrinsic_load_frag_coord
:
1889 return SYSTEM_VALUE_FRAG_COORD
;
1890 case nir_intrinsic_load_front_face
:
1891 return SYSTEM_VALUE_FRONT_FACE
;
1892 case nir_intrinsic_load_sample_id
:
1893 return SYSTEM_VALUE_SAMPLE_ID
;
1894 case nir_intrinsic_load_sample_pos
:
1895 return SYSTEM_VALUE_SAMPLE_POS
;
1896 case nir_intrinsic_load_sample_mask_in
:
1897 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1898 case nir_intrinsic_load_local_invocation_id
:
1899 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1900 case nir_intrinsic_load_local_invocation_index
:
1901 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1902 case nir_intrinsic_load_num_work_groups
:
1903 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1904 case nir_intrinsic_load_work_group_id
:
1905 return SYSTEM_VALUE_WORK_GROUP_ID
;
1906 case nir_intrinsic_load_primitive_id
:
1907 return SYSTEM_VALUE_PRIMITIVE_ID
;
1908 case nir_intrinsic_load_tess_coord
:
1909 return SYSTEM_VALUE_TESS_COORD
;
1910 case nir_intrinsic_load_tess_level_outer
:
1911 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1912 case nir_intrinsic_load_tess_level_inner
:
1913 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1914 case nir_intrinsic_load_patch_vertices_in
:
1915 return SYSTEM_VALUE_VERTICES_IN
;
1916 case nir_intrinsic_load_helper_invocation
:
1917 return SYSTEM_VALUE_HELPER_INVOCATION
;
1918 case nir_intrinsic_load_view_index
:
1919 return SYSTEM_VALUE_VIEW_INDEX
;
1920 case nir_intrinsic_load_subgroup_size
:
1921 return SYSTEM_VALUE_SUBGROUP_SIZE
;
1922 case nir_intrinsic_load_subgroup_invocation
:
1923 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1924 case nir_intrinsic_load_subgroup_eq_mask
:
1925 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
1926 case nir_intrinsic_load_subgroup_ge_mask
:
1927 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
1928 case nir_intrinsic_load_subgroup_gt_mask
:
1929 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
1930 case nir_intrinsic_load_subgroup_le_mask
:
1931 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
1932 case nir_intrinsic_load_subgroup_lt_mask
:
1933 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
1934 case nir_intrinsic_load_num_subgroups
:
1935 return SYSTEM_VALUE_NUM_SUBGROUPS
;
1936 case nir_intrinsic_load_subgroup_id
:
1937 return SYSTEM_VALUE_SUBGROUP_ID
;
1938 case nir_intrinsic_load_local_group_size
:
1939 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1940 case nir_intrinsic_load_global_invocation_id
:
1941 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1943 unreachable("intrinsic doesn't produce a system value");
1947 /* OpenGL utility method that remaps the location attributes if they are
1948 * doubles. Not needed for vulkan due the differences on the input location
1949 * count for doubles on vulkan vs OpenGL
1951 * The bitfield returned in dual_slot is one bit for each double input slot in
1952 * the original OpenGL single-slot input numbering. The mapping from old
1953 * locations to new locations is as follows:
1955 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
1958 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
1960 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
1963 nir_foreach_variable(var
, &shader
->inputs
) {
1964 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
1965 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
1966 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
1970 nir_foreach_variable(var
, &shader
->inputs
) {
1971 var
->data
.location
+=
1972 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
1976 /* Returns an attribute mask that has been re-compacted using the given
1980 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
1983 unsigned loc
= u_bit_scan64(&dual_slot
);
1984 /* mask of all bits up to and including loc */
1985 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
1986 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);