2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include "vtn_private.h"
25 #include "nir/nir_vla.h"
27 static struct vtn_pointer
*
28 vtn_load_param_pointer(struct vtn_builder
*b
,
29 struct vtn_type
*param_type
,
32 struct vtn_type
*ptr_type
= param_type
;
33 if (param_type
->base_type
!= vtn_base_type_pointer
) {
34 assert(param_type
->base_type
== vtn_base_type_image
||
35 param_type
->base_type
== vtn_base_type_sampler
);
36 ptr_type
= rzalloc(b
, struct vtn_type
);
37 ptr_type
->base_type
= vtn_base_type_pointer
;
38 ptr_type
->deref
= param_type
;
39 ptr_type
->storage_class
= SpvStorageClassUniformConstant
;
42 return vtn_pointer_from_ssa(b
, nir_load_param(&b
->nb
, param_idx
), ptr_type
);
46 vtn_type_count_function_params(struct vtn_type
*type
)
48 switch (type
->base_type
) {
49 case vtn_base_type_array
:
50 case vtn_base_type_matrix
:
51 return type
->length
* vtn_type_count_function_params(type
->array_element
);
53 case vtn_base_type_struct
: {
55 for (unsigned i
= 0; i
< type
->length
; i
++)
56 count
+= vtn_type_count_function_params(type
->members
[i
]);
60 case vtn_base_type_sampled_image
:
69 vtn_type_add_to_function_params(struct vtn_type
*type
,
73 static const nir_parameter nir_deref_param
= {
78 switch (type
->base_type
) {
79 case vtn_base_type_array
:
80 case vtn_base_type_matrix
:
81 for (unsigned i
= 0; i
< type
->length
; i
++)
82 vtn_type_add_to_function_params(type
->array_element
, func
, param_idx
);
85 case vtn_base_type_struct
:
86 for (unsigned i
= 0; i
< type
->length
; i
++)
87 vtn_type_add_to_function_params(type
->members
[i
], func
, param_idx
);
90 case vtn_base_type_sampled_image
:
91 func
->params
[(*param_idx
)++] = nir_deref_param
;
92 func
->params
[(*param_idx
)++] = nir_deref_param
;
95 case vtn_base_type_image
:
96 case vtn_base_type_sampler
:
97 func
->params
[(*param_idx
)++] = nir_deref_param
;
100 case vtn_base_type_pointer
:
102 func
->params
[(*param_idx
)++] = (nir_parameter
) {
103 .num_components
= glsl_get_vector_elements(type
->type
),
104 .bit_size
= glsl_get_bit_size(type
->type
),
107 func
->params
[(*param_idx
)++] = nir_deref_param
;
112 func
->params
[(*param_idx
)++] = (nir_parameter
) {
113 .num_components
= glsl_get_vector_elements(type
->type
),
114 .bit_size
= glsl_get_bit_size(type
->type
),
120 vtn_ssa_value_add_to_call_params(struct vtn_builder
*b
,
121 struct vtn_ssa_value
*value
,
122 struct vtn_type
*type
,
123 nir_call_instr
*call
,
126 switch (type
->base_type
) {
127 case vtn_base_type_array
:
128 case vtn_base_type_matrix
:
129 for (unsigned i
= 0; i
< type
->length
; i
++) {
130 vtn_ssa_value_add_to_call_params(b
, value
->elems
[i
],
136 case vtn_base_type_struct
:
137 for (unsigned i
= 0; i
< type
->length
; i
++) {
138 vtn_ssa_value_add_to_call_params(b
, value
->elems
[i
],
145 call
->params
[(*param_idx
)++] = nir_src_for_ssa(value
->def
);
151 vtn_ssa_value_load_function_param(struct vtn_builder
*b
,
152 struct vtn_ssa_value
*value
,
153 struct vtn_type
*type
,
156 switch (type
->base_type
) {
157 case vtn_base_type_array
:
158 case vtn_base_type_matrix
:
159 for (unsigned i
= 0; i
< type
->length
; i
++) {
160 vtn_ssa_value_load_function_param(b
, value
->elems
[i
],
161 type
->array_element
, param_idx
);
165 case vtn_base_type_struct
:
166 for (unsigned i
= 0; i
< type
->length
; i
++) {
167 vtn_ssa_value_load_function_param(b
, value
->elems
[i
],
168 type
->members
[i
], param_idx
);
173 value
->def
= nir_load_param(&b
->nb
, (*param_idx
)++);
179 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
180 const uint32_t *w
, unsigned count
)
182 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
183 struct vtn_function
*vtn_callee
=
184 vtn_value(b
, w
[3], vtn_value_type_function
)->func
;
185 struct nir_function
*callee
= vtn_callee
->impl
->function
;
187 vtn_callee
->referenced
= true;
189 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
191 unsigned param_idx
= 0;
193 nir_deref_instr
*ret_deref
= NULL
;
194 struct vtn_type
*ret_type
= vtn_callee
->type
->return_type
;
195 if (ret_type
->base_type
!= vtn_base_type_void
) {
196 nir_variable
*ret_tmp
=
197 nir_local_variable_create(b
->nb
.impl
,
198 glsl_get_bare_type(ret_type
->type
),
200 ret_deref
= nir_build_deref_var(&b
->nb
, ret_tmp
);
201 call
->params
[param_idx
++] = nir_src_for_ssa(&ret_deref
->dest
.ssa
);
204 for (unsigned i
= 0; i
< vtn_callee
->type
->length
; i
++) {
205 struct vtn_type
*arg_type
= vtn_callee
->type
->params
[i
];
206 unsigned arg_id
= w
[4 + i
];
208 if (arg_type
->base_type
== vtn_base_type_sampled_image
) {
209 struct vtn_sampled_image
*sampled_image
=
210 vtn_value(b
, arg_id
, vtn_value_type_sampled_image
)->sampled_image
;
212 call
->params
[param_idx
++] =
213 nir_src_for_ssa(&sampled_image
->image
->deref
->dest
.ssa
);
214 call
->params
[param_idx
++] =
215 nir_src_for_ssa(&sampled_image
->sampler
->deref
->dest
.ssa
);
216 } else if (arg_type
->base_type
== vtn_base_type_pointer
||
217 arg_type
->base_type
== vtn_base_type_image
||
218 arg_type
->base_type
== vtn_base_type_sampler
) {
219 struct vtn_pointer
*pointer
=
220 vtn_value(b
, arg_id
, vtn_value_type_pointer
)->pointer
;
221 call
->params
[param_idx
++] =
222 nir_src_for_ssa(vtn_pointer_to_ssa(b
, pointer
));
224 vtn_ssa_value_add_to_call_params(b
, vtn_ssa_value(b
, arg_id
),
225 arg_type
, call
, ¶m_idx
);
228 assert(param_idx
== call
->num_params
);
230 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
232 if (ret_type
->base_type
== vtn_base_type_void
) {
233 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
235 vtn_push_ssa(b
, w
[2], res_type
, vtn_local_load(b
, ret_deref
));
240 vtn_cfg_handle_prepass_instruction(struct vtn_builder
*b
, SpvOp opcode
,
241 const uint32_t *w
, unsigned count
)
244 case SpvOpFunction
: {
245 vtn_assert(b
->func
== NULL
);
246 b
->func
= rzalloc(b
, struct vtn_function
);
248 list_inithead(&b
->func
->body
);
249 b
->func
->control
= w
[3];
251 MAYBE_UNUSED
const struct glsl_type
*result_type
=
252 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
253 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_function
);
256 b
->func
->type
= vtn_value(b
, w
[4], vtn_value_type_type
)->type
;
257 const struct vtn_type
*func_type
= b
->func
->type
;
259 vtn_assert(func_type
->return_type
->type
== result_type
);
262 nir_function_create(b
->shader
, ralloc_strdup(b
->shader
, val
->name
));
264 unsigned num_params
= 0;
265 for (unsigned i
= 0; i
< func_type
->length
; i
++)
266 num_params
+= vtn_type_count_function_params(func_type
->params
[i
]);
268 /* Add one parameter for the function return value */
269 if (func_type
->return_type
->base_type
!= vtn_base_type_void
)
272 func
->num_params
= num_params
;
273 func
->params
= ralloc_array(b
->shader
, nir_parameter
, num_params
);
276 if (func_type
->return_type
->base_type
!= vtn_base_type_void
) {
277 /* The return value is a regular pointer */
278 func
->params
[idx
++] = (nir_parameter
) {
279 .num_components
= 1, .bit_size
= 32,
283 for (unsigned i
= 0; i
< func_type
->length
; i
++)
284 vtn_type_add_to_function_params(func_type
->params
[i
], func
, &idx
);
285 assert(idx
== num_params
);
287 b
->func
->impl
= nir_function_impl_create(func
);
288 nir_builder_init(&b
->nb
, func
->impl
);
289 b
->nb
.cursor
= nir_before_cf_list(&b
->func
->impl
->body
);
291 b
->func_param_idx
= 0;
293 /* The return value is the first parameter */
294 if (func_type
->return_type
->base_type
!= vtn_base_type_void
)
299 case SpvOpFunctionEnd
:
304 case SpvOpFunctionParameter
: {
305 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
307 vtn_assert(b
->func_param_idx
< b
->func
->impl
->function
->num_params
);
309 if (type
->base_type
== vtn_base_type_sampled_image
) {
310 /* Sampled images are actually two parameters. The first is the
311 * image and the second is the sampler.
313 struct vtn_value
*val
=
314 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
316 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
317 val
->sampled_image
->type
= type
;
319 struct vtn_type
*sampler_type
= rzalloc(b
, struct vtn_type
);
320 sampler_type
->base_type
= vtn_base_type_sampler
;
321 sampler_type
->type
= glsl_bare_sampler_type();
323 val
->sampled_image
->image
=
324 vtn_load_param_pointer(b
, type
, b
->func_param_idx
++);
325 val
->sampled_image
->sampler
=
326 vtn_load_param_pointer(b
, sampler_type
, b
->func_param_idx
++);
327 } else if (type
->base_type
== vtn_base_type_pointer
&&
328 type
->type
!= NULL
) {
329 /* This is a pointer with an actual storage type */
330 struct vtn_value
*val
=
331 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
332 nir_ssa_def
*ssa_ptr
= nir_load_param(&b
->nb
, b
->func_param_idx
++);
333 val
->pointer
= vtn_pointer_from_ssa(b
, ssa_ptr
, type
);
334 } else if (type
->base_type
== vtn_base_type_pointer
||
335 type
->base_type
== vtn_base_type_image
||
336 type
->base_type
== vtn_base_type_sampler
) {
337 struct vtn_value
*val
=
338 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
340 vtn_load_param_pointer(b
, type
, b
->func_param_idx
++);
342 /* We're a regular SSA value. */
343 struct vtn_ssa_value
*value
= vtn_create_ssa_value(b
, type
->type
);
344 vtn_ssa_value_load_function_param(b
, value
, type
, &b
->func_param_idx
);
345 vtn_push_ssa(b
, w
[2], type
, value
);
351 vtn_assert(b
->block
== NULL
);
352 b
->block
= rzalloc(b
, struct vtn_block
);
353 b
->block
->node
.type
= vtn_cf_node_type_block
;
355 vtn_push_value(b
, w
[1], vtn_value_type_block
)->block
= b
->block
;
357 if (b
->func
->start_block
== NULL
) {
358 /* This is the first block encountered for this function. In this
359 * case, we set the start block and add it to the list of
360 * implemented functions that we'll walk later.
362 b
->func
->start_block
= b
->block
;
363 exec_list_push_tail(&b
->functions
, &b
->func
->node
);
368 case SpvOpSelectionMerge
:
370 vtn_assert(b
->block
&& b
->block
->merge
== NULL
);
375 case SpvOpBranchConditional
:
379 case SpvOpReturnValue
:
380 case SpvOpUnreachable
:
381 vtn_assert(b
->block
&& b
->block
->branch
== NULL
);
382 b
->block
->branch
= w
;
387 /* Continue on as per normal */
395 vtn_add_case(struct vtn_builder
*b
, struct vtn_switch
*swtch
,
396 struct vtn_block
*break_block
,
397 uint32_t block_id
, uint64_t val
, bool is_default
)
399 struct vtn_block
*case_block
=
400 vtn_value(b
, block_id
, vtn_value_type_block
)->block
;
402 /* Don't create dummy cases that just break */
403 if (case_block
== break_block
)
406 if (case_block
->switch_case
== NULL
) {
407 struct vtn_case
*c
= ralloc(b
, struct vtn_case
);
409 list_inithead(&c
->body
);
410 c
->start_block
= case_block
;
411 c
->fallthrough
= NULL
;
412 util_dynarray_init(&c
->values
, b
);
413 c
->is_default
= false;
416 list_addtail(&c
->link
, &swtch
->cases
);
418 case_block
->switch_case
= c
;
422 case_block
->switch_case
->is_default
= true;
424 util_dynarray_append(&case_block
->switch_case
->values
, uint64_t, val
);
428 /* This function performs a depth-first search of the cases and puts them
429 * in fall-through order.
432 vtn_order_case(struct vtn_switch
*swtch
, struct vtn_case
*cse
)
439 list_del(&cse
->link
);
441 if (cse
->fallthrough
) {
442 vtn_order_case(swtch
, cse
->fallthrough
);
444 /* If we have a fall-through, place this case right before the case it
445 * falls through to. This ensures that fallthroughs come one after
446 * the other. These two can never get separated because that would
447 * imply something else falling through to the same case. Also, this
448 * can't break ordering because the DFS ensures that this case is
449 * visited before anything that falls through to it.
451 list_addtail(&cse
->link
, &cse
->fallthrough
->link
);
453 list_add(&cse
->link
, &swtch
->cases
);
457 static enum vtn_branch_type
458 vtn_get_branch_type(struct vtn_builder
*b
,
459 struct vtn_block
*block
,
460 struct vtn_case
*swcase
, struct vtn_block
*switch_break
,
461 struct vtn_block
*loop_break
, struct vtn_block
*loop_cont
)
463 if (block
->switch_case
) {
464 /* This branch is actually a fallthrough */
465 vtn_assert(swcase
->fallthrough
== NULL
||
466 swcase
->fallthrough
== block
->switch_case
);
467 swcase
->fallthrough
= block
->switch_case
;
468 return vtn_branch_type_switch_fallthrough
;
469 } else if (block
== loop_break
) {
470 return vtn_branch_type_loop_break
;
471 } else if (block
== loop_cont
) {
472 return vtn_branch_type_loop_continue
;
473 } else if (block
== switch_break
) {
474 return vtn_branch_type_switch_break
;
476 return vtn_branch_type_none
;
481 vtn_cfg_walk_blocks(struct vtn_builder
*b
, struct list_head
*cf_list
,
482 struct vtn_block
*start
, struct vtn_case
*switch_case
,
483 struct vtn_block
*switch_break
,
484 struct vtn_block
*loop_break
, struct vtn_block
*loop_cont
,
485 struct vtn_block
*end
)
487 struct vtn_block
*block
= start
;
488 while (block
!= end
) {
489 if (block
->merge
&& (*block
->merge
& SpvOpCodeMask
) == SpvOpLoopMerge
&&
491 struct vtn_loop
*loop
= ralloc(b
, struct vtn_loop
);
493 loop
->node
.type
= vtn_cf_node_type_loop
;
494 list_inithead(&loop
->body
);
495 list_inithead(&loop
->cont_body
);
496 loop
->control
= block
->merge
[3];
498 list_addtail(&loop
->node
.link
, cf_list
);
501 struct vtn_block
*new_loop_break
=
502 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
503 struct vtn_block
*new_loop_cont
=
504 vtn_value(b
, block
->merge
[2], vtn_value_type_block
)->block
;
506 /* Note: This recursive call will start with the current block as
507 * its start block. If we weren't careful, we would get here
508 * again and end up in infinite recursion. This is why we set
509 * block->loop above and check for it before creating one. This
510 * way, we only create the loop once and the second call that
511 * tries to handle this loop goes to the cases below and gets
512 * handled as a regular block.
514 * Note: When we make the recursive walk calls, we pass NULL for
515 * the switch break since you have to break out of the loop first.
516 * We do, however, still pass the current switch case because it's
517 * possible that the merge block for the loop is the start of
520 vtn_cfg_walk_blocks(b
, &loop
->body
, block
, switch_case
, NULL
,
521 new_loop_break
, new_loop_cont
, NULL
);
522 vtn_cfg_walk_blocks(b
, &loop
->cont_body
, new_loop_cont
, NULL
, NULL
,
523 new_loop_break
, NULL
, block
);
525 enum vtn_branch_type branch_type
=
526 vtn_get_branch_type(b
, new_loop_break
, switch_case
, switch_break
,
527 loop_break
, loop_cont
);
529 if (branch_type
!= vtn_branch_type_none
) {
530 /* Stop walking through the CFG when this inner loop's break block
531 * ends up as the same block as the outer loop's continue block
532 * because we are already going to visit it.
534 vtn_assert(branch_type
== vtn_branch_type_loop_continue
);
538 block
= new_loop_break
;
542 vtn_assert(block
->node
.link
.next
== NULL
);
543 list_addtail(&block
->node
.link
, cf_list
);
545 switch (*block
->branch
& SpvOpCodeMask
) {
547 struct vtn_block
*branch_block
=
548 vtn_value(b
, block
->branch
[1], vtn_value_type_block
)->block
;
550 block
->branch_type
= vtn_get_branch_type(b
, branch_block
,
551 switch_case
, switch_break
,
552 loop_break
, loop_cont
);
554 if (block
->branch_type
!= vtn_branch_type_none
)
557 block
= branch_block
;
562 case SpvOpReturnValue
:
563 block
->branch_type
= vtn_branch_type_return
;
567 block
->branch_type
= vtn_branch_type_discard
;
570 case SpvOpBranchConditional
: {
571 struct vtn_block
*then_block
=
572 vtn_value(b
, block
->branch
[2], vtn_value_type_block
)->block
;
573 struct vtn_block
*else_block
=
574 vtn_value(b
, block
->branch
[3], vtn_value_type_block
)->block
;
576 struct vtn_if
*if_stmt
= ralloc(b
, struct vtn_if
);
578 if_stmt
->node
.type
= vtn_cf_node_type_if
;
579 if_stmt
->condition
= block
->branch
[1];
580 list_inithead(&if_stmt
->then_body
);
581 list_inithead(&if_stmt
->else_body
);
583 list_addtail(&if_stmt
->node
.link
, cf_list
);
586 (*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
) {
587 if_stmt
->control
= block
->merge
[2];
590 if_stmt
->then_type
= vtn_get_branch_type(b
, then_block
,
591 switch_case
, switch_break
,
592 loop_break
, loop_cont
);
593 if_stmt
->else_type
= vtn_get_branch_type(b
, else_block
,
594 switch_case
, switch_break
,
595 loop_break
, loop_cont
);
597 if (then_block
== else_block
) {
598 block
->branch_type
= if_stmt
->then_type
;
599 if (block
->branch_type
== vtn_branch_type_none
) {
605 } else if (if_stmt
->then_type
== vtn_branch_type_none
&&
606 if_stmt
->else_type
== vtn_branch_type_none
) {
607 /* Neither side of the if is something we can short-circuit. */
608 vtn_assert((*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
);
609 struct vtn_block
*merge_block
=
610 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
612 vtn_cfg_walk_blocks(b
, &if_stmt
->then_body
, then_block
,
613 switch_case
, switch_break
,
614 loop_break
, loop_cont
, merge_block
);
615 vtn_cfg_walk_blocks(b
, &if_stmt
->else_body
, else_block
,
616 switch_case
, switch_break
,
617 loop_break
, loop_cont
, merge_block
);
619 enum vtn_branch_type merge_type
=
620 vtn_get_branch_type(b
, merge_block
, switch_case
, switch_break
,
621 loop_break
, loop_cont
);
622 if (merge_type
== vtn_branch_type_none
) {
628 } else if (if_stmt
->then_type
!= vtn_branch_type_none
&&
629 if_stmt
->else_type
!= vtn_branch_type_none
) {
630 /* Both sides were short-circuited. We're done here. */
633 /* Exeactly one side of the branch could be short-circuited.
634 * We set the branch up as a predicated break/continue and we
635 * continue on with the other side as if it were what comes
638 if (if_stmt
->then_type
== vtn_branch_type_none
) {
645 vtn_fail("Should have returned or continued");
649 vtn_assert((*block
->merge
& SpvOpCodeMask
) == SpvOpSelectionMerge
);
650 struct vtn_block
*break_block
=
651 vtn_value(b
, block
->merge
[1], vtn_value_type_block
)->block
;
653 struct vtn_switch
*swtch
= ralloc(b
, struct vtn_switch
);
655 swtch
->node
.type
= vtn_cf_node_type_switch
;
656 swtch
->selector
= block
->branch
[1];
657 list_inithead(&swtch
->cases
);
659 list_addtail(&swtch
->node
.link
, cf_list
);
661 /* First, we go through and record all of the cases. */
662 const uint32_t *branch_end
=
663 block
->branch
+ (block
->branch
[0] >> SpvWordCountShift
);
665 struct vtn_value
*cond_val
= vtn_untyped_value(b
, block
->branch
[1]);
666 vtn_fail_if(!cond_val
->type
||
667 cond_val
->type
->base_type
!= vtn_base_type_scalar
,
668 "Selector of OpSelect must have a type of OpTypeInt");
670 nir_alu_type cond_type
=
671 nir_get_nir_type_for_glsl_type(cond_val
->type
->type
);
672 vtn_fail_if(nir_alu_type_get_base_type(cond_type
) != nir_type_int
&&
673 nir_alu_type_get_base_type(cond_type
) != nir_type_uint
,
674 "Selector of OpSelect must have a type of OpTypeInt");
676 bool is_default
= true;
677 const unsigned bitsize
= nir_alu_type_get_type_size(cond_type
);
678 for (const uint32_t *w
= block
->branch
+ 2; w
< branch_end
;) {
679 uint64_t literal
= 0;
684 assert(bitsize
== 64);
685 literal
= vtn_u64_literal(w
);
690 uint32_t block_id
= *(w
++);
692 vtn_add_case(b
, swtch
, break_block
, block_id
, literal
, is_default
);
696 /* Now, we go through and walk the blocks. While we walk through
697 * the blocks, we also gather the much-needed fall-through
700 list_for_each_entry(struct vtn_case
, cse
, &swtch
->cases
, link
) {
701 vtn_assert(cse
->start_block
!= break_block
);
702 vtn_cfg_walk_blocks(b
, &cse
->body
, cse
->start_block
, cse
,
703 break_block
, loop_break
, loop_cont
, NULL
);
706 /* Finally, we walk over all of the cases one more time and put
707 * them in fall-through order.
709 for (const uint32_t *w
= block
->branch
+ 2; w
< branch_end
;) {
710 struct vtn_block
*case_block
=
711 vtn_value(b
, *w
, vtn_value_type_block
)->block
;
716 assert(bitsize
== 64);
720 if (case_block
== break_block
)
723 vtn_assert(case_block
->switch_case
);
725 vtn_order_case(swtch
, case_block
->switch_case
);
728 enum vtn_branch_type branch_type
=
729 vtn_get_branch_type(b
, break_block
, switch_case
, NULL
,
730 loop_break
, loop_cont
);
732 if (branch_type
!= vtn_branch_type_none
) {
733 /* It is possible that the break is actually the continue block
734 * for the containing loop. In this case, we need to bail and let
735 * the loop parsing code handle the continue properly.
737 vtn_assert(branch_type
== vtn_branch_type_loop_continue
);
745 case SpvOpUnreachable
:
749 vtn_fail("Unhandled opcode");
755 vtn_build_cfg(struct vtn_builder
*b
, const uint32_t *words
, const uint32_t *end
)
757 vtn_foreach_instruction(b
, words
, end
,
758 vtn_cfg_handle_prepass_instruction
);
760 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
761 vtn_cfg_walk_blocks(b
, &func
->body
, func
->start_block
,
762 NULL
, NULL
, NULL
, NULL
, NULL
);
767 vtn_handle_phis_first_pass(struct vtn_builder
*b
, SpvOp opcode
,
768 const uint32_t *w
, unsigned count
)
770 if (opcode
== SpvOpLabel
)
771 return true; /* Nothing to do */
773 /* If this isn't a phi node, stop. */
774 if (opcode
!= SpvOpPhi
)
777 /* For handling phi nodes, we do a poor-man's out-of-ssa on the spot.
778 * For each phi, we create a variable with the appropreate type and
779 * do a load from that variable. Then, in a second pass, we add
780 * stores to that variable to each of the predecessor blocks.
782 * We could do something more intelligent here. However, in order to
783 * handle loops and things properly, we really need dominance
784 * information. It would end up basically being the into-SSA
785 * algorithm all over again. It's easier if we just let
786 * lower_vars_to_ssa do that for us instead of repeating it here.
788 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
789 nir_variable
*phi_var
=
790 nir_local_variable_create(b
->nb
.impl
, type
->type
, "phi");
791 _mesa_hash_table_insert(b
->phi_table
, w
, phi_var
);
793 vtn_push_ssa(b
, w
[2], type
,
794 vtn_local_load(b
, nir_build_deref_var(&b
->nb
, phi_var
)));
800 vtn_handle_phi_second_pass(struct vtn_builder
*b
, SpvOp opcode
,
801 const uint32_t *w
, unsigned count
)
803 if (opcode
!= SpvOpPhi
)
806 struct hash_entry
*phi_entry
= _mesa_hash_table_search(b
->phi_table
, w
);
807 vtn_assert(phi_entry
);
808 nir_variable
*phi_var
= phi_entry
->data
;
810 for (unsigned i
= 3; i
< count
; i
+= 2) {
811 struct vtn_block
*pred
=
812 vtn_value(b
, w
[i
+ 1], vtn_value_type_block
)->block
;
814 b
->nb
.cursor
= nir_after_instr(&pred
->end_nop
->instr
);
816 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[i
]);
818 vtn_local_store(b
, src
, nir_build_deref_var(&b
->nb
, phi_var
));
825 vtn_emit_branch(struct vtn_builder
*b
, enum vtn_branch_type branch_type
,
826 nir_variable
*switch_fall_var
, bool *has_switch_break
)
828 switch (branch_type
) {
829 case vtn_branch_type_switch_break
:
830 nir_store_var(&b
->nb
, switch_fall_var
, nir_imm_false(&b
->nb
), 1);
831 *has_switch_break
= true;
833 case vtn_branch_type_switch_fallthrough
:
834 break; /* Nothing to do */
835 case vtn_branch_type_loop_break
:
836 nir_jump(&b
->nb
, nir_jump_break
);
838 case vtn_branch_type_loop_continue
:
839 nir_jump(&b
->nb
, nir_jump_continue
);
841 case vtn_branch_type_return
:
842 nir_jump(&b
->nb
, nir_jump_return
);
844 case vtn_branch_type_discard
: {
845 nir_intrinsic_instr
*discard
=
846 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_discard
);
847 nir_builder_instr_insert(&b
->nb
, &discard
->instr
);
851 vtn_fail("Invalid branch type");
856 vtn_emit_cf_list(struct vtn_builder
*b
, struct list_head
*cf_list
,
857 nir_variable
*switch_fall_var
, bool *has_switch_break
,
858 vtn_instruction_handler handler
)
860 list_for_each_entry(struct vtn_cf_node
, node
, cf_list
, link
) {
861 switch (node
->type
) {
862 case vtn_cf_node_type_block
: {
863 struct vtn_block
*block
= (struct vtn_block
*)node
;
865 const uint32_t *block_start
= block
->label
;
866 const uint32_t *block_end
= block
->merge
? block
->merge
:
869 block_start
= vtn_foreach_instruction(b
, block_start
, block_end
,
870 vtn_handle_phis_first_pass
);
872 vtn_foreach_instruction(b
, block_start
, block_end
, handler
);
874 block
->end_nop
= nir_intrinsic_instr_create(b
->nb
.shader
,
876 nir_builder_instr_insert(&b
->nb
, &block
->end_nop
->instr
);
878 if ((*block
->branch
& SpvOpCodeMask
) == SpvOpReturnValue
) {
879 vtn_fail_if(b
->func
->type
->return_type
->base_type
==
881 "Return with a value from a function returning void");
882 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, block
->branch
[1]);
883 const struct glsl_type
*ret_type
=
884 glsl_get_bare_type(b
->func
->type
->return_type
->type
);
885 nir_deref_instr
*ret_deref
=
886 nir_build_deref_cast(&b
->nb
, nir_load_param(&b
->nb
, 0),
887 nir_var_local
, ret_type
);
888 vtn_local_store(b
, src
, ret_deref
);
891 if (block
->branch_type
!= vtn_branch_type_none
) {
892 vtn_emit_branch(b
, block
->branch_type
,
893 switch_fall_var
, has_switch_break
);
899 case vtn_cf_node_type_if
: {
900 struct vtn_if
*vtn_if
= (struct vtn_if
*)node
;
901 bool sw_break
= false;
904 nir_push_if(&b
->nb
, vtn_ssa_value(b
, vtn_if
->condition
)->def
);
905 if (vtn_if
->then_type
== vtn_branch_type_none
) {
906 vtn_emit_cf_list(b
, &vtn_if
->then_body
,
907 switch_fall_var
, &sw_break
, handler
);
909 vtn_emit_branch(b
, vtn_if
->then_type
, switch_fall_var
, &sw_break
);
912 nir_push_else(&b
->nb
, nif
);
913 if (vtn_if
->else_type
== vtn_branch_type_none
) {
914 vtn_emit_cf_list(b
, &vtn_if
->else_body
,
915 switch_fall_var
, &sw_break
, handler
);
917 vtn_emit_branch(b
, vtn_if
->else_type
, switch_fall_var
, &sw_break
);
920 nir_pop_if(&b
->nb
, nif
);
922 /* If we encountered a switch break somewhere inside of the if,
923 * then it would have been handled correctly by calling
924 * emit_cf_list or emit_branch for the interrior. However, we
925 * need to predicate everything following on wether or not we're
929 *has_switch_break
= true;
930 nir_push_if(&b
->nb
, nir_load_var(&b
->nb
, switch_fall_var
));
935 case vtn_cf_node_type_loop
: {
936 struct vtn_loop
*vtn_loop
= (struct vtn_loop
*)node
;
938 nir_loop
*loop
= nir_push_loop(&b
->nb
);
939 vtn_emit_cf_list(b
, &vtn_loop
->body
, NULL
, NULL
, handler
);
941 if (!list_empty(&vtn_loop
->cont_body
)) {
942 /* If we have a non-trivial continue body then we need to put
943 * it at the beginning of the loop with a flag to ensure that
944 * it doesn't get executed in the first iteration.
946 nir_variable
*do_cont
=
947 nir_local_variable_create(b
->nb
.impl
, glsl_bool_type(), "cont");
949 b
->nb
.cursor
= nir_before_cf_node(&loop
->cf_node
);
950 nir_store_var(&b
->nb
, do_cont
, nir_imm_false(&b
->nb
), 1);
952 b
->nb
.cursor
= nir_before_cf_list(&loop
->body
);
955 nir_push_if(&b
->nb
, nir_load_var(&b
->nb
, do_cont
));
957 vtn_emit_cf_list(b
, &vtn_loop
->cont_body
, NULL
, NULL
, handler
);
959 nir_pop_if(&b
->nb
, cont_if
);
961 nir_store_var(&b
->nb
, do_cont
, nir_imm_true(&b
->nb
), 1);
963 b
->has_loop_continue
= true;
966 nir_pop_loop(&b
->nb
, loop
);
970 case vtn_cf_node_type_switch
: {
971 struct vtn_switch
*vtn_switch
= (struct vtn_switch
*)node
;
973 /* First, we create a variable to keep track of whether or not the
974 * switch is still going at any given point. Any switch breaks
975 * will set this variable to false.
977 nir_variable
*fall_var
=
978 nir_local_variable_create(b
->nb
.impl
, glsl_bool_type(), "fall");
979 nir_store_var(&b
->nb
, fall_var
, nir_imm_false(&b
->nb
), 1);
981 /* Next, we gather up all of the conditions. We have to do this
982 * up-front because we also need to build an "any" condition so
983 * that we can use !any for default.
985 const int num_cases
= list_length(&vtn_switch
->cases
);
986 NIR_VLA(nir_ssa_def
*, conditions
, num_cases
);
988 nir_ssa_def
*sel
= vtn_ssa_value(b
, vtn_switch
->selector
)->def
;
989 /* An accumulation of all conditions. Used for the default */
990 nir_ssa_def
*any
= NULL
;
993 list_for_each_entry(struct vtn_case
, cse
, &vtn_switch
->cases
, link
) {
994 if (cse
->is_default
) {
995 conditions
[i
++] = NULL
;
999 nir_ssa_def
*cond
= NULL
;
1000 util_dynarray_foreach(&cse
->values
, uint64_t, val
) {
1001 nir_ssa_def
*imm
= nir_imm_intN_t(&b
->nb
, *val
, sel
->bit_size
);
1002 nir_ssa_def
*is_val
= nir_ieq(&b
->nb
, sel
, imm
);
1004 cond
= cond
? nir_ior(&b
->nb
, cond
, is_val
) : is_val
;
1007 any
= any
? nir_ior(&b
->nb
, any
, cond
) : cond
;
1008 conditions
[i
++] = cond
;
1010 vtn_assert(i
== num_cases
);
1012 /* Now we can walk the list of cases and actually emit code */
1014 list_for_each_entry(struct vtn_case
, cse
, &vtn_switch
->cases
, link
) {
1015 /* Figure out the condition */
1016 nir_ssa_def
*cond
= conditions
[i
++];
1017 if (cse
->is_default
) {
1018 vtn_assert(cond
== NULL
);
1019 cond
= nir_inot(&b
->nb
, any
);
1021 /* Take fallthrough into account */
1022 cond
= nir_ior(&b
->nb
, cond
, nir_load_var(&b
->nb
, fall_var
));
1024 nir_if
*case_if
= nir_push_if(&b
->nb
, cond
);
1026 bool has_break
= false;
1027 nir_store_var(&b
->nb
, fall_var
, nir_imm_true(&b
->nb
), 1);
1028 vtn_emit_cf_list(b
, &cse
->body
, fall_var
, &has_break
, handler
);
1029 (void)has_break
; /* We don't care */
1031 nir_pop_if(&b
->nb
, case_if
);
1033 vtn_assert(i
== num_cases
);
1039 vtn_fail("Invalid CF node type");
1045 vtn_function_emit(struct vtn_builder
*b
, struct vtn_function
*func
,
1046 vtn_instruction_handler instruction_handler
)
1048 nir_builder_init(&b
->nb
, func
->impl
);
1050 b
->nb
.cursor
= nir_after_cf_list(&func
->impl
->body
);
1051 b
->has_loop_continue
= false;
1052 b
->phi_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
1053 _mesa_key_pointer_equal
);
1055 vtn_emit_cf_list(b
, &func
->body
, NULL
, NULL
, instruction_handler
);
1057 vtn_foreach_instruction(b
, func
->start_block
->label
, func
->end
,
1058 vtn_handle_phi_second_pass
);
1060 nir_rematerialize_derefs_in_use_blocks_impl(func
->impl
);
1062 /* Continue blocks for loops get inserted before the body of the loop
1063 * but instructions in the continue may use SSA defs in the loop body.
1064 * Therefore, we need to repair SSA to insert the needed phi nodes.
1066 if (b
->has_loop_continue
)
1067 nir_repair_ssa_impl(func
->impl
);
1069 func
->emitted
= true;