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
->globals
);
62 exec_list_make_empty(&shader
->system_values
);
64 shader
->num_inputs
= 0;
65 shader
->num_outputs
= 0;
66 shader
->num_uniforms
= 0;
67 shader
->num_shared
= 0;
73 reg_create(void *mem_ctx
, struct exec_list
*list
)
75 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
77 list_inithead(®
->uses
);
78 list_inithead(®
->defs
);
79 list_inithead(®
->if_uses
);
81 reg
->num_components
= 0;
83 reg
->num_array_elems
= 0;
86 exec_list_push_tail(list
, ®
->node
);
92 nir_local_reg_create(nir_function_impl
*impl
)
94 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
95 reg
->index
= impl
->reg_alloc
++;
101 nir_reg_remove(nir_register
*reg
)
103 exec_node_remove(®
->node
);
107 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
109 switch (var
->data
.mode
) {
111 assert(!"invalid mode");
114 case nir_var_function_temp
:
115 assert(!"nir_shader_add_variable cannot be used for local variables");
118 case nir_var_shader_temp
:
119 exec_list_push_tail(&shader
->globals
, &var
->node
);
122 case nir_var_shader_in
:
123 exec_list_push_tail(&shader
->inputs
, &var
->node
);
126 case nir_var_shader_out
:
127 exec_list_push_tail(&shader
->outputs
, &var
->node
);
130 case nir_var_uniform
:
131 case nir_var_mem_ubo
:
132 case nir_var_mem_ssbo
:
133 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
136 case nir_var_mem_shared
:
137 assert(gl_shader_stage_is_compute(shader
->info
.stage
));
138 exec_list_push_tail(&shader
->shared
, &var
->node
);
141 case nir_var_mem_global
:
142 assert(!"nir_shader_add_variable cannot be used for global memory");
145 case nir_var_system_value
:
146 exec_list_push_tail(&shader
->system_values
, &var
->node
);
152 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
153 const struct glsl_type
*type
, const char *name
)
155 nir_variable
*var
= rzalloc(shader
, nir_variable
);
156 var
->name
= ralloc_strdup(var
, name
);
158 var
->data
.mode
= mode
;
159 var
->data
.how_declared
= nir_var_declared_normally
;
161 if ((mode
== nir_var_shader_in
&&
162 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
163 (mode
== nir_var_shader_out
&&
164 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
165 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
167 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
168 var
->data
.read_only
= true;
170 nir_shader_add_variable(shader
, var
);
176 nir_local_variable_create(nir_function_impl
*impl
,
177 const struct glsl_type
*type
, const char *name
)
179 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
180 var
->name
= ralloc_strdup(var
, name
);
182 var
->data
.mode
= nir_var_function_temp
;
184 nir_function_impl_add_variable(impl
, var
);
190 nir_function_create(nir_shader
*shader
, const char *name
)
192 nir_function
*func
= ralloc(shader
, nir_function
);
194 exec_list_push_tail(&shader
->functions
, &func
->node
);
196 func
->name
= ralloc_strdup(func
, name
);
197 func
->shader
= shader
;
198 func
->num_params
= 0;
201 func
->is_entrypoint
= false;
206 /* NOTE: if the instruction you are copying a src to is already added
207 * to the IR, use nir_instr_rewrite_src() instead.
209 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
211 dest
->is_ssa
= src
->is_ssa
;
213 dest
->ssa
= src
->ssa
;
215 dest
->reg
.base_offset
= src
->reg
.base_offset
;
216 dest
->reg
.reg
= src
->reg
.reg
;
217 if (src
->reg
.indirect
) {
218 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
219 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
221 dest
->reg
.indirect
= NULL
;
226 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
228 /* Copying an SSA definition makes no sense whatsoever. */
229 assert(!src
->is_ssa
);
231 dest
->is_ssa
= false;
233 dest
->reg
.base_offset
= src
->reg
.base_offset
;
234 dest
->reg
.reg
= src
->reg
.reg
;
235 if (src
->reg
.indirect
) {
236 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
237 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
239 dest
->reg
.indirect
= NULL
;
244 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
245 nir_alu_instr
*instr
)
247 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
248 dest
->abs
= src
->abs
;
249 dest
->negate
= src
->negate
;
250 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
251 dest
->swizzle
[i
] = src
->swizzle
[i
];
255 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
256 nir_alu_instr
*instr
)
258 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
259 dest
->write_mask
= src
->write_mask
;
260 dest
->saturate
= src
->saturate
;
265 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
267 exec_node_init(&node
->node
);
273 nir_function_impl_create_bare(nir_shader
*shader
)
275 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
277 impl
->function
= NULL
;
279 cf_init(&impl
->cf_node
, nir_cf_node_function
);
281 exec_list_make_empty(&impl
->body
);
282 exec_list_make_empty(&impl
->registers
);
283 exec_list_make_empty(&impl
->locals
);
286 impl
->valid_metadata
= nir_metadata_none
;
288 /* create start & end blocks */
289 nir_block
*start_block
= nir_block_create(shader
);
290 nir_block
*end_block
= nir_block_create(shader
);
291 start_block
->cf_node
.parent
= &impl
->cf_node
;
292 end_block
->cf_node
.parent
= &impl
->cf_node
;
293 impl
->end_block
= end_block
;
295 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
297 start_block
->successors
[0] = end_block
;
298 _mesa_set_add(end_block
->predecessors
, start_block
);
303 nir_function_impl_create(nir_function
*function
)
305 assert(function
->impl
== NULL
);
307 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
309 function
->impl
= impl
;
310 impl
->function
= function
;
316 nir_block_create(nir_shader
*shader
)
318 nir_block
*block
= rzalloc(shader
, nir_block
);
320 cf_init(&block
->cf_node
, nir_cf_node_block
);
322 block
->successors
[0] = block
->successors
[1] = NULL
;
323 block
->predecessors
= _mesa_pointer_set_create(block
);
324 block
->imm_dom
= NULL
;
325 /* XXX maybe it would be worth it to defer allocation? This
326 * way it doesn't get allocated for shader refs that never run
327 * nir_calc_dominance? For example, state-tracker creates an
328 * initial IR, clones that, runs appropriate lowering pass, passes
329 * to driver which does common lowering/opt, and then stores ref
330 * which is later used to do state specific lowering and futher
331 * opt. Do any of the references not need dominance metadata?
333 block
->dom_frontier
= _mesa_pointer_set_create(block
);
335 exec_list_make_empty(&block
->instr_list
);
341 src_init(nir_src
*src
)
345 src
->reg
.indirect
= NULL
;
346 src
->reg
.base_offset
= 0;
350 nir_if_create(nir_shader
*shader
)
352 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
354 if_stmt
->control
= nir_selection_control_none
;
356 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
357 src_init(&if_stmt
->condition
);
359 nir_block
*then
= nir_block_create(shader
);
360 exec_list_make_empty(&if_stmt
->then_list
);
361 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
362 then
->cf_node
.parent
= &if_stmt
->cf_node
;
364 nir_block
*else_stmt
= nir_block_create(shader
);
365 exec_list_make_empty(&if_stmt
->else_list
);
366 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
367 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
373 nir_loop_create(nir_shader
*shader
)
375 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
377 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
379 nir_block
*body
= nir_block_create(shader
);
380 exec_list_make_empty(&loop
->body
);
381 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
382 body
->cf_node
.parent
= &loop
->cf_node
;
384 body
->successors
[0] = body
;
385 _mesa_set_add(body
->predecessors
, body
);
391 instr_init(nir_instr
*instr
, nir_instr_type type
)
395 exec_node_init(&instr
->node
);
399 dest_init(nir_dest
*dest
)
401 dest
->is_ssa
= false;
402 dest
->reg
.reg
= NULL
;
403 dest
->reg
.indirect
= NULL
;
404 dest
->reg
.base_offset
= 0;
408 alu_dest_init(nir_alu_dest
*dest
)
410 dest_init(&dest
->dest
);
411 dest
->saturate
= false;
412 dest
->write_mask
= 0xf;
416 alu_src_init(nir_alu_src
*src
)
419 src
->abs
= src
->negate
= false;
420 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
425 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
427 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
428 /* TODO: don't use rzalloc */
429 nir_alu_instr
*instr
=
431 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
433 instr_init(&instr
->instr
, nir_instr_type_alu
);
435 alu_dest_init(&instr
->dest
);
436 for (unsigned i
= 0; i
< num_srcs
; i
++)
437 alu_src_init(&instr
->src
[i
]);
443 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
445 nir_deref_instr
*instr
=
446 rzalloc_size(shader
, sizeof(nir_deref_instr
));
448 instr_init(&instr
->instr
, nir_instr_type_deref
);
450 instr
->deref_type
= deref_type
;
451 if (deref_type
!= nir_deref_type_var
)
452 src_init(&instr
->parent
);
454 if (deref_type
== nir_deref_type_array
||
455 deref_type
== nir_deref_type_ptr_as_array
)
456 src_init(&instr
->arr
.index
);
458 dest_init(&instr
->dest
);
464 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
466 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
467 instr_init(&instr
->instr
, nir_instr_type_jump
);
472 nir_load_const_instr
*
473 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
476 nir_load_const_instr
*instr
=
477 rzalloc_size(shader
, sizeof(*instr
) + num_components
* sizeof(*instr
->value
));
478 instr_init(&instr
->instr
, nir_instr_type_load_const
);
480 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
485 nir_intrinsic_instr
*
486 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
488 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
489 /* TODO: don't use rzalloc */
490 nir_intrinsic_instr
*instr
=
492 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
494 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
495 instr
->intrinsic
= op
;
497 if (nir_intrinsic_infos
[op
].has_dest
)
498 dest_init(&instr
->dest
);
500 for (unsigned i
= 0; i
< num_srcs
; i
++)
501 src_init(&instr
->src
[i
]);
507 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
509 const unsigned num_params
= callee
->num_params
;
510 nir_call_instr
*instr
=
511 rzalloc_size(shader
, sizeof(*instr
) +
512 num_params
* sizeof(instr
->params
[0]));
514 instr_init(&instr
->instr
, nir_instr_type_call
);
515 instr
->callee
= callee
;
516 instr
->num_params
= num_params
;
517 for (unsigned i
= 0; i
< num_params
; i
++)
518 src_init(&instr
->params
[i
]);
523 static int8_t default_tg4_offsets
[4][2] =
532 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
534 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
535 instr_init(&instr
->instr
, nir_instr_type_tex
);
537 dest_init(&instr
->dest
);
539 instr
->num_srcs
= num_srcs
;
540 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
541 for (unsigned i
= 0; i
< num_srcs
; i
++)
542 src_init(&instr
->src
[i
].src
);
544 instr
->texture_index
= 0;
545 instr
->texture_array_size
= 0;
546 instr
->sampler_index
= 0;
547 memcpy(instr
->tg4_offsets
, default_tg4_offsets
, sizeof(instr
->tg4_offsets
));
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_tex_instr_has_explicit_tg4_offsets(nir_tex_instr
*tex
)
593 if (tex
->op
!= nir_texop_tg4
)
595 return memcmp(tex
->tg4_offsets
, default_tg4_offsets
,
596 sizeof(tex
->tg4_offsets
)) != 0;
600 nir_phi_instr_create(nir_shader
*shader
)
602 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
603 instr_init(&instr
->instr
, nir_instr_type_phi
);
605 dest_init(&instr
->dest
);
606 exec_list_make_empty(&instr
->srcs
);
610 nir_parallel_copy_instr
*
611 nir_parallel_copy_instr_create(nir_shader
*shader
)
613 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
614 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
616 exec_list_make_empty(&instr
->entries
);
621 nir_ssa_undef_instr
*
622 nir_ssa_undef_instr_create(nir_shader
*shader
,
623 unsigned num_components
,
626 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
627 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
629 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
634 static nir_const_value
635 const_value_float(double d
, unsigned bit_size
)
638 memset(&v
, 0, sizeof(v
));
640 case 16: v
.u16
= _mesa_float_to_half(d
); break;
641 case 32: v
.f32
= d
; break;
642 case 64: v
.f64
= d
; break;
644 unreachable("Invalid bit size");
649 static nir_const_value
650 const_value_int(int64_t i
, unsigned bit_size
)
653 memset(&v
, 0, sizeof(v
));
655 case 1: v
.b
= i
& 1; break;
656 case 8: v
.i8
= i
; break;
657 case 16: v
.i16
= i
; break;
658 case 32: v
.i32
= i
; break;
659 case 64: v
.i64
= i
; break;
661 unreachable("Invalid bit size");
667 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
669 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
670 const int64_t min_int
= -max_int
- 1;
673 return const_value_int(0, bit_size
);
675 return const_value_float(0, bit_size
);
677 return const_value_int(1, bit_size
);
679 return const_value_float(1, bit_size
);
681 return const_value_int(max_int
, bit_size
);
683 return const_value_int(~0ull, bit_size
);
685 return const_value_float(INFINITY
, bit_size
);
687 return const_value_int(min_int
, bit_size
);
689 return const_value_int(0, bit_size
);
691 return const_value_float(-INFINITY
, bit_size
);
693 return const_value_int(~0ull, bit_size
);
695 return const_value_int(0, bit_size
);
697 return const_value_int(0, bit_size
);
699 unreachable("Invalid reduction operation");
704 nir_cf_node_get_function(nir_cf_node
*node
)
706 while (node
->type
!= nir_cf_node_function
) {
710 return nir_cf_node_as_function(node
);
713 /* Reduces a cursor by trying to convert everything to after and trying to
714 * go up to block granularity when possible.
717 reduce_cursor(nir_cursor cursor
)
719 switch (cursor
.option
) {
720 case nir_cursor_before_block
:
721 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
722 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
723 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
724 /* Empty block. After is as good as before. */
725 cursor
.option
= nir_cursor_after_block
;
729 case nir_cursor_after_block
:
732 case nir_cursor_before_instr
: {
733 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
735 /* Before this instruction is after the previous */
736 cursor
.instr
= prev_instr
;
737 cursor
.option
= nir_cursor_after_instr
;
739 /* No previous instruction. Switch to before block */
740 cursor
.block
= cursor
.instr
->block
;
741 cursor
.option
= nir_cursor_before_block
;
743 return reduce_cursor(cursor
);
746 case nir_cursor_after_instr
:
747 if (nir_instr_next(cursor
.instr
) == NULL
) {
748 /* This is the last instruction, switch to after block */
749 cursor
.option
= nir_cursor_after_block
;
750 cursor
.block
= cursor
.instr
->block
;
755 unreachable("Inavlid cursor option");
760 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
762 /* Reduced cursors should be unique */
763 a
= reduce_cursor(a
);
764 b
= reduce_cursor(b
);
766 return a
.block
== b
.block
&& a
.option
== b
.option
;
770 add_use_cb(nir_src
*src
, void *state
)
772 nir_instr
*instr
= state
;
774 src
->parent_instr
= instr
;
775 list_addtail(&src
->use_link
,
776 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
782 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
784 nir_instr
*instr
= state
;
786 if (instr
->block
&& def
->index
== UINT_MAX
) {
787 nir_function_impl
*impl
=
788 nir_cf_node_get_function(&instr
->block
->cf_node
);
790 def
->index
= impl
->ssa_alloc
++;
797 add_reg_def_cb(nir_dest
*dest
, void *state
)
799 nir_instr
*instr
= state
;
802 dest
->reg
.parent_instr
= instr
;
803 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
810 add_defs_uses(nir_instr
*instr
)
812 nir_foreach_src(instr
, add_use_cb
, instr
);
813 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
814 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
818 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
820 switch (cursor
.option
) {
821 case nir_cursor_before_block
:
822 /* Only allow inserting jumps into empty blocks. */
823 if (instr
->type
== nir_instr_type_jump
)
824 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
826 instr
->block
= cursor
.block
;
827 add_defs_uses(instr
);
828 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
830 case nir_cursor_after_block
: {
831 /* Inserting instructions after a jump is illegal. */
832 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
833 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
836 instr
->block
= cursor
.block
;
837 add_defs_uses(instr
);
838 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
841 case nir_cursor_before_instr
:
842 assert(instr
->type
!= nir_instr_type_jump
);
843 instr
->block
= cursor
.instr
->block
;
844 add_defs_uses(instr
);
845 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
847 case nir_cursor_after_instr
:
848 /* Inserting instructions after a jump is illegal. */
849 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
851 /* Only allow inserting jumps at the end of the block. */
852 if (instr
->type
== nir_instr_type_jump
)
853 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
855 instr
->block
= cursor
.instr
->block
;
856 add_defs_uses(instr
);
857 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
861 if (instr
->type
== nir_instr_type_jump
)
862 nir_handle_add_jump(instr
->block
);
866 src_is_valid(const nir_src
*src
)
868 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
872 remove_use_cb(nir_src
*src
, void *state
)
876 if (src_is_valid(src
))
877 list_del(&src
->use_link
);
883 remove_def_cb(nir_dest
*dest
, void *state
)
888 list_del(&dest
->reg
.def_link
);
894 remove_defs_uses(nir_instr
*instr
)
896 nir_foreach_dest(instr
, remove_def_cb
, instr
);
897 nir_foreach_src(instr
, remove_use_cb
, instr
);
900 void nir_instr_remove_v(nir_instr
*instr
)
902 remove_defs_uses(instr
);
903 exec_node_remove(&instr
->node
);
905 if (instr
->type
== nir_instr_type_jump
) {
906 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
907 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
914 nir_index_local_regs(nir_function_impl
*impl
)
917 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
918 reg
->index
= index
++;
920 impl
->reg_alloc
= index
;
924 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
926 return cb(&instr
->dest
.dest
, state
);
930 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
932 return cb(&instr
->dest
, state
);
936 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
939 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
940 return cb(&instr
->dest
, state
);
946 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
949 return cb(&instr
->dest
, state
);
953 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
955 return cb(&instr
->dest
, state
);
959 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
960 nir_foreach_dest_cb cb
, void *state
)
962 nir_foreach_parallel_copy_entry(entry
, instr
) {
963 if (!cb(&entry
->dest
, state
))
971 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
973 switch (instr
->type
) {
974 case nir_instr_type_alu
:
975 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
976 case nir_instr_type_deref
:
977 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
978 case nir_instr_type_intrinsic
:
979 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
980 case nir_instr_type_tex
:
981 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
982 case nir_instr_type_phi
:
983 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
984 case nir_instr_type_parallel_copy
:
985 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
988 case nir_instr_type_load_const
:
989 case nir_instr_type_ssa_undef
:
990 case nir_instr_type_call
:
991 case nir_instr_type_jump
:
995 unreachable("Invalid instruction type");
1002 struct foreach_ssa_def_state
{
1003 nir_foreach_ssa_def_cb cb
;
1008 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1010 struct foreach_ssa_def_state
*state
= void_state
;
1013 return state
->cb(&dest
->ssa
, state
->client_state
);
1019 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1021 switch (instr
->type
) {
1022 case nir_instr_type_alu
:
1023 case nir_instr_type_deref
:
1024 case nir_instr_type_tex
:
1025 case nir_instr_type_intrinsic
:
1026 case nir_instr_type_phi
:
1027 case nir_instr_type_parallel_copy
: {
1028 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1029 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1032 case nir_instr_type_load_const
:
1033 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1034 case nir_instr_type_ssa_undef
:
1035 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1036 case nir_instr_type_call
:
1037 case nir_instr_type_jump
:
1040 unreachable("Invalid instruction type");
1045 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1047 if (!cb(src
, state
))
1049 if (!src
->is_ssa
&& src
->reg
.indirect
)
1050 return cb(src
->reg
.indirect
, state
);
1055 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1057 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1058 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1065 visit_deref_instr_src(nir_deref_instr
*instr
,
1066 nir_foreach_src_cb cb
, void *state
)
1068 if (instr
->deref_type
!= nir_deref_type_var
) {
1069 if (!visit_src(&instr
->parent
, cb
, state
))
1073 if (instr
->deref_type
== nir_deref_type_array
||
1074 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1075 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1083 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1085 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1086 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1094 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1097 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1098 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1099 if (!visit_src(&instr
->src
[i
], cb
, state
))
1107 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1109 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1110 if (!visit_src(&instr
->params
[i
], cb
, state
))
1118 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1120 nir_foreach_phi_src(src
, instr
) {
1121 if (!visit_src(&src
->src
, cb
, state
))
1129 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1130 nir_foreach_src_cb cb
, void *state
)
1132 nir_foreach_parallel_copy_entry(entry
, instr
) {
1133 if (!visit_src(&entry
->src
, cb
, state
))
1142 nir_foreach_src_cb cb
;
1143 } visit_dest_indirect_state
;
1146 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1148 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1150 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1151 return state
->cb(dest
->reg
.indirect
, state
->state
);
1157 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1159 switch (instr
->type
) {
1160 case nir_instr_type_alu
:
1161 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1164 case nir_instr_type_deref
:
1165 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1168 case nir_instr_type_intrinsic
:
1169 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1172 case nir_instr_type_tex
:
1173 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1176 case nir_instr_type_call
:
1177 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1180 case nir_instr_type_load_const
:
1181 /* Constant load instructions have no regular sources */
1183 case nir_instr_type_phi
:
1184 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1187 case nir_instr_type_parallel_copy
:
1188 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1192 case nir_instr_type_jump
:
1193 case nir_instr_type_ssa_undef
:
1197 unreachable("Invalid instruction type");
1201 visit_dest_indirect_state dest_state
;
1202 dest_state
.state
= state
;
1204 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1208 nir_const_value_for_float(double f
, unsigned bit_size
)
1211 memset(&v
, 0, sizeof(v
));
1215 v
.u16
= _mesa_float_to_half(f
);
1224 unreachable("Invalid bit size");
1231 nir_const_value_as_float(nir_const_value value
, unsigned bit_size
)
1234 case 16: return _mesa_half_to_float(value
.u16
);
1235 case 32: return value
.f32
;
1236 case 64: return value
.f64
;
1238 unreachable("Invalid bit size");
1243 nir_src_comp_as_int(nir_src src
, unsigned comp
)
1245 assert(nir_src_is_const(src
));
1246 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1248 assert(comp
< load
->def
.num_components
);
1249 switch (load
->def
.bit_size
) {
1250 /* int1_t uses 0/-1 convention */
1251 case 1: return -(int)load
->value
[comp
].b
;
1252 case 8: return load
->value
[comp
].i8
;
1253 case 16: return load
->value
[comp
].i16
;
1254 case 32: return load
->value
[comp
].i32
;
1255 case 64: return load
->value
[comp
].i64
;
1257 unreachable("Invalid bit size");
1262 nir_src_comp_as_uint(nir_src src
, unsigned comp
)
1264 assert(nir_src_is_const(src
));
1265 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1267 assert(comp
< load
->def
.num_components
);
1268 switch (load
->def
.bit_size
) {
1269 case 1: return load
->value
[comp
].b
;
1270 case 8: return load
->value
[comp
].u8
;
1271 case 16: return load
->value
[comp
].u16
;
1272 case 32: return load
->value
[comp
].u32
;
1273 case 64: return load
->value
[comp
].u64
;
1275 unreachable("Invalid bit size");
1280 nir_src_comp_as_bool(nir_src src
, unsigned comp
)
1282 int64_t i
= nir_src_comp_as_int(src
, comp
);
1284 /* Booleans of any size use 0/-1 convention */
1285 assert(i
== 0 || i
== -1);
1291 nir_src_comp_as_float(nir_src src
, unsigned comp
)
1293 assert(nir_src_is_const(src
));
1294 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1296 assert(comp
< load
->def
.num_components
);
1297 switch (load
->def
.bit_size
) {
1298 case 16: return _mesa_half_to_float(load
->value
[comp
].u16
);
1299 case 32: return load
->value
[comp
].f32
;
1300 case 64: return load
->value
[comp
].f64
;
1302 unreachable("Invalid bit size");
1307 nir_src_as_int(nir_src src
)
1309 assert(nir_src_num_components(src
) == 1);
1310 return nir_src_comp_as_int(src
, 0);
1314 nir_src_as_uint(nir_src src
)
1316 assert(nir_src_num_components(src
) == 1);
1317 return nir_src_comp_as_uint(src
, 0);
1321 nir_src_as_bool(nir_src src
)
1323 assert(nir_src_num_components(src
) == 1);
1324 return nir_src_comp_as_bool(src
, 0);
1328 nir_src_as_float(nir_src src
)
1330 assert(nir_src_num_components(src
) == 1);
1331 return nir_src_comp_as_float(src
, 0);
1335 nir_src_as_const_value(nir_src src
)
1340 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1343 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1349 * Returns true if the source is known to be dynamically uniform. Otherwise it
1350 * returns false which means it may or may not be dynamically uniform but it
1351 * can't be determined.
1354 nir_src_is_dynamically_uniform(nir_src src
)
1359 /* Constants are trivially dynamically uniform */
1360 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1363 /* As are uniform variables */
1364 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1365 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1367 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1371 /* Operating together dynamically uniform expressions produces a
1372 * dynamically uniform result
1374 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
1375 nir_alu_instr
*alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
1376 for (int i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
1377 if (!nir_src_is_dynamically_uniform(alu
->src
[i
].src
))
1384 /* XXX: this could have many more tests, such as when a sampler function is
1385 * called with dynamically uniform arguments.
1391 src_remove_all_uses(nir_src
*src
)
1393 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1394 if (!src_is_valid(src
))
1397 list_del(&src
->use_link
);
1402 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1404 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1405 if (!src_is_valid(src
))
1409 src
->parent_instr
= parent_instr
;
1411 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1413 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1416 src
->parent_if
= parent_if
;
1418 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1420 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1426 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1428 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1430 src_remove_all_uses(src
);
1432 src_add_all_uses(src
, instr
, NULL
);
1436 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1438 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1440 src_remove_all_uses(dest
);
1441 src_remove_all_uses(src
);
1443 *src
= NIR_SRC_INIT
;
1444 src_add_all_uses(dest
, dest_instr
, NULL
);
1448 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1450 nir_src
*src
= &if_stmt
->condition
;
1451 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1453 src_remove_all_uses(src
);
1455 src_add_all_uses(src
, NULL
, if_stmt
);
1459 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1462 /* We can only overwrite an SSA destination if it has no uses. */
1463 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1465 list_del(&dest
->reg
.def_link
);
1466 if (dest
->reg
.indirect
)
1467 src_remove_all_uses(dest
->reg
.indirect
);
1470 /* We can't re-write with an SSA def */
1471 assert(!new_dest
.is_ssa
);
1473 nir_dest_copy(dest
, &new_dest
, instr
);
1475 dest
->reg
.parent_instr
= instr
;
1476 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1478 if (dest
->reg
.indirect
)
1479 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1482 /* note: does *not* take ownership of 'name' */
1484 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1485 unsigned num_components
,
1486 unsigned bit_size
, const char *name
)
1488 def
->name
= ralloc_strdup(instr
, name
);
1489 def
->parent_instr
= instr
;
1490 list_inithead(&def
->uses
);
1491 list_inithead(&def
->if_uses
);
1492 def
->num_components
= num_components
;
1493 def
->bit_size
= bit_size
;
1496 nir_function_impl
*impl
=
1497 nir_cf_node_get_function(&instr
->block
->cf_node
);
1499 def
->index
= impl
->ssa_alloc
++;
1501 def
->index
= UINT_MAX
;
1505 /* note: does *not* take ownership of 'name' */
1507 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1508 unsigned num_components
, unsigned bit_size
,
1511 dest
->is_ssa
= true;
1512 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1516 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1518 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1520 nir_foreach_use_safe(use_src
, def
)
1521 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1523 nir_foreach_if_use_safe(use_src
, def
)
1524 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1528 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1530 assert(start
->block
== end
->block
);
1532 if (between
->block
!= start
->block
)
1535 /* Search backwards looking for "between" */
1536 while (start
!= end
) {
1540 end
= nir_instr_prev(end
);
1547 /* Replaces all uses of the given SSA def with the given source but only if
1548 * the use comes after the after_me instruction. This can be useful if you
1549 * are emitting code to fix up the result of some instruction: you can freely
1550 * use the result in that code and then call rewrite_uses_after and pass the
1551 * last fixup instruction as after_me and it will replace all of the uses you
1552 * want without touching the fixup code.
1554 * This function assumes that after_me is in the same block as
1555 * def->parent_instr and that after_me comes after def->parent_instr.
1558 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1559 nir_instr
*after_me
)
1561 if (new_src
.is_ssa
&& def
== new_src
.ssa
)
1564 nir_foreach_use_safe(use_src
, def
) {
1565 assert(use_src
->parent_instr
!= def
->parent_instr
);
1566 /* Since def already dominates all of its uses, the only way a use can
1567 * not be dominated by after_me is if it is between def and after_me in
1568 * the instruction list.
1570 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1571 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1574 nir_foreach_if_use_safe(use_src
, def
)
1575 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1578 nir_component_mask_t
1579 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1581 nir_component_mask_t read_mask
= 0;
1582 nir_foreach_use(use
, def
) {
1583 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1584 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1585 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1586 int src_idx
= alu_src
- &alu
->src
[0];
1587 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1588 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1590 return (1 << def
->num_components
) - 1;
1594 if (!list_empty(&def
->if_uses
))
1601 nir_block_cf_tree_next(nir_block
*block
)
1603 if (block
== NULL
) {
1604 /* nir_foreach_block_safe() will call this function on a NULL block
1605 * after the last iteration, but it won't use the result so just return
1611 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1613 return nir_cf_node_cf_tree_first(cf_next
);
1615 nir_cf_node
*parent
= block
->cf_node
.parent
;
1617 switch (parent
->type
) {
1618 case nir_cf_node_if
: {
1619 /* Are we at the end of the if? Go to the beginning of the else */
1620 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1621 if (block
== nir_if_last_then_block(if_stmt
))
1622 return nir_if_first_else_block(if_stmt
);
1624 assert(block
== nir_if_last_else_block(if_stmt
));
1628 case nir_cf_node_loop
:
1629 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1631 case nir_cf_node_function
:
1635 unreachable("unknown cf node type");
1640 nir_block_cf_tree_prev(nir_block
*block
)
1642 if (block
== NULL
) {
1643 /* do this for consistency with nir_block_cf_tree_next() */
1647 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1649 return nir_cf_node_cf_tree_last(cf_prev
);
1651 nir_cf_node
*parent
= block
->cf_node
.parent
;
1653 switch (parent
->type
) {
1654 case nir_cf_node_if
: {
1655 /* Are we at the beginning of the else? Go to the end of the if */
1656 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1657 if (block
== nir_if_first_else_block(if_stmt
))
1658 return nir_if_last_then_block(if_stmt
);
1660 assert(block
== nir_if_first_then_block(if_stmt
));
1664 case nir_cf_node_loop
:
1665 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1667 case nir_cf_node_function
:
1671 unreachable("unknown cf node type");
1675 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1677 switch (node
->type
) {
1678 case nir_cf_node_function
: {
1679 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1680 return nir_start_block(impl
);
1683 case nir_cf_node_if
: {
1684 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1685 return nir_if_first_then_block(if_stmt
);
1688 case nir_cf_node_loop
: {
1689 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1690 return nir_loop_first_block(loop
);
1693 case nir_cf_node_block
: {
1694 return nir_cf_node_as_block(node
);
1698 unreachable("unknown node type");
1702 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1704 switch (node
->type
) {
1705 case nir_cf_node_function
: {
1706 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1707 return nir_impl_last_block(impl
);
1710 case nir_cf_node_if
: {
1711 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1712 return nir_if_last_else_block(if_stmt
);
1715 case nir_cf_node_loop
: {
1716 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1717 return nir_loop_last_block(loop
);
1720 case nir_cf_node_block
: {
1721 return nir_cf_node_as_block(node
);
1725 unreachable("unknown node type");
1729 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1731 if (node
->type
== nir_cf_node_block
)
1732 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1733 else if (node
->type
== nir_cf_node_function
)
1736 return nir_cf_node_as_block(nir_cf_node_next(node
));
1740 nir_block_get_following_if(nir_block
*block
)
1742 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1745 if (nir_cf_node_is_last(&block
->cf_node
))
1748 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1750 if (next_node
->type
!= nir_cf_node_if
)
1753 return nir_cf_node_as_if(next_node
);
1757 nir_block_get_following_loop(nir_block
*block
)
1759 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1762 if (nir_cf_node_is_last(&block
->cf_node
))
1765 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1767 if (next_node
->type
!= nir_cf_node_loop
)
1770 return nir_cf_node_as_loop(next_node
);
1774 nir_index_blocks(nir_function_impl
*impl
)
1778 if (impl
->valid_metadata
& nir_metadata_block_index
)
1781 nir_foreach_block(block
, impl
) {
1782 block
->index
= index
++;
1785 /* The end_block isn't really part of the program, which is why its index
1788 impl
->num_blocks
= impl
->end_block
->index
= index
;
1792 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1794 unsigned *index
= (unsigned *) state
;
1795 def
->index
= (*index
)++;
1801 * The indices are applied top-to-bottom which has the very nice property
1802 * that, if A dominates B, then A->index <= B->index.
1805 nir_index_ssa_defs(nir_function_impl
*impl
)
1809 nir_foreach_block(block
, impl
) {
1810 nir_foreach_instr(instr
, block
)
1811 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1814 impl
->ssa_alloc
= index
;
1818 * The indices are applied top-to-bottom which has the very nice property
1819 * that, if A dominates B, then A->index <= B->index.
1822 nir_index_instrs(nir_function_impl
*impl
)
1826 nir_foreach_block(block
, impl
) {
1827 nir_foreach_instr(instr
, block
)
1828 instr
->index
= index
++;
1835 nir_intrinsic_from_system_value(gl_system_value val
)
1838 case SYSTEM_VALUE_VERTEX_ID
:
1839 return nir_intrinsic_load_vertex_id
;
1840 case SYSTEM_VALUE_INSTANCE_ID
:
1841 return nir_intrinsic_load_instance_id
;
1842 case SYSTEM_VALUE_DRAW_ID
:
1843 return nir_intrinsic_load_draw_id
;
1844 case SYSTEM_VALUE_BASE_INSTANCE
:
1845 return nir_intrinsic_load_base_instance
;
1846 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1847 return nir_intrinsic_load_vertex_id_zero_base
;
1848 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
1849 return nir_intrinsic_load_is_indexed_draw
;
1850 case SYSTEM_VALUE_FIRST_VERTEX
:
1851 return nir_intrinsic_load_first_vertex
;
1852 case SYSTEM_VALUE_BASE_VERTEX
:
1853 return nir_intrinsic_load_base_vertex
;
1854 case SYSTEM_VALUE_INVOCATION_ID
:
1855 return nir_intrinsic_load_invocation_id
;
1856 case SYSTEM_VALUE_FRAG_COORD
:
1857 return nir_intrinsic_load_frag_coord
;
1858 case SYSTEM_VALUE_FRONT_FACE
:
1859 return nir_intrinsic_load_front_face
;
1860 case SYSTEM_VALUE_SAMPLE_ID
:
1861 return nir_intrinsic_load_sample_id
;
1862 case SYSTEM_VALUE_SAMPLE_POS
:
1863 return nir_intrinsic_load_sample_pos
;
1864 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1865 return nir_intrinsic_load_sample_mask_in
;
1866 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1867 return nir_intrinsic_load_local_invocation_id
;
1868 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
1869 return nir_intrinsic_load_local_invocation_index
;
1870 case SYSTEM_VALUE_WORK_GROUP_ID
:
1871 return nir_intrinsic_load_work_group_id
;
1872 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1873 return nir_intrinsic_load_num_work_groups
;
1874 case SYSTEM_VALUE_PRIMITIVE_ID
:
1875 return nir_intrinsic_load_primitive_id
;
1876 case SYSTEM_VALUE_TESS_COORD
:
1877 return nir_intrinsic_load_tess_coord
;
1878 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1879 return nir_intrinsic_load_tess_level_outer
;
1880 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1881 return nir_intrinsic_load_tess_level_inner
;
1882 case SYSTEM_VALUE_VERTICES_IN
:
1883 return nir_intrinsic_load_patch_vertices_in
;
1884 case SYSTEM_VALUE_HELPER_INVOCATION
:
1885 return nir_intrinsic_load_helper_invocation
;
1886 case SYSTEM_VALUE_COLOR0
:
1887 return nir_intrinsic_load_color0
;
1888 case SYSTEM_VALUE_COLOR1
:
1889 return nir_intrinsic_load_color1
;
1890 case SYSTEM_VALUE_VIEW_INDEX
:
1891 return nir_intrinsic_load_view_index
;
1892 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1893 return nir_intrinsic_load_subgroup_size
;
1894 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1895 return nir_intrinsic_load_subgroup_invocation
;
1896 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1897 return nir_intrinsic_load_subgroup_eq_mask
;
1898 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1899 return nir_intrinsic_load_subgroup_ge_mask
;
1900 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1901 return nir_intrinsic_load_subgroup_gt_mask
;
1902 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1903 return nir_intrinsic_load_subgroup_le_mask
;
1904 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1905 return nir_intrinsic_load_subgroup_lt_mask
;
1906 case SYSTEM_VALUE_NUM_SUBGROUPS
:
1907 return nir_intrinsic_load_num_subgroups
;
1908 case SYSTEM_VALUE_SUBGROUP_ID
:
1909 return nir_intrinsic_load_subgroup_id
;
1910 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
1911 return nir_intrinsic_load_local_group_size
;
1912 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
1913 return nir_intrinsic_load_global_invocation_id
;
1914 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
:
1915 return nir_intrinsic_load_global_invocation_index
;
1916 case SYSTEM_VALUE_WORK_DIM
:
1917 return nir_intrinsic_load_work_dim
;
1919 unreachable("system value does not directly correspond to intrinsic");
1924 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1927 case nir_intrinsic_load_vertex_id
:
1928 return SYSTEM_VALUE_VERTEX_ID
;
1929 case nir_intrinsic_load_instance_id
:
1930 return SYSTEM_VALUE_INSTANCE_ID
;
1931 case nir_intrinsic_load_draw_id
:
1932 return SYSTEM_VALUE_DRAW_ID
;
1933 case nir_intrinsic_load_base_instance
:
1934 return SYSTEM_VALUE_BASE_INSTANCE
;
1935 case nir_intrinsic_load_vertex_id_zero_base
:
1936 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1937 case nir_intrinsic_load_first_vertex
:
1938 return SYSTEM_VALUE_FIRST_VERTEX
;
1939 case nir_intrinsic_load_is_indexed_draw
:
1940 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
1941 case nir_intrinsic_load_base_vertex
:
1942 return SYSTEM_VALUE_BASE_VERTEX
;
1943 case nir_intrinsic_load_invocation_id
:
1944 return SYSTEM_VALUE_INVOCATION_ID
;
1945 case nir_intrinsic_load_frag_coord
:
1946 return SYSTEM_VALUE_FRAG_COORD
;
1947 case nir_intrinsic_load_front_face
:
1948 return SYSTEM_VALUE_FRONT_FACE
;
1949 case nir_intrinsic_load_sample_id
:
1950 return SYSTEM_VALUE_SAMPLE_ID
;
1951 case nir_intrinsic_load_sample_pos
:
1952 return SYSTEM_VALUE_SAMPLE_POS
;
1953 case nir_intrinsic_load_sample_mask_in
:
1954 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1955 case nir_intrinsic_load_local_invocation_id
:
1956 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1957 case nir_intrinsic_load_local_invocation_index
:
1958 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1959 case nir_intrinsic_load_num_work_groups
:
1960 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1961 case nir_intrinsic_load_work_group_id
:
1962 return SYSTEM_VALUE_WORK_GROUP_ID
;
1963 case nir_intrinsic_load_primitive_id
:
1964 return SYSTEM_VALUE_PRIMITIVE_ID
;
1965 case nir_intrinsic_load_tess_coord
:
1966 return SYSTEM_VALUE_TESS_COORD
;
1967 case nir_intrinsic_load_tess_level_outer
:
1968 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1969 case nir_intrinsic_load_tess_level_inner
:
1970 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1971 case nir_intrinsic_load_patch_vertices_in
:
1972 return SYSTEM_VALUE_VERTICES_IN
;
1973 case nir_intrinsic_load_helper_invocation
:
1974 return SYSTEM_VALUE_HELPER_INVOCATION
;
1975 case nir_intrinsic_load_color0
:
1976 return SYSTEM_VALUE_COLOR0
;
1977 case nir_intrinsic_load_color1
:
1978 return SYSTEM_VALUE_COLOR1
;
1979 case nir_intrinsic_load_view_index
:
1980 return SYSTEM_VALUE_VIEW_INDEX
;
1981 case nir_intrinsic_load_subgroup_size
:
1982 return SYSTEM_VALUE_SUBGROUP_SIZE
;
1983 case nir_intrinsic_load_subgroup_invocation
:
1984 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1985 case nir_intrinsic_load_subgroup_eq_mask
:
1986 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
1987 case nir_intrinsic_load_subgroup_ge_mask
:
1988 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
1989 case nir_intrinsic_load_subgroup_gt_mask
:
1990 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
1991 case nir_intrinsic_load_subgroup_le_mask
:
1992 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
1993 case nir_intrinsic_load_subgroup_lt_mask
:
1994 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
1995 case nir_intrinsic_load_num_subgroups
:
1996 return SYSTEM_VALUE_NUM_SUBGROUPS
;
1997 case nir_intrinsic_load_subgroup_id
:
1998 return SYSTEM_VALUE_SUBGROUP_ID
;
1999 case nir_intrinsic_load_local_group_size
:
2000 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2001 case nir_intrinsic_load_global_invocation_id
:
2002 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
2004 unreachable("intrinsic doesn't produce a system value");
2008 /* OpenGL utility method that remaps the location attributes if they are
2009 * doubles. Not needed for vulkan due the differences on the input location
2010 * count for doubles on vulkan vs OpenGL
2012 * The bitfield returned in dual_slot is one bit for each double input slot in
2013 * the original OpenGL single-slot input numbering. The mapping from old
2014 * locations to new locations is as follows:
2016 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
2019 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
2021 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
2024 nir_foreach_variable(var
, &shader
->inputs
) {
2025 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
2026 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
2027 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
2031 nir_foreach_variable(var
, &shader
->inputs
) {
2032 var
->data
.location
+=
2033 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
2037 /* Returns an attribute mask that has been re-compacted using the given
2041 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
2044 unsigned loc
= u_bit_scan64(&dual_slot
);
2045 /* mask of all bits up to and including loc */
2046 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
2047 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);
2053 nir_rewrite_image_intrinsic(nir_intrinsic_instr
*intrin
, nir_ssa_def
*src
,
2056 switch (intrin
->intrinsic
) {
2058 case nir_intrinsic_image_deref_##op: \
2059 intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
2060 : nir_intrinsic_image_##op; \
2070 CASE(atomic_exchange
)
2071 CASE(atomic_comp_swap
)
2075 CASE(load_raw_intel
)
2076 CASE(store_raw_intel
)
2079 unreachable("Unhanded image intrinsic");
2082 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
2083 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2085 nir_intrinsic_set_image_dim(intrin
, glsl_get_sampler_dim(deref
->type
));
2086 nir_intrinsic_set_image_array(intrin
, glsl_sampler_type_is_array(deref
->type
));
2087 nir_intrinsic_set_format(intrin
, var
->data
.image
.format
);
2089 nir_instr_rewrite_src(&intrin
->instr
, &intrin
->src
[0],
2090 nir_src_for_ssa(src
));