Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions devito/ir/iet/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'Lambda',
'List',
'ListMajor',
'MacroStatement',
'MetaCall',
'MultiTraversable',
'Node',
Expand Down Expand Up @@ -1331,6 +1332,24 @@ def periodic(self):
return self._periodic


class MacroStatement(ExprStmt, Node):

"""
A standalone macro invocation, such as ``MY_MACRO;``.

Parameters
----------
name : str
The macro name, without a trailing semicolon.
"""

def __init__(self, name):
self.name = name

def __repr__(self):
return f"<MacroStatement({self.name})>"


class Using(Node):

"""
Expand Down
3 changes: 3 additions & 0 deletions devito/ir/iet/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ def visit_Section(self, o):
def visit_Break(self, o):
return c.Statement('break')

def visit_MacroStatement(self, o):
return c.Statement(o.name)

def visit_Return(self, o):
v = 'return'
if o.value is not None:
Expand Down
33 changes: 31 additions & 2 deletions tests/test_iet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from devito.ir.iet import (
Call, Callable, CGen, Conditional, Definition, Dereference, DeviceCall, DummyExpr,
ElementalFunction, FindNodes, FindSymbols, Iteration, KernelLaunch, Lambda, List,
Switch, Transformer, filter_iterations, make_callable, make_efunc,
retrieve_iteration_tree
MacroStatement, MapExprStmts, Switch, Transformer, filter_iterations, make_callable,
make_efunc, retrieve_iteration_tree
)
from devito.ir.iet.visitors import sorted_efuncs
from devito.passes.iet.engine import Graph, reuse_efuncs
Expand Down Expand Up @@ -120,6 +120,35 @@ def test_nested_calls_cgen():
assert str(code) == 'foo(bar());'


def test_macro_statement_codegen():
macro = MacroStatement('MY_MACRO')

assert str(CGen().visit(macro)) == 'MY_MACRO;'
assert macro.view_cir == 'MY_MACRO;'

func = Callable('foo', [macro, Call('bar')], 'void', (), prefix=())
assert str(func) == """\
void foo()
{
MY_MACRO;
bar();
}"""


def test_macro_statement_visitors():
macro = MacroStatement('MY_MACRO')
body = List(body=[macro, Call('bar')])

assert FindNodes(MacroStatement).visit(body) == [macro]
assert macro in MapExprStmts().visit(body)
assert FindSymbols('basics').visit(body) == []

replacement = macro._rebuild(name='OTHER_MACRO')
rebuilt = Transformer({macro: replacement}).visit(body)
assert str(rebuilt) == 'OTHER_MACRO;\nbar();'
assert str(body) == 'MY_MACRO;\nbar();'


@pytest.mark.parametrize('mode,expected', [
('basics', '["x"]'),
('symbolics', '["f"]')
Expand Down