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