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