spirv: Add a MESA_SPIRV_FORCE_UNSTRUCTURED environment variable
[mesa.git] / src / compiler / spirv / vtn_cfg.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "vtn_private.h"
25 #include "spirv_info.h"
26 #include "nir/nir_vla.h"
27
28 static struct vtn_block *
29 vtn_block(struct vtn_builder *b, uint32_t value_id)
30 {
31 return vtn_value(b, value_id, vtn_value_type_block)->block;
32 }
33
34 static unsigned
35 glsl_type_count_function_params(const struct glsl_type *type)
36 {
37 if (glsl_type_is_vector_or_scalar(type)) {
38 return 1;
39 } else if (glsl_type_is_array_or_matrix(type)) {
40 return glsl_get_length(type) *
41 glsl_type_count_function_params(glsl_get_array_element(type));
42 } else {
43 assert(glsl_type_is_struct_or_ifc(type));
44 unsigned count = 0;
45 unsigned elems = glsl_get_length(type);
46 for (unsigned i = 0; i < elems; i++) {
47 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
48 count += glsl_type_count_function_params(elem_type);
49 }
50 return count;
51 }
52 }
53
54 static void
55 glsl_type_add_to_function_params(const struct glsl_type *type,
56 nir_function *func,
57 unsigned *param_idx)
58 {
59 if (glsl_type_is_vector_or_scalar(type)) {
60 func->params[(*param_idx)++] = (nir_parameter) {
61 .num_components = glsl_get_vector_elements(type),
62 .bit_size = glsl_get_bit_size(type),
63 };
64 } else if (glsl_type_is_array_or_matrix(type)) {
65 unsigned elems = glsl_get_length(type);
66 const struct glsl_type *elem_type = glsl_get_array_element(type);
67 for (unsigned i = 0; i < elems; i++)
68 glsl_type_add_to_function_params(elem_type,func, param_idx);
69 } else {
70 assert(glsl_type_is_struct_or_ifc(type));
71 unsigned elems = glsl_get_length(type);
72 for (unsigned i = 0; i < elems; i++) {
73 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
74 glsl_type_add_to_function_params(elem_type, func, param_idx);
75 }
76 }
77 }
78
79 static void
80 vtn_ssa_value_add_to_call_params(struct vtn_builder *b,
81 struct vtn_ssa_value *value,
82 nir_call_instr *call,
83 unsigned *param_idx)
84 {
85 if (glsl_type_is_vector_or_scalar(value->type)) {
86 call->params[(*param_idx)++] = nir_src_for_ssa(value->def);
87 } else {
88 unsigned elems = glsl_get_length(value->type);
89 for (unsigned i = 0; i < elems; i++) {
90 vtn_ssa_value_add_to_call_params(b, value->elems[i],
91 call, param_idx);
92 }
93 }
94 }
95
96 static void
97 vtn_ssa_value_load_function_param(struct vtn_builder *b,
98 struct vtn_ssa_value *value,
99 unsigned *param_idx)
100 {
101 if (glsl_type_is_vector_or_scalar(value->type)) {
102 value->def = nir_load_param(&b->nb, (*param_idx)++);
103 } else {
104 unsigned elems = glsl_get_length(value->type);
105 for (unsigned i = 0; i < elems; i++)
106 vtn_ssa_value_load_function_param(b, value->elems[i], param_idx);
107 }
108 }
109
110 void
111 vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
112 const uint32_t *w, unsigned count)
113 {
114 struct vtn_function *vtn_callee =
115 vtn_value(b, w[3], vtn_value_type_function)->func;
116 struct nir_function *callee = vtn_callee->impl->function;
117
118 vtn_callee->referenced = true;
119
120 nir_call_instr *call = nir_call_instr_create(b->nb.shader, callee);
121
122 unsigned param_idx = 0;
123
124 nir_deref_instr *ret_deref = NULL;
125 struct vtn_type *ret_type = vtn_callee->type->return_type;
126 if (ret_type->base_type != vtn_base_type_void) {
127 nir_variable *ret_tmp =
128 nir_local_variable_create(b->nb.impl,
129 glsl_get_bare_type(ret_type->type),
130 "return_tmp");
131 ret_deref = nir_build_deref_var(&b->nb, ret_tmp);
132 call->params[param_idx++] = nir_src_for_ssa(&ret_deref->dest.ssa);
133 }
134
135 for (unsigned i = 0; i < vtn_callee->type->length; i++) {
136 vtn_ssa_value_add_to_call_params(b, vtn_ssa_value(b, w[4 + i]),
137 call, &param_idx);
138 }
139 assert(param_idx == call->num_params);
140
141 nir_builder_instr_insert(&b->nb, &call->instr);
142
143 if (ret_type->base_type == vtn_base_type_void) {
144 vtn_push_value(b, w[2], vtn_value_type_undef);
145 } else {
146 vtn_push_ssa_value(b, w[2], vtn_local_load(b, ret_deref, 0));
147 }
148 }
149
150 static bool
151 vtn_cfg_handle_prepass_instruction(struct vtn_builder *b, SpvOp opcode,
152 const uint32_t *w, unsigned count)
153 {
154 switch (opcode) {
155 case SpvOpFunction: {
156 vtn_assert(b->func == NULL);
157 b->func = rzalloc(b, struct vtn_function);
158
159 b->func->node.type = vtn_cf_node_type_function;
160 b->func->node.parent = NULL;
161 list_inithead(&b->func->body);
162 b->func->control = w[3];
163
164 UNUSED const struct glsl_type *result_type = vtn_get_type(b, w[1])->type;
165 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_function);
166 val->func = b->func;
167
168 b->func->type = vtn_get_type(b, w[4]);
169 const struct vtn_type *func_type = b->func->type;
170
171 vtn_assert(func_type->return_type->type == result_type);
172
173 nir_function *func =
174 nir_function_create(b->shader, ralloc_strdup(b->shader, val->name));
175
176 unsigned num_params = 0;
177 for (unsigned i = 0; i < func_type->length; i++)
178 num_params += glsl_type_count_function_params(func_type->params[i]->type);
179
180 /* Add one parameter for the function return value */
181 if (func_type->return_type->base_type != vtn_base_type_void)
182 num_params++;
183
184 func->num_params = num_params;
185 func->params = ralloc_array(b->shader, nir_parameter, num_params);
186
187 unsigned idx = 0;
188 if (func_type->return_type->base_type != vtn_base_type_void) {
189 nir_address_format addr_format =
190 vtn_mode_to_address_format(b, vtn_variable_mode_function);
191 /* The return value is a regular pointer */
192 func->params[idx++] = (nir_parameter) {
193 .num_components = nir_address_format_num_components(addr_format),
194 .bit_size = nir_address_format_bit_size(addr_format),
195 };
196 }
197
198 for (unsigned i = 0; i < func_type->length; i++)
199 glsl_type_add_to_function_params(func_type->params[i]->type, func, &idx);
200 assert(idx == num_params);
201
202 b->func->impl = nir_function_impl_create(func);
203 nir_builder_init(&b->nb, func->impl);
204 b->nb.cursor = nir_before_cf_list(&b->func->impl->body);
205 b->nb.exact = b->exact;
206
207 b->func_param_idx = 0;
208
209 /* The return value is the first parameter */
210 if (func_type->return_type->base_type != vtn_base_type_void)
211 b->func_param_idx++;
212 break;
213 }
214
215 case SpvOpFunctionEnd:
216 b->func->end = w;
217 b->func = NULL;
218 break;
219
220 case SpvOpFunctionParameter: {
221 vtn_assert(b->func_param_idx < b->func->impl->function->num_params);
222 struct vtn_type *type = vtn_get_type(b, w[1]);
223 struct vtn_ssa_value *value = vtn_create_ssa_value(b, type->type);
224 vtn_ssa_value_load_function_param(b, value, &b->func_param_idx);
225 vtn_push_ssa_value(b, w[2], value);
226 break;
227 }
228
229 case SpvOpLabel: {
230 vtn_assert(b->block == NULL);
231 b->block = rzalloc(b, struct vtn_block);
232 b->block->node.type = vtn_cf_node_type_block;
233 b->block->label = w;
234 vtn_push_value(b, w[1], vtn_value_type_block)->block = b->block;
235
236 if (b->func->start_block == NULL) {
237 /* This is the first block encountered for this function. In this
238 * case, we set the start block and add it to the list of
239 * implemented functions that we'll walk later.
240 */
241 b->func->start_block = b->block;
242 list_addtail(&b->func->node.link, &b->functions);
243 }
244 break;
245 }
246
247 case SpvOpSelectionMerge:
248 case SpvOpLoopMerge:
249 vtn_assert(b->block && b->block->merge == NULL);
250 b->block->merge = w;
251 break;
252
253 case SpvOpBranch:
254 case SpvOpBranchConditional:
255 case SpvOpSwitch:
256 case SpvOpKill:
257 case SpvOpReturn:
258 case SpvOpReturnValue:
259 case SpvOpUnreachable:
260 vtn_assert(b->block && b->block->branch == NULL);
261 b->block->branch = w;
262 b->block = NULL;
263 break;
264
265 default:
266 /* Continue on as per normal */
267 return true;
268 }
269
270 return true;
271 }
272
273 /* This function performs a depth-first search of the cases and puts them
274 * in fall-through order.
275 */
276 static void
277 vtn_order_case(struct vtn_switch *swtch, struct vtn_case *cse)
278 {
279 if (cse->visited)
280 return;
281
282 cse->visited = true;
283
284 list_del(&cse->node.link);
285
286 if (cse->fallthrough) {
287 vtn_order_case(swtch, cse->fallthrough);
288
289 /* If we have a fall-through, place this case right before the case it
290 * falls through to. This ensures that fallthroughs come one after
291 * the other. These two can never get separated because that would
292 * imply something else falling through to the same case. Also, this
293 * can't break ordering because the DFS ensures that this case is
294 * visited before anything that falls through to it.
295 */
296 list_addtail(&cse->node.link, &cse->fallthrough->node.link);
297 } else {
298 list_add(&cse->node.link, &swtch->cases);
299 }
300 }
301
302 static void
303 vtn_switch_order_cases(struct vtn_switch *swtch)
304 {
305 struct list_head cases;
306 list_replace(&swtch->cases, &cases);
307 list_inithead(&swtch->cases);
308 while (!list_is_empty(&cases)) {
309 struct vtn_case *cse =
310 list_first_entry(&cases, struct vtn_case, node.link);
311 vtn_order_case(swtch, cse);
312 }
313 }
314
315 static void
316 vtn_block_set_merge_cf_node(struct vtn_builder *b, struct vtn_block *block,
317 struct vtn_cf_node *cf_node)
318 {
319 vtn_fail_if(block->merge_cf_node != NULL,
320 "The merge block declared by a header block cannot be a "
321 "merge block declared by any other header block.");
322
323 block->merge_cf_node = cf_node;
324 }
325
326 #define VTN_DECL_CF_NODE_FIND(_type) \
327 static inline struct vtn_##_type * \
328 vtn_cf_node_find_##_type(struct vtn_cf_node *node) \
329 { \
330 while (node && node->type != vtn_cf_node_type_##_type) \
331 node = node->parent; \
332 return (struct vtn_##_type *)node; \
333 }
334
335 VTN_DECL_CF_NODE_FIND(if)
336 VTN_DECL_CF_NODE_FIND(loop)
337 VTN_DECL_CF_NODE_FIND(case)
338 VTN_DECL_CF_NODE_FIND(switch)
339 VTN_DECL_CF_NODE_FIND(function)
340
341 static enum vtn_branch_type
342 vtn_handle_branch(struct vtn_builder *b,
343 struct vtn_cf_node *cf_parent,
344 struct vtn_block *target_block)
345 {
346 struct vtn_loop *loop = vtn_cf_node_find_loop(cf_parent);
347
348 /* Detect a loop back-edge first. That way none of the code below
349 * accidentally operates on a loop back-edge.
350 */
351 if (loop && target_block == loop->header_block)
352 return vtn_branch_type_loop_back_edge;
353
354 /* Try to detect fall-through */
355 if (target_block->switch_case) {
356 /* When it comes to handling switch cases, we can break calls to
357 * vtn_handle_branch into two cases: calls from within a case construct
358 * and calls for the jump to each case construct. In the second case,
359 * cf_parent is the vtn_switch itself and vtn_cf_node_find_case() will
360 * return the outer switch case in which this switch is contained. It's
361 * fine if the target block is a switch case from an outer switch as
362 * long as it is also the switch break for this switch.
363 */
364 struct vtn_case *switch_case = vtn_cf_node_find_case(cf_parent);
365
366 /* This doesn't get called for the OpSwitch */
367 vtn_fail_if(switch_case == NULL,
368 "A switch case can only be entered through an OpSwitch or "
369 "falling through from another switch case.");
370
371 /* Because block->switch_case is only set on the entry block for a given
372 * switch case, we only ever get here if we're jumping to the start of a
373 * switch case. It's possible, however, that a switch case could jump
374 * to itself via a back-edge. That *should* get caught by the loop
375 * handling case above but if we have a back edge without a loop merge,
376 * we could en up here.
377 */
378 vtn_fail_if(target_block->switch_case == switch_case,
379 "A switch cannot fall-through to itself. Likely, there is "
380 "a back-edge which is not to a loop header.");
381
382 vtn_fail_if(target_block->switch_case->node.parent !=
383 switch_case->node.parent,
384 "A switch case fall-through must come from the same "
385 "OpSwitch construct");
386
387 vtn_fail_if(switch_case->fallthrough != NULL &&
388 switch_case->fallthrough != target_block->switch_case,
389 "Each case construct can have at most one branch to "
390 "another case construct");
391
392 switch_case->fallthrough = target_block->switch_case;
393
394 /* We don't immediately return vtn_branch_type_switch_fallthrough
395 * because it may also be a loop or switch break for an inner loop or
396 * switch and that takes precedence.
397 */
398 }
399
400 if (loop && target_block == loop->cont_block)
401 return vtn_branch_type_loop_continue;
402
403 /* We walk blocks as a breadth-first search on the control-flow construct
404 * tree where, when we find a construct, we add the vtn_cf_node for that
405 * construct and continue iterating at the merge target block (if any).
406 * Therefore, we want merges whose with parent == cf_parent to be treated
407 * as regular branches. We only want to consider merges if they break out
408 * of the current CF construct.
409 */
410 if (target_block->merge_cf_node != NULL &&
411 target_block->merge_cf_node->parent != cf_parent) {
412 switch (target_block->merge_cf_node->type) {
413 case vtn_cf_node_type_if:
414 for (struct vtn_cf_node *node = cf_parent;
415 node != target_block->merge_cf_node; node = node->parent) {
416 vtn_fail_if(node == NULL || node->type != vtn_cf_node_type_if,
417 "Branching to the merge block of a selection "
418 "construct can only be used to break out of a "
419 "selection construct");
420
421 struct vtn_if *if_stmt = vtn_cf_node_as_if(node);
422
423 /* This should be guaranteed by our iteration */
424 assert(if_stmt->merge_block != target_block);
425
426 vtn_fail_if(if_stmt->merge_block != NULL,
427 "Branching to the merge block of a selection "
428 "construct can only be used to break out of the "
429 "inner most nested selection level");
430 }
431 return vtn_branch_type_if_merge;
432
433 case vtn_cf_node_type_loop:
434 vtn_fail_if(target_block->merge_cf_node != &loop->node,
435 "Loop breaks can only break out of the inner most "
436 "nested loop level");
437 return vtn_branch_type_loop_break;
438
439 case vtn_cf_node_type_switch: {
440 struct vtn_switch *swtch = vtn_cf_node_find_switch(cf_parent);
441 vtn_fail_if(target_block->merge_cf_node != &swtch->node,
442 "Switch breaks can only break out of the inner most "
443 "nested switch level");
444 return vtn_branch_type_switch_break;
445 }
446
447 default:
448 unreachable("Invalid CF node type for a merge");
449 }
450 }
451
452 if (target_block->switch_case)
453 return vtn_branch_type_switch_fallthrough;
454
455 return vtn_branch_type_none;
456 }
457
458 struct vtn_cfg_work_item {
459 struct list_head link;
460
461 struct vtn_cf_node *cf_parent;
462 struct list_head *cf_list;
463 struct vtn_block *start_block;
464 };
465
466 static void
467 vtn_add_cfg_work_item(struct vtn_builder *b,
468 struct list_head *work_list,
469 struct vtn_cf_node *cf_parent,
470 struct list_head *cf_list,
471 struct vtn_block *start_block)
472 {
473 struct vtn_cfg_work_item *work = ralloc(b, struct vtn_cfg_work_item);
474 work->cf_parent = cf_parent;
475 work->cf_list = cf_list;
476 work->start_block = start_block;
477 list_addtail(&work->link, work_list);
478 }
479
480 /* returns the default block */
481 static void
482 vtn_parse_switch(struct vtn_builder *b,
483 struct vtn_switch *swtch,
484 const uint32_t *branch,
485 struct list_head *case_list)
486 {
487 const uint32_t *branch_end = branch + (branch[0] >> SpvWordCountShift);
488
489 struct vtn_value *sel_val = vtn_untyped_value(b, branch[1]);
490 vtn_fail_if(!sel_val->type ||
491 sel_val->type->base_type != vtn_base_type_scalar,
492 "Selector of OpSwitch must have a type of OpTypeInt");
493
494 nir_alu_type sel_type =
495 nir_get_nir_type_for_glsl_type(sel_val->type->type);
496 vtn_fail_if(nir_alu_type_get_base_type(sel_type) != nir_type_int &&
497 nir_alu_type_get_base_type(sel_type) != nir_type_uint,
498 "Selector of OpSwitch must have a type of OpTypeInt");
499
500 struct hash_table *block_to_case = _mesa_pointer_hash_table_create(b);
501
502 bool is_default = true;
503 const unsigned bitsize = nir_alu_type_get_type_size(sel_type);
504 for (const uint32_t *w = branch + 2; w < branch_end;) {
505 uint64_t literal = 0;
506 if (!is_default) {
507 if (bitsize <= 32) {
508 literal = *(w++);
509 } else {
510 assert(bitsize == 64);
511 literal = vtn_u64_literal(w);
512 w += 2;
513 }
514 }
515 struct vtn_block *case_block = vtn_block(b, *(w++));
516
517 struct hash_entry *case_entry =
518 _mesa_hash_table_search(block_to_case, case_block);
519
520 struct vtn_case *cse;
521 if (case_entry) {
522 cse = case_entry->data;
523 } else {
524 cse = rzalloc(b, struct vtn_case);
525
526 cse->node.type = vtn_cf_node_type_case;
527 cse->node.parent = swtch ? &swtch->node : NULL;
528 cse->block = case_block;
529 list_inithead(&cse->body);
530 util_dynarray_init(&cse->values, b);
531
532 list_addtail(&cse->node.link, case_list);
533 _mesa_hash_table_insert(block_to_case, case_block, cse);
534 }
535
536 if (is_default) {
537 cse->is_default = true;
538 } else {
539 util_dynarray_append(&cse->values, uint64_t, literal);
540 }
541
542 is_default = false;
543 }
544
545 _mesa_hash_table_destroy(block_to_case, NULL);
546 }
547
548 /* Processes a block and returns the next block to process or NULL if we've
549 * reached the end of the construct.
550 */
551 static struct vtn_block *
552 vtn_process_block(struct vtn_builder *b,
553 struct list_head *work_list,
554 struct vtn_cf_node *cf_parent,
555 struct list_head *cf_list,
556 struct vtn_block *block)
557 {
558 if (!list_is_empty(cf_list)) {
559 /* vtn_process_block() acts like an iterator: it processes the given
560 * block and then returns the next block to process. For a given
561 * control-flow construct, vtn_build_cfg() calls vtn_process_block()
562 * repeatedly until it finally returns NULL. Therefore, we know that
563 * the only blocks on which vtn_process_block() can be called are either
564 * the first block in a construct or a block that vtn_process_block()
565 * returned for the current construct. If cf_list is empty then we know
566 * that we're processing the first block in the construct and we have to
567 * add it to the list.
568 *
569 * If cf_list is not empty, then it must be the block returned by the
570 * previous call to vtn_process_block(). We know a priori that
571 * vtn_process_block only returns either normal branches
572 * (vtn_branch_type_none) or merge target blocks.
573 */
574 switch (vtn_handle_branch(b, cf_parent, block)) {
575 case vtn_branch_type_none:
576 /* For normal branches, we want to process them and add them to the
577 * current construct. Merge target blocks also look like normal
578 * branches from the perspective of this construct. See also
579 * vtn_handle_branch().
580 */
581 break;
582
583 case vtn_branch_type_loop_continue:
584 case vtn_branch_type_switch_fallthrough:
585 /* The two cases where we can get early exits from a construct that
586 * are not to that construct's merge target are loop continues and
587 * switch fall-throughs. In these cases, we need to break out of the
588 * current construct by returning NULL.
589 */
590 return NULL;
591
592 default:
593 /* The only way we can get here is if something was used as two kinds
594 * of merges at the same time and that's illegal.
595 */
596 vtn_fail("A block was used as a merge target from two or more "
597 "structured control-flow constructs");
598 }
599 }
600
601 /* Once a block has been processed, it is placed into and the list link
602 * will point to something non-null. If we see a node we've already
603 * processed here, it either exists in multiple functions or it's an
604 * invalid back-edge.
605 */
606 if (block->node.parent != NULL) {
607 vtn_fail_if(vtn_cf_node_find_function(&block->node) !=
608 vtn_cf_node_find_function(cf_parent),
609 "A block cannot exist in two functions at the "
610 "same time");
611
612 vtn_fail("Invalid back or cross-edge in the CFG");
613 }
614
615 if (block->merge && (*block->merge & SpvOpCodeMask) == SpvOpLoopMerge &&
616 block->loop == NULL) {
617 vtn_fail_if((*block->branch & SpvOpCodeMask) != SpvOpBranch &&
618 (*block->branch & SpvOpCodeMask) != SpvOpBranchConditional,
619 "An OpLoopMerge instruction must immediately precede "
620 "either an OpBranch or OpBranchConditional instruction.");
621
622 struct vtn_loop *loop = rzalloc(b, struct vtn_loop);
623
624 loop->node.type = vtn_cf_node_type_loop;
625 loop->node.parent = cf_parent;
626 list_inithead(&loop->body);
627 list_inithead(&loop->cont_body);
628 loop->header_block = block;
629 loop->break_block = vtn_block(b, block->merge[1]);
630 loop->cont_block = vtn_block(b, block->merge[2]);
631 loop->control = block->merge[3];
632
633 list_addtail(&loop->node.link, cf_list);
634 block->loop = loop;
635
636 /* Note: The work item for the main loop body will start with the
637 * current block as its start block. If we weren't careful, we would
638 * get here again and end up in an infinite loop. This is why we set
639 * block->loop above and check for it before creating one. This way,
640 * we only create the loop once and the second iteration that tries to
641 * handle this loop goes to the cases below and gets handled as a
642 * regular block.
643 */
644 vtn_add_cfg_work_item(b, work_list, &loop->node,
645 &loop->body, loop->header_block);
646
647 /* For continue targets, SPIR-V guarantees the following:
648 *
649 * - the Continue Target must dominate the back-edge block
650 * - the back-edge block must post dominate the Continue Target
651 *
652 * If the header block is the same as the continue target, this
653 * condition is trivially satisfied and there is no real continue
654 * section.
655 */
656 if (loop->cont_block != loop->header_block) {
657 vtn_add_cfg_work_item(b, work_list, &loop->node,
658 &loop->cont_body, loop->cont_block);
659 }
660
661 vtn_block_set_merge_cf_node(b, loop->break_block, &loop->node);
662
663 return loop->break_block;
664 }
665
666 /* Add the block to the CF list */
667 block->node.parent = cf_parent;
668 list_addtail(&block->node.link, cf_list);
669
670 switch (*block->branch & SpvOpCodeMask) {
671 case SpvOpBranch: {
672 struct vtn_block *branch_block = vtn_block(b, block->branch[1]);
673
674 block->branch_type = vtn_handle_branch(b, cf_parent, branch_block);
675
676 if (block->branch_type == vtn_branch_type_none)
677 return branch_block;
678 else
679 return NULL;
680 }
681
682 case SpvOpReturn:
683 case SpvOpReturnValue:
684 block->branch_type = vtn_branch_type_return;
685 return NULL;
686
687 case SpvOpKill:
688 block->branch_type = vtn_branch_type_discard;
689 return NULL;
690
691 case SpvOpBranchConditional: {
692 struct vtn_value *cond_val = vtn_untyped_value(b, block->branch[1]);
693 vtn_fail_if(!cond_val->type ||
694 cond_val->type->base_type != vtn_base_type_scalar ||
695 cond_val->type->type != glsl_bool_type(),
696 "Condition must be a Boolean type scalar");
697
698 struct vtn_block *then_block = vtn_block(b, block->branch[2]);
699 struct vtn_block *else_block = vtn_block(b, block->branch[3]);
700
701 if (then_block == else_block) {
702 /* This is uncommon but it can happen. We treat this the same way as
703 * an unconditional branch.
704 */
705 block->branch_type = vtn_handle_branch(b, cf_parent, then_block);
706
707 if (block->branch_type == vtn_branch_type_none)
708 return then_block;
709 else
710 return NULL;
711 }
712
713 struct vtn_if *if_stmt = rzalloc(b, struct vtn_if);
714
715 if_stmt->node.type = vtn_cf_node_type_if;
716 if_stmt->node.parent = cf_parent;
717 if_stmt->condition = block->branch[1];
718 list_inithead(&if_stmt->then_body);
719 list_inithead(&if_stmt->else_body);
720
721 list_addtail(&if_stmt->node.link, cf_list);
722
723 if (block->merge &&
724 (*block->merge & SpvOpCodeMask) == SpvOpSelectionMerge) {
725 /* We may not always have a merge block and that merge doesn't
726 * technically have to be an OpSelectionMerge. We could have a block
727 * with an OpLoopMerge which ends in an OpBranchConditional.
728 */
729 if_stmt->merge_block = vtn_block(b, block->merge[1]);
730 vtn_block_set_merge_cf_node(b, if_stmt->merge_block, &if_stmt->node);
731
732 if_stmt->control = block->merge[2];
733 }
734
735 if_stmt->then_type = vtn_handle_branch(b, &if_stmt->node, then_block);
736 if (if_stmt->then_type == vtn_branch_type_none) {
737 vtn_add_cfg_work_item(b, work_list, &if_stmt->node,
738 &if_stmt->then_body, then_block);
739 }
740
741 if_stmt->else_type = vtn_handle_branch(b, &if_stmt->node, else_block);
742 if (if_stmt->else_type == vtn_branch_type_none) {
743 vtn_add_cfg_work_item(b, work_list, &if_stmt->node,
744 &if_stmt->else_body, else_block);
745 }
746
747 return if_stmt->merge_block;
748 }
749
750 case SpvOpSwitch: {
751 struct vtn_switch *swtch = rzalloc(b, struct vtn_switch);
752
753 swtch->node.type = vtn_cf_node_type_switch;
754 swtch->node.parent = cf_parent;
755 swtch->selector = block->branch[1];
756 list_inithead(&swtch->cases);
757
758 list_addtail(&swtch->node.link, cf_list);
759
760 /* We may not always have a merge block */
761 if (block->merge) {
762 vtn_fail_if((*block->merge & SpvOpCodeMask) != SpvOpSelectionMerge,
763 "An OpLoopMerge instruction must immediately precede "
764 "either an OpBranch or OpBranchConditional "
765 "instruction.");
766 swtch->break_block = vtn_block(b, block->merge[1]);
767 vtn_block_set_merge_cf_node(b, swtch->break_block, &swtch->node);
768 }
769
770 /* First, we go through and record all of the cases. */
771 vtn_parse_switch(b, swtch, block->branch, &swtch->cases);
772
773 /* Gather the branch types for the switch */
774 vtn_foreach_cf_node(case_node, &swtch->cases) {
775 struct vtn_case *cse = vtn_cf_node_as_case(case_node);
776
777 cse->type = vtn_handle_branch(b, &swtch->node, cse->block);
778 switch (cse->type) {
779 case vtn_branch_type_none:
780 /* This is a "real" cases which has stuff in it */
781 vtn_fail_if(cse->block->switch_case != NULL,
782 "OpSwitch has a case which is also in another "
783 "OpSwitch construct");
784 cse->block->switch_case = cse;
785 vtn_add_cfg_work_item(b, work_list, &cse->node,
786 &cse->body, cse->block);
787 break;
788
789 case vtn_branch_type_switch_break:
790 case vtn_branch_type_loop_break:
791 case vtn_branch_type_loop_continue:
792 /* Switch breaks as well as loop breaks and continues can be
793 * used to break out of a switch construct or as direct targets
794 * of the OpSwitch.
795 */
796 break;
797
798 default:
799 vtn_fail("Target of OpSwitch is not a valid structured exit "
800 "from the switch construct.");
801 }
802 }
803
804 return swtch->break_block;
805 }
806
807 case SpvOpUnreachable:
808 return NULL;
809
810 default:
811 vtn_fail("Block did not end with a valid branch instruction");
812 }
813 }
814
815 void
816 vtn_build_cfg(struct vtn_builder *b, const uint32_t *words, const uint32_t *end)
817 {
818 vtn_foreach_instruction(b, words, end,
819 vtn_cfg_handle_prepass_instruction);
820
821 if (b->shader->info.stage == MESA_SHADER_KERNEL)
822 return;
823
824 vtn_foreach_cf_node(func_node, &b->functions) {
825 struct vtn_function *func = vtn_cf_node_as_function(func_node);
826
827 /* We build the CFG for each function by doing a breadth-first search on
828 * the control-flow graph. We keep track of our state using a worklist.
829 * Doing a BFS ensures that we visit each structured control-flow
830 * construct and its merge node before we visit the stuff inside the
831 * construct.
832 */
833 struct list_head work_list;
834 list_inithead(&work_list);
835 vtn_add_cfg_work_item(b, &work_list, &func->node, &func->body,
836 func->start_block);
837
838 while (!list_is_empty(&work_list)) {
839 struct vtn_cfg_work_item *work =
840 list_first_entry(&work_list, struct vtn_cfg_work_item, link);
841 list_del(&work->link);
842
843 for (struct vtn_block *block = work->start_block; block; ) {
844 block = vtn_process_block(b, &work_list, work->cf_parent,
845 work->cf_list, block);
846 }
847 }
848 }
849 }
850
851 static bool
852 vtn_handle_phis_first_pass(struct vtn_builder *b, SpvOp opcode,
853 const uint32_t *w, unsigned count)
854 {
855 if (opcode == SpvOpLabel)
856 return true; /* Nothing to do */
857
858 /* If this isn't a phi node, stop. */
859 if (opcode != SpvOpPhi)
860 return false;
861
862 /* For handling phi nodes, we do a poor-man's out-of-ssa on the spot.
863 * For each phi, we create a variable with the appropreate type and
864 * do a load from that variable. Then, in a second pass, we add
865 * stores to that variable to each of the predecessor blocks.
866 *
867 * We could do something more intelligent here. However, in order to
868 * handle loops and things properly, we really need dominance
869 * information. It would end up basically being the into-SSA
870 * algorithm all over again. It's easier if we just let
871 * lower_vars_to_ssa do that for us instead of repeating it here.
872 */
873 struct vtn_type *type = vtn_get_type(b, w[1]);
874 nir_variable *phi_var =
875 nir_local_variable_create(b->nb.impl, type->type, "phi");
876 _mesa_hash_table_insert(b->phi_table, w, phi_var);
877
878 vtn_push_ssa_value(b, w[2],
879 vtn_local_load(b, nir_build_deref_var(&b->nb, phi_var), 0));
880
881 return true;
882 }
883
884 static bool
885 vtn_handle_phi_second_pass(struct vtn_builder *b, SpvOp opcode,
886 const uint32_t *w, unsigned count)
887 {
888 if (opcode != SpvOpPhi)
889 return true;
890
891 struct hash_entry *phi_entry = _mesa_hash_table_search(b->phi_table, w);
892
893 /* It's possible that this phi is in an unreachable block in which case it
894 * may never have been emitted and therefore may not be in the hash table.
895 * In this case, there's no var for it and it's safe to just bail.
896 */
897 if (phi_entry == NULL)
898 return true;
899
900 nir_variable *phi_var = phi_entry->data;
901
902 for (unsigned i = 3; i < count; i += 2) {
903 struct vtn_block *pred = vtn_block(b, w[i + 1]);
904
905 /* If block does not have end_nop, that is because it is an unreacheable
906 * block, and hence it is not worth to handle it */
907 if (!pred->end_nop)
908 continue;
909
910 b->nb.cursor = nir_after_instr(&pred->end_nop->instr);
911
912 struct vtn_ssa_value *src = vtn_ssa_value(b, w[i]);
913
914 vtn_local_store(b, src, nir_build_deref_var(&b->nb, phi_var), 0);
915 }
916
917 return true;
918 }
919
920 static void
921 vtn_emit_branch(struct vtn_builder *b, enum vtn_branch_type branch_type,
922 nir_variable *switch_fall_var, bool *has_switch_break)
923 {
924 switch (branch_type) {
925 case vtn_branch_type_if_merge:
926 break; /* Nothing to do */
927 case vtn_branch_type_switch_break:
928 nir_store_var(&b->nb, switch_fall_var, nir_imm_false(&b->nb), 1);
929 *has_switch_break = true;
930 break;
931 case vtn_branch_type_switch_fallthrough:
932 break; /* Nothing to do */
933 case vtn_branch_type_loop_break:
934 nir_jump(&b->nb, nir_jump_break);
935 break;
936 case vtn_branch_type_loop_continue:
937 nir_jump(&b->nb, nir_jump_continue);
938 break;
939 case vtn_branch_type_loop_back_edge:
940 break;
941 case vtn_branch_type_return:
942 nir_jump(&b->nb, nir_jump_return);
943 break;
944 case vtn_branch_type_discard: {
945 nir_intrinsic_instr *discard =
946 nir_intrinsic_instr_create(b->nb.shader, nir_intrinsic_discard);
947 nir_builder_instr_insert(&b->nb, &discard->instr);
948 break;
949 }
950 default:
951 vtn_fail("Invalid branch type");
952 }
953 }
954
955 static nir_ssa_def *
956 vtn_switch_case_condition(struct vtn_builder *b, struct vtn_switch *swtch,
957 nir_ssa_def *sel, struct vtn_case *cse)
958 {
959 if (cse->is_default) {
960 nir_ssa_def *any = nir_imm_false(&b->nb);
961 vtn_foreach_cf_node(other_node, &swtch->cases) {
962 struct vtn_case *other = vtn_cf_node_as_case(other_node);
963 if (other->is_default)
964 continue;
965
966 any = nir_ior(&b->nb, any,
967 vtn_switch_case_condition(b, swtch, sel, other));
968 }
969 return nir_inot(&b->nb, any);
970 } else {
971 nir_ssa_def *cond = nir_imm_false(&b->nb);
972 util_dynarray_foreach(&cse->values, uint64_t, val) {
973 nir_ssa_def *imm = nir_imm_intN_t(&b->nb, *val, sel->bit_size);
974 cond = nir_ior(&b->nb, cond, nir_ieq(&b->nb, sel, imm));
975 }
976 return cond;
977 }
978 }
979
980 static nir_loop_control
981 vtn_loop_control(struct vtn_builder *b, struct vtn_loop *vtn_loop)
982 {
983 if (vtn_loop->control == SpvLoopControlMaskNone)
984 return nir_loop_control_none;
985 else if (vtn_loop->control & SpvLoopControlDontUnrollMask)
986 return nir_loop_control_dont_unroll;
987 else if (vtn_loop->control & SpvLoopControlUnrollMask)
988 return nir_loop_control_unroll;
989 else if (vtn_loop->control & SpvLoopControlDependencyInfiniteMask ||
990 vtn_loop->control & SpvLoopControlDependencyLengthMask ||
991 vtn_loop->control & SpvLoopControlMinIterationsMask ||
992 vtn_loop->control & SpvLoopControlMaxIterationsMask ||
993 vtn_loop->control & SpvLoopControlIterationMultipleMask ||
994 vtn_loop->control & SpvLoopControlPeelCountMask ||
995 vtn_loop->control & SpvLoopControlPartialCountMask) {
996 /* We do not do anything special with these yet. */
997 return nir_loop_control_none;
998 } else {
999 vtn_fail("Invalid loop control");
1000 }
1001 }
1002
1003 static nir_selection_control
1004 vtn_selection_control(struct vtn_builder *b, struct vtn_if *vtn_if)
1005 {
1006 if (vtn_if->control == SpvSelectionControlMaskNone)
1007 return nir_selection_control_none;
1008 else if (vtn_if->control & SpvSelectionControlDontFlattenMask)
1009 return nir_selection_control_dont_flatten;
1010 else if (vtn_if->control & SpvSelectionControlFlattenMask)
1011 return nir_selection_control_flatten;
1012 else
1013 vtn_fail("Invalid selection control");
1014 }
1015
1016 static void
1017 vtn_emit_ret_store(struct vtn_builder *b, struct vtn_block *block)
1018 {
1019 if ((*block->branch & SpvOpCodeMask) != SpvOpReturnValue)
1020 return;
1021
1022 vtn_fail_if(b->func->type->return_type->base_type == vtn_base_type_void,
1023 "Return with a value from a function returning void");
1024 struct vtn_ssa_value *src = vtn_ssa_value(b, block->branch[1]);
1025 const struct glsl_type *ret_type =
1026 glsl_get_bare_type(b->func->type->return_type->type);
1027 nir_deref_instr *ret_deref =
1028 nir_build_deref_cast(&b->nb, nir_load_param(&b->nb, 0),
1029 nir_var_function_temp, ret_type, 0);
1030 vtn_local_store(b, src, ret_deref, 0);
1031 }
1032
1033 static void
1034 vtn_emit_cf_list_structured(struct vtn_builder *b, struct list_head *cf_list,
1035 nir_variable *switch_fall_var,
1036 bool *has_switch_break,
1037 vtn_instruction_handler handler)
1038 {
1039 vtn_foreach_cf_node(node, cf_list) {
1040 switch (node->type) {
1041 case vtn_cf_node_type_block: {
1042 struct vtn_block *block = vtn_cf_node_as_block(node);
1043
1044 const uint32_t *block_start = block->label;
1045 const uint32_t *block_end = block->merge ? block->merge :
1046 block->branch;
1047
1048 block_start = vtn_foreach_instruction(b, block_start, block_end,
1049 vtn_handle_phis_first_pass);
1050
1051 vtn_foreach_instruction(b, block_start, block_end, handler);
1052
1053 block->end_nop = nir_intrinsic_instr_create(b->nb.shader,
1054 nir_intrinsic_nop);
1055 nir_builder_instr_insert(&b->nb, &block->end_nop->instr);
1056
1057 vtn_emit_ret_store(b, block);
1058
1059 if (block->branch_type != vtn_branch_type_none) {
1060 vtn_emit_branch(b, block->branch_type,
1061 switch_fall_var, has_switch_break);
1062 return;
1063 }
1064
1065 break;
1066 }
1067
1068 case vtn_cf_node_type_if: {
1069 struct vtn_if *vtn_if = vtn_cf_node_as_if(node);
1070 bool sw_break = false;
1071
1072 nir_if *nif =
1073 nir_push_if(&b->nb, vtn_get_nir_ssa(b, vtn_if->condition));
1074
1075 nif->control = vtn_selection_control(b, vtn_if);
1076
1077 if (vtn_if->then_type == vtn_branch_type_none) {
1078 vtn_emit_cf_list_structured(b, &vtn_if->then_body,
1079 switch_fall_var, &sw_break, handler);
1080 } else {
1081 vtn_emit_branch(b, vtn_if->then_type, switch_fall_var, &sw_break);
1082 }
1083
1084 nir_push_else(&b->nb, nif);
1085 if (vtn_if->else_type == vtn_branch_type_none) {
1086 vtn_emit_cf_list_structured(b, &vtn_if->else_body,
1087 switch_fall_var, &sw_break, handler);
1088 } else {
1089 vtn_emit_branch(b, vtn_if->else_type, switch_fall_var, &sw_break);
1090 }
1091
1092 nir_pop_if(&b->nb, nif);
1093
1094 /* If we encountered a switch break somewhere inside of the if,
1095 * then it would have been handled correctly by calling
1096 * emit_cf_list or emit_branch for the interrior. However, we
1097 * need to predicate everything following on wether or not we're
1098 * still going.
1099 */
1100 if (sw_break) {
1101 *has_switch_break = true;
1102 nir_push_if(&b->nb, nir_load_var(&b->nb, switch_fall_var));
1103 }
1104 break;
1105 }
1106
1107 case vtn_cf_node_type_loop: {
1108 struct vtn_loop *vtn_loop = vtn_cf_node_as_loop(node);
1109
1110 nir_loop *loop = nir_push_loop(&b->nb);
1111 loop->control = vtn_loop_control(b, vtn_loop);
1112
1113 vtn_emit_cf_list_structured(b, &vtn_loop->body, NULL, NULL, handler);
1114
1115 if (!list_is_empty(&vtn_loop->cont_body)) {
1116 /* If we have a non-trivial continue body then we need to put
1117 * it at the beginning of the loop with a flag to ensure that
1118 * it doesn't get executed in the first iteration.
1119 */
1120 nir_variable *do_cont =
1121 nir_local_variable_create(b->nb.impl, glsl_bool_type(), "cont");
1122
1123 b->nb.cursor = nir_before_cf_node(&loop->cf_node);
1124 nir_store_var(&b->nb, do_cont, nir_imm_false(&b->nb), 1);
1125
1126 b->nb.cursor = nir_before_cf_list(&loop->body);
1127
1128 nir_if *cont_if =
1129 nir_push_if(&b->nb, nir_load_var(&b->nb, do_cont));
1130
1131 vtn_emit_cf_list_structured(b, &vtn_loop->cont_body, NULL, NULL,
1132 handler);
1133
1134 nir_pop_if(&b->nb, cont_if);
1135
1136 nir_store_var(&b->nb, do_cont, nir_imm_true(&b->nb), 1);
1137
1138 b->has_loop_continue = true;
1139 }
1140
1141 nir_pop_loop(&b->nb, loop);
1142 break;
1143 }
1144
1145 case vtn_cf_node_type_switch: {
1146 struct vtn_switch *vtn_switch = vtn_cf_node_as_switch(node);
1147
1148 /* Before we can emit anything, we need to sort the list of cases in
1149 * fall-through order.
1150 */
1151 vtn_switch_order_cases(vtn_switch);
1152
1153 /* First, we create a variable to keep track of whether or not the
1154 * switch is still going at any given point. Any switch breaks
1155 * will set this variable to false.
1156 */
1157 nir_variable *fall_var =
1158 nir_local_variable_create(b->nb.impl, glsl_bool_type(), "fall");
1159 nir_store_var(&b->nb, fall_var, nir_imm_false(&b->nb), 1);
1160
1161 nir_ssa_def *sel = vtn_get_nir_ssa(b, vtn_switch->selector);
1162
1163 /* Now we can walk the list of cases and actually emit code */
1164 vtn_foreach_cf_node(case_node, &vtn_switch->cases) {
1165 struct vtn_case *cse = vtn_cf_node_as_case(case_node);
1166
1167 /* Figure out the condition */
1168 nir_ssa_def *cond =
1169 vtn_switch_case_condition(b, vtn_switch, sel, cse);
1170 /* Take fallthrough into account */
1171 cond = nir_ior(&b->nb, cond, nir_load_var(&b->nb, fall_var));
1172
1173 nir_if *case_if = nir_push_if(&b->nb, cond);
1174
1175 bool has_break = false;
1176 nir_store_var(&b->nb, fall_var, nir_imm_true(&b->nb), 1);
1177 vtn_emit_cf_list_structured(b, &cse->body, fall_var, &has_break,
1178 handler);
1179 (void)has_break; /* We don't care */
1180
1181 nir_pop_if(&b->nb, case_if);
1182 }
1183
1184 break;
1185 }
1186
1187 default:
1188 vtn_fail("Invalid CF node type");
1189 }
1190 }
1191 }
1192
1193 static struct nir_block *
1194 vtn_new_unstructured_block(struct vtn_builder *b, struct vtn_function *func)
1195 {
1196 struct nir_block *n = nir_block_create(b->shader);
1197 exec_list_push_tail(&func->impl->body, &n->cf_node.node);
1198 n->cf_node.parent = &func->impl->cf_node;
1199 return n;
1200 }
1201
1202 static void
1203 vtn_add_unstructured_block(struct vtn_builder *b,
1204 struct vtn_function *func,
1205 struct list_head *work_list,
1206 struct vtn_block *block)
1207 {
1208 if (!block->block) {
1209 block->block = vtn_new_unstructured_block(b, func);
1210 list_addtail(&block->node.link, work_list);
1211 }
1212 }
1213
1214 static void
1215 vtn_emit_cf_func_unstructured(struct vtn_builder *b, struct vtn_function *func,
1216 vtn_instruction_handler handler)
1217 {
1218 struct list_head work_list;
1219 list_inithead(&work_list);
1220
1221 func->start_block->block = nir_start_block(func->impl);
1222 list_addtail(&func->start_block->node.link, &work_list);
1223 while (!list_is_empty(&work_list)) {
1224 struct vtn_block *block =
1225 list_first_entry(&work_list, struct vtn_block, node.link);
1226 list_del(&block->node.link);
1227
1228 vtn_assert(block->block);
1229
1230 const uint32_t *block_start = block->label;
1231 const uint32_t *block_end = block->branch;
1232
1233 b->nb.cursor = nir_after_block(block->block);
1234 block_start = vtn_foreach_instruction(b, block_start, block_end,
1235 vtn_handle_phis_first_pass);
1236 vtn_foreach_instruction(b, block_start, block_end, handler);
1237 block->end_nop = nir_intrinsic_instr_create(b->nb.shader,
1238 nir_intrinsic_nop);
1239 nir_builder_instr_insert(&b->nb, &block->end_nop->instr);
1240
1241 SpvOp op = *block_end & SpvOpCodeMask;
1242 switch (op) {
1243 case SpvOpBranch: {
1244 struct vtn_block *branch_block = vtn_block(b, block->branch[1]);
1245 vtn_add_unstructured_block(b, func, &work_list, branch_block);
1246 nir_goto(&b->nb, branch_block->block);
1247 break;
1248 }
1249
1250 case SpvOpBranchConditional: {
1251 nir_ssa_def *cond = vtn_ssa_value(b, block->branch[1])->def;
1252 struct vtn_block *then_block = vtn_block(b, block->branch[2]);
1253 struct vtn_block *else_block = vtn_block(b, block->branch[3]);
1254
1255 vtn_add_unstructured_block(b, func, &work_list, then_block);
1256 if (then_block == else_block) {
1257 nir_goto(&b->nb, then_block->block);
1258 } else {
1259 vtn_add_unstructured_block(b, func, &work_list, else_block);
1260 nir_goto_if(&b->nb, then_block->block, nir_src_for_ssa(cond),
1261 else_block->block);
1262 }
1263
1264 break;
1265 }
1266
1267 case SpvOpSwitch: {
1268 struct list_head cases;
1269 list_inithead(&cases);
1270 vtn_parse_switch(b, NULL, block->branch, &cases);
1271
1272 nir_ssa_def *sel = vtn_get_nir_ssa(b, block->branch[1]);
1273
1274 struct vtn_case *def = NULL;
1275 vtn_foreach_cf_node(case_node, &cases) {
1276 struct vtn_case *cse = vtn_cf_node_as_case(case_node);
1277 if (cse->is_default) {
1278 assert(def == NULL);
1279 def = cse;
1280 continue;
1281 }
1282
1283 nir_ssa_def *cond = nir_imm_false(&b->nb);
1284 util_dynarray_foreach(&cse->values, uint64_t, val) {
1285 nir_ssa_def *imm = nir_imm_intN_t(&b->nb, *val, sel->bit_size);
1286 cond = nir_ior(&b->nb, cond, nir_ieq(&b->nb, sel, imm));
1287 }
1288
1289 /* block for the next check */
1290 nir_block *e = vtn_new_unstructured_block(b, func);
1291 vtn_add_unstructured_block(b, func, &work_list, cse->block);
1292
1293 /* add branching */
1294 nir_goto_if(&b->nb, cse->block->block, nir_src_for_ssa(cond), e);
1295 b->nb.cursor = nir_after_block(e);
1296 }
1297
1298 vtn_assert(def != NULL);
1299 vtn_add_unstructured_block(b, func, &work_list, def->block);
1300
1301 /* now that all cases are handled, branch into the default block */
1302 nir_goto(&b->nb, def->block->block);
1303 break;
1304 }
1305
1306 case SpvOpKill: {
1307 nir_intrinsic_instr *discard =
1308 nir_intrinsic_instr_create(b->nb.shader, nir_intrinsic_discard);
1309 nir_builder_instr_insert(&b->nb, &discard->instr);
1310 nir_goto(&b->nb, b->func->impl->end_block);
1311 break;
1312 }
1313
1314 case SpvOpUnreachable:
1315 case SpvOpReturn:
1316 case SpvOpReturnValue: {
1317 vtn_emit_ret_store(b, block);
1318 nir_goto(&b->nb, b->func->impl->end_block);
1319 break;
1320 }
1321
1322 default:
1323 vtn_fail("Unhandled opcode %s", spirv_op_to_string(op));
1324 }
1325 }
1326 }
1327
1328 void
1329 vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
1330 vtn_instruction_handler instruction_handler)
1331 {
1332 static int force_unstructured = -1;
1333 if (force_unstructured < 0) {
1334 force_unstructured =
1335 env_var_as_boolean("MESA_SPIRV_FORCE_UNSTRUCTURED", false);
1336 }
1337
1338 nir_builder_init(&b->nb, func->impl);
1339 b->func = func;
1340 b->nb.cursor = nir_after_cf_list(&func->impl->body);
1341 b->nb.exact = b->exact;
1342 b->has_loop_continue = false;
1343 b->phi_table = _mesa_pointer_hash_table_create(b);
1344
1345 if (b->shader->info.stage == MESA_SHADER_KERNEL || force_unstructured) {
1346 b->func->impl->structured = false;
1347 vtn_emit_cf_func_unstructured(b, func, instruction_handler);
1348 } else {
1349 vtn_emit_cf_list_structured(b, &func->body, NULL, NULL,
1350 instruction_handler);
1351 }
1352
1353 vtn_foreach_instruction(b, func->start_block->label, func->end,
1354 vtn_handle_phi_second_pass);
1355
1356 nir_rematerialize_derefs_in_use_blocks_impl(func->impl);
1357
1358 /* Continue blocks for loops get inserted before the body of the loop
1359 * but instructions in the continue may use SSA defs in the loop body.
1360 * Therefore, we need to repair SSA to insert the needed phi nodes.
1361 */
1362 if (b->has_loop_continue)
1363 nir_repair_ssa_impl(func->impl);
1364
1365 func->emitted = true;
1366 }