2 * Copyright © 2015 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 #include "vtn_private.h"
25 #include "nir/nir_vla.h"
27 static struct vtn_pointer
*
28 vtn_load_param_pointer(struct vtn_builder
*b
,
29 struct vtn_type
*param_type
,
32 struct vtn_type
*ptr_type
= param_type
;
33 if (param_type
->base_type
!= vtn_base_type_pointer
) {
34 assert(param_type
->base_type
== vtn_base_type_image
||
35 param_type
->base_type
== vtn_base_type_sampler
);
36 ptr_type
= rzalloc(b
, struct vtn_type
);
37 ptr_type
->base_type
= vtn_base_type_pointer
;
38 ptr_type
->deref
= param_type
;
39 ptr_type
->storage_class
= SpvStorageClassUniformConstant
;
42 return vtn_pointer_from_ssa(b
, nir_load_param(&b
->nb
, param_idx
), ptr_type
);
46 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
47 const uint32_t *w
, unsigned count
)
49 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
50 struct vtn_function
*vtn_callee
=
51 vtn_value(b
, w
[3], vtn_value_type_function
)->func
;
52 struct nir_function
*callee
= vtn_callee
->impl
->function
;
54 vtn_callee
->referenced
= true;
56 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
58 unsigned param_idx
= 0;
60 nir_deref_instr
*ret_deref
= NULL
;
61 struct vtn_type
*ret_type
= vtn_callee
->type
->return_type
;
62 if (ret_type
->base_type
!= vtn_base_type_void
) {
63 nir_variable
*ret_tmp
=
64 nir_local_variable_create(b
->nb
.impl
, ret_type
->type
, "return_tmp");
65 ret_deref
= nir_build_deref_var(&b
->nb
, ret_tmp
);
66 call
->params
[param_idx
++] = nir_src_for_ssa(&ret_deref
->dest
.ssa
);
69 for (unsigned i
= 0; i
< vtn_callee
->type
->length
; i
++) {
70 struct vtn_type
*arg_type
= vtn_callee
->type
->params
[i
];
71 unsigned arg_id
= w
[4 + i
];
73 if (arg_type
->base_type
== vtn_base_type_sampled_image
) {
74 struct vtn_sampled_image
*sampled_image
=
75 vtn_value(b
, arg_id
, vtn_value_type_sampled_image
)->sampled_image
;
77 call
->params
[param_idx
++] =
78 nir_src_for_ssa(&sampled_image
->image
->deref
->dest
.ssa
);
79 call
->params
[param_idx
++] =
80 nir_src_for_ssa(&sampled_image
->sampler
->deref
->dest
.ssa
);
81 } else if (arg_type
->base_type
== vtn_base_type_pointer
||
82 arg_type
->base_type
== vtn_base_type_image
||
83 arg_type
->base_type
== vtn_base_type_sampler
) {
84 struct vtn_pointer
*pointer
=
85 vtn_value(b
, arg_id
, vtn_value_type_pointer
)->pointer
;
86 call
->params
[param_idx
++] =
87 nir_src_for_ssa(vtn_pointer_to_ssa(b
, pointer
));
89 /* This is a regular SSA value and we need a temporary */
91 nir_local_variable_create(b
->nb
.impl
, arg_type
->type
, "arg_tmp");
92 nir_deref_instr
*tmp_deref
= nir_build_deref_var(&b
->nb
, tmp
);
93 vtn_local_store(b
, vtn_ssa_value(b
, arg_id
), tmp_deref
);
94 call
->params
[param_idx
++] = nir_src_for_ssa(&tmp_deref
->dest
.ssa
);
97 assert(param_idx
== call
->num_params
);
99 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
101 if (ret_type
->base_type
== vtn_base_type_void
) {
102 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
104 vtn_push_ssa(b
, w
[2], res_type
, vtn_local_load(b
, ret_deref
));
109 vtn_cfg_handle_prepass_instruction(struct vtn_builder
*b
, SpvOp opcode
,
110 const uint32_t *w
, unsigned count
)
113 case SpvOpFunction
: {
114 vtn_assert(b
->func
== NULL
);
115 b
->func
= rzalloc(b
, struct vtn_function
);
117 list_inithead(&b
->func
->body
);
118 b
->func
->control
= w
[3];
120 MAYBE_UNUSED
const struct glsl_type
*result_type
=
121 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
122 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_function
);
125 b
->func
->type
= vtn_value(b
, w
[4], vtn_value_type_type
)->type
;
126 const struct vtn_type
*func_type
= b
->func
->type
;
128 vtn_assert(func_type
->return_type
->type
== result_type
);
131 nir_function_create(b
->shader
, ralloc_strdup(b
->shader
, val
->name
));
133 unsigned num_params
= func_type
->length
;
134 for (unsigned i
= 0; i
< func_type
->length
; i
++) {
135 /* Sampled images are actually two parameters */
136 if (func_type
->params
[i
]->base_type
== vtn_base_type_sampled_image
)
140 /* Add one parameter for the function return value */
141 if (func_type
->return_type
->base_type
!= vtn_base_type_void
)
144 func
->num_params
= num_params
;
145 func
->params
= ralloc_array(b
->shader
, nir_parameter
, num_params
);
148 if (func_type
->return_type
->base_type
!= vtn_base_type_void
) {
149 /* The return value is a regular pointer */
150 func
->params
[idx
++] = (nir_parameter
) {
151 .num_components
= 1, .bit_size
= 32,
155 for (unsigned i
= 0; i
< func_type
->length
; i
++) {
156 if (func_type
->params
[i
]->base_type
== vtn_base_type_sampled_image
) {
157 /* Sampled images are two pointer parameters */
158 func
->params
[idx
++] = (nir_parameter
) {
159 .num_components
= 1, .bit_size
= 32,
161 func
->params
[idx
++] = (nir_parameter
) {
162 .num_components
= 1, .bit_size
= 32,
164 } else if (func_type
->params
[i
]->base_type
== vtn_base_type_pointer
&&
165 func_type
->params
[i
]->type
!= NULL
) {
166 /* Pointers with as storage class get passed by-value */
167 assert(glsl_type_is_vector_or_scalar(func_type
->params
[i
]->type
));
168 func
->params
[idx
++] = (nir_parameter
) {
170 glsl_get_vector_elements(func_type
->params
[i
]->type
),
171 .bit_size
= glsl_get_bit_size(func_type
->params
[i
]->type
),
174 /* Everything else is a regular pointer */
175 func
->params
[idx
++] = (nir_parameter
) {
176 .num_components
= 1, .bit_size
= 32,
180 assert(idx
== num_params
);
182 b
->func
->impl
= nir_function_impl_create(func
);
183 nir_builder_init(&b
->nb
, func
->impl
);
184 b
->nb
.cursor
= nir_before_cf_list(&b
->func
->impl
->body
);
186 b
->func_param_idx
= 0;
188 /* The return value is the first parameter */
189 if (func_type
->return_type
->base_type
!= vtn_base_type_void
)
194 case SpvOpFunctionEnd
:
199 case SpvOpFunctionParameter
: {
200 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
202 vtn_assert(b
->func_param_idx
< b
->func
->impl
->function
->num_params
);
204 if (type
->base_type
== vtn_base_type_sampled_image
) {
205 /* Sampled images are actually two parameters. The first is the
206 * image and the second is the sampler.
208 struct vtn_value
*val
=
209 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
211 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
212 val
->sampled_image
->type
= type
;
214 struct vtn_type
*sampler_type
= rzalloc(b
, struct vtn_type
);
215 sampler_type
->base_type
= vtn_base_type_sampler
;
216 sampler_type
->type
= glsl_bare_sampler_type();
218 val
->sampled_image
->image
=
219 vtn_load_param_pointer(b
, type
, b
->func_param_idx
++);
220 val
->sampled_image
->sampler
=
221 vtn_load_param_pointer(b
, sampler_type
, b
->func_param_idx
++);
222 } else if (type
->base_type
== vtn_base_type_pointer
&&
223 type
->type
!= NULL
) {
224 /* This is a pointer with an actual storage type */
225 struct vtn_value
*val
=
226 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
227 nir_ssa_def
*ssa_ptr
= nir_load_param(&b
->nb
, b
->func_param_idx
++);
228 val
->pointer
= vtn_pointer_from_ssa(b
, ssa_ptr
, type
);
229 } else if (type
->base_type
== vtn_base_type_pointer
||
230 type
->base_type
== vtn_base_type_image
||
231 type
->base_type
== vtn_base_type_sampler
) {
232 struct vtn_value
*val
=
233 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
235 vtn_load_param_pointer(b
, type
, b
->func_param_idx
++);
237 /* We're a regular SSA value. */
238 nir_ssa_def
*param_val
= nir_load_param(&b
->nb
, b
->func_param_idx
++);
239 nir_deref_instr
*deref
=
240 nir_build_deref_cast(&b
->nb
, param_val
, nir_var_local
, type
->type
);
241 vtn_push_ssa(b
, w
[2], type
, vtn_local_load(b
, deref
));
247 vtn_assert(b
->block
== NULL
);
248 b
->block
= rzalloc(b
, struct vtn_block
);
249 b
->block
->node
.type
= vtn_cf_node_type_block
;
251 vtn_push_value(b
, w
[1], vtn_value_type_block
)->block
= b
->block
;
253 if (b
->func
->start_block
== NULL
) {
254 /* This is the first block encountered for this function. In this
255 * case, we set the start block and add it to the list of
256 * implemented functions that we'll walk later.
258 b
->func
->start_block
= b
->block
;
259 exec_list_push_tail(&b
->functions
, &b
->func
->node
);
264 case SpvOpSelectionMerge
:
266 vtn_assert(b
->block
&& b
->block
->merge
== NULL
);
271 case SpvOpBranchConditional
:
275 case SpvOpReturnValue
:
276 case SpvOpUnreachable
:
277 vtn_assert(b
->block
&& b
->block
->branch
== NULL
);
278 b
->block
->branch
= w
;
283 /* Continue on as per normal */
291 vtn_add_case(struct vtn_builder
*b
, struct vtn_switch
*swtch
,
292 struct vtn_block
*break_block
,
293 uint32_t block_id
, uint64_t val
, bool is_default
)
295 struct vtn_block
*case_block
=
296 vtn_value(b
, block_id
, vtn_value_type_block
)->block
;
298 /* Don't create dummy cases that just break */
299 if (case_block
== break_block
)
302 if (case_block
->switch_case
== NULL
) {
303 struct vtn_case
*c
= ralloc(b
, struct vtn_case
);
305 list_inithead(&c
->body
);
306 c
->start_block
= case_block
;
307 c
->fallthrough
= NULL
;
308 util_dynarray_init(&c
->values
, b
);
309 c
->is_default
= false;
312 list_addtail(&c
->link
, &swtch
->cases
);
314 case_block
->switch_case
= c
;
318 case_block
->switch_case
->is_default
= true;
320 util_dynarray_append(&case_block
->switch_case
->values
, uint64_t, val
);
324 /* This function performs a depth-first search of the cases and puts them
325 * in fall-through order.
328 vtn_order_case(struct vtn_switch
*swtch
, struct vtn_case
*cse
)
335 list_del(&cse
->link
);
337 if (cse
->fallthrough
) {
338 vtn_order_case(swtch
, cse
->fallthrough
);
340 /* If we have a fall-through, place this case right before the case it
341 * falls through to. This ensures that fallthroughs come one after
342 * the other. These two can never get separated because that would
343 * imply something else falling through to the same case. Also, this
344 * can't break ordering because the DFS ensures that this case is
345 * visited before anything that falls through to it.
347 list_addtail(&cse
->link
, &cse
->fallthrough
->link
);
349 list_add(&cse
->link
, &swtch
->cases
);
353 static enum vtn_branch_type
354 vtn_get_branch_type(struct vtn_builder
*b
,
355 struct vtn_block
*block
,
356 struct vtn_case
*swcase
, struct vtn_block
*switch_break
,
357 struct vtn_block
*loop_break
, struct vtn_block
*loop_cont
)
359 if (block
->switch_case
) {
360 /* This branch is actually a fallthrough */
361 vtn_assert(swcase
->fallthrough
== NULL
||
362 swcase
->fallthrough
== block
->switch_case
);
363 swcase
->fallthrough
= block
->switch_case
;
364 return vtn_branch_type_switch_fallthrough
;
365 } else if (block
== loop_break
) {
366 return vtn_branch_type_loop_break
;
367 } else if (block
== loop_cont
) {
368 return vtn_branch_type_loop_continue
;
369 } else if (block
== switch_break
) {
370 return vtn_branch_type_switch_break
;
372 return vtn_branch_type_none
;
377 vtn_cfg_walk_blocks(struct vtn_builder
*b
, struct list_head
*cf_list
,
378 struct vtn_block
*start
, struct vtn_case
*switch_case
,
379 struct vtn_block
*switch_break
,
380 struct vtn_block
*loop_break
, struct vtn_block
*loop_cont
,
381 struct vtn_block
*end
)
383 struct vtn_block
*block
= start
;
384 while (block
!= end
) {
385 if (block
->merge
&& (*block
->merge
& SpvOpCodeMask
) == SpvOpLoopMerge
&&
387 struct vtn_loop
*loop
= ralloc(b
, struct vtn_loop
);
389 loop
->node
.type
= vtn_cf_node_type_loop
;
390 list_inithead(&loop
->body
);
391 list_inithead(&loop
->cont_body
);
392 loop
->control
= block
->merge
[3];
394 list_addtail(&loop
->node
.link
, cf_list
);
397 struct vtn_block
*new_loop_break
=
398 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
399 struct vtn_block
*new_loop_cont
=
400 vtn_value(b
, block
->merge
[2], vtn_value_type_block
)->block
;
402 /* Note: This recursive call will start with the current block as
403 * its start block. If we weren't careful, we would get here
404 * again and end up in infinite recursion. This is why we set
405 * block->loop above and check for it before creating one. This
406 * way, we only create the loop once and the second call that
407 * tries to handle this loop goes to the cases below and gets
408 * handled as a regular block.
410 * Note: When we make the recursive walk calls, we pass NULL for
411 * the switch break since you have to break out of the loop first.
412 * We do, however, still pass the current switch case because it's
413 * possible that the merge block for the loop is the start of
416 vtn_cfg_walk_blocks(b
, &loop
->body
, block
, switch_case
, NULL
,
417 new_loop_break
, new_loop_cont
, NULL
);
418 vtn_cfg_walk_blocks(b
, &loop
->cont_body
, new_loop_cont
, NULL
, NULL
,
419 new_loop_break
, NULL
, block
);
421 enum vtn_branch_type branch_type
=
422 vtn_get_branch_type(b
, new_loop_break
, switch_case
, switch_break
,
423 loop_break
, loop_cont
);
425 if (branch_type
!= vtn_branch_type_none
) {
426 /* Stop walking through the CFG when this inner loop's break block
427 * ends up as the same block as the outer loop's continue block
428 * because we are already going to visit it.
430 vtn_assert(branch_type
== vtn_branch_type_loop_continue
);
434 block
= new_loop_break
;
438 vtn_assert(block
->node
.link
.next
== NULL
);
439 list_addtail(&block
->node
.link
, cf_list
);
441 switch (*block
->branch
& SpvOpCodeMask
) {
443 struct vtn_block
*branch_block
=
444 vtn_value(b
, block
->branch
[1], vtn_value_type_block
)->block
;
446 block
->branch_type
= vtn_get_branch_type(b
, branch_block
,
447 switch_case
, switch_break
,
448 loop_break
, loop_cont
);
450 if (block
->branch_type
!= vtn_branch_type_none
)
453 block
= branch_block
;
458 case SpvOpReturnValue
:
459 block
->branch_type
= vtn_branch_type_return
;
463 block
->branch_type
= vtn_branch_type_discard
;
466 case SpvOpBranchConditional
: {
467 struct vtn_block
*then_block
=
468 vtn_value(b
, block
->branch
[2], vtn_value_type_block
)->block
;
469 struct vtn_block
*else_block
=
470 vtn_value(b
, block
->branch
[3], vtn_value_type_block
)->block
;
472 struct vtn_if
*if_stmt
= ralloc(b
, struct vtn_if
);
474 if_stmt
->node
.type
= vtn_cf_node_type_if
;
475 if_stmt
->condition
= block
->branch
[1];
476 list_inithead(&if_stmt
->then_body
);
477 list_inithead(&if_stmt
->else_body
);
479 list_addtail(&if_stmt
->node
.link
, cf_list
);
482 (*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
) {
483 if_stmt
->control
= block
->merge
[2];
486 if_stmt
->then_type
= vtn_get_branch_type(b
, then_block
,
487 switch_case
, switch_break
,
488 loop_break
, loop_cont
);
489 if_stmt
->else_type
= vtn_get_branch_type(b
, else_block
,
490 switch_case
, switch_break
,
491 loop_break
, loop_cont
);
493 if (then_block
== else_block
) {
494 block
->branch_type
= if_stmt
->then_type
;
495 if (block
->branch_type
== vtn_branch_type_none
) {
501 } else if (if_stmt
->then_type
== vtn_branch_type_none
&&
502 if_stmt
->else_type
== vtn_branch_type_none
) {
503 /* Neither side of the if is something we can short-circuit. */
504 vtn_assert((*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
);
505 struct vtn_block
*merge_block
=
506 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
508 vtn_cfg_walk_blocks(b
, &if_stmt
->then_body
, then_block
,
509 switch_case
, switch_break
,
510 loop_break
, loop_cont
, merge_block
);
511 vtn_cfg_walk_blocks(b
, &if_stmt
->else_body
, else_block
,
512 switch_case
, switch_break
,
513 loop_break
, loop_cont
, merge_block
);
515 enum vtn_branch_type merge_type
=
516 vtn_get_branch_type(b
, merge_block
, switch_case
, switch_break
,
517 loop_break
, loop_cont
);
518 if (merge_type
== vtn_branch_type_none
) {
524 } else if (if_stmt
->then_type
!= vtn_branch_type_none
&&
525 if_stmt
->else_type
!= vtn_branch_type_none
) {
526 /* Both sides were short-circuited. We're done here. */
529 /* Exeactly one side of the branch could be short-circuited.
530 * We set the branch up as a predicated break/continue and we
531 * continue on with the other side as if it were what comes
534 if (if_stmt
->then_type
== vtn_branch_type_none
) {
541 vtn_fail("Should have returned or continued");
545 vtn_assert((*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
);
546 struct vtn_block
*break_block
=
547 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
549 struct vtn_switch
*swtch
= ralloc(b
, struct vtn_switch
);
551 swtch
->node
.type
= vtn_cf_node_type_switch
;
552 swtch
->selector
= block
->branch
[1];
553 list_inithead(&swtch
->cases
);
555 list_addtail(&swtch
->node
.link
, cf_list
);
557 /* First, we go through and record all of the cases. */
558 const uint32_t *branch_end
=
559 block
->branch
+ (block
->branch
[0] >> SpvWordCountShift
);
561 struct vtn_value
*cond_val
= vtn_untyped_value(b
, block
->branch
[1]);
562 vtn_fail_if(!cond_val
->type
||
563 cond_val
->type
->base_type
!= vtn_base_type_scalar
,
564 "Selector of OpSelect must have a type of OpTypeInt");
566 nir_alu_type cond_type
=
567 nir_get_nir_type_for_glsl_type(cond_val
->type
->type
);
568 vtn_fail_if(nir_alu_type_get_base_type(cond_type
) != nir_type_int
&&
569 nir_alu_type_get_base_type(cond_type
) != nir_type_uint
,
570 "Selector of OpSelect must have a type of OpTypeInt");
572 bool is_default
= true;
573 const unsigned bitsize
= nir_alu_type_get_type_size(cond_type
);
574 for (const uint32_t *w
= block
->branch
+ 2; w
< branch_end
;) {
575 uint64_t literal
= 0;
580 assert(bitsize
== 64);
581 literal
= vtn_u64_literal(w
);
586 uint32_t block_id
= *(w
++);
588 vtn_add_case(b
, swtch
, break_block
, block_id
, literal
, is_default
);
592 /* Now, we go through and walk the blocks. While we walk through
593 * the blocks, we also gather the much-needed fall-through
596 list_for_each_entry(struct vtn_case
, cse
, &swtch
->cases
, link
) {
597 vtn_assert(cse
->start_block
!= break_block
);
598 vtn_cfg_walk_blocks(b
, &cse
->body
, cse
->start_block
, cse
,
599 break_block
, loop_break
, loop_cont
, NULL
);
602 /* Finally, we walk over all of the cases one more time and put
603 * them in fall-through order.
605 for (const uint32_t *w
= block
->branch
+ 2; w
< branch_end
;) {
606 struct vtn_block
*case_block
=
607 vtn_value(b
, *w
, vtn_value_type_block
)->block
;
612 assert(bitsize
== 64);
616 if (case_block
== break_block
)
619 vtn_assert(case_block
->switch_case
);
621 vtn_order_case(swtch
, case_block
->switch_case
);
624 enum vtn_branch_type branch_type
=
625 vtn_get_branch_type(b
, break_block
, switch_case
, NULL
,
626 loop_break
, loop_cont
);
628 if (branch_type
!= vtn_branch_type_none
) {
629 /* It is possible that the break is actually the continue block
630 * for the containing loop. In this case, we need to bail and let
631 * the loop parsing code handle the continue properly.
633 vtn_assert(branch_type
== vtn_branch_type_loop_continue
);
641 case SpvOpUnreachable
:
645 vtn_fail("Unhandled opcode");
651 vtn_build_cfg(struct vtn_builder
*b
, const uint32_t *words
, const uint32_t *end
)
653 vtn_foreach_instruction(b
, words
, end
,
654 vtn_cfg_handle_prepass_instruction
);
656 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
657 vtn_cfg_walk_blocks(b
, &func
->body
, func
->start_block
,
658 NULL
, NULL
, NULL
, NULL
, NULL
);
663 vtn_handle_phis_first_pass(struct vtn_builder
*b
, SpvOp opcode
,
664 const uint32_t *w
, unsigned count
)
666 if (opcode
== SpvOpLabel
)
667 return true; /* Nothing to do */
669 /* If this isn't a phi node, stop. */
670 if (opcode
!= SpvOpPhi
)
673 /* For handling phi nodes, we do a poor-man's out-of-ssa on the spot.
674 * For each phi, we create a variable with the appropreate type and
675 * do a load from that variable. Then, in a second pass, we add
676 * stores to that variable to each of the predecessor blocks.
678 * We could do something more intelligent here. However, in order to
679 * handle loops and things properly, we really need dominance
680 * information. It would end up basically being the into-SSA
681 * algorithm all over again. It's easier if we just let
682 * lower_vars_to_ssa do that for us instead of repeating it here.
684 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
685 nir_variable
*phi_var
=
686 nir_local_variable_create(b
->nb
.impl
, type
->type
, "phi");
687 _mesa_hash_table_insert(b
->phi_table
, w
, phi_var
);
689 vtn_push_ssa(b
, w
[2], type
,
690 vtn_local_load(b
, nir_build_deref_var(&b
->nb
, phi_var
)));
696 vtn_handle_phi_second_pass(struct vtn_builder
*b
, SpvOp opcode
,
697 const uint32_t *w
, unsigned count
)
699 if (opcode
!= SpvOpPhi
)
702 struct hash_entry
*phi_entry
= _mesa_hash_table_search(b
->phi_table
, w
);
703 vtn_assert(phi_entry
);
704 nir_variable
*phi_var
= phi_entry
->data
;
706 for (unsigned i
= 3; i
< count
; i
+= 2) {
707 struct vtn_block
*pred
=
708 vtn_value(b
, w
[i
+ 1], vtn_value_type_block
)->block
;
710 b
->nb
.cursor
= nir_after_instr(&pred
->end_nop
->instr
);
712 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[i
]);
714 vtn_local_store(b
, src
, nir_build_deref_var(&b
->nb
, phi_var
));
721 vtn_emit_branch(struct vtn_builder
*b
, enum vtn_branch_type branch_type
,
722 nir_variable
*switch_fall_var
, bool *has_switch_break
)
724 switch (branch_type
) {
725 case vtn_branch_type_switch_break
:
726 nir_store_var(&b
->nb
, switch_fall_var
, nir_imm_false(&b
->nb
), 1);
727 *has_switch_break
= true;
729 case vtn_branch_type_switch_fallthrough
:
730 break; /* Nothing to do */
731 case vtn_branch_type_loop_break
:
732 nir_jump(&b
->nb
, nir_jump_break
);
734 case vtn_branch_type_loop_continue
:
735 nir_jump(&b
->nb
, nir_jump_continue
);
737 case vtn_branch_type_return
:
738 nir_jump(&b
->nb
, nir_jump_return
);
740 case vtn_branch_type_discard
: {
741 nir_intrinsic_instr
*discard
=
742 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_discard
);
743 nir_builder_instr_insert(&b
->nb
, &discard
->instr
);
747 vtn_fail("Invalid branch type");
752 vtn_emit_cf_list(struct vtn_builder
*b
, struct list_head
*cf_list
,
753 nir_variable
*switch_fall_var
, bool *has_switch_break
,
754 vtn_instruction_handler handler
)
756 list_for_each_entry(struct vtn_cf_node
, node
, cf_list
, link
) {
757 switch (node
->type
) {
758 case vtn_cf_node_type_block
: {
759 struct vtn_block
*block
= (struct vtn_block
*)node
;
761 const uint32_t *block_start
= block
->label
;
762 const uint32_t *block_end
= block
->merge
? block
->merge
:
765 block_start
= vtn_foreach_instruction(b
, block_start
, block_end
,
766 vtn_handle_phis_first_pass
);
768 vtn_foreach_instruction(b
, block_start
, block_end
, handler
);
770 block
->end_nop
= nir_intrinsic_instr_create(b
->nb
.shader
,
772 nir_builder_instr_insert(&b
->nb
, &block
->end_nop
->instr
);
774 if ((*block
->branch
& SpvOpCodeMask
) == SpvOpReturnValue
) {
775 vtn_fail_if(b
->func
->type
->return_type
->base_type
==
777 "Return with a value from a function returning void");
778 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, block
->branch
[1]);
779 nir_deref_instr
*ret_deref
=
780 nir_build_deref_cast(&b
->nb
, nir_load_param(&b
->nb
, 0),
781 nir_var_local
, src
->type
);
782 vtn_local_store(b
, src
, ret_deref
);
785 if (block
->branch_type
!= vtn_branch_type_none
) {
786 vtn_emit_branch(b
, block
->branch_type
,
787 switch_fall_var
, has_switch_break
);
793 case vtn_cf_node_type_if
: {
794 struct vtn_if
*vtn_if
= (struct vtn_if
*)node
;
795 bool sw_break
= false;
798 nir_push_if(&b
->nb
, vtn_ssa_value(b
, vtn_if
->condition
)->def
);
799 if (vtn_if
->then_type
== vtn_branch_type_none
) {
800 vtn_emit_cf_list(b
, &vtn_if
->then_body
,
801 switch_fall_var
, &sw_break
, handler
);
803 vtn_emit_branch(b
, vtn_if
->then_type
, switch_fall_var
, &sw_break
);
806 nir_push_else(&b
->nb
, nif
);
807 if (vtn_if
->else_type
== vtn_branch_type_none
) {
808 vtn_emit_cf_list(b
, &vtn_if
->else_body
,
809 switch_fall_var
, &sw_break
, handler
);
811 vtn_emit_branch(b
, vtn_if
->else_type
, switch_fall_var
, &sw_break
);
814 nir_pop_if(&b
->nb
, nif
);
816 /* If we encountered a switch break somewhere inside of the if,
817 * then it would have been handled correctly by calling
818 * emit_cf_list or emit_branch for the interrior. However, we
819 * need to predicate everything following on wether or not we're
823 *has_switch_break
= true;
824 nir_push_if(&b
->nb
, nir_load_var(&b
->nb
, switch_fall_var
));
829 case vtn_cf_node_type_loop
: {
830 struct vtn_loop
*vtn_loop
= (struct vtn_loop
*)node
;
832 nir_loop
*loop
= nir_push_loop(&b
->nb
);
833 vtn_emit_cf_list(b
, &vtn_loop
->body
, NULL
, NULL
, handler
);
835 if (!list_empty(&vtn_loop
->cont_body
)) {
836 /* If we have a non-trivial continue body then we need to put
837 * it at the beginning of the loop with a flag to ensure that
838 * it doesn't get executed in the first iteration.
840 nir_variable
*do_cont
=
841 nir_local_variable_create(b
->nb
.impl
, glsl_bool_type(), "cont");
843 b
->nb
.cursor
= nir_before_cf_node(&loop
->cf_node
);
844 nir_store_var(&b
->nb
, do_cont
, nir_imm_false(&b
->nb
), 1);
846 b
->nb
.cursor
= nir_before_cf_list(&loop
->body
);
849 nir_push_if(&b
->nb
, nir_load_var(&b
->nb
, do_cont
));
851 vtn_emit_cf_list(b
, &vtn_loop
->cont_body
, NULL
, NULL
, handler
);
853 nir_pop_if(&b
->nb
, cont_if
);
855 nir_store_var(&b
->nb
, do_cont
, nir_imm_true(&b
->nb
), 1);
857 b
->has_loop_continue
= true;
860 nir_pop_loop(&b
->nb
, loop
);
864 case vtn_cf_node_type_switch
: {
865 struct vtn_switch
*vtn_switch
= (struct vtn_switch
*)node
;
867 /* First, we create a variable to keep track of whether or not the
868 * switch is still going at any given point. Any switch breaks
869 * will set this variable to false.
871 nir_variable
*fall_var
=
872 nir_local_variable_create(b
->nb
.impl
, glsl_bool_type(), "fall");
873 nir_store_var(&b
->nb
, fall_var
, nir_imm_false(&b
->nb
), 1);
875 /* Next, we gather up all of the conditions. We have to do this
876 * up-front because we also need to build an "any" condition so
877 * that we can use !any for default.
879 const int num_cases
= list_length(&vtn_switch
->cases
);
880 NIR_VLA(nir_ssa_def
*, conditions
, num_cases
);
882 nir_ssa_def
*sel
= vtn_ssa_value(b
, vtn_switch
->selector
)->def
;
883 /* An accumulation of all conditions. Used for the default */
884 nir_ssa_def
*any
= NULL
;
887 list_for_each_entry(struct vtn_case
, cse
, &vtn_switch
->cases
, link
) {
888 if (cse
->is_default
) {
889 conditions
[i
++] = NULL
;
893 nir_ssa_def
*cond
= NULL
;
894 util_dynarray_foreach(&cse
->values
, uint64_t, val
) {
895 nir_ssa_def
*imm
= nir_imm_intN_t(&b
->nb
, *val
, sel
->bit_size
);
896 nir_ssa_def
*is_val
= nir_ieq(&b
->nb
, sel
, imm
);
898 cond
= cond
? nir_ior(&b
->nb
, cond
, is_val
) : is_val
;
901 any
= any
? nir_ior(&b
->nb
, any
, cond
) : cond
;
902 conditions
[i
++] = cond
;
904 vtn_assert(i
== num_cases
);
906 /* Now we can walk the list of cases and actually emit code */
908 list_for_each_entry(struct vtn_case
, cse
, &vtn_switch
->cases
, link
) {
909 /* Figure out the condition */
910 nir_ssa_def
*cond
= conditions
[i
++];
911 if (cse
->is_default
) {
912 vtn_assert(cond
== NULL
);
913 cond
= nir_inot(&b
->nb
, any
);
915 /* Take fallthrough into account */
916 cond
= nir_ior(&b
->nb
, cond
, nir_load_var(&b
->nb
, fall_var
));
918 nir_if
*case_if
= nir_push_if(&b
->nb
, cond
);
920 bool has_break
= false;
921 nir_store_var(&b
->nb
, fall_var
, nir_imm_true(&b
->nb
), 1);
922 vtn_emit_cf_list(b
, &cse
->body
, fall_var
, &has_break
, handler
);
923 (void)has_break
; /* We don't care */
925 nir_pop_if(&b
->nb
, case_if
);
927 vtn_assert(i
== num_cases
);
933 vtn_fail("Invalid CF node type");
939 vtn_function_emit(struct vtn_builder
*b
, struct vtn_function
*func
,
940 vtn_instruction_handler instruction_handler
)
942 nir_builder_init(&b
->nb
, func
->impl
);
944 b
->nb
.cursor
= nir_after_cf_list(&func
->impl
->body
);
945 b
->has_loop_continue
= false;
946 b
->phi_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
947 _mesa_key_pointer_equal
);
949 vtn_emit_cf_list(b
, &func
->body
, NULL
, NULL
, instruction_handler
);
951 vtn_foreach_instruction(b
, func
->start_block
->label
, func
->end
,
952 vtn_handle_phi_second_pass
);
954 /* Continue blocks for loops get inserted before the body of the loop
955 * but instructions in the continue may use SSA defs in the loop body.
956 * Therefore, we need to repair SSA to insert the needed phi nodes.
958 if (b
->has_loop_continue
)
959 nir_repair_ssa_impl(func
->impl
);
961 func
->emitted
= true;