[Mesa-dev] [PATCH 23/23] glsl: Rewrite ir_triop_csel when condition is ir_unop_logic_not

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


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

An expression like

    x = !condition ? a : b;

will get rewritten as

    x = condition ? b : a;

This has two immediate advantages.  First, it saves a (very small)
amount of memory.  Second, and more importantly, it means that
transformations that look at the condition of an ir_triop_csel don't
have to handle cases where the root of the condition's expression tree
is an ir_unop_logic_not.

For example, commit 467077b adds an optimization to the i965 FS backend
for expressions like

    x = gl_FrontFacing ? 1.0 : 0.0;

However, that optimization is oblivious to a preceeding logical-not, so
it fails to optimize an expression like

    x = !gl_FrontFacing ? 0.0 : 1.0;

even though they are functionally identical.

The one place in shader-db that uses !gl_FrontFacing still doesn't hit
the optimization from 467077b because it uses integer values.

Shader-db results:

GM45 (0x2A42):
total instructions in shared programs: 3544133 -> 3544141 (0.00%)
instructions in affected programs:     1410 -> 1418 (0.57%)
HURT:                                  8

Iron Lake (0x0046):
total instructions in shared programs: 4973849 -> 4973858 (0.00%)
instructions in affected programs:     1606 -> 1615 (0.56%)
HURT:                                  9

Sandy Bridge (0x0116):
total instructions in shared programs: 6799682 -> 6799644 (-0.00%)
instructions in affected programs:     5926 -> 5888 (-0.64%)
helped:                                22
HURT:                                  6

Sandy Bridge (0x0116) NIR:
total instructions in shared programs: 6787227 -> 6787141 (-0.00%)
instructions in affected programs:     9016 -> 8930 (-0.95%)
helped:                                42

Ivy Bridge (0x0166):
total instructions in shared programs: 6273868 -> 6273830 (-0.00%)
instructions in affected programs:     5491 -> 5453 (-0.69%)
helped:                                22
HURT:                                  6

Ivy Bridge (0x0166) NIR:
total instructions in shared programs: 6298635 -> 6298549 (-0.00%)
instructions in affected programs:     8199 -> 8113 (-1.05%)
helped:                                42

Haswell (0x0426):
total instructions in shared programs: 5760189 -> 5760151 (-0.00%)
instructions in affected programs:     5030 -> 4992 (-0.76%)
helped:                                22
HURT:                                  6

Haswell (0x0426) NIR:
total instructions in shared programs: 5767160 -> 5767074 (-0.00%)
instructions in affected programs:     7450 -> 7364 (-1.15%)
helped:                                42

Broadwell (0x162E):
total instructions in shared programs: 6808224 -> 6808186 (-0.00%)
instructions in affected programs:     4978 -> 4940 (-0.76%)
helped:                                22
HURT:                                  6

Broadwell (0x162E) NIR:
total instructions in shared programs: 6986297 -> 6986211 (-0.00%)
instructions in affected programs:     7410 -> 7324 (-1.16%)
helped:                                42

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

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index af22bed..f8bbb2a 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -1156,6 +1156,17 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
          if (is_vec_one(op_const[1]) && is_vec_zero(op_const[2]))
             return logic_not(ir->operands[0]);
       }
+
+      /* Optimize an expression like 'mix(a, b, !condition)' to 'mix(b, a,
+       * condition)'.
+       */
+      if (op_expr[0] && op_expr[0]->operation == ir_unop_logic_not) {
+         ir->operands[0] = op_expr[0]->operands[0];
+
+         ir_rvalue *const tmp = ir->operands[1];
+         ir->operands[1] = ir->operands[2];
+         ir->operands[2] = tmp;
+      }
       break;
 
    default:
-- 
2.1.0



More information about the mesa-dev mailing list