[Mesa-dev] [PATCH 4/4] nir: Move fsat outside of fmin/fmax if second arg is 0 to 1.

Matt Turner mattst88 at gmail.com
Wed Nov 30 01:38:55 UTC 2016


instructions in affected programs: 550 -> 544 (-1.09%)
helped: 6

cycles in affected programs: 6952 -> 6850 (-1.47%)
helped: 6
---
 src/compiler/nir/nir_opt_algebraic.py |  2 ++
 src/compiler/nir/nir_search_helpers.h | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py
index 82d92f4..698ac67 100644
--- a/src/compiler/nir/nir_opt_algebraic.py
+++ b/src/compiler/nir/nir_opt_algebraic.py
@@ -175,6 +175,8 @@ optimizations = [
    (('fmin', ('fmax', ('fmin', ('fmax', a, b), c), b), c), ('fmin', ('fmax', a, b), c)),
    (('imin', ('imax', ('imin', ('imax', a, b), c), b), c), ('imin', ('imax', a, b), c)),
    (('umin', ('umax', ('umin', ('umax', a, b), c), b), c), ('umin', ('umax', a, b), c)),
+   (('fmax', ('fsat', a), '#b at 32(is_zero_to_one)'), ('fsat', ('fmax', a, b))),
+   (('fmin', ('fsat', a), '#b at 32(is_zero_to_one)'), ('fsat', ('fmin', a, b))),
    (('extract_u8', ('imin', ('imax', a, 0), 0xff), 0), ('imin', ('imax', a, 0), 0xff)),
    (('~ior', ('flt', a, b), ('flt', a, c)), ('flt', a, ('fmax', b, c))),
    (('~ior', ('flt', a, c), ('flt', b, c)), ('flt', ('fmin', a, b), c)),
diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h
index 5037944..20fdae6 100644
--- a/src/compiler/nir/nir_search_helpers.h
+++ b/src/compiler/nir/nir_search_helpers.h
@@ -91,4 +91,27 @@ is_neg_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
    return true;
 }
 
+static inline bool
+is_zero_to_one(nir_alu_instr *instr, unsigned src, unsigned num_components,
+               const uint8_t *swizzle)
+{
+   nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
+
+   if (!val)
+      return false;
+
+   for (unsigned i = 0; i < num_components; i++) {
+      switch (nir_op_infos[instr->op].input_types[src]) {
+      case nir_type_float:
+         if (val->f32[swizzle[i]] < 0.0f || val->f32[swizzle[i]] > 1.0f)
+            return false;
+         break;
+      default:
+         return false;
+      }
+   }
+
+   return true;
+}
+
 #endif /* _NIR_SEARCH_ */
-- 
2.7.3



More information about the mesa-dev mailing list