Skip to content

Commit 3732737

Browse files
committed
compiler: make cse toposort stable
1 parent ed3585a commit 3732737

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

devito/passes/clusters/cse.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ def _toposort(exprs):
258258
"""
259259
Ensure the expression list is topologically sorted.
260260
"""
261+
if not any(isinstance(e.lhs, CTemp) for e in exprs):
262+
# Not CSE temps, no need to topologically sort
263+
return exprs
264+
261265
dag = DAG(exprs)
262266

263267
for e0 in exprs:
@@ -270,8 +274,12 @@ def _toposort(exprs):
270274

271275
def choose_element(queue, scheduled):
272276
# Try to honor temporary names as much as possible
273-
first = sorted(queue, key=lambda i: str(i.lhs)).pop(0)
274-
queue.remove(first)
277+
tmps = [i for i in queue if isinstance(i.lhs, CTemp)]
278+
if tmps:
279+
first = sorted(tmps, key=lambda i: str(i.lhs)).pop(0)
280+
queue.remove(first)
281+
else:
282+
first = queue.pop()
275283
return first
276284

277285
processed = dag.topological_sort(choose_element)

0 commit comments

Comments
 (0)