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_barrier
:
168 case nir_intrinsic_memory_barrier
:
169 written
->modes
|= nir_var_shader_out
|
174 case nir_intrinsic_emit_vertex
:
175 case nir_intrinsic_emit_vertex_with_counter
:
176 written
->modes
= nir_var_shader_out
;
179 case nir_intrinsic_deref_atomic_add
:
180 case nir_intrinsic_deref_atomic_imin
:
181 case nir_intrinsic_deref_atomic_umin
:
182 case nir_intrinsic_deref_atomic_imax
:
183 case nir_intrinsic_deref_atomic_umax
:
184 case nir_intrinsic_deref_atomic_and
:
185 case nir_intrinsic_deref_atomic_or
:
186 case nir_intrinsic_deref_atomic_xor
:
187 case nir_intrinsic_deref_atomic_exchange
:
188 case nir_intrinsic_deref_atomic_comp_swap
:
189 case nir_intrinsic_store_deref
:
190 case nir_intrinsic_copy_deref
: {
191 /* Destination in all of store_deref, copy_deref and the atomics is src[0]. */
192 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
194 uintptr_t mask
= intrin
->intrinsic
== nir_intrinsic_store_deref
?
195 nir_intrinsic_write_mask(intrin
) : (1 << glsl_get_vector_elements(dst
->type
)) - 1;
197 struct hash_entry
*ht_entry
= _mesa_hash_table_search(written
->derefs
, dst
);
199 ht_entry
->data
= (void *)(mask
| (uintptr_t)ht_entry
->data
);
201 _mesa_hash_table_insert(written
->derefs
, dst
, (void *)mask
);
214 case nir_cf_node_if
: {
215 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
217 new_written
= create_vars_written(state
);
219 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->then_list
)
220 gather_vars_written(state
, new_written
, cf_node
);
222 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->else_list
)
223 gather_vars_written(state
, new_written
, cf_node
);
228 case nir_cf_node_loop
: {
229 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
231 new_written
= create_vars_written(state
);
233 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &loop
->body
)
234 gather_vars_written(state
, new_written
, cf_node
);
240 unreachable("Invalid CF node type");
244 /* Merge new information to the parent control flow node. */
246 written
->modes
|= new_written
->modes
;
247 hash_table_foreach(new_written
->derefs
, new_entry
) {
248 struct hash_entry
*old_entry
=
249 _mesa_hash_table_search_pre_hashed(written
->derefs
, new_entry
->hash
,
252 nir_component_mask_t merged
= (uintptr_t) new_entry
->data
|
253 (uintptr_t) old_entry
->data
;
254 old_entry
->data
= (void *) ((uintptr_t) merged
);
256 _mesa_hash_table_insert_pre_hashed(written
->derefs
, new_entry
->hash
,
257 new_entry
->key
, new_entry
->data
);
261 _mesa_hash_table_insert(state
->vars_written_map
, cf_node
, new_written
);
265 static struct copy_entry
*
266 copy_entry_create(struct util_dynarray
*copies
,
267 nir_deref_instr
*dst_deref
)
269 struct copy_entry new_entry
= {
272 util_dynarray_append(copies
, struct copy_entry
, new_entry
);
273 return util_dynarray_top_ptr(copies
, struct copy_entry
);
276 /* Remove copy entry by swapping it with the last element and reducing the
277 * size. If used inside an iteration on copies, it must be a reverse
278 * (backwards) iteration. It is safe to use in those cases because the swap
279 * will not affect the rest of the iteration.
282 copy_entry_remove(struct util_dynarray
*copies
,
283 struct copy_entry
*entry
)
285 /* This also works when removing the last element since pop don't shrink
286 * the memory used by the array, so the swap is useless but not invalid.
288 *entry
= util_dynarray_pop(copies
, struct copy_entry
);
291 static struct copy_entry
*
292 lookup_entry_for_deref(struct util_dynarray
*copies
,
293 nir_deref_instr
*deref
,
294 nir_deref_compare_result allowed_comparisons
)
296 util_dynarray_foreach(copies
, struct copy_entry
, iter
) {
297 if (nir_compare_derefs(iter
->dst
, deref
) & allowed_comparisons
)
304 static struct copy_entry
*
305 lookup_entry_and_kill_aliases(struct util_dynarray
*copies
,
306 nir_deref_instr
*deref
,
309 /* TODO: Take into account the write_mask. */
311 nir_deref_instr
*dst_match
= NULL
;
312 util_dynarray_foreach_reverse(copies
, struct copy_entry
, iter
) {
313 if (!iter
->src
.is_ssa
) {
314 /* If this write aliases the source of some entry, get rid of it */
315 if (nir_compare_derefs(iter
->src
.deref
, deref
) & nir_derefs_may_alias_bit
) {
316 copy_entry_remove(copies
, iter
);
321 nir_deref_compare_result comp
= nir_compare_derefs(iter
->dst
, deref
);
323 if (comp
& nir_derefs_equal_bit
) {
324 /* Removing entries invalidate previous iter pointers, so we'll
325 * collect the matching entry later. Just make sure it is unique.
328 dst_match
= iter
->dst
;
329 } else if (comp
& nir_derefs_may_alias_bit
) {
330 copy_entry_remove(copies
, iter
);
334 struct copy_entry
*entry
= NULL
;
336 util_dynarray_foreach(copies
, struct copy_entry
, iter
) {
337 if (iter
->dst
== dst_match
) {
348 kill_aliases(struct util_dynarray
*copies
,
349 nir_deref_instr
*deref
,
352 /* TODO: Take into account the write_mask. */
354 struct copy_entry
*entry
=
355 lookup_entry_and_kill_aliases(copies
, deref
, write_mask
);
357 copy_entry_remove(copies
, entry
);
360 static struct copy_entry
*
361 get_entry_and_kill_aliases(struct util_dynarray
*copies
,
362 nir_deref_instr
*deref
,
365 /* TODO: Take into account the write_mask. */
367 struct copy_entry
*entry
=
368 lookup_entry_and_kill_aliases(copies
, deref
, write_mask
);
371 entry
= copy_entry_create(copies
, deref
);
377 apply_barrier_for_modes(struct util_dynarray
*copies
,
378 nir_variable_mode modes
)
380 util_dynarray_foreach_reverse(copies
, struct copy_entry
, iter
) {
381 if ((iter
->dst
->mode
& modes
) ||
382 (!iter
->src
.is_ssa
&& (iter
->src
.deref
->mode
& modes
)))
383 copy_entry_remove(copies
, iter
);
388 value_set_from_value(struct value
*value
, const struct value
*from
,
392 /* Clear value if it was being used as non-SSA. */
394 memset(&value
->ssa
, 0, sizeof(value
->ssa
));
395 value
->is_ssa
= true;
396 /* Only overwrite the written components */
397 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
398 if (write_mask
& (1 << i
)) {
399 value
->ssa
.def
[i
] = from
->ssa
.def
[i
];
400 value
->ssa
.component
[i
] = from
->ssa
.component
[i
];
404 /* Non-ssa stores always write everything */
405 value
->is_ssa
= false;
406 value
->deref
= from
->deref
;
410 /* Do a "load" from an SSA-based entry return it in "value" as a value with a
411 * single SSA def. Because an entry could reference multiple different SSA
412 * defs, a vecN operation may be inserted to combine them into a single SSA
413 * def before handing it back to the caller. If the load instruction is no
414 * longer needed, it is removed and nir_instr::block is set to NULL. (It is
415 * possible, in some cases, for the load to be used in the vecN operation in
416 * which case it isn't deleted.)
419 load_from_ssa_entry_value(struct copy_prop_var_state
*state
,
420 struct copy_entry
*entry
,
421 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
425 assert(value
->is_ssa
);
427 const struct glsl_type
*type
= entry
->dst
->type
;
428 unsigned num_components
= glsl_get_vector_elements(type
);
430 nir_component_mask_t available
= 0;
431 bool all_same
= true;
432 for (unsigned i
= 0; i
< num_components
; i
++) {
433 if (value
->ssa
.def
[i
])
434 available
|= (1 << i
);
436 if (value
->ssa
.def
[i
] != value
->ssa
.def
[0])
439 if (value
->ssa
.component
[i
] != i
)
444 /* Our work here is done */
445 b
->cursor
= nir_instr_remove(&intrin
->instr
);
446 intrin
->instr
.block
= NULL
;
450 if (available
!= (1 << num_components
) - 1 &&
451 intrin
->intrinsic
== nir_intrinsic_load_deref
&&
452 (available
& nir_ssa_def_components_read(&intrin
->dest
.ssa
)) == 0) {
453 /* If none of the components read are available as SSA values, then we
454 * should just bail. Otherwise, we would end up replacing the uses of
455 * the load_deref a vecN() that just gathers up its components.
460 b
->cursor
= nir_after_instr(&intrin
->instr
);
462 nir_ssa_def
*load_def
=
463 intrin
->intrinsic
== nir_intrinsic_load_deref
? &intrin
->dest
.ssa
: NULL
;
465 bool keep_intrin
= false;
466 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
467 for (unsigned i
= 0; i
< num_components
; i
++) {
468 if (value
->ssa
.def
[i
]) {
469 comps
[i
] = nir_channel(b
, value
->ssa
.def
[i
], value
->ssa
.component
[i
]);
471 /* We don't have anything for this component in our
472 * list. Just re-use a channel from the load.
474 if (load_def
== NULL
)
475 load_def
= nir_load_deref(b
, entry
->dst
);
477 if (load_def
->parent_instr
== &intrin
->instr
)
480 comps
[i
] = nir_channel(b
, load_def
, i
);
484 nir_ssa_def
*vec
= nir_vec(b
, comps
, num_components
);
485 value_set_ssa_components(value
, vec
, num_components
);
488 /* Removing this instruction should not touch the cursor because we
489 * created the cursor after the intrinsic and have added at least one
490 * instruction (the vec) since then.
492 assert(b
->cursor
.instr
!= &intrin
->instr
);
493 nir_instr_remove(&intrin
->instr
);
494 intrin
->instr
.block
= NULL
;
501 * Specialize the wildcards in a deref chain
503 * This function returns a deref chain identical to \param deref except that
504 * some of its wildcards are replaced with indices from \param specific. The
505 * process is guided by \param guide which references the same type as \param
506 * specific but has the same wildcard array lengths as \param deref.
508 static nir_deref_instr
*
509 specialize_wildcards(nir_builder
*b
,
510 nir_deref_path
*deref
,
511 nir_deref_path
*guide
,
512 nir_deref_path
*specific
)
514 nir_deref_instr
**deref_p
= &deref
->path
[1];
515 nir_deref_instr
**guide_p
= &guide
->path
[1];
516 nir_deref_instr
**spec_p
= &specific
->path
[1];
517 nir_deref_instr
*ret_tail
= deref
->path
[0];
518 for (; *deref_p
; deref_p
++) {
519 if ((*deref_p
)->deref_type
== nir_deref_type_array_wildcard
) {
520 /* This is where things get tricky. We have to search through
521 * the entry deref to find its corresponding wildcard and fill
522 * this slot in with the value from the src.
525 (*guide_p
)->deref_type
!= nir_deref_type_array_wildcard
) {
529 assert(*guide_p
&& *spec_p
);
531 ret_tail
= nir_build_deref_follower(b
, ret_tail
, *spec_p
);
536 ret_tail
= nir_build_deref_follower(b
, ret_tail
, *deref_p
);
543 /* Do a "load" from an deref-based entry return it in "value" as a value. The
544 * deref returned in "value" will always be a fresh copy so the caller can
545 * steal it and assign it to the instruction directly without copying it
549 load_from_deref_entry_value(struct copy_prop_var_state
*state
,
550 struct copy_entry
*entry
,
551 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
552 nir_deref_instr
*src
, struct value
*value
)
556 b
->cursor
= nir_instr_remove(&intrin
->instr
);
558 nir_deref_path entry_dst_path
, src_path
;
559 nir_deref_path_init(&entry_dst_path
, entry
->dst
, state
->mem_ctx
);
560 nir_deref_path_init(&src_path
, src
, state
->mem_ctx
);
562 bool need_to_specialize_wildcards
= false;
563 nir_deref_instr
**entry_p
= &entry_dst_path
.path
[1];
564 nir_deref_instr
**src_p
= &src_path
.path
[1];
565 while (*entry_p
&& *src_p
) {
566 nir_deref_instr
*entry_tail
= *entry_p
++;
567 nir_deref_instr
*src_tail
= *src_p
++;
569 if (src_tail
->deref_type
== nir_deref_type_array
&&
570 entry_tail
->deref_type
== nir_deref_type_array_wildcard
)
571 need_to_specialize_wildcards
= true;
574 /* If the entry deref is longer than the source deref then it refers to a
575 * smaller type and we can't source from it.
577 assert(*entry_p
== NULL
);
579 if (need_to_specialize_wildcards
) {
580 /* The entry has some wildcards that are not in src. This means we need
581 * to construct a new deref based on the entry but using the wildcards
582 * from the source and guided by the entry dst. Oof.
584 nir_deref_path entry_src_path
;
585 nir_deref_path_init(&entry_src_path
, entry
->src
.deref
, state
->mem_ctx
);
586 value
->deref
= specialize_wildcards(b
, &entry_src_path
,
587 &entry_dst_path
, &src_path
);
588 nir_deref_path_finish(&entry_src_path
);
591 /* If our source deref is longer than the entry deref, that's ok because
592 * it just means the entry deref needs to be extended a bit.
595 nir_deref_instr
*src_tail
= *src_p
++;
596 value
->deref
= nir_build_deref_follower(b
, value
->deref
, src_tail
);
599 nir_deref_path_finish(&entry_dst_path
);
600 nir_deref_path_finish(&src_path
);
606 try_load_from_entry(struct copy_prop_var_state
*state
, struct copy_entry
*entry
,
607 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
608 nir_deref_instr
*src
, struct value
*value
)
613 if (entry
->src
.is_ssa
) {
614 return load_from_ssa_entry_value(state
, entry
, b
, intrin
, value
);
616 return load_from_deref_entry_value(state
, entry
, b
, intrin
, src
, value
);
621 invalidate_copies_for_cf_node(struct copy_prop_var_state
*state
,
622 struct util_dynarray
*copies
,
623 nir_cf_node
*cf_node
)
625 struct hash_entry
*ht_entry
= _mesa_hash_table_search(state
->vars_written_map
, cf_node
);
628 struct vars_written
*written
= ht_entry
->data
;
629 if (written
->modes
) {
630 util_dynarray_foreach_reverse(copies
, struct copy_entry
, entry
) {
631 if (entry
->dst
->mode
& written
->modes
)
632 copy_entry_remove(copies
, entry
);
636 hash_table_foreach (written
->derefs
, entry
) {
637 nir_deref_instr
*deref_written
= (nir_deref_instr
*)entry
->key
;
638 kill_aliases(copies
, deref_written
, (uintptr_t)entry
->data
);
643 is_array_deref_of_vector(nir_deref_instr
*deref
)
645 if (deref
->deref_type
!= nir_deref_type_array
)
647 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
648 return glsl_type_is_vector(parent
->type
);
652 print_value(struct value
*value
, unsigned num_components
)
654 if (!value
->is_ssa
) {
655 printf(" %s ", glsl_get_type_name(value
->deref
->type
));
656 nir_print_deref(value
->deref
, stdout
);
660 bool same_ssa
= true;
661 for (unsigned i
= 0; i
< num_components
; i
++) {
662 if (value
->ssa
.component
[i
] != i
||
663 (i
> 0 && value
->ssa
.def
[i
- 1] != value
->ssa
.def
[i
])) {
669 printf(" ssa_%d", value
->ssa
.def
[0]->index
);
671 for (int i
= 0; i
< num_components
; i
++) {
672 if (value
->ssa
.def
[i
])
673 printf(" ssa_%d[%u]", value
->ssa
.def
[i
]->index
, value
->ssa
.component
[i
]);
681 print_copy_entry(struct copy_entry
*entry
)
683 printf(" %s ", glsl_get_type_name(entry
->dst
->type
));
684 nir_print_deref(entry
->dst
, stdout
);
687 unsigned num_components
= glsl_get_vector_elements(entry
->dst
->type
);
688 print_value(&entry
->src
, num_components
);
693 dump_instr(nir_instr
*instr
)
696 nir_print_instr(instr
, stdout
);
701 dump_copy_entries(struct util_dynarray
*copies
)
703 util_dynarray_foreach(copies
, struct copy_entry
, iter
)
704 print_copy_entry(iter
);
709 copy_prop_vars_block(struct copy_prop_var_state
*state
,
710 nir_builder
*b
, nir_block
*block
,
711 struct util_dynarray
*copies
)
714 printf("# block%d\n", block
->index
);
715 dump_copy_entries(copies
);
718 nir_foreach_instr_safe(instr
, block
) {
719 if (debug
&& instr
->type
== nir_instr_type_deref
)
722 if (instr
->type
== nir_instr_type_call
) {
723 if (debug
) dump_instr(instr
);
724 apply_barrier_for_modes(copies
, nir_var_shader_out
|
725 nir_var_shader_temp
|
726 nir_var_function_temp
|
729 if (debug
) dump_copy_entries(copies
);
733 if (instr
->type
!= nir_instr_type_intrinsic
)
736 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
737 switch (intrin
->intrinsic
) {
738 case nir_intrinsic_barrier
:
739 case nir_intrinsic_memory_barrier
:
740 if (debug
) dump_instr(instr
);
742 apply_barrier_for_modes(copies
, nir_var_shader_out
|
747 case nir_intrinsic_emit_vertex
:
748 case nir_intrinsic_emit_vertex_with_counter
:
749 if (debug
) dump_instr(instr
);
751 apply_barrier_for_modes(copies
, nir_var_shader_out
);
754 case nir_intrinsic_load_deref
: {
755 if (debug
) dump_instr(instr
);
757 nir_deref_instr
*src
= nir_src_as_deref(intrin
->src
[0]);
759 if (is_array_deref_of_vector(src
)) {
760 /* Not handled yet. This load won't invalidate existing copies. */
764 struct copy_entry
*src_entry
=
765 lookup_entry_for_deref(copies
, src
, nir_derefs_a_contains_b_bit
);
766 struct value value
= {0};
767 if (try_load_from_entry(state
, src_entry
, b
, intrin
, src
, &value
)) {
769 /* lookup_load has already ensured that we get a single SSA
770 * value that has all of the channels. We just have to do the
773 if (intrin
->instr
.block
) {
774 /* The lookup left our instruction in-place. This means it
775 * must have used it to vec up a bunch of different sources.
776 * We need to be careful when rewriting uses so we don't
777 * rewrite the vecN itself.
779 nir_ssa_def_rewrite_uses_after(&intrin
->dest
.ssa
,
780 nir_src_for_ssa(value
.ssa
.def
[0]),
781 value
.ssa
.def
[0]->parent_instr
);
783 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
784 nir_src_for_ssa(value
.ssa
.def
[0]));
787 /* We're turning it into a load of a different variable */
788 intrin
->src
[0] = nir_src_for_ssa(&value
.deref
->dest
.ssa
);
790 /* Put it back in again. */
791 nir_builder_instr_insert(b
, instr
);
792 value_set_ssa_components(&value
, &intrin
->dest
.ssa
,
793 intrin
->num_components
);
795 state
->progress
= true;
797 value_set_ssa_components(&value
, &intrin
->dest
.ssa
,
798 intrin
->num_components
);
801 /* Now that we have a value, we're going to store it back so that we
802 * have the right value next time we come looking for it. In order
803 * to do this, we need an exact match, not just something that
804 * contains what we're looking for.
806 struct copy_entry
*entry
=
807 lookup_entry_for_deref(copies
, src
, nir_derefs_equal_bit
);
809 entry
= copy_entry_create(copies
, src
);
811 /* Update the entry with the value of the load. This way
812 * we can potentially remove subsequent loads.
814 value_set_from_value(&entry
->src
, &value
,
815 (1 << intrin
->num_components
) - 1);
819 case nir_intrinsic_store_deref
: {
820 if (debug
) dump_instr(instr
);
822 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
823 struct copy_entry
*entry
=
824 lookup_entry_for_deref(copies
, dst
, nir_derefs_equal_bit
);
825 if (entry
&& value_equals_store_src(&entry
->src
, intrin
)) {
826 /* If we are storing the value from a load of the same var the
827 * store is redundant so remove it.
829 nir_instr_remove(instr
);
830 } else if (is_array_deref_of_vector(dst
)) {
831 /* Not handled yet. Writing into an element of 'dst' invalidates
832 * any related entries in copies.
834 nir_deref_instr
*vector
= nir_deref_instr_parent(dst
);
835 unsigned vector_components
= glsl_get_vector_elements(vector
->type
);
836 kill_aliases(copies
, vector
, (1 << vector_components
) - 1);
838 struct value value
= {0};
839 value_set_ssa_components(&value
, intrin
->src
[1].ssa
,
840 intrin
->num_components
);
841 unsigned wrmask
= nir_intrinsic_write_mask(intrin
);
842 struct copy_entry
*entry
=
843 get_entry_and_kill_aliases(copies
, dst
, wrmask
);
844 value_set_from_value(&entry
->src
, &value
, wrmask
);
850 case nir_intrinsic_copy_deref
: {
851 if (debug
) dump_instr(instr
);
853 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
854 nir_deref_instr
*src
= nir_src_as_deref(intrin
->src
[1]);
856 if (nir_compare_derefs(src
, dst
) & nir_derefs_equal_bit
) {
857 /* This is a no-op self-copy. Get rid of it */
858 nir_instr_remove(instr
);
862 /* The copy_deref intrinsic doesn't keep track of num_components, so
865 unsigned num_components
= glsl_get_vector_elements(dst
->type
);
866 unsigned full_mask
= (1 << num_components
) - 1;
868 if (is_array_deref_of_vector(src
) || is_array_deref_of_vector(dst
)) {
869 /* Cases not handled yet. Writing into an element of 'dst'
870 * invalidates any related entries in copies. Reading from 'src'
871 * doesn't invalidate anything, so no action needed for it.
873 kill_aliases(copies
, dst
, full_mask
);
877 struct copy_entry
*src_entry
=
878 lookup_entry_for_deref(copies
, src
, nir_derefs_a_contains_b_bit
);
880 if (try_load_from_entry(state
, src_entry
, b
, intrin
, src
, &value
)) {
881 /* If load works, intrin (the copy_deref) is removed. */
883 nir_store_deref(b
, dst
, value
.ssa
.def
[0], full_mask
);
885 /* If this would be a no-op self-copy, don't bother. */
886 if (nir_compare_derefs(value
.deref
, dst
) & nir_derefs_equal_bit
)
889 /* Just turn it into a copy of a different deref */
890 intrin
->src
[1] = nir_src_for_ssa(&value
.deref
->dest
.ssa
);
892 /* Put it back in again. */
893 nir_builder_instr_insert(b
, instr
);
896 state
->progress
= true;
898 value
= (struct value
) {
904 struct copy_entry
*dst_entry
=
905 get_entry_and_kill_aliases(copies
, dst
, full_mask
);
906 value_set_from_value(&dst_entry
->src
, &value
, full_mask
);
910 case nir_intrinsic_deref_atomic_add
:
911 case nir_intrinsic_deref_atomic_imin
:
912 case nir_intrinsic_deref_atomic_umin
:
913 case nir_intrinsic_deref_atomic_imax
:
914 case nir_intrinsic_deref_atomic_umax
:
915 case nir_intrinsic_deref_atomic_and
:
916 case nir_intrinsic_deref_atomic_or
:
917 case nir_intrinsic_deref_atomic_xor
:
918 case nir_intrinsic_deref_atomic_exchange
:
919 case nir_intrinsic_deref_atomic_comp_swap
:
920 if (debug
) dump_instr(instr
);
922 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
923 unsigned num_components
= glsl_get_vector_elements(dst
->type
);
924 unsigned full_mask
= (1 << num_components
) - 1;
925 kill_aliases(copies
, dst
, full_mask
);
929 continue; /* To skip the debug below. */
932 if (debug
) dump_copy_entries(copies
);
937 copy_prop_vars_cf_node(struct copy_prop_var_state
*state
,
938 struct util_dynarray
*copies
,
939 nir_cf_node
*cf_node
)
941 switch (cf_node
->type
) {
942 case nir_cf_node_function
: {
943 nir_function_impl
*impl
= nir_cf_node_as_function(cf_node
);
945 struct util_dynarray impl_copies
;
946 util_dynarray_init(&impl_copies
, state
->mem_ctx
);
948 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &impl
->body
)
949 copy_prop_vars_cf_node(state
, &impl_copies
, cf_node
);
954 case nir_cf_node_block
: {
955 nir_block
*block
= nir_cf_node_as_block(cf_node
);
957 nir_builder_init(&b
, state
->impl
);
958 copy_prop_vars_block(state
, &b
, block
, copies
);
962 case nir_cf_node_if
: {
963 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
965 /* Clone the copies for each branch of the if statement. The idea is
966 * that they both see the same state of available copies, but do not
967 * interfere to each other.
970 struct util_dynarray then_copies
;
971 util_dynarray_clone(&then_copies
, state
->mem_ctx
, copies
);
973 struct util_dynarray else_copies
;
974 util_dynarray_clone(&else_copies
, state
->mem_ctx
, copies
);
976 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->then_list
)
977 copy_prop_vars_cf_node(state
, &then_copies
, cf_node
);
979 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &if_stmt
->else_list
)
980 copy_prop_vars_cf_node(state
, &else_copies
, cf_node
);
982 /* Both branches copies can be ignored, since the effect of running both
983 * branches was captured in the first pass that collects vars_written.
986 invalidate_copies_for_cf_node(state
, copies
, cf_node
);
991 case nir_cf_node_loop
: {
992 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
994 /* Invalidate before cloning the copies for the loop, since the loop
995 * body can be executed more than once.
998 invalidate_copies_for_cf_node(state
, copies
, cf_node
);
1000 struct util_dynarray loop_copies
;
1001 util_dynarray_clone(&loop_copies
, state
->mem_ctx
, copies
);
1003 foreach_list_typed_safe(nir_cf_node
, cf_node
, node
, &loop
->body
)
1004 copy_prop_vars_cf_node(state
, &loop_copies
, cf_node
);
1010 unreachable("Invalid CF node type");
1015 nir_copy_prop_vars_impl(nir_function_impl
*impl
)
1017 void *mem_ctx
= ralloc_context(NULL
);
1020 nir_metadata_require(impl
, nir_metadata_block_index
);
1021 printf("## nir_copy_prop_vars_impl for %s\n", impl
->function
->name
);
1024 struct copy_prop_var_state state
= {
1027 .lin_ctx
= linear_zalloc_parent(mem_ctx
, 0),
1029 .vars_written_map
= _mesa_pointer_hash_table_create(mem_ctx
),
1032 gather_vars_written(&state
, NULL
, &impl
->cf_node
);
1034 copy_prop_vars_cf_node(&state
, NULL
, &impl
->cf_node
);
1036 if (state
.progress
) {
1037 nir_metadata_preserve(impl
, nir_metadata_block_index
|
1038 nir_metadata_dominance
);
1041 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
1045 ralloc_free(mem_ctx
);
1046 return state
.progress
;
1050 nir_opt_copy_prop_vars(nir_shader
*shader
)
1052 bool progress
= false;
1054 nir_foreach_function(function
, shader
) {
1055 if (!function
->impl
)
1057 progress
|= nir_copy_prop_vars_impl(function
->impl
);