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