[Mesa-dev] [PATCH 16/23] glsl: Rewrite mix(constant_bool, constant_bool, condition) as condition or !condition

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


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

An expression like 'mix(false, true, condition)' is the same as just
'condition'.  Similarly, an expression like 'mix(true, false,
condition)' is the same as just '!condition'.

Expressions like 'mix(true, true, condition)' should already be handled
elsewhere.

Many, many more of the shaders hurt by "glsl: Optimize certain
if-statements to ir_triop_csel" are even or impoved now.  The helped
vs. hurt ratio is 2323:307 on Ivy Bride without NIR, and it is still
1045:1975.

Shader-db results:

GM45 (0x2A42):
total instructions in shared programs: 3545378 -> 3544299 (-0.03%)
instructions in affected programs:     115235 -> 114156 (-0.94%)
helped:                                393

Iron Lake (0x0046):
total instructions in shared programs: 4975572 -> 4974090 (-0.03%)
instructions in affected programs:     150594 -> 149112 (-0.98%)
helped:                                534

Sandy Bridge (0x0116):
total instructions in shared programs: 6800913 -> 6799711 (-0.02%)
instructions in affected programs:     196165 -> 194963 (-0.61%)
helped:                                808

Sandy Bridge (0x0116) NIR:
total instructions in shared programs: 6815782 -> 6792447 (-0.34%)
instructions in affected programs:     198743 -> 175408 (-11.74%)
helped:                                656
HURT:                                  152

Ivy Bridge (0x0166):
total instructions in shared programs: 6275305 -> 6274069 (-0.02%)
instructions in affected programs:     181703 -> 180467 (-0.68%)
helped:                                798

Ivy Bridge (0x0166) NIR:
total instructions in shared programs: 6322876 -> 6300986 (-0.35%)
instructions in affected programs:     185337 -> 163447 (-11.81%)
helped:                                650
HURT:                                  152
GAINED:                                2

Haswell (0x0426):
total instructions in shared programs: 5761392 -> 5760186 (-0.02%)
instructions in affected programs:     160962 -> 159756 (-0.75%)
helped:                                798

Haswell (0x0426) NIR:
total instructions in shared programs: 5798963 -> 5780281 (-0.32%)
instructions in affected programs:     163859 -> 145177 (-11.40%)
helped:                                650
HURT:                                  152
GAINED:                                2

Broadwell (0x162E):
total instructions in shared programs: 6809460 -> 6808264 (-0.02%)
instructions in affected programs:     162030 -> 160834 (-0.74%)
helped:                                802

Broadwell (0x162E) NIR:
total instructions in shared programs: 7013761 -> 6994887 (-0.27%)
instructions in affected programs:     163346 -> 144472 (-11.55%)
helped:                                650
HURT:                                  152
GAINED:                                4

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

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 7d58e9b..0f15f52 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -1121,6 +1121,20 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
 	 return ir->operands[1];
       if (is_vec_zero(op_const[0]))
 	 return ir->operands[2];
+
+      if (ir->type->base_type == GLSL_TYPE_BOOL) {
+         /* Optimize an expression like 'mix(false, true, condition)' to
+          * just 'condition'.
+          */
+         if (is_vec_zero(op_const[1]) && is_vec_one(op_const[2]))
+            return ir->operands[0];
+
+         /* Optimize an expression like 'mix(true, false, condition)' to
+          * just '!condition'.
+          */
+         if (is_vec_one(op_const[1]) && is_vec_zero(op_const[2]))
+            return logic_not(ir->operands[0]);
+      }
       break;
 
    default:
-- 
2.1.0



More information about the mesa-dev mailing list