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