[Mesa-dev] [PATCH 6/8] nir/gcm: Delete dead instructions

Jason Ekstrand jason at jlekstrand.net
Wed Jan 18 02:38:40 UTC 2017


Classically, global code motion is also a dead code pass.  However, in
the initial implementation, the decision was made to place every
instruction and let conventional DCE clean up the dead ones.  Because
any uses of a dead instruction are unreachable, we have no late block
and the dead instructions are always scheduled early.  The problem is
that, because we place the dead instruction early, it  pushes the
placement of any dependencies of the dead instruction earlier than they
may need to be placed.  In order prevent dead instructions from
affecting the placement of live ones, we need to delete them.
---
 src/compiler/nir/nir_opt_gcm.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/compiler/nir/nir_opt_gcm.c b/src/compiler/nir/nir_opt_gcm.c
index 5b95d6b..9d61c65 100644
--- a/src/compiler/nir/nir_opt_gcm.c
+++ b/src/compiler/nir/nir_opt_gcm.c
@@ -356,11 +356,11 @@ gcm_schedule_late_def(nir_ssa_def *def, void *void_state)
    nir_block *early_block =
       state->instr_infos[def->parent_instr->index].early_block;
 
-   /* Some instructions may never be used.  We'll just schedule them early and
-    * let dead code clean them up.
+   /* Some instructions may never be used.  Flag them and the instruction
+    * placement code will get rid of them for us.
     */
    if (lca == NULL) {
-      def->parent_instr->block = early_block;
+      def->parent_instr->block = NULL;
       return true;
    }
 
@@ -423,6 +423,23 @@ gcm_place_instr_def(nir_ssa_def *def, void *state)
    return false;
 }
 
+static bool
+gcm_replace_def_with_undef(nir_ssa_def *def, void *void_state)
+{
+   struct gcm_state *state = void_state;
+
+   if (list_empty(&def->uses) && list_empty(&def->if_uses))
+      return true;
+
+   nir_ssa_undef_instr *undef =
+      nir_ssa_undef_instr_create(state->impl->function->shader,
+                                 def->num_components, def->bit_size);
+   nir_instr_insert(nir_before_cf_list(&state->impl->body), &undef->instr);
+   nir_ssa_def_rewrite_uses(def, nir_src_for_ssa(&undef->def));
+
+   return true;
+}
+
 /** Places an instrution back into the program
  *
  * The earlier passes of GCM simply choose blocks for each instruction and
@@ -446,6 +463,12 @@ gcm_place_instr(nir_instr *instr, struct gcm_state *state)
 
    instr->pass_flags |= GCM_INSTR_PLACED;
 
+   if (instr->block == NULL) {
+      nir_foreach_ssa_def(instr, gcm_replace_def_with_undef, state);
+      nir_instr_remove(instr);
+      return;
+   }
+
    /* Phi nodes are our once source of back-edges.  Since right now we are
     * only doing scheduling within blocks, we don't need to worry about
     * them since they are always at the top.  Just skip them completely.
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list