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
:
129 assert(!"nir_shader_add_variable cannot be used for local variables");
132 case nir_var_private
:
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
;
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_set_create(block
, _mesa_hash_pointer
,
334 _mesa_key_pointer_equal
);
335 block
->imm_dom
= NULL
;
336 /* XXX maybe it would be worth it to defer allocation? This
337 * way it doesn't get allocated for shader refs that never run
338 * nir_calc_dominance? For example, state-tracker creates an
339 * initial IR, clones that, runs appropriate lowering pass, passes
340 * to driver which does common lowering/opt, and then stores ref
341 * which is later used to do state specific lowering and futher
342 * opt. Do any of the references not need dominance metadata?
344 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
345 _mesa_key_pointer_equal
);
347 exec_list_make_empty(&block
->instr_list
);
353 src_init(nir_src
*src
)
357 src
->reg
.indirect
= NULL
;
358 src
->reg
.base_offset
= 0;
362 nir_if_create(nir_shader
*shader
)
364 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
366 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
367 src_init(&if_stmt
->condition
);
369 nir_block
*then
= nir_block_create(shader
);
370 exec_list_make_empty(&if_stmt
->then_list
);
371 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
372 then
->cf_node
.parent
= &if_stmt
->cf_node
;
374 nir_block
*else_stmt
= nir_block_create(shader
);
375 exec_list_make_empty(&if_stmt
->else_list
);
376 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
377 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
383 nir_loop_create(nir_shader
*shader
)
385 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
387 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
389 nir_block
*body
= nir_block_create(shader
);
390 exec_list_make_empty(&loop
->body
);
391 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
392 body
->cf_node
.parent
= &loop
->cf_node
;
394 body
->successors
[0] = body
;
395 _mesa_set_add(body
->predecessors
, body
);
401 instr_init(nir_instr
*instr
, nir_instr_type type
)
405 exec_node_init(&instr
->node
);
409 dest_init(nir_dest
*dest
)
411 dest
->is_ssa
= false;
412 dest
->reg
.reg
= NULL
;
413 dest
->reg
.indirect
= NULL
;
414 dest
->reg
.base_offset
= 0;
418 alu_dest_init(nir_alu_dest
*dest
)
420 dest_init(&dest
->dest
);
421 dest
->saturate
= false;
422 dest
->write_mask
= 0xf;
426 alu_src_init(nir_alu_src
*src
)
429 src
->abs
= src
->negate
= false;
430 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
435 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
437 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
438 /* TODO: don't use rzalloc */
439 nir_alu_instr
*instr
=
441 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
443 instr_init(&instr
->instr
, nir_instr_type_alu
);
445 alu_dest_init(&instr
->dest
);
446 for (unsigned i
= 0; i
< num_srcs
; i
++)
447 alu_src_init(&instr
->src
[i
]);
453 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
455 nir_deref_instr
*instr
=
456 rzalloc_size(shader
, sizeof(nir_deref_instr
));
458 instr_init(&instr
->instr
, nir_instr_type_deref
);
460 instr
->deref_type
= deref_type
;
461 if (deref_type
!= nir_deref_type_var
)
462 src_init(&instr
->parent
);
464 if (deref_type
== nir_deref_type_array
||
465 deref_type
== nir_deref_type_ptr_as_array
)
466 src_init(&instr
->arr
.index
);
468 dest_init(&instr
->dest
);
474 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
476 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
477 instr_init(&instr
->instr
, nir_instr_type_jump
);
482 nir_load_const_instr
*
483 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
486 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
487 instr_init(&instr
->instr
, nir_instr_type_load_const
);
489 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
494 nir_intrinsic_instr
*
495 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
497 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
498 /* TODO: don't use rzalloc */
499 nir_intrinsic_instr
*instr
=
501 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
503 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
504 instr
->intrinsic
= op
;
506 if (nir_intrinsic_infos
[op
].has_dest
)
507 dest_init(&instr
->dest
);
509 for (unsigned i
= 0; i
< num_srcs
; i
++)
510 src_init(&instr
->src
[i
]);
516 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
518 const unsigned num_params
= callee
->num_params
;
519 nir_call_instr
*instr
=
520 rzalloc_size(shader
, sizeof(*instr
) +
521 num_params
* sizeof(instr
->params
[0]));
523 instr_init(&instr
->instr
, nir_instr_type_call
);
524 instr
->callee
= callee
;
525 instr
->num_params
= num_params
;
526 for (unsigned i
= 0; i
< num_params
; i
++)
527 src_init(&instr
->params
[i
]);
533 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
535 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
536 instr_init(&instr
->instr
, nir_instr_type_tex
);
538 dest_init(&instr
->dest
);
540 instr
->num_srcs
= num_srcs
;
541 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
542 for (unsigned i
= 0; i
< num_srcs
; i
++)
543 src_init(&instr
->src
[i
].src
);
545 instr
->texture_index
= 0;
546 instr
->texture_array_size
= 0;
547 instr
->sampler_index
= 0;
553 nir_tex_instr_add_src(nir_tex_instr
*tex
,
554 nir_tex_src_type src_type
,
557 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
560 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
561 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
562 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
566 ralloc_free(tex
->src
);
569 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
570 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
575 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
577 assert(src_idx
< tex
->num_srcs
);
579 /* First rewrite the source to NIR_SRC_INIT */
580 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
582 /* Now, move all of the other sources down */
583 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
584 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
585 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
591 nir_phi_instr_create(nir_shader
*shader
)
593 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
594 instr_init(&instr
->instr
, nir_instr_type_phi
);
596 dest_init(&instr
->dest
);
597 exec_list_make_empty(&instr
->srcs
);
601 nir_parallel_copy_instr
*
602 nir_parallel_copy_instr_create(nir_shader
*shader
)
604 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
605 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
607 exec_list_make_empty(&instr
->entries
);
612 nir_ssa_undef_instr
*
613 nir_ssa_undef_instr_create(nir_shader
*shader
,
614 unsigned num_components
,
617 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
618 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
620 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
625 static nir_const_value
626 const_value_float(double d
, unsigned bit_size
)
630 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
631 case 32: v
.f32
[0] = d
; break;
632 case 64: v
.f64
[0] = d
; break;
634 unreachable("Invalid bit size");
639 static nir_const_value
640 const_value_int(int64_t i
, unsigned bit_size
)
644 case 1: v
.b
[0] = i
& 1; break;
645 case 8: v
.i8
[0] = i
; break;
646 case 16: v
.i16
[0] = i
; break;
647 case 32: v
.i32
[0] = i
; break;
648 case 64: v
.i64
[0] = i
; break;
650 unreachable("Invalid bit size");
656 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
658 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
659 const int64_t min_int
= -max_int
- 1;
662 return const_value_int(0, bit_size
);
664 return const_value_float(0, bit_size
);
666 return const_value_int(1, bit_size
);
668 return const_value_float(1, bit_size
);
670 return const_value_int(max_int
, bit_size
);
672 return const_value_int(~0ull, bit_size
);
674 return const_value_float(INFINITY
, bit_size
);
676 return const_value_int(min_int
, bit_size
);
678 return const_value_int(0, bit_size
);
680 return const_value_float(-INFINITY
, bit_size
);
682 return const_value_int(~0ull, bit_size
);
684 return const_value_int(0, bit_size
);
686 return const_value_int(0, bit_size
);
688 unreachable("Invalid reduction operation");
693 nir_cf_node_get_function(nir_cf_node
*node
)
695 while (node
->type
!= nir_cf_node_function
) {
699 return nir_cf_node_as_function(node
);
702 /* Reduces a cursor by trying to convert everything to after and trying to
703 * go up to block granularity when possible.
706 reduce_cursor(nir_cursor cursor
)
708 switch (cursor
.option
) {
709 case nir_cursor_before_block
:
710 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
711 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
712 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
713 /* Empty block. After is as good as before. */
714 cursor
.option
= nir_cursor_after_block
;
718 case nir_cursor_after_block
:
721 case nir_cursor_before_instr
: {
722 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
724 /* Before this instruction is after the previous */
725 cursor
.instr
= prev_instr
;
726 cursor
.option
= nir_cursor_after_instr
;
728 /* No previous instruction. Switch to before block */
729 cursor
.block
= cursor
.instr
->block
;
730 cursor
.option
= nir_cursor_before_block
;
732 return reduce_cursor(cursor
);
735 case nir_cursor_after_instr
:
736 if (nir_instr_next(cursor
.instr
) == NULL
) {
737 /* This is the last instruction, switch to after block */
738 cursor
.option
= nir_cursor_after_block
;
739 cursor
.block
= cursor
.instr
->block
;
744 unreachable("Inavlid cursor option");
749 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
751 /* Reduced cursors should be unique */
752 a
= reduce_cursor(a
);
753 b
= reduce_cursor(b
);
755 return a
.block
== b
.block
&& a
.option
== b
.option
;
759 add_use_cb(nir_src
*src
, void *state
)
761 nir_instr
*instr
= state
;
763 src
->parent_instr
= instr
;
764 list_addtail(&src
->use_link
,
765 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
771 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
773 nir_instr
*instr
= state
;
775 if (instr
->block
&& def
->index
== UINT_MAX
) {
776 nir_function_impl
*impl
=
777 nir_cf_node_get_function(&instr
->block
->cf_node
);
779 def
->index
= impl
->ssa_alloc
++;
786 add_reg_def_cb(nir_dest
*dest
, void *state
)
788 nir_instr
*instr
= state
;
791 dest
->reg
.parent_instr
= instr
;
792 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
799 add_defs_uses(nir_instr
*instr
)
801 nir_foreach_src(instr
, add_use_cb
, instr
);
802 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
803 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
807 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
809 switch (cursor
.option
) {
810 case nir_cursor_before_block
:
811 /* Only allow inserting jumps into empty blocks. */
812 if (instr
->type
== nir_instr_type_jump
)
813 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
815 instr
->block
= cursor
.block
;
816 add_defs_uses(instr
);
817 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
819 case nir_cursor_after_block
: {
820 /* Inserting instructions after a jump is illegal. */
821 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
822 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
825 instr
->block
= cursor
.block
;
826 add_defs_uses(instr
);
827 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
830 case nir_cursor_before_instr
:
831 assert(instr
->type
!= nir_instr_type_jump
);
832 instr
->block
= cursor
.instr
->block
;
833 add_defs_uses(instr
);
834 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
836 case nir_cursor_after_instr
:
837 /* Inserting instructions after a jump is illegal. */
838 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
840 /* Only allow inserting jumps at the end of the block. */
841 if (instr
->type
== nir_instr_type_jump
)
842 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
844 instr
->block
= cursor
.instr
->block
;
845 add_defs_uses(instr
);
846 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
850 if (instr
->type
== nir_instr_type_jump
)
851 nir_handle_add_jump(instr
->block
);
855 src_is_valid(const nir_src
*src
)
857 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
861 remove_use_cb(nir_src
*src
, void *state
)
865 if (src_is_valid(src
))
866 list_del(&src
->use_link
);
872 remove_def_cb(nir_dest
*dest
, void *state
)
877 list_del(&dest
->reg
.def_link
);
883 remove_defs_uses(nir_instr
*instr
)
885 nir_foreach_dest(instr
, remove_def_cb
, instr
);
886 nir_foreach_src(instr
, remove_use_cb
, instr
);
889 void nir_instr_remove_v(nir_instr
*instr
)
891 remove_defs_uses(instr
);
892 exec_node_remove(&instr
->node
);
894 if (instr
->type
== nir_instr_type_jump
) {
895 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
896 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
903 nir_index_local_regs(nir_function_impl
*impl
)
906 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
907 reg
->index
= index
++;
909 impl
->reg_alloc
= index
;
913 nir_index_global_regs(nir_shader
*shader
)
916 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
917 reg
->index
= index
++;
919 shader
->reg_alloc
= index
;
923 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
925 return cb(&instr
->dest
.dest
, state
);
929 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
931 return cb(&instr
->dest
, state
);
935 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
938 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
939 return cb(&instr
->dest
, state
);
945 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
948 return cb(&instr
->dest
, state
);
952 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
954 return cb(&instr
->dest
, state
);
958 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
959 nir_foreach_dest_cb cb
, void *state
)
961 nir_foreach_parallel_copy_entry(entry
, instr
) {
962 if (!cb(&entry
->dest
, state
))
970 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
972 switch (instr
->type
) {
973 case nir_instr_type_alu
:
974 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
975 case nir_instr_type_deref
:
976 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
977 case nir_instr_type_intrinsic
:
978 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
979 case nir_instr_type_tex
:
980 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
981 case nir_instr_type_phi
:
982 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
983 case nir_instr_type_parallel_copy
:
984 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
987 case nir_instr_type_load_const
:
988 case nir_instr_type_ssa_undef
:
989 case nir_instr_type_call
:
990 case nir_instr_type_jump
:
994 unreachable("Invalid instruction type");
1001 struct foreach_ssa_def_state
{
1002 nir_foreach_ssa_def_cb cb
;
1007 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1009 struct foreach_ssa_def_state
*state
= void_state
;
1012 return state
->cb(&dest
->ssa
, state
->client_state
);
1018 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1020 switch (instr
->type
) {
1021 case nir_instr_type_alu
:
1022 case nir_instr_type_deref
:
1023 case nir_instr_type_tex
:
1024 case nir_instr_type_intrinsic
:
1025 case nir_instr_type_phi
:
1026 case nir_instr_type_parallel_copy
: {
1027 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1028 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1031 case nir_instr_type_load_const
:
1032 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1033 case nir_instr_type_ssa_undef
:
1034 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1035 case nir_instr_type_call
:
1036 case nir_instr_type_jump
:
1039 unreachable("Invalid instruction type");
1044 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1046 if (!cb(src
, state
))
1048 if (!src
->is_ssa
&& src
->reg
.indirect
)
1049 return cb(src
->reg
.indirect
, state
);
1054 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1056 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1057 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1064 visit_deref_instr_src(nir_deref_instr
*instr
,
1065 nir_foreach_src_cb cb
, void *state
)
1067 if (instr
->deref_type
!= nir_deref_type_var
) {
1068 if (!visit_src(&instr
->parent
, cb
, state
))
1072 if (instr
->deref_type
== nir_deref_type_array
||
1073 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1074 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1082 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1084 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1085 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1093 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1096 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1097 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1098 if (!visit_src(&instr
->src
[i
], cb
, state
))
1106 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1108 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1109 if (!visit_src(&instr
->params
[i
], cb
, state
))
1117 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1119 nir_foreach_phi_src(src
, instr
) {
1120 if (!visit_src(&src
->src
, cb
, state
))
1128 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1129 nir_foreach_src_cb cb
, void *state
)
1131 nir_foreach_parallel_copy_entry(entry
, instr
) {
1132 if (!visit_src(&entry
->src
, cb
, state
))
1141 nir_foreach_src_cb cb
;
1142 } visit_dest_indirect_state
;
1145 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1147 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1149 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1150 return state
->cb(dest
->reg
.indirect
, state
->state
);
1156 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1158 switch (instr
->type
) {
1159 case nir_instr_type_alu
:
1160 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1163 case nir_instr_type_deref
:
1164 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1167 case nir_instr_type_intrinsic
:
1168 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1171 case nir_instr_type_tex
:
1172 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1175 case nir_instr_type_call
:
1176 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1179 case nir_instr_type_load_const
:
1180 /* Constant load instructions have no regular sources */
1182 case nir_instr_type_phi
:
1183 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1186 case nir_instr_type_parallel_copy
:
1187 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1191 case nir_instr_type_jump
:
1192 case nir_instr_type_ssa_undef
:
1196 unreachable("Invalid instruction type");
1200 visit_dest_indirect_state dest_state
;
1201 dest_state
.state
= state
;
1203 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1207 nir_src_comp_as_int(nir_src src
, unsigned comp
)
1209 assert(nir_src_is_const(src
));
1210 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1212 assert(comp
< load
->def
.num_components
);
1213 switch (load
->def
.bit_size
) {
1214 /* int1_t uses 0/-1 convention */
1215 case 1: return -(int)load
->value
.b
[comp
];
1216 case 8: return load
->value
.i8
[comp
];
1217 case 16: return load
->value
.i16
[comp
];
1218 case 32: return load
->value
.i32
[comp
];
1219 case 64: return load
->value
.i64
[comp
];
1221 unreachable("Invalid bit size");
1226 nir_src_comp_as_uint(nir_src src
, unsigned comp
)
1228 assert(nir_src_is_const(src
));
1229 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1231 assert(comp
< load
->def
.num_components
);
1232 switch (load
->def
.bit_size
) {
1233 case 1: return load
->value
.b
[comp
];
1234 case 8: return load
->value
.u8
[comp
];
1235 case 16: return load
->value
.u16
[comp
];
1236 case 32: return load
->value
.u32
[comp
];
1237 case 64: return load
->value
.u64
[comp
];
1239 unreachable("Invalid bit size");
1244 nir_src_comp_as_bool(nir_src src
, unsigned comp
)
1246 int64_t i
= nir_src_comp_as_int(src
, comp
);
1248 /* Booleans of any size use 0/-1 convention */
1249 assert(i
== 0 || i
== -1);
1255 nir_src_comp_as_float(nir_src src
, unsigned comp
)
1257 assert(nir_src_is_const(src
));
1258 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1260 assert(comp
< load
->def
.num_components
);
1261 switch (load
->def
.bit_size
) {
1262 case 16: return _mesa_half_to_float(load
->value
.u16
[comp
]);
1263 case 32: return load
->value
.f32
[comp
];
1264 case 64: return load
->value
.f64
[comp
];
1266 unreachable("Invalid bit size");
1271 nir_src_as_int(nir_src src
)
1273 assert(nir_src_num_components(src
) == 1);
1274 return nir_src_comp_as_int(src
, 0);
1278 nir_src_as_uint(nir_src src
)
1280 assert(nir_src_num_components(src
) == 1);
1281 return nir_src_comp_as_uint(src
, 0);
1285 nir_src_as_bool(nir_src src
)
1287 assert(nir_src_num_components(src
) == 1);
1288 return nir_src_comp_as_bool(src
, 0);
1292 nir_src_as_float(nir_src src
)
1294 assert(nir_src_num_components(src
) == 1);
1295 return nir_src_comp_as_float(src
, 0);
1299 nir_src_as_const_value(nir_src src
)
1304 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1307 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1309 return &load
->value
;
1313 * Returns true if the source is known to be dynamically uniform. Otherwise it
1314 * returns false which means it may or may not be dynamically uniform but it
1315 * can't be determined.
1318 nir_src_is_dynamically_uniform(nir_src src
)
1323 /* Constants are trivially dynamically uniform */
1324 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1327 /* As are uniform variables */
1328 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1329 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1331 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1335 /* XXX: this could have many more tests, such as when a sampler function is
1336 * called with dynamically uniform arguments.
1342 src_remove_all_uses(nir_src
*src
)
1344 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1345 if (!src_is_valid(src
))
1348 list_del(&src
->use_link
);
1353 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1355 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1356 if (!src_is_valid(src
))
1360 src
->parent_instr
= parent_instr
;
1362 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1364 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1367 src
->parent_if
= parent_if
;
1369 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1371 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1377 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1379 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1381 src_remove_all_uses(src
);
1383 src_add_all_uses(src
, instr
, NULL
);
1387 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1389 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1391 src_remove_all_uses(dest
);
1392 src_remove_all_uses(src
);
1394 *src
= NIR_SRC_INIT
;
1395 src_add_all_uses(dest
, dest_instr
, NULL
);
1399 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1401 nir_src
*src
= &if_stmt
->condition
;
1402 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1404 src_remove_all_uses(src
);
1406 src_add_all_uses(src
, NULL
, if_stmt
);
1410 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1413 /* We can only overwrite an SSA destination if it has no uses. */
1414 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1416 list_del(&dest
->reg
.def_link
);
1417 if (dest
->reg
.indirect
)
1418 src_remove_all_uses(dest
->reg
.indirect
);
1421 /* We can't re-write with an SSA def */
1422 assert(!new_dest
.is_ssa
);
1424 nir_dest_copy(dest
, &new_dest
, instr
);
1426 dest
->reg
.parent_instr
= instr
;
1427 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1429 if (dest
->reg
.indirect
)
1430 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1433 /* note: does *not* take ownership of 'name' */
1435 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1436 unsigned num_components
,
1437 unsigned bit_size
, const char *name
)
1439 def
->name
= ralloc_strdup(instr
, name
);
1440 def
->parent_instr
= instr
;
1441 list_inithead(&def
->uses
);
1442 list_inithead(&def
->if_uses
);
1443 def
->num_components
= num_components
;
1444 def
->bit_size
= bit_size
;
1447 nir_function_impl
*impl
=
1448 nir_cf_node_get_function(&instr
->block
->cf_node
);
1450 def
->index
= impl
->ssa_alloc
++;
1452 def
->index
= UINT_MAX
;
1456 /* note: does *not* take ownership of 'name' */
1458 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1459 unsigned num_components
, unsigned bit_size
,
1462 dest
->is_ssa
= true;
1463 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1467 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1469 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1471 nir_foreach_use_safe(use_src
, def
)
1472 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1474 nir_foreach_if_use_safe(use_src
, def
)
1475 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1479 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1481 assert(start
->block
== end
->block
);
1483 if (between
->block
!= start
->block
)
1486 /* Search backwards looking for "between" */
1487 while (start
!= end
) {
1491 end
= nir_instr_prev(end
);
1498 /* Replaces all uses of the given SSA def with the given source but only if
1499 * the use comes after the after_me instruction. This can be useful if you
1500 * are emitting code to fix up the result of some instruction: you can freely
1501 * use the result in that code and then call rewrite_uses_after and pass the
1502 * last fixup instruction as after_me and it will replace all of the uses you
1503 * want without touching the fixup code.
1505 * This function assumes that after_me is in the same block as
1506 * def->parent_instr and that after_me comes after def->parent_instr.
1509 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1510 nir_instr
*after_me
)
1512 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1514 nir_foreach_use_safe(use_src
, def
) {
1515 assert(use_src
->parent_instr
!= def
->parent_instr
);
1516 /* Since def already dominates all of its uses, the only way a use can
1517 * not be dominated by after_me is if it is between def and after_me in
1518 * the instruction list.
1520 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1521 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1524 nir_foreach_if_use_safe(use_src
, def
)
1525 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1528 nir_component_mask_t
1529 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1531 nir_component_mask_t read_mask
= 0;
1532 nir_foreach_use(use
, def
) {
1533 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1534 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1535 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1536 int src_idx
= alu_src
- &alu
->src
[0];
1537 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1538 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1540 return (1 << def
->num_components
) - 1;
1544 if (!list_empty(&def
->if_uses
))
1551 nir_block_cf_tree_next(nir_block
*block
)
1553 if (block
== NULL
) {
1554 /* nir_foreach_block_safe() will call this function on a NULL block
1555 * after the last iteration, but it won't use the result so just return
1561 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1563 return nir_cf_node_cf_tree_first(cf_next
);
1565 nir_cf_node
*parent
= block
->cf_node
.parent
;
1567 switch (parent
->type
) {
1568 case nir_cf_node_if
: {
1569 /* Are we at the end of the if? Go to the beginning of the else */
1570 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1571 if (block
== nir_if_last_then_block(if_stmt
))
1572 return nir_if_first_else_block(if_stmt
);
1574 assert(block
== nir_if_last_else_block(if_stmt
));
1578 case nir_cf_node_loop
:
1579 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1581 case nir_cf_node_function
:
1585 unreachable("unknown cf node type");
1590 nir_block_cf_tree_prev(nir_block
*block
)
1592 if (block
== NULL
) {
1593 /* do this for consistency with nir_block_cf_tree_next() */
1597 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1599 return nir_cf_node_cf_tree_last(cf_prev
);
1601 nir_cf_node
*parent
= block
->cf_node
.parent
;
1603 switch (parent
->type
) {
1604 case nir_cf_node_if
: {
1605 /* Are we at the beginning of the else? Go to the end of the if */
1606 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1607 if (block
== nir_if_first_else_block(if_stmt
))
1608 return nir_if_last_then_block(if_stmt
);
1610 assert(block
== nir_if_first_then_block(if_stmt
));
1614 case nir_cf_node_loop
:
1615 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1617 case nir_cf_node_function
:
1621 unreachable("unknown cf node type");
1625 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1627 switch (node
->type
) {
1628 case nir_cf_node_function
: {
1629 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1630 return nir_start_block(impl
);
1633 case nir_cf_node_if
: {
1634 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1635 return nir_if_first_then_block(if_stmt
);
1638 case nir_cf_node_loop
: {
1639 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1640 return nir_loop_first_block(loop
);
1643 case nir_cf_node_block
: {
1644 return nir_cf_node_as_block(node
);
1648 unreachable("unknown node type");
1652 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1654 switch (node
->type
) {
1655 case nir_cf_node_function
: {
1656 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1657 return nir_impl_last_block(impl
);
1660 case nir_cf_node_if
: {
1661 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1662 return nir_if_last_else_block(if_stmt
);
1665 case nir_cf_node_loop
: {
1666 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1667 return nir_loop_last_block(loop
);
1670 case nir_cf_node_block
: {
1671 return nir_cf_node_as_block(node
);
1675 unreachable("unknown node type");
1679 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1681 if (node
->type
== nir_cf_node_block
)
1682 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1683 else if (node
->type
== nir_cf_node_function
)
1686 return nir_cf_node_as_block(nir_cf_node_next(node
));
1690 nir_block_get_following_if(nir_block
*block
)
1692 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1695 if (nir_cf_node_is_last(&block
->cf_node
))
1698 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1700 if (next_node
->type
!= nir_cf_node_if
)
1703 return nir_cf_node_as_if(next_node
);
1707 nir_block_get_following_loop(nir_block
*block
)
1709 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1712 if (nir_cf_node_is_last(&block
->cf_node
))
1715 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1717 if (next_node
->type
!= nir_cf_node_loop
)
1720 return nir_cf_node_as_loop(next_node
);
1724 nir_index_blocks(nir_function_impl
*impl
)
1728 if (impl
->valid_metadata
& nir_metadata_block_index
)
1731 nir_foreach_block(block
, impl
) {
1732 block
->index
= index
++;
1735 /* The end_block isn't really part of the program, which is why its index
1738 impl
->num_blocks
= impl
->end_block
->index
= index
;
1742 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1744 unsigned *index
= (unsigned *) state
;
1745 def
->index
= (*index
)++;
1751 * The indices are applied top-to-bottom which has the very nice property
1752 * that, if A dominates B, then A->index <= B->index.
1755 nir_index_ssa_defs(nir_function_impl
*impl
)
1759 nir_foreach_block(block
, impl
) {
1760 nir_foreach_instr(instr
, block
)
1761 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1764 impl
->ssa_alloc
= index
;
1768 * The indices are applied top-to-bottom which has the very nice property
1769 * that, if A dominates B, then A->index <= B->index.
1772 nir_index_instrs(nir_function_impl
*impl
)
1776 nir_foreach_block(block
, impl
) {
1777 nir_foreach_instr(instr
, block
)
1778 instr
->index
= index
++;
1785 nir_intrinsic_from_system_value(gl_system_value val
)
1788 case SYSTEM_VALUE_VERTEX_ID
:
1789 return nir_intrinsic_load_vertex_id
;
1790 case SYSTEM_VALUE_INSTANCE_ID
:
1791 return nir_intrinsic_load_instance_id
;
1792 case SYSTEM_VALUE_DRAW_ID
:
1793 return nir_intrinsic_load_draw_id
;
1794 case SYSTEM_VALUE_BASE_INSTANCE
:
1795 return nir_intrinsic_load_base_instance
;
1796 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1797 return nir_intrinsic_load_vertex_id_zero_base
;
1798 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
1799 return nir_intrinsic_load_is_indexed_draw
;
1800 case SYSTEM_VALUE_FIRST_VERTEX
:
1801 return nir_intrinsic_load_first_vertex
;
1802 case SYSTEM_VALUE_BASE_VERTEX
:
1803 return nir_intrinsic_load_base_vertex
;
1804 case SYSTEM_VALUE_INVOCATION_ID
:
1805 return nir_intrinsic_load_invocation_id
;
1806 case SYSTEM_VALUE_FRAG_COORD
:
1807 return nir_intrinsic_load_frag_coord
;
1808 case SYSTEM_VALUE_FRONT_FACE
:
1809 return nir_intrinsic_load_front_face
;
1810 case SYSTEM_VALUE_SAMPLE_ID
:
1811 return nir_intrinsic_load_sample_id
;
1812 case SYSTEM_VALUE_SAMPLE_POS
:
1813 return nir_intrinsic_load_sample_pos
;
1814 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1815 return nir_intrinsic_load_sample_mask_in
;
1816 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1817 return nir_intrinsic_load_local_invocation_id
;
1818 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
1819 return nir_intrinsic_load_local_invocation_index
;
1820 case SYSTEM_VALUE_WORK_GROUP_ID
:
1821 return nir_intrinsic_load_work_group_id
;
1822 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1823 return nir_intrinsic_load_num_work_groups
;
1824 case SYSTEM_VALUE_PRIMITIVE_ID
:
1825 return nir_intrinsic_load_primitive_id
;
1826 case SYSTEM_VALUE_TESS_COORD
:
1827 return nir_intrinsic_load_tess_coord
;
1828 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1829 return nir_intrinsic_load_tess_level_outer
;
1830 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1831 return nir_intrinsic_load_tess_level_inner
;
1832 case SYSTEM_VALUE_VERTICES_IN
:
1833 return nir_intrinsic_load_patch_vertices_in
;
1834 case SYSTEM_VALUE_HELPER_INVOCATION
:
1835 return nir_intrinsic_load_helper_invocation
;
1836 case SYSTEM_VALUE_VIEW_INDEX
:
1837 return nir_intrinsic_load_view_index
;
1838 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1839 return nir_intrinsic_load_subgroup_size
;
1840 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1841 return nir_intrinsic_load_subgroup_invocation
;
1842 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1843 return nir_intrinsic_load_subgroup_eq_mask
;
1844 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1845 return nir_intrinsic_load_subgroup_ge_mask
;
1846 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1847 return nir_intrinsic_load_subgroup_gt_mask
;
1848 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1849 return nir_intrinsic_load_subgroup_le_mask
;
1850 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1851 return nir_intrinsic_load_subgroup_lt_mask
;
1852 case SYSTEM_VALUE_NUM_SUBGROUPS
:
1853 return nir_intrinsic_load_num_subgroups
;
1854 case SYSTEM_VALUE_SUBGROUP_ID
:
1855 return nir_intrinsic_load_subgroup_id
;
1856 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
1857 return nir_intrinsic_load_local_group_size
;
1858 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
1859 return nir_intrinsic_load_global_invocation_id
;
1860 case SYSTEM_VALUE_WORK_DIM
:
1861 return nir_intrinsic_load_work_dim
;
1863 unreachable("system value does not directly correspond to intrinsic");
1868 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1871 case nir_intrinsic_load_vertex_id
:
1872 return SYSTEM_VALUE_VERTEX_ID
;
1873 case nir_intrinsic_load_instance_id
:
1874 return SYSTEM_VALUE_INSTANCE_ID
;
1875 case nir_intrinsic_load_draw_id
:
1876 return SYSTEM_VALUE_DRAW_ID
;
1877 case nir_intrinsic_load_base_instance
:
1878 return SYSTEM_VALUE_BASE_INSTANCE
;
1879 case nir_intrinsic_load_vertex_id_zero_base
:
1880 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1881 case nir_intrinsic_load_first_vertex
:
1882 return SYSTEM_VALUE_FIRST_VERTEX
;
1883 case nir_intrinsic_load_is_indexed_draw
:
1884 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
1885 case nir_intrinsic_load_base_vertex
:
1886 return SYSTEM_VALUE_BASE_VERTEX
;
1887 case nir_intrinsic_load_invocation_id
:
1888 return SYSTEM_VALUE_INVOCATION_ID
;
1889 case nir_intrinsic_load_frag_coord
:
1890 return SYSTEM_VALUE_FRAG_COORD
;
1891 case nir_intrinsic_load_front_face
:
1892 return SYSTEM_VALUE_FRONT_FACE
;
1893 case nir_intrinsic_load_sample_id
:
1894 return SYSTEM_VALUE_SAMPLE_ID
;
1895 case nir_intrinsic_load_sample_pos
:
1896 return SYSTEM_VALUE_SAMPLE_POS
;
1897 case nir_intrinsic_load_sample_mask_in
:
1898 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1899 case nir_intrinsic_load_local_invocation_id
:
1900 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1901 case nir_intrinsic_load_local_invocation_index
:
1902 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1903 case nir_intrinsic_load_num_work_groups
:
1904 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1905 case nir_intrinsic_load_work_group_id
:
1906 return SYSTEM_VALUE_WORK_GROUP_ID
;
1907 case nir_intrinsic_load_primitive_id
:
1908 return SYSTEM_VALUE_PRIMITIVE_ID
;
1909 case nir_intrinsic_load_tess_coord
:
1910 return SYSTEM_VALUE_TESS_COORD
;
1911 case nir_intrinsic_load_tess_level_outer
:
1912 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1913 case nir_intrinsic_load_tess_level_inner
:
1914 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1915 case nir_intrinsic_load_patch_vertices_in
:
1916 return SYSTEM_VALUE_VERTICES_IN
;
1917 case nir_intrinsic_load_helper_invocation
:
1918 return SYSTEM_VALUE_HELPER_INVOCATION
;
1919 case nir_intrinsic_load_view_index
:
1920 return SYSTEM_VALUE_VIEW_INDEX
;
1921 case nir_intrinsic_load_subgroup_size
:
1922 return SYSTEM_VALUE_SUBGROUP_SIZE
;
1923 case nir_intrinsic_load_subgroup_invocation
:
1924 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1925 case nir_intrinsic_load_subgroup_eq_mask
:
1926 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
1927 case nir_intrinsic_load_subgroup_ge_mask
:
1928 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
1929 case nir_intrinsic_load_subgroup_gt_mask
:
1930 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
1931 case nir_intrinsic_load_subgroup_le_mask
:
1932 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
1933 case nir_intrinsic_load_subgroup_lt_mask
:
1934 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
1935 case nir_intrinsic_load_num_subgroups
:
1936 return SYSTEM_VALUE_NUM_SUBGROUPS
;
1937 case nir_intrinsic_load_subgroup_id
:
1938 return SYSTEM_VALUE_SUBGROUP_ID
;
1939 case nir_intrinsic_load_local_group_size
:
1940 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1941 case nir_intrinsic_load_global_invocation_id
:
1942 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1944 unreachable("intrinsic doesn't produce a system value");
1948 /* OpenGL utility method that remaps the location attributes if they are
1949 * doubles. Not needed for vulkan due the differences on the input location
1950 * count for doubles on vulkan vs OpenGL
1952 * The bitfield returned in dual_slot is one bit for each double input slot in
1953 * the original OpenGL single-slot input numbering. The mapping from old
1954 * locations to new locations is as follows:
1956 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
1959 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
1961 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
1964 nir_foreach_variable(var
, &shader
->inputs
) {
1965 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
1966 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
1967 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
1971 nir_foreach_variable(var
, &shader
->inputs
) {
1972 var
->data
.location
+=
1973 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
1977 /* Returns an attribute mask that has been re-compacted using the given
1981 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
1984 unsigned loc
= u_bit_scan64(&dual_slot
);
1985 /* mask of all bits up to and including loc */
1986 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
1987 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);