[Mesa-dev] [PATCH 09/16] RFC: nir: cleanup dead deref instructions

Rob Clark robdclark at gmail.com
Sat Apr 7 16:13:41 UTC 2018


---
 src/compiler/nir/nir.h          |  4 +++-
 src/compiler/nir/nir_deref.c    | 22 ++++++++++++++++++++--
 src/compiler/nir/nir_validate.c |  5 +++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 4ec42b4406b..fe5a79d5f5a 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -979,7 +979,7 @@ nir_deref_instr_get_variable(const nir_deref_instr *instr)
    return instr->var;
 }
 
-void nir_deref_instr_cleanup(nir_deref_instr *instr);
+bool nir_deref_instr_cleanup(nir_deref_instr *instr);
 
 typedef struct {
    nir_instr instr;
@@ -1764,6 +1764,8 @@ typedef struct {
    nir_metadata valid_metadata;
 } nir_function_impl;
 
+void nir_deref_function_cleanup(nir_function_impl *impl);
+
 ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
 nir_start_block(nir_function_impl *impl)
 {
diff --git a/src/compiler/nir/nir_deref.c b/src/compiler/nir/nir_deref.c
index b8fb448f8f3..886febd63bc 100644
--- a/src/compiler/nir/nir_deref.c
+++ b/src/compiler/nir/nir_deref.c
@@ -73,17 +73,35 @@ nir_deref_path_finish(struct nir_deref_path *path)
       ralloc_free(path->path);
 }
 
-void
+bool
 nir_deref_instr_cleanup(nir_deref_instr *instr)
 {
+   bool progress = false;
+
    for (nir_deref_instr *d = instr; d; d = nir_deref_instr_parent(d)) {
       /* If anyone is using this deref, leave it alone */
       assert(d->dest.is_ssa);
       if (!list_empty(&d->dest.ssa.uses))
-         return;
+         break;
 
       nir_instr_remove(&d->instr);
+      progress = true;
    }
+
+   return progress;
+}
+
+void
+nir_deref_function_cleanup(nir_function_impl *impl)
+{
+   bool progress;
+   do {
+      progress = false;
+      nir_foreach_block(block, impl)
+         nir_foreach_instr_safe(instr, block)
+            if (instr->type == nir_instr_type_deref)
+               progress |= nir_deref_instr_cleanup(nir_instr_as_deref(instr));
+   } while (progress);
 }
 
 void
diff --git a/src/compiler/nir/nir_validate.c b/src/compiler/nir/nir_validate.c
index 6820f4ed622..da5d8fb0f74 100644
--- a/src/compiler/nir/nir_validate.c
+++ b/src/compiler/nir/nir_validate.c
@@ -1084,6 +1084,11 @@ validate_function_impl(nir_function_impl *impl, validate_state *state)
    state->impl = impl;
    state->parent_node = &impl->cf_node;
 
+   /* clean up dead deref instructions, which can exist at the exit of
+    * one pass, but before running DCE, since they confuse nir_validate:
+    */
+   nir_deref_function_cleanup(impl);
+
    exec_list_validate(&impl->locals);
    nir_foreach_variable(var, &impl->locals) {
       validate_var_decl(var, false, state);
-- 
2.14.3



More information about the mesa-dev mailing list