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