r600/sfn: Base instr lowering class on nir_lower_instruction code
authorGert Wollny <gert.wollny@collabora.com>
Sat, 20 Feb 2021 09:58:36 +0000 (10:58 +0100)
committerMarge Bot <eric+marge@anholt.net>
Mon, 22 Feb 2021 20:43:02 +0000 (20:43 +0000)
Now that the lowering pass has been update there is no need to
duplicate the code to scan the shader.

Signed-off-by: Gert Wollny <gert.wollny@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9167>

src/gallium/drivers/r600/sfn/sfn_nir.cpp
src/gallium/drivers/r600/sfn/sfn_nir.h
src/gallium/drivers/r600/sfn/sfn_nir_lower_64bit.cpp

index 5ce326e..307f1b4 100644 (file)
@@ -54,92 +54,25 @@ NirLowerInstruction::NirLowerInstruction():
 
 }
 
-bool NirLowerInstruction::run(nir_shader *shader)
+bool NirLowerInstruction::filter_instr(const nir_instr *instr, const void *data)
 {
-   bool progress = false;
-
-   nir_metadata preserved = nir_metadata_block_index |
-                            nir_metadata_dominance;
-
-   nir_foreach_function(function, shader) {
-      if (function->impl) {
-         nir_builder builder;
-         b = &builder;
-         nir_builder_init(b, function->impl);
-         nir_foreach_block(block, function->impl) {
-            nir_foreach_instr_safe(instr, block) {
-               if (!filter(instr))
-                  continue;
-
-               nir_ssa_def *old_def = nir_instr_ssa_def(instr);
-               struct list_head old_uses, old_if_uses;
-               if (old_def != nullptr) {
-                  list_replace(&old_def->uses, &old_uses);
-                  list_inithead(&old_def->uses);
-                  list_replace(&old_def->if_uses, &old_if_uses);
-                  list_inithead(&old_def->if_uses);
-               }
-
-               b->cursor = nir_after_instr(instr);
-               nir_ssa_def *new_def = lower(instr);
-               if (new_def && new_def != progress_keep &&
-                   new_def != progress_replace) {
-                  assert(old_def != NULL);
-                  if (new_def->parent_instr->block != instr->block)
-                     preserved = nir_metadata_none;
-
-                  nir_src new_src = nir_src_for_ssa(new_def);
-                  list_for_each_entry_safe(nir_src, use_src, &old_uses, use_link)
-                        nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
-
-                  list_for_each_entry_safe(nir_src, use_src, &old_if_uses, use_link)
-                        nir_if_rewrite_condition(use_src->parent_if, new_src);
-
-                  if (list_is_empty(&old_def->uses) &&
-                      list_is_empty(&old_def->if_uses)) {
-                     nir_instr_remove(instr);
-                  }
-                  progress = true;
-               } else {
-                  /* We didn't end up lowering after all.  Put the uses back */
-                  if (old_def) {
-                     list_replace(&old_uses, &old_def->uses);
-                     list_replace(&old_if_uses, &old_def->if_uses);
-                  }
-                  if (new_def == progress_replace) {
-                     /* Only instructions without a return value can be removed like this */
-                     assert(!old_def);
-                     nir_instr_remove(instr);
-                     progress = true;
-                  }
-
-                  if (new_def == progress_keep)
-                     progress = true;
-               }
-            }
-
-            if (progress) {
-               nir_metadata_preserve(function->impl, preserved);
-            } else {
-               nir_metadata_preserve(function->impl, nir_metadata_all);
-            }
-         }
-      }
-   }
-   return progress;
+   auto me = reinterpret_cast<const NirLowerInstruction*>(data);
+   return me->filter(instr);
 }
 
-nir_ssa_def *NirLowerInstruction::progress_keep = (nir_ssa_def *)1;
-nir_ssa_def *NirLowerInstruction::progress_replace = (nir_ssa_def *)2;
-
+nir_ssa_def *NirLowerInstruction::lower_instr(nir_builder *b, nir_instr *instr, void *data)
+{
+   auto me = reinterpret_cast<NirLowerInstruction*>(data);
+   me->set_builder(b);
+   return me->lower(instr);
+}
 
-bool NirLowerInstruction::run(nir_instr *instr)
+bool NirLowerInstruction::run(nir_shader *shader)
 {
-       bool progress = false;
-       if (filter(instr)) {
-               progress = lower(instr);
-       }
-       return progress;
+   return nir_shader_lower_instructions(shader,
+                                        filter_instr,
+                                        lower_instr,
+                                        (void *)this);
 }
 
 
index 2740f42..fe77f68 100644 (file)
@@ -43,15 +43,15 @@ public:
        bool run(nir_shader *shader);
 
 private:
