Skip to content

Commit

Permalink
Transform: Correctly (re-)attach scopes and use after inlining
Browse files Browse the repository at this point in the history
This is needed to correctly infer parentage among scoped IR nodes
is they have been moved (eg. via inlining associates).
  • Loading branch information
mlange05 committed Apr 10, 2024
1 parent 29a4202 commit da93e18
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions loki/expression/expr_visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ def visit_Scope(self, o, **kwargs):
# entry in the scope's table
self._update_symbol_table_with_decls_and_imports(o)

# Attach parent scope if it is new before passing self down to children
parent_scope = kwargs.get('scope', o.parent)
if o.parent is not parent_scope and o is not parent_scope:
o._reset_parent(parent=parent_scope)

# Then recurse to all children
kwargs['scope'] = o
children = tuple(self.visit(i, **kwargs) for i in o.children)
Expand Down
2 changes: 1 addition & 1 deletion loki/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def rescope_symbols(self):
to a scope in the scope hierarchy
"""
from loki.expression import AttachScopes # pylint: disable=import-outside-toplevel,cyclic-import
AttachScopes().visit(self)
AttachScopes().visit(self, scope=self)

def make_complete(self, **frontend_args):
"""
Expand Down
4 changes: 4 additions & 0 deletions loki/transform/transform_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ def inline_subroutine_calls(routine, calls, callee, allowed_aliases=None):
# Replace calls to child procedure with the child's body
routine.body = Transformer(call_map).visit(routine.body)

# We need this to ensure that symbols, as well as nested scopes
# are correctly attached to each other (eg. nested associates).
routine.rescope_symbols()


def inline_internal_procedures(routine, allowed_aliases=None):
"""
Expand Down

0 comments on commit da93e18

Please sign in to comment.