[Mesa-dev] [PATCH 2/5] glsl: Propagate negates through multiplication chains.

Ian Romanick idr at freedesktop.org
Wed Apr 8 16:38:28 PDT 2015


From: Matt Turner <mattst88 at gmail.com>

We propagate negations to the right-most leaves of the multiplication
expression trees:

 - mul(neg(x), neg(y)) -> mul(x, y)
 - mul(neg(x), y) -> neg(mul(x, y))
 - mul(x, neg(y)) -> neg(mul(x, y))

Sandy Bridge w/o NIR and Broadwell w/o NIR are the only platforms hurt
by this change.

Shader-db results:

GM45:
total instructions in shared programs: 4061744 -> 4060813 (-0.02%)
instructions in affected programs:     294024 -> 293093 (-0.32%)
helped:                                991
HURT:                                  332

GM45 NIR:
total instructions in shared programs: 4080646 -> 4079727 (-0.02%)
instructions in affected programs:     269940 -> 269021 (-0.34%)
helped:                                935
HURT:                                  328

Iron Lake:
total instructions in shared programs: 5478897 -> 5477992 (-0.02%)
instructions in affected programs:     363125 -> 362220 (-0.25%)
helped:                                1127
HURT:                                  373

Iron Lake NIR:
total instructions in shared programs: 5677395 -> 5676542 (-0.02%)
instructions in affected programs:     316047 -> 315194 (-0.27%)
helped:                                1028
HURT:                                  371

Sandy Bridge:
total instructions in shared programs: 7308115 -> 7308267 (0.00%)
instructions in affected programs:     520138 -> 520290 (0.03%)
helped:                                2071
HURT:                                  1316
LOST:                                  8

Sandy Bridge NIR:
total instructions in shared programs: 7328069 -> 7325820 (-0.03%)
instructions in affected programs:     836683 -> 834434 (-0.27%)
helped:                                3032
HURT:                                  1349
GAINED:                                4

Ivy Bridge:
total instructions in shared programs: 6765781 -> 6763909 (-0.03%)
instructions in affected programs:     337545 -> 335673 (-0.55%)
helped:                                2183
HURT:                                  390

Ivy Bridge NIR:
total instructions in shared programs: 6768159 -> 6764295 (-0.06%)
instructions in affected programs:     621048 -> 617184 (-0.62%)
helped:                                3102
HURT:                                  426
GAINED:                                2

Haswell:
total instructions in shared programs: 6225372 -> 6223591 (-0.03%)
instructions in affected programs:     323481 -> 321700 (-0.55%)
helped:                                2170
HURT:                                  401

Haswell NIR:
total instructions in shared programs: 6182538 -> 6180262 (-0.04%)
instructions in affected programs:     434003 -> 431727 (-0.52%)
helped:                                2597
HURT:                                  422
GAINED:                                2

Broadwell:
total instructions in shared programs: 7284537 -> 7284561 (0.00%)
instructions in affected programs:     166451 -> 166475 (0.01%)
helped:                                561
HURT:                                  313

Broadwell NIR:
total instructions in shared programs: 7501544 -> 7499619 (-0.03%)
instructions in affected programs:     698111 -> 696186 (-0.28%)
helped:                                2157
HURT:                                  396
GAINED:                                2

Reviewed-by: Ben Widawsky <ben at bwidawsk.net>
Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/glsl/opt_algebraic.cpp | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 3d2f2ca..473eb90 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -549,6 +549,21 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
          return b2f(logic_and(op_expr[0]->operands[0], op_expr[1]->operands[0]));
       }
 
+      if (op_expr[0] && op_expr[0]->operation == ir_unop_neg) {
+         if (op_expr[1] && op_expr[1]->operation == ir_unop_neg) {
+            /* mul(neg(x), neg(y)) -> mul(x, y) */
+            return mul(op_expr[0]->operands[0], op_expr[1]->operands[0]);
+         }
+
+         /* mul(neg(x), y) -> neg(mul(x, y)) */
+         return neg(mul(op_expr[0]->operands[0], ir->operands[1]));
+      }
+
+      /* mul(x, neg(y)) -> neg(mul(x, y)) */
+      if (op_expr[1] && op_expr[1]->operation == ir_unop_neg) {
+         return neg(mul(ir->operands[0], op_expr[1]->operands[0]));
+      }
+
       /* Reassociate multiplication of constants so that we can do
        * constant folding.
        */
-- 
2.1.0



More information about the mesa-dev mailing list