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

Submitted by Connor Abbott on Feb. 8, 2015, 1:05 a.m.


Message ID 1423357546-30702-4-git-send-email-cwabbott0@gmail.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Connor Abbott Feb. 8, 2015, 1:05 a.m.
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)


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@gmail.com>
 src/glsl/nir/nir_opt_algebraic.py | 10 ++++++++++
 1 file changed, 10 insertions(+)

Patch hide | download patch | download mbox

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@associative,commutative', '#a', ('!a', '#b', 'c')),
+    ('!a', ('!a', 'a', 'b'), 'c')),
+   (('!a@associative,commutative', ('!a', '#a', 'b'), ('!a', '#c', 'd')),
+    ('!a', ('!a', 'a', 'c'), ('!a', 'b', 'd'))),
+   (('!a@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))),