2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "nir_control_flow_private.h"
30 #include "util/half_float.h"
36 nir_shader_create(void *mem_ctx
,
37 gl_shader_stage stage
,
38 const nir_shader_compiler_options
*options
,
41 nir_shader
*shader
= rzalloc(mem_ctx
, nir_shader
);
43 exec_list_make_empty(&shader
->uniforms
);
44 exec_list_make_empty(&shader
->inputs
);
45 exec_list_make_empty(&shader
->outputs
);
46 exec_list_make_empty(&shader
->shared
);
48 shader
->options
= options
;
51 assert(si
->stage
== stage
);
54 shader
->info
.stage
= stage
;
57 exec_list_make_empty(&shader
->functions
);
58 exec_list_make_empty(&shader
->registers
);
59 exec_list_make_empty(&shader
->globals
);
60 exec_list_make_empty(&shader
->system_values
);
61 shader
->reg_alloc
= 0;
63 shader
->num_inputs
= 0;
64 shader
->num_outputs
= 0;
65 shader
->num_uniforms
= 0;
66 shader
->num_shared
= 0;
72 reg_create(void *mem_ctx
, struct exec_list
*list
)
74 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
76 list_inithead(®
->uses
);
77 list_inithead(®
->defs
);
78 list_inithead(®
->if_uses
);
80 reg
->num_components
= 0;
82 reg
->num_array_elems
= 0;
83 reg
->is_packed
= false;
86 exec_list_push_tail(list
, ®
->node
);
92 nir_global_reg_create(nir_shader
*shader
)
94 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
95 reg
->index
= shader
->reg_alloc
++;
96 reg
->is_global
= true;
102 nir_local_reg_create(nir_function_impl
*impl
)
104 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
105 reg
->index
= impl
->reg_alloc
++;
106 reg
->is_global
= false;
112 nir_reg_remove(nir_register
*reg
)
114 exec_node_remove(®
->node
);
118 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
120 switch (var
->data
.mode
) {
122 assert(!"invalid mode");
126 assert(!"nir_shader_add_variable cannot be used for local variables");
130 assert(!"nir_shader_add_variable cannot be used for function parameters");
134 exec_list_push_tail(&shader
->globals
, &var
->node
);
137 case nir_var_shader_in
:
138 exec_list_push_tail(&shader
->inputs
, &var
->node
);
141 case nir_var_shader_out
:
142 exec_list_push_tail(&shader
->outputs
, &var
->node
);
145 case nir_var_uniform
:
146 case nir_var_shader_storage
:
147 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
151 assert(shader
->info
.stage
== MESA_SHADER_COMPUTE
);
152 exec_list_push_tail(&shader
->shared
, &var
->node
);
155 case nir_var_system_value
:
156 exec_list_push_tail(&shader
->system_values
, &var
->node
);
162 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
163 const struct glsl_type
*type
, const char *name
)
165 nir_variable
*var
= rzalloc(shader
, nir_variable
);
166 var
->name
= ralloc_strdup(var
, name
);
168 var
->data
.mode
= mode
;
170 if ((mode
== nir_var_shader_in
&&
171 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
172 (mode
== nir_var_shader_out
&&
173 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
174 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
176 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
177 var
->data
.read_only
= true;
179 nir_shader_add_variable(shader
, var
);
185 nir_local_variable_create(nir_function_impl
*impl
,
186 const struct glsl_type
*type
, const char *name
)
188 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
189 var
->name
= ralloc_strdup(var
, name
);
191 var
->data
.mode
= nir_var_local
;
193 nir_function_impl_add_variable(impl
, var
);
199 nir_function_create(nir_shader
*shader
, const char *name
)
201 nir_function
*func
= ralloc(shader
, nir_function
);
203 exec_list_push_tail(&shader
->functions
, &func
->node
);
205 func
->name
= ralloc_strdup(func
, name
);
206 func
->shader
= shader
;
207 func
->num_params
= 0;
209 func
->return_type
= glsl_void_type();
215 /* NOTE: if the instruction you are copying a src to is already added
216 * to the IR, use nir_instr_rewrite_src() instead.
218 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
220 dest
->is_ssa
= src
->is_ssa
;
222 dest
->ssa
= src
->ssa
;
224 dest
->reg
.base_offset
= src
->reg
.base_offset
;
225 dest
->reg
.reg
= src
->reg
.reg
;
226 if (src
->reg
.indirect
) {
227 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
228 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
230 dest
->reg
.indirect
= NULL
;
235 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
237 /* Copying an SSA definition makes no sense whatsoever. */
238 assert(!src
->is_ssa
);
240 dest
->is_ssa
= false;
242 dest
->reg
.base_offset
= src
->reg
.base_offset
;
243 dest
->reg
.reg
= src
->reg
.reg
;
244 if (src
->reg
.indirect
) {
245 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
246 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
248 dest
->reg
.indirect
= NULL
;
253 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
254 nir_alu_instr
*instr
)
256 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
257 dest
->abs
= src
->abs
;
258 dest
->negate
= src
->negate
;
259 for (unsigned i
= 0; i
< 4; i
++)
260 dest
->swizzle
[i
] = src
->swizzle
[i
];
264 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
265 nir_alu_instr
*instr
)
267 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
268 dest
->write_mask
= src
->write_mask
;
269 dest
->saturate
= src
->saturate
;
274 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
276 exec_node_init(&node
->node
);
282 nir_function_impl_create_bare(nir_shader
*shader
)
284 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
286 impl
->function
= NULL
;
288 cf_init(&impl
->cf_node
, nir_cf_node_function
);
290 exec_list_make_empty(&impl
->body
);
291 exec_list_make_empty(&impl
->registers
);
292 exec_list_make_empty(&impl
->locals
);
293 impl
->num_params
= 0;
295 impl
->return_var
= NULL
;
298 impl
->valid_metadata
= nir_metadata_none
;
300 /* create start & end blocks */
301 nir_block
*start_block
= nir_block_create(shader
);
302 nir_block
*end_block
= nir_block_create(shader
);
303 start_block
->cf_node
.parent
= &impl
->cf_node
;
304 end_block
->cf_node
.parent
= &impl
->cf_node
;
305 impl
->end_block
= end_block
;
307 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
309 start_block
->successors
[0] = end_block
;
310 _mesa_set_add(end_block
->predecessors
, start_block
);
315 nir_function_impl_create(nir_function
*function
)
317 assert(function
->impl
== NULL
);
319 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
321 function
->impl
= impl
;
322 impl
->function
= function
;
324 impl
->num_params
= function
->num_params
;
325 impl
->params
= ralloc_array(function
->shader
,
326 nir_variable
*, impl
->num_params
);
328 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
329 impl
->params
[i
] = rzalloc(function
->shader
, nir_variable
);
330 impl
->params
[i
]->type
= function
->params
[i
].type
;
331 impl
->params
[i
]->data
.mode
= nir_var_param
;
332 impl
->params
[i
]->data
.location
= i
;
335 if (!glsl_type_is_void(function
->return_type
)) {
336 impl
->return_var
= rzalloc(function
->shader
, nir_variable
);
337 impl
->return_var
->type
= function
->return_type
;
338 impl
->return_var
->data
.mode
= nir_var_param
;
339 impl
->return_var
->data
.location
= -1;
341 impl
->return_var
= NULL
;
348 nir_block_create(nir_shader
*shader
)
350 nir_block
*block
= rzalloc(shader
, nir_block
);
352 cf_init(&block
->cf_node
, nir_cf_node_block
);
354 block
->successors
[0] = block
->successors
[1] = NULL
;
355 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
356 _mesa_key_pointer_equal
);
357 block
->imm_dom
= NULL
;
358 /* XXX maybe it would be worth it to defer allocation? This
359 * way it doesn't get allocated for shader refs that never run
360 * nir_calc_dominance? For example, state-tracker creates an
361 * initial IR, clones that, runs appropriate lowering pass, passes
362 * to driver which does common lowering/opt, and then stores ref
363 * which is later used to do state specific lowering and futher
364 * opt. Do any of the references not need dominance metadata?
366 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
367 _mesa_key_pointer_equal
);
369 exec_list_make_empty(&block
->instr_list
);
375 src_init(nir_src
*src
)
379 src
->reg
.indirect
= NULL
;
380 src
->reg
.base_offset
= 0;
384 nir_if_create(nir_shader
*shader
)
386 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
388 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
389 src_init(&if_stmt
->condition
);
391 nir_block
*then
= nir_block_create(shader
);
392 exec_list_make_empty(&if_stmt
->then_list
);
393 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
394 then
->cf_node
.parent
= &if_stmt
->cf_node
;
396 nir_block
*else_stmt
= nir_block_create(shader
);
397 exec_list_make_empty(&if_stmt
->else_list
);
398 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
399 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
405 nir_loop_create(nir_shader
*shader
)
407 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
409 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
411 nir_block
*body
= nir_block_create(shader
);
412 exec_list_make_empty(&loop
->body
);
413 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
414 body
->cf_node
.parent
= &loop
->cf_node
;
416 body
->successors
[0] = body
;
417 _mesa_set_add(body
->predecessors
, body
);
423 instr_init(nir_instr
*instr
, nir_instr_type type
)
427 exec_node_init(&instr
->node
);
431 dest_init(nir_dest
*dest
)
433 dest
->is_ssa
= false;
434 dest
->reg
.reg
= NULL
;
435 dest
->reg
.indirect
= NULL
;
436 dest
->reg
.base_offset
= 0;
440 alu_dest_init(nir_alu_dest
*dest
)
442 dest_init(&dest
->dest
);
443 dest
->saturate
= false;
444 dest
->write_mask
= 0xf;
448 alu_src_init(nir_alu_src
*src
)
451 src
->abs
= src
->negate
= false;
459 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
461 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
462 /* TODO: don't use rzalloc */
463 nir_alu_instr
*instr
=
465 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
467 instr_init(&instr
->instr
, nir_instr_type_alu
);
469 alu_dest_init(&instr
->dest
);
470 for (unsigned i
= 0; i
< num_srcs
; i
++)
471 alu_src_init(&instr
->src
[i
]);
477 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
479 nir_deref_instr
*instr
=
480 rzalloc_size(shader
, sizeof(nir_deref_instr
));
482 instr_init(&instr
->instr
, nir_instr_type_deref
);
484 instr
->deref_type
= deref_type
;
485 if (deref_type
!= nir_deref_type_var
)
486 src_init(&instr
->parent
);
488 if (deref_type
== nir_deref_type_array
)
489 src_init(&instr
->arr
.index
);
491 dest_init(&instr
->dest
);
497 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
499 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
500 instr_init(&instr
->instr
, nir_instr_type_jump
);
505 nir_load_const_instr
*
506 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
509 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
510 instr_init(&instr
->instr
, nir_instr_type_load_const
);
512 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
517 nir_intrinsic_instr
*
518 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
520 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
521 /* TODO: don't use rzalloc */
522 nir_intrinsic_instr
*instr
=
524 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
526 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
527 instr
->intrinsic
= op
;
529 if (nir_intrinsic_infos
[op
].has_dest
)
530 dest_init(&instr
->dest
);
532 for (unsigned i
= 0; i
< num_srcs
; i
++)
533 src_init(&instr
->src
[i
]);
539 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
541 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
542 instr_init(&instr
->instr
, nir_instr_type_call
);
544 instr
->callee
= callee
;
545 instr
->num_params
= callee
->num_params
;
546 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
547 instr
->return_deref
= NULL
;
553 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
555 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
556 instr_init(&instr
->instr
, nir_instr_type_tex
);
558 dest_init(&instr
->dest
);
560 instr
->num_srcs
= num_srcs
;
561 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
562 for (unsigned i
= 0; i
< num_srcs
; i
++)
563 src_init(&instr
->src
[i
].src
);
565 instr
->texture_index
= 0;
566 instr
->texture_array_size
= 0;
567 instr
->texture
= NULL
;
568 instr
->sampler_index
= 0;
569 instr
->sampler
= NULL
;
575 nir_tex_instr_add_src(nir_tex_instr
*tex
,
576 nir_tex_src_type src_type
,
579 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
582 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
583 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
584 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
588 ralloc_free(tex
->src
);
591 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
592 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
597 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
599 assert(src_idx
< tex
->num_srcs
);
601 /* First rewrite the source to NIR_SRC_INIT */
602 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
604 /* Now, move all of the other sources down */
605 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
606 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
607 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
613 nir_phi_instr_create(nir_shader
*shader
)
615 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
616 instr_init(&instr
->instr
, nir_instr_type_phi
);
618 dest_init(&instr
->dest
);
619 exec_list_make_empty(&instr
->srcs
);
623 nir_parallel_copy_instr
*
624 nir_parallel_copy_instr_create(nir_shader
*shader
)
626 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
627 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
629 exec_list_make_empty(&instr
->entries
);
634 nir_ssa_undef_instr
*
635 nir_ssa_undef_instr_create(nir_shader
*shader
,
636 unsigned num_components
,
639 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
640 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
642 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
648 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
650 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
651 deref
->deref
.deref_type
= nir_deref_type_var
;
652 deref
->deref
.child
= NULL
;
653 deref
->deref
.type
= var
->type
;
659 nir_deref_array_create(void *mem_ctx
)
661 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
662 deref
->deref
.deref_type
= nir_deref_type_array
;
663 deref
->deref
.child
= NULL
;
664 deref
->deref_array_type
= nir_deref_array_type_direct
;
665 src_init(&deref
->indirect
);
666 deref
->base_offset
= 0;
671 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
673 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
674 deref
->deref
.deref_type
= nir_deref_type_struct
;
675 deref
->deref
.child
= NULL
;
676 deref
->index
= field_index
;
681 nir_deref_var_clone(const nir_deref_var
*deref
, void *mem_ctx
)
686 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
687 ret
->deref
.type
= deref
->deref
.type
;
688 if (deref
->deref
.child
)
689 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
693 static nir_deref_array
*
694 deref_array_clone(const nir_deref_array
*deref
, void *mem_ctx
)
696 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
697 ret
->base_offset
= deref
->base_offset
;
698 ret
->deref_array_type
= deref
->deref_array_type
;
699 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
700 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
702 ret
->deref
.type
= deref
->deref
.type
;
703 if (deref
->deref
.child
)
704 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
708 static nir_deref_struct
*
709 deref_struct_clone(const nir_deref_struct
*deref
, void *mem_ctx
)
711 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
712 ret
->deref
.type
= deref
->deref
.type
;
713 if (deref
->deref
.child
)
714 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
719 nir_deref_clone(const nir_deref
*deref
, void *mem_ctx
)
724 switch (deref
->deref_type
) {
725 case nir_deref_type_var
:
726 return &nir_deref_var_clone(nir_deref_as_var(deref
), mem_ctx
)->deref
;
727 case nir_deref_type_array
:
728 return &deref_array_clone(nir_deref_as_array(deref
), mem_ctx
)->deref
;
729 case nir_deref_type_struct
:
730 return &deref_struct_clone(nir_deref_as_struct(deref
), mem_ctx
)->deref
;
732 unreachable("Invalid dereference type");
738 /* This is the second step in the recursion. We've found the tail and made a
739 * copy. Now we need to iterate over all possible leaves and call the
740 * callback on each one.
743 deref_foreach_leaf_build_recur(nir_deref_var
*deref
, nir_deref
*tail
,
744 nir_deref_foreach_leaf_cb cb
, void *state
)
749 nir_deref_struct str
;
752 assert(tail
->child
== NULL
);
753 switch (glsl_get_base_type(tail
->type
)) {
755 case GLSL_TYPE_UINT16
:
756 case GLSL_TYPE_UINT64
:
758 case GLSL_TYPE_INT16
:
759 case GLSL_TYPE_INT64
:
760 case GLSL_TYPE_FLOAT
:
761 case GLSL_TYPE_FLOAT16
:
762 case GLSL_TYPE_DOUBLE
:
764 if (glsl_type_is_vector_or_scalar(tail
->type
))
765 return cb(deref
, state
);
768 case GLSL_TYPE_ARRAY
:
769 tmp
.arr
.deref
.deref_type
= nir_deref_type_array
;
770 tmp
.arr
.deref
.type
= glsl_get_array_element(tail
->type
);
771 tmp
.arr
.deref_array_type
= nir_deref_array_type_direct
;
772 tmp
.arr
.indirect
= NIR_SRC_INIT
;
773 tail
->child
= &tmp
.arr
.deref
;
775 length
= glsl_get_length(tail
->type
);
776 for (unsigned i
= 0; i
< length
; i
++) {
777 tmp
.arr
.deref
.child
= NULL
;
778 tmp
.arr
.base_offset
= i
;
779 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
784 case GLSL_TYPE_STRUCT
:
785 tmp
.str
.deref
.deref_type
= nir_deref_type_struct
;
786 tail
->child
= &tmp
.str
.deref
;
788 length
= glsl_get_length(tail
->type
);
789 for (unsigned i
= 0; i
< length
; i
++) {
790 tmp
.arr
.deref
.child
= NULL
;
791 tmp
.str
.deref
.type
= glsl_get_struct_field(tail
->type
, i
);
793 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
799 unreachable("Invalid type for dereference");
803 /* This is the first step of the foreach_leaf recursion. In this step we are
804 * walking to the end of the deref chain and making a copy in the stack as we
805 * go. This is because we don't want to mutate the deref chain that was
806 * passed in by the caller. The downside is that this deref chain is on the
807 * stack and , if the caller wants to do anything with it, they will have to
808 * make their own copy because this one will go away.
811 deref_foreach_leaf_copy_recur(nir_deref_var
*deref
, nir_deref
*tail
,
812 nir_deref_foreach_leaf_cb cb
, void *state
)
816 nir_deref_struct str
;
820 switch (tail
->child
->deref_type
) {
821 case nir_deref_type_array
:
822 c
.arr
= *nir_deref_as_array(tail
->child
);
823 tail
->child
= &c
.arr
.deref
;
824 return deref_foreach_leaf_copy_recur(deref
, &c
.arr
.deref
, cb
, state
);
826 case nir_deref_type_struct
:
827 c
.str
= *nir_deref_as_struct(tail
->child
);
828 tail
->child
= &c
.str
.deref
;
829 return deref_foreach_leaf_copy_recur(deref
, &c
.str
.deref
, cb
, state
);
831 case nir_deref_type_var
:
833 unreachable("Invalid deref type for a child");
836 /* We've gotten to the end of the original deref. Time to start
837 * building our own derefs.
839 return deref_foreach_leaf_build_recur(deref
, tail
, cb
, state
);
844 * This function iterates over all of the possible derefs that can be created
845 * with the given deref as the head. It then calls the provided callback with
846 * a full deref for each one.
848 * The deref passed to the callback will be allocated on the stack. You will
849 * need to make a copy if you want it to hang around.
852 nir_deref_foreach_leaf(nir_deref_var
*deref
,
853 nir_deref_foreach_leaf_cb cb
, void *state
)
855 nir_deref_var copy
= *deref
;
856 return deref_foreach_leaf_copy_recur(©
, ©
.deref
, cb
, state
);
859 /* Returns a load_const instruction that represents the constant
860 * initializer for the given deref chain. The caller is responsible for
861 * ensuring that there actually is a constant initializer.
863 nir_load_const_instr
*
864 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
866 nir_constant
*constant
= deref
->var
->constant_initializer
;
869 const nir_deref
*tail
= &deref
->deref
;
870 unsigned matrix_col
= 0;
871 while (tail
->child
) {
872 switch (tail
->child
->deref_type
) {
873 case nir_deref_type_array
: {
874 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
875 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
876 if (glsl_type_is_matrix(tail
->type
)) {
877 assert(arr
->deref
.child
== NULL
);
878 matrix_col
= arr
->base_offset
;
880 constant
= constant
->elements
[arr
->base_offset
];
885 case nir_deref_type_struct
: {
886 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
891 unreachable("Invalid deref child type");
897 unsigned bit_size
= glsl_get_bit_size(tail
->type
);
898 nir_load_const_instr
*load
=
899 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
),
902 switch (glsl_get_base_type(tail
->type
)) {
903 case GLSL_TYPE_FLOAT
:
906 case GLSL_TYPE_FLOAT16
:
907 case GLSL_TYPE_DOUBLE
:
908 case GLSL_TYPE_INT16
:
909 case GLSL_TYPE_UINT16
:
910 case GLSL_TYPE_UINT64
:
911 case GLSL_TYPE_INT64
:
913 load
->value
= constant
->values
[matrix_col
];
916 unreachable("Invalid immediate type");
922 static nir_const_value
923 const_value_float(double d
, unsigned bit_size
)
927 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
928 case 32: v
.f32
[0] = d
; break;
929 case 64: v
.f64
[0] = d
; break;
931 unreachable("Invalid bit size");
936 static nir_const_value
937 const_value_int(int64_t i
, unsigned bit_size
)
941 case 8: v
.i8
[0] = i
; break;
942 case 16: v
.i16
[0] = i
; break;
943 case 32: v
.i32
[0] = i
; break;
944 case 64: v
.i64
[0] = i
; break;
946 unreachable("Invalid bit size");
952 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
954 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
955 const int64_t min_int
= -max_int
- 1;
958 return const_value_int(0, bit_size
);
960 return const_value_float(0, bit_size
);
962 return const_value_int(1, bit_size
);
964 return const_value_float(1, bit_size
);
966 return const_value_int(max_int
, bit_size
);
968 return const_value_int(~0ull, bit_size
);
970 return const_value_float(INFINITY
, bit_size
);
972 return const_value_int(min_int
, bit_size
);
974 return const_value_int(0, bit_size
);
976 return const_value_float(-INFINITY
, bit_size
);
978 return const_value_int(~0ull, bit_size
);
980 return const_value_int(0, bit_size
);
982 return const_value_int(0, bit_size
);
984 unreachable("Invalid reduction operation");
989 nir_cf_node_get_function(nir_cf_node
*node
)
991 while (node
->type
!= nir_cf_node_function
) {
995 return nir_cf_node_as_function(node
);
998 /* Reduces a cursor by trying to convert everything to after and trying to
999 * go up to block granularity when possible.
1002 reduce_cursor(nir_cursor cursor
)
1004 switch (cursor
.option
) {
1005 case nir_cursor_before_block
:
1006 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
1007 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
1008 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
1009 /* Empty block. After is as good as before. */
1010 cursor
.option
= nir_cursor_after_block
;
1014 case nir_cursor_after_block
:
1017 case nir_cursor_before_instr
: {
1018 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
1020 /* Before this instruction is after the previous */
1021 cursor
.instr
= prev_instr
;
1022 cursor
.option
= nir_cursor_after_instr
;
1024 /* No previous instruction. Switch to before block */
1025 cursor
.block
= cursor
.instr
->block
;
1026 cursor
.option
= nir_cursor_before_block
;
1028 return reduce_cursor(cursor
);
1031 case nir_cursor_after_instr
:
1032 if (nir_instr_next(cursor
.instr
) == NULL
) {
1033 /* This is the last instruction, switch to after block */
1034 cursor
.option
= nir_cursor_after_block
;
1035 cursor
.block
= cursor
.instr
->block
;
1040 unreachable("Inavlid cursor option");
1045 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
1047 /* Reduced cursors should be unique */
1048 a
= reduce_cursor(a
);
1049 b
= reduce_cursor(b
);
1051 return a
.block
== b
.block
&& a
.option
== b
.option
;
1055 add_use_cb(nir_src
*src
, void *state
)
1057 nir_instr
*instr
= state
;
1059 src
->parent_instr
= instr
;
1060 list_addtail(&src
->use_link
,
1061 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
1067 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1069 nir_instr
*instr
= state
;
1071 if (instr
->block
&& def
->index
== UINT_MAX
) {
1072 nir_function_impl
*impl
=
1073 nir_cf_node_get_function(&instr
->block
->cf_node
);
1075 def
->index
= impl
->ssa_alloc
++;
1082 add_reg_def_cb(nir_dest
*dest
, void *state
)
1084 nir_instr
*instr
= state
;
1086 if (!dest
->is_ssa
) {
1087 dest
->reg
.parent_instr
= instr
;
1088 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
1095 add_defs_uses(nir_instr
*instr
)
1097 nir_foreach_src(instr
, add_use_cb
, instr
);
1098 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
1099 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
1103 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
1105 switch (cursor
.option
) {
1106 case nir_cursor_before_block
:
1107 /* Only allow inserting jumps into empty blocks. */
1108 if (instr
->type
== nir_instr_type_jump
)
1109 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
1111 instr
->block
= cursor
.block
;
1112 add_defs_uses(instr
);
1113 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
1115 case nir_cursor_after_block
: {
1116 /* Inserting instructions after a jump is illegal. */
1117 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
1118 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
1121 instr
->block
= cursor
.block
;
1122 add_defs_uses(instr
);
1123 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
1126 case nir_cursor_before_instr
:
1127 assert(instr
->type
!= nir_instr_type_jump
);
1128 instr
->block
= cursor
.instr
->block
;
1129 add_defs_uses(instr
);
1130 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
1132 case nir_cursor_after_instr
:
1133 /* Inserting instructions after a jump is illegal. */
1134 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
1136 /* Only allow inserting jumps at the end of the block. */
1137 if (instr
->type
== nir_instr_type_jump
)
1138 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
1140 instr
->block
= cursor
.instr
->block
;
1141 add_defs_uses(instr
);
1142 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
1146 if (instr
->type
== nir_instr_type_jump
)
1147 nir_handle_add_jump(instr
->block
);
1151 src_is_valid(const nir_src
*src
)
1153 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
1157 remove_use_cb(nir_src
*src
, void *state
)
1161 if (src_is_valid(src
))
1162 list_del(&src
->use_link
);
1168 remove_def_cb(nir_dest
*dest
, void *state
)
1173 list_del(&dest
->reg
.def_link
);
1179 remove_defs_uses(nir_instr
*instr
)
1181 nir_foreach_dest(instr
, remove_def_cb
, instr
);
1182 nir_foreach_src(instr
, remove_use_cb
, instr
);
1185 void nir_instr_remove_v(nir_instr
*instr
)
1187 remove_defs_uses(instr
);
1188 exec_node_remove(&instr
->node
);
1190 if (instr
->type
== nir_instr_type_jump
) {
1191 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
1192 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
1199 nir_index_local_regs(nir_function_impl
*impl
)
1202 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1203 reg
->index
= index
++;
1205 impl
->reg_alloc
= index
;
1209 nir_index_global_regs(nir_shader
*shader
)
1212 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1213 reg
->index
= index
++;
1215 shader
->reg_alloc
= index
;
1219 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1221 return cb(&instr
->dest
.dest
, state
);
1225 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1227 return cb(&instr
->dest
, state
);
1231 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
1234 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1235 return cb(&instr
->dest
, state
);
1241 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
1244 return cb(&instr
->dest
, state
);
1248 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1250 return cb(&instr
->dest
, state
);
1254 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
1255 nir_foreach_dest_cb cb
, void *state
)
1257 nir_foreach_parallel_copy_entry(entry
, instr
) {
1258 if (!cb(&entry
->dest
, state
))
1266 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1268 switch (instr
->type
) {
1269 case nir_instr_type_alu
:
1270 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
1271 case nir_instr_type_deref
:
1272 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
1273 case nir_instr_type_intrinsic
:
1274 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
1275 case nir_instr_type_tex
:
1276 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
1277 case nir_instr_type_phi
:
1278 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1279 case nir_instr_type_parallel_copy
:
1280 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1283 case nir_instr_type_load_const
:
1284 case nir_instr_type_ssa_undef
:
1285 case nir_instr_type_call
:
1286 case nir_instr_type_jump
:
1290 unreachable("Invalid instruction type");
1297 struct foreach_ssa_def_state
{
1298 nir_foreach_ssa_def_cb cb
;
1303 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1305 struct foreach_ssa_def_state
*state
= void_state
;
1308 return state
->cb(&dest
->ssa
, state
->client_state
);
1314 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1316 switch (instr
->type
) {
1317 case nir_instr_type_alu
:
1318 case nir_instr_type_deref
:
1319 case nir_instr_type_tex
:
1320 case nir_instr_type_intrinsic
:
1321 case nir_instr_type_phi
:
1322 case nir_instr_type_parallel_copy
: {
1323 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1324 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1327 case nir_instr_type_load_const
:
1328 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1329 case nir_instr_type_ssa_undef
:
1330 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1331 case nir_instr_type_call
:
1332 case nir_instr_type_jump
:
1335 unreachable("Invalid instruction type");
1340 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1342 if (!cb(src
, state
))
1344 if (!src
->is_ssa
&& src
->reg
.indirect
)
1345 return cb(src
->reg
.indirect
, state
);
1350 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
1353 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
1354 return visit_src(&deref
->indirect
, cb
, state
);
1359 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
1361 nir_deref
*cur
= &deref
->deref
;
1362 while (cur
!= NULL
) {
1363 if (cur
->deref_type
== nir_deref_type_array
) {
1364 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
1375 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1377 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1378 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1385 visit_deref_instr_src(nir_deref_instr
*instr
,
1386 nir_foreach_src_cb cb
, void *state
)
1388 if (instr
->deref_type
!= nir_deref_type_var
) {
1389 if (!visit_src(&instr
->parent
, cb
, state
))
1393 if (instr
->deref_type
== nir_deref_type_array
) {
1394 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1402 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1404 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1405 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1409 if (instr
->texture
!= NULL
) {
1410 if (!visit_deref_src(instr
->texture
, cb
, state
))
1414 if (instr
->sampler
!= NULL
) {
1415 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1423 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1426 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1427 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1428 if (!visit_src(&instr
->src
[i
], cb
, state
))
1433 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1434 for (unsigned i
= 0; i
< num_vars
; i
++) {
1435 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1443 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1445 nir_foreach_phi_src(src
, instr
) {
1446 if (!visit_src(&src
->src
, cb
, state
))
1454 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1455 nir_foreach_src_cb cb
, void *state
)
1457 nir_foreach_parallel_copy_entry(entry
, instr
) {
1458 if (!visit_src(&entry
->src
, cb
, state
))
1467 nir_foreach_src_cb cb
;
1468 } visit_dest_indirect_state
;
1471 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1473 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1475 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1476 return state
->cb(dest
->reg
.indirect
, state
->state
);
1482 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1484 switch (instr
->type
) {
1485 case nir_instr_type_alu
:
1486 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1489 case nir_instr_type_deref
:
1490 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1493 case nir_instr_type_intrinsic
:
1494 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1497 case nir_instr_type_tex
:
1498 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1501 case nir_instr_type_call
:
1502 /* Call instructions have no regular sources */
1504 case nir_instr_type_load_const
:
1505 /* Constant load instructions have no regular sources */
1507 case nir_instr_type_phi
:
1508 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1511 case nir_instr_type_parallel_copy
:
1512 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1516 case nir_instr_type_jump
:
1517 case nir_instr_type_ssa_undef
:
1521 unreachable("Invalid instruction type");
1525 visit_dest_indirect_state dest_state
;
1526 dest_state
.state
= state
;
1528 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1532 nir_src_as_const_value(nir_src src
)
1537 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1540 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1542 return &load
->value
;
1546 * Returns true if the source is known to be dynamically uniform. Otherwise it
1547 * returns false which means it may or may not be dynamically uniform but it
1548 * can't be determined.
1551 nir_src_is_dynamically_uniform(nir_src src
)
1556 /* Constants are trivially dynamically uniform */
1557 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1560 /* As are uniform variables */
1561 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1562 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1564 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1568 /* XXX: this could have many more tests, such as when a sampler function is
1569 * called with dynamically uniform arguments.
1575 src_remove_all_uses(nir_src
*src
)
1577 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1578 if (!src_is_valid(src
))
1581 list_del(&src
->use_link
);
1586 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1588 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1589 if (!src_is_valid(src
))
1593 src
->parent_instr
= parent_instr
;
1595 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1597 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1600 src
->parent_if
= parent_if
;
1602 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1604 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1610 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1612 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1614 src_remove_all_uses(src
);
1616 src_add_all_uses(src
, instr
, NULL
);
1620 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1622 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1624 src_remove_all_uses(dest
);
1625 src_remove_all_uses(src
);
1627 *src
= NIR_SRC_INIT
;
1628 src_add_all_uses(dest
, dest_instr
, NULL
);
1632 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1634 nir_src
*src
= &if_stmt
->condition
;
1635 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1637 src_remove_all_uses(src
);
1639 src_add_all_uses(src
, NULL
, if_stmt
);
1643 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1646 /* We can only overwrite an SSA destination if it has no uses. */
1647 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1649 list_del(&dest
->reg
.def_link
);
1650 if (dest
->reg
.indirect
)
1651 src_remove_all_uses(dest
->reg
.indirect
);
1654 /* We can't re-write with an SSA def */
1655 assert(!new_dest
.is_ssa
);
1657 nir_dest_copy(dest
, &new_dest
, instr
);
1659 dest
->reg
.parent_instr
= instr
;
1660 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1662 if (dest
->reg
.indirect
)
1663 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1667 nir_instr_rewrite_deref(nir_instr
*instr
, nir_deref_var
**deref
,
1668 nir_deref_var
*new_deref
)
1671 visit_deref_src(*deref
, remove_use_cb
, NULL
);
1676 visit_deref_src(*deref
, add_use_cb
, instr
);
1679 /* note: does *not* take ownership of 'name' */
1681 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1682 unsigned num_components
,
1683 unsigned bit_size
, const char *name
)
1685 def
->name
= ralloc_strdup(instr
, name
);
1686 def
->parent_instr
= instr
;
1687 list_inithead(&def
->uses
);
1688 list_inithead(&def
->if_uses
);
1689 def
->num_components
= num_components
;
1690 def
->bit_size
= bit_size
;
1693 nir_function_impl
*impl
=
1694 nir_cf_node_get_function(&instr
->block
->cf_node
);
1696 def
->index
= impl
->ssa_alloc
++;
1698 def
->index
= UINT_MAX
;
1702 /* note: does *not* take ownership of 'name' */
1704 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1705 unsigned num_components
, unsigned bit_size
,
1708 dest
->is_ssa
= true;
1709 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1713 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1715 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1717 nir_foreach_use_safe(use_src
, def
)
1718 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1720 nir_foreach_if_use_safe(use_src
, def
)
1721 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1725 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1727 assert(start
->block
== end
->block
);
1729 if (between
->block
!= start
->block
)
1732 /* Search backwards looking for "between" */
1733 while (start
!= end
) {
1737 end
= nir_instr_prev(end
);
1744 /* Replaces all uses of the given SSA def with the given source but only if
1745 * the use comes after the after_me instruction. This can be useful if you
1746 * are emitting code to fix up the result of some instruction: you can freely
1747 * use the result in that code and then call rewrite_uses_after and pass the
1748 * last fixup instruction as after_me and it will replace all of the uses you
1749 * want without touching the fixup code.
1751 * This function assumes that after_me is in the same block as
1752 * def->parent_instr and that after_me comes after def->parent_instr.
1755 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1756 nir_instr
*after_me
)
1758 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1760 nir_foreach_use_safe(use_src
, def
) {
1761 assert(use_src
->parent_instr
!= def
->parent_instr
);
1762 /* Since def already dominates all of its uses, the only way a use can
1763 * not be dominated by after_me is if it is between def and after_me in
1764 * the instruction list.
1766 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1767 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1770 nir_foreach_if_use_safe(use_src
, def
)
1771 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1775 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1777 uint8_t read_mask
= 0;
1778 nir_foreach_use(use
, def
) {
1779 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1780 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1781 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1782 int src_idx
= alu_src
- &alu
->src
[0];
1783 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1785 for (unsigned c
= 0; c
< 4; c
++) {
1786 if (!nir_alu_instr_channel_used(alu
, src_idx
, c
))
1789 read_mask
|= (1 << alu_src
->swizzle
[c
]);
1792 return (1 << def
->num_components
) - 1;
1800 nir_block_cf_tree_next(nir_block
*block
)
1802 if (block
== NULL
) {
1803 /* nir_foreach_block_safe() will call this function on a NULL block
1804 * after the last iteration, but it won't use the result so just return
1810 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1812 return nir_cf_node_cf_tree_first(cf_next
);
1814 nir_cf_node
*parent
= block
->cf_node
.parent
;
1816 switch (parent
->type
) {
1817 case nir_cf_node_if
: {
1818 /* Are we at the end of the if? Go to the beginning of the else */
1819 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1820 if (block
== nir_if_last_then_block(if_stmt
))
1821 return nir_if_first_else_block(if_stmt
);
1823 assert(block
== nir_if_last_else_block(if_stmt
));
1827 case nir_cf_node_loop
:
1828 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1830 case nir_cf_node_function
:
1834 unreachable("unknown cf node type");
1839 nir_block_cf_tree_prev(nir_block
*block
)
1841 if (block
== NULL
) {
1842 /* do this for consistency with nir_block_cf_tree_next() */
1846 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1848 return nir_cf_node_cf_tree_last(cf_prev
);
1850 nir_cf_node
*parent
= block
->cf_node
.parent
;
1852 switch (parent
->type
) {
1853 case nir_cf_node_if
: {
1854 /* Are we at the beginning of the else? Go to the end of the if */
1855 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1856 if (block
== nir_if_first_else_block(if_stmt
))
1857 return nir_if_last_then_block(if_stmt
);
1859 assert(block
== nir_if_first_then_block(if_stmt
));
1863 case nir_cf_node_loop
:
1864 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1866 case nir_cf_node_function
:
1870 unreachable("unknown cf node type");
1874 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1876 switch (node
->type
) {
1877 case nir_cf_node_function
: {
1878 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1879 return nir_start_block(impl
);
1882 case nir_cf_node_if
: {
1883 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1884 return nir_if_first_then_block(if_stmt
);
1887 case nir_cf_node_loop
: {
1888 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1889 return nir_loop_first_block(loop
);
1892 case nir_cf_node_block
: {
1893 return nir_cf_node_as_block(node
);
1897 unreachable("unknown node type");
1901 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1903 switch (node
->type
) {
1904 case nir_cf_node_function
: {
1905 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1906 return nir_impl_last_block(impl
);
1909 case nir_cf_node_if
: {
1910 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1911 return nir_if_last_else_block(if_stmt
);
1914 case nir_cf_node_loop
: {
1915 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1916 return nir_loop_last_block(loop
);
1919 case nir_cf_node_block
: {
1920 return nir_cf_node_as_block(node
);
1924 unreachable("unknown node type");
1928 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1930 if (node
->type
== nir_cf_node_block
)
1931 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1932 else if (node
->type
== nir_cf_node_function
)
1935 return nir_cf_node_as_block(nir_cf_node_next(node
));
1939 nir_block_get_following_if(nir_block
*block
)
1941 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1944 if (nir_cf_node_is_last(&block
->cf_node
))
1947 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1949 if (next_node
->type
!= nir_cf_node_if
)
1952 return nir_cf_node_as_if(next_node
);
1956 nir_block_get_following_loop(nir_block
*block
)
1958 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1961 if (nir_cf_node_is_last(&block
->cf_node
))
1964 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1966 if (next_node
->type
!= nir_cf_node_loop
)
1969 return nir_cf_node_as_loop(next_node
);
1973 nir_index_blocks(nir_function_impl
*impl
)
1977 if (impl
->valid_metadata
& nir_metadata_block_index
)
1980 nir_foreach_block(block
, impl
) {
1981 block
->index
= index
++;
1984 impl
->num_blocks
= index
;
1988 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1990 unsigned *index
= (unsigned *) state
;
1991 def
->index
= (*index
)++;
1997 * The indices are applied top-to-bottom which has the very nice property
1998 * that, if A dominates B, then A->index <= B->index.
2001 nir_index_ssa_defs(nir_function_impl
*impl
)
2005 nir_foreach_block(block
, impl
) {
2006 nir_foreach_instr(instr
, block
)
2007 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
2010 impl
->ssa_alloc
= index
;
2014 * The indices are applied top-to-bottom which has the very nice property
2015 * that, if A dominates B, then A->index <= B->index.
2018 nir_index_instrs(nir_function_impl
*impl
)
2022 nir_foreach_block(block
, impl
) {
2023 nir_foreach_instr(instr
, block
)
2024 instr
->index
= index
++;
2031 nir_intrinsic_from_system_value(gl_system_value val
)
2034 case SYSTEM_VALUE_VERTEX_ID
:
2035 return nir_intrinsic_load_vertex_id
;
2036 case SYSTEM_VALUE_INSTANCE_ID
:
2037 return nir_intrinsic_load_instance_id
;
2038 case SYSTEM_VALUE_DRAW_ID
:
2039 return nir_intrinsic_load_draw_id
;
2040 case SYSTEM_VALUE_BASE_INSTANCE
:
2041 return nir_intrinsic_load_base_instance
;
2042 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
2043 return nir_intrinsic_load_vertex_id_zero_base
;
2044 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
2045 return nir_intrinsic_load_is_indexed_draw
;
2046 case SYSTEM_VALUE_FIRST_VERTEX
:
2047 return nir_intrinsic_load_first_vertex
;
2048 case SYSTEM_VALUE_BASE_VERTEX
:
2049 return nir_intrinsic_load_base_vertex
;
2050 case SYSTEM_VALUE_INVOCATION_ID
:
2051 return nir_intrinsic_load_invocation_id
;
2052 case SYSTEM_VALUE_FRAG_COORD
:
2053 return nir_intrinsic_load_frag_coord
;
2054 case SYSTEM_VALUE_FRONT_FACE
:
2055 return nir_intrinsic_load_front_face
;
2056 case SYSTEM_VALUE_SAMPLE_ID
:
2057 return nir_intrinsic_load_sample_id
;
2058 case SYSTEM_VALUE_SAMPLE_POS
:
2059 return nir_intrinsic_load_sample_pos
;
2060 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
2061 return nir_intrinsic_load_sample_mask_in
;
2062 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
2063 return nir_intrinsic_load_local_invocation_id
;
2064 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
2065 return nir_intrinsic_load_local_invocation_index
;
2066 case SYSTEM_VALUE_WORK_GROUP_ID
:
2067 return nir_intrinsic_load_work_group_id
;
2068 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
2069 return nir_intrinsic_load_num_work_groups
;
2070 case SYSTEM_VALUE_PRIMITIVE_ID
:
2071 return nir_intrinsic_load_primitive_id
;
2072 case SYSTEM_VALUE_TESS_COORD
:
2073 return nir_intrinsic_load_tess_coord
;
2074 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
2075 return nir_intrinsic_load_tess_level_outer
;
2076 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
2077 return nir_intrinsic_load_tess_level_inner
;
2078 case SYSTEM_VALUE_VERTICES_IN
:
2079 return nir_intrinsic_load_patch_vertices_in
;
2080 case SYSTEM_VALUE_HELPER_INVOCATION
:
2081 return nir_intrinsic_load_helper_invocation
;
2082 case SYSTEM_VALUE_VIEW_INDEX
:
2083 return nir_intrinsic_load_view_index
;
2084 case SYSTEM_VALUE_SUBGROUP_SIZE
:
2085 return nir_intrinsic_load_subgroup_size
;
2086 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
2087 return nir_intrinsic_load_subgroup_invocation
;
2088 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
2089 return nir_intrinsic_load_subgroup_eq_mask
;
2090 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
2091 return nir_intrinsic_load_subgroup_ge_mask
;
2092 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
2093 return nir_intrinsic_load_subgroup_gt_mask
;
2094 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
2095 return nir_intrinsic_load_subgroup_le_mask
;
2096 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
2097 return nir_intrinsic_load_subgroup_lt_mask
;
2098 case SYSTEM_VALUE_NUM_SUBGROUPS
:
2099 return nir_intrinsic_load_num_subgroups
;
2100 case SYSTEM_VALUE_SUBGROUP_ID
:
2101 return nir_intrinsic_load_subgroup_id
;
2102 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
2103 return nir_intrinsic_load_local_group_size
;
2104 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
2105 return nir_intrinsic_load_global_invocation_id
;
2107 unreachable("system value does not directly correspond to intrinsic");
2112 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
2115 case nir_intrinsic_load_vertex_id
:
2116 return SYSTEM_VALUE_VERTEX_ID
;
2117 case nir_intrinsic_load_instance_id
:
2118 return SYSTEM_VALUE_INSTANCE_ID
;
2119 case nir_intrinsic_load_draw_id
:
2120 return SYSTEM_VALUE_DRAW_ID
;
2121 case nir_intrinsic_load_base_instance
:
2122 return SYSTEM_VALUE_BASE_INSTANCE
;
2123 case nir_intrinsic_load_vertex_id_zero_base
:
2124 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2125 case nir_intrinsic_load_first_vertex
:
2126 return SYSTEM_VALUE_FIRST_VERTEX
;
2127 case nir_intrinsic_load_is_indexed_draw
:
2128 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
2129 case nir_intrinsic_load_base_vertex
:
2130 return SYSTEM_VALUE_BASE_VERTEX
;
2131 case nir_intrinsic_load_invocation_id
:
2132 return SYSTEM_VALUE_INVOCATION_ID
;
2133 case nir_intrinsic_load_frag_coord
:
2134 return SYSTEM_VALUE_FRAG_COORD
;
2135 case nir_intrinsic_load_front_face
:
2136 return SYSTEM_VALUE_FRONT_FACE
;
2137 case nir_intrinsic_load_sample_id
:
2138 return SYSTEM_VALUE_SAMPLE_ID
;
2139 case nir_intrinsic_load_sample_pos
:
2140 return SYSTEM_VALUE_SAMPLE_POS
;
2141 case nir_intrinsic_load_sample_mask_in
:
2142 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
2143 case nir_intrinsic_load_local_invocation_id
:
2144 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
2145 case nir_intrinsic_load_local_invocation_index
:
2146 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
2147 case nir_intrinsic_load_num_work_groups
:
2148 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2149 case nir_intrinsic_load_work_group_id
:
2150 return SYSTEM_VALUE_WORK_GROUP_ID
;
2151 case nir_intrinsic_load_primitive_id
:
2152 return SYSTEM_VALUE_PRIMITIVE_ID
;
2153 case nir_intrinsic_load_tess_coord
:
2154 return SYSTEM_VALUE_TESS_COORD
;
2155 case nir_intrinsic_load_tess_level_outer
:
2156 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2157 case nir_intrinsic_load_tess_level_inner
:
2158 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
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_view_index
:
2164 return SYSTEM_VALUE_VIEW_INDEX
;
2165 case nir_intrinsic_load_subgroup_size
:
2166 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2167 case nir_intrinsic_load_subgroup_invocation
:
2168 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2169 case nir_intrinsic_load_subgroup_eq_mask
:
2170 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2171 case nir_intrinsic_load_subgroup_ge_mask
:
2172 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2173 case nir_intrinsic_load_subgroup_gt_mask
:
2174 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2175 case nir_intrinsic_load_subgroup_le_mask
:
2176 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2177 case nir_intrinsic_load_subgroup_lt_mask
:
2178 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2179 case nir_intrinsic_load_num_subgroups
:
2180 return SYSTEM_VALUE_NUM_SUBGROUPS
;
2181 case nir_intrinsic_load_subgroup_id
:
2182 return SYSTEM_VALUE_SUBGROUP_ID
;
2183 case nir_intrinsic_load_local_group_size
:
2184 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2185 case nir_intrinsic_load_global_invocation_id
:
2186 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
2188 unreachable("intrinsic doesn't produce a system value");