-       bool run(nir_instr *instr);
+       static bool filter_instr(const nir_instr *instr, const void *data);
+        static nir_ssa_def *lower_instr(nir_builder *b, nir_instr *instr,  void *data);
+
+        void set_builder(nir_builder *_b) { b = _b;}
 
        virtual bool filter(const nir_instr *instr) const = 0;
        virtual nir_ssa_def *lower(nir_instr *instr) = 0;
 protected:
        nir_builder *b;
-
-        static nir_ssa_def *progress_keep;
-        static nir_ssa_def *progress_replace;
 };
 
 bool r600_nir_lower_pack_unpack_2x16(nir_shader *shader);
index fcff6af..d953830 100644 (file)
@@ -267,7 +267,7 @@ LowerSplit64BitVar::split_store_deref_array(nir_intrinsic_instr *intr, nir_deref
    else
       nir_build_store_deref(b, &deref_array2->dest.ssa, nir_channels(b, intr->src[1].ssa, 0xc), 3);
 
-   return progress_replace;
+   return NIR_LOWER_INSTR_PROGRESS_REPLACE;
 }
 
 nir_ssa_def *
@@ -291,7 +291,7 @@ LowerSplit64BitVar::split_store_deref_var(nir_intrinsic_instr *intr, nir_deref_i
    else
       nir_build_store_deref(b, &deref2->dest.ssa, nir_channels(b, intr->src[1].ssa, 0xc), 3);
 
-   return progress_replace;
+   return NIR_LOWER_INSTR_PROGRESS_REPLACE;
 }
 
 nir_ssa_def *
@@ -394,7 +394,7 @@ LowerSplit64BitVar::split_store_output(nir_intrinsic_instr *store1)
    nir_intrinsic_set_base(store2, nir_intrinsic_base(store1));
 
    nir_builder_instr_insert(b, &store2->instr);
-   return progress_keep;
+   return NIR_LOWER_INSTR_PROGRESS;
 }
 
 
@@ -711,13 +711,13 @@ Lower64BitToVec2::lower(nir_instr *instr)
       default:
          return NULL;
       }
-      return progress_keep;
+      return NIR_LOWER_INSTR_PROGRESS;
    }
    case nir_instr_type_phi: {
       auto phi = nir_instr_as_phi(instr);
       phi->dest.ssa.bit_size = 32;
       phi->dest.ssa.num_components = 2;
-      return progress_keep;
+      return NIR_LOWER_INSTR_PROGRESS;
    }
    case nir_instr_type_load_const:  {
       auto lc = nir_instr_as_load_const(instr);
@@ -735,7 +735,7 @@ Lower64BitToVec2::lower(nir_instr *instr)
       auto undef = nir_instr_as_ssa_undef(instr);
       undef->def.num_components *= 2;
       undef->def.bit_size = 32;
-      return progress_keep;
+      return NIR_LOWER_INSTR_PROGRESS;
    }
    default:
       return nullptr;
@@ -774,7 +774,7 @@ Lower64BitToVec2::load_deref_64_to_vec2(nir_intrinsic_instr *intr)
    intr->num_components = components;
    intr->dest.ssa.bit_size = 32;
    intr->dest.ssa.num_components = components;
-   return progress_keep;
+   return NIR_LOWER_INSTR_PROGRESS;
 }
 
 nir_ssa_def *
@@ -805,7 +805,7 @@ Lower64BitToVec2::store_64_to_vec2(nir_intrinsic_instr *intr)
    }
    intr->num_components = components;
    nir_intrinsic_set_write_mask(intr, wrmask == 1 ? 3 : 0xf);
-   return progress_keep;
+   return NIR_LOWER_INSTR_PROGRESS;
 }
 
 
@@ -816,7 +816,7 @@ Lower64BitToVec2::load_uniform_64_to_vec2(nir_intrinsic_instr *intr)
    intr->dest.ssa.bit_size = 32;
    intr->dest.ssa.num_components *= 2;
    nir_intrinsic_set_dest_type(intr, nir_type_float32);
-   return progress_keep;
+   return NIR_LOWER_INSTR_PROGRESS;
 }
 
 nir_ssa_def *
@@ -826,7 +826,7 @@ Lower64BitToVec2::load_64_to_vec2(nir_intrinsic_instr *intr)
    intr->dest.ssa.bit_size = 32;
    intr->dest.ssa.num_components *= 2;
    nir_intrinsic_set_component(intr, nir_intrinsic_component(intr) * 2);
-   return progress_keep;
+   return NIR_LOWER_INSTR_PROGRESS;
 }
 
 nir_ssa_def *
@@ -835,7 +835,7 @@ Lower64BitToVec2::load_ssbo_64_to_vec2(nir_intrinsic_instr *intr)
    intr->num_components *= 2;
    intr->dest.ssa.bit_size = 32;
    intr->dest.ssa.num_components *= 2;
-   return progress_keep;
+   return NIR_LOWER_INSTR_PROGRESS;
 }
 
 static bool store_64bit_intr(nir_src *src, void *state)