2018-07-27 13:56:35 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2018 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nir.h"
|
|
|
|
|
#include "nir_builder.h"
|
|
|
|
|
#include "nir_deref.h"
|
|
|
|
|
|
|
|
|
|
#include "util/u_dynarray.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Elimination of dead writes based on derefs.
|
|
|
|
|
*
|
|
|
|
|
* Dead writes are stores and copies that write to a deref, which then gets
|
|
|
|
|
* another write before it was used (read or sourced for a copy). Those
|
|
|
|
|
* writes can be removed since they don't affect anything.
|
|
|
|
|
*
|
|
|
|
|
* For derefs that refer to a memory area that can be read after the program,
|
|
|
|
|
* the last write is considered used. The presence of certain instructions
|
|
|
|
|
* may also cause writes to be considered used, e.g. memory barrier (in this case
|
|
|
|
|
* the value must be written as other thread might use it).
|
|
|
|
|
*
|
|
|
|
|
* The write mask for store instructions is considered, so it is possible that
|
|
|
|
|
* a store is removed because of the combination of other stores overwritten
|
|
|
|
|
* its value.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Entry for unused_writes arrays. */
|
|
|
|
|
struct write_entry {
|
|
|
|
|
/* If NULL indicates the entry is free to be reused. */
|
|
|
|
|
nir_intrinsic_instr *intrin;
|
|
|
|
|
nir_component_mask_t mask;
|
|
|
|
|
nir_deref_instr *dst;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
clear_unused_for_modes(struct util_dynarray *unused_writes, nir_variable_mode modes)
|
|
|
|
|
{
|
|
|
|
|
util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
|
2020-11-01 17:46:56 -06:00
|
|
|
if (nir_deref_mode_may_be(entry->dst, modes))
|
2018-07-27 13:56:35 -07:00
|
|
|
*entry = util_dynarray_pop(unused_writes, struct write_entry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
clear_unused_for_read(struct util_dynarray *unused_writes, nir_deref_instr *src)
|
|
|
|
|
{
|
|
|
|
|
util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
|
|
|
|
|
if (nir_compare_derefs(src, entry->dst) & nir_derefs_may_alias_bit)
|
|
|
|
|
*entry = util_dynarray_pop(unused_writes, struct write_entry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
update_unused_writes(struct util_dynarray *unused_writes,
|
|
|
|
|
nir_intrinsic_instr *intrin,
|
|
|
|
|
nir_deref_instr *dst, nir_component_mask_t mask)
|
|
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
|
|
/* This pass assumes that destination of copies and stores are derefs that
|
|
|
|
|
* end in a vector or scalar (it is OK to have wildcards or indirects for
|
|
|
|
|
* arrays).
|
|
|
|
|
*/
|
|
|
|
|
assert(glsl_type_is_vector_or_scalar(dst->type));
|
|
|
|
|
|
|
|
|
|
/* Find writes that are unused and can be removed. */
|
|
|
|
|
util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
|
|
|
|
|
nir_deref_compare_result comp = nir_compare_derefs(dst, entry->dst);
|
|
|
|
|
if (comp & nir_derefs_a_contains_b_bit) {
|
|
|
|
|
entry->mask &= ~mask;
|
|
|
|
|
if (entry->mask == 0) {
|
|
|
|
|
nir_instr_remove(&entry->intrin->instr);
|
|
|
|
|
*entry = util_dynarray_pop(unused_writes, struct write_entry);
|
|
|
|
|
progress = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add the new write to the unused array. */
|
|
|
|
|
struct write_entry new_entry = {
|
|
|
|
|
.intrin = intrin,
|
|
|
|
|
.mask = mask,
|
|
|
|
|
.dst = dst,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
util_dynarray_append(unused_writes, struct write_entry, new_entry);
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-06 15:19:54 -08:00
|
|
|
static bool
|
|
|
|
|
ends_program(nir_block *block)
|
|
|
|
|
{
|
|
|
|
|
/* Avoid back-edges */
|
|
|
|
|
if (block->cf_node.parent->type == nir_cf_node_loop)
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-02-27 17:41:45 +01:00
|
|
|
/* Avoid called functions */
|
|
|
|
|
if (!nir_cf_node_get_function(&block->cf_node)->function->is_entrypoint)
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-02-06 15:19:54 -08:00
|
|
|
if (block->successors[0] == NULL) {
|
|
|
|
|
/* This is the end block */
|
|
|
|
|
assert(block->successors[1] == NULL);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (block->successors[1] != NULL)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return exec_list_is_empty(&block->successors[0]->instr_list) &&
|
|
|
|
|
ends_program(block->successors[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 13:56:35 -07:00
|
|
|
static bool
|
2025-08-11 13:18:15 -04:00
|
|
|
remove_dead_write_vars_local(nir_shader *shader, nir_block *block,
|
|
|
|
|
struct util_dynarray *unused_writes)
|
2018-07-27 13:56:35 -07:00
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
2025-08-11 13:18:15 -04:00
|
|
|
util_dynarray_clear(unused_writes);
|
2018-07-27 13:56:35 -07:00
|
|
|
|
|
|
|
|
nir_foreach_instr_safe(instr, block) {
|
|
|
|
|
if (instr->type == nir_instr_type_call) {
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_modes(unused_writes, nir_var_shader_out |
|
2023-08-08 12:00:35 -05:00
|
|
|
nir_var_shader_temp |
|
|
|
|
|
nir_var_function_temp |
|
|
|
|
|
nir_var_mem_ssbo |
|
|
|
|
|
nir_var_mem_shared |
|
|
|
|
|
nir_var_mem_global);
|
2018-07-27 13:56:35 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (instr->type != nir_instr_type_intrinsic)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
|
|
|
|
switch (intrin->intrinsic) {
|
2023-07-28 15:08:00 -04:00
|
|
|
case nir_intrinsic_barrier: {
|
2019-07-18 16:14:03 -07:00
|
|
|
if (nir_intrinsic_memory_semantics(intrin) & NIR_MEMORY_RELEASE) {
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_modes(unused_writes,
|
2019-07-18 16:14:03 -07:00
|
|
|
nir_intrinsic_memory_modes(intrin));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 13:56:35 -07:00
|
|
|
case nir_intrinsic_emit_vertex:
|
|
|
|
|
case nir_intrinsic_emit_vertex_with_counter: {
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_modes(unused_writes, nir_var_shader_out);
|
2018-07-27 13:56:35 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 03:45:22 +02:00
|
|
|
case nir_intrinsic_execute_callable:
|
|
|
|
|
case nir_intrinsic_rt_execute_callable: {
|
2021-03-31 02:41:15 +02:00
|
|
|
/* Mark payload as it can be used by the callee */
|
|
|
|
|
nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_read(unused_writes, src);
|
2021-03-31 02:41:15 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 03:45:22 +02:00
|
|
|
case nir_intrinsic_trace_ray:
|
|
|
|
|
case nir_intrinsic_rt_trace_ray: {
|
2021-03-31 02:41:15 +02:00
|
|
|
/* Mark payload as it can be used by the callees */
|
|
|
|
|
nir_deref_instr *src = nir_src_as_deref(intrin->src[10]);
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_read(unused_writes, src);
|
2021-03-31 02:41:15 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 13:56:35 -07:00
|
|
|
case nir_intrinsic_load_deref: {
|
|
|
|
|
nir_deref_instr *src = nir_src_as_deref(intrin->src[0]);
|
2020-11-05 17:00:56 +00:00
|
|
|
if (nir_deref_mode_must_be(src, nir_var_read_only_modes))
|
|
|
|
|
break;
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_read(unused_writes, src);
|
2018-07-27 13:56:35 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_store_deref: {
|
|
|
|
|
nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);
|
2020-04-27 01:54:40 -05:00
|
|
|
|
|
|
|
|
if (nir_intrinsic_access(intrin) & ACCESS_VOLATILE) {
|
|
|
|
|
/* Consider a volatile write to also be a sort of read. This
|
|
|
|
|
* prevents us from deleting a non-volatile write just before a
|
|
|
|
|
* volatile write thanks to a non-volatile write afterwards. It's
|
|
|
|
|
* quite the corner case, but this should be safer and more
|
|
|
|
|
* predictable for the programmer than allowing two non-volatile
|
|
|
|
|
* writes to be combined with a volatile write between them.
|
|
|
|
|
*/
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_read(unused_writes, dst);
|
2020-04-27 01:54:40 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 13:56:35 -07:00
|
|
|
nir_component_mask_t mask = nir_intrinsic_write_mask(intrin);
|
2025-08-11 13:18:15 -04:00
|
|
|
progress |= update_unused_writes(unused_writes, intrin, dst, mask);
|
2018-07-27 13:56:35 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_copy_deref: {
|
|
|
|
|
nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);
|
|
|
|
|
nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);
|
|
|
|
|
|
2020-04-27 01:54:40 -05:00
|
|
|
if (nir_intrinsic_dst_access(intrin) & ACCESS_VOLATILE) {
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_read(unused_writes, src);
|
|
|
|
|
clear_unused_for_read(unused_writes, dst);
|
2020-04-27 01:54:40 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 13:56:35 -07:00
|
|
|
/* Self-copy is removed. */
|
|
|
|
|
if (nir_compare_derefs(src, dst) & nir_derefs_equal_bit) {
|
|
|
|
|
nir_instr_remove(instr);
|
|
|
|
|
progress = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 13:18:15 -04:00
|
|
|
clear_unused_for_read(unused_writes, src);
|
2018-07-27 13:56:35 -07:00
|
|
|
nir_component_mask_t mask = (1 << glsl_get_vector_elements(dst->type)) - 1;
|
2025-08-11 13:18:15 -04:00
|
|
|
progress |= update_unused_writes(unused_writes, intrin, dst, mask);
|
2018-07-27 13:56:35 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* All unused writes at the end of the block are kept, since we can't be
|
|
|
|
|
* sure they'll be overwritten or not with local analysis only.
|
2025-02-06 15:19:54 -08:00
|
|
|
*
|
|
|
|
|
* However, if the next block is the end of the program, then we can
|
|
|
|
|
* eliminate any shared writes, since no one will ever read it again.
|
2018-07-27 13:56:35 -07:00
|
|
|
*/
|
2025-02-06 15:19:54 -08:00
|
|
|
if (ends_program(block)) {
|
2025-08-11 13:18:15 -04:00
|
|
|
util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
|
2025-02-06 15:19:54 -08:00
|
|
|
if (nir_deref_mode_may_be(entry->dst, nir_var_mem_shared) &&
|
|
|
|
|
nir_deref_mode_is(entry->dst, nir_var_mem_shared)) {
|
|
|
|
|
nir_instr_remove(&entry->intrin->instr);
|
|
|
|
|
progress = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 13:56:35 -07:00
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2025-08-11 13:18:15 -04:00
|
|
|
remove_dead_write_vars_impl(nir_shader *shader, nir_function_impl *impl,
|
|
|
|
|
struct util_dynarray *unused_writes)
|
2018-07-27 13:56:35 -07:00
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
|
|
nir_metadata_require(impl, nir_metadata_block_index);
|
|
|
|
|
|
|
|
|
|
nir_foreach_block(block, impl)
|
2025-08-11 13:18:15 -04:00
|
|
|
progress |= remove_dead_write_vars_local(shader, block, unused_writes);
|
2018-07-27 13:56:35 -07:00
|
|
|
|
treewide: Switch to nir_progress
Via the Coccinelle patch at the end of the commit message, followed by
sed -ie 's/progress = progress | /progress |=/g' $(git grep -l 'progress = prog')
ninja -C ~/mesa/build clang-format
cd ~/mesa/src/compiler/nir && clang-format -i *.c
agxfmt
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
-return prog;
+return nir_progress(prog, impl, metadata);
@@
expression prog_expr, impl, metadata;
@@
-if (prog_expr) {
-nir_metadata_preserve(impl, metadata);
-return true;
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-return false;
-}
+bool progress = prog_expr;
+return nir_progress(progress, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-nir_metadata_preserve(impl, prog ? (metadata) : nir_metadata_all);
-return prog;
+return nir_progress(prog, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-nir_metadata_preserve(impl, prog ? (metadata) : nir_metadata_all);
+nir_progress(prog, impl, metadata);
@@
expression impl, metadata;
@@
-nir_metadata_preserve(impl, metadata);
-return true;
+return nir_progress(true, impl, metadata);
@@
expression impl;
@@
-nir_metadata_preserve(impl, nir_metadata_all);
-return false;
+return nir_no_progress(impl);
@@
identifier other_prog, prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
-other_prog |= prog;
+other_prog = other_prog | nir_progress(prog, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+nir_progress(prog, impl, metadata);
@@
identifier other_prog, prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-other_prog = true;
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+other_prog = other_prog | nir_progress(prog, impl, metadata);
@@
expression prog_expr, impl, metadata;
identifier prog;
@@
-if (prog_expr) {
-nir_metadata_preserve(impl, metadata);
-prog = true;
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+bool impl_progress = prog_expr;
+prog = prog | nir_progress(impl_progress, impl, metadata);
@@
identifier other_prog, prog;
expression impl, metadata;
@@
-if (prog) {
-other_prog = true;
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+other_prog = other_prog | nir_progress(prog, impl, metadata);
@@
expression prog_expr, impl, metadata;
identifier prog;
@@
-if (prog_expr) {
-prog = true;
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+bool impl_progress = prog_expr;
+prog = prog | nir_progress(impl_progress, impl, metadata);
@@
expression prog_expr, impl, metadata;
@@
-if (prog_expr) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+bool impl_progress = prog_expr;
+nir_progress(impl_progress, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-nir_metadata_preserve(impl, metadata);
-prog = true;
+prog = nir_progress(true, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-}
-return prog;
+return nir_progress(prog, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-}
+nir_progress(prog, impl, metadata);
@@
expression impl;
@@
-nir_metadata_preserve(impl, nir_metadata_all);
+nir_no_progress(impl);
@@
expression impl, metadata;
@@
-nir_metadata_preserve(impl, metadata);
+nir_progress(true, impl, metadata);
squashme! sed -ie 's/progress = progress | /progress |=/g' $(git grep -l 'progress = prog')
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Georg Lehmann <dadschoorse@gmail.com>
Acked-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33722>
2025-02-24 15:10:33 -05:00
|
|
|
return nir_progress(progress, impl, nir_metadata_control_flow);
|
2018-07-27 13:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
nir_opt_dead_write_vars(nir_shader *shader)
|
|
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
2025-08-11 13:18:15 -04:00
|
|
|
struct util_dynarray unused_writes;
|
|
|
|
|
util_dynarray_init(&unused_writes, NULL);
|
|
|
|
|
|
2023-06-22 13:27:59 -04:00
|
|
|
nir_foreach_function_impl(impl, shader) {
|
2025-08-11 13:18:15 -04:00
|
|
|
progress |= remove_dead_write_vars_impl(shader, impl, &unused_writes);
|
2018-07-27 13:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-11 13:18:15 -04:00
|
|
|
util_dynarray_fini(&unused_writes);
|
2018-07-27 13:56:35 -07:00
|
|
|
return progress;
|
|
|
|
|
}
|