2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "nir_control_flow_private.h"
30 #include "util/half_float.h"
36 nir_shader_create(void *mem_ctx
,
37 gl_shader_stage stage
,
38 const nir_shader_compiler_options
*options
,
41 nir_shader
*shader
= rzalloc(mem_ctx
, nir_shader
);
43 exec_list_make_empty(&shader
->uniforms
);
44 exec_list_make_empty(&shader
->inputs
);
45 exec_list_make_empty(&shader
->outputs
);
46 exec_list_make_empty(&shader
->shared
);
48 shader
->options
= options
;
51 assert(si
->stage
== stage
);
54 shader
->info
.stage
= stage
;
57 exec_list_make_empty(&shader
->functions
);
58 exec_list_make_empty(&shader
->registers
);
59 exec_list_make_empty(&shader
->globals
);
60 exec_list_make_empty(&shader
->system_values
);
61 shader
->reg_alloc
= 0;
63 shader
->num_inputs
= 0;
64 shader
->num_outputs
= 0;
65 shader
->num_uniforms
= 0;
66 shader
->num_shared
= 0;
72 reg_create(void *mem_ctx
, struct exec_list
*list
)
74 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
76 list_inithead(®
->uses
);
77 list_inithead(®
->defs
);
78 list_inithead(®
->if_uses
);
80 reg
->num_components
= 0;
82 reg
->num_array_elems
= 0;
83 reg
->is_packed
= false;
86 exec_list_push_tail(list
, ®
->node
);
92 nir_global_reg_create(nir_shader
*shader
)
94 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
95 reg
->index
= shader
->reg_alloc
++;
96 reg
->is_global
= true;
102 nir_local_reg_create(nir_function_impl
*impl
)
104 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
105 reg
->index
= impl
->reg_alloc
++;
106 reg
->is_global
= false;
112 nir_reg_remove(nir_register
*reg
)
114 exec_node_remove(®
->node
);
118 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
120 switch (var
->data
.mode
) {
122 assert(!"invalid mode");
126 assert(!"nir_shader_add_variable cannot be used for local variables");
130 exec_list_push_tail(&shader
->globals
, &var
->node
);
133 case nir_var_shader_in
:
134 exec_list_push_tail(&shader
->inputs
, &var
->node
);
137 case nir_var_shader_out
:
138 exec_list_push_tail(&shader
->outputs
, &var
->node
);
141 case nir_var_uniform
:
142 case nir_var_shader_storage
:
143 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
147 assert(shader
->info
.stage
== MESA_SHADER_COMPUTE
);
148 exec_list_push_tail(&shader
->shared
, &var
->node
);
151 case nir_var_system_value
:
152 exec_list_push_tail(&shader
->system_values
, &var
->node
);
158 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
159 const struct glsl_type
*type
, const char *name
)
161 nir_variable
*var
= rzalloc(shader
, nir_variable
);
162 var
->name
= ralloc_strdup(var
, name
);
164 var
->data
.mode
= mode
;
166 if ((mode
== nir_var_shader_in
&&
167 shader
->info
.stage
!= MESA_SHADER_VERTEX
) ||
168 (mode
== nir_var_shader_out
&&
169 shader
->info
.stage
!= MESA_SHADER_FRAGMENT
))
170 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
172 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
173 var
->data
.read_only
= true;
175 nir_shader_add_variable(shader
, var
);
181 nir_local_variable_create(nir_function_impl
*impl
,
182 const struct glsl_type
*type
, const char *name
)
184 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
185 var
->name
= ralloc_strdup(var
, name
);
187 var
->data
.mode
= nir_var_local
;
189 nir_function_impl_add_variable(impl
, var
);
195 nir_function_create(nir_shader
*shader
, const char *name
)
197 nir_function
*func
= ralloc(shader
, nir_function
);
199 exec_list_push_tail(&shader
->functions
, &func
->node
);
201 func
->name
= ralloc_strdup(func
, name
);
202 func
->shader
= shader
;
203 func
->num_params
= 0;
210 /* NOTE: if the instruction you are copying a src to is already added
211 * to the IR, use nir_instr_rewrite_src() instead.
213 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
215 dest
->is_ssa
= src
->is_ssa
;
217 dest
->ssa
= src
->ssa
;
219 dest
->reg
.base_offset
= src
->reg
.base_offset
;
220 dest
->reg
.reg
= src
->reg
.reg
;
221 if (src
->reg
.indirect
) {
222 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
223 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
225 dest
->reg
.indirect
= NULL
;
230 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
232 /* Copying an SSA definition makes no sense whatsoever. */
233 assert(!src
->is_ssa
);
235 dest
->is_ssa
= false;
237 dest
->reg
.base_offset
= src
->reg
.base_offset
;
238 dest
->reg
.reg
= src
->reg
.reg
;
239 if (src
->reg
.indirect
) {
240 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
241 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
243 dest
->reg
.indirect
= NULL
;
248 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
249 nir_alu_instr
*instr
)
251 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
252 dest
->abs
= src
->abs
;
253 dest
->negate
= src
->negate
;
254 for (unsigned i
= 0; i
< 4; i
++)
255 dest
->swizzle
[i
] = src
->swizzle
[i
];
259 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
260 nir_alu_instr
*instr
)
262 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
263 dest
->write_mask
= src
->write_mask
;
264 dest
->saturate
= src
->saturate
;
269 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
271 exec_node_init(&node
->node
);
277 nir_function_impl_create_bare(nir_shader
*shader
)
279 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
281 impl
->function
= NULL
;
283 cf_init(&impl
->cf_node
, nir_cf_node_function
);
285 exec_list_make_empty(&impl
->body
);
286 exec_list_make_empty(&impl
->registers
);
287 exec_list_make_empty(&impl
->locals
);
290 impl
->valid_metadata
= nir_metadata_none
;
292 /* create start & end blocks */
293 nir_block
*start_block
= nir_block_create(shader
);
294 nir_block
*end_block
= nir_block_create(shader
);
295 start_block
->cf_node
.parent
= &impl
->cf_node
;
296 end_block
->cf_node
.parent
= &impl
->cf_node
;
297 impl
->end_block
= end_block
;
299 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
301 start_block
->successors
[0] = end_block
;
302 _mesa_set_add(end_block
->predecessors
, start_block
);
307 nir_function_impl_create(nir_function
*function
)
309 assert(function
->impl
== NULL
);
311 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
313 function
->impl
= impl
;
314 impl
->function
= function
;
320 nir_block_create(nir_shader
*shader
)
322 nir_block
*block
= rzalloc(shader
, nir_block
);
324 cf_init(&block
->cf_node
, nir_cf_node_block
);
326 block
->successors
[0] = block
->successors
[1] = NULL
;
327 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
328 _mesa_key_pointer_equal
);
329 block
->imm_dom
= NULL
;
330 /* XXX maybe it would be worth it to defer allocation? This
331 * way it doesn't get allocated for shader refs that never run
332 * nir_calc_dominance? For example, state-tracker creates an
333 * initial IR, clones that, runs appropriate lowering pass, passes
334 * to driver which does common lowering/opt, and then stores ref
335 * which is later used to do state specific lowering and futher
336 * opt. Do any of the references not need dominance metadata?
338 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
339 _mesa_key_pointer_equal
);
341 exec_list_make_empty(&block
->instr_list
);
347 src_init(nir_src
*src
)
351 src
->reg
.indirect
= NULL
;
352 src
->reg
.base_offset
= 0;
356 nir_if_create(nir_shader
*shader
)
358 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
360 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
361 src_init(&if_stmt
->condition
);
363 nir_block
*then
= nir_block_create(shader
);
364 exec_list_make_empty(&if_stmt
->then_list
);
365 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
366 then
->cf_node
.parent
= &if_stmt
->cf_node
;
368 nir_block
*else_stmt
= nir_block_create(shader
);
369 exec_list_make_empty(&if_stmt
->else_list
);
370 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
371 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
377 nir_loop_create(nir_shader
*shader
)
379 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
381 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
383 nir_block
*body
= nir_block_create(shader
);
384 exec_list_make_empty(&loop
->body
);
385 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
386 body
->cf_node
.parent
= &loop
->cf_node
;
388 body
->successors
[0] = body
;
389 _mesa_set_add(body
->predecessors
, body
);
395 instr_init(nir_instr
*instr
, nir_instr_type type
)
399 exec_node_init(&instr
->node
);
403 dest_init(nir_dest
*dest
)
405 dest
->is_ssa
= false;
406 dest
->reg
.reg
= NULL
;
407 dest
->reg
.indirect
= NULL
;
408 dest
->reg
.base_offset
= 0;
412 alu_dest_init(nir_alu_dest
*dest
)
414 dest_init(&dest
->dest
);
415 dest
->saturate
= false;
416 dest
->write_mask
= 0xf;
420 alu_src_init(nir_alu_src
*src
)
423 src
->abs
= src
->negate
= false;
431 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
433 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
434 /* TODO: don't use rzalloc */
435 nir_alu_instr
*instr
=
437 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
439 instr_init(&instr
->instr
, nir_instr_type_alu
);
441 alu_dest_init(&instr
->dest
);
442 for (unsigned i
= 0; i
< num_srcs
; i
++)
443 alu_src_init(&instr
->src
[i
]);
449 nir_deref_instr_create(nir_shader
*shader
, nir_deref_type deref_type
)
451 nir_deref_instr
*instr
=
452 rzalloc_size(shader
, sizeof(nir_deref_instr
));
454 instr_init(&instr
->instr
, nir_instr_type_deref
);
456 instr
->deref_type
= deref_type
;
457 if (deref_type
!= nir_deref_type_var
)
458 src_init(&instr
->parent
);
460 if (deref_type
== nir_deref_type_array
)
461 src_init(&instr
->arr
.index
);
463 dest_init(&instr
->dest
);
469 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
471 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
472 instr_init(&instr
->instr
, nir_instr_type_jump
);
477 nir_load_const_instr
*
478 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
481 nir_load_const_instr
*instr
= rzalloc(shader
, nir_load_const_instr
);
482 instr_init(&instr
->instr
, nir_instr_type_load_const
);
484 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
489 nir_intrinsic_instr
*
490 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
492 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
493 /* TODO: don't use rzalloc */
494 nir_intrinsic_instr
*instr
=
496 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
498 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
499 instr
->intrinsic
= op
;
501 if (nir_intrinsic_infos
[op
].has_dest
)
502 dest_init(&instr
->dest
);
504 for (unsigned i
= 0; i
< num_srcs
; i
++)
505 src_init(&instr
->src
[i
]);
511 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
513 const unsigned num_params
= callee
->num_params
;
514 nir_call_instr
*instr
=
515 rzalloc_size(shader
, sizeof(*instr
) +
516 num_params
* sizeof(instr
->params
[0]));
518 instr_init(&instr
->instr
, nir_instr_type_call
);
519 instr
->callee
= callee
;
520 instr
->num_params
= num_params
;
521 for (unsigned i
= 0; i
< num_params
; i
++)
522 src_init(&instr
->params
[i
]);
528 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
530 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
531 instr_init(&instr
->instr
, nir_instr_type_tex
);
533 dest_init(&instr
->dest
);
535 instr
->num_srcs
= num_srcs
;
536 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
537 for (unsigned i
= 0; i
< num_srcs
; i
++)
538 src_init(&instr
->src
[i
].src
);
540 instr
->texture_index
= 0;
541 instr
->texture_array_size
= 0;
542 instr
->sampler_index
= 0;
548 nir_tex_instr_add_src(nir_tex_instr
*tex
,
549 nir_tex_src_type src_type
,
552 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
555 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
556 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
557 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
561 ralloc_free(tex
->src
);
564 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
565 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
570 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
572 assert(src_idx
< tex
->num_srcs
);
574 /* First rewrite the source to NIR_SRC_INIT */
575 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
577 /* Now, move all of the other sources down */
578 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
579 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
580 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
586 nir_phi_instr_create(nir_shader
*shader
)
588 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
589 instr_init(&instr
->instr
, nir_instr_type_phi
);
591 dest_init(&instr
->dest
);
592 exec_list_make_empty(&instr
->srcs
);
596 nir_parallel_copy_instr
*
597 nir_parallel_copy_instr_create(nir_shader
*shader
)
599 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
600 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
602 exec_list_make_empty(&instr
->entries
);
607 nir_ssa_undef_instr
*
608 nir_ssa_undef_instr_create(nir_shader
*shader
,
609 unsigned num_components
,
612 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
613 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
615 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
620 static nir_const_value
621 const_value_float(double d
, unsigned bit_size
)
625 case 16: v
.u16
[0] = _mesa_float_to_half(d
); break;
626 case 32: v
.f32
[0] = d
; break;
627 case 64: v
.f64
[0] = d
; break;
629 unreachable("Invalid bit size");
634 static nir_const_value
635 const_value_int(int64_t i
, unsigned bit_size
)
639 case 8: v
.i8
[0] = i
; break;
640 case 16: v
.i16
[0] = i
; break;
641 case 32: v
.i32
[0] = i
; break;
642 case 64: v
.i64
[0] = i
; break;
644 unreachable("Invalid bit size");
650 nir_alu_binop_identity(nir_op binop
, unsigned bit_size
)
652 const int64_t max_int
= (1ull << (bit_size
- 1)) - 1;
653 const int64_t min_int
= -max_int
- 1;
656 return const_value_int(0, bit_size
);
658 return const_value_float(0, bit_size
);
660 return const_value_int(1, bit_size
);
662 return const_value_float(1, bit_size
);
664 return const_value_int(max_int
, bit_size
);
666 return const_value_int(~0ull, bit_size
);
668 return const_value_float(INFINITY
, bit_size
);
670 return const_value_int(min_int
, bit_size
);
672 return const_value_int(0, bit_size
);
674 return const_value_float(-INFINITY
, bit_size
);
676 return const_value_int(~0ull, bit_size
);
678 return const_value_int(0, bit_size
);
680 return const_value_int(0, bit_size
);
682 unreachable("Invalid reduction operation");
687 nir_cf_node_get_function(nir_cf_node
*node
)
689 while (node
->type
!= nir_cf_node_function
) {
693 return nir_cf_node_as_function(node
);
696 /* Reduces a cursor by trying to convert everything to after and trying to
697 * go up to block granularity when possible.
700 reduce_cursor(nir_cursor cursor
)
702 switch (cursor
.option
) {
703 case nir_cursor_before_block
:
704 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
705 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
706 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
707 /* Empty block. After is as good as before. */
708 cursor
.option
= nir_cursor_after_block
;
712 case nir_cursor_after_block
:
715 case nir_cursor_before_instr
: {
716 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
718 /* Before this instruction is after the previous */
719 cursor
.instr
= prev_instr
;
720 cursor
.option
= nir_cursor_after_instr
;
722 /* No previous instruction. Switch to before block */
723 cursor
.block
= cursor
.instr
->block
;
724 cursor
.option
= nir_cursor_before_block
;
726 return reduce_cursor(cursor
);
729 case nir_cursor_after_instr
:
730 if (nir_instr_next(cursor
.instr
) == NULL
) {
731 /* This is the last instruction, switch to after block */
732 cursor
.option
= nir_cursor_after_block
;
733 cursor
.block
= cursor
.instr
->block
;
738 unreachable("Inavlid cursor option");
743 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
745 /* Reduced cursors should be unique */
746 a
= reduce_cursor(a
);
747 b
= reduce_cursor(b
);
749 return a
.block
== b
.block
&& a
.option
== b
.option
;
753 add_use_cb(nir_src
*src
, void *state
)
755 nir_instr
*instr
= state
;
757 src
->parent_instr
= instr
;
758 list_addtail(&src
->use_link
,
759 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
765 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
767 nir_instr
*instr
= state
;
769 if (instr
->block
&& def
->index
== UINT_MAX
) {
770 nir_function_impl
*impl
=
771 nir_cf_node_get_function(&instr
->block
->cf_node
);
773 def
->index
= impl
->ssa_alloc
++;
780 add_reg_def_cb(nir_dest
*dest
, void *state
)
782 nir_instr
*instr
= state
;
785 dest
->reg
.parent_instr
= instr
;
786 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
793 add_defs_uses(nir_instr
*instr
)
795 nir_foreach_src(instr
, add_use_cb
, instr
);
796 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
797 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
801 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
803 switch (cursor
.option
) {
804 case nir_cursor_before_block
:
805 /* Only allow inserting jumps into empty blocks. */
806 if (instr
->type
== nir_instr_type_jump
)
807 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
809 instr
->block
= cursor
.block
;
810 add_defs_uses(instr
);
811 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
813 case nir_cursor_after_block
: {
814 /* Inserting instructions after a jump is illegal. */
815 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
816 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
819 instr
->block
= cursor
.block
;
820 add_defs_uses(instr
);
821 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
824 case nir_cursor_before_instr
:
825 assert(instr
->type
!= nir_instr_type_jump
);
826 instr
->block
= cursor
.instr
->block
;
827 add_defs_uses(instr
);
828 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
830 case nir_cursor_after_instr
:
831 /* Inserting instructions after a jump is illegal. */
832 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
834 /* Only allow inserting jumps at the end of the block. */
835 if (instr
->type
== nir_instr_type_jump
)
836 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
838 instr
->block
= cursor
.instr
->block
;
839 add_defs_uses(instr
);
840 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
844 if (instr
->type
== nir_instr_type_jump
)
845 nir_handle_add_jump(instr
->block
);
849 src_is_valid(const nir_src
*src
)
851 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
855 remove_use_cb(nir_src
*src
, void *state
)
859 if (src_is_valid(src
))
860 list_del(&src
->use_link
);
866 remove_def_cb(nir_dest
*dest
, void *state
)
871 list_del(&dest
->reg
.def_link
);
877 remove_defs_uses(nir_instr
*instr
)
879 nir_foreach_dest(instr
, remove_def_cb
, instr
);
880 nir_foreach_src(instr
, remove_use_cb
, instr
);
883 void nir_instr_remove_v(nir_instr
*instr
)
885 remove_defs_uses(instr
);
886 exec_node_remove(&instr
->node
);
888 if (instr
->type
== nir_instr_type_jump
) {
889 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
890 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
897 nir_index_local_regs(nir_function_impl
*impl
)
900 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
901 reg
->index
= index
++;
903 impl
->reg_alloc
= index
;
907 nir_index_global_regs(nir_shader
*shader
)
910 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
911 reg
->index
= index
++;
913 shader
->reg_alloc
= index
;
917 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
919 return cb(&instr
->dest
.dest
, state
);
923 visit_deref_dest(nir_deref_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
925 return cb(&instr
->dest
, state
);
929 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
932 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
933 return cb(&instr
->dest
, state
);
939 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
942 return cb(&instr
->dest
, state
);
946 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
948 return cb(&instr
->dest
, state
);
952 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
953 nir_foreach_dest_cb cb
, void *state
)
955 nir_foreach_parallel_copy_entry(entry
, instr
) {
956 if (!cb(&entry
->dest
, state
))
964 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
966 switch (instr
->type
) {
967 case nir_instr_type_alu
:
968 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
969 case nir_instr_type_deref
:
970 return visit_deref_dest(nir_instr_as_deref(instr
), cb
, state
);
971 case nir_instr_type_intrinsic
:
972 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
973 case nir_instr_type_tex
:
974 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
975 case nir_instr_type_phi
:
976 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
977 case nir_instr_type_parallel_copy
:
978 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
981 case nir_instr_type_load_const
:
982 case nir_instr_type_ssa_undef
:
983 case nir_instr_type_call
:
984 case nir_instr_type_jump
:
988 unreachable("Invalid instruction type");
995 struct foreach_ssa_def_state
{
996 nir_foreach_ssa_def_cb cb
;
1001 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1003 struct foreach_ssa_def_state
*state
= void_state
;
1006 return state
->cb(&dest
->ssa
, state
->client_state
);
1012 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1014 switch (instr
->type
) {
1015 case nir_instr_type_alu
:
1016 case nir_instr_type_deref
:
1017 case nir_instr_type_tex
:
1018 case nir_instr_type_intrinsic
:
1019 case nir_instr_type_phi
:
1020 case nir_instr_type_parallel_copy
: {
1021 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1022 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1025 case nir_instr_type_load_const
:
1026 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1027 case nir_instr_type_ssa_undef
:
1028 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1029 case nir_instr_type_call
:
1030 case nir_instr_type_jump
:
1033 unreachable("Invalid instruction type");
1038 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1040 if (!cb(src
, state
))
1042 if (!src
->is_ssa
&& src
->reg
.indirect
)
1043 return cb(src
->reg
.indirect
, state
);
1048 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1050 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1051 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1058 visit_deref_instr_src(nir_deref_instr
*instr
,
1059 nir_foreach_src_cb cb
, void *state
)
1061 if (instr
->deref_type
!= nir_deref_type_var
) {
1062 if (!visit_src(&instr
->parent
, cb
, state
))
1066 if (instr
->deref_type
== nir_deref_type_array
) {
1067 if (!visit_src(&instr
->arr
.index
, cb
, state
))
1075 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1077 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1078 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1086 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1089 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1090 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1091 if (!visit_src(&instr
->src
[i
], cb
, state
))
1099 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1101 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1102 if (!visit_src(&instr
->params
[i
], cb
, state
))
1110 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1112 nir_foreach_phi_src(src
, instr
) {
1113 if (!visit_src(&src
->src
, cb
, state
))
1121 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1122 nir_foreach_src_cb cb
, void *state
)
1124 nir_foreach_parallel_copy_entry(entry
, instr
) {
1125 if (!visit_src(&entry
->src
, cb
, state
))
1134 nir_foreach_src_cb cb
;
1135 } visit_dest_indirect_state
;
1138 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1140 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1142 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1143 return state
->cb(dest
->reg
.indirect
, state
->state
);
1149 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1151 switch (instr
->type
) {
1152 case nir_instr_type_alu
:
1153 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1156 case nir_instr_type_deref
:
1157 if (!visit_deref_instr_src(nir_instr_as_deref(instr
), cb
, state
))
1160 case nir_instr_type_intrinsic
:
1161 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1164 case nir_instr_type_tex
:
1165 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1168 case nir_instr_type_call
:
1169 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1172 case nir_instr_type_load_const
:
1173 /* Constant load instructions have no regular sources */
1175 case nir_instr_type_phi
:
1176 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1179 case nir_instr_type_parallel_copy
:
1180 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1184 case nir_instr_type_jump
:
1185 case nir_instr_type_ssa_undef
:
1189 unreachable("Invalid instruction type");
1193 visit_dest_indirect_state dest_state
;
1194 dest_state
.state
= state
;
1196 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1200 nir_src_as_const_value(nir_src src
)
1205 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1208 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1210 return &load
->value
;
1214 * Returns true if the source is known to be dynamically uniform. Otherwise it
1215 * returns false which means it may or may not be dynamically uniform but it
1216 * can't be determined.
1219 nir_src_is_dynamically_uniform(nir_src src
)
1224 /* Constants are trivially dynamically uniform */
1225 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1228 /* As are uniform variables */
1229 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1230 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1232 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1236 /* XXX: this could have many more tests, such as when a sampler function is
1237 * called with dynamically uniform arguments.
1243 src_remove_all_uses(nir_src
*src
)
1245 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1246 if (!src_is_valid(src
))
1249 list_del(&src
->use_link
);
1254 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1256 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1257 if (!src_is_valid(src
))
1261 src
->parent_instr
= parent_instr
;
1263 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1265 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1268 src
->parent_if
= parent_if
;
1270 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1272 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1278 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1280 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1282 src_remove_all_uses(src
);
1284 src_add_all_uses(src
, instr
, NULL
);
1288 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1290 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1292 src_remove_all_uses(dest
);
1293 src_remove_all_uses(src
);
1295 *src
= NIR_SRC_INIT
;
1296 src_add_all_uses(dest
, dest_instr
, NULL
);
1300 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1302 nir_src
*src
= &if_stmt
->condition
;
1303 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1305 src_remove_all_uses(src
);
1307 src_add_all_uses(src
, NULL
, if_stmt
);
1311 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1314 /* We can only overwrite an SSA destination if it has no uses. */
1315 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1317 list_del(&dest
->reg
.def_link
);
1318 if (dest
->reg
.indirect
)
1319 src_remove_all_uses(dest
->reg
.indirect
);
1322 /* We can't re-write with an SSA def */
1323 assert(!new_dest
.is_ssa
);
1325 nir_dest_copy(dest
, &new_dest
, instr
);
1327 dest
->reg
.parent_instr
= instr
;
1328 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1330 if (dest
->reg
.indirect
)
1331 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1334 /* note: does *not* take ownership of 'name' */
1336 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1337 unsigned num_components
,
1338 unsigned bit_size
, const char *name
)
1340 def
->name
= ralloc_strdup(instr
, name
);
1341 def
->parent_instr
= instr
;
1342 list_inithead(&def
->uses
);
1343 list_inithead(&def
->if_uses
);
1344 def
->num_components
= num_components
;
1345 def
->bit_size
= bit_size
;
1348 nir_function_impl
*impl
=
1349 nir_cf_node_get_function(&instr
->block
->cf_node
);
1351 def
->index
= impl
->ssa_alloc
++;
1353 def
->index
= UINT_MAX
;
1357 /* note: does *not* take ownership of 'name' */
1359 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1360 unsigned num_components
, unsigned bit_size
,
1363 dest
->is_ssa
= true;
1364 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1368 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1370 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1372 nir_foreach_use_safe(use_src
, def
)
1373 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1375 nir_foreach_if_use_safe(use_src
, def
)
1376 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1380 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1382 assert(start
->block
== end
->block
);
1384 if (between
->block
!= start
->block
)
1387 /* Search backwards looking for "between" */
1388 while (start
!= end
) {
1392 end
= nir_instr_prev(end
);
1399 /* Replaces all uses of the given SSA def with the given source but only if
1400 * the use comes after the after_me instruction. This can be useful if you
1401 * are emitting code to fix up the result of some instruction: you can freely
1402 * use the result in that code and then call rewrite_uses_after and pass the
1403 * last fixup instruction as after_me and it will replace all of the uses you
1404 * want without touching the fixup code.
1406 * This function assumes that after_me is in the same block as
1407 * def->parent_instr and that after_me comes after def->parent_instr.
1410 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1411 nir_instr
*after_me
)
1413 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1415 nir_foreach_use_safe(use_src
, def
) {
1416 assert(use_src
->parent_instr
!= def
->parent_instr
);
1417 /* Since def already dominates all of its uses, the only way a use can
1418 * not be dominated by after_me is if it is between def and after_me in
1419 * the instruction list.
1421 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1422 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1425 nir_foreach_if_use_safe(use_src
, def
)
1426 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1430 nir_ssa_def_components_read(const nir_ssa_def
*def
)
1432 uint8_t read_mask
= 0;
1433 nir_foreach_use(use
, def
) {
1434 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1435 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1436 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1437 int src_idx
= alu_src
- &alu
->src
[0];
1438 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1440 for (unsigned c
= 0; c
< 4; c
++) {
1441 if (!nir_alu_instr_channel_used(alu
, src_idx
, c
))
1444 read_mask
|= (1 << alu_src
->swizzle
[c
]);
1447 return (1 << def
->num_components
) - 1;
1455 nir_block_cf_tree_next(nir_block
*block
)
1457 if (block
== NULL
) {
1458 /* nir_foreach_block_safe() will call this function on a NULL block
1459 * after the last iteration, but it won't use the result so just return
1465 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1467 return nir_cf_node_cf_tree_first(cf_next
);
1469 nir_cf_node
*parent
= block
->cf_node
.parent
;
1471 switch (parent
->type
) {
1472 case nir_cf_node_if
: {
1473 /* Are we at the end of the if? Go to the beginning of the else */
1474 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1475 if (block
== nir_if_last_then_block(if_stmt
))
1476 return nir_if_first_else_block(if_stmt
);
1478 assert(block
== nir_if_last_else_block(if_stmt
));
1482 case nir_cf_node_loop
:
1483 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1485 case nir_cf_node_function
:
1489 unreachable("unknown cf node type");
1494 nir_block_cf_tree_prev(nir_block
*block
)
1496 if (block
== NULL
) {
1497 /* do this for consistency with nir_block_cf_tree_next() */
1501 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1503 return nir_cf_node_cf_tree_last(cf_prev
);
1505 nir_cf_node
*parent
= block
->cf_node
.parent
;
1507 switch (parent
->type
) {
1508 case nir_cf_node_if
: {
1509 /* Are we at the beginning of the else? Go to the end of the if */
1510 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1511 if (block
== nir_if_first_else_block(if_stmt
))
1512 return nir_if_last_then_block(if_stmt
);
1514 assert(block
== nir_if_first_then_block(if_stmt
));
1518 case nir_cf_node_loop
:
1519 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1521 case nir_cf_node_function
:
1525 unreachable("unknown cf node type");
1529 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1531 switch (node
->type
) {
1532 case nir_cf_node_function
: {
1533 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1534 return nir_start_block(impl
);
1537 case nir_cf_node_if
: {
1538 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1539 return nir_if_first_then_block(if_stmt
);
1542 case nir_cf_node_loop
: {
1543 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1544 return nir_loop_first_block(loop
);
1547 case nir_cf_node_block
: {
1548 return nir_cf_node_as_block(node
);
1552 unreachable("unknown node type");
1556 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1558 switch (node
->type
) {
1559 case nir_cf_node_function
: {
1560 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1561 return nir_impl_last_block(impl
);
1564 case nir_cf_node_if
: {
1565 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1566 return nir_if_last_else_block(if_stmt
);
1569 case nir_cf_node_loop
: {
1570 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1571 return nir_loop_last_block(loop
);
1574 case nir_cf_node_block
: {
1575 return nir_cf_node_as_block(node
);
1579 unreachable("unknown node type");
1583 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1585 if (node
->type
== nir_cf_node_block
)
1586 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1587 else if (node
->type
== nir_cf_node_function
)
1590 return nir_cf_node_as_block(nir_cf_node_next(node
));
1594 nir_block_get_following_if(nir_block
*block
)
1596 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1599 if (nir_cf_node_is_last(&block
->cf_node
))
1602 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1604 if (next_node
->type
!= nir_cf_node_if
)
1607 return nir_cf_node_as_if(next_node
);
1611 nir_block_get_following_loop(nir_block
*block
)
1613 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1616 if (nir_cf_node_is_last(&block
->cf_node
))
1619 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1621 if (next_node
->type
!= nir_cf_node_loop
)
1624 return nir_cf_node_as_loop(next_node
);
1628 nir_index_blocks(nir_function_impl
*impl
)
1632 if (impl
->valid_metadata
& nir_metadata_block_index
)
1635 nir_foreach_block(block
, impl
) {
1636 block
->index
= index
++;
1639 impl
->num_blocks
= index
;
1643 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1645 unsigned *index
= (unsigned *) state
;
1646 def
->index
= (*index
)++;
1652 * The indices are applied top-to-bottom which has the very nice property
1653 * that, if A dominates B, then A->index <= B->index.
1656 nir_index_ssa_defs(nir_function_impl
*impl
)
1660 nir_foreach_block(block
, impl
) {
1661 nir_foreach_instr(instr
, block
)
1662 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1665 impl
->ssa_alloc
= index
;
1669 * The indices are applied top-to-bottom which has the very nice property
1670 * that, if A dominates B, then A->index <= B->index.
1673 nir_index_instrs(nir_function_impl
*impl
)
1677 nir_foreach_block(block
, impl
) {
1678 nir_foreach_instr(instr
, block
)
1679 instr
->index
= index
++;
1686 nir_intrinsic_from_system_value(gl_system_value val
)
1689 case SYSTEM_VALUE_VERTEX_ID
:
1690 return nir_intrinsic_load_vertex_id
;
1691 case SYSTEM_VALUE_INSTANCE_ID
:
1692 return nir_intrinsic_load_instance_id
;
1693 case SYSTEM_VALUE_DRAW_ID
:
1694 return nir_intrinsic_load_draw_id
;
1695 case SYSTEM_VALUE_BASE_INSTANCE
:
1696 return nir_intrinsic_load_base_instance
;
1697 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1698 return nir_intrinsic_load_vertex_id_zero_base
;
1699 case SYSTEM_VALUE_IS_INDEXED_DRAW
:
1700 return nir_intrinsic_load_is_indexed_draw
;
1701 case SYSTEM_VALUE_FIRST_VERTEX
:
1702 return nir_intrinsic_load_first_vertex
;
1703 case SYSTEM_VALUE_BASE_VERTEX
:
1704 return nir_intrinsic_load_base_vertex
;
1705 case SYSTEM_VALUE_INVOCATION_ID
:
1706 return nir_intrinsic_load_invocation_id
;
1707 case SYSTEM_VALUE_FRAG_COORD
:
1708 return nir_intrinsic_load_frag_coord
;
1709 case SYSTEM_VALUE_FRONT_FACE
:
1710 return nir_intrinsic_load_front_face
;
1711 case SYSTEM_VALUE_SAMPLE_ID
:
1712 return nir_intrinsic_load_sample_id
;
1713 case SYSTEM_VALUE_SAMPLE_POS
:
1714 return nir_intrinsic_load_sample_pos
;
1715 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1716 return nir_intrinsic_load_sample_mask_in
;
1717 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1718 return nir_intrinsic_load_local_invocation_id
;
1719 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
1720 return nir_intrinsic_load_local_invocation_index
;
1721 case SYSTEM_VALUE_WORK_GROUP_ID
:
1722 return nir_intrinsic_load_work_group_id
;
1723 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1724 return nir_intrinsic_load_num_work_groups
;
1725 case SYSTEM_VALUE_PRIMITIVE_ID
:
1726 return nir_intrinsic_load_primitive_id
;
1727 case SYSTEM_VALUE_TESS_COORD
:
1728 return nir_intrinsic_load_tess_coord
;
1729 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1730 return nir_intrinsic_load_tess_level_outer
;
1731 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1732 return nir_intrinsic_load_tess_level_inner
;
1733 case SYSTEM_VALUE_VERTICES_IN
:
1734 return nir_intrinsic_load_patch_vertices_in
;
1735 case SYSTEM_VALUE_HELPER_INVOCATION
:
1736 return nir_intrinsic_load_helper_invocation
;
1737 case SYSTEM_VALUE_VIEW_INDEX
:
1738 return nir_intrinsic_load_view_index
;
1739 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1740 return nir_intrinsic_load_subgroup_size
;
1741 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1742 return nir_intrinsic_load_subgroup_invocation
;
1743 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1744 return nir_intrinsic_load_subgroup_eq_mask
;
1745 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1746 return nir_intrinsic_load_subgroup_ge_mask
;
1747 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1748 return nir_intrinsic_load_subgroup_gt_mask
;
1749 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1750 return nir_intrinsic_load_subgroup_le_mask
;
1751 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1752 return nir_intrinsic_load_subgroup_lt_mask
;
1753 case SYSTEM_VALUE_NUM_SUBGROUPS
:
1754 return nir_intrinsic_load_num_subgroups
;
1755 case SYSTEM_VALUE_SUBGROUP_ID
:
1756 return nir_intrinsic_load_subgroup_id
;
1757 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
1758 return nir_intrinsic_load_local_group_size
;
1759 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
1760 return nir_intrinsic_load_global_invocation_id
;
1761 case SYSTEM_VALUE_WORK_DIM
:
1762 return nir_intrinsic_load_work_dim
;
1764 unreachable("system value does not directly correspond to intrinsic");
1769 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1772 case nir_intrinsic_load_vertex_id
:
1773 return SYSTEM_VALUE_VERTEX_ID
;
1774 case nir_intrinsic_load_instance_id
:
1775 return SYSTEM_VALUE_INSTANCE_ID
;
1776 case nir_intrinsic_load_draw_id
:
1777 return SYSTEM_VALUE_DRAW_ID
;
1778 case nir_intrinsic_load_base_instance
:
1779 return SYSTEM_VALUE_BASE_INSTANCE
;
1780 case nir_intrinsic_load_vertex_id_zero_base
:
1781 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1782 case nir_intrinsic_load_first_vertex
:
1783 return SYSTEM_VALUE_FIRST_VERTEX
;
1784 case nir_intrinsic_load_is_indexed_draw
:
1785 return SYSTEM_VALUE_IS_INDEXED_DRAW
;
1786 case nir_intrinsic_load_base_vertex
:
1787 return SYSTEM_VALUE_BASE_VERTEX
;
1788 case nir_intrinsic_load_invocation_id
:
1789 return SYSTEM_VALUE_INVOCATION_ID
;
1790 case nir_intrinsic_load_frag_coord
:
1791 return SYSTEM_VALUE_FRAG_COORD
;
1792 case nir_intrinsic_load_front_face
:
1793 return SYSTEM_VALUE_FRONT_FACE
;
1794 case nir_intrinsic_load_sample_id
:
1795 return SYSTEM_VALUE_SAMPLE_ID
;
1796 case nir_intrinsic_load_sample_pos
:
1797 return SYSTEM_VALUE_SAMPLE_POS
;
1798 case nir_intrinsic_load_sample_mask_in
:
1799 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1800 case nir_intrinsic_load_local_invocation_id
:
1801 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1802 case nir_intrinsic_load_local_invocation_index
:
1803 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1804 case nir_intrinsic_load_num_work_groups
:
1805 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1806 case nir_intrinsic_load_work_group_id
:
1807 return SYSTEM_VALUE_WORK_GROUP_ID
;
1808 case nir_intrinsic_load_primitive_id
:
1809 return SYSTEM_VALUE_PRIMITIVE_ID
;
1810 case nir_intrinsic_load_tess_coord
:
1811 return SYSTEM_VALUE_TESS_COORD
;
1812 case nir_intrinsic_load_tess_level_outer
:
1813 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1814 case nir_intrinsic_load_tess_level_inner
:
1815 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1816 case nir_intrinsic_load_patch_vertices_in
:
1817 return SYSTEM_VALUE_VERTICES_IN
;
1818 case nir_intrinsic_load_helper_invocation
:
1819 return SYSTEM_VALUE_HELPER_INVOCATION
;
1820 case nir_intrinsic_load_view_index
:
1821 return SYSTEM_VALUE_VIEW_INDEX
;
1822 case nir_intrinsic_load_subgroup_size
:
1823 return SYSTEM_VALUE_SUBGROUP_SIZE
;
1824 case nir_intrinsic_load_subgroup_invocation
:
1825 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1826 case nir_intrinsic_load_subgroup_eq_mask
:
1827 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
1828 case nir_intrinsic_load_subgroup_ge_mask
:
1829 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
1830 case nir_intrinsic_load_subgroup_gt_mask
:
1831 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
1832 case nir_intrinsic_load_subgroup_le_mask
:
1833 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
1834 case nir_intrinsic_load_subgroup_lt_mask
:
1835 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
1836 case nir_intrinsic_load_num_subgroups
:
1837 return SYSTEM_VALUE_NUM_SUBGROUPS
;
1838 case nir_intrinsic_load_subgroup_id
:
1839 return SYSTEM_VALUE_SUBGROUP_ID
;
1840 case nir_intrinsic_load_local_group_size
:
1841 return SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1842 case nir_intrinsic_load_global_invocation_id
:
1843 return SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1845 unreachable("intrinsic doesn't produce a system value");