2 * Copyright © 2016 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
25 #include "nir_builder.h"
26 #include "nir_deref.h"
28 #include "util/bitscan.h"
29 #include "util/u_dynarray.h"
31 static const bool debug
= false;
34 * Variable-based copy propagation
36 * Normally, NIR trusts in SSA form for most of its copy-propagation needs.
37 * However, there are cases, especially when dealing with indirects, where SSA
38 * won't help you. This pass is for those times. Specifically, it handles
39 * the following things that the rest of NIR can't:
41 * 1) Copy-propagation on variables that have indirect access. This includes
42 * propagating from indirect stores into indirect loads.
44 * 2) Removal of redundant load_deref intrinsics. We can't trust regular CSE
45 * to do this because it isn't aware of variable writes that may alias the
46 * value and make the former load invalid.
48 * This pass uses an intermediate solution between being local / "per-block"
49 * and a complete data-flow analysis. It follows the control flow graph, and
50 * propagate the available copy information forward, invalidating data at each
53 * Removal of dead writes to variables is handled by another pass.
57 nir_variable_mode modes
;
59 /* Key is deref and value is the uintptr_t with the write mask. */
60 struct hash_table
*derefs
;
67 nir_ssa_def
*def
[NIR_MAX_VEC_COMPONENTS
];
68 uint8_t component
[NIR_MAX_VEC_COMPONENTS
];
70 nir_deref_instr
*deref
;
75 value_set_ssa_components(struct value
*value
, nir_ssa_def
*def
,
76 unsigned num_components
)
79 memset(&value
->ssa
, 0, sizeof(value
->ssa
));
81 for (unsigned i
= 0; i
< num_components
; i
++) {
82 value
->ssa
.def
[i
] = def
;
83 value
->ssa
.component
[i
] = i
;
93 struct copy_prop_var_state
{
94 nir_function_impl
*impl
;
99 /* Maps nodes to vars_written. Used to invalidate copy entries when
100 * visiting each node.
102 struct hash_table
*vars_written_map
;
108 value_equals_store_src(struct value
*value
, nir_intrinsic_instr
*intrin
)
110 assert(intrin
->intrinsic
== nir_intrinsic_store_deref
);
111 uintptr_t write_mask
= nir_intrinsic_write_mask(intrin
);
113 for (unsigned i
= 0; i
< intrin
->num_components
; i
++) {
114 if ((write_mask
& (1 << i
)) &&
115 (value
->ssa
.def
[i
] != intrin
->src
[1].ssa
||
116 value
->ssa
.component
[i
] != i
))
123 static struct vars_written
*
124 create_vars_written(struct copy_prop_var_state
*state
)
126 struct vars_written
*written
=
127 linear_zalloc_child(state
->lin_ctx
, sizeof(struct vars_written
));
128 written
->derefs
= _mesa_pointer_hash_table_create(state
->mem_ctx
);
133 gather_vars_written(struct copy_prop_var_state
*state
,
134 struct vars_written
*written
,
135 nir_cf_node
*cf_node
)
137 struct vars_written
*new_written
= NULL
;
139 switch (cf_node
->type
) {
140 case nir_cf_node_function
: {
141 nir_function_impl
*impl
= nir_cf_node_as_function(cf_node
);
142 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &impl
->body
)
143 gather_vars_written(state
, NULL
, cf_node
);
147 case nir_cf_node_block
: {
151 nir_block
*block
= nir_cf_node_as_block(cf_node
);
152 nir_foreach_instr(instr
, block
) {
153 if (instr
->type
== nir_instr_type_call
) {
154 written
->modes
|= nir_var_shader_out
|
155 nir_var_shader_temp
|
156 nir_var_function_temp
|
162 if (instr
->type
!= nir_instr_type_intrinsic
)
165 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
166 switch (intrin
->intrinsic
) {
167 case nir_intrinsic_control_barrier
:
168 case nir_intrinsic_memory_barrier
:
169 written
->modes
|= nir_var_shader_out
|
174 case nir_intrinsic_scoped_memory_barrier
:
175 if (nir_intrinsic_memory_semantics(intrin
) & NIR_MEMORY_ACQUIRE
)
176 written
->modes
|= nir_intrinsic_memory_modes(intrin
);
179 case nir_intrinsic_emit_vertex
:
180 case nir_intrinsic_emit_vertex_with_counter
:
181 written
->modes
= nir_var_shader_out
;
184 case nir_intrinsic_deref_atomic_add
:
185 case nir_intrinsic_deref_atomic_imin
:
186 case nir_intrinsic_deref_atomic_umin
:
187 case nir_intrinsic_deref_atomic_imax
:
188 case nir_intrinsic_deref_atomic_umax
:
189 case nir_intrinsic_deref_atomic_and
:
190 case nir_intrinsic_deref_atomic_or
:
191 case nir_intrinsic_deref_atomic_xor
:
192 case nir_intrinsic_deref_atomic_exchange
:
193 case nir_intrinsic_deref_atomic_comp_swap
:
194 case nir_intrinsic_store_deref
:
195 case nir_intrinsic_copy_deref
: {
196 /* Destination in all of store_deref, copy_deref and the atomics is src[0]. */
197 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
199 uintptr_t mask
= intrin
->intrinsic
== nir_intrinsic_store_deref
?
200 nir_intrinsic_write_mask(intrin
) : (1 << glsl_get_vector_elements(dst
->type
)) - 1;
202 struct hash_entry
*ht_entry
= _mesa_hash_table_search(written
->derefs
, dst
);
204 ht_entry
->data
= (void *)(mask
| (uintptr_t)ht_entry
->data
);
206 _mesa_hash_table_insert(written
->derefs
, dst
, (void *)mask
);
219 case nir_cf_node_if
: {
220 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
222 new_written
= create_vars_written(state
);
224 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->then_list
)
225 gather_vars_written(state
, new_written
, cf_node
);
227 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->else_list
)
228 gather_vars_written(state
, new_written
, cf_node
);
233 case nir_cf_node_loop
: {
234 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
236 new_written
= create_vars_written(state
);
238 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &loop
->body
)
239 gather_vars_written(state
, new_written
, cf_node
);
245 unreachable("Invalid CF node type");
249 /* Merge new information to the parent control flow node. */
251 written
->modes
|= new_written
->modes
;
252 hash_table_foreach(new_written
->derefs
, new_entry
) {
253 struct hash_entry
*old_entry
=
254 _mesa_hash_table_search_pre_hashed(written
->derefs
, new_entry
->hash
,
257 nir_component_mask_t merged
= (uintptr_t) new_entry
->data
|
258 (uintptr_t) old_entry
->data
;
259 old_entry
->data
= (void *) ((uintptr_t) merged
);
261 _mesa_hash_table_insert_pre_hashed(written
->derefs
, new_entry
->hash
,
262 new_entry
->key
, new_entry
->data
);
266 _mesa_hash_table_insert(state
->vars_written_map
, cf_node
, new_written
);
270 static struct copy_entry
*
271 copy_entry_create(struct util_dynarray
*copies
,
272 nir_deref_instr
*dst_deref
)
274 struct copy_entry new_entry
= {
277 util_dynarray_append(copies
, struct copy_entry
, new_entry
);
278 return util_dynarray_top_ptr(copies
, struct copy_entry
);
281 /* Remove copy entry by swapping it with the last element and reducing the
282 * size. If used inside an iteration on copies, it must be a reverse
283 * (backwards) iteration. It is safe to use in those cases because the swap
284 * will not affect the rest of the iteration.
287 copy_entry_remove(struct util_dynarray
*copies
,
288 struct copy_entry
*entry
)
290 /* This also works when removing the last element since pop don't shrink
291 * the memory used by the array, so the swap is useless but not invalid.
293 *entry
= util_dynarray_pop(copies
, struct copy_entry
);
297 is_array_deref_of_vector(nir_deref_instr
*deref
)
299 if (deref
->deref_type
!= nir_deref_type_array
)
301 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
302 return glsl_type_is_vector(parent
->type
);
305 static struct copy_entry
*
306 lookup_entry_for_deref(struct util_dynarray
*copies
,
307 nir_deref_instr
*deref
,
308 nir_deref_compare_result allowed_comparisons
)
310 struct copy_entry
*entry
= NULL
;
311 util_dynarray_foreach(copies
, struct copy_entry
, iter
) {
312 nir_deref_compare_result result
= nir_compare_derefs(iter
->dst
, deref
);
313 if (result
& allowed_comparisons
) {
315 if (result
& nir_derefs_equal_bit
)
317 /* Keep looking in case we have an equal match later in the array. */
323 static struct copy_entry
*
324 lookup_entry_and_kill_aliases(struct util_dynarray
*copies
,
325 nir_deref_instr
*deref
,
328 /* TODO: Take into account the write_mask. */
330 nir_deref_instr
*dst_match
= NULL
;
331 util_dynarray_foreach_reverse(copies
, struct copy_entry
, iter
) {
332 if (!iter
->src
.is_ssa
) {
333 /* If this write aliases the source of some entry, get rid of it */
334 if (nir_compare_derefs(iter
->src
.deref
, deref
) & nir_derefs_may_alias_bit
) {
335 copy_entry_remove(copies
, iter
);
340 nir_deref_compare_result comp
= nir_compare_derefs(iter
->dst
, deref
);
342 if (comp
& nir_derefs_equal_bit
) {
343 /* Removing entries invalidate previous iter pointers, so we'll
344 * collect the matching entry later. Just make sure it is unique.
347 dst_match
= iter
->dst
;
348 } else if (comp
& nir_derefs_may_alias_bit
) {
349 copy_entry_remove(copies
, iter
);
353 struct copy_entry
*entry
= NULL
;
355 util_dynarray_foreach(copies
, struct copy_entry
, iter
) {
356 if (iter
->dst
== dst_match
) {
367 kill_aliases(struct util_dynarray
*copies
,
368 nir_deref_instr
*deref
,
371 /* TODO: Take into account the write_mask. */
373 struct copy_entry
*entry
=
374 lookup_entry_and_kill_aliases(copies
, deref
, write_mask
);
376 copy_entry_remove(copies
, entry
);
379 static struct copy_entry
*
380 get_entry_and_kill_aliases(struct util_dynarray
*copies
,
381 nir_deref_instr
*deref
,
384 /* TODO: Take into account the write_mask. */
386 struct copy_entry
*entry
=
387 lookup_entry_and_kill_aliases(copies
, deref
, write_mask
);
390 entry
= copy_entry_create(copies
, deref
);
396 apply_barrier_for_modes(struct util_dynarray
*copies
,
397 nir_variable_mode modes
)
399 util_dynarray_foreach_reverse(copies
, struct copy_entry
, iter
) {
400 if ((iter
->dst
->mode
& modes
) ||
401 (!iter
->src
.is_ssa
&& (iter
->src
.deref
->mode
& modes
)))
402 copy_entry_remove(copies
, iter
);
407 value_set_from_value(struct value
*value
, const struct value
*from
,
408 unsigned base_index
, unsigned write_mask
)
410 /* We can't have non-zero indexes with non-trivial write masks */
411 assert(base_index
== 0 || write_mask
== 1);
414 /* Clear value if it was being used as non-SSA. */
416 memset(&value
->ssa
, 0, sizeof(value
->ssa
));
417 value
->is_ssa
= true;
418 /* Only overwrite the written components */
419 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
420 if (write_mask
& (1 << i
)) {
421 value
->ssa
.def
[base_index
+ i
] = from
->ssa
.def
[i
];
422 value
->ssa
.component
[base_index
+ i
] = from
->ssa
.component
[i
];
426 /* Non-ssa stores always write everything */
427 value
->is_ssa
= false;
428 value
->deref
= from
->deref
;
432 /* Try to load a single element of a vector from the copy_entry. If the data
433 * isn't available, just let the original intrinsic do the work.
436 load_element_from_ssa_entry_value(struct copy_prop_var_state
*state
,
437 struct copy_entry
*entry
,
438 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
439 struct value
*value
, unsigned index
)
441 assert(index
< glsl_get_vector_elements(entry
->dst
->type
));
443 /* We don't have the element available, so let the instruction do the work. */
444 if (!entry
->src
.ssa
.def
[index
])
447 b
->cursor
= nir_instr_remove(&intrin
->instr
);
448 intrin
->instr
.block
= NULL
;
450 assert(entry
->src
.ssa
.component
[index
] <
451 entry
->src
.ssa
.def
[index
]->num_components
);
452 nir_ssa_def
*def
= nir_channel(b
, entry
->src
.ssa
.def
[index
],
453 entry
->src
.ssa
.component
[index
]);
455 *value
= (struct value
) {
468 /* Do a "load" from an SSA-based entry return it in "value" as a value with a
469 * single SSA def. Because an entry could reference multiple different SSA
470 * defs, a vecN operation may be inserted to combine them into a single SSA
471 * def before handing it back to the caller. If the load instruction is no
472 * longer needed, it is removed and nir_instr::block is set to NULL. (It is
473 * possible, in some cases, for the load to be used in the vecN operation in
474 * which case it isn't deleted.)
477 load_from_ssa_entry_value(struct copy_prop_var_state
*state
,
478 struct copy_entry
*entry
,
479 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
480 nir_deref_instr
*src
, struct value
*value
)
482 if (is_array_deref_of_vector(src
)) {
483 if (nir_src_is_const(src
->arr
.index
)) {
484 return load_element_from_ssa_entry_value(state
, entry
, b
, intrin
, value
,
485 nir_src_as_uint(src
->arr
.index
));
488 /* An SSA copy_entry for the vector won't help indirect load. */
489 if (glsl_type_is_vector(entry
->dst
->type
)) {
490 assert(entry
->dst
->type
== nir_deref_instr_parent(src
)->type
);
491 /* TODO: If all SSA entries are there, try an if-ladder. */
497 assert(value
->is_ssa
);
499 const struct glsl_type
*type
= entry
->dst
->type
;
500 unsigned num_components
= glsl_get_vector_elements(type
);
502 nir_component_mask_t available
= 0;
503 bool all_same
= true;
504 for (unsigned i
= 0; i
< num_components
; i
++) {
505 if (value
->ssa
.def
[i
])
506 available
|= (1 << i
);
508 if (value
->ssa
.def
[i
] != value
->ssa
.def
[0])
511 if (value
->ssa
.component
[i
] != i
)
516 /* Our work here is done */
517 b
->cursor
= nir_instr_remove(&intrin
->instr
);
518 intrin
->instr
.block
= NULL
;
522 if (available
!= (1 << num_components
) - 1 &&
523 intrin
->intrinsic
== nir_intrinsic_load_deref
&&
524 (available
& nir_ssa_def_components_read(&intrin
->dest
.ssa
)) == 0) {
525 /* If none of the components read are available as SSA values, then we
526 * should just bail. Otherwise, we would end up replacing the uses of
527 * the load_deref a vecN() that just gathers up its components.
532 b
->cursor
= nir_after_instr(&intrin
->instr
);
534 nir_ssa_def
*load_def
=
535 intrin
->intrinsic
== nir_intrinsic_load_deref
? &intrin
->dest
.ssa
: NULL
;
537 bool keep_intrin
= false;
538 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
539 for (unsigned i
= 0; i
< num_components
; i
++) {
540 if (value
->ssa
.def
[i
]) {
541 comps
[i
] = nir_channel(b
, value
->ssa
.def
[i
], value
->ssa
.component
[i
]);
543 /* We don't have anything for this component in our
544 * list. Just re-use a channel from the load.
546 if (load_def
== NULL
)
547 load_def
= nir_load_deref(b
, entry
->dst
);
549 if (load_def
->parent_instr
== &intrin
->instr
)
552 comps
[i
] = nir_channel(b
, load_def
, i
);
556 nir_ssa_def
*vec
= nir_vec(b
, comps
, num_components
);
557 value_set_ssa_components(value
, vec
, num_components
);
560 /* Removing this instruction should not touch the cursor because we
561 * created the cursor after the intrinsic and have added at least one
562 * instruction (the vec) since then.
564 assert(b
->cursor
.instr
!= &intrin
->instr
);
565 nir_instr_remove(&intrin
->instr
);
566 intrin
->instr
.block
= NULL
;
573 * Specialize the wildcards in a deref chain
575 * This function returns a deref chain identical to \param deref except that
576 * some of its wildcards are replaced with indices from \param specific. The
577 * process is guided by \param guide which references the same type as \param
578 * specific but has the same wildcard array lengths as \param deref.
580 static nir_deref_instr
*
581 specialize_wildcards(nir_builder
*b
,
582 nir_deref_path
*deref
,
583 nir_deref_path
*guide
,
584 nir_deref_path
*specific
)
586 nir_deref_instr
**deref_p
= &deref
->path
[1];
587 nir_deref_instr
**guide_p
= &guide
->path
[1];
588 nir_deref_instr
**spec_p
= &specific
->path
[1];
589 nir_deref_instr
*ret_tail
= deref
->path
[0];
590 for (; *deref_p
; deref_p
++) {
591 if ((*deref_p
)->deref_type
== nir_deref_type_array_wildcard
) {
592 /* This is where things get tricky. We have to search through
593 * the entry deref to find its corresponding wildcard and fill
594 * this slot in with the value from the src.
597 (*guide_p
)->deref_type
!= nir_deref_type_array_wildcard
) {
601 assert(*guide_p
&& *spec_p
);
603 ret_tail
= nir_build_deref_follower(b
, ret_tail
, *spec_p
);
608 ret_tail
= nir_build_deref_follower(b
, ret_tail
, *deref_p
);
615 /* Do a "load" from an deref-based entry return it in "value" as a value. The
616 * deref returned in "value" will always be a fresh copy so the caller can
617 * steal it and assign it to the instruction directly without copying it
621 load_from_deref_entry_value(struct copy_prop_var_state
*state
,
622 struct copy_entry
*entry
,
623 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
624 nir_deref_instr
*src
, struct value
*value
)
628 b
->cursor
= nir_instr_remove(&intrin
->instr
);
630 nir_deref_path entry_dst_path
, src_path
;
631 nir_deref_path_init(&entry_dst_path
, entry
->dst
, state
->mem_ctx
);
632 nir_deref_path_init(&src_path
, src
, state
->mem_ctx
);
634 bool need_to_specialize_wildcards
= false;
635 nir_deref_instr
**entry_p
= &entry_dst_path
.path
[1];
636 nir_deref_instr
**src_p
= &src_path
.path
[1];
637 while (*entry_p
&& *src_p
) {
638 nir_deref_instr
*entry_tail
= *entry_p
++;
639 nir_deref_instr
*src_tail
= *src_p
++;
641 if (src_tail
->deref_type
== nir_deref_type_array
&&
642 entry_tail
->deref_type
== nir_deref_type_array_wildcard
)
643 need_to_specialize_wildcards
= true;
646 /* If the entry deref is longer than the source deref then it refers to a
647 * smaller type and we can't source from it.
649 assert(*entry_p
== NULL
);
651 if (need_to_specialize_wildcards
) {
652 /* The entry has some wildcards that are not in src. This means we need
653 * to construct a new deref based on the entry but using the wildcards
654 * from the source and guided by the entry dst. Oof.
656 nir_deref_path entry_src_path
;
657 nir_deref_path_init(&entry_src_path
, entry
->src
.deref
, state
->mem_ctx
);
658 value
->deref
= specialize_wildcards(b
, &entry_src_path
,
659 &entry_dst_path
, &src_path
);
660 nir_deref_path_finish(&entry_src_path
);
663 /* If our source deref is longer than the entry deref, that's ok because
664 * it just means the entry deref needs to be extended a bit.
667 nir_deref_instr
*src_tail
= *src_p
++;
668 value
->deref
= nir_build_deref_follower(b
, value
->deref
, src_tail
);
671 nir_deref_path_finish(&entry_dst_path
);
672 nir_deref_path_finish(&src_path
);
678 try_load_from_entry(struct copy_prop_var_state
*state
, struct copy_entry
*entry
,
679 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
680 nir_deref_instr
*src
, struct value
*value
)
685 if (entry
->src
.is_ssa
) {
686 return load_from_ssa_entry_value(state
, entry
, b
, intrin
, src
, value
);
688 return load_from_deref_entry_value(state
, entry
, b
, intrin
, src
, value
);
693 invalidate_copies_for_cf_node(struct copy_prop_var_state
*state
,
694 struct util_dynarray
*copies
,
695 nir_cf_node
*cf_node
)
697 struct hash_entry
*ht_entry
= _mesa_hash_table_search(state
->vars_written_map
, cf_node
);
700 struct vars_written
*written
= ht_entry
->data
;
701 if (written
->modes
) {
702 util_dynarray_foreach_reverse(copies
, struct copy_entry
, entry
) {
703 if (entry
->dst
->mode
& written
->modes
)
704 copy_entry_remove(copies
, entry
);
708 hash_table_foreach (written
->derefs
, entry
) {
709 nir_deref_instr
*deref_written
= (nir_deref_instr
*)entry
->key
;
710 kill_aliases(copies
, deref_written
, (uintptr_t)entry
->data
);
715 print_value(struct value
*value
, unsigned num_components
)
717 if (!value
->is_ssa
) {
718 printf(" %s ", glsl_get_type_name(value
->deref
->type
));
719 nir_print_deref(value
->deref
, stdout
);
723 bool same_ssa
= true;
724 for (unsigned i
= 0; i
< num_components
; i
++) {
725 if (value
->ssa
.component
[i
] != i
||
726 (i
> 0 && value
->ssa
.def
[i
- 1] != value
->ssa
.def
[i
])) {
732 printf(" ssa_%d", value
->ssa
.def
[0]->index
);
734 for (int i
= 0; i
< num_components
; i
++) {
735 if (value
->ssa
.def
[i
])
736 printf(" ssa_%d[%u]", value
->ssa
.def
[i
]->index
, value
->ssa
.component
[i
]);
744 print_copy_entry(struct copy_entry
*entry
)
746 printf(" %s ", glsl_get_type_name(entry
->dst
->type
));
747 nir_print_deref(entry
->dst
, stdout
);
750 unsigned num_components
= glsl_get_vector_elements(entry
->dst
->type
);
751 print_value(&entry
->src
, num_components
);
756 dump_instr(nir_instr
*instr
)
759 nir_print_instr(instr
, stdout
);
764 dump_copy_entries(struct util_dynarray
*copies
)
766 util_dynarray_foreach(copies
, struct copy_entry
, iter
)
767 print_copy_entry(iter
);
772 copy_prop_vars_block(struct copy_prop_var_state
*state
,
773 nir_builder
*b
, nir_block
*block
,
774 struct util_dynarray
*copies
)
777 printf("# block%d\n", block
->index
);
778 dump_copy_entries(copies
);
781 nir_foreach_instr_safe(instr
, block
) {
782 if (debug
&& instr
->type
== nir_instr_type_deref
)
785 if (instr
->type
== nir_instr_type_call
) {
786 if (debug
) dump_instr(instr
);
787 apply_barrier_for_modes(copies
, nir_var_shader_out
|
788 nir_var_shader_temp
|
789 nir_var_function_temp
|
792 if (debug
) dump_copy_entries(copies
);
796 if (instr
->type
!= nir_instr_type_intrinsic
)
799 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
800 switch (intrin
->intrinsic
) {
801 case nir_intrinsic_control_barrier
:
802 case nir_intrinsic_memory_barrier
:
803 if (debug
) dump_instr(instr
);
805 apply_barrier_for_modes(copies
, nir_var_shader_out
|
810 case nir_intrinsic_memory_barrier_buffer
:
811 case nir_intrinsic_memory_barrier_atomic_counter
:
812 if (debug
) dump_instr(instr
);
814 apply_barrier_for_modes(copies
, nir_var_mem_ssbo
);
817 case nir_intrinsic_memory_barrier_shared
:
818 if (debug
) dump_instr(instr
);
820 apply_barrier_for_modes(copies
, nir_var_mem_shared
);
823 case nir_intrinsic_memory_barrier_tcs_patch
:
824 if (debug
) dump_instr(instr
);
826 apply_barrier_for_modes(copies
, nir_var_shader_out
);
829 case nir_intrinsic_scoped_memory_barrier
:
830 if (debug
) dump_instr(instr
);
832 if (nir_intrinsic_memory_semantics(intrin
) & NIR_MEMORY_ACQUIRE
)
833 apply_barrier_for_modes(copies
, nir_intrinsic_memory_modes(intrin
));
836 case nir_intrinsic_emit_vertex
:
837 case nir_intrinsic_emit_vertex_with_counter
:
838 if (debug
) dump_instr(instr
);
840 apply_barrier_for_modes(copies
, nir_var_shader_out
);
843 case nir_intrinsic_load_deref
: {
844 if (debug
) dump_instr(instr
);
846 if (nir_intrinsic_access(intrin
) & ACCESS_VOLATILE
)
849 nir_deref_instr
*src
= nir_src_as_deref(intrin
->src
[0]);
851 /* Direct array_derefs of vectors operate on the vectors (the parent
852 * deref). Indirects will be handled like other derefs.
855 nir_deref_instr
*vec_src
= src
;
856 if (is_array_deref_of_vector(src
) && nir_src_is_const(src
->arr
.index
)) {
857 vec_src
= nir_deref_instr_parent(src
);
858 unsigned vec_comps
= glsl_get_vector_elements(vec_src
->type
);
859 vec_index
= nir_src_as_uint(src
->arr
.index
);
861 /* Loading from an invalid index yields an undef */
862 if (vec_index
>= vec_comps
) {
863 b
->cursor
= nir_instr_remove(instr
);
864 nir_ssa_def
*u
= nir_ssa_undef(b
, 1, intrin
->dest
.ssa
.bit_size
);
865 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
, nir_src_for_ssa(u
));
870 struct copy_entry
*src_entry
=
871 lookup_entry_for_deref(copies
, src
, nir_derefs_a_contains_b_bit
);
872 struct value value
= {0};
873 if (try_load_from_entry(state
, src_entry
, b
, intrin
, src
, &value
)) {
875 /* lookup_load has already ensured that we get a single SSA
876 * value that has all of the channels. We just have to do the
877 * rewrite operation. Note for array derefs of vectors, the
880 if (intrin
->instr
.block
) {
881 /* The lookup left our instruction in-place. This means it
882 * must have used it to vec up a bunch of different sources.
883 * We need to be careful when rewriting uses so we don't
884 * rewrite the vecN itself.
886 nir_ssa_def_rewrite_uses_after(&intrin
->dest
.ssa
,
887 nir_src_for_ssa(value
.ssa
.def
[0]),
888 value
.ssa
.def
[0]->parent_instr
);
890 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
891 nir_src_for_ssa(value
.ssa
.def
[0]));
894 /* We're turning it into a load of a different variable */
895 intrin
->src
[0] = nir_src_for_ssa(&value
.deref
->dest
.ssa
);
897 /* Put it back in again. */
898 nir_builder_instr_insert(b
, instr
);
899 value_set_ssa_components(&value
, &intrin
->dest
.ssa
,
900 intrin
->num_components
);
902 state
->progress
= true;
904 value_set_ssa_components(&value
, &intrin
->dest
.ssa
,
905 intrin
->num_components
);
908 /* Now that we have a value, we're going to store it back so that we
909 * have the right value next time we come looking for it. In order
910 * to do this, we need an exact match, not just something that
911 * contains what we're looking for.
913 struct copy_entry
*entry
=
914 lookup_entry_for_deref(copies
, vec_src
, nir_derefs_equal_bit
);
916 entry
= copy_entry_create(copies
, vec_src
);
918 /* Update the entry with the value of the load. This way
919 * we can potentially remove subsequent loads.
921 value_set_from_value(&entry
->src
, &value
, vec_index
,
922 (1 << intrin
->num_components
) - 1);
926 case nir_intrinsic_store_deref
: {
927 if (debug
) dump_instr(instr
);
929 if (nir_intrinsic_access(intrin
) & ACCESS_VOLATILE
)
932 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
933 assert(glsl_type_is_vector_or_scalar(dst
->type
));
935 /* Direct array_derefs of vectors operate on the vectors (the parent
936 * deref). Indirects will be handled like other derefs.
939 nir_deref_instr
*vec_dst
= dst
;
940 if (is_array_deref_of_vector(dst
) && nir_src_is_const(dst
->arr
.index
)) {
941 vec_dst
= nir_deref_instr_parent(dst
);
942 unsigned vec_comps
= glsl_get_vector_elements(vec_dst
->type
);
944 vec_index
= nir_src_as_uint(dst
->arr
.index
);
946 /* Storing to an invalid index is a no-op. */
947 if (vec_index
>= vec_comps
) {
948 nir_instr_remove(instr
);
953 struct copy_entry
*entry
=
954 lookup_entry_for_deref(copies
, dst
, nir_derefs_equal_bit
);
955 if (entry
&& value_equals_store_src(&entry
->src
, intrin
)) {
956 /* If we are storing the value from a load of the same var the
957 * store is redundant so remove it.
959 nir_instr_remove(instr
);
961 struct value value
= {0};
962 value_set_ssa_components(&value
, intrin
->src
[1].ssa
,
963 intrin
->num_components
);
964 unsigned wrmask
= nir_intrinsic_write_mask(intrin
);
965 struct copy_entry
*entry
=
966 get_entry_and_kill_aliases(copies
, vec_dst
, wrmask
);
967 value_set_from_value(&entry
->src
, &value
, vec_index
, wrmask
);
973 case nir_intrinsic_copy_deref
: {
974 if (debug
) dump_instr(instr
);
976 if ((nir_intrinsic_src_access(intrin
) & ACCESS_VOLATILE
) ||
977 (nir_intrinsic_dst_access(intrin
) & ACCESS_VOLATILE
))
980 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
981 nir_deref_instr
*src
= nir_src_as_deref(intrin
->src
[1]);
983 if (nir_compare_derefs(src
, dst
) & nir_derefs_equal_bit
) {
984 /* This is a no-op self-copy. Get rid of it */
985 nir_instr_remove(instr
);
989 /* The copy_deref intrinsic doesn't keep track of num_components, so
992 unsigned num_components
= glsl_get_vector_elements(dst
->type
);
993 unsigned full_mask
= (1 << num_components
) - 1;
995 /* Copy of direct array derefs of vectors are not handled. Just
996 * invalidate what's written and bail.
998 if ((is_array_deref_of_vector(src
) && nir_src_is_const(src
->arr
.index
)) ||
999 (is_array_deref_of_vector(dst
) && nir_src_is_const(dst
->arr
.index
))) {
1000 kill_aliases(copies
, dst
, full_mask
);
1004 struct copy_entry
*src_entry
=
1005 lookup_entry_for_deref(copies
, src
, nir_derefs_a_contains_b_bit
);
1007 if (try_load_from_entry(state
, src_entry
, b
, intrin
, src
, &value
)) {
1008 /* If load works, intrin (the copy_deref) is removed. */
1010 nir_store_deref(b
, dst
, value
.ssa
.def
[0], full_mask
);
1012 /* If this would be a no-op self-copy, don't bother. */
1013 if (nir_compare_derefs(value
.deref
, dst
) & nir_derefs_equal_bit
)
1016 /* Just turn it into a copy of a different deref */
1017 intrin
->src
[1] = nir_src_for_ssa(&value
.deref
->dest
.ssa
);
1019 /* Put it back in again. */
1020 nir_builder_instr_insert(b
, instr
);
1023 state
->progress
= true;
1025 value
= (struct value
) {
1031 nir_variable
*src_var
= nir_deref_instr_get_variable(src
);
1032 if (src_var
&& src_var
->data
.cannot_coalesce
) {
1033 /* The source cannot be coaleseced, which means we can't propagate
1039 struct copy_entry
*dst_entry
=
1040 get_entry_and_kill_aliases(copies
, dst
, full_mask
);
1041 value_set_from_value(&dst_entry
->src
, &value
, 0, full_mask
);
1045 case nir_intrinsic_deref_atomic_add
:
1046 case nir_intrinsic_deref_atomic_imin
:
1047 case nir_intrinsic_deref_atomic_umin
:
1048 case nir_intrinsic_deref_atomic_imax
:
1049 case nir_intrinsic_deref_atomic_umax
:
1050 case nir_intrinsic_deref_atomic_and
:
1051 case nir_intrinsic_deref_atomic_or
:
1052 case nir_intrinsic_deref_atomic_xor
:
1053 case nir_intrinsic_deref_atomic_exchange
:
1054 case nir_intrinsic_deref_atomic_comp_swap
:
1055 if (debug
) dump_instr(instr
);
1057 if (nir_intrinsic_access(intrin
) & ACCESS_VOLATILE
)
1060 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
1061 unsigned num_components
= glsl_get_vector_elements(dst
->type
);
1062 unsigned full_mask
= (1 << num_components
) - 1;
1063 kill_aliases(copies
, dst
, full_mask
);
1067 continue; /* To skip the debug below. */
1070 if (debug
) dump_copy_entries(copies
);
1075 copy_prop_vars_cf_node(struct copy_prop_var_state
*state
,
1076 struct util_dynarray
*copies
,
1077 nir_cf_node
*cf_node
)
1079 switch (cf_node
->type
) {
1080 case nir_cf_node_function
: {
1081 nir_function_impl
*impl
= nir_cf_node_as_function(cf_node
);
1083 struct util_dynarray impl_copies
;
1084 util_dynarray_init(&impl_copies
, state
->mem_ctx
);
1086 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &impl
->body
)
1087 copy_prop_vars_cf_node(state
, &impl_copies
, cf_node
);
1092 case nir_cf_node_block
: {
1093 nir_block
*block
= nir_cf_node_as_block(cf_node
);
1095 nir_builder_init(&b
, state
->impl
);
1096 copy_prop_vars_block(state
, &b
, block
, copies
);
1100 case nir_cf_node_if
: {
1101 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
1103 /* Clone the copies for each branch of the if statement. The idea is
1104 * that they both see the same state of available copies, but do not
1105 * interfere to each other.
1108 struct util_dynarray then_copies
;
1109 util_dynarray_clone(&then_copies
, state
->mem_ctx
, copies
);
1111 struct util_dynarray else_copies
;
1112 util_dynarray_clone(&else_copies
, state
->mem_ctx
, copies
);
1114 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->then_list
)
1115 copy_prop_vars_cf_node(state
, &then_copies
, cf_node
);
1117 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->else_list
)
1118 copy_prop_vars_cf_node(state
, &else_copies
, cf_node
);
1120 /* Both branches copies can be ignored, since the effect of running both
1121 * branches was captured in the first pass that collects vars_written.
1124 invalidate_copies_for_cf_node(state
, copies
, cf_node
);
1129 case nir_cf_node_loop
: {
1130 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
1132 /* Invalidate before cloning the copies for the loop, since the loop
1133 * body can be executed more than once.
1136 invalidate_copies_for_cf_node(state
, copies
, cf_node
);
1138 struct util_dynarray loop_copies
;
1139 util_dynarray_clone(&loop_copies
, state
->mem_ctx
, copies
);
1141 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &loop
->body
)
1142 copy_prop_vars_cf_node(state
, &loop_copies
, cf_node
);
1148 unreachable("Invalid CF node type");
1153 nir_copy_prop_vars_impl(nir_function_impl
*impl
)
1155 void *mem_ctx
= ralloc_context(NULL
);
1158 nir_metadata_require(impl
, nir_metadata_block_index
);
1159 printf("## nir_copy_prop_vars_impl for %s\n", impl
->function
->name
);
1162 struct copy_prop_var_state state
= {
1165 .lin_ctx
= linear_zalloc_parent(mem_ctx
, 0),
1167 .vars_written_map
= _mesa_pointer_hash_table_create(mem_ctx
),
1170 gather_vars_written(&state
, NULL
, &impl
->cf_node
);
1172 copy_prop_vars_cf_node(&state
, NULL
, &impl
->cf_node
);
1174 if (state
.progress
) {
1175 nir_metadata_preserve(impl
, nir_metadata_block_index
|
1176 nir_metadata_dominance
);
1179 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
1183 ralloc_free(mem_ctx
);
1184 return state
.progress
;
1188 nir_opt_copy_prop_vars(nir_shader
*shader
)
1190 bool progress
= false;
1192 nir_foreach_function(function
, shader
) {
1193 if (!function
->impl
)
1195 progress
|= nir_copy_prop_vars_impl(function
->impl
);