[Mesa-dev] [PATCH 17/23] glsl: Distribute ir_unop_neg over ir_binop_mul of a constant

Ian Romanick idr at freedesktop.org
Fri Mar 20 13:58:17 PDT 2015


From: Ian Romanick <ian.d.romanick at intel.com>

An expression like -(x * 8) becomes (x * -8).

Much of the hurt caused by this change appears to be from CSE not being
able to see (x * -8) and (x * 8) as common.  Previously -(x * 8) and (x
* 8) would have had a common subexpression.  Later patches address this
at the GLSL IR level, but additional work will be needed in NIR and the
i965 backend.

Shader-db results:

GM45 (0x2A42):
total instructions in shared programs: 3544299 -> 3544158 (-0.00%)
instructions in affected programs:     19768 -> 19627 (-0.71%)
helped:                                33
HURT:                                  52

Iron Lake (0x0046):
total instructions in shared programs: 4974090 -> 4973887 (-0.00%)
instructions in affected programs:     22831 -> 22628 (-0.89%)
helped:                                54
HURT:                                  55

Sandy Bridge (0x0116):
total instructions in shared programs: 6799711 -> 6799717 (0.00%)
instructions in affected programs:     1410 -> 1416 (0.43%)
helped:                                9
HURT:                                  5

Sandy Bridge (0x0116) NIR:
total instructions in shared programs: 6789759 -> 6785948 (-0.06%)
instructions in affected programs:     929330 -> 925519 (-0.41%)
helped:                                1689
HURT:                                  2129
GAINED:                                3
LOST:                                  6

Ivy Bridge (0x0166):
total instructions in shared programs: 6274069 -> 6274097 (0.00%)
instructions in affected programs:     1275 -> 1303 (2.20%)
helped:                                3
HURT:                                  12

Ivy Bridge (0x0166) NIR:
total instructions in shared programs: 6300857 -> 6296339 (-0.07%)
instructions in affected programs:     783687 -> 779169 (-0.58%)
helped:                                1704
HURT:                                  1858
GAINED:                                7
LOST:                                  3

Haswell (0x0426):
total instructions in shared programs: 5760186 -> 5760202 (0.00%)
instructions in affected programs:     1176 -> 1192 (1.36%)
helped:                                4
HURT:                                  11

Haswell (0x0426) NIR:
total instructions in shared programs: 5780293 -> 5766919 (-0.23%)
instructions in affected programs:     749765 -> 736391 (-1.78%)
helped:                                3281
HURT:                                  217
GAINED:                                1
LOST:                                  3

Broadwell (0x162E):
total instructions in shared programs: 6808264 -> 6808264 (0.00%)
instructions in affected programs:     1224 -> 1224 (0.00%)
helped:                                5
HURT:                                  10

Broadwell (0x162E) NIR:
total instructions in shared programs: 6995761 -> 6982615 (-0.19%)
instructions in affected programs:     782786 -> 769640 (-1.68%)
helped:                                3452
HURT:                                  230
GAINED:                                10
LOST:                                  3

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
Cc: Jason Ekstrand <jason.ekstrand at intel.com>
---
 src/glsl/opt_algebraic.cpp | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 0f15f52..af22bed 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -443,6 +443,27 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       if (op_expr[0]->operation == ir_unop_neg) {
          return op_expr[0]->operands[0];
       }
+
+      /* Replace -(x * constant) with (x * -constant).
+       */
+      if (op_expr[0]->operation == ir_binop_mul) {
+         for (i = 0; i < 2; i++) {
+            if (op_expr[0]->operands[i]->as_constant()) {
+               ir_expression *const tmp_expr =
+                  new(mem_ctx) ir_expression(ir_unop_neg,
+                                             op_expr[0]->operands[i]);
+
+               /* Constant-fold the expression now to (possibly) save an
+                * interation through the optimization loop.
+                */
+               op_expr[0]->operands[i] =
+                  tmp_expr->constant_expression_value();
+
+               assert(op_expr[0]->operands[i] != NULL);
+               return op_expr[0];
+            }
+         }
+      }
       break;
 
    case ir_unop_exp:
-- 
2.1.0



More information about the mesa-dev mailing list