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