72922464633657813cb0913a8953849025ea3762
[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_function *vtn_callee =
189 vtn_value(b, w[3], vtn_value_type_function)->func;
190 struct nir_function *callee = vtn_callee->impl->function;
191
192 vtn_callee->referenced = true;
193
194 nir_call_instr *call = nir_call_instr_create(b->nb.shader, callee);
195
196 unsigned param_idx = 0;
197
198 nir_deref_instr *ret_deref = NULL;
199 struct vtn_type *ret_type = vtn_callee->type->return_type;
200 if (ret_type->base_type != vtn_base_type_void) {
201 nir_variable *ret_tmp =
202 nir_local_variable_create(b->nb.impl,
203 glsl_get_bare_type(ret_type->type),
204 "return_tmp");
205 ret_deref = nir_build_deref_var(&b->nb, ret_tmp);
206 call->params[param_idx++] = nir_src_for_ssa(&ret_deref->dest.ssa);
207 }
208
209 for (unsigned i = 0; i < vtn_callee->type->length; i++) {
210 struct vtn_type *arg_type = vtn_callee->type->params[i];
211 unsigned arg_id = w[4 + i];
212
213 if (arg_type->base_type == vtn_base_type_sampled_image) {
214 struct vtn_sampled_image *sampled_image =
215 vtn_value(b, arg_id, vtn_value_type_sampled_image)->sampled_image;
216
217 call->params[param_idx++] =
218 nir_src_for_ssa(vtn_pointer_to_ssa(b, sampled_image->image));
219 call->params[param_idx++] =
220 nir_src_for_ssa(vtn_pointer_to_ssa(b, sampled_image->sampler));
221 } else if (arg_type->base_type == vtn_base_type_pointer ||
222 arg_type->base_type == vtn_base_type_image ||
223 arg_type->base_type == vtn_base_type_sampler) {
224 struct vtn_pointer *pointer =
225 vtn_value(b, arg_id, vtn_value_type_pointer)->pointer;
226 call->params[param_idx++] =
227 nir_src_for_ssa(vtn_pointer_to_ssa(b, pointer));
228 } else {
229 vtn_ssa_value_add_to_call_params(b, vtn_ssa_value(b, arg_id),
230 arg_type, call, &param_idx);
231 }
232 }
233 assert(param_idx == call->num_params);
234
235 nir_builder_instr_insert(&b->nb, &call->instr);
236
237 if (ret_type->base_type == vtn_base_type_void) {
238 vtn_push_value(b, w[2], vtn_value_type_undef);
239 } else {
240 vtn_push_ssa_value(b, w[2], vtn_local_load(b, ret_deref, 0));
241 }
242 }
243
244 static bool
245 vtn_cfg_handle_prepass_instruction(struct vtn_builder *b, SpvOp opcode,
246 const uint32_t *w, unsigned count)
247 {
248 switch (opcode) {
249 case SpvOpFunction: {
250 vtn_assert(b->func == NULL);
251 b->func = rzalloc(b, struct vtn_function);
252
253 b->func->node.type = vtn_cf_node_type_function;
254 b->func->node.parent = NULL;
255 list_inithead(&b->func->body);
256 b->func->control = w[3];
257
258 UNUSED const struct glsl_type *result_type = vtn_get_type(b, w[1])->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_get_type(b, w[4]);
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_get_type(b, w[1]);
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 *image_type = rzalloc(b, struct vtn_type);
329 image_type->base_type = vtn_base_type_image;
330 image_type->type = type->type;
331
332 struct vtn_type *sampler_type = rzalloc(b, struct vtn_type);
333 sampler_type->base_type = vtn_base_type_sampler;
334 sampler_type->type = glsl_bare_sampler_type();
335
336 val->sampled_image->image =
337 vtn_load_param_pointer(b, image_type, b->func_param_idx++);
338 val->sampled_image->sampler =
339 vtn_load_param_pointer(b, sampler_type, b->func_param_idx++);
340 } else if (type->base_type == vtn_base_type_pointer &&
341 type->type != NULL) {
342 /* This is a pointer with an actual storage type */
343 nir_ssa_def *ssa_ptr = nir_load_param(&b->nb, b->func_param_idx++);
344 vtn_push_pointer(b, w[2], vtn_pointer_from_ssa(b, ssa_ptr, type));
345 } else if (type->base_type == vtn_base_type_pointer ||
346 type->base_type == vtn_base_type_image ||
347 type->base_type == vtn_base_type_sampler) {
348 vtn_push_pointer(b, w[2], vtn_load_param_pointer(b, type, b->func_param_idx++));
349 } else {
350 /* We're a regular SSA value. */
351 struct vtn_ssa_value *value = vtn_create_ssa_value(b, type->type);
352 vtn_ssa_value_load_function_param(b, value, type, &b->func_param_idx);
353 vtn_push_ssa_value(b, w[2], value);
354 }
355 break;
356 }
357
358 case SpvOpLabel: {
359 vtn_assert(b->block == NULL);
360 b->block = rzalloc(b, struct vtn_block);
361 b->block->node.type = vtn_cf_node_type_block;
362 b->block->label = w;
363 vtn_push_value(b, w[1], vtn_value_type_block)->block = b->block;
364
365 if (b->func->start_block == NULL) {
366 /* This is the first block encountered for this function. In this
367 * case, we set the start block and add it to the list of
368 * implemented functions that we'll walk later.
369 */
370 b->func->start_block = b->block;
371 list_addtail(&b->func->node.link, &b->functions);
372 }
373 break;
374 }
375
376 case SpvOpSelectionMerge:
377 case SpvOpLoopMerge:
378 vtn_assert(b->block && b->block->merge == NULL);
379 b->block->merge = w;
380 break;
381
382 case SpvOpBranch:
383 case SpvOpBranchConditional:
384 case SpvOpSwitch:
385 case SpvOpKill:
386 case SpvOpReturn:
387 case SpvOpReturnValue:
388 case SpvOpUnreachable:
389 vtn_assert(b->block && b->block->branch == NULL);
390 b->block->branch = w;
391 b->block = NULL;
392 break;
393
394 default:
395 /* Continue on as per normal */
396 return true;
397 }
398
399 return true;
400 }
401
402 /* This function performs a depth-first search of the cases and puts them
403 * in fall-through order.
404 */
405 static void
406 vtn_order_case(struct vtn_switch *swtch, struct vtn_case *cse)
407 {
408 if (cse->visited)
409 return;
410
411 cse->visited = true;
412
413 list_del(&cse->node.link);
414
415 if (cse->fallthrough) {
416 vtn_order_case(swtch, cse->fallthrough);
417
418 /* If we have a fall-through, place this case right before the case it
419 * falls through to. This ensures that fallthroughs come one after
420 * the other. These two can never get separated because that would
421 * imply something else falling through to the same case. Also, this
422 * can't break ordering because the DFS ensures that this case is
423 * visited before anything that falls through to it.
424 */
425 list_addtail(&cse->node.link, &cse->fallthrough->node.link);
426 } else {
427 list_add(&cse->node.link, &swtch->cases);
428 }
429 }
430
431 static void
432 vtn_switch_order_cases(struct vtn_switch *swtch)
433 {
434 struct list_head cases;
435 list_replace(&swtch->cases, &cases);
436 list_inithead(&swtch->cases);
437 while (!list_is_empty(&cases)) {
438 struct vtn_case *cse =
439 list_first_entry(&cases, struct vtn_case, node.link);
440 vtn_order_case(swtch, cse);
441 }
442 }
443
444 static void
445 vtn_block_set_merge_cf_node(struct vtn_builder *b, struct vtn_block *block,
446 struct vtn_cf_node *cf_node)
447 {
448 vtn_fail_if(block->merge_cf_node != NULL,
449 "The merge block declared by a header block cannot be a "
450 "merge block declared by any other header block.");
451
452 block->merge_cf_node = cf_node;
453 }
454
455 #define VTN_DECL_CF_NODE_FIND(_type) \
456 static inline struct vtn_##_type * \
457 vtn_cf_node_find_##_type(struct vtn_cf_node *node) \
458 { \
459 while (node && node->type != vtn_cf_node_type_##_type) \
460 node = node->parent; \
461 return (struct vtn_##_type *)node; \
462 }
463
464 VTN_DECL_CF_NODE_FIND(if)
465 VTN_DECL_CF_NODE_FIND(loop)
466 VTN_DECL_CF_NODE_FIND(case)
467 VTN_DECL_CF_NODE_FIND(switch)
468 VTN_DECL_CF_NODE_FIND(function)
469
470 static enum vtn_branch_type
471 vtn_handle_branch(struct vtn_builder *b,
472 struct vtn_cf_node *cf_parent,
473 struct vtn_block *target_block)
474 {
475 struct vtn_loop *loop = vtn_cf_node_find_loop(cf_parent);
476
477 /* Detect a loop back-edge first. That way none of the code below
478 * accidentally operates on a loop back-edge.
479 */
480 if (loop && target_block == loop->header_block)
481 return vtn_branch_type_loop_back_edge;
482
483 /* Try to detect fall-through */
484 if (target_block->switch_case) {
485 /* When it comes to handling switch cases, we can break calls to
486 * vtn_handle_branch into two cases: calls from within a case construct
487 * and calls for the jump to each case construct. In the second case,
488 * cf_parent is the vtn_switch itself and vtn_cf_node_find_case() will
489 * return the outer switch case in which this switch is contained. It's
490 * fine if the target block is a switch case from an outer switch as
491 * long as it is also the switch break for this switch.
492 */
493 struct vtn_case *switch_case = vtn_cf_node_find_case(cf_parent);
494
495 /* This doesn't get called for the OpSwitch */
496 vtn_fail_if(switch_case == NULL,
497 "A switch case can only be entered through an OpSwitch or "
498 "falling through from another switch case.");
499
500 /* Because block->switch_case is only set on the entry block for a given
501 * switch case, we only ever get here if we're jumping to the start of a
502 * switch case. It's possible, however, that a switch case could jump
503 * to itself via a back-edge. That *should* get caught by the loop
504 * handling case above but if we have a back edge without a loop merge,
505 * we could en up here.
506 */
507 vtn_fail_if(target_block->switch_case == switch_case,
508 "A switch cannot fall-through to itself. Likely, there is "
509 "a back-edge which is not to a loop header.");
510
511 vtn_fail_if(target_block->switch_case->node.parent !=
512 switch_case->node.parent,
513 "A switch case fall-through must come from the same "
514 "OpSwitch construct");
515
516 vtn_fail_if(switch_case->fallthrough != NULL &&
517 switch_case->fallthrough != target_block->switch_case,
518 "Each case construct can have at most one branch to "
519 "another case construct");
520
521 switch_case->fallthrough = target_block->switch_case;
522
523 /* We don't immediately return vtn_branch_type_switch_fallthrough
524 * because it may also be a loop or switch break for an inner loop or
525 * switch and that takes precedence.
526 */
527 }
528
529 if (loop && target_block == loop->cont_block)
530 return vtn_branch_type_loop_continue;
531
532 /* We walk blocks as a breadth-first search on the control-flow construct
533 * tree where, when we find a construct, we add the vtn_cf_node for that
534 * construct and continue iterating at the merge target block (if any).
535 * Therefore, we want merges whose with parent == cf_parent to be treated
536 * as regular branches. We only want to consider merges if they break out
537 * of the current CF construct.
538 */
539 if (target_block->merge_cf_node != NULL &&
540 target_block->merge_cf_node->parent != cf_parent) {
541 switch (target_block->merge_cf_node->type) {
542 case vtn_cf_node_type_if:
543 for (struct vtn_cf_node *node = cf_parent;
544 node != target_block->merge_cf_node; node = node->parent) {
545 vtn_fail_if(node == NULL || node->type != vtn_cf_node_type_if,
546 "Branching to the merge block of a selection "
547 "construct can only be used to break out of a "
548 "selection construct");
549
550 struct vtn_if *if_stmt = vtn_cf_node_as_if(node);
551
552 /* This should be guaranteed by our iteration */
553 assert(if_stmt->merge_block != target_block);
554
555 vtn_fail_if(if_stmt->merge_block != NULL,
556 "Branching to the merge block of a selection "
557 "construct can only be used to break out of the "
558 "inner most nested selection level");
559 }
560 return vtn_branch_type_if_merge;
561
562 case vtn_cf_node_type_loop:
563 vtn_fail_if(target_block->merge_cf_node != &loop->node,
564 "Loop breaks can only break out of the inner most "
565 "nested loop level");
566 return vtn_branch_type_loop_break;
567
568 case vtn_cf_node_type_switch: {
569 struct vtn_switch *swtch = vtn_cf_node_find_switch(cf_parent);
570 vtn_fail_if(target_block->merge_cf_node != &swtch->node,
571 "Switch breaks can only break out of the inner most "
572 "nested switch level");
573 return vtn_branch_type_switch_break;
574 }
575
576 default:
577 unreachable("Invalid CF node type for a merge");
578 }
579 }
580
581 if (target_block->switch_case)
582 return vtn_branch_type_switch_fallthrough;
583
584 return vtn_branch_type_none;
585 }
586
587 struct vtn_cfg_work_item {
588 struct list_head link;
589
590 struct vtn_cf_node *cf_parent;
591 struct list_head *cf_list;
592 struct vtn_block *start_block;
593 };
594
595 static void
596 vtn_add_cfg_work_item(struct vtn_builder *b,
597 struct list_head *work_list,
598 struct vtn_cf_node *cf_parent,
599 struct list_head *cf_list,
600 struct vtn_block *start_block)
601 {
602 struct vtn_cfg_work_item *work = ralloc(b, struct vtn_cfg_work_item);
603 work->cf_parent = cf_parent;
604 work->cf_list = cf_list;
605 work->start_block = start_block;
606 list_addtail(&work->link, work_list);
607 }
608
609 /* Processes a block and returns the next block to process or NULL if we've
610 * reached the end of the construct.
611 */
612 static struct vtn_block *
613 vtn_process_block(struct vtn_builder *b,
614 struct list_head *work_list,
615 struct vtn_cf_node *cf_parent,
616 struct list_head *cf_list,
617 struct vtn_block *block)
618 {
619 if (!list_is_empty(cf_list)) {
620 /* vtn_process_block() acts like an iterator: it processes the given
621 * block and then returns the next block to process. For a given
622 * control-flow construct, vtn_build_cfg() calls vtn_process_block()
623 * repeatedly until it finally returns NULL. Therefore, we know that
624 * the only blocks on which vtn_process_block() can be called are either
625 * the first block in a construct or a block that vtn_process_block()
626 * returned for the current construct. If cf_list is empty then we know
627 * that we're processing the first block in the construct and we have to
628 * add it to the list.
629 *
630 * If cf_list is not empty, then it must be the block returned by the
631 * previous call to vtn_process_block(). We know a priori that
632 * vtn_process_block only returns either normal branches
633 * (vtn_branch_type_none) or merge target blocks.
634 */
635 switch (vtn_handle_branch(b, cf_parent, block)) {
636 case vtn_branch_type_none:
637 /* For normal branches, we want to process them and add them to the
638 * current construct. Merge target blocks also look like normal
639 * branches from the perspective of this construct. See also
640 * vtn_handle_branch().
641 */
642 break;
643
644 case vtn_branch_type_loop_continue:
645 case vtn_branch_type_switch_fallthrough:
646 /* The two cases where we can get early exits from a construct that
647 * are not to that construct's merge target are loop continues and
648 * switch fall-throughs. In these cases, we need to break out of the
649 * current construct by returning NULL.
650 */
651 return NULL;
652
653 default:
654 /* The only way we can get here is if something was used as two kinds
655 * of merges at the same time and that's illegal.
656 */
657 vtn_fail("A block was used as a merge target from two or more "
658 "structured control-flow constructs");
659 }
660 }
661
662 /* Once a block has been processed, it is placed into and the list link
663 * will point to something non-null. If we see a node we've already
664 * processed here, it either exists in multiple functions or it's an
665 * invalid back-edge.
666 */
667 if (block->node.parent != NULL) {
668 vtn_fail_if(vtn_cf_node_find_function(&block->node) !=
669 vtn_cf_node_find_function(cf_parent),
670 "A block cannot exist in two functions at the "
671 "same time");
672
673 vtn_fail("Invalid back or cross-edge in the CFG");
674 }
675
676 if (block->merge && (*block->merge & SpvOpCodeMask) == SpvOpLoopMerge &&
677 block->loop == NULL) {
678 vtn_fail_if((*block->branch & SpvOpCodeMask) != SpvOpBranch &&
679 (*block->branch & SpvOpCodeMask) != SpvOpBranchConditional,
680 "An OpLoopMerge instruction must immediately precede "
681 "either an OpBranch or OpBranchConditional instruction.");
682
683 struct vtn_loop *loop = rzalloc(b, struct vtn_loop);
684
685 loop->node.type = vtn_cf_node_type_loop;
686 loop->node.parent = cf_parent;
687 list_inithead(&loop->body);
688 list_inithead(&loop->cont_body);
689 loop->header_block = block;
690 loop->break_block = vtn_block(b, block->merge[1]);
691 loop->cont_block = vtn_block(b, block->merge[2]);
692 loop->control = block->merge[3];
693
694 list_addtail(&loop->node.link, cf_list);
695 block->loop = loop;
696
697 /* Note: The work item for the main loop body will start with the
698 * current block as its start block. If we weren't careful, we would
699 * get here again and end up in an infinite loop. This is why we set
700 * block->loop above and check for it before creating one. This way,
701 * we only create the loop once and the second iteration that tries to
702 * handle this loop goes to the cases below and gets handled as a
703 * regular block.
704 */
705 vtn_add_cfg_work_item(b, work_list, &loop->node,
706 &loop->body, loop->header_block);
707
708 /* For continue targets, SPIR-V guarantees the following:
709 *
710 * - the Continue Target must dominate the back-edge block
711 * - the back-edge block must post dominate the Continue Target
712 *
713 * If the header block is the same as the continue target, this
714 * condition is trivially satisfied and there is no real continue
715 * section.
716 */
717 if (loop->cont_block != loop->header_block) {
718 vtn_add_cfg_work_item(b, work_list, &loop->node,
719 &loop->cont_body, loop->cont_block);
720 }
721
722 vtn_block_set_merge_cf_node(b, loop->break_block, &loop->node);
723
724 return loop->break_block;
725 }
726
727 /* Add the block to the CF list */
728 block->node.parent = cf_parent;
729 list_addtail(&block->node.link, cf_list);
730
731 switch (*block->branch & SpvOpCodeMask) {
732 case SpvOpBranch: {
733 struct vtn_block *branch_block = vtn_block(b, block->branch[1]);
734
735 block->branch_type = vtn_handle_branch(b, cf_parent, branch_block);
736
737 if (block->branch_type == vtn_branch_type_none)
738 return branch_block;
739 else
740 return NULL;
741 }
742
743 case SpvOpReturn:
744 case SpvOpReturnValue:
745 block->branch_type = vtn_branch_type_return;
746 return NULL;
747
748 case SpvOpKill:
749 block->branch_type = vtn_branch_type_discard;
750 return NULL;
751
752 case SpvOpBranchConditional: {
753 struct vtn_value *cond_val = vtn_untyped_value(b, block->branch[1]);
754 vtn_fail_if(!cond_val->type ||
755 cond_val->type->base_type != vtn_base_type_scalar ||
756 cond_val->type->type != glsl_bool_type(),
757 "Condition must be a Boolean type scalar");
758
759 struct vtn_block *then_block = vtn_block(b, block->branch[2]);
760 struct vtn_block *else_block = vtn_block(b, block->branch[3]);
761
762 if (then_block == else_block) {
763 /* This is uncommon but it can happen. We treat this the same way as
764 * an unconditional branch.
765 */
766 block->branch_type = vtn_handle_branch(b, cf_parent, then_block);
767
768 if (block->branch_type == vtn_branch_type_none)
769 return then_block;
770 else
771 return NULL;
772 }
773
774 struct vtn_if *if_stmt = rzalloc(b, struct vtn_if);
775
776 if_stmt->node.type = vtn_cf_node_type_if;
777 if_stmt->node.parent = cf_parent;
778 if_stmt->condition = block->branch[1];
779 list_inithead(&if_stmt->then_body);
780 list_inithead(&if_stmt->else_body);
781
782 list_addtail(&if_stmt->node.link, cf_list);
783
784 if (block->merge &&
785 (*block->merge & SpvOpCodeMask) == SpvOpSelectionMerge) {
786 /* We may not always have a merge block and that merge doesn't
787 * technically have to be an OpSelectionMerge. We could have a block
788 * with an OpLoopMerge which ends in an OpBranchConditional.
789 */
790 if_stmt->merge_block = vtn_block(b, block->merge[1]);
791 vtn_block_set_merge_cf_node(b, if_stmt->merge_block, &if_stmt->node);
792
793 if_stmt->control = block->merge[2];
794 }
795
796 if_stmt->then_type = vtn_handle_branch(b, &if_stmt->node, then_block);
797 if (if_stmt->then_type == vtn_branch_type_none) {
798 vtn_add_cfg_work_item(b, work_list, &if_stmt->node,
799 &if_stmt->then_body, then_block);
800 }
801
802 if_stmt->else_type = vtn_handle_branch(b, &if_stmt->node, else_block);
803 if (if_stmt->else_type == vtn_branch_type_none) {
804 vtn_add_cfg_work_item(b, work_list, &if_stmt->node,
805 &if_stmt->else_body, else_block);
806 }
807
808 return if_stmt->merge_block;
809 }
810
811 case SpvOpSwitch: {
812 struct vtn_value *sel_val = vtn_untyped_value(b, block->branch[1]);
813 vtn_fail_if(!sel_val->type ||
814 sel_val->type->base_type != vtn_base_type_scalar,
815 "Selector of OpSwitch must have a type of OpTypeInt");
816
817 nir_alu_type sel_type =
818 nir_get_nir_type_for_glsl_type(sel_val->type->type);
819 vtn_fail_if(nir_alu_type_get_base_type(sel_type) != nir_type_int &&
820 nir_alu_type_get_base_type(sel_type) != nir_type_uint,
821 "Selector of OpSwitch must have a type of OpTypeInt");
822
823 struct vtn_switch *swtch = rzalloc(b, struct vtn_switch);
824
825 swtch->node.type = vtn_cf_node_type_switch;
826 swtch->node.parent = cf_parent;
827 swtch->selector = block->branch[1];
828 list_inithead(&swtch->cases);
829
830 list_addtail(&swtch->node.link, cf_list);
831
832 /* We may not always have a merge block */
833 if (block->merge) {
834 vtn_fail_if((*block->merge & SpvOpCodeMask) != SpvOpSelectionMerge,
835 "An OpLoopMerge instruction must immediately precede "
836 "either an OpBranch or OpBranchConditional "
837 "instruction.");
838 swtch->break_block = vtn_block(b, block->merge[1]);
839 vtn_block_set_merge_cf_node(b, swtch->break_block, &swtch->node);
840 }
841
842 /* First, we go through and record all of the cases. */
843 const uint32_t *branch_end =
844 block->branch + (block->branch[0] >> SpvWordCountShift);
845
846 struct hash_table *block_to_case = _mesa_pointer_hash_table_create(b);
847
848 bool is_default = true;
849 const unsigned bitsize = nir_alu_type_get_type_size(sel_type);
850 for (const uint32_t *w = block->branch + 2; w < branch_end;) {
851 uint64_t literal = 0;
852 if (!is_default) {
853 if (bitsize <= 32) {
854 literal = *(w++);
855 } else {
856 assert(bitsize == 64);
857 literal = vtn_u64_literal(w);
858 w += 2;
859 }
860 }
861 struct vtn_block *case_block = vtn_block(b, *(w++));
862
863 struct hash_entry *case_entry =
864 _mesa_hash_table_search(block_to_case, case_block);
865
866 struct vtn_case *cse;
867 if (case_entry) {
868 cse = case_entry->data;
869 } else {
870 cse = rzalloc(b, struct vtn_case);
871
872 cse->node.type = vtn_cf_node_type_case;
873 cse->node.parent = &swtch->node;
874 list_inithead(&cse->body);
875 util_dynarray_init(&cse->values, b);
876
877 cse->type = vtn_handle_branch(b, &swtch->node, case_block);
878 switch (cse->type) {
879 case vtn_branch_type_none:
880 /* This is a "real" cases which has stuff in it */
881 vtn_fail_if(case_block->switch_case != NULL,
882 "OpSwitch has a case which is also in another "
883 "OpSwitch construct");
884 case_block->switch_case = cse;
885 vtn_add_cfg_work_item(b, work_list, &cse->node,
886 &cse->body, case_block);
887 break;
888
889 case vtn_branch_type_switch_break:
890 case vtn_branch_type_loop_break:
891 case vtn_branch_type_loop_continue:
892 /* Switch breaks as well as loop breaks and continues can be
893 * used to break out of a switch construct or as direct targets
894 * of the OpSwitch.
895 */
896 break;
897
898 default:
899 vtn_fail("Target of OpSwitch is not a valid structured exit "
900 "from the switch construct.");
901 }
902
903 list_addtail(&cse->node.link, &swtch->cases);
904
905 _mesa_hash_table_insert(block_to_case, case_block, cse);
906 }
907
908 if (is_default) {
909 cse->is_default = true;
910 } else {
911 util_dynarray_append(&cse->values, uint64_t, literal);
912 }
913
914 is_default = false;
915 }
916
917 _mesa_hash_table_destroy(block_to_case, NULL);
918
919 return swtch->break_block;
920 }
921
922 case SpvOpUnreachable:
923 return NULL;
924
925 default:
926 vtn_fail("Block did not end with a valid branch instruction");
927 }
928 }
929
930 void
931 vtn_build_cfg(struct vtn_builder *b, const uint32_t *words, const uint32_t *end)
932 {
933 vtn_foreach_instruction(b, words, end,
934 vtn_cfg_handle_prepass_instruction);
935
936 vtn_foreach_cf_node(func_node, &b->functions) {
937 struct vtn_function *func = vtn_cf_node_as_function(func_node);
938
939 /* We build the CFG for each function by doing a breadth-first search on
940 * the control-flow graph. We keep track of our state using a worklist.
941 * Doing a BFS ensures that we visit each structured control-flow
942 * construct and its merge node before we visit the stuff inside the
943 * construct.
944 */
945 struct list_head work_list;
946 list_inithead(&work_list);
947 vtn_add_cfg_work_item(b, &work_list, &func->node, &func->body,
948 func->start_block);
949
950 while (!list_is_empty(&work_list)) {
951 struct vtn_cfg_work_item *work =
952 list_first_entry(&work_list, struct vtn_cfg_work_item, link);
953 list_del(&work->link);
954
955 for (struct vtn_block *block = work->start_block; block; ) {
956 block = vtn_process_block(b, &work_list, work->cf_parent,
957 work->cf_list, block);
958 }
959 }
960 }
961 }
962
963 static bool
964 vtn_handle_phis_first_pass(struct vtn_builder *b, SpvOp opcode,
965 const uint32_t *w, unsigned count)
966 {
967 if (opcode == SpvOpLabel)
968 return true; /* Nothing to do */
969
970 /* If this isn't a phi node, stop. */
971 if (opcode != SpvOpPhi)
972 return false;
973
974 /* For handling phi nodes, we do a poor-man's out-of-ssa on the spot.
975 * For each phi, we create a variable with the appropreate type and
976 * do a load from that variable. Then, in a second pass, we add
977 * stores to that variable to each of the predecessor blocks.
978 *
979 * We could do something more intelligent here. However, in order to
980 * handle loops and things properly, we really need dominance
981 * information. It would end up basically being the into-SSA
982 * algorithm all over again. It's easier if we just let
983 * lower_vars_to_ssa do that for us instead of repeating it here.
984 */
985 struct vtn_type *type = vtn_get_type(b, w[1]);
986 nir_variable *phi_var =
987 nir_local_variable_create(b->nb.impl, type->type, "phi");
988 _mesa_hash_table_insert(b->phi_table, w, phi_var);
989
990 vtn_push_ssa_value(b, w[2],
991 vtn_local_load(b, nir_build_deref_var(&b->nb, phi_var), 0));
992
993 return true;
994 }
995
996 static bool
997 vtn_handle_phi_second_pass(struct vtn_builder *b, SpvOp opcode,
998 const uint32_t *w, unsigned count)
999 {
1000 if (opcode != SpvOpPhi)
1001 return true;
1002
1003 struct hash_entry *phi_entry = _mesa_hash_table_search(b->phi_table, w);
1004
1005 /* It's possible that this phi is in an unreachable block in which case it
1006 * may never have been emitted and therefore may not be in the hash table.
1007 * In this case, there's no var for it and it's safe to just bail.
1008 */
1009 if (phi_entry == NULL)
1010 return true;
1011
1012 nir_variable *phi_var = phi_entry->data;
1013
1014 for (unsigned i = 3; i < count; i += 2) {
1015 struct vtn_block *pred = vtn_block(b, w[i + 1]);
1016
1017 /* If block does not have end_nop, that is because it is an unreacheable
1018 * block, and hence it is not worth to handle it */
1019 if (!pred->end_nop)
1020 continue;
1021
1022 b->nb.cursor = nir_after_instr(&pred->end_nop->instr);
1023
1024 struct vtn_ssa_value *src = vtn_ssa_value(b, w[i]);
1025
1026 vtn_local_store(b, src, nir_build_deref_var(&b->nb, phi_var), 0);
1027 }
1028
1029 return true;
1030 }
1031
1032 static void
1033 vtn_emit_branch(struct vtn_builder *b, enum vtn_branch_type branch_type,
1034 nir_variable *switch_fall_var, bool *has_switch_break)
1035 {
1036 switch (branch_type) {
1037 case vtn_branch_type_if_merge:
1038 break; /* Nothing to do */
1039 case vtn_branch_type_switch_break:
1040 nir_store_var(&b->nb, switch_fall_var, nir_imm_false(&b->nb), 1);
1041 *has_switch_break = true;
1042 break;
1043 case vtn_branch_type_switch_fallthrough:
1044 break; /* Nothing to do */
1045 case vtn_branch_type_loop_break:
1046 nir_jump(&b->nb, nir_jump_break);
1047 break;
1048 case vtn_branch_type_loop_continue:
1049 nir_jump(&b->nb, nir_jump_continue);
1050 break;
1051 case vtn_branch_type_loop_back_edge:
1052 break;
1053 case vtn_branch_type_return:
1054 nir_jump(&b->nb, nir_jump_return);
1055 break;
1056 case vtn_branch_type_discard: {
1057 nir_intrinsic_instr *discard =
1058 nir_intrinsic_instr_create(b->nb.shader, nir_intrinsic_discard);
1059 nir_builder_instr_insert(&b->nb, &discard->instr);
1060 break;
1061 }
1062 default:
1063 vtn_fail("Invalid branch type");
1064 }
1065 }
1066
1067 static nir_ssa_def *
1068 vtn_switch_case_condition(struct vtn_builder *b, struct vtn_switch *swtch,
1069 nir_ssa_def *sel, struct vtn_case *cse)
1070 {
1071 if (cse->is_default) {
1072 nir_ssa_def *any = nir_imm_false(&b->nb);
1073 vtn_foreach_cf_node(other_node, &swtch->cases) {
1074 struct vtn_case *other = vtn_cf_node_as_case(other_node);
1075 if (other->is_default)
1076 continue;
1077
1078 any = nir_ior(&b->nb, any,
1079 vtn_switch_case_condition(b, swtch, sel, other));
1080 }
1081 return nir_inot(&b->nb, any);
1082 } else {
1083 nir_ssa_def *cond = nir_imm_false(&b->nb);
1084 util_dynarray_foreach(&cse->values, uint64_t, val) {
1085 nir_ssa_def *imm = nir_imm_intN_t(&b->nb, *val, sel->bit_size);
1086 cond = nir_ior(&b->nb, cond, nir_ieq(&b->nb, sel, imm));
1087 }
1088 return cond;
1089 }
1090 }
1091
1092 static nir_loop_control
1093 vtn_loop_control(struct vtn_builder *b, struct vtn_loop *vtn_loop)
1094 {
1095 if (vtn_loop->control == SpvLoopControlMaskNone)
1096 return nir_loop_control_none;
1097 else if (vtn_loop->control & SpvLoopControlDontUnrollMask)
1098 return nir_loop_control_dont_unroll;
1099 else if (vtn_loop->control & SpvLoopControlUnrollMask)
1100 return nir_loop_control_unroll;
1101 else if (vtn_loop->control & SpvLoopControlDependencyInfiniteMask ||
1102 vtn_loop->control & SpvLoopControlDependencyLengthMask ||
1103 vtn_loop->control & SpvLoopControlMinIterationsMask ||
1104 vtn_loop->control & SpvLoopControlMaxIterationsMask ||
1105 vtn_loop->control & SpvLoopControlIterationMultipleMask ||
1106 vtn_loop->control & SpvLoopControlPeelCountMask ||
1107 vtn_loop->control & SpvLoopControlPartialCountMask) {
1108 /* We do not do anything special with these yet. */
1109 return nir_loop_control_none;
1110 } else {
1111 vtn_fail("Invalid loop control");
1112 }
1113 }
1114
1115 static nir_selection_control
1116 vtn_selection_control(struct vtn_builder *b, struct vtn_if *vtn_if)
1117 {
1118 if (vtn_if->control == SpvSelectionControlMaskNone)
1119 return nir_selection_control_none;
1120 else if (vtn_if->control & SpvSelectionControlDontFlattenMask)
1121 return nir_selection_control_dont_flatten;
1122 else if (vtn_if->control & SpvSelectionControlFlattenMask)
1123 return nir_selection_control_flatten;
1124 else
1125 vtn_fail("Invalid selection control");
1126 }
1127
1128 static void
1129 vtn_emit_cf_list(struct vtn_builder *b, struct list_head *cf_list,
1130 nir_variable *switch_fall_var, bool *has_switch_break,
1131 vtn_instruction_handler handler)
1132 {
1133 vtn_foreach_cf_node(node, cf_list) {
1134 switch (node->type) {
1135 case vtn_cf_node_type_block: {
1136 struct vtn_block *block = vtn_cf_node_as_block(node);
1137
1138 const uint32_t *block_start = block->label;
1139 const uint32_t *block_end = block->merge ? block->merge :
1140 block->branch;
1141
1142 block_start = vtn_foreach_instruction(b, block_start, block_end,
1143 vtn_handle_phis_first_pass);
1144
1145 vtn_foreach_instruction(b, block_start, block_end, handler);
1146
1147 block->end_nop = nir_intrinsic_instr_create(b->nb.shader,
1148 nir_intrinsic_nop);
1149 nir_builder_instr_insert(&b->nb, &block->end_nop->instr);
1150
1151 if ((*block->branch & SpvOpCodeMask) == SpvOpReturnValue) {
1152 vtn_fail_if(b->func->type->return_type->base_type ==
1153 vtn_base_type_void,
1154 "Return with a value from a function returning void");
1155 struct vtn_ssa_value *src = vtn_ssa_value(b, block->branch[1]);
1156 const struct glsl_type *ret_type =
1157 glsl_get_bare_type(b->func->type->return_type->type);
1158 nir_deref_instr *ret_deref =
1159 nir_build_deref_cast(&b->nb, nir_load_param(&b->nb, 0),
1160 nir_var_function_temp, ret_type, 0);
1161 vtn_local_store(b, src, ret_deref, 0);
1162 }
1163
1164 if (block->branch_type != vtn_branch_type_none) {
1165 vtn_emit_branch(b, block->branch_type,
1166 switch_fall_var, has_switch_break);
1167 return;
1168 }
1169
1170 break;
1171 }
1172
1173 case vtn_cf_node_type_if: {
1174 struct vtn_if *vtn_if = vtn_cf_node_as_if(node);
1175 bool sw_break = false;
1176
1177 nir_if *nif =
1178 nir_push_if(&b->nb, vtn_get_nir_ssa(b, vtn_if->condition));
1179
1180 nif->control = vtn_selection_control(b, vtn_if);
1181
1182 if (vtn_if->then_type == vtn_branch_type_none) {
1183 vtn_emit_cf_list(b, &vtn_if->then_body,
1184 switch_fall_var, &sw_break, handler);
1185 } else {
1186 vtn_emit_branch(b, vtn_if->then_type, switch_fall_var, &sw_break);
1187 }
1188
1189 nir_push_else(&b->nb, nif);
1190 if (vtn_if->else_type == vtn_branch_type_none) {
1191 vtn_emit_cf_list(b, &vtn_if->else_body,
1192 switch_fall_var, &sw_break, handler);
1193 } else {
1194 vtn_emit_branch(b, vtn_if->else_type, switch_fall_var, &sw_break);
1195 }
1196
1197 nir_pop_if(&b->nb, nif);
1198
1199 /* If we encountered a switch break somewhere inside of the if,
1200 * then it would have been handled correctly by calling
1201 * emit_cf_list or emit_branch for the interrior. However, we
1202 * need to predicate everything following on wether or not we're
1203 * still going.
1204 */
1205 if (sw_break) {
1206 *has_switch_break = true;
1207 nir_push_if(&b->nb, nir_load_var(&b->nb, switch_fall_var));
1208 }
1209 break;
1210 }
1211
1212 case vtn_cf_node_type_loop: {
1213 struct vtn_loop *vtn_loop = vtn_cf_node_as_loop(node);
1214
1215 nir_loop *loop = nir_push_loop(&b->nb);
1216 loop->control = vtn_loop_control(b, vtn_loop);
1217
1218 vtn_emit_cf_list(b, &vtn_loop->body, NULL, NULL, handler);
1219
1220 if (!list_is_empty(&vtn_loop->cont_body)) {
1221 /* If we have a non-trivial continue body then we need to put
1222 * it at the beginning of the loop with a flag to ensure that
1223 * it doesn't get executed in the first iteration.
1224 */
1225 nir_variable *do_cont =
1226 nir_local_variable_create(b->nb.impl, glsl_bool_type(), "cont");
1227
1228 b->nb.cursor = nir_before_cf_node(&loop->cf_node);
1229 nir_store_var(&b->nb, do_cont, nir_imm_false(&b->nb), 1);
1230
1231 b->nb.cursor = nir_before_cf_list(&loop->body);
1232
1233 nir_if *cont_if =
1234 nir_push_if(&b->nb, nir_load_var(&b->nb, do_cont));
1235
1236 vtn_emit_cf_list(b, &vtn_loop->cont_body, NULL, NULL, handler);
1237
1238 nir_pop_if(&b->nb, cont_if);
1239
1240 nir_store_var(&b->nb, do_cont, nir_imm_true(&b->nb), 1);
1241
1242 b->has_loop_continue = true;
1243 }
1244
1245 nir_pop_loop(&b->nb, loop);
1246 break;
1247 }
1248
1249 case vtn_cf_node_type_switch: {
1250 struct vtn_switch *vtn_switch = vtn_cf_node_as_switch(node);
1251
1252 /* Before we can emit anything, we need to sort the list of cases in
1253 * fall-through order.
1254 */
1255 vtn_switch_order_cases(vtn_switch);
1256
1257 /* First, we create a variable to keep track of whether or not the
1258 * switch is still going at any given point. Any switch breaks
1259 * will set this variable to false.
1260 */
1261 nir_variable *fall_var =
1262 nir_local_variable_create(b->nb.impl, glsl_bool_type(), "fall");
1263 nir_store_var(&b->nb, fall_var, nir_imm_false(&b->nb), 1);
1264
1265 nir_ssa_def *sel = vtn_get_nir_ssa(b, vtn_switch->selector);
1266
1267 /* Now we can walk the list of cases and actually emit code */
1268 vtn_foreach_cf_node(case_node, &vtn_switch->cases) {
1269 struct vtn_case *cse = vtn_cf_node_as_case(case_node);
1270
1271 /* Figure out the condition */
1272 nir_ssa_def *cond =
1273 vtn_switch_case_condition(b, vtn_switch, sel, cse);
1274 /* Take fallthrough into account */
1275 cond = nir_ior(&b->nb, cond, nir_load_var(&b->nb, fall_var));
1276
1277 nir_if *case_if = nir_push_if(&b->nb, cond);
1278
1279 bool has_break = false;
1280 nir_store_var(&b->nb, fall_var, nir_imm_true(&b->nb), 1);
1281 vtn_emit_cf_list(b, &cse->body, fall_var, &has_break, handler);
1282 (void)has_break; /* We don't care */
1283
1284 nir_pop_if(&b->nb, case_if);
1285 }
1286
1287 break;
1288 }
1289
1290 default:
1291 vtn_fail("Invalid CF node type");
1292 }
1293 }
1294 }
1295
1296 void
1297 vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
1298 vtn_instruction_handler instruction_handler)
1299 {
1300 nir_builder_init(&b->nb, func->impl);
1301 b->func = func;
1302 b->nb.cursor = nir_after_cf_list(&func->impl->body);
1303 b->nb.exact = b->exact;
1304 b->has_loop_continue = false;
1305 b->phi_table = _mesa_pointer_hash_table_create(b);
1306
1307 vtn_emit_cf_list(b, &func->body, NULL, NULL, instruction_handler);
1308
1309 vtn_foreach_instruction(b, func->start_block->label, func->end,
1310 vtn_handle_phi_second_pass);
1311
1312 nir_rematerialize_derefs_in_use_blocks_impl(func->impl);
1313
1314 /* Continue blocks for loops get inserted before the body of the loop
1315 * but instructions in the continue may use SSA defs in the loop body.
1316 * Therefore, we need to repair SSA to insert the needed phi nodes.
1317 */
1318 if (b->has_loop_continue)
1319 nir_repair_ssa_impl(func->impl);
1320
1321 func->emitted = true;
1322 }