b03db904cc832e0333e68a7ed97b88b4de1ac4e7
[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 "nir/nir_vla.h"
26
27 static bool
28 vtn_cfg_handle_prepass_instruction(struct vtn_builder *b, SpvOp opcode,
29 const uint32_t *w, unsigned count)
30 {
31 switch (opcode) {
32 case SpvOpFunction: {
33 assert(b->func == NULL);
34 b->func = rzalloc(b, struct vtn_function);
35
36 list_inithead(&b->func->body);
37 b->func->control = w[3];
38
39 MAYBE_UNUSED const struct glsl_type *result_type =
40 vtn_value(b, w[1], vtn_value_type_type)->type->type;
41 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_function);
42 val->func = b->func;
43
44 const struct glsl_type *func_type =
45 vtn_value(b, w[4], vtn_value_type_type)->type->type;
46
47 assert(glsl_get_function_return_type(func_type) == result_type);
48
49 nir_function *func =
50 nir_function_create(b->shader, ralloc_strdup(b->shader, val->name));
51
52 func->num_params = glsl_get_length(func_type);
53 func->params = ralloc_array(b->shader, nir_parameter, func->num_params);
54 for (unsigned i = 0; i < func->num_params; i++) {
55 const struct glsl_function_param *param =
56 glsl_get_function_param(func_type, i);
57 func->params[i].type = param->type;
58 if (param->in) {
59 if (param->out) {
60 func->params[i].param_type = nir_parameter_inout;
61 } else {
62 func->params[i].param_type = nir_parameter_in;
63 }
64 } else {
65 if (param->out) {
66 func->params[i].param_type = nir_parameter_out;
67 } else {
68 assert(!"Parameter is neither in nor out");
69 }
70 }
71 }
72
73 func->return_type = glsl_get_function_return_type(func_type);
74
75 b->func->impl = nir_function_impl_create(func);
76
77 b->func_param_idx = 0;
78 break;
79 }
80
81 case SpvOpFunctionEnd:
82 b->func->end = w;
83 b->func = NULL;
84 break;
85
86 case SpvOpFunctionParameter: {
87 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
88
89 assert(b->func_param_idx < b->func->impl->num_params);
90 nir_variable *param = b->func->impl->params[b->func_param_idx++];
91
92 assert(param->type == type->type);
93
94 struct vtn_variable *vtn_var = rzalloc(b, struct vtn_variable);
95 vtn_var->type = type;
96 vtn_var->var = param;
97
98 struct vtn_type *without_array = type;
99 while(glsl_type_is_array(without_array->type))
100 without_array = without_array->array_element;
101
102 if (glsl_type_is_image(without_array->type)) {
103 vtn_var->mode = vtn_variable_mode_image;
104 param->interface_type = without_array->type;
105 } else if (glsl_type_is_sampler(without_array->type)) {
106 vtn_var->mode = vtn_variable_mode_sampler;
107 param->interface_type = without_array->type;
108 } else {
109 vtn_var->mode = vtn_variable_mode_param;
110 }
111
112 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
113
114 /* Name the parameter so it shows up nicely in NIR */
115 param->name = ralloc_strdup(param, val->name);
116
117 val->pointer = vtn_pointer_for_variable(b, vtn_var);
118 break;
119 }
120
121 case SpvOpLabel: {
122 assert(b->block == NULL);
123 b->block = rzalloc(b, struct vtn_block);
124 b->block->node.type = vtn_cf_node_type_block;
125 b->block->label = w;
126 vtn_push_value(b, w[1], vtn_value_type_block)->block = b->block;
127
128 if (b->func->start_block == NULL) {
129 /* This is the first block encountered for this function. In this
130 * case, we set the start block and add it to the list of
131 * implemented functions that we'll walk later.
132 */
133 b->func->start_block = b->block;
134 exec_list_push_tail(&b->functions, &b->func->node);
135 }
136 break;
137 }
138
139 case SpvOpSelectionMerge:
140 case SpvOpLoopMerge:
141 assert(b->block && b->block->merge == NULL);
142 b->block->merge = w;
143 break;
144
145 case SpvOpBranch:
146 case SpvOpBranchConditional:
147 case SpvOpSwitch:
148 case SpvOpKill:
149 case SpvOpReturn:
150 case SpvOpReturnValue:
151 case SpvOpUnreachable:
152 assert(b->block && b->block->branch == NULL);
153 b->block->branch = w;
154 b->block = NULL;
155 break;
156
157 default:
158 /* Continue on as per normal */
159 return true;
160 }
161
162 return true;
163 }
164
165 static void
166 vtn_add_case(struct vtn_builder *b, struct vtn_switch *swtch,
167 struct vtn_block *break_block,
168 uint32_t block_id, uint32_t val, bool is_default)
169 {
170 struct vtn_block *case_block =
171 vtn_value(b, block_id, vtn_value_type_block)->block;
172
173 /* Don't create dummy cases that just break */
174 if (case_block == break_block)
175 return;
176
177 if (case_block->switch_case == NULL) {
178 struct vtn_case *c = ralloc(b, struct vtn_case);
179
180 list_inithead(&c->body);
181 c->start_block = case_block;
182 c->fallthrough = NULL;
183 util_dynarray_init(&c->values, b);
184 c->is_default = false;
185 c->visited = false;
186
187 list_addtail(&c->link, &swtch->cases);
188
189 case_block->switch_case = c;
190 }
191
192 if (is_default) {
193 case_block->switch_case->is_default = true;
194 } else {
195 util_dynarray_append(&case_block->switch_case->values, uint32_t, val);
196 }
197 }
198
199 /* This function performs a depth-first search of the cases and puts them
200 * in fall-through order.
201 */
202 static void
203 vtn_order_case(struct vtn_switch *swtch, struct vtn_case *cse)
204 {
205 if (cse->visited)
206 return;
207
208 cse->visited = true;
209
210 list_del(&cse->link);
211
212 if (cse->fallthrough) {
213 vtn_order_case(swtch, cse->fallthrough);
214
215 /* If we have a fall-through, place this case right before the case it
216 * falls through to. This ensures that fallthroughs come one after
217 * the other. These two can never get separated because that would
218 * imply something else falling through to the same case. Also, this
219 * can't break ordering because the DFS ensures that this case is
220 * visited before anything that falls through to it.
221 */
222 list_addtail(&cse->link, &cse->fallthrough->link);
223 } else {
224 list_add(&cse->link, &swtch->cases);
225 }
226 }
227
228 static enum vtn_branch_type
229 vtn_get_branch_type(struct vtn_block *block,
230 struct vtn_case *swcase, struct vtn_block *switch_break,
231 struct vtn_block *loop_break, struct vtn_block *loop_cont)
232 {
233 if (block->switch_case) {
234 /* This branch is actually a fallthrough */
235 assert(swcase->fallthrough == NULL ||
236 swcase->fallthrough == block->switch_case);
237 swcase->fallthrough = block->switch_case;
238 return vtn_branch_type_switch_fallthrough;
239 } else if (block == loop_break) {
240 return vtn_branch_type_loop_break;
241 } else if (block == loop_cont) {
242 return vtn_branch_type_loop_continue;
243 } else if (block == switch_break) {
244 return vtn_branch_type_switch_break;
245 } else {
246 return vtn_branch_type_none;
247 }
248 }
249
250 static void
251 vtn_cfg_walk_blocks(struct vtn_builder *b, struct list_head *cf_list,
252 struct vtn_block *start, struct vtn_case *switch_case,
253 struct vtn_block *switch_break,
254 struct vtn_block *loop_break, struct vtn_block *loop_cont,
255 struct vtn_block *end)
256 {
257 struct vtn_block *block = start;
258 while (block != end) {
259 if (block->merge && (*block->merge & SpvOpCodeMask) == SpvOpLoopMerge &&
260 !block->loop) {
261 struct vtn_loop *loop = ralloc(b, struct vtn_loop);
262
263 loop->node.type = vtn_cf_node_type_loop;
264 list_inithead(&loop->body);
265 list_inithead(&loop->cont_body);
266 loop->control = block->merge[3];
267
268 list_addtail(&loop->node.link, cf_list);
269 block->loop = loop;
270
271 struct vtn_block *new_loop_break =
272 vtn_value(b, block->merge[1], vtn_value_type_block)->block;
273 struct vtn_block *new_loop_cont =
274 vtn_value(b, block->merge[2], vtn_value_type_block)->block;
275
276 /* Note: This recursive call will start with the current block as
277 * its start block. If we weren't careful, we would get here
278 * again and end up in infinite recursion. This is why we set
279 * block->loop above and check for it before creating one. This
280 * way, we only create the loop once and the second call that
281 * tries to handle this loop goes to the cases below and gets
282 * handled as a regular block.
283 *
284 * Note: When we make the recursive walk calls, we pass NULL for
285 * the switch break since you have to break out of the loop first.
286 * We do, however, still pass the current switch case because it's
287 * possible that the merge block for the loop is the start of
288 * another case.
289 */
290 vtn_cfg_walk_blocks(b, &loop->body, block, switch_case, NULL,
291 new_loop_break, new_loop_cont, NULL );
292 vtn_cfg_walk_blocks(b, &loop->cont_body, new_loop_cont, NULL, NULL,
293 new_loop_break, NULL, block);
294
295 block = new_loop_break;
296 continue;
297 }
298
299 assert(block->node.link.next == NULL);
300 list_addtail(&block->node.link, cf_list);
301
302 switch (*block->branch & SpvOpCodeMask) {
303 case SpvOpBranch: {
304 struct vtn_block *branch_block =
305 vtn_value(b, block->branch[1], vtn_value_type_block)->block;
306
307 block->branch_type = vtn_get_branch_type(branch_block,
308 switch_case, switch_break,
309 loop_break, loop_cont);
310
311 if (block->branch_type != vtn_branch_type_none)
312 return;
313
314 block = branch_block;
315 continue;
316 }
317
318 case SpvOpReturn:
319 case SpvOpReturnValue:
320 block->branch_type = vtn_branch_type_return;
321 return;
322
323 case SpvOpKill:
324 block->branch_type = vtn_branch_type_discard;
325 return;
326
327 case SpvOpBranchConditional: {
328 struct vtn_block *then_block =
329 vtn_value(b, block->branch[2], vtn_value_type_block)->block;
330 struct vtn_block *else_block =
331 vtn_value(b, block->branch[3], vtn_value_type_block)->block;
332
333 struct vtn_if *if_stmt = ralloc(b, struct vtn_if);
334
335 if_stmt->node.type = vtn_cf_node_type_if;
336 if_stmt->condition = block->branch[1];
337 list_inithead(&if_stmt->then_body);
338 list_inithead(&if_stmt->else_body);
339
340 list_addtail(&if_stmt->node.link, cf_list);
341
342 if (block->merge &&
343 (*block->merge & SpvOpCodeMask) == SpvOpSelectionMerge) {
344 if_stmt->control = block->merge[2];
345 }
346
347 if_stmt->then_type = vtn_get_branch_type(then_block,
348 switch_case, switch_break,
349 loop_break, loop_cont);
350 if_stmt->else_type = vtn_get_branch_type(else_block,
351 switch_case, switch_break,
352 loop_break, loop_cont);
353
354 if (if_stmt->then_type == vtn_branch_type_none &&
355 if_stmt->else_type == vtn_branch_type_none) {
356 /* Neither side of the if is something we can short-circuit. */
357 assert((*block->merge & SpvOpCodeMask) == SpvOpSelectionMerge);
358 struct vtn_block *merge_block =
359 vtn_value(b, block->merge[1], vtn_value_type_block)->block;
360
361 vtn_cfg_walk_blocks(b, &if_stmt->then_body, then_block,
362 switch_case, switch_break,
363 loop_break, loop_cont, merge_block);
364 vtn_cfg_walk_blocks(b, &if_stmt->else_body, else_block,
365 switch_case, switch_break,
366 loop_break, loop_cont, merge_block);
367
368 enum vtn_branch_type merge_type =
369 vtn_get_branch_type(merge_block, switch_case, switch_break,
370 loop_break, loop_cont);
371 if (merge_type == vtn_branch_type_none) {
372 block = merge_block;
373 continue;
374 } else {
375 return;
376 }
377 } else if (if_stmt->then_type != vtn_branch_type_none &&
378 if_stmt->else_type != vtn_branch_type_none) {
379 /* Both sides were short-circuited. We're done here. */
380 return;
381 } else {
382 /* Exeactly one side of the branch could be short-circuited.
383 * We set the branch up as a predicated break/continue and we
384 * continue on with the other side as if it were what comes
385 * after the if.
386 */
387 if (if_stmt->then_type == vtn_branch_type_none) {
388 block = then_block;
389 } else {
390 block = else_block;
391 }
392 continue;
393 }
394 unreachable("Should have returned or continued");
395 }
396
397 case SpvOpSwitch: {
398 assert((*block->merge & SpvOpCodeMask) == SpvOpSelectionMerge);
399 struct vtn_block *break_block =
400 vtn_value(b, block->merge[1], vtn_value_type_block)->block;
401
402 struct vtn_switch *swtch = ralloc(b, struct vtn_switch);
403
404 swtch->node.type = vtn_cf_node_type_switch;
405 swtch->selector = block->branch[1];
406 list_inithead(&swtch->cases);
407
408 list_addtail(&swtch->node.link, cf_list);
409
410 /* First, we go through and record all of the cases. */
411 const uint32_t *branch_end =
412 block->branch + (block->branch[0] >> SpvWordCountShift);
413
414 vtn_add_case(b, swtch, break_block, block->branch[2], 0, true);
415 for (const uint32_t *w = block->branch + 3; w < branch_end; w += 2)
416 vtn_add_case(b, swtch, break_block, w[1], w[0], false);
417
418 /* Now, we go through and walk the blocks. While we walk through
419 * the blocks, we also gather the much-needed fall-through
420 * information.
421 */
422 list_for_each_entry(struct vtn_case, cse, &swtch->cases, link) {
423 assert(cse->start_block != break_block);
424 vtn_cfg_walk_blocks(b, &cse->body, cse->start_block, cse,
425 break_block, NULL, loop_cont, NULL);
426 }
427
428 /* Finally, we walk over all of the cases one more time and put
429 * them in fall-through order.
430 */
431 for (const uint32_t *w = block->branch + 2; w < branch_end; w += 2) {
432 struct vtn_block *case_block =
433 vtn_value(b, *w, vtn_value_type_block)->block;
434
435 if (case_block == break_block)
436 continue;
437
438 assert(case_block->switch_case);
439
440 vtn_order_case(swtch, case_block->switch_case);
441 }
442
443 enum vtn_branch_type branch_type =
444 vtn_get_branch_type(break_block, switch_case, NULL,
445 loop_break, loop_cont);
446
447 if (branch_type != vtn_branch_type_none) {
448 /* It is possible that the break is actually the continue block
449 * for the containing loop. In this case, we need to bail and let
450 * the loop parsing code handle the continue properly.
451 */
452 assert(branch_type == vtn_branch_type_loop_continue);
453 return;
454 }
455
456 block = break_block;
457 continue;
458 }
459
460 case SpvOpUnreachable:
461 return;
462
463 default:
464 unreachable("Unhandled opcode");
465 }
466 }
467 }
468
469 void
470 vtn_build_cfg(struct vtn_builder *b, const uint32_t *words, const uint32_t *end)
471 {
472 vtn_foreach_instruction(b, words, end,
473 vtn_cfg_handle_prepass_instruction);
474
475 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
476 vtn_cfg_walk_blocks(b, &func->body, func->start_block,
477 NULL, NULL, NULL, NULL, NULL);
478 }
479 }
480
481 static bool
482 vtn_handle_phis_first_pass(struct vtn_builder *b, SpvOp opcode,
483 const uint32_t *w, unsigned count)
484 {
485 if (opcode == SpvOpLabel)
486 return true; /* Nothing to do */
487
488 /* If this isn't a phi node, stop. */
489 if (opcode != SpvOpPhi)
490 return false;
491
492 /* For handling phi nodes, we do a poor-man's out-of-ssa on the spot.
493 * For each phi, we create a variable with the appropreate type and
494 * do a load from that variable. Then, in a second pass, we add
495 * stores to that variable to each of the predecessor blocks.
496 *
497 * We could do something more intelligent here. However, in order to
498 * handle loops and things properly, we really need dominance
499 * information. It would end up basically being the into-SSA
500 * algorithm all over again. It's easier if we just let
501 * lower_vars_to_ssa do that for us instead of repeating it here.
502 */
503 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
504
505 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
506 nir_variable *phi_var =
507 nir_local_variable_create(b->nb.impl, type->type, "phi");
508 _mesa_hash_table_insert(b->phi_table, w, phi_var);
509
510 val->ssa = vtn_local_load(b, nir_deref_var_create(b, phi_var));
511
512 return true;
513 }
514
515 static bool
516 vtn_handle_phi_second_pass(struct vtn_builder *b, SpvOp opcode,
517 const uint32_t *w, unsigned count)
518 {
519 if (opcode != SpvOpPhi)
520 return true;
521
522 struct hash_entry *phi_entry = _mesa_hash_table_search(b->phi_table, w);
523 assert(phi_entry);
524 nir_variable *phi_var = phi_entry->data;
525
526 for (unsigned i = 3; i < count; i += 2) {
527 struct vtn_block *pred =
528 vtn_value(b, w[i + 1], vtn_value_type_block)->block;
529
530 b->nb.cursor = nir_after_instr(&pred->end_nop->instr);
531
532 struct vtn_ssa_value *src = vtn_ssa_value(b, w[i]);
533
534 vtn_local_store(b, src, nir_deref_var_create(b, phi_var));
535 }
536
537 return true;
538 }
539
540 static void
541 vtn_emit_branch(struct vtn_builder *b, enum vtn_branch_type branch_type,
542 nir_variable *switch_fall_var, bool *has_switch_break)
543 {
544 switch (branch_type) {
545 case vtn_branch_type_switch_break:
546 nir_store_var(&b->nb, switch_fall_var, nir_imm_int(&b->nb, NIR_FALSE), 1);
547 *has_switch_break = true;
548 break;
549 case vtn_branch_type_switch_fallthrough:
550 break; /* Nothing to do */
551 case vtn_branch_type_loop_break:
552 nir_jump(&b->nb, nir_jump_break);
553 break;
554 case vtn_branch_type_loop_continue:
555 nir_jump(&b->nb, nir_jump_continue);
556 break;
557 case vtn_branch_type_return:
558 nir_jump(&b->nb, nir_jump_return);
559 break;
560 case vtn_branch_type_discard: {
561 nir_intrinsic_instr *discard =
562 nir_intrinsic_instr_create(b->nb.shader, nir_intrinsic_discard);
563 nir_builder_instr_insert(&b->nb, &discard->instr);
564 break;
565 }
566 default:
567 unreachable("Invalid branch type");
568 }
569 }
570
571 static void
572 vtn_emit_cf_list(struct vtn_builder *b, struct list_head *cf_list,
573 nir_variable *switch_fall_var, bool *has_switch_break,
574 vtn_instruction_handler handler)
575 {
576 list_for_each_entry(struct vtn_cf_node, node, cf_list, link) {
577 switch (node->type) {
578 case vtn_cf_node_type_block: {
579 struct vtn_block *block = (struct vtn_block *)node;
580
581 const uint32_t *block_start = block->label;
582 const uint32_t *block_end = block->merge ? block->merge :
583 block->branch;
584
585 block_start = vtn_foreach_instruction(b, block_start, block_end,
586 vtn_handle_phis_first_pass);
587
588 vtn_foreach_instruction(b, block_start, block_end, handler);
589
590 block->end_nop = nir_intrinsic_instr_create(b->nb.shader,
591 nir_intrinsic_nop);
592 nir_builder_instr_insert(&b->nb, &block->end_nop->instr);
593
594 if ((*block->branch & SpvOpCodeMask) == SpvOpReturnValue) {
595 struct vtn_ssa_value *src = vtn_ssa_value(b, block->branch[1]);
596 vtn_local_store(b, src,
597 nir_deref_var_create(b, b->impl->return_var));
598 }
599
600 if (block->branch_type != vtn_branch_type_none) {
601 vtn_emit_branch(b, block->branch_type,
602 switch_fall_var, has_switch_break);
603 }
604
605 break;
606 }
607
608 case vtn_cf_node_type_if: {
609 struct vtn_if *vtn_if = (struct vtn_if *)node;
610 bool sw_break = false;
611
612 nir_if *nif =
613 nir_push_if(&b->nb, vtn_ssa_value(b, vtn_if->condition)->def);
614 if (vtn_if->then_type == vtn_branch_type_none) {
615 vtn_emit_cf_list(b, &vtn_if->then_body,
616 switch_fall_var, &sw_break, handler);
617 } else {
618 vtn_emit_branch(b, vtn_if->then_type, switch_fall_var, &sw_break);
619 }
620
621 nir_push_else(&b->nb, nif);
622 if (vtn_if->else_type == vtn_branch_type_none) {
623 vtn_emit_cf_list(b, &vtn_if->else_body,
624 switch_fall_var, &sw_break, handler);
625 } else {
626 vtn_emit_branch(b, vtn_if->else_type, switch_fall_var, &sw_break);
627 }
628
629 nir_pop_if(&b->nb, nif);
630
631 /* If we encountered a switch break somewhere inside of the if,
632 * then it would have been handled correctly by calling
633 * emit_cf_list or emit_branch for the interrior. However, we
634 * need to predicate everything following on wether or not we're
635 * still going.
636 */
637 if (sw_break) {
638 *has_switch_break = true;
639 nir_push_if(&b->nb, nir_load_var(&b->nb, switch_fall_var));
640 }
641 break;
642 }
643
644 case vtn_cf_node_type_loop: {
645 struct vtn_loop *vtn_loop = (struct vtn_loop *)node;
646
647 nir_loop *loop = nir_push_loop(&b->nb);
648 vtn_emit_cf_list(b, &vtn_loop->body, NULL, NULL, handler);
649
650 if (!list_empty(&vtn_loop->cont_body)) {
651 /* If we have a non-trivial continue body then we need to put
652 * it at the beginning of the loop with a flag to ensure that
653 * it doesn't get executed in the first iteration.
654 */
655 nir_variable *do_cont =
656 nir_local_variable_create(b->nb.impl, glsl_bool_type(), "cont");
657
658 b->nb.cursor = nir_before_cf_node(&loop->cf_node);
659 nir_store_var(&b->nb, do_cont, nir_imm_int(&b->nb, NIR_FALSE), 1);
660
661 b->nb.cursor = nir_before_cf_list(&loop->body);
662
663 nir_if *cont_if =
664 nir_push_if(&b->nb, nir_load_var(&b->nb, do_cont));
665
666 vtn_emit_cf_list(b, &vtn_loop->cont_body, NULL, NULL, handler);
667
668 nir_pop_if(&b->nb, cont_if);
669
670 nir_store_var(&b->nb, do_cont, nir_imm_int(&b->nb, NIR_TRUE), 1);
671
672 b->has_loop_continue = true;
673 }
674
675 nir_pop_loop(&b->nb, loop);
676 break;
677 }
678
679 case vtn_cf_node_type_switch: {
680 struct vtn_switch *vtn_switch = (struct vtn_switch *)node;
681
682 /* First, we create a variable to keep track of whether or not the
683 * switch is still going at any given point. Any switch breaks
684 * will set this variable to false.
685 */
686 nir_variable *fall_var =
687 nir_local_variable_create(b->nb.impl, glsl_bool_type(), "fall");
688 nir_store_var(&b->nb, fall_var, nir_imm_int(&b->nb, NIR_FALSE), 1);
689
690 /* Next, we gather up all of the conditions. We have to do this
691 * up-front because we also need to build an "any" condition so
692 * that we can use !any for default.
693 */
694 const int num_cases = list_length(&vtn_switch->cases);
695 NIR_VLA(nir_ssa_def *, conditions, num_cases);
696
697 nir_ssa_def *sel = vtn_ssa_value(b, vtn_switch->selector)->def;
698 /* An accumulation of all conditions. Used for the default */
699 nir_ssa_def *any = NULL;
700
701 int i = 0;
702 list_for_each_entry(struct vtn_case, cse, &vtn_switch->cases, link) {
703 if (cse->is_default) {
704 conditions[i++] = NULL;
705 continue;
706 }
707
708 nir_ssa_def *cond = NULL;
709 util_dynarray_foreach(&cse->values, uint32_t, val) {
710 nir_ssa_def *is_val =
711 nir_ieq(&b->nb, sel, nir_imm_int(&b->nb, *val));
712
713 cond = cond ? nir_ior(&b->nb, cond, is_val) : is_val;
714 }
715
716 any = any ? nir_ior(&b->nb, any, cond) : cond;
717 conditions[i++] = cond;
718 }
719 assert(i == num_cases);
720
721 /* Now we can walk the list of cases and actually emit code */
722 i = 0;
723 list_for_each_entry(struct vtn_case, cse, &vtn_switch->cases, link) {
724 /* Figure out the condition */
725 nir_ssa_def *cond = conditions[i++];
726 if (cse->is_default) {
727 assert(cond == NULL);
728 cond = nir_inot(&b->nb, any);
729 }
730 /* Take fallthrough into account */
731 cond = nir_ior(&b->nb, cond, nir_load_var(&b->nb, fall_var));
732
733 nir_if *case_if = nir_push_if(&b->nb, cond);
734
735 bool has_break = false;
736 nir_store_var(&b->nb, fall_var, nir_imm_int(&b->nb, NIR_TRUE), 1);
737 vtn_emit_cf_list(b, &cse->body, fall_var, &has_break, handler);
738 (void)has_break; /* We don't care */
739
740 nir_pop_if(&b->nb, case_if);
741 }
742 assert(i == num_cases);
743
744 break;
745 }
746
747 default:
748 unreachable("Invalid CF node type");
749 }
750 }
751 }
752
753 void
754 vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
755 vtn_instruction_handler instruction_handler)
756 {
757 nir_builder_init(&b->nb, func->impl);
758 b->nb.cursor = nir_after_cf_list(&func->impl->body);
759 b->has_loop_continue = false;
760 b->phi_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
761 _mesa_key_pointer_equal);
762
763 vtn_emit_cf_list(b, &func->body, NULL, NULL, instruction_handler);
764
765 vtn_foreach_instruction(b, func->start_block->label, func->end,
766 vtn_handle_phi_second_pass);
767
768 /* Continue blocks for loops get inserted before the body of the loop
769 * but instructions in the continue may use SSA defs in the loop body.
770 * Therefore, we need to repair SSA to insert the needed phi nodes.
771 */
772 if (b->has_loop_continue)
773 nir_repair_ssa_impl(func->impl);
774 }