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"
33 nir_shader_create(void *mem_ctx
,
34 gl_shader_stage stage
,
35 const nir_shader_compiler_options
*options
)
37 nir_shader
*shader
= ralloc(mem_ctx
, nir_shader
);
39 exec_list_make_empty(&shader
->uniforms
);
40 exec_list_make_empty(&shader
->inputs
);
41 exec_list_make_empty(&shader
->outputs
);
43 shader
->options
= options
;
44 memset(&shader
->info
, 0, sizeof(shader
->info
));
46 exec_list_make_empty(&shader
->functions
);
47 exec_list_make_empty(&shader
->registers
);
48 exec_list_make_empty(&shader
->globals
);
49 exec_list_make_empty(&shader
->system_values
);
50 shader
->reg_alloc
= 0;
52 shader
->num_inputs
= 0;
53 shader
->num_outputs
= 0;
54 shader
->num_uniforms
= 0;
56 shader
->stage
= stage
;
62 reg_create(void *mem_ctx
, struct exec_list
*list
)
64 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
66 list_inithead(®
->uses
);
67 list_inithead(®
->defs
);
68 list_inithead(®
->if_uses
);
70 reg
->num_components
= 0;
71 reg
->num_array_elems
= 0;
72 reg
->is_packed
= false;
75 exec_list_push_tail(list
, ®
->node
);
81 nir_global_reg_create(nir_shader
*shader
)
83 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
84 reg
->index
= shader
->reg_alloc
++;
85 reg
->is_global
= true;
91 nir_local_reg_create(nir_function_impl
*impl
)
93 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
94 reg
->index
= impl
->reg_alloc
++;
95 reg
->is_global
= false;
101 nir_reg_remove(nir_register
*reg
)
103 exec_node_remove(®
->node
);
107 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
109 switch (var
->data
.mode
) {
111 assert(!"invalid mode");
115 assert(!"nir_shader_add_variable cannot be used for local variables");
119 exec_list_push_tail(&shader
->globals
, &var
->node
);
122 case nir_var_shader_in
:
123 exec_list_push_tail(&shader
->inputs
, &var
->node
);
126 case nir_var_shader_out
:
127 exec_list_push_tail(&shader
->outputs
, &var
->node
);
130 case nir_var_uniform
:
131 case nir_var_shader_storage
:
132 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
135 case nir_var_system_value
:
136 exec_list_push_tail(&shader
->system_values
, &var
->node
);
142 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
143 const struct glsl_type
*type
, const char *name
)
145 nir_variable
*var
= rzalloc(shader
, nir_variable
);
146 var
->name
= ralloc_strdup(var
, name
);
148 var
->data
.mode
= mode
;
150 if ((mode
== nir_var_shader_in
&& shader
->stage
!= MESA_SHADER_VERTEX
) ||
151 (mode
== nir_var_shader_out
&& shader
->stage
!= MESA_SHADER_FRAGMENT
))
152 var
->data
.interpolation
= INTERP_QUALIFIER_SMOOTH
;
154 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
155 var
->data
.read_only
= true;
157 nir_shader_add_variable(shader
, var
);
163 nir_local_variable_create(nir_function_impl
*impl
,
164 const struct glsl_type
*type
, const char *name
)
166 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
167 var
->name
= ralloc_strdup(var
, name
);
169 var
->data
.mode
= nir_var_local
;
171 nir_function_impl_add_variable(impl
, var
);
177 nir_function_create(nir_shader
*shader
, const char *name
)
179 nir_function
*func
= ralloc(shader
, nir_function
);
181 exec_list_push_tail(&shader
->functions
, &func
->node
);
183 func
->name
= ralloc_strdup(func
, name
);
184 func
->shader
= shader
;
185 func
->num_params
= 0;
187 func
->return_type
= glsl_void_type();
193 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
195 dest
->is_ssa
= src
->is_ssa
;
197 dest
->ssa
= src
->ssa
;
199 dest
->reg
.base_offset
= src
->reg
.base_offset
;
200 dest
->reg
.reg
= src
->reg
.reg
;
201 if (src
->reg
.indirect
) {
202 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
203 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
205 dest
->reg
.indirect
= NULL
;
210 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
212 /* Copying an SSA definition makes no sense whatsoever. */
213 assert(!src
->is_ssa
);
215 dest
->is_ssa
= false;
217 dest
->reg
.base_offset
= src
->reg
.base_offset
;
218 dest
->reg
.reg
= src
->reg
.reg
;
219 if (src
->reg
.indirect
) {
220 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
221 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
223 dest
->reg
.indirect
= NULL
;
228 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
229 nir_alu_instr
*instr
)
231 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
232 dest
->abs
= src
->abs
;
233 dest
->negate
= src
->negate
;
234 for (unsigned i
= 0; i
< 4; i
++)
235 dest
->swizzle
[i
] = src
->swizzle
[i
];
239 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
240 nir_alu_instr
*instr
)
242 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
243 dest
->write_mask
= src
->write_mask
;
244 dest
->saturate
= src
->saturate
;
249 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
251 exec_node_init(&node
->node
);
257 nir_function_impl_create_bare(nir_shader
*shader
)
259 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
261 impl
->function
= NULL
;
263 cf_init(&impl
->cf_node
, nir_cf_node_function
);
265 exec_list_make_empty(&impl
->body
);
266 exec_list_make_empty(&impl
->registers
);
267 exec_list_make_empty(&impl
->locals
);
268 impl
->num_params
= 0;
270 impl
->return_var
= NULL
;
273 impl
->valid_metadata
= nir_metadata_none
;
275 /* create start & end blocks */
276 nir_block
*start_block
= nir_block_create(shader
);
277 nir_block
*end_block
= nir_block_create(shader
);
278 start_block
->cf_node
.parent
= &impl
->cf_node
;
279 end_block
->cf_node
.parent
= &impl
->cf_node
;
280 impl
->end_block
= end_block
;
282 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
284 start_block
->successors
[0] = end_block
;
285 _mesa_set_add(end_block
->predecessors
, start_block
);
290 nir_function_impl_create(nir_function
*function
)
292 assert(function
->impl
== NULL
);
294 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
296 function
->impl
= impl
;
297 impl
->function
= function
;
299 impl
->num_params
= function
->num_params
;
300 impl
->params
= ralloc_array(function
->shader
,
301 nir_variable
*, impl
->num_params
);
307 nir_block_create(nir_shader
*shader
)
309 nir_block
*block
= ralloc(shader
, nir_block
);
311 cf_init(&block
->cf_node
, nir_cf_node_block
);
313 block
->successors
[0] = block
->successors
[1] = NULL
;
314 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
315 _mesa_key_pointer_equal
);
316 block
->imm_dom
= NULL
;
317 /* XXX maybe it would be worth it to defer allocation? This
318 * way it doesn't get allocated for shader ref's that never run
319 * nir_calc_dominance? For example, state-tracker creates an
320 * initial IR, clones that, runs appropriate lowering pass, passes
321 * to driver which does common lowering/opt, and then stores ref
322 * which is later used to do state specific lowering and futher
323 * opt. Do any of the references not need dominance metadata?
325 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
326 _mesa_key_pointer_equal
);
328 exec_list_make_empty(&block
->instr_list
);
334 src_init(nir_src
*src
)
338 src
->reg
.indirect
= NULL
;
339 src
->reg
.base_offset
= 0;
343 nir_if_create(nir_shader
*shader
)
345 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
347 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
348 src_init(&if_stmt
->condition
);
350 nir_block
*then
= nir_block_create(shader
);
351 exec_list_make_empty(&if_stmt
->then_list
);
352 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
353 then
->cf_node
.parent
= &if_stmt
->cf_node
;
355 nir_block
*else_stmt
= nir_block_create(shader
);
356 exec_list_make_empty(&if_stmt
->else_list
);
357 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
358 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
364 nir_loop_create(nir_shader
*shader
)
366 nir_loop
*loop
= ralloc(shader
, nir_loop
);
368 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
370 nir_block
*body
= nir_block_create(shader
);
371 exec_list_make_empty(&loop
->body
);
372 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
373 body
->cf_node
.parent
= &loop
->cf_node
;
375 body
->successors
[0] = body
;
376 _mesa_set_add(body
->predecessors
, body
);
382 instr_init(nir_instr
*instr
, nir_instr_type type
)
386 exec_node_init(&instr
->node
);
390 dest_init(nir_dest
*dest
)
392 dest
->is_ssa
= false;
393 dest
->reg
.reg
= NULL
;
394 dest
->reg
.indirect
= NULL
;
395 dest
->reg
.base_offset
= 0;
399 alu_dest_init(nir_alu_dest
*dest
)
401 dest_init(&dest
->dest
);
402 dest
->saturate
= false;
403 dest
->write_mask
= 0xf;
407 alu_src_init(nir_alu_src
*src
)
410 src
->abs
= src
->negate
= false;
418 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
420 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
421 nir_alu_instr
*instr
=
423 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
425 instr_init(&instr
->instr
, nir_instr_type_alu
);
427 alu_dest_init(&instr
->dest
);
428 for (unsigned i
= 0; i
< num_srcs
; i
++)
429 alu_src_init(&instr
->src
[i
]);
435 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
437 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
438 instr_init(&instr
->instr
, nir_instr_type_jump
);
443 nir_load_const_instr
*
444 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
)
446 nir_load_const_instr
*instr
= ralloc(shader
, nir_load_const_instr
);
447 instr_init(&instr
->instr
, nir_instr_type_load_const
);
449 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
454 nir_intrinsic_instr
*
455 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
457 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
458 nir_intrinsic_instr
*instr
=
460 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
462 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
463 instr
->intrinsic
= op
;
465 if (nir_intrinsic_infos
[op
].has_dest
)
466 dest_init(&instr
->dest
);
468 for (unsigned i
= 0; i
< num_srcs
; i
++)
469 src_init(&instr
->src
[i
]);
475 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
477 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
478 instr_init(&instr
->instr
, nir_instr_type_call
);
480 instr
->callee
= callee
;
481 instr
->num_params
= callee
->num_params
;
482 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
483 instr
->return_deref
= NULL
;
489 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
491 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
492 instr_init(&instr
->instr
, nir_instr_type_tex
);
494 dest_init(&instr
->dest
);
496 instr
->num_srcs
= num_srcs
;
497 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
498 for (unsigned i
= 0; i
< num_srcs
; i
++)
499 src_init(&instr
->src
[i
].src
);
501 instr
->texture_index
= 0;
502 instr
->texture_array_size
= 0;
503 instr
->texture
= NULL
;
504 instr
->sampler_index
= 0;
505 instr
->sampler
= NULL
;
511 nir_phi_instr_create(nir_shader
*shader
)
513 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
514 instr_init(&instr
->instr
, nir_instr_type_phi
);
516 dest_init(&instr
->dest
);
517 exec_list_make_empty(&instr
->srcs
);
521 nir_parallel_copy_instr
*
522 nir_parallel_copy_instr_create(nir_shader
*shader
)
524 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
525 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
527 exec_list_make_empty(&instr
->entries
);
532 nir_ssa_undef_instr
*
533 nir_ssa_undef_instr_create(nir_shader
*shader
, unsigned num_components
)
535 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
536 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
538 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
544 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
546 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
547 deref
->deref
.deref_type
= nir_deref_type_var
;
548 deref
->deref
.child
= NULL
;
549 deref
->deref
.type
= var
->type
;
555 nir_deref_array_create(void *mem_ctx
)
557 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
558 deref
->deref
.deref_type
= nir_deref_type_array
;
559 deref
->deref
.child
= NULL
;
560 deref
->deref_array_type
= nir_deref_array_type_direct
;
561 src_init(&deref
->indirect
);
562 deref
->base_offset
= 0;
567 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
569 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
570 deref
->deref
.deref_type
= nir_deref_type_struct
;
571 deref
->deref
.child
= NULL
;
572 deref
->index
= field_index
;
576 static nir_deref_var
*
577 copy_deref_var(void *mem_ctx
, nir_deref_var
*deref
)
579 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
580 ret
->deref
.type
= deref
->deref
.type
;
581 if (deref
->deref
.child
)
582 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
586 static nir_deref_array
*
587 copy_deref_array(void *mem_ctx
, nir_deref_array
*deref
)
589 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
590 ret
->base_offset
= deref
->base_offset
;
591 ret
->deref_array_type
= deref
->deref_array_type
;
592 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
593 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
595 ret
->deref
.type
= deref
->deref
.type
;
596 if (deref
->deref
.child
)
597 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
601 static nir_deref_struct
*
602 copy_deref_struct(void *mem_ctx
, nir_deref_struct
*deref
)
604 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
605 ret
->deref
.type
= deref
->deref
.type
;
606 if (deref
->deref
.child
)
607 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
612 nir_copy_deref(void *mem_ctx
, nir_deref
*deref
)
614 switch (deref
->deref_type
) {
615 case nir_deref_type_var
:
616 return ©_deref_var(mem_ctx
, nir_deref_as_var(deref
))->deref
;
617 case nir_deref_type_array
:
618 return ©_deref_array(mem_ctx
, nir_deref_as_array(deref
))->deref
;
619 case nir_deref_type_struct
:
620 return ©_deref_struct(mem_ctx
, nir_deref_as_struct(deref
))->deref
;
622 unreachable("Invalid dereference type");
628 /* Returns a load_const instruction that represents the constant
629 * initializer for the given deref chain. The caller is responsible for
630 * ensuring that there actually is a constant initializer.
632 nir_load_const_instr
*
633 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
635 nir_constant
*constant
= deref
->var
->constant_initializer
;
638 const nir_deref
*tail
= &deref
->deref
;
639 unsigned matrix_offset
= 0;
640 while (tail
->child
) {
641 switch (tail
->child
->deref_type
) {
642 case nir_deref_type_array
: {
643 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
644 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
645 if (glsl_type_is_matrix(tail
->type
)) {
646 assert(arr
->deref
.child
== NULL
);
647 matrix_offset
= arr
->base_offset
;
649 constant
= constant
->elements
[arr
->base_offset
];
654 case nir_deref_type_struct
: {
655 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
660 unreachable("Invalid deref child type");
666 nir_load_const_instr
*load
=
667 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
));
669 matrix_offset
*= load
->def
.num_components
;
670 for (unsigned i
= 0; i
< load
->def
.num_components
; i
++) {
671 switch (glsl_get_base_type(tail
->type
)) {
672 case GLSL_TYPE_FLOAT
:
675 load
->value
.u
[i
] = constant
->value
.u
[matrix_offset
+ i
];
678 load
->value
.u
[i
] = constant
->value
.b
[matrix_offset
+ i
] ?
679 NIR_TRUE
: NIR_FALSE
;
682 unreachable("Invalid immediate type");
690 nir_cf_node_get_function(nir_cf_node
*node
)
692 while (node
->type
!= nir_cf_node_function
) {
696 return nir_cf_node_as_function(node
);
699 /* Reduces a cursor by trying to convert everything to after and trying to
700 * go up to block granularity when possible.
703 reduce_cursor(nir_cursor cursor
)
705 switch (cursor
.option
) {
706 case nir_cursor_before_block
:
707 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
708 /* Empty block. After is as good as before. */
709 cursor
.option
= nir_cursor_after_block
;
711 /* Try to switch to after the previous block if there is one.
712 * (This isn't likely, but it can happen.)
714 nir_cf_node
*prev_node
= nir_cf_node_prev(&cursor
.block
->cf_node
);
715 if (prev_node
&& prev_node
->type
== nir_cf_node_block
) {
716 cursor
.block
= nir_cf_node_as_block(prev_node
);
717 cursor
.option
= nir_cursor_after_block
;
722 case nir_cursor_after_block
:
725 case nir_cursor_before_instr
: {
726 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
728 /* Before this instruction is after the previous */
729 cursor
.instr
= prev_instr
;
730 cursor
.option
= nir_cursor_after_instr
;
732 /* No previous instruction. Switch to before block */
733 cursor
.block
= cursor
.instr
->block
;
734 cursor
.option
= nir_cursor_before_block
;
736 return reduce_cursor(cursor
);
739 case nir_cursor_after_instr
:
740 if (nir_instr_next(cursor
.instr
) == NULL
) {
741 /* This is the last instruction, switch to after block */
742 cursor
.option
= nir_cursor_after_block
;
743 cursor
.block
= cursor
.instr
->block
;
748 unreachable("Inavlid cursor option");
753 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
755 /* Reduced cursors should be unique */
756 a
= reduce_cursor(a
);
757 b
= reduce_cursor(b
);
759 return a
.block
== b
.block
&& a
.option
== b
.option
;
763 add_use_cb(nir_src
*src
, void *state
)
765 nir_instr
*instr
= state
;
767 src
->parent_instr
= instr
;
768 list_addtail(&src
->use_link
,
769 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
775 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
777 nir_instr
*instr
= state
;
779 if (instr
->block
&& def
->index
== UINT_MAX
) {
780 nir_function_impl
*impl
=
781 nir_cf_node_get_function(&instr
->block
->cf_node
);
783 def
->index
= impl
->ssa_alloc
++;
790 add_reg_def_cb(nir_dest
*dest
, void *state
)
792 nir_instr
*instr
= state
;
795 dest
->reg
.parent_instr
= instr
;
796 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
803 add_defs_uses(nir_instr
*instr
)
805 nir_foreach_src(instr
, add_use_cb
, instr
);
806 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
807 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
811 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
813 switch (cursor
.option
) {
814 case nir_cursor_before_block
:
815 /* Only allow inserting jumps into empty blocks. */
816 if (instr
->type
== nir_instr_type_jump
)
817 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
819 instr
->block
= cursor
.block
;
820 add_defs_uses(instr
);
821 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
823 case nir_cursor_after_block
: {
824 /* Inserting instructions after a jump is illegal. */
825 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
826 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
829 instr
->block
= cursor
.block
;
830 add_defs_uses(instr
);
831 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
834 case nir_cursor_before_instr
:
835 assert(instr
->type
!= nir_instr_type_jump
);
836 instr
->block
= cursor
.instr
->block
;
837 add_defs_uses(instr
);
838 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
840 case nir_cursor_after_instr
:
841 /* Inserting instructions after a jump is illegal. */
842 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
844 /* Only allow inserting jumps at the end of the block. */
845 if (instr
->type
== nir_instr_type_jump
)
846 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
848 instr
->block
= cursor
.instr
->block
;
849 add_defs_uses(instr
);
850 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
854 if (instr
->type
== nir_instr_type_jump
)
855 nir_handle_add_jump(instr
->block
);
859 src_is_valid(const nir_src
*src
)
861 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
865 remove_use_cb(nir_src
*src
, void *state
)
867 if (src_is_valid(src
))
868 list_del(&src
->use_link
);
874 remove_def_cb(nir_dest
*dest
, void *state
)
877 list_del(&dest
->reg
.def_link
);
883 remove_defs_uses(nir_instr
*instr
)
885 nir_foreach_dest(instr
, remove_def_cb
, instr
);
886 nir_foreach_src(instr
, remove_use_cb
, instr
);
889 void nir_instr_remove(nir_instr
*instr
)
891 remove_defs_uses(instr
);
892 exec_node_remove(&instr
->node
);
894 if (instr
->type
== nir_instr_type_jump
) {
895 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
896 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
903 nir_index_local_regs(nir_function_impl
*impl
)
906 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
907 reg
->index
= index
++;
909 impl
->reg_alloc
= index
;
913 nir_index_global_regs(nir_shader
*shader
)
916 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
917 reg
->index
= index
++;
919 shader
->reg_alloc
= index
;
923 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
925 return cb(&instr
->dest
.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(instr
, entry
) {
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_intrinsic
:
970 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
971 case nir_instr_type_tex
:
972 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
973 case nir_instr_type_phi
:
974 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
975 case nir_instr_type_parallel_copy
:
976 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
979 case nir_instr_type_load_const
:
980 case nir_instr_type_ssa_undef
:
981 case nir_instr_type_call
:
982 case nir_instr_type_jump
:
986 unreachable("Invalid instruction type");
993 struct foreach_ssa_def_state
{
994 nir_foreach_ssa_def_cb cb
;
999 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1001 struct foreach_ssa_def_state
*state
= void_state
;
1004 return state
->cb(&dest
->ssa
, state
->client_state
);
1010 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1012 switch (instr
->type
) {
1013 case nir_instr_type_alu
:
1014 case nir_instr_type_tex
:
1015 case nir_instr_type_intrinsic
:
1016 case nir_instr_type_phi
:
1017 case nir_instr_type_parallel_copy
: {
1018 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1019 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1022 case nir_instr_type_load_const
:
1023 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1024 case nir_instr_type_ssa_undef
:
1025 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1026 case nir_instr_type_call
:
1027 case nir_instr_type_jump
:
1030 unreachable("Invalid instruction type");
1035 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1037 if (!cb(src
, state
))
1039 if (!src
->is_ssa
&& src
->reg
.indirect
)
1040 return cb(src
->reg
.indirect
, state
);
1045 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
1048 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
1049 return visit_src(&deref
->indirect
, cb
, state
);
1054 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
1056 nir_deref
*cur
= &deref
->deref
;
1057 while (cur
!= NULL
) {
1058 if (cur
->deref_type
== nir_deref_type_array
)
1059 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
1069 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1071 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1072 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1079 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1081 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
1082 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1085 if (instr
->texture
!= NULL
)
1086 if (!visit_deref_src(instr
->texture
, cb
, state
))
1089 if (instr
->sampler
!= NULL
)
1090 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1097 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1100 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1101 for (unsigned i
= 0; i
< num_srcs
; i
++)
1102 if (!visit_src(&instr
->src
[i
], cb
, state
))
1106 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1107 for (unsigned i
= 0; i
< num_vars
; i
++)
1108 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1115 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1121 visit_load_const_src(nir_load_const_instr
*instr
, nir_foreach_src_cb cb
,
1128 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1130 nir_foreach_phi_src(instr
, src
) {
1131 if (!visit_src(&src
->src
, cb
, state
))
1139 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1140 nir_foreach_src_cb cb
, void *state
)
1142 nir_foreach_parallel_copy_entry(instr
, entry
) {
1143 if (!visit_src(&entry
->src
, cb
, state
))
1152 nir_foreach_src_cb cb
;
1153 } visit_dest_indirect_state
;
1156 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1158 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1160 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1161 return state
->cb(dest
->reg
.indirect
, state
->state
);
1167 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1169 switch (instr
->type
) {
1170 case nir_instr_type_alu
:
1171 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1174 case nir_instr_type_intrinsic
:
1175 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1178 case nir_instr_type_tex
:
1179 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1182 case nir_instr_type_call
:
1183 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1186 case nir_instr_type_load_const
:
1187 if (!visit_load_const_src(nir_instr_as_load_const(instr
), cb
, state
))
1190 case nir_instr_type_phi
:
1191 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1194 case nir_instr_type_parallel_copy
:
1195 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1199 case nir_instr_type_jump
:
1200 case nir_instr_type_ssa_undef
:
1204 unreachable("Invalid instruction type");
1208 visit_dest_indirect_state dest_state
;
1209 dest_state
.state
= state
;
1211 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1215 nir_src_as_const_value(nir_src src
)
1220 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1223 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1225 return &load
->value
;
1229 * Returns true if the source is known to be dynamically uniform. Otherwise it
1230 * returns false which means it may or may not be dynamically uniform but it
1231 * can't be determined.
1234 nir_src_is_dynamically_uniform(nir_src src
)
1239 /* Constants are trivially dynamically uniform */
1240 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1243 /* As are uniform variables */
1244 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1245 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1247 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1251 /* XXX: this could have many more tests, such as when a sampler function is
1252 * called with dynamically uniform arguments.
1258 src_remove_all_uses(nir_src
*src
)
1260 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1261 if (!src_is_valid(src
))
1264 list_del(&src
->use_link
);
1269 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1271 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1272 if (!src_is_valid(src
))
1276 src
->parent_instr
= parent_instr
;
1278 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1280 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1283 src
->parent_if
= parent_if
;
1285 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1287 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1293 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1295 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1297 src_remove_all_uses(src
);
1299 src_add_all_uses(src
, instr
, NULL
);
1303 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1305 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1307 src_remove_all_uses(dest
);
1308 src_remove_all_uses(src
);
1310 *src
= NIR_SRC_INIT
;
1311 src_add_all_uses(dest
, dest_instr
, NULL
);
1315 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1317 nir_src
*src
= &if_stmt
->condition
;
1318 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1320 src_remove_all_uses(src
);
1322 src_add_all_uses(src
, NULL
, if_stmt
);
1326 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1329 /* We can only overwrite an SSA destination if it has no uses. */
1330 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1332 list_del(&dest
->reg
.def_link
);
1333 if (dest
->reg
.indirect
)
1334 src_remove_all_uses(dest
->reg
.indirect
);
1337 /* We can't re-write with an SSA def */
1338 assert(!new_dest
.is_ssa
);
1340 nir_dest_copy(dest
, &new_dest
, instr
);
1342 dest
->reg
.parent_instr
= instr
;
1343 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1345 if (dest
->reg
.indirect
)
1346 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1350 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1351 unsigned num_components
, const char *name
)
1354 def
->parent_instr
= instr
;
1355 list_inithead(&def
->uses
);
1356 list_inithead(&def
->if_uses
);
1357 def
->num_components
= num_components
;
1360 nir_function_impl
*impl
=
1361 nir_cf_node_get_function(&instr
->block
->cf_node
);
1363 def
->index
= impl
->ssa_alloc
++;
1365 def
->index
= UINT_MAX
;
1370 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1371 unsigned num_components
, const char *name
)
1373 dest
->is_ssa
= true;
1374 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, name
);
1378 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1380 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1382 nir_foreach_use_safe(def
, use_src
)
1383 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1385 nir_foreach_if_use_safe(def
, use_src
)
1386 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1390 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1392 assert(start
->block
== end
->block
);
1394 if (between
->block
!= start
->block
)
1397 /* Search backwards looking for "between" */
1398 while (start
!= end
) {
1402 end
= nir_instr_prev(end
);
1409 /* Replaces all uses of the given SSA def with the given source but only if
1410 * the use comes after the after_me instruction. This can be useful if you
1411 * are emitting code to fix up the result of some instruction: you can freely
1412 * use the result in that code and then call rewrite_uses_after and pass the
1413 * last fixup instruction as after_me and it will replace all of the uses you
1414 * want without touching the fixup code.
1416 * This function assumes that after_me is in the same block as
1417 * def->parent_instr and that after_me comes after def->parent_instr.
1420 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1421 nir_instr
*after_me
)
1423 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1425 nir_foreach_use_safe(def
, use_src
) {
1426 assert(use_src
->parent_instr
!= def
->parent_instr
);
1427 /* Since def already dominates all of its uses, the only way a use can
1428 * not be dominated by after_me is if it is between def and after_me in
1429 * the instruction list.
1431 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1432 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1435 nir_foreach_if_use_safe(def
, use_src
)
1436 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1439 static bool foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1440 bool reverse
, void *state
);
1443 foreach_if(nir_if
*if_stmt
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1446 foreach_list_typed_reverse_safe(nir_cf_node
, node
, node
,
1447 &if_stmt
->else_list
) {
1448 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1452 foreach_list_typed_reverse_safe(nir_cf_node
, node
, node
,
1453 &if_stmt
->then_list
) {
1454 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1458 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1459 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1463 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1464 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1473 foreach_loop(nir_loop
*loop
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1476 foreach_list_typed_reverse_safe(nir_cf_node
, node
, node
, &loop
->body
) {
1477 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1481 foreach_list_typed_safe(nir_cf_node
, node
, node
, &loop
->body
) {
1482 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1491 foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1492 bool reverse
, void *state
)
1494 switch (node
->type
) {
1495 case nir_cf_node_block
:
1496 return cb(nir_cf_node_as_block(node
), state
);
1497 case nir_cf_node_if
:
1498 return foreach_if(nir_cf_node_as_if(node
), cb
, reverse
, state
);
1499 case nir_cf_node_loop
:
1500 return foreach_loop(nir_cf_node_as_loop(node
), cb
, reverse
, state
);
1504 unreachable("Invalid CFG node type");
1512 nir_foreach_block_in_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1515 return foreach_cf_node(node
, cb
, false, state
);
1519 nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
, void *state
)
1521 foreach_list_typed_safe(nir_cf_node
, node
, node
, &impl
->body
) {
1522 if (!foreach_cf_node(node
, cb
, false, state
))
1526 return cb(impl
->end_block
, state
);
1530 nir_foreach_block_reverse(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1533 if (!cb(impl
->end_block
, state
))
1536 foreach_list_typed_reverse_safe(nir_cf_node
, node
, node
, &impl
->body
) {
1537 if (!foreach_cf_node(node
, cb
, true, state
))
1545 nir_block_get_following_if(nir_block
*block
)
1547 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1550 if (nir_cf_node_is_last(&block
->cf_node
))
1553 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1555 if (next_node
->type
!= nir_cf_node_if
)
1558 return nir_cf_node_as_if(next_node
);
1562 nir_block_get_following_loop(nir_block
*block
)
1564 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1567 if (nir_cf_node_is_last(&block
->cf_node
))
1570 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1572 if (next_node
->type
!= nir_cf_node_loop
)
1575 return nir_cf_node_as_loop(next_node
);
1578 index_block(nir_block
*block
, void *state
)
1580 unsigned *index
= state
;
1581 block
->index
= (*index
)++;
1586 nir_index_blocks(nir_function_impl
*impl
)
1590 if (impl
->valid_metadata
& nir_metadata_block_index
)
1593 nir_foreach_block(impl
, index_block
, &index
);
1595 impl
->num_blocks
= index
;
1599 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1601 unsigned *index
= (unsigned *) state
;
1602 def
->index
= (*index
)++;
1608 index_ssa_block(nir_block
*block
, void *state
)
1610 nir_foreach_instr(block
, instr
)
1611 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, state
);
1617 * The indices are applied top-to-bottom which has the very nice property
1618 * that, if A dominates B, then A->index <= B->index.
1621 nir_index_ssa_defs(nir_function_impl
*impl
)
1624 nir_foreach_block(impl
, index_ssa_block
, &index
);
1625 impl
->ssa_alloc
= index
;
1629 index_instrs_block(nir_block
*block
, void *state
)
1631 unsigned *index
= state
;
1632 nir_foreach_instr(block
, instr
)
1633 instr
->index
= (*index
)++;
1639 * The indices are applied top-to-bottom which has the very nice property
1640 * that, if A dominates B, then A->index <= B->index.
1643 nir_index_instrs(nir_function_impl
*impl
)
1646 nir_foreach_block(impl
, index_instrs_block
, &index
);
1651 nir_intrinsic_from_system_value(gl_system_value val
)
1654 case SYSTEM_VALUE_VERTEX_ID
:
1655 return nir_intrinsic_load_vertex_id
;
1656 case SYSTEM_VALUE_INSTANCE_ID
:
1657 return nir_intrinsic_load_instance_id
;
1658 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1659 return nir_intrinsic_load_vertex_id_zero_base
;
1660 case SYSTEM_VALUE_BASE_VERTEX
:
1661 return nir_intrinsic_load_base_vertex
;
1662 case SYSTEM_VALUE_INVOCATION_ID
:
1663 return nir_intrinsic_load_invocation_id
;
1664 case SYSTEM_VALUE_FRONT_FACE
:
1665 return nir_intrinsic_load_front_face
;
1666 case SYSTEM_VALUE_SAMPLE_ID
:
1667 return nir_intrinsic_load_sample_id
;
1668 case SYSTEM_VALUE_SAMPLE_POS
:
1669 return nir_intrinsic_load_sample_pos
;
1670 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1671 return nir_intrinsic_load_sample_mask_in
;
1672 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1673 return nir_intrinsic_load_local_invocation_id
;
1674 case SYSTEM_VALUE_WORK_GROUP_ID
:
1675 return nir_intrinsic_load_work_group_id
;
1676 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1677 return nir_intrinsic_load_num_work_groups
;
1678 case SYSTEM_VALUE_PRIMITIVE_ID
:
1679 return nir_intrinsic_load_primitive_id
;
1680 case SYSTEM_VALUE_TESS_COORD
:
1681 return nir_intrinsic_load_tess_coord
;
1682 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1683 return nir_intrinsic_load_tess_level_outer
;
1684 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1685 return nir_intrinsic_load_tess_level_inner
;
1686 case SYSTEM_VALUE_VERTICES_IN
:
1687 return nir_intrinsic_load_patch_vertices_in
;
1688 case SYSTEM_VALUE_HELPER_INVOCATION
:
1689 return nir_intrinsic_load_helper_invocation
;
1691 unreachable("system value does not directly correspond to intrinsic");
1696 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1699 case nir_intrinsic_load_vertex_id
:
1700 return SYSTEM_VALUE_VERTEX_ID
;
1701 case nir_intrinsic_load_instance_id
:
1702 return SYSTEM_VALUE_INSTANCE_ID
;
1703 case nir_intrinsic_load_vertex_id_zero_base
:
1704 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1705 case nir_intrinsic_load_base_vertex
:
1706 return SYSTEM_VALUE_BASE_VERTEX
;
1707 case nir_intrinsic_load_invocation_id
:
1708 return SYSTEM_VALUE_INVOCATION_ID
;
1709 case nir_intrinsic_load_front_face
:
1710 return SYSTEM_VALUE_FRONT_FACE
;
1711 case nir_intrinsic_load_sample_id
:
1712 return SYSTEM_VALUE_SAMPLE_ID
;
1713 case nir_intrinsic_load_sample_pos
:
1714 return SYSTEM_VALUE_SAMPLE_POS
;
1715 case nir_intrinsic_load_sample_mask_in
:
1716 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1717 case nir_intrinsic_load_local_invocation_id
:
1718 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1719 case nir_intrinsic_load_num_work_groups
:
1720 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1721 case nir_intrinsic_load_work_group_id
:
1722 return SYSTEM_VALUE_WORK_GROUP_ID
;
1723 case nir_intrinsic_load_primitive_id
:
1724 return SYSTEM_VALUE_PRIMITIVE_ID
;
1725 case nir_intrinsic_load_tess_coord
:
1726 return SYSTEM_VALUE_TESS_COORD
;
1727 case nir_intrinsic_load_tess_level_outer
:
1728 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1729 case nir_intrinsic_load_tess_level_inner
:
1730 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1731 case nir_intrinsic_load_patch_vertices_in
:
1732 return SYSTEM_VALUE_VERTICES_IN
;
1733 case nir_intrinsic_load_helper_invocation
:
1734 return SYSTEM_VALUE_HELPER_INVOCATION
;
1736 unreachable("intrinsic doesn't produce a system value");