Skip to content

Commit a7f9373

Browse files
committed
compiler: Avoid generating parentheses when not needed
1 parent d851d66 commit a7f9373

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

devito/ir/cgen/printer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from devito import configuration
1717
from devito.arch.compiler import AOMPCompiler
1818
from devito.symbolics.inspection import has_integer_args, sympy_dtype
19+
from devito.symbolics.queries import q_leaf
1920
from devito.types.basic import AbstractFunction
2021
from devito.tools import ctypes_to_cstr, dtype_to_ctype, ctypes_vector_mapper
2122

@@ -364,7 +365,7 @@ def _print_InlineIf(self, expr):
364365
def _print_UnaryOp(self, expr, op=None, parenthesize=False):
365366
op = op or expr._op
366367
base = self._print(expr.base)
367-
if not expr.base.is_Symbol or parenthesize:
368+
if not q_leaf(expr.function) or parenthesize:
368369
base = f'({base})'
369370
return f'{op}{base}'
370371

devito/symbolics/extended_sympy.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ def function(self):
135135
"""
136136
The underlying function.
137137
"""
138-
return self.base.function
138+
try:
139+
return self.base.function
140+
except AttributeError:
141+
# Fallback, mostly for legacy tests, when wrapping SymPy types
142+
return self.base
139143

140144
@property
141145
def dtype(self):

devito/symbolics/queries.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ def q_comp_acc(expr):
3737

3838

3939
def q_leaf(expr):
40-
return (expr.is_Atom or
41-
expr.is_Indexed or
42-
isinstance(expr, extra_leaves))
40+
try:
41+
return (expr.is_Atom or
42+
expr.is_Indexed or
43+
isinstance(expr, extra_leaves))
44+
except AttributeError:
45+
return False
4346

4447

4548
def q_indexed(expr):

tests/test_iet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def _C_free(self):
288288
static void foo()
289289
{
290290
cudaStream_t stream;
291-
cudaStreamCreate(&(stream));
291+
cudaStreamCreate(&stream);
292292
293293
foo(stream);
294294

0 commit comments

Comments
 (0)