Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InlineMember: rename duplicate locals in the body #172

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion loki/transform/transform_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ def _map_unbound_dims(var, val):
{v: v.clone(name=f'{member.name}_{v.name}') for v in duplicate_locals}
)
member.spec = shadow_mapper.visit(member.spec)
member.body = shadow_mapper.visit(member.body)

var_map = {}
for v in FindVariables(unique=False).visit(member.body):
if v.name in [dl.name for dl in duplicate_locals]:
var_map[v] = v.clone(name=f'{member.name}_{v.name}')
rolfhm marked this conversation as resolved.
Show resolved Hide resolved
member.body = SubstituteExpressions(var_map).visit(member.body)

# Get local variable declarations and hoist them
decls = FindNodes(VariableDeclaration).visit(member.spec)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_transform_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,11 @@ def test_inline_member_routines_variable_shadowing(frontend):
fcode = """
subroutine outer()
real :: x = 3 ! 'x' is real in outer.
real :: tmp = 0
real :: y

y = 1.0
call inner(tmp, y=y)
x = x + tmp
call inner(y)
x = x + y

contains
subroutine inner(y)
Expand Down Expand Up @@ -515,7 +514,7 @@ def test_inline_member_routines_variable_shadowing(frontend):
# Check inner 'y' was substituted, not renamed!
assign = FindNodes(Assignment).visit(routine.body)
assert routine.variable_map['y'] == 'y'
assert assign[2].lhs == 'y' and assign[2].rhs == 'y + sum(x)'
assert assign[2].lhs == 'y' and assign[2].rhs == 'y + sum(inner_x)'


@pytest.mark.parametrize('frontend', available_frontends())
Expand Down