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_pointer_for_image_or_sampler_variable(struct vtn_builder
*b
,
29 struct vtn_variable
*var
)
31 assert(var
->type
->base_type
== vtn_base_type_image
||
32 var
->type
->base_type
== vtn_base_type_sampler
);
34 struct vtn_type
*ptr_type
= rzalloc(b
, struct vtn_type
);
35 ptr_type
->base_type
= vtn_base_type_pointer
;
36 ptr_type
->storage_class
= SpvStorageClassUniformConstant
;
37 ptr_type
->deref
= var
->type
;
39 return vtn_pointer_for_variable(b
, var
, ptr_type
);
43 vtn_cfg_handle_prepass_instruction(struct vtn_builder
*b
, SpvOp opcode
,
44 const uint32_t *w
, unsigned count
)
48 vtn_assert(b
->func
== NULL
);
49 b
->func
= rzalloc(b
, struct vtn_function
);
51 list_inithead(&b
->func
->body
);
52 b
->func
->control
= w
[3];
54 MAYBE_UNUSED
const struct glsl_type
*result_type
=
55 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
56 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_function
);
59 const struct vtn_type
*func_type
=
60 vtn_value(b
, w
[4], vtn_value_type_type
)->type
;
62 vtn_assert(func_type
->return_type
->type
== result_type
);
65 nir_function_create(b
->shader
, ralloc_strdup(b
->shader
, val
->name
));
67 func
->num_params
= func_type
->length
;
68 func
->params
= ralloc_array(b
->shader
, nir_parameter
, func
->num_params
);
70 for (unsigned i
= 0; i
< func_type
->length
; i
++) {
71 if (func_type
->params
[i
]->base_type
== vtn_base_type_pointer
&&
72 func_type
->params
[i
]->type
== NULL
) {
73 func
->params
[np
].type
= func_type
->params
[i
]->deref
->type
;
74 func
->params
[np
].param_type
= nir_parameter_inout
;
76 } else if (func_type
->params
[i
]->base_type
==
77 vtn_base_type_sampled_image
) {
78 /* Sampled images are actually two parameters */
79 func
->params
= reralloc(b
->shader
, func
->params
,
80 nir_parameter
, func
->num_params
++);
81 func
->params
[np
].type
= func_type
->params
[i
]->type
;
82 func
->params
[np
].param_type
= nir_parameter_in
;
84 func
->params
[np
].type
= glsl_bare_sampler_type();
85 func
->params
[np
].param_type
= nir_parameter_in
;
88 func
->params
[np
].type
= func_type
->params
[i
]->type
;
89 func
->params
[np
].param_type
= nir_parameter_in
;
93 assert(np
== func
->num_params
);
95 func
->return_type
= func_type
->return_type
->type
;
97 b
->func
->impl
= nir_function_impl_create(func
);
98 b
->nb
.cursor
= nir_before_cf_list(&b
->func
->impl
->body
);
100 b
->func_param_idx
= 0;
104 case SpvOpFunctionEnd
:
109 case SpvOpFunctionParameter
: {
110 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
112 vtn_assert(b
->func_param_idx
< b
->func
->impl
->num_params
);
113 nir_variable
*param
= b
->func
->impl
->params
[b
->func_param_idx
++];
115 if (type
->base_type
== vtn_base_type_pointer
&& type
->type
== NULL
) {
116 struct vtn_variable
*vtn_var
= rzalloc(b
, struct vtn_variable
);
117 vtn_var
->type
= type
->deref
;
118 vtn_var
->var
= param
;
120 vtn_assert(vtn_var
->type
->type
== param
->type
);
122 struct vtn_type
*without_array
= vtn_var
->type
;
123 while(glsl_type_is_array(without_array
->type
))
124 without_array
= without_array
->array_element
;
126 if (glsl_type_is_image(without_array
->type
)) {
127 vtn_var
->mode
= vtn_variable_mode_uniform
;
128 param
->interface_type
= without_array
->type
;
129 } else if (glsl_type_is_sampler(without_array
->type
)) {
130 vtn_var
->mode
= vtn_variable_mode_uniform
;
131 param
->interface_type
= without_array
->type
;
133 vtn_var
->mode
= vtn_variable_mode_param
;
136 struct vtn_value
*val
=
137 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
139 /* Name the parameter so it shows up nicely in NIR */
140 param
->name
= ralloc_strdup(param
, val
->name
);
142 val
->pointer
= vtn_pointer_for_variable(b
, vtn_var
, type
);
143 } else if (type
->base_type
== vtn_base_type_image
||
144 type
->base_type
== vtn_base_type_sampler
||
145 type
->base_type
== vtn_base_type_sampled_image
) {
146 struct vtn_variable
*vtn_var
= rzalloc(b
, struct vtn_variable
);
147 vtn_var
->type
= type
;
148 vtn_var
->var
= param
;
149 param
->interface_type
= param
->type
;
151 if (type
->base_type
== vtn_base_type_sampled_image
) {
152 /* Sampled images are actually two parameters. The first is the
153 * image and the second is the sampler.
155 struct vtn_value
*val
=
156 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
158 /* Name the parameter so it shows up nicely in NIR */
159 param
->name
= ralloc_strdup(param
, val
->name
);
161 /* Adjust the type of the image variable to the image type */
162 vtn_var
->type
= type
->image
;
164 /* Now get the sampler parameter and set up its variable */
165 param
= b
->func
->impl
->params
[b
->func_param_idx
++];
166 struct vtn_variable
*sampler_var
= rzalloc(b
, struct vtn_variable
);
167 sampler_var
->type
= rzalloc(b
, struct vtn_type
);
168 sampler_var
->type
->base_type
= vtn_base_type_sampler
;
169 sampler_var
->type
->type
= glsl_bare_sampler_type();
170 sampler_var
->var
= param
;
171 param
->interface_type
= param
->type
;
172 param
->name
= ralloc_strdup(param
, val
->name
);
174 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
175 val
->sampled_image
->type
= type
;
176 val
->sampled_image
->image
=
177 vtn_pointer_for_image_or_sampler_variable(b
, vtn_var
);
178 val
->sampled_image
->sampler
=
179 vtn_pointer_for_image_or_sampler_variable(b
, sampler_var
);
181 struct vtn_value
*val
=
182 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
184 /* Name the parameter so it shows up nicely in NIR */
185 param
->name
= ralloc_strdup(param
, val
->name
);
188 vtn_pointer_for_image_or_sampler_variable(b
, vtn_var
);
191 /* We're a regular SSA value. */
192 struct vtn_ssa_value
*param_ssa
=
193 vtn_local_load(b
, nir_deref_var_create(b
, param
));
194 struct vtn_value
*val
= vtn_push_ssa(b
, w
[2], type
, param_ssa
);
196 /* Name the parameter so it shows up nicely in NIR */
197 param
->name
= ralloc_strdup(param
, val
->name
);
203 vtn_assert(b
->block
== NULL
);
204 b
->block
= rzalloc(b
, struct vtn_block
);
205 b
->block
->node
.type
= vtn_cf_node_type_block
;
207 vtn_push_value(b
, w
[1], vtn_value_type_block
)->block
= b
->block
;
209 if (b
->func
->start_block
== NULL
) {
210 /* This is the first block encountered for this function. In this
211 * case, we set the start block and add it to the list of
212 * implemented functions that we'll walk later.
214 b
->func
->start_block
= b
->block
;
215 exec_list_push_tail(&b
->functions
, &b
->func
->node
);
220 case SpvOpSelectionMerge
:
222 vtn_assert(b
->block
&& b
->block
->merge
== NULL
);
227 case SpvOpBranchConditional
:
231 case SpvOpReturnValue
:
232 case SpvOpUnreachable
:
233 vtn_assert(b
->block
&& b
->block
->branch
== NULL
);
234 b
->block
->branch
= w
;
239 /* Continue on as per normal */
247 vtn_add_case(struct vtn_builder
*b
, struct vtn_switch
*swtch
,
248 struct vtn_block
*break_block
,
249 uint32_t block_id
, uint64_t val
, bool is_default
)
251 struct vtn_block
*case_block
=
252 vtn_value(b
, block_id
, vtn_value_type_block
)->block
;
254 /* Don't create dummy cases that just break */
255 if (case_block
== break_block
)
258 if (case_block
->switch_case
== NULL
) {
259 struct vtn_case
*c
= ralloc(b
, struct vtn_case
);
261 list_inithead(&c
->body
);
262 c
->start_block
= case_block
;
263 c
->fallthrough
= NULL
;
264 util_dynarray_init(&c
->values
, b
);
265 c
->is_default
= false;
268 list_addtail(&c
->link
, &swtch
->cases
);
270 case_block
->switch_case
= c
;
274 case_block
->switch_case
->is_default
= true;
276 util_dynarray_append(&case_block
->switch_case
->values
, uint64_t, val
);
280 /* This function performs a depth-first search of the cases and puts them
281 * in fall-through order.
284 vtn_order_case(struct vtn_switch
*swtch
, struct vtn_case
*cse
)
291 list_del(&cse
->link
);
293 if (cse
->fallthrough
) {
294 vtn_order_case(swtch
, cse
->fallthrough
);
296 /* If we have a fall-through, place this case right before the case it
297 * falls through to. This ensures that fallthroughs come one after
298 * the other. These two can never get separated because that would
299 * imply something else falling through to the same case. Also, this
300 * can't break ordering because the DFS ensures that this case is
301 * visited before anything that falls through to it.
303 list_addtail(&cse
->link
, &cse
->fallthrough
->link
);
305 list_add(&cse
->link
, &swtch
->cases
);
309 static enum vtn_branch_type
310 vtn_get_branch_type(struct vtn_builder
*b
,
311 struct vtn_block
*block
,
312 struct vtn_case
*swcase
, struct vtn_block
*switch_break
,
313 struct vtn_block
*loop_break
, struct vtn_block
*loop_cont
)
315 if (block
->switch_case
) {
316 /* This branch is actually a fallthrough */
317 vtn_assert(swcase
->fallthrough
== NULL
||
318 swcase
->fallthrough
== block
->switch_case
);
319 swcase
->fallthrough
= block
->switch_case
;
320 return vtn_branch_type_switch_fallthrough
;
321 } else if (block
== loop_break
) {
322 return vtn_branch_type_loop_break
;
323 } else if (block
== loop_cont
) {
324 return vtn_branch_type_loop_continue
;
325 } else if (block
== switch_break
) {
326 return vtn_branch_type_switch_break
;
328 return vtn_branch_type_none
;
333 vtn_cfg_walk_blocks(struct vtn_builder
*b
, struct list_head
*cf_list
,
334 struct vtn_block
*start
, struct vtn_case
*switch_case
,
335 struct vtn_block
*switch_break
,
336 struct vtn_block
*loop_break
, struct vtn_block
*loop_cont
,
337 struct vtn_block
*end
)
339 struct vtn_block
*block
= start
;
340 while (block
!= end
) {
341 if (block
->merge
&& (*block
->merge
& SpvOpCodeMask
) == SpvOpLoopMerge
&&
343 struct vtn_loop
*loop
= ralloc(b
, struct vtn_loop
);
345 loop
->node
.type
= vtn_cf_node_type_loop
;
346 list_inithead(&loop
->body
);
347 list_inithead(&loop
->cont_body
);
348 loop
->control
= block
->merge
[3];
350 list_addtail(&loop
->node
.link
, cf_list
);
353 struct vtn_block
*new_loop_break
=
354 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
355 struct vtn_block
*new_loop_cont
=
356 vtn_value(b
, block
->merge
[2], vtn_value_type_block
)->block
;
358 /* Note: This recursive call will start with the current block as
359 * its start block. If we weren't careful, we would get here
360 * again and end up in infinite recursion. This is why we set
361 * block->loop above and check for it before creating one. This
362 * way, we only create the loop once and the second call that
363 * tries to handle this loop goes to the cases below and gets
364 * handled as a regular block.
366 * Note: When we make the recursive walk calls, we pass NULL for
367 * the switch break since you have to break out of the loop first.
368 * We do, however, still pass the current switch case because it's
369 * possible that the merge block for the loop is the start of
372 vtn_cfg_walk_blocks(b
, &loop
->body
, block
, switch_case
, NULL
,
373 new_loop_break
, new_loop_cont
, NULL
);
374 vtn_cfg_walk_blocks(b
, &loop
->cont_body
, new_loop_cont
, NULL
, NULL
,
375 new_loop_break
, NULL
, block
);
377 enum vtn_branch_type branch_type
=
378 vtn_get_branch_type(b
, new_loop_break
, switch_case
, switch_break
,
379 loop_break
, loop_cont
);
381 if (branch_type
!= vtn_branch_type_none
) {
382 /* Stop walking through the CFG when this inner loop's break block
383 * ends up as the same block as the outer loop's continue block
384 * because we are already going to visit it.
386 vtn_assert(branch_type
== vtn_branch_type_loop_continue
);
390 block
= new_loop_break
;
394 vtn_assert(block
->node
.link
.next
== NULL
);
395 list_addtail(&block
->node
.link
, cf_list
);
397 switch (*block
->branch
& SpvOpCodeMask
) {
399 struct vtn_block
*branch_block
=
400 vtn_value(b
, block
->branch
[1], vtn_value_type_block
)->block
;
402 block
->branch_type
= vtn_get_branch_type(b
, branch_block
,
403 switch_case
, switch_break
,
404 loop_break
, loop_cont
);
406 if (block
->branch_type
!= vtn_branch_type_none
)
409 block
= branch_block
;
414 case SpvOpReturnValue
:
415 block
->branch_type
= vtn_branch_type_return
;
419 block
->branch_type
= vtn_branch_type_discard
;
422 case SpvOpBranchConditional
: {
423 struct vtn_block
*then_block
=
424 vtn_value(b
, block
->branch
[2], vtn_value_type_block
)->block
;
425 struct vtn_block
*else_block
=
426 vtn_value(b
, block
->branch
[3], vtn_value_type_block
)->block
;
428 struct vtn_if
*if_stmt
= ralloc(b
, struct vtn_if
);
430 if_stmt
->node
.type
= vtn_cf_node_type_if
;
431 if_stmt
->condition
= block
->branch
[1];
432 list_inithead(&if_stmt
->then_body
);
433 list_inithead(&if_stmt
->else_body
);
435 list_addtail(&if_stmt
->node
.link
, cf_list
);
438 (*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
) {
439 if_stmt
->control
= block
->merge
[2];
442 if_stmt
->then_type
= vtn_get_branch_type(b
, then_block
,
443 switch_case
, switch_break
,
444 loop_break
, loop_cont
);
445 if_stmt
->else_type
= vtn_get_branch_type(b
, else_block
,
446 switch_case
, switch_break
,
447 loop_break
, loop_cont
);
449 if (then_block
== else_block
) {
450 block
->branch_type
= if_stmt
->then_type
;
451 if (block
->branch_type
== vtn_branch_type_none
) {
457 } else if (if_stmt
->then_type
== vtn_branch_type_none
&&
458 if_stmt
->else_type
== vtn_branch_type_none
) {
459 /* Neither side of the if is something we can short-circuit. */
460 vtn_assert((*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
);
461 struct vtn_block
*merge_block
=
462 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
464 vtn_cfg_walk_blocks(b
, &if_stmt
->then_body
, then_block
,
465 switch_case
, switch_break
,
466 loop_break
, loop_cont
, merge_block
);
467 vtn_cfg_walk_blocks(b
, &if_stmt
->else_body
, else_block
,
468 switch_case
, switch_break
,
469 loop_break
, loop_cont
, merge_block
);
471 enum vtn_branch_type merge_type
=
472 vtn_get_branch_type(b
, merge_block
, switch_case
, switch_break
,
473 loop_break
, loop_cont
);
474 if (merge_type
== vtn_branch_type_none
) {
480 } else if (if_stmt
->then_type
!= vtn_branch_type_none
&&
481 if_stmt
->else_type
!= vtn_branch_type_none
) {
482 /* Both sides were short-circuited. We're done here. */
485 /* Exeactly one side of the branch could be short-circuited.
486 * We set the branch up as a predicated break/continue and we
487 * continue on with the other side as if it were what comes
490 if (if_stmt
->then_type
== vtn_branch_type_none
) {
497 vtn_fail("Should have returned or continued");
501 vtn_assert((*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
);
502 struct vtn_block
*break_block
=
503 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
505 struct vtn_switch
*swtch
= ralloc(b
, struct vtn_switch
);
507 swtch
->node
.type
= vtn_cf_node_type_switch
;
508 swtch
->selector
= block
->branch
[1];
509 list_inithead(&swtch
->cases
);
511 list_addtail(&swtch
->node
.link
, cf_list
);
513 /* First, we go through and record all of the cases. */
514 const uint32_t *branch_end
=
515 block
->branch
+ (block
->branch
[0] >> SpvWordCountShift
);
517 struct vtn_value
*cond_val
= vtn_untyped_value(b
, block
->branch
[1]);
518 vtn_fail_if(!cond_val
->type
||
519 cond_val
->type
->base_type
!= vtn_base_type_scalar
,
520 "Selector of OpSelect must have a type of OpTypeInt");
522 nir_alu_type cond_type
=
523 nir_get_nir_type_for_glsl_type(cond_val
->type
->type
);
524 vtn_fail_if(nir_alu_type_get_base_type(cond_type
) != nir_type_int
&&
525 nir_alu_type_get_base_type(cond_type
) != nir_type_uint
,
526 "Selector of OpSelect must have a type of OpTypeInt");
528 bool is_default
= true;
529 const unsigned bitsize
= nir_alu_type_get_type_size(cond_type
);
530 for (const uint32_t *w
= block
->branch
+ 2; w
< branch_end
;) {
531 uint64_t literal
= 0;
536 assert(bitsize
== 64);
537 literal
= vtn_u64_literal(w
);
542 uint32_t block_id
= *(w
++);
544 vtn_add_case(b
, swtch
, break_block
, block_id
, literal
, is_default
);
548 /* Now, we go through and walk the blocks. While we walk through
549 * the blocks, we also gather the much-needed fall-through
552 list_for_each_entry(struct vtn_case
, cse
, &swtch
->cases
, link
) {
553 vtn_assert(cse
->start_block
!= break_block
);
554 vtn_cfg_walk_blocks(b
, &cse
->body
, cse
->start_block
, cse
,
555 break_block
, loop_break
, loop_cont
, NULL
);
558 /* Finally, we walk over all of the cases one more time and put
559 * them in fall-through order.
561 for (const uint32_t *w
= block
->branch
+ 2; w
< branch_end
;) {
562 struct vtn_block
*case_block
=
563 vtn_value(b
, *w
, vtn_value_type_block
)->block
;
568 assert(bitsize
== 64);
572 if (case_block
== break_block
)
575 vtn_assert(case_block
->switch_case
);
577 vtn_order_case(swtch
, case_block
->switch_case
);
580 enum vtn_branch_type branch_type
=
581 vtn_get_branch_type(b
, break_block
, switch_case
, NULL
,
582 loop_break
, loop_cont
);
584 if (branch_type
!= vtn_branch_type_none
) {
585 /* It is possible that the break is actually the continue block
586 * for the containing loop. In this case, we need to bail and let
587 * the loop parsing code handle the continue properly.
589 vtn_assert(branch_type
== vtn_branch_type_loop_continue
);
597 case SpvOpUnreachable
:
601 vtn_fail("Unhandled opcode");
607 vtn_build_cfg(struct vtn_builder
*b
, const uint32_t *words
, const uint32_t *end
)
609 vtn_foreach_instruction(b
, words
, end
,
610 vtn_cfg_handle_prepass_instruction
);
612 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
613 vtn_cfg_walk_blocks(b
, &func
->body
, func
->start_block
,
614 NULL
, NULL
, NULL
, NULL
, NULL
);
619 vtn_handle_phis_first_pass(struct vtn_builder
*b
, SpvOp opcode
,
620 const uint32_t *w
, unsigned count
)
622 if (opcode
== SpvOpLabel
)
623 return true; /* Nothing to do */
625 /* If this isn't a phi node, stop. */
626 if (opcode
!= SpvOpPhi
)
629 /* For handling phi nodes, we do a poor-man's out-of-ssa on the spot.
630 * For each phi, we create a variable with the appropreate type and
631 * do a load from that variable. Then, in a second pass, we add
632 * stores to that variable to each of the predecessor blocks.
634 * We could do something more intelligent here. However, in order to
635 * handle loops and things properly, we really need dominance
636 * information. It would end up basically being the into-SSA
637 * algorithm all over again. It's easier if we just let
638 * lower_vars_to_ssa do that for us instead of repeating it here.
640 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
641 nir_variable
*phi_var
=
642 nir_local_variable_create(b
->nb
.impl
, type
->type
, "phi");
643 _mesa_hash_table_insert(b
->phi_table
, w
, phi_var
);
645 vtn_push_ssa(b
, w
[2], type
,
646 vtn_local_load(b
, nir_deref_var_create(b
, phi_var
)));
652 vtn_handle_phi_second_pass(struct vtn_builder
*b
, SpvOp opcode
,
653 const uint32_t *w
, unsigned count
)
655 if (opcode
!= SpvOpPhi
)
658 struct hash_entry
*phi_entry
= _mesa_hash_table_search(b
->phi_table
, w
);
659 vtn_assert(phi_entry
);
660 nir_variable
*phi_var
= phi_entry
->data
;
662 for (unsigned i
= 3; i
< count
; i
+= 2) {
663 struct vtn_block
*pred
=
664 vtn_value(b
, w
[i
+ 1], vtn_value_type_block
)->block
;
666 b
->nb
.cursor
= nir_after_instr(&pred
->end_nop
->instr
);
668 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[i
]);
670 vtn_local_store(b
, src
, nir_deref_var_create(b
, phi_var
));
677 vtn_emit_branch(struct vtn_builder
*b
, enum vtn_branch_type branch_type
,
678 nir_variable
*switch_fall_var
, bool *has_switch_break
)
680 switch (branch_type
) {
681 case vtn_branch_type_switch_break
:
682 nir_store_var(&b
->nb
, switch_fall_var
, nir_imm_int(&b
->nb
, NIR_FALSE
), 1);
683 *has_switch_break
= true;
685 case vtn_branch_type_switch_fallthrough
:
686 break; /* Nothing to do */
687 case vtn_branch_type_loop_break
:
688 nir_jump(&b
->nb
, nir_jump_break
);
690 case vtn_branch_type_loop_continue
:
691 nir_jump(&b
->nb
, nir_jump_continue
);
693 case vtn_branch_type_return
:
694 nir_jump(&b
->nb
, nir_jump_return
);
696 case vtn_branch_type_discard
: {
697 nir_intrinsic_instr
*discard
=
698 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_discard
);
699 nir_builder_instr_insert(&b
->nb
, &discard
->instr
);
703 vtn_fail("Invalid branch type");
708 vtn_emit_cf_list(struct vtn_builder
*b
, struct list_head
*cf_list
,
709 nir_variable
*switch_fall_var
, bool *has_switch_break
,
710 vtn_instruction_handler handler
)
712 list_for_each_entry(struct vtn_cf_node
, node
, cf_list
, link
) {
713 switch (node
->type
) {
714 case vtn_cf_node_type_block
: {
715 struct vtn_block
*block
= (struct vtn_block
*)node
;
717 const uint32_t *block_start
= block
->label
;
718 const uint32_t *block_end
= block
->merge
? block
->merge
:
721 block_start
= vtn_foreach_instruction(b
, block_start
, block_end
,
722 vtn_handle_phis_first_pass
);
724 vtn_foreach_instruction(b
, block_start
, block_end
, handler
);
726 block
->end_nop
= nir_intrinsic_instr_create(b
->nb
.shader
,
728 nir_builder_instr_insert(&b
->nb
, &block
->end_nop
->instr
);
730 if ((*block
->branch
& SpvOpCodeMask
) == SpvOpReturnValue
) {
731 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, block
->branch
[1]);
732 vtn_local_store(b
, src
,
733 nir_deref_var_create(b
, b
->nb
.impl
->return_var
));
736 if (block
->branch_type
!= vtn_branch_type_none
) {
737 vtn_emit_branch(b
, block
->branch_type
,
738 switch_fall_var
, has_switch_break
);
744 case vtn_cf_node_type_if
: {
745 struct vtn_if
*vtn_if
= (struct vtn_if
*)node
;
746 bool sw_break
= false;
749 nir_push_if(&b
->nb
, vtn_ssa_value(b
, vtn_if
->condition
)->def
);
750 if (vtn_if
->then_type
== vtn_branch_type_none
) {
751 vtn_emit_cf_list(b
, &vtn_if
->then_body
,
752 switch_fall_var
, &sw_break
, handler
);
754 vtn_emit_branch(b
, vtn_if
->then_type
, switch_fall_var
, &sw_break
);
757 nir_push_else(&b
->nb
, nif
);
758 if (vtn_if
->else_type
== vtn_branch_type_none
) {
759 vtn_emit_cf_list(b
, &vtn_if
->else_body
,
760 switch_fall_var
, &sw_break
, handler
);
762 vtn_emit_branch(b
, vtn_if
->else_type
, switch_fall_var
, &sw_break
);
765 nir_pop_if(&b
->nb
, nif
);
767 /* If we encountered a switch break somewhere inside of the if,
768 * then it would have been handled correctly by calling
769 * emit_cf_list or emit_branch for the interrior. However, we
770 * need to predicate everything following on wether or not we're
774 *has_switch_break
= true;
775 nir_push_if(&b
->nb
, nir_load_var(&b
->nb
, switch_fall_var
));
780 case vtn_cf_node_type_loop
: {
781 struct vtn_loop
*vtn_loop
= (struct vtn_loop
*)node
;
783 nir_loop
*loop
= nir_push_loop(&b
->nb
);
784 vtn_emit_cf_list(b
, &vtn_loop
->body
, NULL
, NULL
, handler
);
786 if (!list_empty(&vtn_loop
->cont_body
)) {
787 /* If we have a non-trivial continue body then we need to put
788 * it at the beginning of the loop with a flag to ensure that
789 * it doesn't get executed in the first iteration.
791 nir_variable
*do_cont
=
792 nir_local_variable_create(b
->nb
.impl
, glsl_bool_type(), "cont");
794 b
->nb
.cursor
= nir_before_cf_node(&loop
->cf_node
);
795 nir_store_var(&b
->nb
, do_cont
, nir_imm_int(&b
->nb
, NIR_FALSE
), 1);
797 b
->nb
.cursor
= nir_before_cf_list(&loop
->body
);
800 nir_push_if(&b
->nb
, nir_load_var(&b
->nb
, do_cont
));
802 vtn_emit_cf_list(b
, &vtn_loop
->cont_body
, NULL
, NULL
, handler
);
804 nir_pop_if(&b
->nb
, cont_if
);
806 nir_store_var(&b
->nb
, do_cont
, nir_imm_int(&b
->nb
, NIR_TRUE
), 1);
808 b
->has_loop_continue
= true;
811 nir_pop_loop(&b
->nb
, loop
);
815 case vtn_cf_node_type_switch
: {
816 struct vtn_switch
*vtn_switch
= (struct vtn_switch
*)node
;
818 /* First, we create a variable to keep track of whether or not the
819 * switch is still going at any given point. Any switch breaks
820 * will set this variable to false.
822 nir_variable
*fall_var
=
823 nir_local_variable_create(b
->nb
.impl
, glsl_bool_type(), "fall");
824 nir_store_var(&b
->nb
, fall_var
, nir_imm_int(&b
->nb
, NIR_FALSE
), 1);
826 /* Next, we gather up all of the conditions. We have to do this
827 * up-front because we also need to build an "any" condition so
828 * that we can use !any for default.
830 const int num_cases
= list_length(&vtn_switch
->cases
);
831 NIR_VLA(nir_ssa_def
*, conditions
, num_cases
);
833 nir_ssa_def
*sel
= vtn_ssa_value(b
, vtn_switch
->selector
)->def
;
834 /* An accumulation of all conditions. Used for the default */
835 nir_ssa_def
*any
= NULL
;
838 list_for_each_entry(struct vtn_case
, cse
, &vtn_switch
->cases
, link
) {
839 if (cse
->is_default
) {
840 conditions
[i
++] = NULL
;
844 nir_ssa_def
*cond
= NULL
;
845 util_dynarray_foreach(&cse
->values
, uint64_t, val
) {
846 nir_ssa_def
*imm
= nir_imm_intN_t(&b
->nb
, *val
, sel
->bit_size
);
847 nir_ssa_def
*is_val
= nir_ieq(&b
->nb
, sel
, imm
);
849 cond
= cond
? nir_ior(&b
->nb
, cond
, is_val
) : is_val
;
852 any
= any
? nir_ior(&b
->nb
, any
, cond
) : cond
;
853 conditions
[i
++] = cond
;
855 vtn_assert(i
== num_cases
);
857 /* Now we can walk the list of cases and actually emit code */
859 list_for_each_entry(struct vtn_case
, cse
, &vtn_switch
->cases
, link
) {
860 /* Figure out the condition */
861 nir_ssa_def
*cond
= conditions
[i
++];
862 if (cse
->is_default
) {
863 vtn_assert(cond
== NULL
);
864 cond
= nir_inot(&b
->nb
, any
);
866 /* Take fallthrough into account */
867 cond
= nir_ior(&b
->nb
, cond
, nir_load_var(&b
->nb
, fall_var
));
869 nir_if
*case_if
= nir_push_if(&b
->nb
, cond
);
871 bool has_break
= false;
872 nir_store_var(&b
->nb
, fall_var
, nir_imm_int(&b
->nb
, NIR_TRUE
), 1);
873 vtn_emit_cf_list(b
, &cse
->body
, fall_var
, &has_break
, handler
);
874 (void)has_break
; /* We don't care */
876 nir_pop_if(&b
->nb
, case_if
);
878 vtn_assert(i
== num_cases
);
884 vtn_fail("Invalid CF node type");
890 vtn_function_emit(struct vtn_builder
*b
, struct vtn_function
*func
,
891 vtn_instruction_handler instruction_handler
)
893 nir_builder_init(&b
->nb
, func
->impl
);
894 b
->nb
.cursor
= nir_after_cf_list(&func
->impl
->body
);
895 b
->has_loop_continue
= false;
896 b
->phi_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
897 _mesa_key_pointer_equal
);
899 vtn_emit_cf_list(b
, &func
->body
, NULL
, NULL
, instruction_handler
);
901 vtn_foreach_instruction(b
, func
->start_block
->label
, func
->end
,
902 vtn_handle_phi_second_pass
);
904 /* Continue blocks for loops get inserted before the body of the loop
905 * but instructions in the continue may use SSA defs in the loop body.
906 * Therefore, we need to repair SSA to insert the needed phi nodes.
908 if (b
->has_loop_continue
)
909 nir_repair_ssa_impl(func
->impl
);
911 func
->emitted
= true;