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"
31 * Variable-based copy propagation
33 * Normally, NIR trusts in SSA form for most of its copy-propagation needs.
34 * However, there are cases, especially when dealing with indirects, where SSA
35 * won't help you. This pass is for those times. Specifically, it handles
36 * the following things that the rest of NIR can't:
38 * 1) Copy-propagation on variables that have indirect access. This includes
39 * propagating from indirect stores into indirect loads.
41 * 2) Dead code elimination of store_var and copy_var intrinsics based on
42 * killed destination values.
44 * 3) 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 * Unfortunately, properly handling all of those cases makes this path rather
49 * complex. In order to avoid additional complexity, this pass is entirely
50 * block-local. If we tried to make it global, the data-flow analysis would
51 * rapidly get out of hand. Fortunately, for anything that is only ever
52 * accessed directly, we get SSA based copy-propagation which is extremely
53 * powerful so this isn't that great a loss.
60 nir_deref_instr
*deref
;
65 struct list_head link
;
67 nir_instr
*store_instr
[4];
69 unsigned comps_may_be_read
;
75 struct copy_prop_var_state
{
80 struct list_head copies
;
82 /* We're going to be allocating and deleting a lot of copy entries so we'll
83 * keep a free list to avoid thrashing malloc too badly.
85 struct list_head copy_free_list
;
90 static struct copy_entry
*
91 copy_entry_create(struct copy_prop_var_state
*state
,
92 nir_deref_instr
*dst_deref
)
94 struct copy_entry
*entry
;
95 if (!list_empty(&state
->copy_free_list
)) {
96 struct list_head
*item
= state
->copy_free_list
.next
;
98 entry
= LIST_ENTRY(struct copy_entry
, item
, link
);
99 memset(entry
, 0, sizeof(*entry
));
101 entry
= rzalloc(state
->mem_ctx
, struct copy_entry
);
104 entry
->dst
= dst_deref
;
105 list_add(&entry
->link
, &state
->copies
);
111 copy_entry_remove(struct copy_prop_var_state
*state
, struct copy_entry
*entry
)
113 list_del(&entry
->link
);
114 list_add(&entry
->link
, &state
->copy_free_list
);
118 remove_dead_writes(struct copy_prop_var_state
*state
,
119 struct copy_entry
*entry
, unsigned write_mask
)
121 /* We're overwriting another entry. Some of it's components may not
122 * have been read yet and, if that's the case, we may be able to delete
123 * some instructions but we have to be careful.
125 unsigned dead_comps
= write_mask
& ~entry
->comps_may_be_read
;
127 for (unsigned mask
= dead_comps
; mask
;) {
128 unsigned i
= u_bit_scan(&mask
);
130 nir_instr
*instr
= entry
->store_instr
[i
];
132 /* We may have already deleted it on a previous iteration */
136 /* See if this instr is used anywhere that it's not dead */
138 for (unsigned j
= 0; j
< 4; j
++) {
139 if (entry
->store_instr
[j
] == instr
) {
140 if (dead_comps
& (1 << j
)) {
141 entry
->store_instr
[j
] = NULL
;
149 nir_instr_remove(instr
);
150 state
->progress
= true;
155 static struct copy_entry
*
156 lookup_entry_for_deref(struct copy_prop_var_state
*state
,
157 nir_deref_instr
*deref
,
158 nir_deref_compare_result allowed_comparisons
)
160 list_for_each_entry(struct copy_entry
, iter
, &state
->copies
, link
) {
161 if (nir_compare_derefs(iter
->dst
, deref
) & allowed_comparisons
)
169 mark_aliased_entries_as_read(struct copy_prop_var_state
*state
,
170 nir_deref_instr
*deref
, unsigned components
)
172 list_for_each_entry(struct copy_entry
, iter
, &state
->copies
, link
) {
173 if (nir_compare_derefs(iter
->dst
, deref
) & nir_derefs_may_alias_bit
)
174 iter
->comps_may_be_read
|= components
;
178 static struct copy_entry
*
179 get_entry_and_kill_aliases(struct copy_prop_var_state
*state
,
180 nir_deref_instr
*deref
,
183 struct copy_entry
*entry
= NULL
;
184 list_for_each_entry_safe(struct copy_entry
, iter
, &state
->copies
, link
) {
185 if (!iter
->src
.is_ssa
) {
186 /* If this write aliases the source of some entry, get rid of it */
187 if (nir_compare_derefs(iter
->src
.deref
, deref
) & nir_derefs_may_alias_bit
) {
188 copy_entry_remove(state
, iter
);
193 nir_deref_compare_result comp
= nir_compare_derefs(iter
->dst
, deref
);
194 /* This is a store operation. If we completely overwrite some value, we
195 * want to delete any dead writes that may be present.
197 if (comp
& nir_derefs_b_contains_a_bit
)
198 remove_dead_writes(state
, iter
, write_mask
);
200 if (comp
& nir_derefs_equal_bit
) {
201 assert(entry
== NULL
);
203 } else if (comp
& nir_derefs_may_alias_bit
) {
204 copy_entry_remove(state
, iter
);
209 entry
= copy_entry_create(state
, deref
);
215 apply_barrier_for_modes(struct copy_prop_var_state
*state
,
216 nir_variable_mode modes
)
218 list_for_each_entry_safe(struct copy_entry
, iter
, &state
->copies
, link
) {
219 nir_variable
*dst_var
= nir_deref_instr_get_variable(iter
->dst
);
220 nir_variable
*src_var
= iter
->src
.is_ssa
? NULL
:
221 nir_deref_instr_get_variable(iter
->src
.deref
);
223 if ((dst_var
->data
.mode
& modes
) ||
224 (src_var
&& (src_var
->data
.mode
& modes
)))
225 copy_entry_remove(state
, iter
);
230 store_to_entry(struct copy_prop_var_state
*state
, struct copy_entry
*entry
,
231 const struct value
*value
, unsigned write_mask
,
232 nir_instr
*store_instr
)
234 entry
->comps_may_be_read
&= ~write_mask
;
236 entry
->src
.is_ssa
= true;
237 /* Only overwrite the written components */
238 for (unsigned i
= 0; i
< 4; i
++) {
239 if (write_mask
& (1 << i
)) {
240 entry
->store_instr
[i
] = store_instr
;
241 entry
->src
.ssa
[i
] = value
->ssa
[i
];
245 /* Non-ssa stores always write everything */
246 entry
->src
.is_ssa
= false;
247 entry
->src
.deref
= value
->deref
;
248 for (unsigned i
= 0; i
< 4; i
++)
249 entry
->store_instr
[i
] = store_instr
;
253 /* Do a "load" from an SSA-based entry return it in "value" as a value with a
254 * single SSA def. Because an entry could reference up to 4 different SSA
255 * defs, a vecN operation may be inserted to combine them into a single SSA
256 * def before handing it back to the caller. If the load instruction is no
257 * longer needed, it is removed and nir_instr::block is set to NULL. (It is
258 * possible, in some cases, for the load to be used in the vecN operation in
259 * which case it isn't deleted.)
262 load_from_ssa_entry_value(struct copy_prop_var_state
*state
,
263 struct copy_entry
*entry
,
264 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
268 assert(value
->is_ssa
);
270 const struct glsl_type
*type
= entry
->dst
->type
;
271 unsigned num_components
= glsl_get_vector_elements(type
);
273 nir_component_mask_t available
= 0;
274 bool all_same
= true;
275 for (unsigned i
= 0; i
< num_components
; i
++) {
277 available
|= (1 << i
);
279 if (value
->ssa
[i
] != value
->ssa
[0])
284 /* Our work here is done */
285 b
->cursor
= nir_instr_remove(&intrin
->instr
);
286 intrin
->instr
.block
= NULL
;
290 if (available
!= (1 << num_components
) - 1 &&
291 intrin
->intrinsic
== nir_intrinsic_load_deref
&&
292 (available
& nir_ssa_def_components_read(&intrin
->dest
.ssa
)) == 0) {
293 /* If none of the components read are available as SSA values, then we
294 * should just bail. Otherwise, we would end up replacing the uses of
295 * the load_deref a vecN() that just gathers up its components.
300 b
->cursor
= nir_after_instr(&intrin
->instr
);
302 nir_ssa_def
*load_def
=
303 intrin
->intrinsic
== nir_intrinsic_load_deref
? &intrin
->dest
.ssa
: NULL
;
305 bool keep_intrin
= false;
306 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
307 for (unsigned i
= 0; i
< num_components
; i
++) {
309 comps
[i
] = nir_channel(b
, value
->ssa
[i
], i
);
311 /* We don't have anything for this component in our
312 * list. Just re-use a channel from the load.
314 if (load_def
== NULL
)
315 load_def
= nir_load_deref(b
, entry
->dst
);
317 if (load_def
->parent_instr
== &intrin
->instr
)
320 comps
[i
] = nir_channel(b
, load_def
, i
);
324 nir_ssa_def
*vec
= nir_vec(b
, comps
, num_components
);
325 for (unsigned i
= 0; i
< num_components
; i
++)
329 /* Removing this instruction should not touch the cursor because we
330 * created the cursor after the intrinsic and have added at least one
331 * instruction (the vec) since then.
333 assert(b
->cursor
.instr
!= &intrin
->instr
);
334 nir_instr_remove(&intrin
->instr
);
335 intrin
->instr
.block
= NULL
;
342 * Specialize the wildcards in a deref chain
344 * This function returns a deref chain identical to \param deref except that
345 * some of its wildcards are replaced with indices from \param specific. The
346 * process is guided by \param guide which references the same type as \param
347 * specific but has the same wildcard array lengths as \param deref.
349 static nir_deref_instr
*
350 specialize_wildcards(nir_builder
*b
,
351 nir_deref_path
*deref
,
352 nir_deref_path
*guide
,
353 nir_deref_path
*specific
)
355 nir_deref_instr
**deref_p
= &deref
->path
[1];
356 nir_deref_instr
**guide_p
= &guide
->path
[1];
357 nir_deref_instr
**spec_p
= &specific
->path
[1];
358 nir_deref_instr
*ret_tail
= deref
->path
[0];
359 for (; *deref_p
; deref_p
++) {
360 if ((*deref_p
)->deref_type
== nir_deref_type_array_wildcard
) {
361 /* This is where things get tricky. We have to search through
362 * the entry deref to find its corresponding wildcard and fill
363 * this slot in with the value from the src.
366 (*guide_p
)->deref_type
!= nir_deref_type_array_wildcard
) {
370 assert(*guide_p
&& *spec_p
);
372 ret_tail
= nir_build_deref_follower(b
, ret_tail
, *spec_p
);
377 ret_tail
= nir_build_deref_follower(b
, ret_tail
, *deref_p
);
384 /* Do a "load" from an deref-based entry return it in "value" as a value. The
385 * deref returned in "value" will always be a fresh copy so the caller can
386 * steal it and assign it to the instruction directly without copying it
390 load_from_deref_entry_value(struct copy_prop_var_state
*state
,
391 struct copy_entry
*entry
,
392 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
393 nir_deref_instr
*src
, struct value
*value
)
397 b
->cursor
= nir_instr_remove(&intrin
->instr
);
399 nir_deref_path entry_dst_path
, src_path
;
400 nir_deref_path_init(&entry_dst_path
, entry
->dst
, state
->mem_ctx
);
401 nir_deref_path_init(&src_path
, src
, state
->mem_ctx
);
403 bool need_to_specialize_wildcards
= false;
404 nir_deref_instr
**entry_p
= &entry_dst_path
.path
[1];
405 nir_deref_instr
**src_p
= &src_path
.path
[1];
406 while (*entry_p
&& *src_p
) {
407 nir_deref_instr
*entry_tail
= *entry_p
++;
408 nir_deref_instr
*src_tail
= *src_p
++;
410 if (src_tail
->deref_type
== nir_deref_type_array
&&
411 entry_tail
->deref_type
== nir_deref_type_array_wildcard
)
412 need_to_specialize_wildcards
= true;
415 /* If the entry deref is longer than the source deref then it refers to a
416 * smaller type and we can't source from it.
418 assert(*entry_p
== NULL
);
420 if (need_to_specialize_wildcards
) {
421 /* The entry has some wildcards that are not in src. This means we need
422 * to construct a new deref based on the entry but using the wildcards
423 * from the source and guided by the entry dst. Oof.
425 nir_deref_path entry_src_path
;
426 nir_deref_path_init(&entry_src_path
, entry
->src
.deref
, state
->mem_ctx
);
427 value
->deref
= specialize_wildcards(b
, &entry_src_path
,
428 &entry_dst_path
, &src_path
);
429 nir_deref_path_finish(&entry_src_path
);
432 /* If our source deref is longer than the entry deref, that's ok because
433 * it just means the entry deref needs to be extended a bit.
436 nir_deref_instr
*src_tail
= *src_p
++;
437 value
->deref
= nir_build_deref_follower(b
, value
->deref
, src_tail
);
440 nir_deref_path_finish(&entry_dst_path
);
441 nir_deref_path_finish(&src_path
);
447 try_load_from_entry(struct copy_prop_var_state
*state
, struct copy_entry
*entry
,
448 nir_builder
*b
, nir_intrinsic_instr
*intrin
,
449 nir_deref_instr
*src
, struct value
*value
)
454 if (entry
->src
.is_ssa
) {
455 return load_from_ssa_entry_value(state
, entry
, b
, intrin
, value
);
457 return load_from_deref_entry_value(state
, entry
, b
, intrin
, src
, value
);
462 copy_prop_vars_block(struct copy_prop_var_state
*state
,
463 nir_builder
*b
, nir_block
*block
)
465 /* Start each block with a blank slate */
466 list_for_each_entry_safe(struct copy_entry
, iter
, &state
->copies
, link
)
467 copy_entry_remove(state
, iter
);
469 nir_foreach_instr_safe(instr
, block
) {
470 if (instr
->type
!= nir_instr_type_intrinsic
)
473 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
474 switch (intrin
->intrinsic
) {
475 case nir_intrinsic_barrier
:
476 case nir_intrinsic_memory_barrier
:
477 /* If we hit a barrier, we need to trash everything that may possibly
478 * be accessible to another thread. Locals, globals, and things of
479 * the like are safe, however.
481 apply_barrier_for_modes(state
, ~(nir_var_local
| nir_var_global
|
482 nir_var_shader_in
| nir_var_uniform
));
485 case nir_intrinsic_emit_vertex
:
486 case nir_intrinsic_emit_vertex_with_counter
:
487 apply_barrier_for_modes(state
, nir_var_shader_out
);
490 case nir_intrinsic_load_deref
: {
491 nir_deref_instr
*src
= nir_src_as_deref(intrin
->src
[0]);
493 uint8_t comps_read
= nir_ssa_def_components_read(&intrin
->dest
.ssa
);
494 mark_aliased_entries_as_read(state
, src
, comps_read
);
496 struct copy_entry
*src_entry
=
497 lookup_entry_for_deref(state
, src
, nir_derefs_a_contains_b_bit
);
499 if (try_load_from_entry(state
, src_entry
, b
, intrin
, src
, &value
)) {
501 /* lookup_load has already ensured that we get a single SSA
502 * value that has all of the channels. We just have to do the
505 if (intrin
->instr
.block
) {
506 /* The lookup left our instruction in-place. This means it
507 * must have used it to vec up a bunch of different sources.
508 * We need to be careful when rewriting uses so we don't
509 * rewrite the vecN itself.
511 nir_ssa_def_rewrite_uses_after(&intrin
->dest
.ssa
,
512 nir_src_for_ssa(value
.ssa
[0]),
513 value
.ssa
[0]->parent_instr
);
515 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
516 nir_src_for_ssa(value
.ssa
[0]));
519 /* We're turning it into a load of a different variable */
520 intrin
->src
[0] = nir_src_for_ssa(&value
.deref
->dest
.ssa
);
522 /* Put it back in again. */
523 nir_builder_instr_insert(b
, instr
);
526 for (unsigned i
= 0; i
< intrin
->num_components
; i
++)
527 value
.ssa
[i
] = &intrin
->dest
.ssa
;
529 state
->progress
= true;
532 for (unsigned i
= 0; i
< intrin
->num_components
; i
++)
533 value
.ssa
[i
] = &intrin
->dest
.ssa
;
536 /* Now that we have a value, we're going to store it back so that we
537 * have the right value next time we come looking for it. In order
538 * to do this, we need an exact match, not just something that
539 * contains what we're looking for.
541 struct copy_entry
*store_entry
=
542 lookup_entry_for_deref(state
, src
, nir_derefs_equal_bit
);
544 store_entry
= copy_entry_create(state
, src
);
546 /* Set up a store to this entry with the value of the load. This way
547 * we can potentially remove subsequent loads. However, we use a
548 * NULL instruction so we don't try and delete the load on a
551 store_to_entry(state
, store_entry
, &value
,
552 ((1 << intrin
->num_components
) - 1), NULL
);
556 case nir_intrinsic_store_deref
: {
557 struct value value
= {
561 for (unsigned i
= 0; i
< intrin
->num_components
; i
++)
562 value
.ssa
[i
] = intrin
->src
[1].ssa
;
564 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
565 unsigned wrmask
= nir_intrinsic_write_mask(intrin
);
566 struct copy_entry
*entry
=
567 get_entry_and_kill_aliases(state
, dst
, wrmask
);
568 store_to_entry(state
, entry
, &value
, wrmask
, &intrin
->instr
);
572 case nir_intrinsic_copy_deref
: {
573 nir_deref_instr
*dst
= nir_src_as_deref(intrin
->src
[0]);
574 nir_deref_instr
*src
= nir_src_as_deref(intrin
->src
[1]);
576 if (nir_compare_derefs(src
, dst
) & nir_derefs_equal_bit
) {
577 /* This is a no-op self-copy. Get rid of it */
578 nir_instr_remove(instr
);
582 mark_aliased_entries_as_read(state
, src
, 0xf);
584 struct copy_entry
*src_entry
=
585 lookup_entry_for_deref(state
, src
, nir_derefs_a_contains_b_bit
);
587 if (try_load_from_entry(state
, src_entry
, b
, intrin
, src
, &value
)) {
589 nir_store_deref(b
, dst
, value
.ssa
[0], 0xf);
590 intrin
= nir_instr_as_intrinsic(nir_builder_last_instr(b
));
592 /* If this would be a no-op self-copy, don't bother. */
593 if (nir_compare_derefs(value
.deref
, dst
) & nir_derefs_equal_bit
)
596 /* Just turn it into a copy of a different deref */
597 intrin
->src
[1] = nir_src_for_ssa(&value
.deref
->dest
.ssa
);
599 /* Put it back in again. */
600 nir_builder_instr_insert(b
, instr
);
603 state
->progress
= true;
605 value
= (struct value
) {
611 struct copy_entry
*dst_entry
=
612 get_entry_and_kill_aliases(state
, dst
, 0xf);
613 store_to_entry(state
, dst_entry
, &value
, 0xf, &intrin
->instr
);
624 nir_opt_copy_prop_vars(nir_shader
*shader
)
626 struct copy_prop_var_state state
;
628 state
.shader
= shader
;
629 state
.mem_ctx
= ralloc_context(NULL
);
630 list_inithead(&state
.copies
);
631 list_inithead(&state
.copy_free_list
);
633 bool global_progress
= false;
634 nir_foreach_function(function
, shader
) {
639 nir_builder_init(&b
, function
->impl
);
641 state
.progress
= false;
642 nir_foreach_block(block
, function
->impl
)
643 copy_prop_vars_block(&state
, &b
, block
);
645 if (state
.progress
) {
646 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
647 nir_metadata_dominance
);
648 global_progress
= true;
652 ralloc_free(state
.mem_ctx
);
654 return global_progress
;