nir: Support deref instructions in remove_dead_variables
authorJason Ekstrand <jason.ekstrand@intel.com>
Tue, 20 Mar 2018 21:16:06 +0000 (14:16 -0700)
committerJason Ekstrand <jason.ekstrand@intel.com>
Sat, 23 Jun 2018 03:15:54 +0000 (20:15 -0700)
Reviewed-by: Caio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Acked-by: Rob Clark <robdclark@gmail.com>
Acked-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Acked-by: Dave Airlie <airlied@redhat.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/compiler/nir/nir_remove_dead_variables.c

index 4a36ef9ba528c953e0285c5ecfd1ea62341c15b3..89e544f9e1f377cdc4dd02d8beab6e05565b5c4b 100644 (file)
 
 #include "nir.h"
 
+static bool
+deref_used_for_not_store(nir_deref_instr *deref)
+{
+   nir_foreach_use(src, &deref->dest.ssa) {
+      switch (src->parent_instr->type) {
+      case nir_instr_type_deref:
+         if (deref_used_for_not_store(nir_instr_as_deref(src->parent_instr)))
+            return true;
+         break;
+
+      case nir_instr_type_intrinsic: {
+         nir_intrinsic_instr *intrin =
+            nir_instr_as_intrinsic(src->parent_instr);
+         /* The first source of copy and store intrinsics is the deref to
+          * write.  Don't record those.
+          */
+         if ((intrin->intrinsic != nir_intrinsic_store_deref &&
+              intrin->intrinsic != nir_intrinsic_copy_deref) ||
+             src != &intrin->src[0])
+            return true;
+         break;
+      }
+
+      default:
+         /* If it's used by any other instruction type (most likely a texture
+          * instruction), consider it used.
+          */
+         return true;
+      }
+   }
+
+   return false;
+}
+
+static void
+add_var_use_deref(nir_deref_instr *deref, struct set *live)
+{
+   if (deref->deref_type != nir_deref_type_var)
+      return;
+
+   /* If it's not a local that never escapes the shader, then any access at
+    * all means we need to keep it alive.
+    */
+   assert(deref->mode == deref->var->data.mode);
+   if (!(deref->mode & (nir_var_local | nir_var_global | nir_var_shared)) ||
+       deref_used_for_not_store(deref))
+      _mesa_set_add(live, deref->var);
+}
+
 static void
 add_var_use_intrinsic(nir_intrinsic_instr *instr, struct set *live,
                       nir_variable_mode modes)
@@ -100,6 +149,10 @@ add_var_use_shader(nir_shader *shader, struct set *live, nir_variable_mode modes
          nir_foreach_block(block, function->impl) {
             nir_foreach_instr(instr, block) {
                switch(instr->type) {
+               case nir_instr_type_deref:
+                  add_var_use_deref(nir_instr_as_deref(instr), live);
+                  break;
+
                case nir_instr_type_intrinsic:
                   add_var_use_intrinsic(nir_instr_as_intrinsic(instr), live,
                                         modes);
@@ -144,6 +197,45 @@ remove_dead_var_writes(nir_shader *shader, struct set *live)
                nir_instr_remove(instr);
          }
       }
+
+      nir_foreach_block(block, function->impl) {
+         nir_foreach_instr_safe(instr, block) {
+            switch (instr->type) {
+            case nir_instr_type_deref: {
+               nir_deref_instr *deref = nir_instr_as_deref(instr);
+
+               nir_variable_mode parent_mode;
+               if (deref->deref_type == nir_deref_type_var)
+                  parent_mode = deref->var->data.mode;
+               else
+                  parent_mode = nir_deref_instr_parent(deref)->mode;
+
+               /* If the parent mode is 0, then it references a dead variable.
+                * Flag this deref as dead and remove it.
+                */
+               if (parent_mode == 0) {
+                  deref->mode = 0;
+                  nir_instr_remove(&deref->instr);
+               }
+               break;
+            }
+
+            case nir_instr_type_intrinsic: {
+               nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+               if (intrin->intrinsic != nir_intrinsic_copy_deref &&
+                   intrin->intrinsic != nir_intrinsic_store_deref)
+                  break;
+
+               if (nir_src_as_deref(intrin->src[0])->mode == 0)
+                  nir_instr_remove(instr);
+               break;
+            }
+
+            default:
+               break; /* Nothing to do */
+            }
+         }
+      }
    }
 }
 
@@ -172,8 +264,6 @@ nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes)
    struct set *live =
       _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
 
-   nir_assert_lowered_derefs(shader, nir_lower_all_derefs);
-
    add_var_use_shader(shader, live, modes);
 
    if (modes & nir_var_uniform)