2 * Copyright © 2014 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 * Jason Ekstrand (jason@jlekstrand.net)
29 #include "nir_builder.h"
30 #include "nir_phi_builder.h"
35 struct deref_node
*parent
;
36 const struct glsl_type
*type
;
40 /* Only valid for things that end up in the direct list.
41 * Note that multiple nir_deref_vars may correspond to this node, but they
42 * will all be equivalent, so any is as good as the other.
45 struct exec_node direct_derefs_link
;
51 struct nir_phi_builder_value
*pb_value
;
53 struct deref_node
*wildcard
;
54 struct deref_node
*indirect
;
55 struct deref_node
*children
[0];
58 struct lower_variables_state
{
61 nir_function_impl
*impl
;
63 /* A hash table mapping variables to deref_node data */
64 struct hash_table
*deref_var_nodes
;
66 /* A hash table mapping fully-qualified direct dereferences, i.e.
67 * dereferences with no indirect or wildcard array dereferences, to
70 * At the moment, we only lower loads, stores, and copies that can be
71 * trivially lowered to loads and stores, i.e. copies with no indirects
72 * and no wildcards. If a part of a variable that is being loaded from
73 * and/or stored into is also involved in a copy operation with
74 * wildcards, then we lower that copy operation to loads and stores, but
75 * otherwise we leave copies with wildcards alone. Since the only derefs
76 * used in these loads, stores, and trivial copies are ones with no
77 * wildcards and no indirects, these are precisely the derefs that we
78 * can actually consider lowering.
80 struct exec_list direct_deref_nodes
;
82 /* Controls whether get_deref_node will add variables to the
83 * direct_deref_nodes table. This is turned on when we are initially
84 * scanning for load/store instructions. It is then turned off so we
85 * don't accidentally change the direct_deref_nodes table while we're
86 * iterating throug it.
88 bool add_to_direct_deref_nodes
;
90 struct nir_phi_builder
*phi_builder
;
93 static struct deref_node
*
94 deref_node_create(struct deref_node
*parent
,
95 const struct glsl_type
*type
, nir_shader
*shader
)
97 size_t size
= sizeof(struct deref_node
) +
98 glsl_get_length(type
) * sizeof(struct deref_node
*);
100 struct deref_node
*node
= rzalloc_size(shader
, size
);
102 node
->parent
= parent
;
104 exec_node_init(&node
->direct_derefs_link
);
109 /* Returns the deref node associated with the given variable. This will be
110 * the root of the tree representing all of the derefs of the given variable.
112 static struct deref_node
*
113 get_deref_node_for_var(nir_variable
*var
, struct lower_variables_state
*state
)
115 struct deref_node
*node
;
117 struct hash_entry
*var_entry
=
118 _mesa_hash_table_search(state
->deref_var_nodes
, var
);
121 return var_entry
->data
;
123 node
= deref_node_create(NULL
, var
->type
, state
->dead_ctx
);
124 _mesa_hash_table_insert(state
->deref_var_nodes
, var
, node
);
129 /* Gets the deref_node for the given deref chain and creates it if it
130 * doesn't yet exist. If the deref is fully-qualified and direct and
131 * state->add_to_direct_deref_nodes is true, it will be added to the hash
132 * table of of fully-qualified direct derefs.
134 static struct deref_node
*
135 get_deref_node(nir_deref_var
*deref
, struct lower_variables_state
*state
)
137 bool is_direct
= true;
139 /* Start at the base of the chain. */
140 struct deref_node
*node
= get_deref_node_for_var(deref
->var
, state
);
141 assert(deref
->deref
.type
== node
->type
);
143 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
144 switch (tail
->deref_type
) {
145 case nir_deref_type_struct
: {
146 nir_deref_struct
*deref_struct
= nir_deref_as_struct(tail
);
148 assert(deref_struct
->index
< glsl_get_length(node
->type
));
150 if (node
->children
[deref_struct
->index
] == NULL
)
151 node
->children
[deref_struct
->index
] =
152 deref_node_create(node
, tail
->type
, state
->dead_ctx
);
154 node
= node
->children
[deref_struct
->index
];
158 case nir_deref_type_array
: {
159 nir_deref_array
*arr
= nir_deref_as_array(tail
);
161 switch (arr
->deref_array_type
) {
162 case nir_deref_array_type_direct
:
163 /* This is possible if a loop unrolls and generates an
164 * out-of-bounds offset. We need to handle this at least
165 * somewhat gracefully.
167 if (arr
->base_offset
>= glsl_get_length(node
->type
))
170 if (node
->children
[arr
->base_offset
] == NULL
)
171 node
->children
[arr
->base_offset
] =
172 deref_node_create(node
, tail
->type
, state
->dead_ctx
);
174 node
= node
->children
[arr
->base_offset
];
177 case nir_deref_array_type_indirect
:
178 if (node
->indirect
== NULL
)
179 node
->indirect
= deref_node_create(node
, tail
->type
,
182 node
= node
->indirect
;
186 case nir_deref_array_type_wildcard
:
187 if (node
->wildcard
== NULL
)
188 node
->wildcard
= deref_node_create(node
, tail
->type
,
191 node
= node
->wildcard
;
196 unreachable("Invalid array deref type");
201 unreachable("Invalid deref type");
207 /* Only insert if it isn't already in the list. */
208 if (is_direct
&& state
->add_to_direct_deref_nodes
&&
209 node
->direct_derefs_link
.next
== NULL
) {
211 assert(deref
->var
!= NULL
);
212 exec_list_push_tail(&state
->direct_deref_nodes
,
213 &node
->direct_derefs_link
);
219 /* \sa foreach_deref_node_match */
221 foreach_deref_node_worker(struct deref_node
*node
, nir_deref
*deref
,
222 bool (* cb
)(struct deref_node
*node
,
223 struct lower_variables_state
*state
),
224 struct lower_variables_state
*state
)
226 if (deref
->child
== NULL
) {
227 return cb(node
, state
);
229 switch (deref
->child
->deref_type
) {
230 case nir_deref_type_array
: {
231 nir_deref_array
*arr
= nir_deref_as_array(deref
->child
);
232 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
233 if (node
->children
[arr
->base_offset
] &&
234 !foreach_deref_node_worker(node
->children
[arr
->base_offset
],
235 deref
->child
, cb
, state
))
238 if (node
->wildcard
&&
239 !foreach_deref_node_worker(node
->wildcard
,
240 deref
->child
, cb
, state
))
246 case nir_deref_type_struct
: {
247 nir_deref_struct
*str
= nir_deref_as_struct(deref
->child
);
248 if (node
->children
[str
->index
] &&
249 !foreach_deref_node_worker(node
->children
[str
->index
],
250 deref
->child
, cb
, state
))
257 unreachable("Invalid deref child type");
262 /* Walks over every "matching" deref_node and calls the callback. A node
263 * is considered to "match" if either refers to that deref or matches up t
264 * a wildcard. In other words, the following would match a[6].foo[3].bar:
271 * The given deref must be a full-length and fully qualified (no wildcards
272 * or indirects) deref chain.
275 foreach_deref_node_match(nir_deref_var
*deref
,
276 bool (* cb
)(struct deref_node
*node
,
277 struct lower_variables_state
*state
),
278 struct lower_variables_state
*state
)
280 nir_deref_var var_deref
= *deref
;
281 var_deref
.deref
.child
= NULL
;
282 struct deref_node
*node
= get_deref_node(&var_deref
, state
);
287 return foreach_deref_node_worker(node
, &deref
->deref
, cb
, state
);
290 /* \sa deref_may_be_aliased */
292 deref_may_be_aliased_node(struct deref_node
*node
, nir_deref
*deref
,
293 struct lower_variables_state
*state
)
295 if (deref
->child
== NULL
) {
298 switch (deref
->child
->deref_type
) {
299 case nir_deref_type_array
: {
300 nir_deref_array
*arr
= nir_deref_as_array(deref
->child
);
302 /* This is a child of one of the derefs in direct_deref_nodes,
303 * so we know it is direct.
305 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
307 /* If there is an indirect at this level, we're aliased. */
311 if (node
->children
[arr
->base_offset
] &&
312 deref_may_be_aliased_node(node
->children
[arr
->base_offset
],
313 deref
->child
, state
))
316 if (node
->wildcard
&&
317 deref_may_be_aliased_node(node
->wildcard
, deref
->child
, state
))
323 case nir_deref_type_struct
: {
324 nir_deref_struct
*str
= nir_deref_as_struct(deref
->child
);
325 if (node
->children
[str
->index
]) {
326 return deref_may_be_aliased_node(node
->children
[str
->index
],
327 deref
->child
, state
);
334 unreachable("Invalid nir_deref child type");
339 /* Returns true if there are no indirects that can ever touch this deref.
341 * For example, if the given deref is a[6].foo, then any uses of a[i].foo
342 * would cause this to return false, but a[i].bar would not affect it
343 * because it's a different structure member. A var_copy involving of
344 * a[*].bar also doesn't affect it because that can be lowered to entirely
345 * direct load/stores.
347 * We only support asking this question about fully-qualified derefs.
348 * Obviously, it's pointless to ask this about indirects, but we also
349 * rule-out wildcards. Handling Wildcard dereferences would involve
350 * checking each array index to make sure that there aren't any indirect
354 deref_may_be_aliased(nir_deref_var
*deref
,
355 struct lower_variables_state
*state
)
357 return deref_may_be_aliased_node(get_deref_node_for_var(deref
->var
, state
),
358 &deref
->deref
, state
);
362 register_load_instr(nir_intrinsic_instr
*load_instr
,
363 struct lower_variables_state
*state
)
365 struct deref_node
*node
= get_deref_node(load_instr
->variables
[0], state
);
369 if (node
->loads
== NULL
)
370 node
->loads
= _mesa_set_create(state
->dead_ctx
, _mesa_hash_pointer
,
371 _mesa_key_pointer_equal
);
373 _mesa_set_add(node
->loads
, load_instr
);
377 register_store_instr(nir_intrinsic_instr
*store_instr
,
378 struct lower_variables_state
*state
)
380 struct deref_node
*node
= get_deref_node(store_instr
->variables
[0], state
);
384 if (node
->stores
== NULL
)
385 node
->stores
= _mesa_set_create(state
->dead_ctx
, _mesa_hash_pointer
,
386 _mesa_key_pointer_equal
);
388 _mesa_set_add(node
->stores
, store_instr
);
392 register_copy_instr(nir_intrinsic_instr
*copy_instr
,
393 struct lower_variables_state
*state
)
395 for (unsigned idx
= 0; idx
< 2; idx
++) {
396 struct deref_node
*node
=
397 get_deref_node(copy_instr
->variables
[idx
], state
);
402 if (node
->copies
== NULL
)
403 node
->copies
= _mesa_set_create(state
->dead_ctx
, _mesa_hash_pointer
,
404 _mesa_key_pointer_equal
);
406 _mesa_set_add(node
->copies
, copy_instr
);
411 register_variable_uses(nir_function_impl
*impl
,
412 struct lower_variables_state
*state
)
414 nir_foreach_block(block
, impl
) {
415 nir_foreach_instr_safe(instr
, block
) {
416 if (instr
->type
!= nir_instr_type_intrinsic
)
419 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
421 switch (intrin
->intrinsic
) {
422 case nir_intrinsic_load_var
:
423 register_load_instr(intrin
, state
);
426 case nir_intrinsic_store_var
:
427 register_store_instr(intrin
, state
);
430 case nir_intrinsic_copy_var
:
431 register_copy_instr(intrin
, state
);
441 /* Walks over all of the copy instructions to or from the given deref_node
442 * and lowers them to load/store intrinsics.
445 lower_copies_to_load_store(struct deref_node
*node
,
446 struct lower_variables_state
*state
)
451 struct set_entry
*copy_entry
;
452 set_foreach(node
->copies
, copy_entry
) {
453 nir_intrinsic_instr
*copy
= (void *)copy_entry
->key
;
455 nir_lower_var_copy_instr(copy
, state
->shader
);
457 for (unsigned i
= 0; i
< 2; ++i
) {
458 struct deref_node
*arg_node
=
459 get_deref_node(copy
->variables
[i
], state
);
461 /* Only bother removing copy entries for other nodes */
462 if (arg_node
== NULL
|| arg_node
== node
)
465 struct set_entry
*arg_entry
= _mesa_set_search(arg_node
->copies
, copy
);
467 _mesa_set_remove(arg_node
->copies
, arg_entry
);
470 nir_instr_remove(©
->instr
);
478 /* Performs variable renaming
480 * This algorithm is very similar to the one outlined in "Efficiently
481 * Computing Static Single Assignment Form and the Control Dependence
482 * Graph" by Cytron et al. The primary difference is that we only put one
483 * SSA def on the stack per block.
486 rename_variables(struct lower_variables_state
*state
)
489 nir_builder_init(&b
, state
->impl
);
491 nir_foreach_block(block
, state
->impl
) {
492 nir_foreach_instr_safe(instr
, block
) {
493 if (instr
->type
!= nir_instr_type_intrinsic
)
496 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
498 switch (intrin
->intrinsic
) {
499 case nir_intrinsic_load_var
: {
500 struct deref_node
*node
=
501 get_deref_node(intrin
->variables
[0], state
);
504 /* If we hit this path then we are referencing an invalid
505 * value. Most likely, we unrolled something and are
506 * reading past the end of some array. In any case, this
507 * should result in an undefined value.
509 nir_ssa_undef_instr
*undef
=
510 nir_ssa_undef_instr_create(state
->shader
,
511 intrin
->num_components
,
512 intrin
->dest
.ssa
.bit_size
);
514 nir_instr_insert_before(&intrin
->instr
, &undef
->instr
);
515 nir_instr_remove(&intrin
->instr
);
517 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
518 nir_src_for_ssa(&undef
->def
));
522 if (!node
->lower_to_ssa
)
525 nir_alu_instr
*mov
= nir_alu_instr_create(state
->shader
,
527 mov
->src
[0].src
= nir_src_for_ssa(
528 nir_phi_builder_value_get_block_def(node
->pb_value
, block
));
529 for (unsigned i
= intrin
->num_components
; i
< 4; i
++)
530 mov
->src
[0].swizzle
[i
] = 0;
532 assert(intrin
->dest
.is_ssa
);
534 mov
->dest
.write_mask
= (1 << intrin
->num_components
) - 1;
535 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
,
536 intrin
->num_components
,
537 intrin
->dest
.ssa
.bit_size
, NULL
);
539 nir_instr_insert_before(&intrin
->instr
, &mov
->instr
);
540 nir_instr_remove(&intrin
->instr
);
542 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
543 nir_src_for_ssa(&mov
->dest
.dest
.ssa
));
547 case nir_intrinsic_store_var
: {
548 struct deref_node
*node
=
549 get_deref_node(intrin
->variables
[0], state
);
552 /* Probably an out-of-bounds array store. That should be a
554 nir_instr_remove(&intrin
->instr
);
558 if (!node
->lower_to_ssa
)
561 assert(intrin
->num_components
==
562 glsl_get_vector_elements(node
->type
));
564 assert(intrin
->src
[0].is_ssa
);
566 nir_ssa_def
*new_def
;
567 b
.cursor
= nir_before_instr(&intrin
->instr
);
569 unsigned wrmask
= nir_intrinsic_write_mask(intrin
);
570 if (wrmask
== (1 << intrin
->num_components
) - 1) {
571 /* Whole variable store - just copy the source. Note that
572 * intrin->num_components and intrin->src[0].ssa->num_components
576 for (unsigned i
= 0; i
< 4; i
++)
577 swiz
[i
] = i
< intrin
->num_components
? i
: 0;
579 new_def
= nir_swizzle(&b
, intrin
->src
[0].ssa
, swiz
,
580 intrin
->num_components
, false);
582 nir_ssa_def
*old_def
=
583 nir_phi_builder_value_get_block_def(node
->pb_value
, block
);
584 /* For writemasked store_var intrinsics, we combine the newly
585 * written values with the existing contents of unwritten
586 * channels, creating a new SSA value for the whole vector.
588 nir_ssa_def
*srcs
[4];
589 for (unsigned i
= 0; i
< intrin
->num_components
; i
++) {
590 if (wrmask
& (1 << i
)) {
591 srcs
[i
] = nir_channel(&b
, intrin
->src
[0].ssa
, i
);
593 srcs
[i
] = nir_channel(&b
, old_def
, i
);
596 new_def
= nir_vec(&b
, srcs
, intrin
->num_components
);
599 assert(new_def
->num_components
== intrin
->num_components
);
601 nir_phi_builder_value_set_block_def(node
->pb_value
, block
, new_def
);
602 nir_instr_remove(&intrin
->instr
);
615 /** Implements a pass to lower variable uses to SSA values
617 * This path walks the list of instructions and tries to lower as many
618 * local variable load/store operations to SSA defs and uses as it can.
619 * The process involves four passes:
621 * 1) Iterate over all of the instructions and mark where each local
622 * variable deref is used in a load, store, or copy. While we're at
623 * it, we keep track of all of the fully-qualified (no wildcards) and
624 * fully-direct references we see and store them in the
625 * direct_deref_nodes hash table.
627 * 2) Walk over the list of fully-qualified direct derefs generated in
628 * the previous pass. For each deref, we determine if it can ever be
629 * aliased, i.e. if there is an indirect reference anywhere that may
630 * refer to it. If it cannot be aliased, we mark it for lowering to an
631 * SSA value. At this point, we lower any var_copy instructions that
632 * use the given deref to load/store operations.
634 * 3) Walk over the list of derefs we plan to lower to SSA values and
635 * insert phi nodes as needed.
637 * 4) Perform "variable renaming" by replacing the load/store instructions
638 * with SSA definitions and SSA uses.
641 nir_lower_vars_to_ssa_impl(nir_function_impl
*impl
)
643 struct lower_variables_state state
;
645 state
.shader
= impl
->function
->shader
;
646 state
.dead_ctx
= ralloc_context(state
.shader
);
649 state
.deref_var_nodes
= _mesa_hash_table_create(state
.dead_ctx
,
651 _mesa_key_pointer_equal
);
652 exec_list_make_empty(&state
.direct_deref_nodes
);
654 /* Build the initial deref structures and direct_deref_nodes table */
655 state
.add_to_direct_deref_nodes
= true;
657 register_variable_uses(impl
, &state
);
659 bool progress
= false;
661 nir_metadata_require(impl
, nir_metadata_block_index
);
663 /* We're about to iterate through direct_deref_nodes. Don't modify it. */
664 state
.add_to_direct_deref_nodes
= false;
666 foreach_list_typed_safe(struct deref_node
, node
, direct_derefs_link
,
667 &state
.direct_deref_nodes
) {
668 nir_deref_var
*deref
= node
->deref
;
670 if (deref
->var
->data
.mode
!= nir_var_local
) {
671 exec_node_remove(&node
->direct_derefs_link
);
675 if (deref_may_be_aliased(deref
, &state
)) {
676 exec_node_remove(&node
->direct_derefs_link
);
680 node
->lower_to_ssa
= true;
683 foreach_deref_node_match(deref
, lower_copies_to_load_store
, &state
);
689 nir_metadata_require(impl
, nir_metadata_dominance
);
691 /* We may have lowered some copy instructions to load/store
692 * instructions. The uses from the copy instructions hav already been
693 * removed but we need to rescan to ensure that the uses from the newly
694 * added load/store instructions are registered. We need this
695 * information for phi node insertion below.
697 register_variable_uses(impl
, &state
);
699 state
.phi_builder
= nir_phi_builder_create(state
.impl
);
701 NIR_VLA(BITSET_WORD
, store_blocks
, BITSET_WORDS(state
.impl
->num_blocks
));
702 foreach_list_typed(struct deref_node
, node
, direct_derefs_link
,
703 &state
.direct_deref_nodes
) {
704 if (!node
->lower_to_ssa
)
707 memset(store_blocks
, 0,
708 BITSET_WORDS(state
.impl
->num_blocks
) * sizeof(*store_blocks
));
710 assert(node
->deref
->var
->constant_initializer
== NULL
);
713 struct set_entry
*store_entry
;
714 set_foreach(node
->stores
, store_entry
) {
715 nir_intrinsic_instr
*store
=
716 (nir_intrinsic_instr
*)store_entry
->key
;
717 BITSET_SET(store_blocks
, store
->instr
.block
->index
);
722 nir_phi_builder_add_value(state
.phi_builder
,
723 glsl_get_vector_elements(node
->type
),
724 glsl_get_bit_size(node
->type
),
728 rename_variables(&state
);
730 nir_phi_builder_finish(state
.phi_builder
);
732 nir_metadata_preserve(impl
, nir_metadata_block_index
|
733 nir_metadata_dominance
);
735 ralloc_free(state
.dead_ctx
);
741 nir_lower_vars_to_ssa(nir_shader
*shader
)
743 bool progress
= false;
745 nir_foreach_function(function
, shader
) {
747 progress
|= nir_lower_vars_to_ssa_impl(function
->impl
);