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_variable_list_for_mode(nir_shader
*shader
, nir_variable_mode mode
)
111 case nir_var_function_temp
:
112 assert(!"nir_shader_add_variable cannot be used for local variables");
115 case nir_var_shader_temp
:
116 return &shader
->globals
;
118 case nir_var_shader_in
:
119 return &shader
->inputs
;
121 case nir_var_shader_out
:
122 return &shader
->outputs
;
124 case nir_var_uniform
:
125 case nir_var_mem_ubo
:
126 case nir_var_mem_ssbo
:
127 return &shader
->uniforms
;
129 case nir_var_mem_shared
:
130 assert(gl_shader_stage_is_compute(shader
->info
.stage
));
131 return &shader
->shared
;
133 case nir_var_mem_global
:
134 assert(!"nir_shader_add_variable cannot be used for global memory");
137 case nir_var_system_value
:
138 return &shader
->system_values
;
140 case nir_var_mem_push_const
:
141 assert(!"nir_var_push_constant is not supposed to be used for variables");
145 assert(!"invalid mode");
151 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
153 struct exec_list
*var_list
=
154 nir_variable_list_for_mode(shader
, var
->data
.mode
);
156 exec_list_push_tail(var_list
, &var
->node
);
160 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
161 const struct glsl_type
*type
, const char *name
)
163 nir_variable
*var
= rzalloc(shader
, nir_variable
);
164 var
->name
= ralloc_strdup(var
, name
);
166 var
->data
.mode
= mode
;
167 var
->data
.how_declared
= nir_var_declared_normally
;
169 if ((mode
== nir_var_shader_in
&&
170 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
171 (mode
== nir_var_shader_out
&&
172 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
173 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
175 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
176 var
->data
.read_only
= true;
178 nir_shader_add_variable(shader
, var
);
184 nir_local_variable_create(nir_function_impl
*impl
,
185 const struct glsl_type
*type
, const char *name
)
187 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
188 var
->name
= ralloc_strdup(var
, name
);
190 var
->data
.mode
= nir_var_function_temp
;
192 nir_function_impl_add_variable(impl
, var
);
198 nir_function_create(nir_shader
*shader
, const char *name
)
200 nir_function
*func
= ralloc(shader
, nir_function
);
202 exec_list_push_tail(&shader
->functions
, &func
->node
);
204 func
->name
= ralloc_strdup(func
, name
);
205 func
->shader
= shader
;
206 func
->num_params
= 0;
209 func
->is_entrypoint
= false;
214 /* NOTE: if the instruction you are copying a src to is already added
215 * to the IR, use nir_instr_rewrite_src() instead.
217 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
219 dest
->is_ssa
= src
->is_ssa
;
221 dest
->ssa
= src
->ssa
;
223 dest
->reg
.base_offset
= src
->reg
.base_offset
;
224 dest
->reg
.reg
= src
->reg
.reg
;
225 if (src
->reg
.indirect
) {
226 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
227 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
229 dest
->reg
.indirect
= NULL
;
234 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
236 /* Copying an SSA definition makes no sense whatsoever. */
237 assert(!src
->is_ssa
);
239 dest
->is_ssa
= false;
241 dest
->reg
.base_offset
= src
->reg
.base_offset
;
242 dest
->reg
.reg
= src
->reg
.reg
;
243 if (src
->reg
.indirect
) {
244 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
245 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
247 dest
->reg
.indirect
= NULL
;
252 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
253 nir_alu_instr
*instr
)
255 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
256 dest
->abs
= src
->abs
;
257 dest
->negate
= src
->negate
;
258 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
259 dest
->swizzle
[i
] = src
->swizzle
[i
];
263 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
264 nir_alu_instr
*instr
)
266 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
267 dest
->write_mask
= src
->write_mask
;
268 dest
->saturate
= src
->saturate
;
273 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
275 exec_node_init(&node
->node
);
281 nir_function_impl_create_bare(nir_shader
*shader
)
283 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
285 impl
->function
= NULL
;
287 cf_init(&impl
->cf_node
, nir_cf_node_function
);
289 exec_list_make_empty(&impl
->body
);
290 exec_list_make_empty(&impl
->registers
);
291 exec_list_make_empty(&impl
->locals
);
294 impl
->valid_metadata
= nir_metadata_none
;
296 /* create start & end blocks */
297 nir_block
*start_block
= nir_block_create(shader
);
298 nir_block
*end_block
= nir_block_create(shader
);
299 start_block
->cf_node
.parent
= &impl
->cf_node
;
300 end_block
->cf_node
.parent
= &impl
->cf_node
;
301 impl
->end_block
= end_block
;
303 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
305 start_block
->successors
[0] = end_block
;
306 _mesa_set_add(end_block
->predecessors
, start_block
);
311 nir_function_impl_create(nir_function
*function
)
313 assert(function
->impl
== NULL
);
315 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
317 function
->impl
= impl
;
318 impl
->function
= function
;
324 nir_block_create(nir_shader
*shader
)
326 nir_block
*block
= rzalloc(shader
, nir_block
);
328 cf_init(&block
->cf_node
, nir_cf_node_block
);
330 block
->successors
[0] = block
->successors
[1] = NULL
;
331 block
->predecessors
= _mesa_pointer_set_create(block
);
332 block
->imm_dom
= NULL
;
333 /* XXX maybe it would be worth it to defer allocation? This
334 * way it doesn't get allocated for shader refs that never run
335 * nir_calc_dominance? For example, state-tracker creates an
336 * initial IR, clones that, runs appropriate lowering pass, passes
337 * to driver which does common lowering/opt, and then stores ref
338 * which is later used to do state specific lowering and futher
339 * opt. Do any of the references not need dominance metadata?
341 block
->dom_frontier
= _mesa_pointer_set_create(block
);
343 exec_list_make_empty(&block
->instr_list
);
349 src_init(nir_src
*src
)
353 src
->reg
.indirect
= NULL
;
354 src
->reg
.base_offset
= 0;
358 nir_if_create(nir_shader
*shader
)
360 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
362 if_stmt
->control
= nir_selection_control_none
;
364 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
365 src_init(&if_stmt
->condition
);
367 nir_block
*then
= nir_block_create(shader
);
368 exec_list_make_empty(&if_stmt
->then_list
);
369 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
370 then
->cf_node
.parent
= &if_stmt
->cf_node
;
372 nir_block
*else_stmt
= nir_block_create(shader
);
373 exec_list_make_empty(&if_stmt
->else_list
);
374 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
375 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
381 nir_loop_create(nir_shader
*shader
)
383 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
385 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
387 nir_block
*body
= nir_block_create(shader
);
388 exec_list_make_empty(&loop
->body
);
389 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
390 body
->cf_node
.parent
= &loop
->cf_node
;
392 body
->successors
[0] = body
;
393 _mesa_set_add(body
->predecessors
, body
);
399 instr_init(nir_instr
*instr
, nir_instr_type type
)
403 exec_node_init(&instr
->node
);
407 dest_init(nir_dest
*dest
)
409 dest
->is_ssa
= false;
410 dest
->reg
.reg
= NULL
;
411 dest
->reg
.indirect
= NULL
;
412 dest
->reg
.base_offset
= 0;
416 alu_dest_init(nir_alu_dest
*dest
)
418 dest_init(&dest
->dest
);
419 dest
->saturate
= false;
420 dest
->write_mask
= 0xf;
424 alu_src_init(nir_alu_src
*src
)
427 src
->abs
= src
->negate
= false;
428 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
433 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
435 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
436 /* TODO: don't use rzalloc */
437 nir_alu_instr
*instr
=
439 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
441 instr_init(&instr
->instr
, nir_instr_type_alu
);
443 alu_dest_init(&instr
->dest
);
444 for (unsigned i
= 0; i
< num_srcs
; i
++)
445 alu_src_init(&instr
->src
[i
]);
451 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
453 nir_deref_instr
*instr
=
454 rzalloc_size(shader
, sizeof(nir_deref_instr
));
456 instr_init(&instr
->instr
, nir_instr_type_deref
);
458 instr
->deref_type
= deref_type
;
459 if (deref_type
!= nir_deref_type_var
)
460 src_init(&instr
->parent
);
462 if (deref_type
== nir_deref_type_array
||
463 deref_type
== nir_deref_type_ptr_as_array
)
464 src_init(&instr
->arr
.index
);
466 dest_init(&instr
->dest
);
472 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
474 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
475 instr_init(&instr
->instr
, nir_instr_type_jump
);
480 nir_load_const_instr
*
481 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
484 nir_load_const_instr
*instr
=
485 rzalloc_size(shader
, sizeof(*instr
) + num_components
* sizeof(*instr
->value
));
486 instr_init(&instr
->instr
, nir_instr_type_load_const
);
488 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
493 nir_intrinsic_instr
*
494 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
496 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
497 /* TODO: don't use rzalloc */
498 nir_intrinsic_instr
*instr
=
500 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
502 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
503 instr
->intrinsic
= op
;
505 if (nir_intrinsic_infos
[op
].has_dest
)
506 dest_init(&instr
->dest
);
508 for (unsigned i
= 0; i
< num_srcs
; i
++)
509 src_init(&instr
->src
[i
]);
515 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
517 const unsigned num_params
= callee
->num_params
;
518 nir_call_instr
*instr
=
519 rzalloc_size(shader
, sizeof(*instr
) +
520 num_params
* sizeof(instr
->params
[0]));
522 instr_init(&instr
->instr
, nir_instr_type_call
);
523 instr
->callee
= callee
;
524 instr
->num_params
= num_params
;
525 for (unsigned i
= 0; i
< num_params
; i
++)
526 src_init(&instr
->params
[i
]);
531 static int8_t default_tg4_offsets
[4][2] =
540 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
542 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
543 instr_init(&instr
->instr
, nir_instr_type_tex
);
545 dest_init(&instr
->dest
);
547 instr
->num_srcs
= num_srcs
;
548 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
549 for (unsigned i
= 0; i
< num_srcs
; i
++)
550 src_init(&instr
->src
[i
].src
);
552 instr
->texture_index
= 0;
553 instr
->sampler_index
= 0;
554 memcpy(instr
->tg4_offsets
, default_tg4_offsets
, sizeof(instr
->tg4_offsets
));
560 nir_tex_instr_add_src(nir_tex_instr
*tex
,
561 nir_tex_src_type src_type
,
564 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
567 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
568 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
569 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
573 ralloc_free(tex
->src
);
576 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
577 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
582 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
584 assert(src_idx
< tex
->num_srcs
);
586 /* First rewrite the source to NIR_SRC_INIT */
587 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
589 /* Now, move all of the other sources down */
590 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
591 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
592 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
598 nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr
*tex
)
600 if (tex
->op
!= nir_texop_tg4
)
602 return memcmp(tex
->tg4_offsets
, default_tg4_offsets
,
603 sizeof(tex
->tg4_offsets
)) != 0;
607 nir_phi_instr_create(nir_shader
*shader
)
609 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
610 instr_init(&instr
->instr
, nir_instr_type_phi
);
612 dest_init(&instr
->dest
);
613 exec_list_make_empty(&instr
->srcs
);
617 nir_parallel_copy_instr
*
618 nir_parallel_copy_instr_create(nir_shader
*shader
)
620 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
621 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
623 exec_list_make_empty(&instr
->entries
);
628 nir_ssa_undef_instr
*
629 nir_ssa_undef_instr_create(nir_shader
*shader
,
630 unsigned num_components
,
633 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
634 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
636 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
641 static nir_const_value
642 const_value_float(double d
, unsigned bit_size
)
645 memset(&v
, 0, sizeof(v
));
647 case 16: v
.u16
= _mesa_float_to_half(d
); break;
648 case 32: v
.f32
= d
; break;
649 case 64: v
.f64
= d
; break;
651 unreachable("Invalid bit size");
656 static nir_const_value
657 const_value_int(int64_t i
, unsigned bit_size
)
660 memset(&v
, 0, sizeof(v
));
662 case 1: v
.b
= i
& 1; break;
663 case 8: v
.i8
= i
; break;
664 case 16: v
.i16
= i
; break;
665 case 32: v
.i32
= i
; break;
666 case 64: v
.i64
= i
; break;
668 unreachable("Invalid bit size");
674 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
676 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
677 const int64_t min_int
= -max_int
- 1;
680 return const_value_int(0, bit_size
);
682 return const_value_float(0, bit_size
);
684 return const_value_int(1, bit_size
);
686 return const_value_float(1, bit_size
);
688 return const_value_int(max_int
, bit_size
);
690 return const_value_int(~0ull, bit_size
);
692 return const_value_float(INFINITY
, bit_size
);
694 return const_value_int(min_int
, bit_size
);
696 return const_value_int(0, bit_size
);
698 return const_value_float(-INFINITY
, bit_size
);
700 return const_value_int(~0ull, bit_size
);
702 return const_value_int(0, bit_size
);
704 return const_value_int(0, bit_size
);
706 unreachable("Invalid reduction operation");
711 nir_cf_node_get_function(nir_cf_node
*node
)
713 while (node
->type
!= nir_cf_node_function
) {
717 return nir_cf_node_as_function(node
);
720 /* Reduces a cursor by trying to convert everything to after and trying to
721 * go up to block granularity when possible.
724 reduce_cursor(nir_cursor cursor
)
726 switch (cursor
.option
) {
727 case nir_cursor_before_block
:
728 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
729 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
730 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
731 /* Empty block. After is as good as before. */
732 cursor
.option
= nir_cursor_after_block
;
736 case nir_cursor_after_block
:
739 case nir_cursor_before_instr
: {
740 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
742 /* Before this instruction is after the previous */
743 cursor
.instr
= prev_instr
;
744 cursor
.option
= nir_cursor_after_instr
;
746 /* No previous instruction. Switch to before block */
747 cursor
.block
= cursor
.instr
->block
;
748 cursor
.option
= nir_cursor_before_block
;
750 return reduce_cursor(cursor
);
753 case nir_cursor_after_instr
:
754 if (nir_instr_next(cursor
.instr
) == NULL
) {
755 /* This is the last instruction, switch to after block */
756 cursor
.option
= nir_cursor_after_block
;
757 cursor
.block
= cursor
.instr
->block
;
762 unreachable("Inavlid cursor option");
767 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
769 /* Reduced cursors should be unique */
770 a
= reduce_cursor(a
);
771 b
= reduce_cursor(b
);
773 return a
.block
== b
.block
&& a
.option
== b
.option
;
777 add_use_cb(nir_src
*src
, void *state
)
779 nir_instr
*instr
= state
;
781 src
->parent_instr
= instr
;
782 list_addtail(&src
->use_link
,
783 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
789 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
791 nir_instr
*instr
= state
;
793 if (instr
->block
&& def
->index
== UINT_MAX
) {
794 nir_function_impl
*impl
=
795 nir_cf_node_get_function(&instr
->block
->cf_node
);
797 def
->index
= impl
->ssa_alloc
++;
804 add_reg_def_cb(nir_dest
*dest
, void *state
)
806 nir_instr
*instr
= state
;
809 dest
->reg
.parent_instr
= instr
;
810 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
817 add_defs_uses(nir_instr
*instr
)
819 nir_foreach_src(instr
, add_use_cb
, instr
);
820 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
821 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
825 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
827 switch (cursor
.option
) {
828 case nir_cursor_before_block
:
829 /* Only allow inserting jumps into empty blocks. */
830 if (instr
->type
== nir_instr_type_jump
)
831 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
833 instr
->block
= cursor
.block
;
834 add_defs_uses(instr
);
835 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
837 case nir_cursor_after_block
: {
838 /* Inserting instructions after a jump is illegal. */
839 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
840 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
843 instr
->block
= cursor
.block
;
844 add_defs_uses(instr
);
845 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
848 case nir_cursor_before_instr
:
849 assert(instr
->type
!= nir_instr_type_jump
);
850 instr
->block
= cursor
.instr
->block
;
851 add_defs_uses(instr
);
852 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
854 case nir_cursor_after_instr
:
855 /* Inserting instructions after a jump is illegal. */
856 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
858 /* Only allow inserting jumps at the end of the block. */
859 if (instr
->type
== nir_instr_type_jump
)
860 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
862 instr
->block
= cursor
.instr
->block
;
863 add_defs_uses(instr
);
864 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
868 if (instr
->type
== nir_instr_type_jump
)
869 nir_handle_add_jump(instr
->block
);
873 src_is_valid(const nir_src
*src
)
875 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
879 remove_use_cb(nir_src
*src
, void *state
)
883 if (src_is_valid(src
))
884 list_del(&src
->use_link
);
890 remove_def_cb(nir_dest
*dest
, void *state
)
895 list_del(&dest
->reg
.def_link
);
901 remove_defs_uses(nir_instr
*instr
)
903 nir_foreach_dest(instr
, remove_def_cb
, instr
);
904 nir_foreach_src(instr
, remove_use_cb
, instr
);
907 void nir_instr_remove_v(nir_instr
*instr
)
909 remove_defs_uses(instr
);
910 exec_node_remove(&instr
->node
);
912 if (instr
->type
== nir_instr_type_jump
) {
913 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
914 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
921 nir_index_local_regs(nir_function_impl
*impl
)
924 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
925 reg
->index
= index
++;
927 impl
->reg_alloc
= index
;
931 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
933 return cb(&instr
->dest
.dest
, state
);
937 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
939 return cb(&instr
->dest
, state
);
943 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
946 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
947 return cb(&instr
->dest
, state
);
953 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
956 return cb(&instr
->dest
, state
);
960 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
962 return cb(&instr
->dest
, state
);
966 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
967 nir_foreach_dest_cb cb
, void *state
)
969 nir_foreach_parallel_copy_entry(entry
, instr
) {
970 if (!cb(&entry
->dest
, state
))
978 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
980 switch (instr
->type
) {
981 case nir_instr_type_alu
:
982 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
983 case nir_instr_type_deref
:
984 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
985 case nir_instr_type_intrinsic
:
986 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
987 case nir_instr_type_tex
:
988 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
989 case nir_instr_type_phi
:
990 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
991 case nir_instr_type_parallel_copy
:
992 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
995 case nir_instr_type_load_const
:
996 case nir_instr_type_ssa_undef
:
997 case nir_instr_type_call
:
998 case nir_instr_type_jump
:
1002 unreachable("Invalid instruction type");
1009 struct foreach_ssa_def_state
{
1010 nir_foreach_ssa_def_cb cb
;
1015 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1017 struct foreach_ssa_def_state
*state
= void_state
;
1020 return state
->cb(&dest
->ssa
, state
->client_state
);
1026 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1028 switch (instr
->type
) {
1029 case nir_instr_type_alu
:
1030 case nir_instr_type_deref
:
1031 case nir_instr_type_tex
:
1032 case nir_instr_type_intrinsic
:
1033 case nir_instr_type_phi
:
1034 case nir_instr_type_parallel_copy
: {
1035 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1036 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1039 case nir_instr_type_load_const
:
1040 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1041 case nir_instr_type_ssa_undef
:
1042 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1043 case nir_instr_type_call
:
1044 case nir_instr_type_jump
:
1047 unreachable("Invalid instruction type");
1052 nir_instr_ssa_def(nir_instr
*instr
)
1054 switch (instr
->type
) {
1055 case nir_instr_type_alu
:
1056 assert(nir_instr_as_alu(instr
)->dest
.dest
.is_ssa
);
1057 return &nir_instr_as_alu(instr
)->dest
.dest
.ssa
;
1059 case nir_instr_type_deref
:
1060 assert(nir_instr_as_deref(instr
)->dest
.is_ssa
);
1061 return &nir_instr_as_deref(instr
)->dest
.ssa
;
1063 case nir_instr_type_tex
:
1064 assert(nir_instr_as_tex(instr
)->dest
.is_ssa
);
1065 return &nir_instr_as_tex(instr
)->dest
.ssa
;
1067 case nir_instr_type_intrinsic
: {
1068 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
1069 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
) {
1070 assert(intrin
->dest
.is_ssa
);
1071 return &intrin
->dest
.ssa
;
1077 case nir_instr_type_phi
:
1078 assert(nir_instr_as_phi(instr
)->dest
.is_ssa
);
1079 return &nir_instr_as_phi(instr
)->dest
.ssa
;
1081 case nir_instr_type_parallel_copy
:
1082 unreachable("Parallel copies are unsupported by this function");
1084 case nir_instr_type_load_const
:
1085 return &nir_instr_as_load_const(instr
)->def
;
1087 case nir_instr_type_ssa_undef
:
1088 return &nir_instr_as_ssa_undef(instr
)->def
;
1090 case nir_instr_type_call
:
1091 case nir_instr_type_jump
:
1095 unreachable("Invalid instruction type");
1099 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1101 if (!cb(src
, state
))
1103 if (!src
->is_ssa
&& src
->reg
.indirect
)
1104 return cb(src
->reg
.indirect
, state
);
1109 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1111 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1112 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1119 visit_deref_instr_src(nir_deref_instr
*instr
,
1120 nir_foreach_src_cb cb
, void *state
)
1122 if (instr
->deref_type
!= nir_deref_type_var
) {
1123 if (!visit_src(&instr
->parent
, cb
, state
))
1127 if (instr
->deref_type
== nir_deref_type_array
||
1128 instr
->deref_type
== nir_deref_type_ptr_as_array
) {
1129 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1137 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1139 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1140 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1148 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1151 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1152 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1153 if (!visit_src(&instr
->src
[i
], cb
, state
))
1161 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1163 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1164 if (!visit_src(&instr
->params
[i
], cb
, state
))
1172 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1174 nir_foreach_phi_src(src
, instr
) {
1175 if (!visit_src(&src
->src
, cb
, state
))
1183 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1184 nir_foreach_src_cb cb
, void *state
)
1186 nir_foreach_parallel_copy_entry(entry
, instr
) {
1187 if (!visit_src(&entry
->src
, cb
, state
))
1196 nir_foreach_src_cb cb
;
1197 } visit_dest_indirect_state
;
1200 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1202 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1204 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1205 return state
->cb(dest
->reg
.indirect
, state
->state
);
1211 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1213 switch (instr
->type
) {
1214 case nir_instr_type_alu
:
1215 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1218 case nir_instr_type_deref
:
1219 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1222 case nir_instr_type_intrinsic
:
1223 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1226 case nir_instr_type_tex
:
1227 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1230 case nir_instr_type_call
:
1231 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1234 case nir_instr_type_load_const
:
1235 /* Constant load instructions have no regular sources */
1237 case nir_instr_type_phi
:
1238 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1241 case nir_instr_type_parallel_copy
:
1242 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1246 case nir_instr_type_jump
:
1247 case nir_instr_type_ssa_undef
:
1251 unreachable("Invalid instruction type");
1255 visit_dest_indirect_state dest_state
;
1256 dest_state
.state
= state
;
1258 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1262 nir_foreach_phi_src_leaving_block(nir_block
*block
,
1263 nir_foreach_src_cb cb
,
1266 for (unsigned i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
1267 if (block
->successors
[i
] == NULL
)
1270 nir_foreach_instr(instr
, block
->successors
[i
]) {
1271 if (instr
->type
!= nir_instr_type_phi
)
1274 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
1275 nir_foreach_phi_src(phi_src
, phi
) {
1276 if (phi_src
->pred
== block
) {
1277 if (!cb(&phi_src
->src
, state
))
1288 nir_const_value_for_float(double f
, unsigned bit_size
)
1291 memset(&v
, 0, sizeof(v
));
1295 v
.u16
= _mesa_float_to_half(f
);
1304 unreachable("Invalid bit size");
1311 nir_const_value_as_float(nir_const_value value
, unsigned bit_size
)
1314 case 16: return _mesa_half_to_float(value
.u16
);
1315 case 32: return value
.f32
;
1316 case 64: return value
.f64
;
1318 unreachable("Invalid bit size");
1323 nir_src_as_const_value(nir_src src
)
1328 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1331 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1337 * Returns true if the source is known to be dynamically uniform. Otherwise it
1338 * returns false which means it may or may not be dynamically uniform but it
1339 * can't be determined.
1342 nir_src_is_dynamically_uniform(nir_src src
)
1347 /* Constants are trivially dynamically uniform */
1348 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1351 /* As are uniform variables */
1352 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1353 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1354 if (intr
->intrinsic
== nir_intrinsic_load_uniform
&&
1355 nir_src_is_dynamically_uniform(intr
->src
[0]))
1359 /* Operating together dynamically uniform expressions produces a
1360 * dynamically uniform result
1362 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
1363 nir_alu_instr
*alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
1364 for (int i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
1365 if (!nir_src_is_dynamically_uniform(alu
->src
[i
].src
))
1372 /* XXX: this could have many more tests, such as when a sampler function is
1373 * called with dynamically uniform arguments.
1379 src_remove_all_uses(nir_src
*src
)
1381 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1382 if (!src_is_valid(src
))
1385 list_del(&src
->use_link
);
1390 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1392 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1393 if (!src_is_valid(src
))
1397 src
->parent_instr
= parent_instr
;
1399 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1401 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1404 src
->parent_if
= parent_if
;
1406 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1408 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1414 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1416 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1418 src_remove_all_uses(src
);
1420 src_add_all_uses(src
, instr
, NULL
);
1424 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1426 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1428 src_remove_all_uses(dest
);
1429 src_remove_all_uses(src
);
1431 *src
= NIR_SRC_INIT
;
1432 src_add_all_uses(dest
, dest_instr
, NULL
);
1436 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1438 nir_src
*src
= &if_stmt
->condition
;
1439 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1441 src_remove_all_uses(src
);
1443 src_add_all_uses(src
, NULL
, if_stmt
);
1447 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1450 /* We can only overwrite an SSA destination if it has no uses. */
1451 assert(list_is_empty(&dest
->ssa
.uses
) && list_is_empty(&dest
->ssa
.if_uses
));
1453 list_del(&dest
->reg
.def_link
);
1454 if (dest
->reg
.indirect
)
1455 src_remove_all_uses(dest
->reg
.indirect
);
1458 /* We can't re-write with an SSA def */
1459 assert(!new_dest
.is_ssa
);
1461 nir_dest_copy(dest
, &new_dest
, instr
);
1463 dest
->reg
.parent_instr
= instr
;
1464 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1466 if (dest
->reg
.indirect
)
1467 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1470 /* note: does *not* take ownership of 'name' */
1472 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1473 unsigned num_components
,
1474 unsigned bit_size
, const char *name
)
1476 def
->name
= ralloc_strdup(instr
, name
);
1477 def
->parent_instr
= instr
;
1478 list_inithead(&def
->uses
);
1479 list_inithead(&def
->if_uses
);
1480 def
->num_components
= num_components
;
1481 def
->bit_size
= bit_size
;
1482 def
->divergent
= true; /* This is the safer default */
1485 nir_function_impl
*impl
=
1486 nir_cf_node_get_function(&instr
->block
->cf_node
);
1488 def
->index
= impl
->ssa_alloc
++;
1490 def
->index
= UINT_MAX
;
1494 /* note: does *not* take ownership of 'name' */
1496 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1497 unsigned num_components
, unsigned bit_size
,
1500 dest
->is_ssa
= true;
1501 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1505 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1507 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1509 nir_foreach_use_safe(use_src
, def
)
1510 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1512 nir_foreach_if_use_safe(use_src
, def
)
1513 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1517 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1519 assert(start
->block
== end
->block
);
1521 if (between
->block
!= start
->block
)
1524 /* Search backwards looking for "between" */
1525 while (start
!= end
) {
1529 end
= nir_instr_prev(end
);
1536 /* Replaces all uses of the given SSA def with the given source but only if
1537 * the use comes after the after_me instruction. This can be useful if you
1538 * are emitting code to fix up the result of some instruction: you can freely
1539 * use the result in that code and then call rewrite_uses_after and pass the
1540 * last fixup instruction as after_me and it will replace all of the uses you
1541 * want without touching the fixup code.
1543 * This function assumes that after_me is in the same block as
1544 * def->parent_instr and that after_me comes after def->parent_instr.
1547 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1548 nir_instr
*after_me
)
1550 if (new_src
.is_ssa
&& def
== new_src
.ssa
)
1553 nir_foreach_use_safe(use_src
, def
) {
1554 assert(use_src
->parent_instr
!= def
->parent_instr
);
1555 /* Since def already dominates all of its uses, the only way a use can
1556 * not be dominated by after_me is if it is between def and after_me in
1557 * the instruction list.
1559 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1560 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1563 nir_foreach_if_use_safe(use_src
, def
)
1564 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1567 nir_component_mask_t
1568 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1570 nir_component_mask_t read_mask
= 0;
1571 nir_foreach_use(use
, def
) {
1572 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1573 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1574 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1575 int src_idx
= alu_src
- &alu
->src
[0];
1576 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1577 read_mask
|= nir_alu_instr_src_read_mask(alu
, src_idx
);
1579 return (1 << def
->num_components
) - 1;
1583 if (!list_is_empty(&def
->if_uses
))
1590 nir_block_cf_tree_next(nir_block
*block
)
1592 if (block
== NULL
) {
1593 /* nir_foreach_block_safe() will call this function on a NULL block
1594 * after the last iteration, but it won't use the result so just return
1600 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1602 return nir_cf_node_cf_tree_first(cf_next
);
1604 nir_cf_node
*parent
= block
->cf_node
.parent
;
1606 switch (parent
->type
) {
1607 case nir_cf_node_if
: {
1608 /* Are we at the end of the if? Go to the beginning of the else */
1609 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1610 if (block
== nir_if_last_then_block(if_stmt
))
1611 return nir_if_first_else_block(if_stmt
);
1613 assert(block
== nir_if_last_else_block(if_stmt
));
1617 case nir_cf_node_loop
:
1618 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1620 case nir_cf_node_function
:
1624 unreachable("unknown cf node type");
1629 nir_block_cf_tree_prev(nir_block
*block
)
1631 if (block
== NULL
) {
1632 /* do this for consistency with nir_block_cf_tree_next() */
1636 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1638 return nir_cf_node_cf_tree_last(cf_prev
);
1640 nir_cf_node
*parent
= block
->cf_node
.parent
;
1642 switch (parent
->type
) {
1643 case nir_cf_node_if
: {
1644 /* Are we at the beginning of the else? Go to the end of the if */
1645 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1646 if (block
== nir_if_first_else_block(if_stmt
))
1647 return nir_if_last_then_block(if_stmt
);
1649 assert(block
== nir_if_first_then_block(if_stmt
));
1653 case nir_cf_node_loop
:
1654 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1656 case nir_cf_node_function
:
1660 unreachable("unknown cf node type");
1664 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1666 switch (node
->type
) {
1667 case nir_cf_node_function
: {
1668 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1669 return nir_start_block(impl
);
1672 case nir_cf_node_if
: {
1673 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1674 return nir_if_first_then_block(if_stmt
);
1677 case nir_cf_node_loop
: {
1678 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1679 return nir_loop_first_block(loop
);
1682 case nir_cf_node_block
: {
1683 return nir_cf_node_as_block(node
);
1687 unreachable("unknown node type");
1691 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1693 switch (node
->type
) {
1694 case nir_cf_node_function
: {
1695 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1696 return nir_impl_last_block(impl
);
1699 case nir_cf_node_if
: {
1700 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1701 return nir_if_last_else_block(if_stmt
);
1704 case nir_cf_node_loop
: {
1705 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1706 return nir_loop_last_block(loop
);
1709 case nir_cf_node_block
: {
1710 return nir_cf_node_as_block(node
);
1714 unreachable("unknown node type");
1718 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1720 if (node
->type
== nir_cf_node_block
)
1721 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1722 else if (node
->type
== nir_cf_node_function
)
1725 return nir_cf_node_as_block(nir_cf_node_next(node
));
1729 nir_block_get_following_if(nir_block
*block
)
1731 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1734 if (nir_cf_node_is_last(&block
->cf_node
))
1737 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1739 if (next_node
->type
!= nir_cf_node_if
)
1742 return nir_cf_node_as_if(next_node
);
1746 nir_block_get_following_loop(nir_block
*block
)
1748 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1751 if (nir_cf_node_is_last(&block
->cf_node
))
1754 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1756 if (next_node
->type
!= nir_cf_node_loop
)
1759 return nir_cf_node_as_loop(next_node
);
1763 nir_index_blocks(nir_function_impl
*impl
)
1767 if (impl
->valid_metadata
& nir_metadata_block_index
)
1770 nir_foreach_block(block
, impl
) {
1771 block
->index
= index
++;
1774 /* The end_block isn't really part of the program, which is why its index
1777 impl
->num_blocks
= impl
->end_block
->index
= index
;
1781 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1783 unsigned *index
= (unsigned *) state
;
1784 def
->index
= (*index
)++;
1790 * The indices are applied top-to-bottom which has the very nice property
1791 * that, if A dominates B, then A->index <= B->index.
1794 nir_index_ssa_defs(nir_function_impl
*impl
)
1798 nir_foreach_block(block
, impl
) {
1799 nir_foreach_instr(instr
, block
)
1800 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1803 impl
->ssa_alloc
= index
;
1807 * The indices are applied top-to-bottom which has the very nice property
1808 * that, if A dominates B, then A->index <= B->index.
1811 nir_index_instrs(nir_function_impl
*impl
)
1815 nir_foreach_block(block
, impl
) {
1816 nir_foreach_instr(instr
, block
)
1817 instr
->index
= index
++;
1824 index_var_list(struct exec_list
*list
)
1826 unsigned next_index
= 0;
1827 nir_foreach_variable(var
, list
)
1828 var
->index
= next_index
++;
1832 nir_index_vars(nir_shader
*shader
, nir_function_impl
*impl
, nir_variable_mode modes
)
1834 if ((modes
& nir_var_function_temp
) && impl
)
1835 index_var_list(&impl
->locals
);
1837 if (modes
& nir_var_shader_temp
)
1838 index_var_list(&shader
->globals
);
1840 if (modes
& nir_var_shader_in
)
1841 index_var_list(&shader
->inputs
);
1843 if (modes
& nir_var_shader_out
)
1844 index_var_list(&shader
->outputs
);
1846 if (modes
& (nir_var_uniform
| nir_var_mem_ubo
| nir_var_mem_ssbo
))
1847 index_var_list(&shader
->uniforms
);
1849 if (modes
& nir_var_mem_shared
)
1850 index_var_list(&shader
->shared
);
1852 if (modes
& nir_var_system_value
)
1853 index_var_list(&shader
->system_values
);
1857 cursor_next_instr(nir_cursor cursor
)
1859 switch (cursor
.option
) {
1860 case nir_cursor_before_block
:
1861 for (nir_block
*block
= cursor
.block
; block
;
1862 block
= nir_block_cf_tree_next(block
)) {
1863 nir_instr
*instr
= nir_block_first_instr(block
);
1869 case nir_cursor_after_block
:
1870 cursor
.block
= nir_block_cf_tree_next(cursor
.block
);
1871 if (cursor
.block
== NULL
)
1874 cursor
.option
= nir_cursor_before_block
;
1875 return cursor_next_instr(cursor
);
1877 case nir_cursor_before_instr
:
1878 return cursor
.instr
;
1880 case nir_cursor_after_instr
:
1881 if (nir_instr_next(cursor
.instr
))
1882 return nir_instr_next(cursor
.instr
);
1884 cursor
.option
= nir_cursor_after_block
;
1885 cursor
.block
= cursor
.instr
->block
;
1886 return cursor_next_instr(cursor
);
1889 unreachable("Inavlid cursor option");
1892 ASSERTED
static bool
1893 dest_is_ssa(nir_dest
*dest
, void *_state
)
1896 return dest
->is_ssa
;
1900 nir_function_impl_lower_instructions(nir_function_impl
*impl
,
1901 nir_instr_filter_cb filter
,
1902 nir_lower_instr_cb lower
,
1906 nir_builder_init(&b
, impl
);
1908 nir_metadata preserved
= nir_metadata_block_index
|
1909 nir_metadata_dominance
;
1911 bool progress
= false;
1912 nir_cursor iter
= nir_before_cf_list(&impl
->body
);
1914 while ((instr
= cursor_next_instr(iter
)) != NULL
) {
1915 if (filter
&& !filter(instr
, cb_data
)) {
1916 iter
= nir_after_instr(instr
);
1920 assert(nir_foreach_dest(instr
, dest_is_ssa
, NULL
));
1921 nir_ssa_def
*old_def
= nir_instr_ssa_def(instr
);
1922 if (old_def
== NULL
) {
1923 iter
= nir_after_instr(instr
);
1927 /* We're about to ask the callback to generate a replacement for instr.
1928 * Save off the uses from instr's SSA def so we know what uses to
1929 * rewrite later. If we use nir_ssa_def_rewrite_uses, it fails in the
1930 * case where the generated replacement code uses the result of instr
1931 * itself. If we use nir_ssa_def_rewrite_uses_after (which is the
1932 * normal solution to this problem), it doesn't work well if control-
1933 * flow is inserted as part of the replacement, doesn't handle cases
1934 * where the replacement is something consumed by instr, and suffers
1935 * from performance issues. This is the only way to 100% guarantee
1936 * that we rewrite the correct set efficiently.
1938 struct list_head old_uses
, old_if_uses
;
1939 list_replace(&old_def
->uses
, &old_uses
);
1940 list_inithead(&old_def
->uses
);
1941 list_replace(&old_def
->if_uses
, &old_if_uses
);
1942 list_inithead(&old_def
->if_uses
);
1944 b
.cursor
= nir_after_instr(instr
);
1945 nir_ssa_def
*new_def
= lower(&b
, instr
, cb_data
);
1946 if (new_def
&& new_def
!= NIR_LOWER_INSTR_PROGRESS
) {
1947 assert(old_def
!= NULL
);
1948 if (new_def
->parent_instr
->block
!= instr
->block
)
1949 preserved
= nir_metadata_none
;
1951 nir_src new_src
= nir_src_for_ssa(new_def
);
1952 list_for_each_entry_safe(nir_src
, use_src
, &old_uses
, use_link
)
1953 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1955 list_for_each_entry_safe(nir_src
, use_src
, &old_if_uses
, use_link
)
1956 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1958 if (list_is_empty(&old_def
->uses
) && list_is_empty(&old_def
->if_uses
)) {
1959 iter
= nir_instr_remove(instr
);
1961 iter
= nir_after_instr(instr
);
1965 /* We didn't end up lowering after all. Put the uses back */
1967 list_replace(&old_uses
, &old_def
->uses
);
1968 list_replace(&old_if_uses
, &old_def
->if_uses
);
1970 iter
= nir_after_instr(instr
);
1972 if (new_def
== NIR_LOWER_INSTR_PROGRESS
)
1978 nir_metadata_preserve(impl
, preserved
);
1980 nir_metadata_preserve(impl
, nir_metadata_all
);
1987 nir_shader_lower_instructions(nir_shader
*shader
,
1988 nir_instr_filter_cb filter
,
1989 nir_lower_instr_cb lower
,
1992 bool progress
= false;
1994 nir_foreach_function(function
, shader
) {
1995 if (function
->impl
&&
1996 nir_function_impl_lower_instructions(function
->impl
,
1997 filter
, lower
, cb_data
))
2005 nir_intrinsic_from_system_value(gl_system_value val
)
2008 case SYSTEM_VALUE_VERTEX_ID
:
2009 return nir_intrinsic_load_vertex_id
;
2010 case SYSTEM_VALUE_INSTANCE_ID
:
2011 return nir_intrinsic_load_instance_id
;
2012 case SYSTEM_VALUE_DRAW_ID
:
2013 return nir_intrinsic_load_draw_id
;
2014 case SYSTEM_VALUE_BASE_INSTANCE
:
2015 return nir_intrinsic_load_base_instance
;
2016 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
2017 return nir_intrinsic_load_vertex_id_zero_base
;
2018 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
2019 return nir_intrinsic_load_is_indexed_draw
;
2020 case SYSTEM_VALUE_FIRST_VERTEX
:
2021 return nir_intrinsic_load_first_vertex
;
2022 case SYSTEM_VALUE_BASE_VERTEX
:
2023 return nir_intrinsic_load_base_vertex
;
2024 case SYSTEM_VALUE_INVOCATION_ID
:
2025 return nir_intrinsic_load_invocation_id
;
2026 case SYSTEM_VALUE_FRAG_COORD
:
2027 return nir_intrinsic_load_frag_coord
;
2028 case SYSTEM_VALUE_POINT_COORD
:
2029 return nir_intrinsic_load_point_coord
;
2030 case SYSTEM_VALUE_LINE_COORD
:
2031 return nir_intrinsic_load_line_coord
;
2032 case SYSTEM_VALUE_FRONT_FACE
:
2033 return nir_intrinsic_load_front_face
;
2034 case SYSTEM_VALUE_SAMPLE_ID
:
2035 return nir_intrinsic_load_sample_id
;
2036 case SYSTEM_VALUE_SAMPLE_POS
:
2037 return nir_intrinsic_load_sample_pos
;
2038 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
2039 return nir_intrinsic_load_sample_mask_in
;
2040 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
2041 return nir_intrinsic_load_local_invocation_id
;
2042 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
2043 return nir_intrinsic_load_local_invocation_index
;
2044 case SYSTEM_VALUE_WORK_GROUP_ID
:
2045 return nir_intrinsic_load_work_group_id
;
2046 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
2047 return nir_intrinsic_load_num_work_groups
;
2048 case SYSTEM_VALUE_PRIMITIVE_ID
:
2049 return nir_intrinsic_load_primitive_id
;
2050 case SYSTEM_VALUE_TESS_COORD
:
2051 return nir_intrinsic_load_tess_coord
;
2052 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
2053 return nir_intrinsic_load_tess_level_outer
;
2054 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
2055 return nir_intrinsic_load_tess_level_inner
;
2056 case SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
:
2057 return nir_intrinsic_load_tess_level_outer_default
;
2058 case SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
:
2059 return nir_intrinsic_load_tess_level_inner_default
;
2060 case SYSTEM_VALUE_VERTICES_IN
:
2061 return nir_intrinsic_load_patch_vertices_in
;
2062 case SYSTEM_VALUE_HELPER_INVOCATION
:
2063 return nir_intrinsic_load_helper_invocation
;
2064 case SYSTEM_VALUE_COLOR0
:
2065 return nir_intrinsic_load_color0
;
2066 case SYSTEM_VALUE_COLOR1
:
2067 return nir_intrinsic_load_color1
;
2068 case SYSTEM_VALUE_VIEW_INDEX
:
2069 return nir_intrinsic_load_view_index
;
2070 case SYSTEM_VALUE_SUBGROUP_SIZE
:
2071 return nir_intrinsic_load_subgroup_size
;
2072 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
2073 return nir_intrinsic_load_subgroup_invocation
;
2074 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
2075 return nir_intrinsic_load_subgroup_eq_mask
;
2076 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
2077 return nir_intrinsic_load_subgroup_ge_mask
;
2078 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
2079 return nir_intrinsic_load_subgroup_gt_mask
;
2080 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
2081 return nir_intrinsic_load_subgroup_le_mask
;
2082 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
2083 return nir_intrinsic_load_subgroup_lt_mask
;
2084 case SYSTEM_VALUE_NUM_SUBGROUPS
:
2085 return nir_intrinsic_load_num_subgroups
;
2086 case SYSTEM_VALUE_SUBGROUP_ID
:
2087 return nir_intrinsic_load_subgroup_id
;
2088 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
2089 return nir_intrinsic_load_local_group_size
;
2090 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
2091 return nir_intrinsic_load_global_invocation_id
;
2092 case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
:
2093 return nir_intrinsic_load_global_invocation_index
;
2094 case SYSTEM_VALUE_WORK_DIM
:
2095 return nir_intrinsic_load_work_dim
;
2096 case SYSTEM_VALUE_USER_DATA_AMD
:
2097 return nir_intrinsic_load_user_data_amd
;
2099 unreachable("system value does not directly correspond to intrinsic");
2104 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
2107 case nir_intrinsic_load_vertex_id
:
2108 return SYSTEM_VALUE_VERTEX_ID
;
2109 case nir_intrinsic_load_instance_id
:
2110 return SYSTEM_VALUE_INSTANCE_ID
;
2111 case nir_intrinsic_load_draw_id
:
2112 return SYSTEM_VALUE_DRAW_ID
;
2113 case nir_intrinsic_load_base_instance
:
2114 return SYSTEM_VALUE_BASE_INSTANCE
;
2115 case nir_intrinsic_load_vertex_id_zero_base
:
2116 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2117 case nir_intrinsic_load_first_vertex
:
2118 return SYSTEM_VALUE_FIRST_VERTEX
;
2119 case nir_intrinsic_load_is_indexed_draw
:
2120 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
2121 case nir_intrinsic_load_base_vertex
:
2122 return SYSTEM_VALUE_BASE_VERTEX
;
2123 case nir_intrinsic_load_invocation_id
:
2124 return SYSTEM_VALUE_INVOCATION_ID
;
2125 case nir_intrinsic_load_frag_coord
:
2126 return SYSTEM_VALUE_FRAG_COORD
;
2127 case nir_intrinsic_load_point_coord
:
2128 return SYSTEM_VALUE_POINT_COORD
;
2129 case nir_intrinsic_load_line_coord
:
2130 return SYSTEM_VALUE_LINE_COORD
;
2131 case nir_intrinsic_load_front_face
:
2132 return SYSTEM_VALUE_FRONT_FACE
;
2133 case nir_intrinsic_load_sample_id
:
2134 return SYSTEM_VALUE_SAMPLE_ID
;
2135 case nir_intrinsic_load_sample_pos
:
2136 return SYSTEM_VALUE_SAMPLE_POS
;
2137 case nir_intrinsic_load_sample_mask_in
:
2138 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
2139 case nir_intrinsic_load_local_invocation_id
:
2140 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
2141 case nir_intrinsic_load_local_invocation_index
:
2142 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
2143 case nir_intrinsic_load_num_work_groups
:
2144 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2145 case nir_intrinsic_load_work_group_id
:
2146 return SYSTEM_VALUE_WORK_GROUP_ID
;
2147 case nir_intrinsic_load_primitive_id
:
2148 return SYSTEM_VALUE_PRIMITIVE_ID
;
2149 case nir_intrinsic_load_tess_coord
:
2150 return SYSTEM_VALUE_TESS_COORD
;
2151 case nir_intrinsic_load_tess_level_outer
:
2152 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2153 case nir_intrinsic_load_tess_level_inner
:
2154 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
2155 case nir_intrinsic_load_tess_level_outer_default
:
2156 return SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT
;
2157 case nir_intrinsic_load_tess_level_inner_default
:
2158 return SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT
;
2159 case nir_intrinsic_load_patch_vertices_in
:
2160 return SYSTEM_VALUE_VERTICES_IN
;
2161 case nir_intrinsic_load_helper_invocation
:
2162 return SYSTEM_VALUE_HELPER_INVOCATION
;
2163 case nir_intrinsic_load_color0
:
2164 return SYSTEM_VALUE_COLOR0
;
2165 case nir_intrinsic_load_color1
:
2166 return SYSTEM_VALUE_COLOR1
;
2167 case nir_intrinsic_load_view_index
:
2168 return SYSTEM_VALUE_VIEW_INDEX
;
2169 case nir_intrinsic_load_subgroup_size
:
2170 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2171 case nir_intrinsic_load_subgroup_invocation
:
2172 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2173 case nir_intrinsic_load_subgroup_eq_mask
:
2174 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2175 case nir_intrinsic_load_subgroup_ge_mask
:
2176 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2177 case nir_intrinsic_load_subgroup_gt_mask
:
2178 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2179 case nir_intrinsic_load_subgroup_le_mask
:
2180 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2181 case nir_intrinsic_load_subgroup_lt_mask
:
2182 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2183 case nir_intrinsic_load_num_subgroups
:
2184 return SYSTEM_VALUE_NUM_SUBGROUPS
;
2185 case nir_intrinsic_load_subgroup_id
:
2186 return SYSTEM_VALUE_SUBGROUP_ID
;
2187 case nir_intrinsic_load_local_group_size
:
2188 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2189 case nir_intrinsic_load_global_invocation_id
:
2190 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
2191 case nir_intrinsic_load_user_data_amd
:
2192 return SYSTEM_VALUE_USER_DATA_AMD
;
2194 unreachable("intrinsic doesn't produce a system value");
2198 /* OpenGL utility method that remaps the location attributes if they are
2199 * doubles. Not needed for vulkan due the differences on the input location
2200 * count for doubles on vulkan vs OpenGL
2202 * The bitfield returned in dual_slot is one bit for each double input slot in
2203 * the original OpenGL single-slot input numbering. The mapping from old
2204 * locations to new locations is as follows:
2206 * new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
2209 nir_remap_dual_slot_attributes(nir_shader
*shader
, uint64_t *dual_slot
)
2211 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
);
2214 nir_foreach_shader_in_variable(var
, shader
) {
2215 if (glsl_type_is_dual_slot(glsl_without_array(var
->type
))) {
2216 unsigned slots
= glsl_count_attribute_slots(var
->type
, true);
2217 *dual_slot
|= BITFIELD64_MASK(slots
) << var
->data
.location
;
2221 nir_foreach_shader_in_variable(var
, shader
) {
2222 var
->data
.location
+=
2223 util_bitcount64(*dual_slot
& BITFIELD64_MASK(var
->data
.location
));
2227 /* Returns an attribute mask that has been re-compacted using the given
2231 nir_get_single_slot_attribs_mask(uint64_t attribs
, uint64_t dual_slot
)
2234 unsigned loc
= u_bit_scan64(&dual_slot
);
2235 /* mask of all bits up to and including loc */
2236 uint64_t mask
= BITFIELD64_MASK(loc
+ 1);
2237 attribs
= (attribs
& mask
) | ((attribs
& ~mask
) >> 1);
2243 nir_rewrite_image_intrinsic(nir_intrinsic_instr
*intrin
, nir_ssa_def
*src
,
2246 enum gl_access_qualifier access
= nir_intrinsic_access(intrin
);
2248 switch (intrin
->intrinsic
) {
2250 case nir_intrinsic_image_deref_##op: \
2251 intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
2252 : nir_intrinsic_image_##op; \
2264 CASE(atomic_exchange
)
2265 CASE(atomic_comp_swap
)
2267 CASE(atomic_inc_wrap
)
2268 CASE(atomic_dec_wrap
)
2271 CASE(load_raw_intel
)
2272 CASE(store_raw_intel
)
2275 unreachable("Unhanded image intrinsic");
2278 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
2279 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2281 nir_intrinsic_set_image_dim(intrin
, glsl_get_sampler_dim(deref
->type
));
2282 nir_intrinsic_set_image_array(intrin
, glsl_sampler_type_is_array(deref
->type
));
2283 nir_intrinsic_set_access(intrin
, access
| var
->data
.access
);
2284 nir_intrinsic_set_format(intrin
, var
->data
.image
.format
);
2286 nir_instr_rewrite_src(&intrin
->instr
, &intrin
->src
[0],
2287 nir_src_for_ssa(src
));
2291 nir_image_intrinsic_coord_components(const nir_intrinsic_instr
*instr
)
2293 enum glsl_sampler_dim dim
= nir_intrinsic_image_dim(instr
);
2294 int coords
= glsl_get_sampler_dim_coordinate_components(dim
);
2295 if (dim
== GLSL_SAMPLER_DIM_CUBE
)
2298 return coords
+ nir_intrinsic_image_array(instr
);