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