[Mesa-dev] [PATCH 3/3] nir/opt_algebraic: add constant reassociation rules

Connor Abbott cwabbott0 at gmail.com
Sat Feb 7 17:05:46 PST 2015


These will optimize any tree of a commutative and associative operation
that contains more than one constant to have only one constant in the
end. For example, it will optimize

((4 * a) * b) * (5 * c)

into

20 * ((a * b) * c)

Note that when we port Matt's tree balancing pass, we'll have to think
carefully to make sure they don't fight.

Signed-off-by: Connor Abbott <cwabbott0 at gmail.com>
---
 src/glsl/nir/nir_opt_algebraic.py | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/glsl/nir/nir_opt_algebraic.py b/src/glsl/nir/nir_opt_algebraic.py
index a5fe19a..f4466b4 100644
--- a/src/glsl/nir/nir_opt_algebraic.py
+++ b/src/glsl/nir/nir_opt_algebraic.py
@@ -128,6 +128,16 @@ optimizations = [
    # next round of opt_algebraic, get picked up by one of the above two.
    (('bcsel', '#a', b, c), ('bcsel', ('ine', 'a', 0), b, c)),
 
+   # constant reassociation
+   # the idea is that we make sure each expression tree always ends with
+   # "constant thing plus tree of non-constant things'
+   (('!a at associative,commutative', '#a', ('!a', '#b', 'c')),
+    ('!a', ('!a', 'a', 'b'), 'c')),
+   (('!a at associative,commutative', ('!a', '#a', 'b'), ('!a', '#c', 'd')),
+    ('!a', ('!a', 'a', 'c'), ('!a', 'b', 'd'))),
+   (('!a at associative,commutative', ('!a', '#a', 'b'), 'c'),
+    ('!a', 'a', ('!a', 'b', 'c'))),
+
 # This one may not be exact
    (('feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
 ]
-- 
2.1.0



More information about the mesa-dev mailing list