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