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;
67 shader
->lowered_derefs
= 0;
73 reg_create(void *mem_ctx
, struct exec_list
*list
)
75 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
77 list_inithead(®
->uses
);
78 list_inithead(®
->defs
);
79 list_inithead(®
->if_uses
);
81 reg
->num_components
= 0;
83 reg
->num_array_elems
= 0;
84 reg
->is_packed
= false;
87 exec_list_push_tail(list
, ®
->node
);
93 nir_global_reg_create(nir_shader
*shader
)
95 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
96 reg
->index
= shader
->reg_alloc
++;
97 reg
->is_global
= true;
103 nir_local_reg_create(nir_function_impl
*impl
)
105 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
106 reg
->index
= impl
->reg_alloc
++;
107 reg
->is_global
= false;
113 nir_reg_remove(nir_register
*reg
)
115 exec_node_remove(®
->node
);
119 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
121 switch (var
->data
.mode
) {
123 assert(!"invalid mode");
127 assert(!"nir_shader_add_variable cannot be used for local variables");
131 assert(!"nir_shader_add_variable cannot be used for function parameters");
135 exec_list_push_tail(&shader
->globals
, &var
->node
);
138 case nir_var_shader_in
:
139 exec_list_push_tail(&shader
->inputs
, &var
->node
);
142 case nir_var_shader_out
:
143 exec_list_push_tail(&shader
->outputs
, &var
->node
);
146 case nir_var_uniform
:
147 case nir_var_shader_storage
:
148 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
152 assert(shader
->info
.stage
== MESA_SHADER_COMPUTE
);
153 exec_list_push_tail(&shader
->shared
, &var
->node
);
156 case nir_var_system_value
:
157 exec_list_push_tail(&shader
->system_values
, &var
->node
);
163 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
164 const struct glsl_type
*type
, const char *name
)
166 nir_variable
*var
= rzalloc(shader
, nir_variable
);
167 var
->name
= ralloc_strdup(var
, name
);
169 var
->data
.mode
= mode
;
171 if ((mode
== nir_var_shader_in
&&
172 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
173 (mode
== nir_var_shader_out
&&
174 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
175 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
177 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
178 var
->data
.read_only
= true;
180 nir_shader_add_variable(shader
, var
);
186 nir_local_variable_create(nir_function_impl
*impl
,
187 const struct glsl_type
*type
, const char *name
)
189 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
190 var
->name
= ralloc_strdup(var
, name
);
192 var
->data
.mode
= nir_var_local
;
194 nir_function_impl_add_variable(impl
, var
);
200 nir_function_create(nir_shader
*shader
, const char *name
)
202 nir_function
*func
= ralloc(shader
, nir_function
);
204 exec_list_push_tail(&shader
->functions
, &func
->node
);
206 func
->name
= ralloc_strdup(func
, name
);
207 func
->shader
= shader
;
208 func
->num_params
= 0;
210 func
->return_type
= glsl_void_type();
216 /* NOTE: if the instruction you are copying a src to is already added
217 * to the IR, use nir_instr_rewrite_src() instead.
219 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
221 dest
->is_ssa
= src
->is_ssa
;
223 dest
->ssa
= src
->ssa
;
225 dest
->reg
.base_offset
= src
->reg
.base_offset
;
226 dest
->reg
.reg
= src
->reg
.reg
;
227 if (src
->reg
.indirect
) {
228 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
229 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
231 dest
->reg
.indirect
= NULL
;
236 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
238 /* Copying an SSA definition makes no sense whatsoever. */
239 assert(!src
->is_ssa
);
241 dest
->is_ssa
= false;
243 dest
->reg
.base_offset
= src
->reg
.base_offset
;
244 dest
->reg
.reg
= src
->reg
.reg
;
245 if (src
->reg
.indirect
) {
246 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
247 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
249 dest
->reg
.indirect
= NULL
;
254 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
255 nir_alu_instr
*instr
)
257 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
258 dest
->abs
= src
->abs
;
259 dest
->negate
= src
->negate
;
260 for (unsigned i
= 0; i
< 4; i
++)
261 dest
->swizzle
[i
] = src
->swizzle
[i
];
265 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
266 nir_alu_instr
*instr
)
268 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
269 dest
->write_mask
= src
->write_mask
;
270 dest
->saturate
= src
->saturate
;
275 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
277 exec_node_init(&node
->node
);
283 nir_function_impl_create_bare(nir_shader
*shader
)
285 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
287 impl
->function
= NULL
;
289 cf_init(&impl
->cf_node
, nir_cf_node_function
);
291 exec_list_make_empty(&impl
->body
);
292 exec_list_make_empty(&impl
->registers
);
293 exec_list_make_empty(&impl
->locals
);
294 impl
->num_params
= 0;
296 impl
->return_var
= NULL
;
299 impl
->valid_metadata
= nir_metadata_none
;
301 /* create start & end blocks */
302 nir_block
*start_block
= nir_block_create(shader
);
303 nir_block
*end_block
= nir_block_create(shader
);
304 start_block
->cf_node
.parent
= &impl
->cf_node
;
305 end_block
->cf_node
.parent
= &impl
->cf_node
;
306 impl
->end_block
= end_block
;
308 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
310 start_block
->successors
[0] = end_block
;
311 _mesa_set_add(end_block
->predecessors
, start_block
);
316 nir_function_impl_create(nir_function
*function
)
318 assert(function
->impl
== NULL
);
320 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
322 function
->impl
= impl
;
323 impl
->function
= function
;
325 impl
->num_params
= function
->num_params
;
326 impl
->params
= ralloc_array(function
->shader
,
327 nir_variable
*, impl
->num_params
);
329 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
330 impl
->params
[i
] = rzalloc(function
->shader
, nir_variable
);
331 impl
->params
[i
]->type
= function
->params
[i
].type
;
332 impl
->params
[i
]->data
.mode
= nir_var_param
;
333 impl
->params
[i
]->data
.location
= i
;
336 if (!glsl_type_is_void(function
->return_type
)) {
337 impl
->return_var
= rzalloc(function
->shader
, nir_variable
);
338 impl
->return_var
->type
= function
->return_type
;
339 impl
->return_var
->data
.mode
= nir_var_param
;
340 impl
->return_var
->data
.location
= -1;
342 impl
->return_var
= NULL
;
349 nir_block_create(nir_shader
*shader
)
351 nir_block
*block
= rzalloc(shader
, nir_block
);
353 cf_init(&block
->cf_node
, nir_cf_node_block
);
355 block
->successors
[0] = block
->successors
[1] = NULL
;
356 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
357 _mesa_key_pointer_equal
);
358 block
->imm_dom
= NULL
;
359 /* XXX maybe it would be worth it to defer allocation? This
360 * way it doesn't get allocated for shader refs that never run
361 * nir_calc_dominance? For example, state-tracker creates an
362 * initial IR, clones that, runs appropriate lowering pass, passes
363 * to driver which does common lowering/opt, and then stores ref
364 * which is later used to do state specific lowering and futher
365 * opt. Do any of the references not need dominance metadata?
367 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
368 _mesa_key_pointer_equal
);
370 exec_list_make_empty(&block
->instr_list
);
376 src_init(nir_src
*src
)
380 src
->reg
.indirect
= NULL
;
381 src
->reg
.base_offset
= 0;
385 nir_if_create(nir_shader
*shader
)
387 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
389 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
390 src_init(&if_stmt
->condition
);
392 nir_block
*then
= nir_block_create(shader
);
393 exec_list_make_empty(&if_stmt
->then_list
);
394 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
395 then
->cf_node
.parent
= &if_stmt
->cf_node
;
397 nir_block
*else_stmt
= nir_block_create(shader
);
398 exec_list_make_empty(&if_stmt
->else_list
);
399 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
400 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
406 nir_loop_create(nir_shader
*shader
)
408 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
410 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
412 nir_block
*body
= nir_block_create(shader
);
413 exec_list_make_empty(&loop
->body
);
414 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
415 body
->cf_node
.parent
= &loop
->cf_node
;
417 body
->successors
[0] = body
;
418 _mesa_set_add(body
->predecessors
, body
);
424 instr_init(nir_instr
*instr
, nir_instr_type type
)
428 exec_node_init(&instr
->node
);
432 dest_init(nir_dest
*dest
)
434 dest
->is_ssa
= false;
435 dest
->reg
.reg
= NULL
;
436 dest
->reg
.indirect
= NULL
;
437 dest
->reg
.base_offset
= 0;
441 alu_dest_init(nir_alu_dest
*dest
)
443 dest_init(&dest
->dest
);
444 dest
->saturate
= false;
445 dest
->write_mask
= 0xf;
449 alu_src_init(nir_alu_src
*src
)
452 src
->abs
= src
->negate
= false;
460 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
462 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
463 /* TODO: don't use rzalloc */
464 nir_alu_instr
*instr
=
466 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
468 instr_init(&instr
->instr
, nir_instr_type_alu
);
470 alu_dest_init(&instr
->dest
);
471 for (unsigned i
= 0; i
< num_srcs
; i
++)
472 alu_src_init(&instr
->src
[i
]);
478 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
480 nir_deref_instr
*instr
=
481 rzalloc_size(shader
, sizeof(nir_deref_instr
));
483 instr_init(&instr
->instr
, nir_instr_type_deref
);
485 instr
->deref_type
= deref_type
;
486 if (deref_type
!= nir_deref_type_var
)
487 src_init(&instr
->parent
);
489 if (deref_type
== nir_deref_type_array
)
490 src_init(&instr
->arr
.index
);
492 dest_init(&instr
->dest
);
498 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
500 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
501 instr_init(&instr
->instr
, nir_instr_type_jump
);
506 nir_load_const_instr
*
507 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
510 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
511 instr_init(&instr
->instr
, nir_instr_type_load_const
);
513 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
518 nir_intrinsic_instr
*
519 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
521 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
522 /* TODO: don't use rzalloc */
523 nir_intrinsic_instr
*instr
=
525 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
527 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
528 instr
->intrinsic
= op
;
530 if (nir_intrinsic_infos
[op
].has_dest
)
531 dest_init(&instr
->dest
);
533 for (unsigned i
= 0; i
< num_srcs
; i
++)
534 src_init(&instr
->src
[i
]);
540 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
542 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
543 instr_init(&instr
->instr
, nir_instr_type_call
);
545 instr
->callee
= callee
;
546 instr
->num_params
= callee
->num_params
;
547 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
548 instr
->return_deref
= NULL
;
554 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
556 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
557 instr_init(&instr
->instr
, nir_instr_type_tex
);
559 dest_init(&instr
->dest
);
561 instr
->num_srcs
= num_srcs
;
562 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
563 for (unsigned i
= 0; i
< num_srcs
; i
++)
564 src_init(&instr
->src
[i
].src
);
566 instr
->texture_index
= 0;
567 instr
->texture_array_size
= 0;
568 instr
->texture
= NULL
;
569 instr
->sampler_index
= 0;
570 instr
->sampler
= NULL
;
576 nir_tex_instr_add_src(nir_tex_instr
*tex
,
577 nir_tex_src_type src_type
,
580 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
583 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
584 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
585 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
589 ralloc_free(tex
->src
);
592 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
593 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
598 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
600 assert(src_idx
< tex
->num_srcs
);
602 /* First rewrite the source to NIR_SRC_INIT */
603 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
605 /* Now, move all of the other sources down */
606 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
607 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
608 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
614 nir_phi_instr_create(nir_shader
*shader
)
616 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
617 instr_init(&instr
->instr
, nir_instr_type_phi
);
619 dest_init(&instr
->dest
);
620 exec_list_make_empty(&instr
->srcs
);
624 nir_parallel_copy_instr
*
625 nir_parallel_copy_instr_create(nir_shader
*shader
)
627 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
628 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
630 exec_list_make_empty(&instr
->entries
);
635 nir_ssa_undef_instr
*
636 nir_ssa_undef_instr_create(nir_shader
*shader
,
637 unsigned num_components
,
640 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
641 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
643 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
649 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
651 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
652 deref
->deref
.deref_type
= nir_deref_type_var
;
653 deref
->deref
.child
= NULL
;
654 deref
->deref
.type
= var
->type
;
660 nir_deref_array_create(void *mem_ctx
)
662 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
663 deref
->deref
.deref_type
= nir_deref_type_array
;
664 deref
->deref
.child
= NULL
;
665 deref
->deref_array_type
= nir_deref_array_type_direct
;
666 src_init(&deref
->indirect
);
667 deref
->base_offset
= 0;
672 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
674 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
675 deref
->deref
.deref_type
= nir_deref_type_struct
;
676 deref
->deref
.child
= NULL
;
677 deref
->index
= field_index
;
682 nir_deref_var_clone(const nir_deref_var
*deref
, void *mem_ctx
)
687 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
688 ret
->deref
.type
= deref
->deref
.type
;
689 if (deref
->deref
.child
)
690 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
694 static nir_deref_array
*
695 deref_array_clone(const nir_deref_array
*deref
, void *mem_ctx
)
697 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
698 ret
->base_offset
= deref
->base_offset
;
699 ret
->deref_array_type
= deref
->deref_array_type
;
700 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
701 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
703 ret
->deref
.type
= deref
->deref
.type
;
704 if (deref
->deref
.child
)
705 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
709 static nir_deref_struct
*
710 deref_struct_clone(const nir_deref_struct
*deref
, void *mem_ctx
)
712 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
713 ret
->deref
.type
= deref
->deref
.type
;
714 if (deref
->deref
.child
)
715 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
720 nir_deref_clone(const nir_deref
*deref
, void *mem_ctx
)
725 switch (deref
->deref_type
) {
726 case nir_deref_type_var
:
727 return &nir_deref_var_clone(nir_deref_as_var(deref
), mem_ctx
)->deref
;
728 case nir_deref_type_array
:
729 return &deref_array_clone(nir_deref_as_array(deref
), mem_ctx
)->deref
;
730 case nir_deref_type_struct
:
731 return &deref_struct_clone(nir_deref_as_struct(deref
), mem_ctx
)->deref
;
733 unreachable("Invalid dereference type");
739 /* This is the second step in the recursion. We've found the tail and made a
740 * copy. Now we need to iterate over all possible leaves and call the
741 * callback on each one.
744 deref_foreach_leaf_build_recur(nir_deref_var
*deref
, nir_deref
*tail
,
745 nir_deref_foreach_leaf_cb cb
, void *state
)
750 nir_deref_struct str
;
753 assert(tail
->child
== NULL
);
754 switch (glsl_get_base_type(tail
->type
)) {
756 case GLSL_TYPE_UINT16
:
757 case GLSL_TYPE_UINT64
:
759 case GLSL_TYPE_INT16
:
760 case GLSL_TYPE_INT64
:
761 case GLSL_TYPE_FLOAT
:
762 case GLSL_TYPE_FLOAT16
:
763 case GLSL_TYPE_DOUBLE
:
765 if (glsl_type_is_vector_or_scalar(tail
->type
))
766 return cb(deref
, state
);
769 case GLSL_TYPE_ARRAY
:
770 tmp
.arr
.deref
.deref_type
= nir_deref_type_array
;
771 tmp
.arr
.deref
.type
= glsl_get_array_element(tail
->type
);
772 tmp
.arr
.deref_array_type
= nir_deref_array_type_direct
;
773 tmp
.arr
.indirect
= NIR_SRC_INIT
;
774 tail
->child
= &tmp
.arr
.deref
;
776 length
= glsl_get_length(tail
->type
);
777 for (unsigned i
= 0; i
< length
; i
++) {
778 tmp
.arr
.deref
.child
= NULL
;
779 tmp
.arr
.base_offset
= i
;
780 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
785 case GLSL_TYPE_STRUCT
:
786 tmp
.str
.deref
.deref_type
= nir_deref_type_struct
;
787 tail
->child
= &tmp
.str
.deref
;
789 length
= glsl_get_length(tail
->type
);
790 for (unsigned i
= 0; i
< length
; i
++) {
791 tmp
.arr
.deref
.child
= NULL
;
792 tmp
.str
.deref
.type
= glsl_get_struct_field(tail
->type
, i
);
794 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
800 unreachable("Invalid type for dereference");
804 /* This is the first step of the foreach_leaf recursion. In this step we are
805 * walking to the end of the deref chain and making a copy in the stack as we
806 * go. This is because we don't want to mutate the deref chain that was
807 * passed in by the caller. The downside is that this deref chain is on the
808 * stack and , if the caller wants to do anything with it, they will have to
809 * make their own copy because this one will go away.
812 deref_foreach_leaf_copy_recur(nir_deref_var
*deref
, nir_deref
*tail
,
813 nir_deref_foreach_leaf_cb cb
, void *state
)
817 nir_deref_struct str
;
821 switch (tail
->child
->deref_type
) {
822 case nir_deref_type_array
:
823 c
.arr
= *nir_deref_as_array(tail
->child
);
824 tail
->child
= &c
.arr
.deref
;
825 return deref_foreach_leaf_copy_recur(deref
, &c
.arr
.deref
, cb
, state
);
827 case nir_deref_type_struct
:
828 c
.str
= *nir_deref_as_struct(tail
->child
);
829 tail
->child
= &c
.str
.deref
;
830 return deref_foreach_leaf_copy_recur(deref
, &c
.str
.deref
, cb
, state
);
832 case nir_deref_type_var
:
834 unreachable("Invalid deref type for a child");
837 /* We've gotten to the end of the original deref. Time to start
838 * building our own derefs.
840 return deref_foreach_leaf_build_recur(deref
, tail
, cb
, state
);
845 * This function iterates over all of the possible derefs that can be created
846 * with the given deref as the head. It then calls the provided callback with
847 * a full deref for each one.
849 * The deref passed to the callback will be allocated on the stack. You will
850 * need to make a copy if you want it to hang around.
853 nir_deref_foreach_leaf(nir_deref_var
*deref
,
854 nir_deref_foreach_leaf_cb cb
, void *state
)
856 nir_deref_var copy
= *deref
;
857 return deref_foreach_leaf_copy_recur(©
, ©
.deref
, cb
, state
);
860 /* Returns a load_const instruction that represents the constant
861 * initializer for the given deref chain. The caller is responsible for
862 * ensuring that there actually is a constant initializer.
864 nir_load_const_instr
*
865 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
867 nir_constant
*constant
= deref
->var
->constant_initializer
;
870 const nir_deref
*tail
= &deref
->deref
;
871 unsigned matrix_col
= 0;
872 while (tail
->child
) {
873 switch (tail
->child
->deref_type
) {
874 case nir_deref_type_array
: {
875 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
876 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
877 if (glsl_type_is_matrix(tail
->type
)) {
878 assert(arr
->deref
.child
== NULL
);
879 matrix_col
= arr
->base_offset
;
881 constant
= constant
->elements
[arr
->base_offset
];
886 case nir_deref_type_struct
: {
887 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
892 unreachable("Invalid deref child type");
898 unsigned bit_size
= glsl_get_bit_size(tail
->type
);
899 nir_load_const_instr
*load
=
900 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
),
903 switch (glsl_get_base_type(tail
->type
)) {
904 case GLSL_TYPE_FLOAT
:
907 case GLSL_TYPE_FLOAT16
:
908 case GLSL_TYPE_DOUBLE
:
909 case GLSL_TYPE_INT16
:
910 case GLSL_TYPE_UINT16
:
911 case GLSL_TYPE_UINT64
:
912 case GLSL_TYPE_INT64
:
914 load
->value
= constant
->values
[matrix_col
];
917 unreachable("Invalid immediate type");
923 static nir_const_value
924 const_value_float(double d
, unsigned bit_size
)
928 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
929 case 32: v
.f32
[0] = d
; break;
930 case 64: v
.f64
[0] = d
; break;
932 unreachable("Invalid bit size");
937 static nir_const_value
938 const_value_int(int64_t i
, unsigned bit_size
)
942 case 8: v
.i8
[0] = i
; break;
943 case 16: v
.i16
[0] = i
; break;
944 case 32: v
.i32
[0] = i
; break;
945 case 64: v
.i64
[0] = i
; break;
947 unreachable("Invalid bit size");
953 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
955 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
956 const int64_t min_int
= -max_int
- 1;
959 return const_value_int(0, bit_size
);
961 return const_value_float(0, bit_size
);
963 return const_value_int(1, bit_size
);
965 return const_value_float(1, bit_size
);
967 return const_value_int(max_int
, bit_size
);
969 return const_value_int(~0ull, bit_size
);
971 return const_value_float(INFINITY
, bit_size
);
973 return const_value_int(min_int
, bit_size
);
975 return const_value_int(0, bit_size
);
977 return const_value_float(-INFINITY
, bit_size
);
979 return const_value_int(~0ull, bit_size
);
981 return const_value_int(0, bit_size
);
983 return const_value_int(0, bit_size
);
985 unreachable("Invalid reduction operation");
990 nir_cf_node_get_function(nir_cf_node
*node
)
992 while (node
->type
!= nir_cf_node_function
) {
996 return nir_cf_node_as_function(node
);
999 /* Reduces a cursor by trying to convert everything to after and trying to
1000 * go up to block granularity when possible.
1003 reduce_cursor(nir_cursor cursor
)
1005 switch (cursor
.option
) {
1006 case nir_cursor_before_block
:
1007 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
1008 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
1009 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
1010 /* Empty block. After is as good as before. */
1011 cursor
.option
= nir_cursor_after_block
;
1015 case nir_cursor_after_block
:
1018 case nir_cursor_before_instr
: {
1019 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
1021 /* Before this instruction is after the previous */
1022 cursor
.instr
= prev_instr
;
1023 cursor
.option
= nir_cursor_after_instr
;
1025 /* No previous instruction. Switch to before block */
1026 cursor
.block
= cursor
.instr
->block
;
1027 cursor
.option
= nir_cursor_before_block
;
1029 return reduce_cursor(cursor
);
1032 case nir_cursor_after_instr
:
1033 if (nir_instr_next(cursor
.instr
) == NULL
) {
1034 /* This is the last instruction, switch to after block */
1035 cursor
.option
= nir_cursor_after_block
;
1036 cursor
.block
= cursor
.instr
->block
;
1041 unreachable("Inavlid cursor option");
1046 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
1048 /* Reduced cursors should be unique */
1049 a
= reduce_cursor(a
);
1050 b
= reduce_cursor(b
);
1052 return a
.block
== b
.block
&& a
.option
== b
.option
;
1056 add_use_cb(nir_src
*src
, void *state
)
1058 nir_instr
*instr
= state
;
1060 src
->parent_instr
= instr
;
1061 list_addtail(&src
->use_link
,
1062 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
1068 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1070 nir_instr
*instr
= state
;
1072 if (instr
->block
&& def
->index
== UINT_MAX
) {
1073 nir_function_impl
*impl
=
1074 nir_cf_node_get_function(&instr
->block
->cf_node
);
1076 def
->index
= impl
->ssa_alloc
++;
1083 add_reg_def_cb(nir_dest
*dest
, void *state
)
1085 nir_instr
*instr
= state
;
1087 if (!dest
->is_ssa
) {
1088 dest
->reg
.parent_instr
= instr
;
1089 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
1096 add_defs_uses(nir_instr
*instr
)
1098 nir_foreach_src(instr
, add_use_cb
, instr
);
1099 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
1100 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
1104 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
1106 switch (cursor
.option
) {
1107 case nir_cursor_before_block
:
1108 /* Only allow inserting jumps into empty blocks. */
1109 if (instr
->type
== nir_instr_type_jump
)
1110 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
1112 instr
->block
= cursor
.block
;
1113 add_defs_uses(instr
);
1114 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
1116 case nir_cursor_after_block
: {
1117 /* Inserting instructions after a jump is illegal. */
1118 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
1119 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
1122 instr
->block
= cursor
.block
;
1123 add_defs_uses(instr
);
1124 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
1127 case nir_cursor_before_instr
:
1128 assert(instr
->type
!= nir_instr_type_jump
);
1129 instr
->block
= cursor
.instr
->block
;
1130 add_defs_uses(instr
);
1131 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
1133 case nir_cursor_after_instr
:
1134 /* Inserting instructions after a jump is illegal. */
1135 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
1137 /* Only allow inserting jumps at the end of the block. */
1138 if (instr
->type
== nir_instr_type_jump
)
1139 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
1141 instr
->block
= cursor
.instr
->block
;
1142 add_defs_uses(instr
);
1143 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
1147 if (instr
->type
== nir_instr_type_jump
)
1148 nir_handle_add_jump(instr
->block
);
1152 src_is_valid(const nir_src
*src
)
1154 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
1158 remove_use_cb(nir_src
*src
, void *state
)
1162 if (src_is_valid(src
))
1163 list_del(&src
->use_link
);
1169 remove_def_cb(nir_dest
*dest
, void *state
)
1174 list_del(&dest
->reg
.def_link
);
1180 remove_defs_uses(nir_instr
*instr
)
1182 nir_foreach_dest(instr
, remove_def_cb
, instr
);
1183 nir_foreach_src(instr
, remove_use_cb
, instr
);
1186 void nir_instr_remove_v(nir_instr
*instr
)
1188 remove_defs_uses(instr
);
1189 exec_node_remove(&instr
->node
);
1191 if (instr
->type
== nir_instr_type_jump
) {
1192 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
1193 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
1200 nir_index_local_regs(nir_function_impl
*impl
)
1203 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1204 reg
->index
= index
++;
1206 impl
->reg_alloc
= index
;
1210 nir_index_global_regs(nir_shader
*shader
)
1213 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1214 reg
->index
= index
++;
1216 shader
->reg_alloc
= index
;
1220 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1222 return cb(&instr
->dest
.dest
, state
);
1226 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1228 return cb(&instr
->dest
, state
);
1232 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
1235 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1236 return cb(&instr
->dest
, state
);
1242 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
1245 return cb(&instr
->dest
, state
);
1249 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1251 return cb(&instr
->dest
, state
);
1255 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
1256 nir_foreach_dest_cb cb
, void *state
)
1258 nir_foreach_parallel_copy_entry(entry
, instr
) {
1259 if (!cb(&entry
->dest
, state
))
1267 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1269 switch (instr
->type
) {
1270 case nir_instr_type_alu
:
1271 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
1272 case nir_instr_type_deref
:
1273 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
1274 case nir_instr_type_intrinsic
:
1275 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
1276 case nir_instr_type_tex
:
1277 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
1278 case nir_instr_type_phi
:
1279 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1280 case nir_instr_type_parallel_copy
:
1281 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1284 case nir_instr_type_load_const
:
1285 case nir_instr_type_ssa_undef
:
1286 case nir_instr_type_call
:
1287 case nir_instr_type_jump
:
1291 unreachable("Invalid instruction type");
1298 struct foreach_ssa_def_state
{
1299 nir_foreach_ssa_def_cb cb
;
1304 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1306 struct foreach_ssa_def_state
*state
= void_state
;
1309 return state
->cb(&dest
->ssa
, state
->client_state
);
1315 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1317 switch (instr
->type
) {
1318 case nir_instr_type_alu
:
1319 case nir_instr_type_deref
:
1320 case nir_instr_type_tex
:
1321 case nir_instr_type_intrinsic
:
1322 case nir_instr_type_phi
:
1323 case nir_instr_type_parallel_copy
: {
1324 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1325 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1328 case nir_instr_type_load_const
:
1329 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1330 case nir_instr_type_ssa_undef
:
1331 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1332 case nir_instr_type_call
:
1333 case nir_instr_type_jump
:
1336 unreachable("Invalid instruction type");
1341 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1343 if (!cb(src
, state
))
1345 if (!src
->is_ssa
&& src
->reg
.indirect
)
1346 return cb(src
->reg
.indirect
, state
);
1351 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
1354 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
1355 return visit_src(&deref
->indirect
, cb
, state
);
1360 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
1362 nir_deref
*cur
= &deref
->deref
;
1363 while (cur
!= NULL
) {
1364 if (cur
->deref_type
== nir_deref_type_array
) {
1365 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
1376 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1378 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1379 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1386 visit_deref_instr_src(nir_deref_instr
*instr
,
1387 nir_foreach_src_cb cb
, void *state
)
1389 if (instr
->deref_type
!= nir_deref_type_var
) {
1390 if (!visit_src(&instr
->parent
, cb
, state
))
1394 if (instr
->deref_type
== nir_deref_type_array
) {
1395 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1403 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1405 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1406 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1410 if (instr
->texture
!= NULL
) {
1411 if (!visit_deref_src(instr
->texture
, cb
, state
))
1415 if (instr
->sampler
!= NULL
) {
1416 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1424 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1427 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1428 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1429 if (!visit_src(&instr
->src
[i
], cb
, state
))
1434 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1435 for (unsigned i
= 0; i
< num_vars
; i
++) {
1436 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1444 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1446 nir_foreach_phi_src(src
, instr
) {
1447 if (!visit_src(&src
->src
, cb
, state
))
1455 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1456 nir_foreach_src_cb cb
, void *state
)
1458 nir_foreach_parallel_copy_entry(entry
, instr
) {
1459 if (!visit_src(&entry
->src
, cb
, state
))
1468 nir_foreach_src_cb cb
;
1469 } visit_dest_indirect_state
;
1472 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1474 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1476 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1477 return state
->cb(dest
->reg
.indirect
, state
->state
);
1483 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1485 switch (instr
->type
) {
1486 case nir_instr_type_alu
:
1487 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1490 case nir_instr_type_deref
:
1491 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1494 case nir_instr_type_intrinsic
:
1495 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1498 case nir_instr_type_tex
:
1499 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1502 case nir_instr_type_call
:
1503 /* Call instructions have no regular sources */
1505 case nir_instr_type_load_const
:
1506 /* Constant load instructions have no regular sources */
1508 case nir_instr_type_phi
:
1509 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1512 case nir_instr_type_parallel_copy
:
1513 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1517 case nir_instr_type_jump
:
1518 case nir_instr_type_ssa_undef
:
1522 unreachable("Invalid instruction type");
1526 visit_dest_indirect_state dest_state
;
1527 dest_state
.state
= state
;
1529 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1533 nir_src_as_const_value(nir_src src
)
1538 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1541 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1543 return &load
->value
;
1547 * Returns true if the source is known to be dynamically uniform. Otherwise it
1548 * returns false which means it may or may not be dynamically uniform but it
1549 * can't be determined.
1552 nir_src_is_dynamically_uniform(nir_src src
)
1557 /* Constants are trivially dynamically uniform */
1558 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1561 /* As are uniform variables */
1562 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1563 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1565 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1569 /* XXX: this could have many more tests, such as when a sampler function is
1570 * called with dynamically uniform arguments.
1576 src_remove_all_uses(nir_src
*src
)
1578 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1579 if (!src_is_valid(src
))
1582 list_del(&src
->use_link
);
1587 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1589 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1590 if (!src_is_valid(src
))
1594 src
->parent_instr
= parent_instr
;
1596 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1598 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1601 src
->parent_if
= parent_if
;
1603 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1605 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1611 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1613 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1615 src_remove_all_uses(src
);
1617 src_add_all_uses(src
, instr
, NULL
);
1621 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1623 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1625 src_remove_all_uses(dest
);
1626 src_remove_all_uses(src
);
1628 *src
= NIR_SRC_INIT
;
1629 src_add_all_uses(dest
, dest_instr
, NULL
);
1633 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1635 nir_src
*src
= &if_stmt
->condition
;
1636 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1638 src_remove_all_uses(src
);
1640 src_add_all_uses(src
, NULL
, if_stmt
);
1644 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1647 /* We can only overwrite an SSA destination if it has no uses. */
1648 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1650 list_del(&dest
->reg
.def_link
);
1651 if (dest
->reg
.indirect
)
1652 src_remove_all_uses(dest
->reg
.indirect
);
1655 /* We can't re-write with an SSA def */
1656 assert(!new_dest
.is_ssa
);
1658 nir_dest_copy(dest
, &new_dest
, instr
);
1660 dest
->reg
.parent_instr
= instr
;
1661 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1663 if (dest
->reg
.indirect
)
1664 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1668 nir_instr_rewrite_deref(nir_instr
*instr
, nir_deref_var
**deref
,
1669 nir_deref_var
*new_deref
)
1672 visit_deref_src(*deref
, remove_use_cb
, NULL
);
1677 visit_deref_src(*deref
, add_use_cb
, instr
);
1680 /* note: does *not* take ownership of 'name' */
1682 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1683 unsigned num_components
,
1684 unsigned bit_size
, const char *name
)
1686 def
->name
= ralloc_strdup(instr
, name
);
1687 def
->parent_instr
= instr
;
1688 list_inithead(&def
->uses
);
1689 list_inithead(&def
->if_uses
);
1690 def
->num_components
= num_components
;
1691 def
->bit_size
= bit_size
;
1694 nir_function_impl
*impl
=
1695 nir_cf_node_get_function(&instr
->block
->cf_node
);
1697 def
->index
= impl
->ssa_alloc
++;
1699 def
->index
= UINT_MAX
;
1703 /* note: does *not* take ownership of 'name' */
1705 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1706 unsigned num_components
, unsigned bit_size
,
1709 dest
->is_ssa
= true;
1710 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1714 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1716 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1718 nir_foreach_use_safe(use_src
, def
)
1719 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1721 nir_foreach_if_use_safe(use_src
, def
)
1722 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1726 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1728 assert(start
->block
== end
->block
);
1730 if (between
->block
!= start
->block
)
1733 /* Search backwards looking for "between" */
1734 while (start
!= end
) {
1738 end
= nir_instr_prev(end
);
1745 /* Replaces all uses of the given SSA def with the given source but only if
1746 * the use comes after the after_me instruction. This can be useful if you
1747 * are emitting code to fix up the result of some instruction: you can freely
1748 * use the result in that code and then call rewrite_uses_after and pass the
1749 * last fixup instruction as after_me and it will replace all of the uses you
1750 * want without touching the fixup code.
1752 * This function assumes that after_me is in the same block as
1753 * def->parent_instr and that after_me comes after def->parent_instr.
1756 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1757 nir_instr
*after_me
)
1759 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1761 nir_foreach_use_safe(use_src
, def
) {
1762 assert(use_src
->parent_instr
!= def
->parent_instr
);
1763 /* Since def already dominates all of its uses, the only way a use can
1764 * not be dominated by after_me is if it is between def and after_me in
1765 * the instruction list.
1767 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1768 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1771 nir_foreach_if_use_safe(use_src
, def
)
1772 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1776 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1778 uint8_t read_mask
= 0;
1779 nir_foreach_use(use
, def
) {
1780 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1781 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1782 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1783 int src_idx
= alu_src
- &alu
->src
[0];
1784 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1786 for (unsigned c
= 0; c
< 4; c
++) {
1787 if (!nir_alu_instr_channel_used(alu
, src_idx
, c
))
1790 read_mask
|= (1 << alu_src
->swizzle
[c
]);
1793 return (1 << def
->num_components
) - 1;
1801 nir_block_cf_tree_next(nir_block
*block
)
1803 if (block
== NULL
) {
1804 /* nir_foreach_block_safe() will call this function on a NULL block
1805 * after the last iteration, but it won't use the result so just return
1811 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1813 return nir_cf_node_cf_tree_first(cf_next
);
1815 nir_cf_node
*parent
= block
->cf_node
.parent
;
1817 switch (parent
->type
) {
1818 case nir_cf_node_if
: {
1819 /* Are we at the end of the if? Go to the beginning of the else */
1820 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1821 if (block
== nir_if_last_then_block(if_stmt
))
1822 return nir_if_first_else_block(if_stmt
);
1824 assert(block
== nir_if_last_else_block(if_stmt
));
1828 case nir_cf_node_loop
:
1829 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1831 case nir_cf_node_function
:
1835 unreachable("unknown cf node type");
1840 nir_block_cf_tree_prev(nir_block
*block
)
1842 if (block
== NULL
) {
1843 /* do this for consistency with nir_block_cf_tree_next() */
1847 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1849 return nir_cf_node_cf_tree_last(cf_prev
);
1851 nir_cf_node
*parent
= block
->cf_node
.parent
;
1853 switch (parent
->type
) {
1854 case nir_cf_node_if
: {
1855 /* Are we at the beginning of the else? Go to the end of the if */
1856 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1857 if (block
== nir_if_first_else_block(if_stmt
))
1858 return nir_if_last_then_block(if_stmt
);
1860 assert(block
== nir_if_first_then_block(if_stmt
));
1864 case nir_cf_node_loop
:
1865 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1867 case nir_cf_node_function
:
1871 unreachable("unknown cf node type");
1875 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1877 switch (node
->type
) {
1878 case nir_cf_node_function
: {
1879 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1880 return nir_start_block(impl
);
1883 case nir_cf_node_if
: {
1884 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1885 return nir_if_first_then_block(if_stmt
);
1888 case nir_cf_node_loop
: {
1889 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1890 return nir_loop_first_block(loop
);
1893 case nir_cf_node_block
: {
1894 return nir_cf_node_as_block(node
);
1898 unreachable("unknown node type");
1902 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1904 switch (node
->type
) {
1905 case nir_cf_node_function
: {
1906 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1907 return nir_impl_last_block(impl
);
1910 case nir_cf_node_if
: {
1911 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1912 return nir_if_last_else_block(if_stmt
);
1915 case nir_cf_node_loop
: {
1916 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1917 return nir_loop_last_block(loop
);
1920 case nir_cf_node_block
: {
1921 return nir_cf_node_as_block(node
);
1925 unreachable("unknown node type");
1929 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1931 if (node
->type
== nir_cf_node_block
)
1932 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1933 else if (node
->type
== nir_cf_node_function
)
1936 return nir_cf_node_as_block(nir_cf_node_next(node
));
1940 nir_block_get_following_if(nir_block
*block
)
1942 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1945 if (nir_cf_node_is_last(&block
->cf_node
))
1948 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1950 if (next_node
->type
!= nir_cf_node_if
)
1953 return nir_cf_node_as_if(next_node
);
1957 nir_block_get_following_loop(nir_block
*block
)
1959 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1962 if (nir_cf_node_is_last(&block
->cf_node
))
1965 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1967 if (next_node
->type
!= nir_cf_node_loop
)
1970 return nir_cf_node_as_loop(next_node
);
1974 nir_index_blocks(nir_function_impl
*impl
)
1978 if (impl
->valid_metadata
& nir_metadata_block_index
)
1981 nir_foreach_block(block
, impl
) {
1982 block
->index
= index
++;
1985 impl
->num_blocks
= index
;
1989 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1991 unsigned *index
= (unsigned *) state
;
1992 def
->index
= (*index
)++;
1998 * The indices are applied top-to-bottom which has the very nice property
1999 * that, if A dominates B, then A->index <= B->index.
2002 nir_index_ssa_defs(nir_function_impl
*impl
)
2006 nir_foreach_block(block
, impl
) {
2007 nir_foreach_instr(instr
, block
)
2008 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
2011 impl
->ssa_alloc
= index
;
2015 * The indices are applied top-to-bottom which has the very nice property
2016 * that, if A dominates B, then A->index <= B->index.
2019 nir_index_instrs(nir_function_impl
*impl
)
2023 nir_foreach_block(block
, impl
) {
2024 nir_foreach_instr(instr
, block
)
2025 instr
->index
= index
++;
2032 nir_intrinsic_from_system_value(gl_system_value val
)
2035 case SYSTEM_VALUE_VERTEX_ID
:
2036 return nir_intrinsic_load_vertex_id
;
2037 case SYSTEM_VALUE_INSTANCE_ID
:
2038 return nir_intrinsic_load_instance_id
;
2039 case SYSTEM_VALUE_DRAW_ID
:
2040 return nir_intrinsic_load_draw_id
;
2041 case SYSTEM_VALUE_BASE_INSTANCE
:
2042 return nir_intrinsic_load_base_instance
;
2043 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
2044 return nir_intrinsic_load_vertex_id_zero_base
;
2045 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
2046 return nir_intrinsic_load_is_indexed_draw
;
2047 case SYSTEM_VALUE_FIRST_VERTEX
:
2048 return nir_intrinsic_load_first_vertex
;
2049 case SYSTEM_VALUE_BASE_VERTEX
:
2050 return nir_intrinsic_load_base_vertex
;
2051 case SYSTEM_VALUE_INVOCATION_ID
:
2052 return nir_intrinsic_load_invocation_id
;
2053 case SYSTEM_VALUE_FRAG_COORD
:
2054 return nir_intrinsic_load_frag_coord
;
2055 case SYSTEM_VALUE_FRONT_FACE
:
2056 return nir_intrinsic_load_front_face
;
2057 case SYSTEM_VALUE_SAMPLE_ID
:
2058 return nir_intrinsic_load_sample_id
;
2059 case SYSTEM_VALUE_SAMPLE_POS
:
2060 return nir_intrinsic_load_sample_pos
;
2061 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
2062 return nir_intrinsic_load_sample_mask_in
;
2063 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
2064 return nir_intrinsic_load_local_invocation_id
;
2065 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
2066 return nir_intrinsic_load_local_invocation_index
;
2067 case SYSTEM_VALUE_WORK_GROUP_ID
:
2068 return nir_intrinsic_load_work_group_id
;
2069 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
2070 return nir_intrinsic_load_num_work_groups
;
2071 case SYSTEM_VALUE_PRIMITIVE_ID
:
2072 return nir_intrinsic_load_primitive_id
;
2073 case SYSTEM_VALUE_TESS_COORD
:
2074 return nir_intrinsic_load_tess_coord
;
2075 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
2076 return nir_intrinsic_load_tess_level_outer
;
2077 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
2078 return nir_intrinsic_load_tess_level_inner
;
2079 case SYSTEM_VALUE_VERTICES_IN
:
2080 return nir_intrinsic_load_patch_vertices_in
;
2081 case SYSTEM_VALUE_HELPER_INVOCATION
:
2082 return nir_intrinsic_load_helper_invocation
;
2083 case SYSTEM_VALUE_VIEW_INDEX
:
2084 return nir_intrinsic_load_view_index
;
2085 case SYSTEM_VALUE_SUBGROUP_SIZE
:
2086 return nir_intrinsic_load_subgroup_size
;
2087 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
2088 return nir_intrinsic_load_subgroup_invocation
;
2089 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
2090 return nir_intrinsic_load_subgroup_eq_mask
;
2091 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
2092 return nir_intrinsic_load_subgroup_ge_mask
;
2093 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
2094 return nir_intrinsic_load_subgroup_gt_mask
;
2095 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
2096 return nir_intrinsic_load_subgroup_le_mask
;
2097 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
2098 return nir_intrinsic_load_subgroup_lt_mask
;
2099 case SYSTEM_VALUE_NUM_SUBGROUPS
:
2100 return nir_intrinsic_load_num_subgroups
;
2101 case SYSTEM_VALUE_SUBGROUP_ID
:
2102 return nir_intrinsic_load_subgroup_id
;
2103 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
2104 return nir_intrinsic_load_local_group_size
;
2105 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
2106 return nir_intrinsic_load_global_invocation_id
;
2108 unreachable("system value does not directly correspond to intrinsic");
2113 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
2116 case nir_intrinsic_load_vertex_id
:
2117 return SYSTEM_VALUE_VERTEX_ID
;
2118 case nir_intrinsic_load_instance_id
:
2119 return SYSTEM_VALUE_INSTANCE_ID
;
2120 case nir_intrinsic_load_draw_id
:
2121 return SYSTEM_VALUE_DRAW_ID
;
2122 case nir_intrinsic_load_base_instance
:
2123 return SYSTEM_VALUE_BASE_INSTANCE
;
2124 case nir_intrinsic_load_vertex_id_zero_base
:
2125 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2126 case nir_intrinsic_load_first_vertex
:
2127 return SYSTEM_VALUE_FIRST_VERTEX
;
2128 case nir_intrinsic_load_is_indexed_draw
:
2129 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
2130 case nir_intrinsic_load_base_vertex
:
2131 return SYSTEM_VALUE_BASE_VERTEX
;
2132 case nir_intrinsic_load_invocation_id
:
2133 return SYSTEM_VALUE_INVOCATION_ID
;
2134 case nir_intrinsic_load_frag_coord
:
2135 return SYSTEM_VALUE_FRAG_COORD
;
2136 case nir_intrinsic_load_front_face
:
2137 return SYSTEM_VALUE_FRONT_FACE
;
2138 case nir_intrinsic_load_sample_id
:
2139 return SYSTEM_VALUE_SAMPLE_ID
;
2140 case nir_intrinsic_load_sample_pos
:
2141 return SYSTEM_VALUE_SAMPLE_POS
;
2142 case nir_intrinsic_load_sample_mask_in
:
2143 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
2144 case nir_intrinsic_load_local_invocation_id
:
2145 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
2146 case nir_intrinsic_load_local_invocation_index
:
2147 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
2148 case nir_intrinsic_load_num_work_groups
:
2149 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2150 case nir_intrinsic_load_work_group_id
:
2151 return SYSTEM_VALUE_WORK_GROUP_ID
;
2152 case nir_intrinsic_load_primitive_id
:
2153 return SYSTEM_VALUE_PRIMITIVE_ID
;
2154 case nir_intrinsic_load_tess_coord
:
2155 return SYSTEM_VALUE_TESS_COORD
;
2156 case nir_intrinsic_load_tess_level_outer
:
2157 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2158 case nir_intrinsic_load_tess_level_inner
:
2159 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
2160 case nir_intrinsic_load_patch_vertices_in
:
2161 return SYSTEM_VALUE_VERTICES_IN
;
2162 case nir_intrinsic_load_helper_invocation
:
2163 return SYSTEM_VALUE_HELPER_INVOCATION
;
2164 case nir_intrinsic_load_view_index
:
2165 return SYSTEM_VALUE_VIEW_INDEX
;
2166 case nir_intrinsic_load_subgroup_size
:
2167 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2168 case nir_intrinsic_load_subgroup_invocation
:
2169 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2170 case nir_intrinsic_load_subgroup_eq_mask
:
2171 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2172 case nir_intrinsic_load_subgroup_ge_mask
:
2173 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2174 case nir_intrinsic_load_subgroup_gt_mask
:
2175 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2176 case nir_intrinsic_load_subgroup_le_mask
:
2177 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2178 case nir_intrinsic_load_subgroup_lt_mask
:
2179 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2180 case nir_intrinsic_load_num_subgroups
:
2181 return SYSTEM_VALUE_NUM_SUBGROUPS
;
2182 case nir_intrinsic_load_subgroup_id
:
2183 return SYSTEM_VALUE_SUBGROUP_ID
;
2184 case nir_intrinsic_load_local_group_size
:
2185 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
2186 case nir_intrinsic_load_global_invocation_id
:
2187 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
2189 unreachable("intrinsic doesn't produce a system value");