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