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"
35 nir_shader_create(void *mem_ctx
,
36 gl_shader_stage stage
,
37 const nir_shader_compiler_options
*options
,
40 nir_shader
*shader
= rzalloc(mem_ctx
, nir_shader
);
42 exec_list_make_empty(&shader
->uniforms
);
43 exec_list_make_empty(&shader
->inputs
);
44 exec_list_make_empty(&shader
->outputs
);
45 exec_list_make_empty(&shader
->shared
);
47 shader
->options
= options
;
50 assert(si
->stage
== stage
);
53 shader
->info
.stage
= stage
;
56 exec_list_make_empty(&shader
->functions
);
57 exec_list_make_empty(&shader
->registers
);
58 exec_list_make_empty(&shader
->globals
);
59 exec_list_make_empty(&shader
->system_values
);
60 shader
->reg_alloc
= 0;
62 shader
->num_inputs
= 0;
63 shader
->num_outputs
= 0;
64 shader
->num_uniforms
= 0;
65 shader
->num_shared
= 0;
71 reg_create(void *mem_ctx
, struct exec_list
*list
)
73 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
75 list_inithead(®
->uses
);
76 list_inithead(®
->defs
);
77 list_inithead(®
->if_uses
);
79 reg
->num_components
= 0;
81 reg
->num_array_elems
= 0;
82 reg
->is_packed
= false;
85 exec_list_push_tail(list
, ®
->node
);
91 nir_global_reg_create(nir_shader
*shader
)
93 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
94 reg
->index
= shader
->reg_alloc
++;
95 reg
->is_global
= true;
101 nir_local_reg_create(nir_function_impl
*impl
)
103 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
104 reg
->index
= impl
->reg_alloc
++;
105 reg
->is_global
= false;
111 nir_reg_remove(nir_register
*reg
)
113 exec_node_remove(®
->node
);
117 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
119 switch (var
->data
.mode
) {
121 assert(!"invalid mode");
125 assert(!"nir_shader_add_variable cannot be used for local variables");
129 assert(!"nir_shader_add_variable cannot be used for function parameters");
133 exec_list_push_tail(&shader
->globals
, &var
->node
);
136 case nir_var_shader_in
:
137 exec_list_push_tail(&shader
->inputs
, &var
->node
);
140 case nir_var_shader_out
:
141 exec_list_push_tail(&shader
->outputs
, &var
->node
);
144 case nir_var_uniform
:
145 case nir_var_shader_storage
:
146 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
150 assert(shader
->info
.stage
== MESA_SHADER_COMPUTE
);
151 exec_list_push_tail(&shader
->shared
, &var
->node
);
154 case nir_var_system_value
:
155 exec_list_push_tail(&shader
->system_values
, &var
->node
);
161 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
162 const struct glsl_type
*type
, const char *name
)
164 nir_variable
*var
= rzalloc(shader
, nir_variable
);
165 var
->name
= ralloc_strdup(var
, name
);
167 var
->data
.mode
= mode
;
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_local
;
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;
208 func
->return_type
= glsl_void_type();
214 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
216 dest
->is_ssa
= src
->is_ssa
;
218 dest
->ssa
= src
->ssa
;
220 dest
->reg
.base_offset
= src
->reg
.base_offset
;
221 dest
->reg
.reg
= src
->reg
.reg
;
222 if (src
->reg
.indirect
) {
223 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
224 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
226 dest
->reg
.indirect
= NULL
;
231 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
233 /* Copying an SSA definition makes no sense whatsoever. */
234 assert(!src
->is_ssa
);
236 dest
->is_ssa
= false;
238 dest
->reg
.base_offset
= src
->reg
.base_offset
;
239 dest
->reg
.reg
= src
->reg
.reg
;
240 if (src
->reg
.indirect
) {
241 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
242 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
244 dest
->reg
.indirect
= NULL
;
249 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
250 nir_alu_instr
*instr
)
252 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
253 dest
->abs
= src
->abs
;
254 dest
->negate
= src
->negate
;
255 for (unsigned i
= 0; i
< 4; i
++)
256 dest
->swizzle
[i
] = src
->swizzle
[i
];
260 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
261 nir_alu_instr
*instr
)
263 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
264 dest
->write_mask
= src
->write_mask
;
265 dest
->saturate
= src
->saturate
;
270 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
272 exec_node_init(&node
->node
);
278 nir_function_impl_create_bare(nir_shader
*shader
)
280 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
282 impl
->function
= NULL
;
284 cf_init(&impl
->cf_node
, nir_cf_node_function
);
286 exec_list_make_empty(&impl
->body
);
287 exec_list_make_empty(&impl
->registers
);
288 exec_list_make_empty(&impl
->locals
);
289 impl
->num_params
= 0;
291 impl
->return_var
= NULL
;
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
;
320 impl
->num_params
= function
->num_params
;
321 impl
->params
= ralloc_array(function
->shader
,
322 nir_variable
*, impl
->num_params
);
324 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
325 impl
->params
[i
] = rzalloc(function
->shader
, nir_variable
);
326 impl
->params
[i
]->type
= function
->params
[i
].type
;
327 impl
->params
[i
]->data
.mode
= nir_var_param
;
328 impl
->params
[i
]->data
.location
= i
;
331 if (!glsl_type_is_void(function
->return_type
)) {
332 impl
->return_var
= rzalloc(function
->shader
, nir_variable
);
333 impl
->return_var
->type
= function
->return_type
;
334 impl
->return_var
->data
.mode
= nir_var_param
;
335 impl
->return_var
->data
.location
= -1;
337 impl
->return_var
= NULL
;
344 nir_block_create(nir_shader
*shader
)
346 nir_block
*block
= rzalloc(shader
, nir_block
);
348 cf_init(&block
->cf_node
, nir_cf_node_block
);
350 block
->successors
[0] = block
->successors
[1] = NULL
;
351 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
352 _mesa_key_pointer_equal
);
353 block
->imm_dom
= NULL
;
354 /* XXX maybe it would be worth it to defer allocation? This
355 * way it doesn't get allocated for shader refs that never run
356 * nir_calc_dominance? For example, state-tracker creates an
357 * initial IR, clones that, runs appropriate lowering pass, passes
358 * to driver which does common lowering/opt, and then stores ref
359 * which is later used to do state specific lowering and futher
360 * opt. Do any of the references not need dominance metadata?
362 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
363 _mesa_key_pointer_equal
);
365 exec_list_make_empty(&block
->instr_list
);
371 src_init(nir_src
*src
)
375 src
->reg
.indirect
= NULL
;
376 src
->reg
.base_offset
= 0;
380 nir_if_create(nir_shader
*shader
)
382 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
384 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
385 src_init(&if_stmt
->condition
);
387 nir_block
*then
= nir_block_create(shader
);
388 exec_list_make_empty(&if_stmt
->then_list
);
389 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
390 then
->cf_node
.parent
= &if_stmt
->cf_node
;
392 nir_block
*else_stmt
= nir_block_create(shader
);
393 exec_list_make_empty(&if_stmt
->else_list
);
394 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
395 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
401 nir_loop_create(nir_shader
*shader
)
403 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
405 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
407 nir_block
*body
= nir_block_create(shader
);
408 exec_list_make_empty(&loop
->body
);
409 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
410 body
->cf_node
.parent
= &loop
->cf_node
;
412 body
->successors
[0] = body
;
413 _mesa_set_add(body
->predecessors
, body
);
419 instr_init(nir_instr
*instr
, nir_instr_type type
)
423 exec_node_init(&instr
->node
);
427 dest_init(nir_dest
*dest
)
429 dest
->is_ssa
= false;
430 dest
->reg
.reg
= NULL
;
431 dest
->reg
.indirect
= NULL
;
432 dest
->reg
.base_offset
= 0;
436 alu_dest_init(nir_alu_dest
*dest
)
438 dest_init(&dest
->dest
);
439 dest
->saturate
= false;
440 dest
->write_mask
= 0xf;
444 alu_src_init(nir_alu_src
*src
)
447 src
->abs
= src
->negate
= false;
455 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
457 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
458 /* TODO: don't use rzalloc */
459 nir_alu_instr
*instr
=
461 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
463 instr_init(&instr
->instr
, nir_instr_type_alu
);
465 alu_dest_init(&instr
->dest
);
466 for (unsigned i
= 0; i
< num_srcs
; i
++)
467 alu_src_init(&instr
->src
[i
]);
473 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
475 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
476 instr_init(&instr
->instr
, nir_instr_type_jump
);
481 nir_load_const_instr
*
482 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
485 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
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 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
518 instr_init(&instr
->instr
, nir_instr_type_call
);
520 instr
->callee
= callee
;
521 instr
->num_params
= callee
->num_params
;
522 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
523 instr
->return_deref
= NULL
;
529 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
531 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
532 instr_init(&instr
->instr
, nir_instr_type_tex
);
534 dest_init(&instr
->dest
);
536 instr
->num_srcs
= num_srcs
;
537 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
538 for (unsigned i
= 0; i
< num_srcs
; i
++)
539 src_init(&instr
->src
[i
].src
);
541 instr
->texture_index
= 0;
542 instr
->texture_array_size
= 0;
543 instr
->texture
= NULL
;
544 instr
->sampler_index
= 0;
545 instr
->sampler
= NULL
;
551 nir_tex_instr_add_src(nir_tex_instr
*tex
,
552 nir_tex_src_type src_type
,
555 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
558 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
559 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
560 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
564 ralloc_free(tex
->src
);
567 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
568 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
573 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
575 assert(src_idx
< tex
->num_srcs
);
577 /* First rewrite the source to NIR_SRC_INIT */
578 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
580 /* Now, move all of the other sources down */
581 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
582 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
583 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
589 nir_phi_instr_create(nir_shader
*shader
)
591 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
592 instr_init(&instr
->instr
, nir_instr_type_phi
);
594 dest_init(&instr
->dest
);
595 exec_list_make_empty(&instr
->srcs
);
599 nir_parallel_copy_instr
*
600 nir_parallel_copy_instr_create(nir_shader
*shader
)
602 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
603 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
605 exec_list_make_empty(&instr
->entries
);
610 nir_ssa_undef_instr
*
611 nir_ssa_undef_instr_create(nir_shader
*shader
,
612 unsigned num_components
,
615 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
616 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
618 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
624 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
626 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
627 deref
->deref
.deref_type
= nir_deref_type_var
;
628 deref
->deref
.child
= NULL
;
629 deref
->deref
.type
= var
->type
;
635 nir_deref_array_create(void *mem_ctx
)
637 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
638 deref
->deref
.deref_type
= nir_deref_type_array
;
639 deref
->deref
.child
= NULL
;
640 deref
->deref_array_type
= nir_deref_array_type_direct
;
641 src_init(&deref
->indirect
);
642 deref
->base_offset
= 0;
647 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
649 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
650 deref
->deref
.deref_type
= nir_deref_type_struct
;
651 deref
->deref
.child
= NULL
;
652 deref
->index
= field_index
;
657 nir_deref_var_clone(const nir_deref_var
*deref
, void *mem_ctx
)
662 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
663 ret
->deref
.type
= deref
->deref
.type
;
664 if (deref
->deref
.child
)
665 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
669 static nir_deref_array
*
670 deref_array_clone(const nir_deref_array
*deref
, void *mem_ctx
)
672 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
673 ret
->base_offset
= deref
->base_offset
;
674 ret
->deref_array_type
= deref
->deref_array_type
;
675 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
676 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
678 ret
->deref
.type
= deref
->deref
.type
;
679 if (deref
->deref
.child
)
680 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
684 static nir_deref_struct
*
685 deref_struct_clone(const nir_deref_struct
*deref
, void *mem_ctx
)
687 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
688 ret
->deref
.type
= deref
->deref
.type
;
689 if (deref
->deref
.child
)
690 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
695 nir_deref_clone(const nir_deref
*deref
, void *mem_ctx
)
700 switch (deref
->deref_type
) {
701 case nir_deref_type_var
:
702 return &nir_deref_var_clone(nir_deref_as_var(deref
), mem_ctx
)->deref
;
703 case nir_deref_type_array
:
704 return &deref_array_clone(nir_deref_as_array(deref
), mem_ctx
)->deref
;
705 case nir_deref_type_struct
:
706 return &deref_struct_clone(nir_deref_as_struct(deref
), mem_ctx
)->deref
;
708 unreachable("Invalid dereference type");
714 /* This is the second step in the recursion. We've found the tail and made a
715 * copy. Now we need to iterate over all possible leaves and call the
716 * callback on each one.
719 deref_foreach_leaf_build_recur(nir_deref_var
*deref
, nir_deref
*tail
,
720 nir_deref_foreach_leaf_cb cb
, void *state
)
725 nir_deref_struct str
;
728 assert(tail
->child
== NULL
);
729 switch (glsl_get_base_type(tail
->type
)) {
731 case GLSL_TYPE_UINT16
:
732 case GLSL_TYPE_UINT64
:
734 case GLSL_TYPE_INT16
:
735 case GLSL_TYPE_INT64
:
736 case GLSL_TYPE_FLOAT
:
737 case GLSL_TYPE_FLOAT16
:
738 case GLSL_TYPE_DOUBLE
:
740 if (glsl_type_is_vector_or_scalar(tail
->type
))
741 return cb(deref
, state
);
744 case GLSL_TYPE_ARRAY
:
745 tmp
.arr
.deref
.deref_type
= nir_deref_type_array
;
746 tmp
.arr
.deref
.type
= glsl_get_array_element(tail
->type
);
747 tmp
.arr
.deref_array_type
= nir_deref_array_type_direct
;
748 tmp
.arr
.indirect
= NIR_SRC_INIT
;
749 tail
->child
= &tmp
.arr
.deref
;
751 length
= glsl_get_length(tail
->type
);
752 for (unsigned i
= 0; i
< length
; i
++) {
753 tmp
.arr
.deref
.child
= NULL
;
754 tmp
.arr
.base_offset
= i
;
755 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
760 case GLSL_TYPE_STRUCT
:
761 tmp
.str
.deref
.deref_type
= nir_deref_type_struct
;
762 tail
->child
= &tmp
.str
.deref
;
764 length
= glsl_get_length(tail
->type
);
765 for (unsigned i
= 0; i
< length
; i
++) {
766 tmp
.arr
.deref
.child
= NULL
;
767 tmp
.str
.deref
.type
= glsl_get_struct_field(tail
->type
, i
);
769 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
775 unreachable("Invalid type for dereference");
779 /* This is the first step of the foreach_leaf recursion. In this step we are
780 * walking to the end of the deref chain and making a copy in the stack as we
781 * go. This is because we don't want to mutate the deref chain that was
782 * passed in by the caller. The downside is that this deref chain is on the
783 * stack and , if the caller wants to do anything with it, they will have to
784 * make their own copy because this one will go away.
787 deref_foreach_leaf_copy_recur(nir_deref_var
*deref
, nir_deref
*tail
,
788 nir_deref_foreach_leaf_cb cb
, void *state
)
792 nir_deref_struct str
;
796 switch (tail
->child
->deref_type
) {
797 case nir_deref_type_array
:
798 c
.arr
= *nir_deref_as_array(tail
->child
);
799 tail
->child
= &c
.arr
.deref
;
800 return deref_foreach_leaf_copy_recur(deref
, &c
.arr
.deref
, cb
, state
);
802 case nir_deref_type_struct
:
803 c
.str
= *nir_deref_as_struct(tail
->child
);
804 tail
->child
= &c
.str
.deref
;
805 return deref_foreach_leaf_copy_recur(deref
, &c
.str
.deref
, cb
, state
);
807 case nir_deref_type_var
:
809 unreachable("Invalid deref type for a child");
812 /* We've gotten to the end of the original deref. Time to start
813 * building our own derefs.
815 return deref_foreach_leaf_build_recur(deref
, tail
, cb
, state
);
820 * This function iterates over all of the possible derefs that can be created
821 * with the given deref as the head. It then calls the provided callback with
822 * a full deref for each one.
824 * The deref passed to the callback will be allocated on the stack. You will
825 * need to make a copy if you want it to hang around.
828 nir_deref_foreach_leaf(nir_deref_var
*deref
,
829 nir_deref_foreach_leaf_cb cb
, void *state
)
831 nir_deref_var copy
= *deref
;
832 return deref_foreach_leaf_copy_recur(©
, ©
.deref
, cb
, state
);
835 /* Returns a load_const instruction that represents the constant
836 * initializer for the given deref chain. The caller is responsible for
837 * ensuring that there actually is a constant initializer.
839 nir_load_const_instr
*
840 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
842 nir_constant
*constant
= deref
->var
->constant_initializer
;
845 const nir_deref
*tail
= &deref
->deref
;
846 unsigned matrix_col
= 0;
847 while (tail
->child
) {
848 switch (tail
->child
->deref_type
) {
849 case nir_deref_type_array
: {
850 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
851 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
852 if (glsl_type_is_matrix(tail
->type
)) {
853 assert(arr
->deref
.child
== NULL
);
854 matrix_col
= arr
->base_offset
;
856 constant
= constant
->elements
[arr
->base_offset
];
861 case nir_deref_type_struct
: {
862 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
867 unreachable("Invalid deref child type");
873 unsigned bit_size
= glsl_get_bit_size(tail
->type
);
874 nir_load_const_instr
*load
=
875 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
),
878 switch (glsl_get_base_type(tail
->type
)) {
879 case GLSL_TYPE_FLOAT
:
882 case GLSL_TYPE_FLOAT16
:
883 case GLSL_TYPE_DOUBLE
:
884 case GLSL_TYPE_INT16
:
885 case GLSL_TYPE_UINT16
:
886 case GLSL_TYPE_UINT64
:
887 case GLSL_TYPE_INT64
:
889 load
->value
= constant
->values
[matrix_col
];
892 unreachable("Invalid immediate type");
898 static nir_const_value
899 const_value_float(double d
, unsigned bit_size
)
903 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
904 case 32: v
.f32
[0] = d
; break;
905 case 64: v
.f64
[0] = d
; break;
907 unreachable("Invalid bit size");
912 static nir_const_value
913 const_value_int(int64_t i
, unsigned bit_size
)
917 case 8: v
.i8
[0] = i
; break;
918 case 16: v
.i16
[0] = i
; break;
919 case 32: v
.i32
[0] = i
; break;
920 case 64: v
.i64
[0] = i
; break;
922 unreachable("Invalid bit size");
928 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
930 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
931 const int64_t min_int
= -max_int
- 1;
934 return const_value_int(0, bit_size
);
936 return const_value_float(0, bit_size
);
938 return const_value_int(1, bit_size
);
940 return const_value_float(1, bit_size
);
942 return const_value_int(max_int
, bit_size
);
944 return const_value_int(~0ull, bit_size
);
946 return const_value_float(INFINITY
, bit_size
);
948 return const_value_int(min_int
, bit_size
);
950 return const_value_int(0, bit_size
);
952 return const_value_float(-INFINITY
, bit_size
);
954 return const_value_int(~0ull, bit_size
);
956 return const_value_int(0, bit_size
);
958 return const_value_int(0, bit_size
);
960 unreachable("Invalid reduction operation");
965 nir_cf_node_get_function(nir_cf_node
*node
)
967 while (node
->type
!= nir_cf_node_function
) {
971 return nir_cf_node_as_function(node
);
974 /* Reduces a cursor by trying to convert everything to after and trying to
975 * go up to block granularity when possible.
978 reduce_cursor(nir_cursor cursor
)
980 switch (cursor
.option
) {
981 case nir_cursor_before_block
:
982 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
983 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
984 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
985 /* Empty block. After is as good as before. */
986 cursor
.option
= nir_cursor_after_block
;
990 case nir_cursor_after_block
:
993 case nir_cursor_before_instr
: {
994 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
996 /* Before this instruction is after the previous */
997 cursor
.instr
= prev_instr
;
998 cursor
.option
= nir_cursor_after_instr
;
1000 /* No previous instruction. Switch to before block */
1001 cursor
.block
= cursor
.instr
->block
;
1002 cursor
.option
= nir_cursor_before_block
;
1004 return reduce_cursor(cursor
);
1007 case nir_cursor_after_instr
:
1008 if (nir_instr_next(cursor
.instr
) == NULL
) {
1009 /* This is the last instruction, switch to after block */
1010 cursor
.option
= nir_cursor_after_block
;
1011 cursor
.block
= cursor
.instr
->block
;
1016 unreachable("Inavlid cursor option");
1021 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
1023 /* Reduced cursors should be unique */
1024 a
= reduce_cursor(a
);
1025 b
= reduce_cursor(b
);
1027 return a
.block
== b
.block
&& a
.option
== b
.option
;
1031 add_use_cb(nir_src
*src
, void *state
)
1033 nir_instr
*instr
= state
;
1035 src
->parent_instr
= instr
;
1036 list_addtail(&src
->use_link
,
1037 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
1043 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1045 nir_instr
*instr
= state
;
1047 if (instr
->block
&& def
->index
== UINT_MAX
) {
1048 nir_function_impl
*impl
=
1049 nir_cf_node_get_function(&instr
->block
->cf_node
);
1051 def
->index
= impl
->ssa_alloc
++;
1058 add_reg_def_cb(nir_dest
*dest
, void *state
)
1060 nir_instr
*instr
= state
;
1062 if (!dest
->is_ssa
) {
1063 dest
->reg
.parent_instr
= instr
;
1064 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
1071 add_defs_uses(nir_instr
*instr
)
1073 nir_foreach_src(instr
, add_use_cb
, instr
);
1074 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
1075 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
1079 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
1081 switch (cursor
.option
) {
1082 case nir_cursor_before_block
:
1083 /* Only allow inserting jumps into empty blocks. */
1084 if (instr
->type
== nir_instr_type_jump
)
1085 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
1087 instr
->block
= cursor
.block
;
1088 add_defs_uses(instr
);
1089 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
1091 case nir_cursor_after_block
: {
1092 /* Inserting instructions after a jump is illegal. */
1093 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
1094 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
1097 instr
->block
= cursor
.block
;
1098 add_defs_uses(instr
);
1099 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
1102 case nir_cursor_before_instr
:
1103 assert(instr
->type
!= nir_instr_type_jump
);
1104 instr
->block
= cursor
.instr
->block
;
1105 add_defs_uses(instr
);
1106 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
1108 case nir_cursor_after_instr
:
1109 /* Inserting instructions after a jump is illegal. */
1110 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
1112 /* Only allow inserting jumps at the end of the block. */
1113 if (instr
->type
== nir_instr_type_jump
)
1114 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
1116 instr
->block
= cursor
.instr
->block
;
1117 add_defs_uses(instr
);
1118 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
1122 if (instr
->type
== nir_instr_type_jump
)
1123 nir_handle_add_jump(instr
->block
);
1127 src_is_valid(const nir_src
*src
)
1129 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
1133 remove_use_cb(nir_src
*src
, void *state
)
1137 if (src_is_valid(src
))
1138 list_del(&src
->use_link
);
1144 remove_def_cb(nir_dest
*dest
, void *state
)
1149 list_del(&dest
->reg
.def_link
);
1155 remove_defs_uses(nir_instr
*instr
)
1157 nir_foreach_dest(instr
, remove_def_cb
, instr
);
1158 nir_foreach_src(instr
, remove_use_cb
, instr
);
1161 void nir_instr_remove(nir_instr
*instr
)
1163 remove_defs_uses(instr
);
1164 exec_node_remove(&instr
->node
);
1166 if (instr
->type
== nir_instr_type_jump
) {
1167 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
1168 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
1175 nir_index_local_regs(nir_function_impl
*impl
)
1178 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1179 reg
->index
= index
++;
1181 impl
->reg_alloc
= index
;
1185 nir_index_global_regs(nir_shader
*shader
)
1188 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1189 reg
->index
= index
++;
1191 shader
->reg_alloc
= index
;
1195 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1197 return cb(&instr
->dest
.dest
, state
);
1201 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
1204 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1205 return cb(&instr
->dest
, state
);
1211 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
1214 return cb(&instr
->dest
, state
);
1218 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1220 return cb(&instr
->dest
, state
);
1224 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
1225 nir_foreach_dest_cb cb
, void *state
)
1227 nir_foreach_parallel_copy_entry(entry
, instr
) {
1228 if (!cb(&entry
->dest
, state
))
1236 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1238 switch (instr
->type
) {
1239 case nir_instr_type_alu
:
1240 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
1241 case nir_instr_type_intrinsic
:
1242 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
1243 case nir_instr_type_tex
:
1244 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
1245 case nir_instr_type_phi
:
1246 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1247 case nir_instr_type_parallel_copy
:
1248 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1251 case nir_instr_type_load_const
:
1252 case nir_instr_type_ssa_undef
:
1253 case nir_instr_type_call
:
1254 case nir_instr_type_jump
:
1258 unreachable("Invalid instruction type");
1265 struct foreach_ssa_def_state
{
1266 nir_foreach_ssa_def_cb cb
;
1271 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1273 struct foreach_ssa_def_state
*state
= void_state
;
1276 return state
->cb(&dest
->ssa
, state
->client_state
);
1282 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1284 switch (instr
->type
) {
1285 case nir_instr_type_alu
:
1286 case nir_instr_type_tex
:
1287 case nir_instr_type_intrinsic
:
1288 case nir_instr_type_phi
:
1289 case nir_instr_type_parallel_copy
: {
1290 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1291 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1294 case nir_instr_type_load_const
:
1295 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1296 case nir_instr_type_ssa_undef
:
1297 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1298 case nir_instr_type_call
:
1299 case nir_instr_type_jump
:
1302 unreachable("Invalid instruction type");
1307 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1309 if (!cb(src
, state
))
1311 if (!src
->is_ssa
&& src
->reg
.indirect
)
1312 return cb(src
->reg
.indirect
, state
);
1317 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
1320 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
1321 return visit_src(&deref
->indirect
, cb
, state
);
1326 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
1328 nir_deref
*cur
= &deref
->deref
;
1329 while (cur
!= NULL
) {
1330 if (cur
->deref_type
== nir_deref_type_array
) {
1331 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
1342 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1344 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1345 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1352 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1354 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1355 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1359 if (instr
->texture
!= NULL
) {
1360 if (!visit_deref_src(instr
->texture
, cb
, state
))
1364 if (instr
->sampler
!= NULL
) {
1365 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1373 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1376 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1377 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1378 if (!visit_src(&instr
->src
[i
], cb
, state
))
1383 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1384 for (unsigned i
= 0; i
< num_vars
; i
++) {
1385 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1393 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1395 nir_foreach_phi_src(src
, instr
) {
1396 if (!visit_src(&src
->src
, cb
, state
))
1404 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1405 nir_foreach_src_cb cb
, void *state
)
1407 nir_foreach_parallel_copy_entry(entry
, instr
) {
1408 if (!visit_src(&entry
->src
, cb
, state
))
1417 nir_foreach_src_cb cb
;
1418 } visit_dest_indirect_state
;
1421 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1423 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1425 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1426 return state
->cb(dest
->reg
.indirect
, state
->state
);
1432 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1434 switch (instr
->type
) {
1435 case nir_instr_type_alu
:
1436 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1439 case nir_instr_type_intrinsic
:
1440 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1443 case nir_instr_type_tex
:
1444 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1447 case nir_instr_type_call
:
1448 /* Call instructions have no regular sources */
1450 case nir_instr_type_load_const
:
1451 /* Constant load instructions have no regular sources */
1453 case nir_instr_type_phi
:
1454 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1457 case nir_instr_type_parallel_copy
:
1458 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1462 case nir_instr_type_jump
:
1463 case nir_instr_type_ssa_undef
:
1467 unreachable("Invalid instruction type");
1471 visit_dest_indirect_state dest_state
;
1472 dest_state
.state
= state
;
1474 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1478 nir_src_as_const_value(nir_src src
)
1483 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1486 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1488 return &load
->value
;
1492 * Returns true if the source is known to be dynamically uniform. Otherwise it
1493 * returns false which means it may or may not be dynamically uniform but it
1494 * can't be determined.
1497 nir_src_is_dynamically_uniform(nir_src src
)
1502 /* Constants are trivially dynamically uniform */
1503 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1506 /* As are uniform variables */
1507 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1508 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1510 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1514 /* XXX: this could have many more tests, such as when a sampler function is
1515 * called with dynamically uniform arguments.
1521 src_remove_all_uses(nir_src
*src
)
1523 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1524 if (!src_is_valid(src
))
1527 list_del(&src
->use_link
);
1532 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1534 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1535 if (!src_is_valid(src
))
1539 src
->parent_instr
= parent_instr
;
1541 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1543 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1546 src
->parent_if
= parent_if
;
1548 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1550 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1556 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1558 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1560 src_remove_all_uses(src
);
1562 src_add_all_uses(src
, instr
, NULL
);
1566 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1568 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1570 src_remove_all_uses(dest
);
1571 src_remove_all_uses(src
);
1573 *src
= NIR_SRC_INIT
;
1574 src_add_all_uses(dest
, dest_instr
, NULL
);
1578 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1580 nir_src
*src
= &if_stmt
->condition
;
1581 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1583 src_remove_all_uses(src
);
1585 src_add_all_uses(src
, NULL
, if_stmt
);
1589 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1592 /* We can only overwrite an SSA destination if it has no uses. */
1593 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1595 list_del(&dest
->reg
.def_link
);
1596 if (dest
->reg
.indirect
)
1597 src_remove_all_uses(dest
->reg
.indirect
);
1600 /* We can't re-write with an SSA def */
1601 assert(!new_dest
.is_ssa
);
1603 nir_dest_copy(dest
, &new_dest
, instr
);
1605 dest
->reg
.parent_instr
= instr
;
1606 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1608 if (dest
->reg
.indirect
)
1609 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1613 nir_instr_rewrite_deref(nir_instr
*instr
, nir_deref_var
**deref
,
1614 nir_deref_var
*new_deref
)
1617 visit_deref_src(*deref
, remove_use_cb
, NULL
);
1622 visit_deref_src(*deref
, add_use_cb
, instr
);
1625 /* note: does *not* take ownership of 'name' */
1627 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1628 unsigned num_components
,
1629 unsigned bit_size
, const char *name
)
1631 def
->name
= ralloc_strdup(instr
, name
);
1632 def
->parent_instr
= instr
;
1633 list_inithead(&def
->uses
);
1634 list_inithead(&def
->if_uses
);
1635 def
->num_components
= num_components
;
1636 def
->bit_size
= bit_size
;
1639 nir_function_impl
*impl
=
1640 nir_cf_node_get_function(&instr
->block
->cf_node
);
1642 def
->index
= impl
->ssa_alloc
++;
1644 def
->index
= UINT_MAX
;
1648 /* note: does *not* take ownership of 'name' */
1650 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1651 unsigned num_components
, unsigned bit_size
,
1654 dest
->is_ssa
= true;
1655 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1659 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1661 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1663 nir_foreach_use_safe(use_src
, def
)
1664 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1666 nir_foreach_if_use_safe(use_src
, def
)
1667 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1671 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1673 assert(start
->block
== end
->block
);
1675 if (between
->block
!= start
->block
)
1678 /* Search backwards looking for "between" */
1679 while (start
!= end
) {
1683 end
= nir_instr_prev(end
);
1690 /* Replaces all uses of the given SSA def with the given source but only if
1691 * the use comes after the after_me instruction. This can be useful if you
1692 * are emitting code to fix up the result of some instruction: you can freely
1693 * use the result in that code and then call rewrite_uses_after and pass the
1694 * last fixup instruction as after_me and it will replace all of the uses you
1695 * want without touching the fixup code.
1697 * This function assumes that after_me is in the same block as
1698 * def->parent_instr and that after_me comes after def->parent_instr.
1701 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1702 nir_instr
*after_me
)
1704 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1706 nir_foreach_use_safe(use_src
, def
) {
1707 assert(use_src
->parent_instr
!= def
->parent_instr
);
1708 /* Since def already dominates all of its uses, the only way a use can
1709 * not be dominated by after_me is if it is between def and after_me in
1710 * the instruction list.
1712 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1713 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1716 nir_foreach_if_use_safe(use_src
, def
)
1717 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1721 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1723 uint8_t read_mask
= 0;
1724 nir_foreach_use(use
, def
) {
1725 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1726 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1727 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1728 int src_idx
= alu_src
- &alu
->src
[0];
1729 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1731 for (unsigned c
= 0; c
< 4; c
++) {
1732 if (!nir_alu_instr_channel_used(alu
, src_idx
, c
))
1735 read_mask
|= (1 << alu_src
->swizzle
[c
]);
1738 return (1 << def
->num_components
) - 1;
1746 nir_block_cf_tree_next(nir_block
*block
)
1748 if (block
== NULL
) {
1749 /* nir_foreach_block_safe() will call this function on a NULL block
1750 * after the last iteration, but it won't use the result so just return
1756 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1758 return nir_cf_node_cf_tree_first(cf_next
);
1760 nir_cf_node
*parent
= block
->cf_node
.parent
;
1762 switch (parent
->type
) {
1763 case nir_cf_node_if
: {
1764 /* Are we at the end of the if? Go to the beginning of the else */
1765 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1766 if (block
== nir_if_last_then_block(if_stmt
))
1767 return nir_if_first_else_block(if_stmt
);
1769 assert(block
== nir_if_last_else_block(if_stmt
));
1773 case nir_cf_node_loop
:
1774 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1776 case nir_cf_node_function
:
1780 unreachable("unknown cf node type");
1785 nir_block_cf_tree_prev(nir_block
*block
)
1787 if (block
== NULL
) {
1788 /* do this for consistency with nir_block_cf_tree_next() */
1792 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1794 return nir_cf_node_cf_tree_last(cf_prev
);
1796 nir_cf_node
*parent
= block
->cf_node
.parent
;
1798 switch (parent
->type
) {
1799 case nir_cf_node_if
: {
1800 /* Are we at the beginning of the else? Go to the end of the if */
1801 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1802 if (block
== nir_if_first_else_block(if_stmt
))
1803 return nir_if_last_then_block(if_stmt
);
1805 assert(block
== nir_if_first_then_block(if_stmt
));
1809 case nir_cf_node_loop
:
1810 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1812 case nir_cf_node_function
:
1816 unreachable("unknown cf node type");
1820 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1822 switch (node
->type
) {
1823 case nir_cf_node_function
: {
1824 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1825 return nir_start_block(impl
);
1828 case nir_cf_node_if
: {
1829 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1830 return nir_if_first_then_block(if_stmt
);
1833 case nir_cf_node_loop
: {
1834 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1835 return nir_loop_first_block(loop
);
1838 case nir_cf_node_block
: {
1839 return nir_cf_node_as_block(node
);
1843 unreachable("unknown node type");
1847 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1849 switch (node
->type
) {
1850 case nir_cf_node_function
: {
1851 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1852 return nir_impl_last_block(impl
);
1855 case nir_cf_node_if
: {
1856 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1857 return nir_if_last_else_block(if_stmt
);
1860 case nir_cf_node_loop
: {
1861 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1862 return nir_loop_last_block(loop
);
1865 case nir_cf_node_block
: {
1866 return nir_cf_node_as_block(node
);
1870 unreachable("unknown node type");
1874 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1876 if (node
->type
== nir_cf_node_block
)
1877 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1878 else if (node
->type
== nir_cf_node_function
)
1881 return nir_cf_node_as_block(nir_cf_node_next(node
));
1885 nir_block_get_following_if(nir_block
*block
)
1887 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1890 if (nir_cf_node_is_last(&block
->cf_node
))
1893 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1895 if (next_node
->type
!= nir_cf_node_if
)
1898 return nir_cf_node_as_if(next_node
);
1902 nir_block_get_following_loop(nir_block
*block
)
1904 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1907 if (nir_cf_node_is_last(&block
->cf_node
))
1910 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1912 if (next_node
->type
!= nir_cf_node_loop
)
1915 return nir_cf_node_as_loop(next_node
);
1919 nir_index_blocks(nir_function_impl
*impl
)
1923 if (impl
->valid_metadata
& nir_metadata_block_index
)
1926 nir_foreach_block(block
, impl
) {
1927 block
->index
= index
++;
1930 impl
->num_blocks
= index
;
1934 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1936 unsigned *index
= (unsigned *) state
;
1937 def
->index
= (*index
)++;
1943 * The indices are applied top-to-bottom which has the very nice property
1944 * that, if A dominates B, then A->index <= B->index.
1947 nir_index_ssa_defs(nir_function_impl
*impl
)
1951 nir_foreach_block(block
, impl
) {
1952 nir_foreach_instr(instr
, block
)
1953 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1956 impl
->ssa_alloc
= index
;
1960 * The indices are applied top-to-bottom which has the very nice property
1961 * that, if A dominates B, then A->index <= B->index.
1964 nir_index_instrs(nir_function_impl
*impl
)
1968 nir_foreach_block(block
, impl
) {
1969 nir_foreach_instr(instr
, block
)
1970 instr
->index
= index
++;
1977 nir_intrinsic_from_system_value(gl_system_value val
)
1980 case SYSTEM_VALUE_VERTEX_ID
:
1981 return nir_intrinsic_load_vertex_id
;
1982 case SYSTEM_VALUE_INSTANCE_ID
:
1983 return nir_intrinsic_load_instance_id
;
1984 case SYSTEM_VALUE_DRAW_ID
:
1985 return nir_intrinsic_load_draw_id
;
1986 case SYSTEM_VALUE_BASE_INSTANCE
:
1987 return nir_intrinsic_load_base_instance
;
1988 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1989 return nir_intrinsic_load_vertex_id_zero_base
;
1990 case SYSTEM_VALUE_BASE_VERTEX
:
1991 return nir_intrinsic_load_base_vertex
;
1992 case SYSTEM_VALUE_INVOCATION_ID
:
1993 return nir_intrinsic_load_invocation_id
;
1994 case SYSTEM_VALUE_FRAG_COORD
:
1995 return nir_intrinsic_load_frag_coord
;
1996 case SYSTEM_VALUE_FRONT_FACE
:
1997 return nir_intrinsic_load_front_face
;
1998 case SYSTEM_VALUE_SAMPLE_ID
:
1999 return nir_intrinsic_load_sample_id
;
2000 case SYSTEM_VALUE_SAMPLE_POS
:
2001 return nir_intrinsic_load_sample_pos
;
2002 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
2003 return nir_intrinsic_load_sample_mask_in
;
2004 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
2005 return nir_intrinsic_load_local_invocation_id
;
2006 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
2007 return nir_intrinsic_load_local_invocation_index
;
2008 case SYSTEM_VALUE_WORK_GROUP_ID
:
2009 return nir_intrinsic_load_work_group_id
;
2010 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
2011 return nir_intrinsic_load_num_work_groups
;
2012 case SYSTEM_VALUE_PRIMITIVE_ID
:
2013 return nir_intrinsic_load_primitive_id
;
2014 case SYSTEM_VALUE_TESS_COORD
:
2015 return nir_intrinsic_load_tess_coord
;
2016 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
2017 return nir_intrinsic_load_tess_level_outer
;
2018 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
2019 return nir_intrinsic_load_tess_level_inner
;
2020 case SYSTEM_VALUE_VERTICES_IN
:
2021 return nir_intrinsic_load_patch_vertices_in
;
2022 case SYSTEM_VALUE_HELPER_INVOCATION
:
2023 return nir_intrinsic_load_helper_invocation
;
2024 case SYSTEM_VALUE_VIEW_INDEX
:
2025 return nir_intrinsic_load_view_index
;
2026 case SYSTEM_VALUE_SUBGROUP_SIZE
:
2027 return nir_intrinsic_load_subgroup_size
;
2028 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
2029 return nir_intrinsic_load_subgroup_invocation
;
2030 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
2031 return nir_intrinsic_load_subgroup_eq_mask
;
2032 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
2033 return nir_intrinsic_load_subgroup_ge_mask
;
2034 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
2035 return nir_intrinsic_load_subgroup_gt_mask
;
2036 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
2037 return nir_intrinsic_load_subgroup_le_mask
;
2038 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
2039 return nir_intrinsic_load_subgroup_lt_mask
;
2040 case SYSTEM_VALUE_NUM_SUBGROUPS
:
2041 return nir_intrinsic_load_num_subgroups
;
2042 case SYSTEM_VALUE_SUBGROUP_ID
:
2043 return nir_intrinsic_load_subgroup_id
;
2044 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
2045 return nir_intrinsic_load_local_group_size
;
2047 unreachable("system value does not directly correspond to intrinsic");
2052 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
2055 case nir_intrinsic_load_vertex_id
:
2056 return SYSTEM_VALUE_VERTEX_ID
;
2057 case nir_intrinsic_load_instance_id
:
2058 return SYSTEM_VALUE_INSTANCE_ID
;
2059 case nir_intrinsic_load_draw_id
:
2060 return SYSTEM_VALUE_DRAW_ID
;
2061 case nir_intrinsic_load_base_instance
:
2062 return SYSTEM_VALUE_BASE_INSTANCE
;
2063 case nir_intrinsic_load_vertex_id_zero_base
:
2064 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2065 case nir_intrinsic_load_base_vertex
:
2066 return SYSTEM_VALUE_BASE_VERTEX
;
2067 case nir_intrinsic_load_invocation_id
:
2068 return SYSTEM_VALUE_INVOCATION_ID
;
2069 case nir_intrinsic_load_frag_coord
:
2070 return SYSTEM_VALUE_FRAG_COORD
;
2071 case nir_intrinsic_load_front_face
:
2072 return SYSTEM_VALUE_FRONT_FACE
;
2073 case nir_intrinsic_load_sample_id
:
2074 return SYSTEM_VALUE_SAMPLE_ID
;
2075 case nir_intrinsic_load_sample_pos
:
2076 return SYSTEM_VALUE_SAMPLE_POS
;
2077 case nir_intrinsic_load_sample_mask_in
:
2078 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
2079 case nir_intrinsic_load_local_invocation_id
:
2080 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
2081 case nir_intrinsic_load_local_invocation_index
:
2082 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
2083 case nir_intrinsic_load_num_work_groups
:
2084 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2085 case nir_intrinsic_load_work_group_id
:
2086 return SYSTEM_VALUE_WORK_GROUP_ID
;
2087 case nir_intrinsic_load_primitive_id
:
2088 return SYSTEM_VALUE_PRIMITIVE_ID
;
2089 case nir_intrinsic_load_tess_coord
:
2090 return SYSTEM_VALUE_TESS_COORD
;
2091 case nir_intrinsic_load_tess_level_outer
:
2092 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2093 case nir_intrinsic_load_tess_level_inner
:
2094 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
2095 case nir_intrinsic_load_patch_vertices_in
:
2096 return SYSTEM_VALUE_VERTICES_IN
;
2097 case nir_intrinsic_load_helper_invocation
:
2098 return SYSTEM_VALUE_HELPER_INVOCATION
;
2099 case nir_intrinsic_load_view_index
:
2100 return SYSTEM_VALUE_VIEW_INDEX
;
2101 case nir_intrinsic_load_subgroup_size
:
2102 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2103 case nir_intrinsic_load_subgroup_invocation
:
2104 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2105 case nir_intrinsic_load_subgroup_eq_mask
:
2106 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2107 case nir_intrinsic_load_subgroup_ge_mask
:
2108 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2109 case nir_intrinsic_load_subgroup_gt_mask
:
2110 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2111 case nir_intrinsic_load_subgroup_le_mask
:
2112 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2113 case nir_intrinsic_load_subgroup_lt_mask
:
2114 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2115 case nir_intrinsic_load_num_subgroups
:
2116 return SYSTEM_VALUE_NUM_SUBGROUPS
;
2117 case nir_intrinsic_load_subgroup_id
:
2118 return SYSTEM_VALUE_SUBGROUP_ID
;
2119 case nir_intrinsic_load_local_group_size
:
2120 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2122 unreachable("intrinsic doesn't produce a system value");