nir/constant_folding: remove zero texel offset
authorRhys Perry <pendingchaos02@gmail.com>
Fri, 29 Sep 2023 15:33:50 +0000 (16:33 +0100)
committerMarge Bot <emma+marge@anholt.net>
Mon, 2 Oct 2023 10:11:37 +0000 (10:11 +0000)
fossil-db (navi31):
Totals from 7 (0.01% of 79330) affected shaders:
Instrs: 7001 -> 6993 (-0.11%)
CodeSize: 35736 -> 35692 (-0.12%)
InvThroughput: 3232 -> 3229 (-0.09%)
Copies: 552 -> 549 (-0.54%)
PreSGPRs: 277 -> 273 (-1.44%)

Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25477>

src/compiler/nir/nir_opt_constant_folding.c

index 7231678..2e5987c 100644 (file)
@@ -338,6 +338,27 @@ try_fold_tex_offset(nir_tex_instr *tex, unsigned *index,
 }
 
 static bool
+try_fold_texel_offset_src(nir_tex_instr *tex)
+{
+   int offset_src = nir_tex_instr_src_index(tex, nir_tex_src_offset);
+   if (offset_src < 0)
+      return false;
+
+   unsigned size = nir_tex_instr_src_size(tex, offset_src);
+   nir_tex_src *src = &tex->src[offset_src];
+
+   for (unsigned i = 0; i < size; i++) {
+      nir_scalar comp = nir_scalar_resolved(src->src.ssa, i);
+      if (!nir_scalar_is_const(comp) || nir_scalar_as_uint(comp) != 0)
+         return false;
+   }
+
+   nir_tex_instr_remove_src(tex, offset_src);
+
+   return true;
+}
+
+static bool
 try_fold_tex(nir_builder *b, nir_tex_instr *tex)
 {
    bool progress = false;
@@ -351,6 +372,9 @@ try_fold_tex(nir_builder *b, nir_tex_instr *tex)
    if (tex->op == nir_texop_txb)
       progress |= try_fold_txb_to_tex(b, tex);
 
+   /* tex with a zero offset is just tex. */
+   progress |= try_fold_texel_offset_src(tex);
+
    return progress;
 }