[Mesa-dev,v2,3/9] glsl: Make compare_ir sort expression operands for commutative operations

Submitted by Iago Toral Quiroga on Oct. 1, 2014, 6:35 a.m.

Details

Message ID 1412145345-3974-4-git-send-email-itoral@igalia.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Iago Toral Quiroga Oct. 1, 2014, 6:35 a.m.
From: Petri Latvala <petri.latvala@intel.com>

Sort expression operands when possible so that building expected IR
sexps doesn't need to know which ordering will be produced by an
optimization pass.

Signed-off-by: Petri Latvala <petri.latvala@intel.com>

Version 2 by Iago Toral Quiroga <itoral@igalia.com>:

In python style constants go at the top of the file, and should be
all caps, as suggested by Dylan Baker.
---
 src/glsl/tests/compare_ir |  4 ++--
 src/glsl/tests/sexps.py   | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/glsl/tests/compare_ir b/src/glsl/tests/compare_ir
index a40fc81..0b63fab 100755
--- a/src/glsl/tests/compare_ir
+++ b/src/glsl/tests/compare_ir
@@ -38,9 +38,9 @@  if len(sys.argv) != 3:
     exit(1)
 
 with open(sys.argv[1]) as f:
-    ir1 = sort_decls(parse_sexp(f.read()))
+    ir1 = sort_decls(sort_commutatives(parse_sexp(f.read())))
 with open(sys.argv[2]) as f:
-    ir2 = sort_decls(parse_sexp(f.read()))
+    ir2 = sort_decls(sort_commutatives(parse_sexp(f.read())))
 
 if ir1 == ir2:
     exit(0)
diff --git a/src/glsl/tests/sexps.py b/src/glsl/tests/sexps.py
index a714af8..49bed88 100644
--- a/src/glsl/tests/sexps.py
+++ b/src/glsl/tests/sexps.py
@@ -29,6 +29,26 @@ 
 
 import re
 
+COMMUTATIVES = [
+    '+',
+    '*',
+    'imul_high',
+    'carry',
+    '==',
+    '!=',
+    'all_equal',
+    'any_nequal',
+    '&',
+    '^',
+    '|',
+    '&&',
+    '^^',
+    '||',
+    'dot',
+    'min',
+    'max'
+    ]
+
 def check_sexp(sexp):
     """Verify that the argument is a proper sexp.
 
@@ -101,3 +121,20 @@  def sort_decls(sexp):
             other_code.append(s)
     return sorted(decls) + other_code
 
+def sort_commutatives(sexp):
+    """Sort operands of expressions that are commutative in sexp.
+
+    This is used to work around the fact that optimization passes might
+    reorder operands.
+    """
+    if not isinstance(sexp, list): return sexp
+
+    code = []
+    for s in sexp:
+        sd = sort_commutatives(s)
+        # An expression is [expression, type, operation, operand1, ...]
+        if isinstance(sd, list) and len(sd) >= 3 and sd[0] == 'expression' and sd[2] in COMMUTATIVES:
+            code.append(['expression', sd[1:2]] + sorted(sd[3:]))
+        else:
+            code.append(sd)
+    return code