Skip to content

Commit c074df1

Browse files
committed
compiler: Add a check to pre-empt expensive symbolic comparisons before try-except
1 parent 35788f4 commit c074df1

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

devito/ir/support/basic.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,21 @@ def distance(self, other):
365365
# Case 1: `sit` is an IterationInterval with statically known
366366
# trip count. E.g. it ranges from 0 to 3; `other` performs a
367367
# constant access at 4
368-
for v in (self[n], other[n]):
369-
try:
370-
# NOTE: Split the boolean to make the conditional short circuit
371-
# more frequently for mild speedup
372-
if bool(v < sit.symbolic_min) or bool(v > sit.symbolic_max):
373-
return Vector(S.ImaginaryUnit)
374-
except TypeError:
375-
pass
368+
369+
# NOTE: To avoid evaluating expensive symbolic Lt or Gt operations,
370+
# we pre-empt such operations by checking if the values to be compared
371+
# to are symbolic, and skip this case if not.
372+
if not any(isinstance(i, sympy.core.Basic)
373+
for i in (sit.symbolic_min, sit.symbolic_max)):
374+
375+
for v in (self[n], other[n]):
376+
try:
377+
# NOTE: Split the boolean to make the conditional short circuit
378+
# more frequently for mild speedup
379+
if bool(v < sit.symbolic_min) or bool(v > sit.symbolic_max):
380+
return Vector(S.ImaginaryUnit)
381+
except TypeError:
382+
pass
376383

377384
# Case 2: `sit` is an IterationInterval over a local SubDimension
378385
# and `other` performs a constant access

0 commit comments

Comments
 (0)