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

Iago Toral Quiroga itoral at igalia.com
Tue Sep 30 23:35:39 PDT 2014


From: Petri Latvala <petri.latvala at 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 at intel.com>

Version 2 by Iago Toral Quiroga <itoral at 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(-)

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
-- 
1.9.1



More information about the mesa-dev mailing list