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_builder.h"
30 #include "nir_control_flow_private.h"
31 #include "util/half_float.h"
35 #include "util/u_math.h"
37 #include "main/menums.h" /* BITFIELD64_MASK */
40 nir_shader_create(void *mem_ctx
,
41 gl_shader_stage stage
,
42 const nir_shader_compiler_options
*options
,
45 nir_shader
*shader
= rzalloc(mem_ctx
, nir_shader
);
47 exec_list_make_empty(&shader
->uniforms
);
48 exec_list_make_empty(&shader
->inputs
);
49 exec_list_make_empty(&shader
->outputs
);
50 exec_list_make_empty(&shader
->shared
);
52 shader
->options
= options
;
55 assert(si
->stage
== stage
);
58 shader
->info
.stage
= stage
;
61 exec_list_make_empty(&shader
->functions
);
62 exec_list_make_empty(&shader
->globals
);
63 exec_list_make_empty(&shader
->system_values
);
65 shader
->num_inputs
= 0;
66 shader
->num_outputs
= 0;
67 shader
->num_uniforms
= 0;
68 shader
->num_shared
= 0;
74 reg_create(void *mem_ctx
, struct exec_list
*list
)
76 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
78 list_inithead(®
->uses
);
79 list_inithead(®
->defs
);
80 list_inithead(®
->if_uses
);
82 reg
->num_components
= 0;
84 reg
->num_array_elems
= 0;
87 exec_list_push_tail(list
, ®
->node
);
93 nir_local_reg_create(nir_function_impl
*impl
)
95 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
96 reg
->index
= impl
->reg_alloc
++;
102 nir_reg_remove(nir_register
*reg
)
104 exec_node_remove(®
->node
);
108 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
110 switch (var
->data
.mode
) {
112 assert(!"invalid mode");
115 case nir_var_function_temp
:
116 assert(!"nir_shader_add_variable cannot be used for local variables");
119 case nir_var_shader_temp
:
120 exec_list_push_tail(&shader
->globals
, &var
->node
);
123 case nir_var_shader_in
:
124 exec_list_push_tail(&shader
->inputs
, &var
->node
);
127 case nir_var_shader_out
:
128 exec_list_push_tail(&shader
->outputs
, &var
->node
);
131 case nir_var_uniform
:
132 case nir_var_mem_ubo
:
133 case nir_var_mem_ssbo
:
134 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
137 case nir_var_mem_shared
:
138 assert(gl_shader_stage_is_compute(shader
->info
.stage
));
139 exec_list_push_tail(&shader
->shared
, &var
->node
);
142 case nir_var_mem_global
:
143 assert(!"nir_shader_add_variable cannot be used for global memory");
146 case nir_var_system_value
:
147 exec_list_push_tail(&shader
->system_values
, &var
->node
);
153 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
154 const struct glsl_type
*type
, const char *name
)
156 nir_variable
*var
= rzalloc(shader
, nir_variable
);
157 var
->name
= ralloc_strdup(var
, name
);
159 var
->data
.mode
= mode
;
160 var
->data
.how_declared
= nir_var_declared_normally
;
162 if ((mode
== nir_var_shader_in
&&
163 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
164 (mode
== nir_var_shader_out
&&
165 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
166 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
168 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
169 var
->data
.read_only
= true;
171 nir_shader_add_variable(shader
, var
);
177 nir_local_variable_create(nir_function_impl
*impl
,
178 const struct glsl_type
*type
, const char *name
)
180 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
181 var
->name
= ralloc_strdup(var
, name
);
183 var
->data
.mode
= nir_var_function_temp
;
185 nir_function_impl_add_variable(impl
, var
);
191 nir_function_create(nir_shader
*shader
, const char *name
)
193 nir_function
*func
= ralloc(shader
, nir_function
);
195 exec_list_push_tail(&shader
->functions
, &func
->node
);
197 func
->name
= ralloc_strdup(func
, name
);
198 func
->shader
= shader
;
199 func
->num_params
= 0;
202 func
->is_entrypoint
= false;
207 /* NOTE: if the instruction you are copying a src to is already added
208 * to the IR, use nir_instr_rewrite_src() instead.
210 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
212 dest
->is_ssa
= src
->is_ssa
;
214 dest
->ssa
= src
->ssa
;
216 dest
->reg
.base_offset
= src
->reg
.base_offset
;
217 dest
->reg
.reg
= src
->reg
.reg
;
218 if (src
->reg
.indirect
) {
219 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
220 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
222 dest
->reg
.indirect
= NULL
;
227 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
229 /* Copying an SSA definition makes no sense whatsoever. */
230 assert(!src
->is_ssa
);
232 dest
->is_ssa
= false;
234 dest
->reg
.base_offset
= src
->reg
.base_offset
;
235 dest
->reg
.reg
= src
->reg
.reg
;
236 if (src
->reg
.indirect
) {
237 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
238 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
240 dest
->reg
.indirect
= NULL
;
245 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
246 nir_alu_instr
*instr
)
248 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
249 dest
->abs
= src
->abs
;
250 dest
->negate
= src
->negate
;
251 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
252 dest
->swizzle
[i
] = src
->swizzle
[i
];
256 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
257 nir_alu_instr
*instr
)
259 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
260 dest
->write_mask
= src
->write_mask
;
261 dest
->saturate
= src
->saturate
;
266 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
268 exec_node_init(&node
->node
);
274 nir_function_impl_create_bare(nir_shader
*shader
)
276 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
278 impl
->function
= NULL
;
280 cf_init(&impl
->cf_node
, nir_cf_node_function
);
282 exec_list_make_empty(&impl
->body
);
283 exec_list_make_empty(&impl
->registers
);
284 exec_list_make_empty(&impl
->locals
);
287 impl
->valid_metadata
= nir_metadata_none
;
289 /* create start & end blocks */
290 nir_block
*start_block
= nir_block_create(shader
);
291 nir_block
*end_block
= nir_block_create(shader
);
292 start_block
->cf_node
.parent
= &impl
->cf_node
;
293 end_block
->cf_node
.parent
= &impl
->cf_node
;
294 impl
->end_block
= end_block
;
296 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
298 start_block
->successors
[0] = end_block
;
299 _mesa_set_add(end_block
->predecessors
, start_block
);
304 nir_function_impl_create(nir_function
*function
)
306 assert(function
->impl
== NULL
);
308 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
310 function
->impl
= impl
;
311 impl
->function
= function
;
317 nir_block_create(nir_shader
*shader
)
319 nir_block
*block
= rzalloc(shader
, nir_block
);
321 cf_init(&block
->cf_node
, nir_cf_node_block
);
323 block
->successors
[0] = block
->successors
[1] = NULL
;
324 block
->predecessors
= _mesa_pointer_set_create(block
);
325 block
->imm_dom
= NULL
;
326 /* XXX maybe it would be worth it to defer allocation? This
327 * way it doesn't get allocated for shader refs that never run
328 * nir_calc_dominance? For example, state-tracker creates an
329 * initial IR, clones that, runs appropriate lowering pass, passes
330 * to driver which does common lowering/opt, and then stores ref
331 * which is later used to do state specific lowering and futher
332 * opt. Do any of the references not need dominance metadata?
334 block
->dom_frontier
= _mesa_pointer_set_create(block
);
336 exec_list_make_empty(&block
->instr_list
);
342 src_init(nir_src
*src
)
346 src
->reg
.indirect
= NULL
;
347 src
->reg
.base_offset
= 0;
351 nir_if_create(nir_shader
*shader
)
353 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
355 if_stmt
->control
= nir_selection_control_none
;
357 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
358 src_init(&if_stmt
->condition
);
360 nir_block
*then
= nir_block_create(shader
);
361 exec_list_make_empty(&if_stmt
->then_list
);
362 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
363 then
->cf_node
.parent
= &if_stmt
->cf_node
;
365 nir_block
*else_stmt
= nir_block_create(shader
);
366 exec_list_make_empty(&if_stmt
->else_list
);
367 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
368 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
374 nir_loop_create(nir_shader
*shader
)
376 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
378 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
380 nir_block
*body
= nir_block_create(shader
);
381 exec_list_make_empty(&loop
->body
);
382 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
383 body
->cf_node
.parent
= &loop
->cf_node
;
385 body
->successors
[0] = body
;
386 _mesa_set_add(body
->predecessors
, body
);
392 instr_init(nir_instr
*instr
, nir_instr_type type
)
396 exec_node_init(&instr
->node
);
400 dest_init(nir_dest
*dest
)
402 dest
->is_ssa
= false;
403 dest
->reg
.reg
= NULL
;
404 dest
->reg
.indirect
= NULL
;
405 dest
->reg
.base_offset
= 0;
409 alu_dest_init(nir_alu_dest
*dest
)
411 dest_init(&dest
->dest
);
412 dest
->saturate
= false;
413 dest
->write_mask
= 0xf;
417 alu_src_init(nir_alu_src
*src
)
420 src
->abs
= src
->negate
= false;
421 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
426 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
428 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
429 /* TODO: don't use rzalloc */
430 nir_alu_instr
*instr
=
432 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
434 instr_init(&instr
->instr
, nir_instr_type_alu
);
436 alu_dest_init(&instr
->dest
);
437 for (unsigned i
= 0; i
< num_srcs
; i
++)
438 alu_src_init(&instr
->src
[i
]);
444 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
446 nir_deref_instr
*instr
=
447 rzalloc_size(shader
, sizeof(nir_deref_instr
));
449 instr_init(&instr
->instr
, nir_instr_type_deref
);
451 instr
->deref_type
= deref_type
;
452 if (deref_type
!= nir_deref_type_var
)
453 src_init(&instr
->parent
);
455 if (deref_type
== nir_deref_type_array
||
456 deref_type
== nir_deref_type_ptr_as_array
)
457 src_init(&instr
->arr
.index
);
459 dest_init(&instr
->dest
);
465 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
467 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
468 instr_init(&instr
->instr
, nir_instr_type_jump
);
473 nir_load_const_instr
*
474 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
477 nir_load_const_instr
*instr
=
478 rzalloc_size(shader
, sizeof(*instr
) + num_components
* sizeof(*instr
->value
));
479 instr_init(&instr
->instr
, nir_instr_type_load_const
);
481 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
486 nir_intrinsic_instr
*
487 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
489 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
490 /* TODO: don't use rzalloc */
491 nir_intrinsic_instr
*instr
=
493 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
495 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
496 instr
->intrinsic
= op
;
498 if (nir_intrinsic_infos
[op
].has_dest
)
499 dest_init(&instr
->dest
);
501 for (unsigned i
= 0; i
< num_srcs
; i
++)
502 src_init(&instr
->src
[i
]);
508 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
510 const unsigned num_params
= callee
->num_params
;
511 nir_call_instr
*instr
=
512 rzalloc_size(shader
, sizeof(*instr
) +
513 num_params
* sizeof(instr
->params
[0]));
515 instr_init(&instr
->instr
, nir_instr_type_call
);
516 instr
->callee
= callee
;
517 instr
->num_params
= num_params
;
518 for (unsigned i
= 0; i
< num_params
; i
++)
519 src_init(&instr
->params
[i
]);
524 static int8_t default_tg4_offsets
[4][2] =
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;
548 memcpy(instr
->tg4_offsets
, default_tg4_offsets
, sizeof(instr
->tg4_offsets
));
554 nir_tex_instr_add_src(nir_tex_instr
*tex
,
555 nir_tex_src_type src_type
,
558 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
561 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
562 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
563 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
567 ralloc_free(tex
->src
);
570 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
571 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
576 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
578 assert(src_idx
< tex
->num_srcs
);
580 /* First rewrite the source to NIR_SRC_INIT */
581 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
583 /* Now, move all of the other sources down */
584 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
585 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
586 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
592 nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr
*tex
)
594 if (tex
->op
!= nir_texop_tg4
)
596 return memcmp(tex
->tg4_offsets
, default_tg4_offsets
,
597 sizeof(tex
->tg4_offsets
)) != 0;
601 nir_phi_instr_create(nir_shader
*shader
)
603 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
604 instr_init(&instr
->instr
, nir_instr_type_phi
);
606 dest_init(&instr
->dest
);
607 exec_list_make_empty(&instr
->srcs
);
611 nir_parallel_copy_instr
*
612 nir_parallel_copy_instr_create(nir_shader
*shader
)
614 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
615 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
617 exec_list_make_empty(&instr
->entries
);
622 nir_ssa_undef_instr
*
623 nir_ssa_undef_instr_create(nir_shader
*shader
,
624 unsigned num_components
,
627 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
628 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
630 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
635 static nir_const_value
636 const_value_float(double d
, unsigned bit_size
)
639 memset(&v
, 0, sizeof(v
));
641 case 16: v
.u16
= _mesa_float_to_half(d
); break;
642 case 32: v
.f32
= d
; break;
643 case 64: v
.f64
= d
; break;
645 unreachable("Invalid bit size");
650 static nir_const_value
651 const_value_int(int64_t i
, unsigned bit_size
)
654 memset(&v
, 0, sizeof(v
));
656 case 1: v
.b
= i
& 1; break;
657 case 8: v
.i8
= i
; break;
658 case 16: v
.i16
= i
; break;
659 case 32: v
.i32
= i
; break;
660 case 64: v
.i64
= i
; break;
662 unreachable("Invalid bit size");
668 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
670 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
671 const int64_t min_int
= -max_int
- 1;
674 return const_value_int(0, bit_size
);
676 return const_value_float(0, bit_size
);
678 return const_value_int(1, bit_size
);
680 return const_value_float(1, bit_size
);
682 return const_value_int(max_int
, bit_size
);
684 return const_value_int(~0ull, bit_size
);
686 return const_value_float(INFINITY
, bit_size
);
688 return const_value_int(min_int
, bit_size
);
690 return const_value_int(0, bit_size
);
692 return const_value_float(-INFINITY
, bit_size
);
694 return const_value_int(~0ull, bit_size
);
696 return const_value_int(0, bit_size
);
698 return const_value_int(0, bit_size
);
700 unreachable("Invalid reduction operation");
705 nir_cf_node_get_function(nir_cf_node
*node
)
707 while (node
->type
!= nir_cf_node_function
) {
711 return nir_cf_node_as_function(node
);
714 /* Reduces a cursor by trying to convert everything to after and trying to
715 * go up to block granularity when possible.
718 reduce_cursor(nir_cursor cursor
)
720 switch (cursor
.option
) {
721 case nir_cursor_before_block
:
722 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
723 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
724 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
725 /* Empty block. After is as good as before. */
726 cursor
.option
= nir_cursor_after_block
;
730 case nir_cursor_after_block
:
733 case nir_cursor_before_instr
: {
734 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
736 /* Before this instruction is after the previous */
737 cursor
.instr
= prev_instr
;
738 cursor
.option
= nir_cursor_after_instr
;
740 /* No previous instruction. Switch to before block */
741 cursor
.block
= cursor
.instr
->block
;
742 cursor
.option
= nir_cursor_before_block
;
744 return reduce_cursor(cursor
);
747 case nir_cursor_after_instr
:
748 if (nir_instr_next(cursor
.instr
) == NULL
) {
749 /* This is the last instruction, switch to after block */
750 cursor
.option
= nir_cursor_after_block
;
751 cursor
.block
= cursor
.instr
->block
;
756 unreachable("Inavlid cursor option");
761 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
763 /* Reduced cursors should be unique */
764 a
= reduce_cursor(a
);
765 b
= reduce_cursor(b
);
767 return a
.block
== b
.block
&& a
.option
== b
.option
;
771 add_use_cb(nir_src
*src
, void *state
)
773 nir_instr
*instr
= state
;
775 src
->parent_instr
= instr
;
776 list_addtail(&src
->use_link
,
777 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
783 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
785 nir_instr
*instr
= state
;
787 if (instr
->block
&& def
->index
== UINT_MAX
) {
788 nir_function_impl
*impl
=
789 nir_cf_node_get_function(&instr
->block
->cf_node
);
791 def
->index
= impl
->ssa_alloc
++;
798 add_reg_def_cb(nir_dest
*dest
, void *state
)
800 nir_instr
*instr
= state
;
803 dest
->reg
.parent_instr
= instr
;
804 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
811 add_defs_uses(nir_instr
*instr
)
813 nir_foreach_src(instr
, add_use_cb
, instr
);
814 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
815 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
819 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
821 switch (cursor
.option
) {
822 case nir_cursor_before_block
:
823 /* Only allow inserting jumps into empty blocks. */
824 if (instr
->type
== nir_instr_type_jump
)
825 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
827 instr
->block
= cursor
.block
;
828 add_defs_uses(instr
);
829 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
831 case nir_cursor_after_block
: {
832 /* Inserting instructions after a jump is illegal. */
833 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
834 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
837 instr
->block
= cursor
.block
;
838 add_defs_uses(instr
);
839 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
842 case nir_cursor_before_instr
:
843 assert(instr
->type
!= nir_instr_type_jump
);
844 instr
->block
= cursor
.instr
->block
;
845 add_defs_uses(instr
);
846 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
848 case nir_cursor_after_instr
:
849 /* Inserting instructions after a jump is illegal. */
850 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
852 /* Only allow inserting jumps at the end of the block. */
853 if (instr
->type
== nir_instr_type_jump
)
854 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
856 instr
->block
= cursor
.instr
->block
;
857 add_defs_uses(instr
);
858 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
862 if (instr
->type
== nir_instr_type_jump
)
863 nir_handle_add_jump(instr
->block
);
867 src_is_valid(const nir_src
*src
)
869 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
873 remove_use_cb(nir_src
*src
, void *state
)
877 if (src_is_valid(src
))
878 list_del(&src
->use_link
);
884 remove_def_cb(nir_dest
*dest
, void *state
)
889 list_del(&dest
->reg
.def_link
);
895 remove_defs_uses(nir_instr
*instr
)
897 nir_foreach_dest(instr
, remove_def_cb
, instr
);
898 nir_foreach_src(instr
, remove_use_cb
, instr
);
901 void nir_instr_remove_v(nir_instr
*instr
)
903 remove_defs_uses(instr
);
904 exec_node_remove(&instr
->node
);
906 if (instr
->type
== nir_instr_type_jump
) {
907 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
908 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
915 nir_index_local_regs(nir_function_impl
*impl
)
918 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
919 reg
->index
= index
++;
921 impl
->reg_alloc
= index
;
925 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
927 return cb(&instr
->dest
.dest
, state
);
931 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
933 return cb(&instr
->dest
, state
);
937 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
940 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
941 return cb(&instr
->dest
, state
);
947 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
950 return cb(&instr
->dest
, state
);
954 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
956 return cb(&instr
->dest
, state
);
960 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
961 nir_foreach_dest_cb cb
, void *state
)
963 nir_foreach_parallel_copy_entry(entry
, instr
) {
964 if (!cb(&entry
->dest
, state
))
972 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
974 switch (instr
->type
) {
975 case nir_instr_type_alu
:
976 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
977 case nir_instr_type_deref
:
978 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
979 case nir_instr_type_intrinsic
:
980 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
981 case nir_instr_type_tex
:
982 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
983 case nir_instr_type_phi
:
984 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
985 case nir_instr_type_parallel_copy
:
986 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
989 case nir_instr_type_load_const
:
990 case nir_instr_type_ssa_undef
:
991 case nir_instr_type_call
:
992 case nir_instr_type_jump
:
996 unreachable("Invalid instruction type");
1003 struct foreach_ssa_def_state
{
1004 nir_foreach_ssa_def_cb cb
;
1009 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1011 struct foreach_ssa_def_state
*state
= void_state
;
1014 return state
->cb(&dest
->ssa
, state
->client_state
);
1020 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1022 switch (instr
->type
) {
1023 case nir_instr_type_alu
:
1024 case nir_instr_type_deref
:
1025 case nir_instr_type_tex
:
1026 case nir_instr_type_intrinsic
:
1027 case nir_instr_type_phi
:
1028 case nir_instr_type_parallel_copy
: {
1029 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1030 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1033 case nir_instr_type_load_const
:
1034 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1035 case nir_instr_type_ssa_undef
:
1036 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1037 case nir_instr_type_call
:
1038 case nir_instr_type_jump
:
1041 unreachable("Invalid instruction type");
1046 nir_instr_ssa_def(nir_instr
*instr
)
1048 switch (instr
->type
) {
1049 case nir_instr_type_alu
:
1050 assert(nir_instr_as_alu(instr
)->dest
.dest
.is_ssa
);
1051 return &nir_instr_as_alu(instr
)->dest
.dest
.ssa
;
1053 case nir_instr_type_deref
:
1054 assert(nir_instr_as_deref(instr
)->dest
.is_ssa
);
1055 return &nir_instr_as_deref(instr
)->dest
.ssa
;
1057 case nir_instr_type_tex
:
1058 assert(nir_instr_as_tex(instr
)->dest
.is_ssa
);
1059 return &nir_instr_as_tex(instr
)->dest
.ssa
;
1061 case nir_instr_type_intrinsic
: {
1062 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
1063 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
) {
1064 assert(intrin
->dest
.is_ssa
);
1065 return &intrin
->dest
.ssa
;
1071 case nir_instr_type_phi
:
1072 assert(nir_instr_as_phi(instr
)->dest
.is_ssa
);
1073 return &nir_instr_as_phi(instr
)->dest
.ssa
;
1075 case nir_instr_type_parallel_copy
:
1076 unreachable("Parallel copies are unsupported by this function");
1078 case nir_instr_type_load_const
:
1079 return &nir_instr_as_load_const(instr
)->def
;
1081 case nir_instr_type_ssa_undef
:
1082 return &nir_instr_as_ssa_undef(instr
)->def
;
1084 case nir_instr_type_call
:
1085 case nir_instr_type_jump
:
1089 unreachable("Invalid instruction type");
1093 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1095 if (!cb(src
, state
))
1097 if (!src
->is_ssa
&& src
->reg
.indirect
)
1098 return cb(src
->reg
.indirect
, state
);
1103 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1105 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1106 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1113 visit_deref_instr_src(nir_deref_instr
*instr
,
1114 nir_foreach_src_cb cb
, void *state
)
1116 if (instr
->deref_type
!= nir_deref_type_var
) {
1117 if (!visit_src(&instr
->parent
, cb
, state
))
1121 if (instr
->deref_type
== nir_deref_type_array
||
1122 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1123 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1131 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1133 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1134 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1142 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1145 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1146 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1147 if (!visit_src(&instr
->src
[i
], cb
, state
))
1155 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1157 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1158 if (!visit_src(&instr
->params
[i
], cb
, state
))
1166 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1168 nir_foreach_phi_src(src
, instr
) {
1169 if (!visit_src(&src
->src
, cb
, state
))
1177 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1178 nir_foreach_src_cb cb
, void *state
)
1180 nir_foreach_parallel_copy_entry(entry
, instr
) {
1181 if (!visit_src(&entry
->src
, cb
, state
))
1190 nir_foreach_src_cb cb
;
1191 } visit_dest_indirect_state
;
1194 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1196 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1198 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1199 return state
->cb(dest
->reg
.indirect
, state
->state
);
1205 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1207 switch (instr
->type
) {
1208 case nir_instr_type_alu
:
1209 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1212 case nir_instr_type_deref
:
1213 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1216 case nir_instr_type_intrinsic
:
1217 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1220 case nir_instr_type_tex
:
1221 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1224 case nir_instr_type_call
:
1225 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1228 case nir_instr_type_load_const
:
1229 /* Constant load instructions have no regular sources */
1231 case nir_instr_type_phi
:
1232 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1235 case nir_instr_type_parallel_copy
:
1236 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1240 case nir_instr_type_jump
:
1241 case nir_instr_type_ssa_undef
:
1245 unreachable("Invalid instruction type");
1249 visit_dest_indirect_state dest_state
;
1250 dest_state
.state
= state
;
1252 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1256 nir_const_value_for_float(double f
, unsigned bit_size
)
1259 memset(&v
, 0, sizeof(v
));
1263 v
.u16
= _mesa_float_to_half(f
);
1272 unreachable("Invalid bit size");
1279 nir_const_value_as_float(nir_const_value value
, unsigned bit_size
)
1282 case 16: return _mesa_half_to_float(value
.u16
);
1283 case 32: return value
.f32
;
1284 case 64: return value
.f64
;
1286 unreachable("Invalid bit size");
1291 nir_src_as_const_value(nir_src src
)
1296 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1299 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1305 * Returns true if the source is known to be dynamically uniform. Otherwise it
1306 * returns false which means it may or may not be dynamically uniform but it
1307 * can't be determined.
1310 nir_src_is_dynamically_uniform(nir_src src
)
1315 /* Constants are trivially dynamically uniform */
1316 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1319 /* As are uniform variables */
1320 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1321 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1323 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1327 /* Operating together dynamically uniform expressions produces a
1328 * dynamically uniform result
1330 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
1331 nir_alu_instr
*alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
1332 for (int i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
1333 if (!nir_src_is_dynamically_uniform(alu
->src
[i
].src
))
1340 /* XXX: this could have many more tests, such as when a sampler function is
1341 * called with dynamically uniform arguments.
1347 src_remove_all_uses(nir_src
*src
)
1349 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1350 if (!src_is_valid(src
))
1353 list_del(&src
->use_link
);
1358 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1360 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1361 if (!src_is_valid(src
))
1365 src
->parent_instr
= parent_instr
;
1367 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1369 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1372 src
->parent_if
= parent_if
;
1374 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1376 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1382 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1384 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1386 src_remove_all_uses(src
);
1388 src_add_all_uses(src
, instr
, NULL
);
1392 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1394 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1396 src_remove_all_uses(dest
);
1397 src_remove_all_uses(src
);
1399 *src
= NIR_SRC_INIT
;
1400 src_add_all_uses(dest
, dest_instr
, NULL
);
1404 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1406 nir_src
*src
= &if_stmt
->condition
;
1407 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1409 src_remove_all_uses(src
);
1411 src_add_all_uses(src
, NULL
, if_stmt
);
1415 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1418 /* We can only overwrite an SSA destination if it has no uses. */
1419 assert(list_is_empty(&dest
->ssa
.uses
) && list_is_empty(&dest
->ssa
.if_uses
));
1421 list_del(&dest
->reg
.def_link
);
1422 if (dest
->reg
.indirect
)
1423 src_remove_all_uses(dest
->reg
.indirect
);
1426 /* We can't re-write with an SSA def */
1427 assert(!new_dest
.is_ssa
);
1429 nir_dest_copy(dest
, &new_dest
, instr
);
1431 dest
->reg
.parent_instr
= instr
;
1432 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1434 if (dest
->reg
.indirect
)
1435 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1438 /* note: does *not* take ownership of 'name' */
1440 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1441 unsigned num_components
,
1442 unsigned bit_size
, const char *name
)
1444 def
->name
= ralloc_strdup(instr
, name
);
1445 def
->parent_instr
= instr
;
1446 list_inithead(&def
->uses
);
1447 list_inithead(&def
->if_uses
);
1448 def
->num_components
= num_components
;
1449 def
->bit_size
= bit_size
;
1452 nir_function_impl
*impl
=
1453 nir_cf_node_get_function(&instr
->block
->cf_node
);
1455 def
->index
= impl
->ssa_alloc
++;
1457 def
->index
= UINT_MAX
;
1461 /* note: does *not* take ownership of 'name' */
1463 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1464 unsigned num_components
, unsigned bit_size
,
1467 dest
->is_ssa
= true;
1468 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1472 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1474 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1476 nir_foreach_use_safe(use_src
, def
)
1477 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1479 nir_foreach_if_use_safe(use_src
, def
)
1480 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1484 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1486 assert(start
->block
== end
->block
);
1488 if (between
->block
!= start
->block
)
1491 /* Search backwards looking for "between" */
1492 while (start
!= end
) {
1496 end
= nir_instr_prev(end
);
1503 /* Replaces all uses of the given SSA def with the given source but only if
1504 * the use comes after the after_me instruction. This can be useful if you
1505 * are emitting code to fix up the result of some instruction: you can freely
1506 * use the result in that code and then call rewrite_uses_after and pass the
1507 * last fixup instruction as after_me and it will replace all of the uses you
1508 * want without touching the fixup code.
1510 * This function assumes that after_me is in the same block as
1511 * def->parent_instr and that after_me comes after def->parent_instr.
1514 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1515 nir_instr
*after_me
)
1517 if (new_src
.is_ssa
&& def
== new_src
.ssa
)
1520 nir_foreach_use_safe(use_src
, def
) {
1521 assert(use_src
->parent_instr
!= def
->parent_instr
);
1522 /* Since def already dominates all of its uses, the only way a use can
1523 * not be dominated by after_me is if it is between def and after_me in
1524 * the instruction list.
1526 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1527 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1530 nir_foreach_if_use_safe(use_src
, def
)
1531 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1534 nir_component_mask_t
1535 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1537 nir_component_mask_t read_mask
= 0;
1538 nir_foreach_use(use
, def
) {
1539 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1540 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1541 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1542 int src_idx
= alu_src
- &alu
->src
[0];
1543 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1544 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1546 return (1 << def
->num_components
) - 1;
1550 if (!list_is_empty(&def
->if_uses
))
1557 nir_block_cf_tree_next(nir_block
*block
)
1559 if (block
== NULL
) {
1560 /* nir_foreach_block_safe() will call this function on a NULL block
1561 * after the last iteration, but it won't use the result so just return
1567 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1569 return nir_cf_node_cf_tree_first(cf_next
);
1571 nir_cf_node
*parent
= block
->cf_node
.parent
;
1573 switch (parent
->type
) {
1574 case nir_cf_node_if
: {
1575 /* Are we at the end of the if? Go to the beginning of the else */
1576 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1577 if (block
== nir_if_last_then_block(if_stmt
))
1578 return nir_if_first_else_block(if_stmt
);
1580 assert(block
== nir_if_last_else_block(if_stmt
));
1584 case nir_cf_node_loop
:
1585 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1587 case nir_cf_node_function
:
1591 unreachable("unknown cf node type");
1596 nir_block_cf_tree_prev(nir_block
*block
)
1598 if (block
== NULL
) {
1599 /* do this for consistency with nir_block_cf_tree_next() */
1603 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1605 return nir_cf_node_cf_tree_last(cf_prev
);
1607 nir_cf_node
*parent
= block
->cf_node
.parent
;
1609 switch (parent
->type
) {
1610 case nir_cf_node_if
: {
1611 /* Are we at the beginning of the else? Go to the end of the if */
1612 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1613 if (block
== nir_if_first_else_block(if_stmt
))
1614 return nir_if_last_then_block(if_stmt
);
1616 assert(block
== nir_if_first_then_block(if_stmt
));
1620 case nir_cf_node_loop
:
1621 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1623 case nir_cf_node_function
:
1627 unreachable("unknown cf node type");
1631 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1633 switch (node
->type
) {
1634 case nir_cf_node_function
: {
1635 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1636 return nir_start_block(impl
);
1639 case nir_cf_node_if
: {
1640 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1641 return nir_if_first_then_block(if_stmt
);
1644 case nir_cf_node_loop
: {
1645 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1646 return nir_loop_first_block(loop
);
1649 case nir_cf_node_block
: {
1650 return nir_cf_node_as_block(node
);
1654 unreachable("unknown node type");
1658 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1660 switch (node
->type
) {
1661 case nir_cf_node_function
: {
1662 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1663 return nir_impl_last_block(impl
);
1666 case nir_cf_node_if
: {
1667 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1668 return nir_if_last_else_block(if_stmt
);
1671 case nir_cf_node_loop
: {
1672 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1673 return nir_loop_last_block(loop
);
1676 case nir_cf_node_block
: {
1677 return nir_cf_node_as_block(node
);
1681 unreachable("unknown node type");
1685 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1687 if (node
->type
== nir_cf_node_block
)
1688 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1689 else if (node
->type
== nir_cf_node_function
)
1692 return nir_cf_node_as_block(nir_cf_node_next(node
));
1696 nir_block_get_following_if(nir_block
*block
)
1698 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1701 if (nir_cf_node_is_last(&block
->cf_node
))
1704 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1706 if (next_node
->type
!= nir_cf_node_if
)
1709 return nir_cf_node_as_if(next_node
);
1713 nir_block_get_following_loop(nir_block
*block
)
1715 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1718 if (nir_cf_node_is_last(&block
->cf_node
))
1721 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1723 if (next_node
->type
!= nir_cf_node_loop
)
1726 return nir_cf_node_as_loop(next_node
);
1730 nir_index_blocks(nir_function_impl
*impl
)
1734 if (impl
->valid_metadata
& nir_metadata_block_index
)
1737 nir_foreach_block(block
, impl
) {
1738 block
->index
= index
++;
1741 /* The end_block isn't really part of the program, which is why its index
1744 impl
->num_blocks
= impl
->end_block
->index
= index
;
1748 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1750 unsigned *index
= (unsigned *) state
;
1751 def
->index
= (*index
)++;
1757 * The indices are applied top-to-bottom which has the very nice property
1758 * that, if A dominates B, then A->index <= B->index.
1761 nir_index_ssa_defs(nir_function_impl
*impl
)
1765 nir_foreach_block(block
, impl
) {
1766 nir_foreach_instr(instr
, block
)
1767 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1770 impl
->ssa_alloc
= index
;
1774 * The indices are applied top-to-bottom which has the very nice property
1775 * that, if A dominates B, then A->index <= B->index.
1778 nir_index_instrs(nir_function_impl
*impl
)
1782 nir_foreach_block(block
, impl
) {
1783 nir_foreach_instr(instr
, block
)
1784 instr
->index
= index
++;
1791 index_var_list(struct exec_list
*list
)
1793 unsigned next_index
= 0;
1794 nir_foreach_variable(var
, list
)
1795 var
->index
= next_index
++;
1799 nir_index_vars(nir_shader
*shader
, nir_function_impl
*impl
, nir_variable_mode modes
)
1801 if ((modes
& nir_var_function_temp
) && impl
)
1802 index_var_list(&impl
->locals
);
1804 if (modes
& nir_var_shader_temp
)
1805 index_var_list(&shader
->globals
);
1807 if (modes
& nir_var_shader_in
)
1808 index_var_list(&shader
->inputs
);
1810 if (modes
& nir_var_shader_out
)
1811 index_var_list(&shader
->outputs
);
1813 if (modes
& (nir_var_uniform
| nir_var_mem_ubo
| nir_var_mem_ssbo
))
1814 index_var_list(&shader
->uniforms
);
1816 if (modes
& nir_var_mem_shared
)
1817 index_var_list(&shader
->shared
);
1819 if (modes
& nir_var_system_value
)
1820 index_var_list(&shader
->system_values
);
1824 cursor_next_instr(nir_cursor cursor
)
1826 switch (cursor
.option
) {
1827 case nir_cursor_before_block
:
1828 for (nir_block
*block
= cursor
.block
; block
;
1829 block
= nir_block_cf_tree_next(block
)) {
1830 nir_instr
*instr
= nir_block_first_instr(block
);
1836 case nir_cursor_after_block
:
1837 cursor
.block
= nir_block_cf_tree_next(cursor
.block
);
1838 if (cursor
.block
== NULL
)
1841 cursor
.option
= nir_cursor_before_block
;
1842 return cursor_next_instr(cursor
);
1844 case nir_cursor_before_instr
:
1845 return cursor
.instr
;
1847 case nir_cursor_after_instr
:
1848 if (nir_instr_next(cursor
.instr
))
1849 return nir_instr_next(cursor
.instr
);
1851 cursor
.option
= nir_cursor_after_block
;
1852 cursor
.block
= cursor
.instr
->block
;
1853 return cursor_next_instr(cursor
);
1856 unreachable("Inavlid cursor option");
1859 ASSERTED
static bool
1860 dest_is_ssa(nir_dest
*dest
, void *_state
)
1863 return dest
->is_ssa
;
1867 nir_function_impl_lower_instructions(nir_function_impl
*impl
,
1868 nir_instr_filter_cb filter
,
1869 nir_lower_instr_cb lower
,
1873 nir_builder_init(&b
, impl
);
1875 nir_metadata preserved
= nir_metadata_block_index
|
1876 nir_metadata_dominance
;
1878 bool progress
= false;
1879 nir_cursor iter
= nir_before_cf_list(&impl
->body
);
1881 while ((instr
= cursor_next_instr(iter
)) != NULL
) {
1882 if (filter
&& !filter(instr
, cb_data
)) {
1883 iter
= nir_after_instr(instr
);
1887 assert(nir_foreach_dest(instr
, dest_is_ssa
, NULL
));
1888 nir_ssa_def
*old_def
= nir_instr_ssa_def(instr
);
1889 if (old_def
== NULL
) {
1890 iter
= nir_after_instr(instr
);
1894 /* We're about to ask the callback to generate a replacement for instr.
1895 * Save off the uses from instr's SSA def so we know what uses to
1896 * rewrite later. If we use nir_ssa_def_rewrite_uses, it fails in the
1897 * case where the generated replacement code uses the result of instr
1898 * itself. If we use nir_ssa_def_rewrite_uses_after (which is the
1899 * normal solution to this problem), it doesn't work well if control-
1900 * flow is inserted as part of the replacement, doesn't handle cases
1901 * where the replacement is something consumed by instr, and suffers
1902 * from performance issues. This is the only way to 100% guarantee
1903 * that we rewrite the correct set efficiently.
1905 struct list_head old_uses
, old_if_uses
;
1906 list_replace(&old_def
->uses
, &old_uses
);
1907 list_inithead(&old_def
->uses
);
1908 list_replace(&old_def
->if_uses
, &old_if_uses
);
1909 list_inithead(&old_def
->if_uses
);
1911 b
.cursor
= nir_after_instr(instr
);
1912 nir_ssa_def
*new_def
= lower(&b
, instr
, cb_data
);
1913 if (new_def
&& new_def
!= NIR_LOWER_INSTR_PROGRESS
) {
1914 assert(old_def
!= NULL
);
1915 if (new_def
->parent_instr
->block
!= instr
->block
)
1916 preserved
= nir_metadata_none
;
1918 nir_src new_src
= nir_src_for_ssa(new_def
);
1919 list_for_each_entry_safe(nir_src
, use_src
, &old_uses
, use_link
)
1920 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1922 list_for_each_entry_safe(nir_src
, use_src
, &old_if_uses
, use_link
)
1923 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1925 if (list_is_empty(&old_def
->uses
) && list_is_empty(&old_def
->if_uses
)) {
1926 iter
= nir_instr_remove(instr
);
1928 iter
= nir_after_instr(instr
);
1932 /* We didn't end up lowering after all. Put the uses back */
1934 list_replace(&old_uses
, &old_def
->uses
);
1935 list_replace(&old_if_uses
, &old_def
->if_uses
);
1937 iter
= nir_after_instr(instr
);
1939 if (new_def
== NIR_LOWER_INSTR_PROGRESS
)
1945 nir_metadata_preserve(impl
, preserved
);
1948 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
1956 nir_shader_lower_instructions(nir_shader
*shader
,
1957 nir_instr_filter_cb filter
,
1958 nir_lower_instr_cb lower
,
1961 bool progress
= false;
1963 nir_foreach_function(function
, shader
) {
1964 if (function
->impl
&&
1965 nir_function_impl_lower_instructions(function
->impl
,
1966 filter
, lower
, cb_data
))
1974 nir_intrinsic_from_system_value(gl_system_value val
)
1977 case SYSTEM_VALUE_VERTEX_ID
:
1978 return nir_intrinsic_load_vertex_id
;
1979 case SYSTEM_VALUE_INSTANCE_ID
:
1980 return nir_intrinsic_load_instance_id
;
1981 case SYSTEM_VALUE_DRAW_ID
:
1982 return nir_intrinsic_load_draw_id
;
1983 case SYSTEM_VALUE_BASE_INSTANCE
:
1984 return nir_intrinsic_load_base_instance
;
1985 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1986 return nir_intrinsic_load_vertex_id_zero_base
;
1987 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
1988 return nir_intrinsic_load_is_indexed_draw
;
1989 case SYSTEM_VALUE_FIRST_VERTEX
:
1990 return nir_intrinsic_load_first_vertex
;
1991 case SYSTEM_VALUE_BASE_VERTEX
:
1992 return nir_intrinsic_load_base_vertex
;
1993 case SYSTEM_VALUE_INVOCATION_ID
:
1994 return nir_intrinsic_load_invocation_id
;
1995 case SYSTEM_VALUE_FRAG_COORD
:
1996 return nir_intrinsic_load_frag_coord
;
1997 case SYSTEM_VALUE_POINT_COORD
:
1998 return nir_intrinsic_load_point_coord
;
1999 case SYSTEM_VALUE_FRONT_FACE
:
2000 return nir_intrinsic_load_front_face
;
2001 case SYSTEM_VALUE_SAMPLE_ID
:
2002 return nir_intrinsic_load_sample_id
;
2003 case SYSTEM_VALUE_SAMPLE_POS
:
2004 return nir_intrinsic_load_sample_pos
;
2005 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
2006 return nir_intrinsic_load_sample_mask_in
;
2007 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
2008 return nir_intrinsic_load_local_invocation_id
;
2009 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
2010 return nir_intrinsic_load_local_invocation_index
;
2011 case SYSTEM_VALUE_WORK_GROUP_ID
:
2012 return nir_intrinsic_load_work_group_id
;
2013 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
2014 return nir_intrinsic_load_num_work_groups
;
2015 case SYSTEM_VALUE_PRIMITIVE_ID
:
2016 return nir_intrinsic_load_primitive_id
;
2017 case SYSTEM_VALUE_TESS_COORD
:
2018 return nir_intrinsic_load_tess_coord
;
2019 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
2020 return nir_intrinsic_load_tess_level_outer
;
2021 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
2022 return nir_intrinsic_load_tess_level_inner
;
2023 case SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
:
2024 return nir_intrinsic_load_tess_level_outer_default
;
2025 case SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
:
2026 return nir_intrinsic_load_tess_level_inner_default
;
2027 case SYSTEM_VALUE_VERTICES_IN
:
2028 return nir_intrinsic_load_patch_vertices_in
;
2029 case SYSTEM_VALUE_HELPER_INVOCATION
:
2030 return nir_intrinsic_load_helper_invocation
;
2031 case SYSTEM_VALUE_COLOR0
:
2032 return nir_intrinsic_load_color0
;
2033 case SYSTEM_VALUE_COLOR1
:
2034 return nir_intrinsic_load_color1
;
2035 case SYSTEM_VALUE_VIEW_INDEX
:
2036 return nir_intrinsic_load_view_index
;
2037 case SYSTEM_VALUE_SUBGROUP_SIZE
:
2038 return nir_intrinsic_load_subgroup_size
;
2039 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
2040 return nir_intrinsic_load_subgroup_invocation
;
2041 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
2042 return nir_intrinsic_load_subgroup_eq_mask
;
2043 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
2044 return nir_intrinsic_load_subgroup_ge_mask
;
2045 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
2046 return nir_intrinsic_load_subgroup_gt_mask
;
2047 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
2048 return nir_intrinsic_load_subgroup_le_mask
;
2049 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
2050 return nir_intrinsic_load_subgroup_lt_mask
;
2051 case SYSTEM_VALUE_NUM_SUBGROUPS
:
2052 return nir_intrinsic_load_num_subgroups
;
2053 case SYSTEM_VALUE_SUBGROUP_ID
:
2054 return nir_intrinsic_load_subgroup_id
;
2055 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
2056 return nir_intrinsic_load_local_group_size
;
2057 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
2058 return nir_intrinsic_load_global_invocation_id
;
2059 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
:
2060 return nir_intrinsic_load_global_invocation_index
;
2061 case SYSTEM_VALUE_WORK_DIM
:
2062 return nir_intrinsic_load_work_dim
;
2063 case SYSTEM_VALUE_USER_DATA_AMD
:
2064 return nir_intrinsic_load_user_data_amd
;
2066 unreachable("system value does not directly correspond to intrinsic");
2071 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
2074 case nir_intrinsic_load_vertex_id
:
2075 return SYSTEM_VALUE_VERTEX_ID
;
2076 case nir_intrinsic_load_instance_id
:
2077 return SYSTEM_VALUE_INSTANCE_ID
;
2078 case nir_intrinsic_load_draw_id
:
2079 return SYSTEM_VALUE_DRAW_ID
;
2080 case nir_intrinsic_load_base_instance
:
2081 return SYSTEM_VALUE_BASE_INSTANCE
;
2082 case nir_intrinsic_load_vertex_id_zero_base
:
2083 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2084 case nir_intrinsic_load_first_vertex
:
2085 return SYSTEM_VALUE_FIRST_VERTEX
;
2086 case nir_intrinsic_load_is_indexed_draw
:
2087 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
2088 case nir_intrinsic_load_base_vertex
:
2089 return SYSTEM_VALUE_BASE_VERTEX
;
2090 case nir_intrinsic_load_invocation_id
:
2091 return SYSTEM_VALUE_INVOCATION_ID
;
2092 case nir_intrinsic_load_frag_coord
:
2093 return SYSTEM_VALUE_FRAG_COORD
;
2094 case nir_intrinsic_load_point_coord
:
2095 return SYSTEM_VALUE_POINT_COORD
;
2096 case nir_intrinsic_load_front_face
:
2097 return SYSTEM_VALUE_FRONT_FACE
;
2098 case nir_intrinsic_load_sample_id
:
2099 return SYSTEM_VALUE_SAMPLE_ID
;
2100 case nir_intrinsic_load_sample_pos
:
2101 return SYSTEM_VALUE_SAMPLE_POS
;
2102 case nir_intrinsic_load_sample_mask_in
:
2103 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
2104 case nir_intrinsic_load_local_invocation_id
:
2105 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
2106 case nir_intrinsic_load_local_invocation_index
:
2107 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
2108 case nir_intrinsic_load_num_work_groups
:
2109 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2110 case nir_intrinsic_load_work_group_id
:
2111 return SYSTEM_VALUE_WORK_GROUP_ID
;
2112 case nir_intrinsic_load_primitive_id
:
2113 return SYSTEM_VALUE_PRIMITIVE_ID
;
2114 case nir_intrinsic_load_tess_coord
:
2115 return SYSTEM_VALUE_TESS_COORD
;
2116 case nir_intrinsic_load_tess_level_outer
:
2117 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2118 case nir_intrinsic_load_tess_level_inner
:
2119 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
2120 case nir_intrinsic_load_tess_level_outer_default
:
2121 return SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
;
2122 case nir_intrinsic_load_tess_level_inner_default
:
2123 return SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
;
2124 case nir_intrinsic_load_patch_vertices_in
:
2125 return SYSTEM_VALUE_VERTICES_IN
;
2126 case nir_intrinsic_load_helper_invocation
:
2127 return SYSTEM_VALUE_HELPER_INVOCATION
;
2128 case nir_intrinsic_load_color0
:
2129 return SYSTEM_VALUE_COLOR0
;
2130 case nir_intrinsic_load_color1
:
2131 return SYSTEM_VALUE_COLOR1
;
2132 case nir_intrinsic_load_view_index
:
2133 return SYSTEM_VALUE_VIEW_INDEX
;
2134 case nir_intrinsic_load_subgroup_size
:
2135 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2136 case nir_intrinsic_load_subgroup_invocation
:
2137 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2138 case nir_intrinsic_load_subgroup_eq_mask
:
2139 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2140 case nir_intrinsic_load_subgroup_ge_mask
:
2141 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2142 case nir_intrinsic_load_subgroup_gt_mask
:
2143 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2144 case nir_intrinsic_load_subgroup_le_mask
:
2145 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2146 case nir_intrinsic_load_subgroup_lt_mask
:
2147 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2148 case nir_intrinsic_load_num_subgroups
:
2149 return SYSTEM_VALUE_NUM_SUBGROUPS
;
2150 case nir_intrinsic_load_subgroup_id
:
2151 return SYSTEM_VALUE_SUBGROUP_ID
;
2152 case nir_intrinsic_load_local_group_size
:
2153 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2154 case nir_intrinsic_load_global_invocation_id
:
2155 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
2156 case nir_intrinsic_load_user_data_amd
:
2157 return SYSTEM_VALUE_USER_DATA_AMD
;
2159 unreachable("intrinsic doesn't produce a system value");
2163 /* OpenGL utility method that remaps the location attributes if they are
2164 * doubles. Not needed for vulkan due the differences on the input location
2165 * count for doubles on vulkan vs OpenGL
2167 * The bitfield returned in dual_slot is one bit for each double input slot in
2168 * the original OpenGL single-slot input numbering. The mapping from old
2169 * locations to new locations is as follows:
2171 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
2174 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
2176 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
2179 nir_foreach_variable(var
, &shader
->inputs
) {
2180 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
2181 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
2182 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
2186 nir_foreach_variable(var
, &shader
->inputs
) {
2187 var
->data
.location
+=
2188 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
2192 /* Returns an attribute mask that has been re-compacted using the given
2196 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
2199 unsigned loc
= u_bit_scan64(&dual_slot
);
2200 /* mask of all bits up to and including loc */
2201 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
2202 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);
2208 nir_rewrite_image_intrinsic(nir_intrinsic_instr
*intrin
, nir_ssa_def
*src
,
2211 enum gl_access_qualifier access
= nir_intrinsic_access(intrin
);
2213 switch (intrin
->intrinsic
) {
2215 case nir_intrinsic_image_deref_##op: \
2216 intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
2217 : nir_intrinsic_image_##op; \
2229 CASE(atomic_exchange
)
2230 CASE(atomic_comp_swap
)
2234 CASE(load_raw_intel
)
2235 CASE(store_raw_intel
)
2238 unreachable("Unhanded image intrinsic");
2241 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
2242 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2244 nir_intrinsic_set_image_dim(intrin
, glsl_get_sampler_dim(deref
->type
));
2245 nir_intrinsic_set_image_array(intrin
, glsl_sampler_type_is_array(deref
->type
));
2246 nir_intrinsic_set_access(intrin
, access
| var
->data
.access
);
2247 nir_intrinsic_set_format(intrin
, var
->data
.image
.format
);
2249 nir_instr_rewrite_src(&intrin
->instr
, &intrin
->src
[0],
2250 nir_src_for_ssa(src
));