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