Mesa (main): nir/deref: Add an alu-of-cast optimization

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Apr 29 00:00:05 UTC 2022


Module: Mesa
Branch: main
Commit: c31db58f656a99796ef1431b2f5063935c86d4a4
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=c31db58f656a99796ef1431b2f5063935c86d4a4

Author: Jason Ekstrand <jason.ekstrand at collabora.com>
Date:   Wed Mar 30 10:57:19 2022 -0500

nir/deref: Add an alu-of-cast optimization

Casts shouldn't change the bit pattern of the deref and you have to cast
again after you're done with the ALU anyway so we can ignore casts on
ALU sources.  This means we can actually start constant folding NULL
checks even if there are annoying casts in the way.

Reviewed-by: Karol Herbst <kherbst at redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15673>

---

 src/compiler/nir/nir_deref.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/compiler/nir/nir_deref.c b/src/compiler/nir/nir_deref.c
index cb6847ac9b8..90ad90d6763 100644
--- a/src/compiler/nir/nir_deref.c
+++ b/src/compiler/nir/nir_deref.c
@@ -851,6 +851,30 @@ nir_deref_instr_fixup_child_types(nir_deref_instr *parent)
    }
 }
 
+static bool
+opt_alu_of_cast(nir_alu_instr *alu)
+{
+   bool progress = false;
+
+   for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
+      assert(alu->src[i].src.is_ssa);
+      nir_instr *src_instr = alu->src[i].src.ssa->parent_instr;
+      if (src_instr->type != nir_instr_type_deref)
+         continue;
+
+      nir_deref_instr *src_deref = nir_instr_as_deref(src_instr);
+      if (src_deref->deref_type != nir_deref_type_cast)
+         continue;
+
+      assert(src_deref->parent.is_ssa);
+      nir_instr_rewrite_src_ssa(&alu->instr, &alu->src[i].src,
+                                src_deref->parent.ssa);
+      progress = true;
+   }
+
+   return progress;
+}
+
 static bool
 is_trivial_array_deref_cast(nir_deref_instr *cast)
 {
@@ -1347,6 +1371,13 @@ nir_opt_deref_impl(nir_function_impl *impl)
          b.cursor = nir_before_instr(instr);
 
          switch (instr->type) {
+         case nir_instr_type_alu: {
+            nir_alu_instr *alu = nir_instr_as_alu(instr);
+            if (opt_alu_of_cast(alu))
+               progress = true;
+            break;
+         }
+
          case nir_instr_type_deref: {
             nir_deref_instr *deref = nir_instr_as_deref(instr);
 



More information about the mesa-commit mailing list