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
:
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_function_temp
;
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;
211 func
->is_entrypoint
= false;
216 /* NOTE: if the instruction you are copying a src to is already added
217 * to the IR, use nir_instr_rewrite_src() instead.
219 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
221 dest
->is_ssa
= src
->is_ssa
;
223 dest
->ssa
= src
->ssa
;
225 dest
->reg
.base_offset
= src
->reg
.base_offset
;
226 dest
->reg
.reg
= src
->reg
.reg
;
227 if (src
->reg
.indirect
) {
228 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
229 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
231 dest
->reg
.indirect
= NULL
;
236 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
238 /* Copying an SSA definition makes no sense whatsoever. */
239 assert(!src
->is_ssa
);
241 dest
->is_ssa
= false;
243 dest
->reg
.base_offset
= src
->reg
.base_offset
;
244 dest
->reg
.reg
= src
->reg
.reg
;
245 if (src
->reg
.indirect
) {
246 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
247 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
249 dest
->reg
.indirect
= NULL
;
254 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
255 nir_alu_instr
*instr
)
257 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
258 dest
->abs
= src
->abs
;
259 dest
->negate
= src
->negate
;
260 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
261 dest
->swizzle
[i
] = src
->swizzle
[i
];
265 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
266 nir_alu_instr
*instr
)
268 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
269 dest
->write_mask
= src
->write_mask
;
270 dest
->saturate
= src
->saturate
;
275 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
277 exec_node_init(&node
->node
);
283 nir_function_impl_create_bare(nir_shader
*shader
)
285 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
287 impl
->function
= NULL
;
289 cf_init(&impl
->cf_node
, nir_cf_node_function
);
291 exec_list_make_empty(&impl
->body
);
292 exec_list_make_empty(&impl
->registers
);
293 exec_list_make_empty(&impl
->locals
);
296 impl
->valid_metadata
= nir_metadata_none
;
298 /* create start & end blocks */
299 nir_block
*start_block
= nir_block_create(shader
);
300 nir_block
*end_block
= nir_block_create(shader
);
301 start_block
->cf_node
.parent
= &impl
->cf_node
;
302 end_block
->cf_node
.parent
= &impl
->cf_node
;
303 impl
->end_block
= end_block
;
305 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
307 start_block
->successors
[0] = end_block
;
308 _mesa_set_add(end_block
->predecessors
, start_block
);
313 nir_function_impl_create(nir_function
*function
)
315 assert(function
->impl
== NULL
);
317 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
319 function
->impl
= impl
;
320 impl
->function
= function
;
326 nir_block_create(nir_shader
*shader
)
328 nir_block
*block
= rzalloc(shader
, nir_block
);
330 cf_init(&block
->cf_node
, nir_cf_node_block
);
332 block
->successors
[0] = block
->successors
[1] = NULL
;
333 block
->predecessors
= _mesa_pointer_set_create(block
);
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_pointer_set_create(block
);
345 exec_list_make_empty(&block
->instr_list
);
351 src_init(nir_src
*src
)
355 src
->reg
.indirect
= NULL
;
356 src
->reg
.base_offset
= 0;
360 nir_if_create(nir_shader
*shader
)
362 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
364 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
365 src_init(&if_stmt
->condition
);
367 nir_block
*then
= nir_block_create(shader
);
368 exec_list_make_empty(&if_stmt
->then_list
);
369 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
370 then
->cf_node
.parent
= &if_stmt
->cf_node
;
372 nir_block
*else_stmt
= nir_block_create(shader
);
373 exec_list_make_empty(&if_stmt
->else_list
);
374 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
375 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
381 nir_loop_create(nir_shader
*shader
)
383 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
385 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
387 nir_block
*body
= nir_block_create(shader
);
388 exec_list_make_empty(&loop
->body
);
389 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
390 body
->cf_node
.parent
= &loop
->cf_node
;
392 body
->successors
[0] = body
;
393 _mesa_set_add(body
->predecessors
, body
);
399 instr_init(nir_instr
*instr
, nir_instr_type type
)
403 exec_node_init(&instr
->node
);
407 dest_init(nir_dest
*dest
)
409 dest
->is_ssa
= false;
410 dest
->reg
.reg
= NULL
;
411 dest
->reg
.indirect
= NULL
;
412 dest
->reg
.base_offset
= 0;
416 alu_dest_init(nir_alu_dest
*dest
)
418 dest_init(&dest
->dest
);
419 dest
->saturate
= false;
420 dest
->write_mask
= 0xf;
424 alu_src_init(nir_alu_src
*src
)
427 src
->abs
= src
->negate
= false;
428 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
433 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
435 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
436 /* TODO: don't use rzalloc */
437 nir_alu_instr
*instr
=
439 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
441 instr_init(&instr
->instr
, nir_instr_type_alu
);
443 alu_dest_init(&instr
->dest
);
444 for (unsigned i
= 0; i
< num_srcs
; i
++)
445 alu_src_init(&instr
->src
[i
]);
451 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
453 nir_deref_instr
*instr
=
454 rzalloc_size(shader
, sizeof(nir_deref_instr
));
456 instr_init(&instr
->instr
, nir_instr_type_deref
);
458 instr
->deref_type
= deref_type
;
459 if (deref_type
!= nir_deref_type_var
)
460 src_init(&instr
->parent
);
462 if (deref_type
== nir_deref_type_array
||
463 deref_type
== nir_deref_type_ptr_as_array
)
464 src_init(&instr
->arr
.index
);
466 dest_init(&instr
->dest
);
472 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
474 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
475 instr_init(&instr
->instr
, nir_instr_type_jump
);
480 nir_load_const_instr
*
481 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
484 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
485 instr_init(&instr
->instr
, nir_instr_type_load_const
);
487 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
492 nir_intrinsic_instr
*
493 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
495 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
496 /* TODO: don't use rzalloc */
497 nir_intrinsic_instr
*instr
=
499 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
501 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
502 instr
->intrinsic
= op
;
504 if (nir_intrinsic_infos
[op
].has_dest
)
505 dest_init(&instr
->dest
);
507 for (unsigned i
= 0; i
< num_srcs
; i
++)
508 src_init(&instr
->src
[i
]);
514 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
516 const unsigned num_params
= callee
->num_params
;
517 nir_call_instr
*instr
=
518 rzalloc_size(shader
, sizeof(*instr
) +
519 num_params
* sizeof(instr
->params
[0]));
521 instr_init(&instr
->instr
, nir_instr_type_call
);
522 instr
->callee
= callee
;
523 instr
->num_params
= num_params
;
524 for (unsigned i
= 0; i
< num_params
; i
++)
525 src_init(&instr
->params
[i
]);
531 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
533 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
534 instr_init(&instr
->instr
, nir_instr_type_tex
);
536 dest_init(&instr
->dest
);
538 instr
->num_srcs
= num_srcs
;
539 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
540 for (unsigned i
= 0; i
< num_srcs
; i
++)
541 src_init(&instr
->src
[i
].src
);
543 instr
->texture_index
= 0;
544 instr
->texture_array_size
= 0;
545 instr
->sampler_index
= 0;
551 nir_tex_instr_add_src(nir_tex_instr
*tex
,
552 nir_tex_src_type src_type
,
555 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
558 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
559 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
560 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
564 ralloc_free(tex
->src
);
567 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
568 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
573 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
575 assert(src_idx
< tex
->num_srcs
);
577 /* First rewrite the source to NIR_SRC_INIT */
578 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
580 /* Now, move all of the other sources down */
581 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
582 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
583 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
589 nir_phi_instr_create(nir_shader
*shader
)
591 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
592 instr_init(&instr
->instr
, nir_instr_type_phi
);
594 dest_init(&instr
->dest
);
595 exec_list_make_empty(&instr
->srcs
);
599 nir_parallel_copy_instr
*
600 nir_parallel_copy_instr_create(nir_shader
*shader
)
602 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
603 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
605 exec_list_make_empty(&instr
->entries
);
610 nir_ssa_undef_instr
*
611 nir_ssa_undef_instr_create(nir_shader
*shader
,
612 unsigned num_components
,
615 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
616 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
618 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
623 static nir_const_value
624 const_value_float(double d
, unsigned bit_size
)
628 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
629 case 32: v
.f32
[0] = d
; break;
630 case 64: v
.f64
[0] = d
; break;
632 unreachable("Invalid bit size");
637 static nir_const_value
638 const_value_int(int64_t i
, unsigned bit_size
)
642 case 1: v
.b
[0] = i
& 1; break;
643 case 8: v
.i8
[0] = i
; break;
644 case 16: v
.i16
[0] = i
; break;
645 case 32: v
.i32
[0] = i
; break;
646 case 64: v
.i64
[0] = i
; break;
648 unreachable("Invalid bit size");
654 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
656 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
657 const int64_t min_int
= -max_int
- 1;
660 return const_value_int(0, bit_size
);
662 return const_value_float(0, bit_size
);
664 return const_value_int(1, bit_size
);
666 return const_value_float(1, bit_size
);
668 return const_value_int(max_int
, bit_size
);
670 return const_value_int(~0ull, bit_size
);
672 return const_value_float(INFINITY
, bit_size
);
674 return const_value_int(min_int
, bit_size
);
676 return const_value_int(0, bit_size
);
678 return const_value_float(-INFINITY
, bit_size
);
680 return const_value_int(~0ull, bit_size
);
682 return const_value_int(0, bit_size
);
684 return const_value_int(0, bit_size
);
686 unreachable("Invalid reduction operation");
691 nir_cf_node_get_function(nir_cf_node
*node
)
693 while (node
->type
!= nir_cf_node_function
) {
697 return nir_cf_node_as_function(node
);
700 /* Reduces a cursor by trying to convert everything to after and trying to
701 * go up to block granularity when possible.
704 reduce_cursor(nir_cursor cursor
)
706 switch (cursor
.option
) {
707 case nir_cursor_before_block
:
708 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
709 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
710 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
711 /* Empty block. After is as good as before. */
712 cursor
.option
= nir_cursor_after_block
;
716 case nir_cursor_after_block
:
719 case nir_cursor_before_instr
: {
720 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
722 /* Before this instruction is after the previous */
723 cursor
.instr
= prev_instr
;
724 cursor
.option
= nir_cursor_after_instr
;
726 /* No previous instruction. Switch to before block */
727 cursor
.block
= cursor
.instr
->block
;
728 cursor
.option
= nir_cursor_before_block
;
730 return reduce_cursor(cursor
);
733 case nir_cursor_after_instr
:
734 if (nir_instr_next(cursor
.instr
) == NULL
) {
735 /* This is the last instruction, switch to after block */
736 cursor
.option
= nir_cursor_after_block
;
737 cursor
.block
= cursor
.instr
->block
;
742 unreachable("Inavlid cursor option");
747 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
749 /* Reduced cursors should be unique */
750 a
= reduce_cursor(a
);
751 b
= reduce_cursor(b
);
753 return a
.block
== b
.block
&& a
.option
== b
.option
;
757 add_use_cb(nir_src
*src
, void *state
)
759 nir_instr
*instr
= state
;
761 src
->parent_instr
= instr
;
762 list_addtail(&src
->use_link
,
763 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
769 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
771 nir_instr
*instr
= state
;
773 if (instr
->block
&& def
->index
== UINT_MAX
) {
774 nir_function_impl
*impl
=
775 nir_cf_node_get_function(&instr
->block
->cf_node
);
777 def
->index
= impl
->ssa_alloc
++;
784 add_reg_def_cb(nir_dest
*dest
, void *state
)
786 nir_instr
*instr
= state
;
789 dest
->reg
.parent_instr
= instr
;
790 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
797 add_defs_uses(nir_instr
*instr
)
799 nir_foreach_src(instr
, add_use_cb
, instr
);
800 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
801 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
805 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
807 switch (cursor
.option
) {
808 case nir_cursor_before_block
:
809 /* Only allow inserting jumps into empty blocks. */
810 if (instr
->type
== nir_instr_type_jump
)
811 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
813 instr
->block
= cursor
.block
;
814 add_defs_uses(instr
);
815 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
817 case nir_cursor_after_block
: {
818 /* Inserting instructions after a jump is illegal. */
819 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
820 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
823 instr
->block
= cursor
.block
;
824 add_defs_uses(instr
);
825 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
828 case nir_cursor_before_instr
:
829 assert(instr
->type
!= nir_instr_type_jump
);
830 instr
->block
= cursor
.instr
->block
;
831 add_defs_uses(instr
);
832 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
834 case nir_cursor_after_instr
:
835 /* Inserting instructions after a jump is illegal. */
836 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
838 /* Only allow inserting jumps at the end of the block. */
839 if (instr
->type
== nir_instr_type_jump
)
840 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
842 instr
->block
= cursor
.instr
->block
;
843 add_defs_uses(instr
);
844 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
848 if (instr
->type
== nir_instr_type_jump
)
849 nir_handle_add_jump(instr
->block
);
853 src_is_valid(const nir_src
*src
)
855 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
859 remove_use_cb(nir_src
*src
, void *state
)
863 if (src_is_valid(src
))
864 list_del(&src
->use_link
);
870 remove_def_cb(nir_dest
*dest
, void *state
)
875 list_del(&dest
->reg
.def_link
);
881 remove_defs_uses(nir_instr
*instr
)
883 nir_foreach_dest(instr
, remove_def_cb
, instr
);
884 nir_foreach_src(instr
, remove_use_cb
, instr
);
887 void nir_instr_remove_v(nir_instr
*instr
)
889 remove_defs_uses(instr
);
890 exec_node_remove(&instr
->node
);
892 if (instr
->type
== nir_instr_type_jump
) {
893 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
894 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
901 nir_index_local_regs(nir_function_impl
*impl
)
904 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
905 reg
->index
= index
++;
907 impl
->reg_alloc
= index
;
911 nir_index_global_regs(nir_shader
*shader
)
914 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
915 reg
->index
= index
++;
917 shader
->reg_alloc
= index
;
921 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
923 return cb(&instr
->dest
.dest
, state
);
927 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
929 return cb(&instr
->dest
, state
);
933 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
936 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
937 return cb(&instr
->dest
, state
);
943 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
946 return cb(&instr
->dest
, state
);
950 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
952 return cb(&instr
->dest
, state
);
956 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
957 nir_foreach_dest_cb cb
, void *state
)
959 nir_foreach_parallel_copy_entry(entry
, instr
) {
960 if (!cb(&entry
->dest
, state
))
968 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
970 switch (instr
->type
) {
971 case nir_instr_type_alu
:
972 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
973 case nir_instr_type_deref
:
974 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
975 case nir_instr_type_intrinsic
:
976 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
977 case nir_instr_type_tex
:
978 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
979 case nir_instr_type_phi
:
980 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
981 case nir_instr_type_parallel_copy
:
982 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
985 case nir_instr_type_load_const
:
986 case nir_instr_type_ssa_undef
:
987 case nir_instr_type_call
:
988 case nir_instr_type_jump
:
992 unreachable("Invalid instruction type");
999 struct foreach_ssa_def_state
{
1000 nir_foreach_ssa_def_cb cb
;
1005 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1007 struct foreach_ssa_def_state
*state
= void_state
;
1010 return state
->cb(&dest
->ssa
, state
->client_state
);
1016 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1018 switch (instr
->type
) {
1019 case nir_instr_type_alu
:
1020 case nir_instr_type_deref
:
1021 case nir_instr_type_tex
:
1022 case nir_instr_type_intrinsic
:
1023 case nir_instr_type_phi
:
1024 case nir_instr_type_parallel_copy
: {
1025 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1026 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1029 case nir_instr_type_load_const
:
1030 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1031 case nir_instr_type_ssa_undef
:
1032 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1033 case nir_instr_type_call
:
1034 case nir_instr_type_jump
:
1037 unreachable("Invalid instruction type");
1042 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1044 if (!cb(src
, state
))
1046 if (!src
->is_ssa
&& src
->reg
.indirect
)
1047 return cb(src
->reg
.indirect
, state
);
1052 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1054 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1055 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1062 visit_deref_instr_src(nir_deref_instr
*instr
,
1063 nir_foreach_src_cb cb
, void *state
)
1065 if (instr
->deref_type
!= nir_deref_type_var
) {
1066 if (!visit_src(&instr
->parent
, cb
, state
))
1070 if (instr
->deref_type
== nir_deref_type_array
||
1071 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1072 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1080 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1082 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1083 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1091 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1094 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1095 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1096 if (!visit_src(&instr
->src
[i
], cb
, state
))
1104 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1106 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1107 if (!visit_src(&instr
->params
[i
], cb
, state
))
1115 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1117 nir_foreach_phi_src(src
, instr
) {
1118 if (!visit_src(&src
->src
, cb
, state
))
1126 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1127 nir_foreach_src_cb cb
, void *state
)
1129 nir_foreach_parallel_copy_entry(entry
, instr
) {
1130 if (!visit_src(&entry
->src
, cb
, state
))
1139 nir_foreach_src_cb cb
;
1140 } visit_dest_indirect_state
;
1143 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1145 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1147 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1148 return state
->cb(dest
->reg
.indirect
, state
->state
);
1154 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1156 switch (instr
->type
) {
1157 case nir_instr_type_alu
:
1158 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1161 case nir_instr_type_deref
:
1162 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1165 case nir_instr_type_intrinsic
:
1166 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1169 case nir_instr_type_tex
:
1170 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1173 case nir_instr_type_call
:
1174 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1177 case nir_instr_type_load_const
:
1178 /* Constant load instructions have no regular sources */
1180 case nir_instr_type_phi
:
1181 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1184 case nir_instr_type_parallel_copy
:
1185 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1189 case nir_instr_type_jump
:
1190 case nir_instr_type_ssa_undef
:
1194 unreachable("Invalid instruction type");
1198 visit_dest_indirect_state dest_state
;
1199 dest_state
.state
= state
;
1201 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1205 nir_src_comp_as_int(nir_src src
, unsigned comp
)
1207 assert(nir_src_is_const(src
));
1208 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1210 assert(comp
< load
->def
.num_components
);
1211 switch (load
->def
.bit_size
) {
1212 /* int1_t uses 0/-1 convention */
1213 case 1: return -(int)load
->value
.b
[comp
];
1214 case 8: return load
->value
.i8
[comp
];
1215 case 16: return load
->value
.i16
[comp
];
1216 case 32: return load
->value
.i32
[comp
];
1217 case 64: return load
->value
.i64
[comp
];
1219 unreachable("Invalid bit size");
1224 nir_src_comp_as_uint(nir_src src
, unsigned comp
)
1226 assert(nir_src_is_const(src
));
1227 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1229 assert(comp
< load
->def
.num_components
);
1230 switch (load
->def
.bit_size
) {
1231 case 1: return load
->value
.b
[comp
];
1232 case 8: return load
->value
.u8
[comp
];
1233 case 16: return load
->value
.u16
[comp
];
1234 case 32: return load
->value
.u32
[comp
];
1235 case 64: return load
->value
.u64
[comp
];
1237 unreachable("Invalid bit size");
1242 nir_src_comp_as_bool(nir_src src
, unsigned comp
)
1244 int64_t i
= nir_src_comp_as_int(src
, comp
);
1246 /* Booleans of any size use 0/-1 convention */
1247 assert(i
== 0 || i
== -1);
1253 nir_src_comp_as_float(nir_src src
, unsigned comp
)
1255 assert(nir_src_is_const(src
));
1256 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1258 assert(comp
< load
->def
.num_components
);
1259 switch (load
->def
.bit_size
) {
1260 case 16: return _mesa_half_to_float(load
->value
.u16
[comp
]);
1261 case 32: return load
->value
.f32
[comp
];
1262 case 64: return load
->value
.f64
[comp
];
1264 unreachable("Invalid bit size");
1269 nir_src_as_int(nir_src src
)
1271 assert(nir_src_num_components(src
) == 1);
1272 return nir_src_comp_as_int(src
, 0);
1276 nir_src_as_uint(nir_src src
)
1278 assert(nir_src_num_components(src
) == 1);
1279 return nir_src_comp_as_uint(src
, 0);
1283 nir_src_as_bool(nir_src src
)
1285 assert(nir_src_num_components(src
) == 1);
1286 return nir_src_comp_as_bool(src
, 0);
1290 nir_src_as_float(nir_src src
)
1292 assert(nir_src_num_components(src
) == 1);
1293 return nir_src_comp_as_float(src
, 0);
1297 nir_src_as_const_value(nir_src src
)
1302 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1305 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1307 return &load
->value
;
1311 * Returns true if the source is known to be dynamically uniform. Otherwise it
1312 * returns false which means it may or may not be dynamically uniform but it
1313 * can't be determined.
1316 nir_src_is_dynamically_uniform(nir_src src
)
1321 /* Constants are trivially dynamically uniform */
1322 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1325 /* As are uniform variables */
1326 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1327 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1329 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1333 /* XXX: this could have many more tests, such as when a sampler function is
1334 * called with dynamically uniform arguments.
1340 src_remove_all_uses(nir_src
*src
)
1342 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1343 if (!src_is_valid(src
))
1346 list_del(&src
->use_link
);
1351 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1353 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1354 if (!src_is_valid(src
))
1358 src
->parent_instr
= parent_instr
;
1360 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1362 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1365 src
->parent_if
= parent_if
;
1367 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1369 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1375 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1377 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1379 src_remove_all_uses(src
);
1381 src_add_all_uses(src
, instr
, NULL
);
1385 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1387 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1389 src_remove_all_uses(dest
);
1390 src_remove_all_uses(src
);
1392 *src
= NIR_SRC_INIT
;
1393 src_add_all_uses(dest
, dest_instr
, NULL
);
1397 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1399 nir_src
*src
= &if_stmt
->condition
;
1400 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1402 src_remove_all_uses(src
);
1404 src_add_all_uses(src
, NULL
, if_stmt
);
1408 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1411 /* We can only overwrite an SSA destination if it has no uses. */
1412 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1414 list_del(&dest
->reg
.def_link
);
1415 if (dest
->reg
.indirect
)
1416 src_remove_all_uses(dest
->reg
.indirect
);
1419 /* We can't re-write with an SSA def */
1420 assert(!new_dest
.is_ssa
);
1422 nir_dest_copy(dest
, &new_dest
, instr
);
1424 dest
->reg
.parent_instr
= instr
;
1425 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1427 if (dest
->reg
.indirect
)
1428 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1431 /* note: does *not* take ownership of 'name' */
1433 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1434 unsigned num_components
,
1435 unsigned bit_size
, const char *name
)
1437 def
->name
= ralloc_strdup(instr
, name
);
1438 def
->parent_instr
= instr
;
1439 list_inithead(&def
->uses
);
1440 list_inithead(&def
->if_uses
);
1441 def
->num_components
= num_components
;
1442 def
->bit_size
= bit_size
;
1445 nir_function_impl
*impl
=
1446 nir_cf_node_get_function(&instr
->block
->cf_node
);
1448 def
->index
= impl
->ssa_alloc
++;
1450 def
->index
= UINT_MAX
;
1454 /* note: does *not* take ownership of 'name' */
1456 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1457 unsigned num_components
, unsigned bit_size
,
1460 dest
->is_ssa
= true;
1461 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1465 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1467 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1469 nir_foreach_use_safe(use_src
, def
)
1470 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1472 nir_foreach_if_use_safe(use_src
, def
)
1473 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1477 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1479 assert(start
->block
== end
->block
);
1481 if (between
->block
!= start
->block
)
1484 /* Search backwards looking for "between" */
1485 while (start
!= end
) {
1489 end
= nir_instr_prev(end
);
1496 /* Replaces all uses of the given SSA def with the given source but only if
1497 * the use comes after the after_me instruction. This can be useful if you
1498 * are emitting code to fix up the result of some instruction: you can freely
1499 * use the result in that code and then call rewrite_uses_after and pass the
1500 * last fixup instruction as after_me and it will replace all of the uses you
1501 * want without touching the fixup code.
1503 * This function assumes that after_me is in the same block as
1504 * def->parent_instr and that after_me comes after def->parent_instr.
1507 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1508 nir_instr
*after_me
)
1510 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1512 nir_foreach_use_safe(use_src
, def
) {
1513 assert(use_src
->parent_instr
!= def
->parent_instr
);
1514 /* Since def already dominates all of its uses, the only way a use can
1515 * not be dominated by after_me is if it is between def and after_me in
1516 * the instruction list.
1518 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1519 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1522 nir_foreach_if_use_safe(use_src
, def
)
1523 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1526 nir_component_mask_t
1527 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1529 nir_component_mask_t read_mask
= 0;
1530 nir_foreach_use(use
, def
) {
1531 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1532 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1533 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1534 int src_idx
= alu_src
- &alu
->src
[0];
1535 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1536 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1538 return (1 << def
->num_components
) - 1;
1542 if (!list_empty(&def
->if_uses
))
1549 nir_block_cf_tree_next(nir_block
*block
)
1551 if (block
== NULL
) {
1552 /* nir_foreach_block_safe() will call this function on a NULL block
1553 * after the last iteration, but it won't use the result so just return
1559 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1561 return nir_cf_node_cf_tree_first(cf_next
);
1563 nir_cf_node
*parent
= block
->cf_node
.parent
;
1565 switch (parent
->type
) {
1566 case nir_cf_node_if
: {
1567 /* Are we at the end of the if? Go to the beginning of the else */
1568 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1569 if (block
== nir_if_last_then_block(if_stmt
))
1570 return nir_if_first_else_block(if_stmt
);
1572 assert(block
== nir_if_last_else_block(if_stmt
));
1576 case nir_cf_node_loop
:
1577 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1579 case nir_cf_node_function
:
1583 unreachable("unknown cf node type");
1588 nir_block_cf_tree_prev(nir_block
*block
)
1590 if (block
== NULL
) {
1591 /* do this for consistency with nir_block_cf_tree_next() */
1595 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1597 return nir_cf_node_cf_tree_last(cf_prev
);
1599 nir_cf_node
*parent
= block
->cf_node
.parent
;
1601 switch (parent
->type
) {
1602 case nir_cf_node_if
: {
1603 /* Are we at the beginning of the else? Go to the end of the if */
1604 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1605 if (block
== nir_if_first_else_block(if_stmt
))
1606 return nir_if_last_then_block(if_stmt
);
1608 assert(block
== nir_if_first_then_block(if_stmt
));
1612 case nir_cf_node_loop
:
1613 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1615 case nir_cf_node_function
:
1619 unreachable("unknown cf node type");
1623 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1625 switch (node
->type
) {
1626 case nir_cf_node_function
: {
1627 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1628 return nir_start_block(impl
);
1631 case nir_cf_node_if
: {
1632 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1633 return nir_if_first_then_block(if_stmt
);
1636 case nir_cf_node_loop
: {
1637 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1638 return nir_loop_first_block(loop
);
1641 case nir_cf_node_block
: {
1642 return nir_cf_node_as_block(node
);
1646 unreachable("unknown node type");
1650 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1652 switch (node
->type
) {
1653 case nir_cf_node_function
: {
1654 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1655 return nir_impl_last_block(impl
);
1658 case nir_cf_node_if
: {
1659 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1660 return nir_if_last_else_block(if_stmt
);
1663 case nir_cf_node_loop
: {
1664 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1665 return nir_loop_last_block(loop
);
1668 case nir_cf_node_block
: {
1669 return nir_cf_node_as_block(node
);
1673 unreachable("unknown node type");
1677 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1679 if (node
->type
== nir_cf_node_block
)
1680 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1681 else if (node
->type
== nir_cf_node_function
)
1684 return nir_cf_node_as_block(nir_cf_node_next(node
));
1688 nir_block_get_following_if(nir_block
*block
)
1690 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1693 if (nir_cf_node_is_last(&block
->cf_node
))
1696 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1698 if (next_node
->type
!= nir_cf_node_if
)
1701 return nir_cf_node_as_if(next_node
);
1705 nir_block_get_following_loop(nir_block
*block
)
1707 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1710 if (nir_cf_node_is_last(&block
->cf_node
))
1713 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1715 if (next_node
->type
!= nir_cf_node_loop
)
1718 return nir_cf_node_as_loop(next_node
);
1722 nir_index_blocks(nir_function_impl
*impl
)
1726 if (impl
->valid_metadata
& nir_metadata_block_index
)
1729 nir_foreach_block(block
, impl
) {
1730 block
->index
= index
++;
1733 /* The end_block isn't really part of the program, which is why its index
1736 impl
->num_blocks
= impl
->end_block
->index
= index
;
1740 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1742 unsigned *index
= (unsigned *) state
;
1743 def
->index
= (*index
)++;
1749 * The indices are applied top-to-bottom which has the very nice property
1750 * that, if A dominates B, then A->index <= B->index.
1753 nir_index_ssa_defs(nir_function_impl
*impl
)
1757 nir_foreach_block(block
, impl
) {
1758 nir_foreach_instr(instr
, block
)
1759 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1762 impl
->ssa_alloc
= index
;
1766 * The indices are applied top-to-bottom which has the very nice property
1767 * that, if A dominates B, then A->index <= B->index.
1770 nir_index_instrs(nir_function_impl
*impl
)
1774 nir_foreach_block(block
, impl
) {
1775 nir_foreach_instr(instr
, block
)
1776 instr
->index
= index
++;
1783 nir_intrinsic_from_system_value(gl_system_value val
)
1786 case SYSTEM_VALUE_VERTEX_ID
:
1787 return nir_intrinsic_load_vertex_id
;
1788 case SYSTEM_VALUE_INSTANCE_ID
:
1789 return nir_intrinsic_load_instance_id
;
1790 case SYSTEM_VALUE_DRAW_ID
:
1791 return nir_intrinsic_load_draw_id
;
1792 case SYSTEM_VALUE_BASE_INSTANCE
:
1793 return nir_intrinsic_load_base_instance
;
1794 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1795 return nir_intrinsic_load_vertex_id_zero_base
;
1796 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
1797 return nir_intrinsic_load_is_indexed_draw
;
1798 case SYSTEM_VALUE_FIRST_VERTEX
:
1799 return nir_intrinsic_load_first_vertex
;
1800 case SYSTEM_VALUE_BASE_VERTEX
:
1801 return nir_intrinsic_load_base_vertex
;
1802 case SYSTEM_VALUE_INVOCATION_ID
:
1803 return nir_intrinsic_load_invocation_id
;
1804 case SYSTEM_VALUE_FRAG_COORD
:
1805 return nir_intrinsic_load_frag_coord
;
1806 case SYSTEM_VALUE_FRONT_FACE
:
1807 return nir_intrinsic_load_front_face
;
1808 case SYSTEM_VALUE_SAMPLE_ID
:
1809 return nir_intrinsic_load_sample_id
;
1810 case SYSTEM_VALUE_SAMPLE_POS
:
1811 return nir_intrinsic_load_sample_pos
;
1812 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1813 return nir_intrinsic_load_sample_mask_in
;
1814 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1815 return nir_intrinsic_load_local_invocation_id
;
1816 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
1817 return nir_intrinsic_load_local_invocation_index
;
1818 case SYSTEM_VALUE_WORK_GROUP_ID
:
1819 return nir_intrinsic_load_work_group_id
;
1820 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1821 return nir_intrinsic_load_num_work_groups
;
1822 case SYSTEM_VALUE_PRIMITIVE_ID
:
1823 return nir_intrinsic_load_primitive_id
;
1824 case SYSTEM_VALUE_TESS_COORD
:
1825 return nir_intrinsic_load_tess_coord
;
1826 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1827 return nir_intrinsic_load_tess_level_outer
;
1828 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1829 return nir_intrinsic_load_tess_level_inner
;
1830 case SYSTEM_VALUE_VERTICES_IN
:
1831 return nir_intrinsic_load_patch_vertices_in
;
1832 case SYSTEM_VALUE_HELPER_INVOCATION
:
1833 return nir_intrinsic_load_helper_invocation
;
1834 case SYSTEM_VALUE_VIEW_INDEX
:
1835 return nir_intrinsic_load_view_index
;
1836 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1837 return nir_intrinsic_load_subgroup_size
;
1838 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1839 return nir_intrinsic_load_subgroup_invocation
;
1840 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1841 return nir_intrinsic_load_subgroup_eq_mask
;
1842 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1843 return nir_intrinsic_load_subgroup_ge_mask
;
1844 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1845 return nir_intrinsic_load_subgroup_gt_mask
;
1846 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1847 return nir_intrinsic_load_subgroup_le_mask
;
1848 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1849 return nir_intrinsic_load_subgroup_lt_mask
;
1850 case SYSTEM_VALUE_NUM_SUBGROUPS
:
1851 return nir_intrinsic_load_num_subgroups
;
1852 case SYSTEM_VALUE_SUBGROUP_ID
:
1853 return nir_intrinsic_load_subgroup_id
;
1854 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
1855 return nir_intrinsic_load_local_group_size
;
1856 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
1857 return nir_intrinsic_load_global_invocation_id
;
1858 case SYSTEM_VALUE_WORK_DIM
:
1859 return nir_intrinsic_load_work_dim
;
1861 unreachable("system value does not directly correspond to intrinsic");
1866 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1869 case nir_intrinsic_load_vertex_id
:
1870 return SYSTEM_VALUE_VERTEX_ID
;
1871 case nir_intrinsic_load_instance_id
:
1872 return SYSTEM_VALUE_INSTANCE_ID
;
1873 case nir_intrinsic_load_draw_id
:
1874 return SYSTEM_VALUE_DRAW_ID
;
1875 case nir_intrinsic_load_base_instance
:
1876 return SYSTEM_VALUE_BASE_INSTANCE
;
1877 case nir_intrinsic_load_vertex_id_zero_base
:
1878 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1879 case nir_intrinsic_load_first_vertex
:
1880 return SYSTEM_VALUE_FIRST_VERTEX
;
1881 case nir_intrinsic_load_is_indexed_draw
:
1882 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
1883 case nir_intrinsic_load_base_vertex
:
1884 return SYSTEM_VALUE_BASE_VERTEX
;
1885 case nir_intrinsic_load_invocation_id
:
1886 return SYSTEM_VALUE_INVOCATION_ID
;
1887 case nir_intrinsic_load_frag_coord
:
1888 return SYSTEM_VALUE_FRAG_COORD
;
1889 case nir_intrinsic_load_front_face
:
1890 return SYSTEM_VALUE_FRONT_FACE
;
1891 case nir_intrinsic_load_sample_id
:
1892 return SYSTEM_VALUE_SAMPLE_ID
;
1893 case nir_intrinsic_load_sample_pos
:
1894 return SYSTEM_VALUE_SAMPLE_POS
;
1895 case nir_intrinsic_load_sample_mask_in
:
1896 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1897 case nir_intrinsic_load_local_invocation_id
:
1898 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1899 case nir_intrinsic_load_local_invocation_index
:
1900 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1901 case nir_intrinsic_load_num_work_groups
:
1902 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1903 case nir_intrinsic_load_work_group_id
:
1904 return SYSTEM_VALUE_WORK_GROUP_ID
;
1905 case nir_intrinsic_load_primitive_id
:
1906 return SYSTEM_VALUE_PRIMITIVE_ID
;
1907 case nir_intrinsic_load_tess_coord
:
1908 return SYSTEM_VALUE_TESS_COORD
;
1909 case nir_intrinsic_load_tess_level_outer
:
1910 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1911 case nir_intrinsic_load_tess_level_inner
:
1912 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1913 case nir_intrinsic_load_patch_vertices_in
:
1914 return SYSTEM_VALUE_VERTICES_IN
;
1915 case nir_intrinsic_load_helper_invocation
:
1916 return SYSTEM_VALUE_HELPER_INVOCATION
;
1917 case nir_intrinsic_load_view_index
:
1918 return SYSTEM_VALUE_VIEW_INDEX
;
1919 case nir_intrinsic_load_subgroup_size
:
1920 return SYSTEM_VALUE_SUBGROUP_SIZE
;
1921 case nir_intrinsic_load_subgroup_invocation
:
1922 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1923 case nir_intrinsic_load_subgroup_eq_mask
:
1924 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
1925 case nir_intrinsic_load_subgroup_ge_mask
:
1926 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
1927 case nir_intrinsic_load_subgroup_gt_mask
:
1928 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
1929 case nir_intrinsic_load_subgroup_le_mask
:
1930 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
1931 case nir_intrinsic_load_subgroup_lt_mask
:
1932 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
1933 case nir_intrinsic_load_num_subgroups
:
1934 return SYSTEM_VALUE_NUM_SUBGROUPS
;
1935 case nir_intrinsic_load_subgroup_id
:
1936 return SYSTEM_VALUE_SUBGROUP_ID
;
1937 case nir_intrinsic_load_local_group_size
:
1938 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1939 case nir_intrinsic_load_global_invocation_id
:
1940 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1942 unreachable("intrinsic doesn't produce a system value");
1946 /* OpenGL utility method that remaps the location attributes if they are
1947 * doubles. Not needed for vulkan due the differences on the input location
1948 * count for doubles on vulkan vs OpenGL
1950 * The bitfield returned in dual_slot is one bit for each double input slot in
1951 * the original OpenGL single-slot input numbering. The mapping from old
1952 * locations to new locations is as follows:
1954 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
1957 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
1959 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
1962 nir_foreach_variable(var
, &shader
->inputs
) {
1963 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
1964 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
1965 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
1969 nir_foreach_variable(var
, &shader
->inputs
) {
1970 var
->data
.location
+=
1971 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
1975 /* Returns an attribute mask that has been re-compacted using the given
1979 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
1982 unsigned loc
= u_bit_scan64(&dual_slot
);
1983 /* mask of all bits up to and including loc */
1984 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
1985 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);