Skip to content

Commit

Permalink
SingleColumn: Fix corner case of empty !$acc data clauses
Browse files Browse the repository at this point in the history
Adds a small test and does not print data clauses if no arrays
are passed to the routine.
  • Loading branch information
mlange05 committed Sep 9, 2024
1 parent 49af9c1 commit fc17ad2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
7 changes: 4 additions & 3 deletions loki/transformations/single_column/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ def annotate_kernel_routine(self, routine):
args += [a for a in routine.arguments if isinstance(a.type.dtype, DerivedType)]
argnames = [str(a.name) for a in args]

routine.body.prepend(ir.Pragma(keyword='acc', content=f'data present({", ".join(argnames)})'))
# Add comment to prevent false-attachment in case it is preceded by an "END DO" statement
routine.body.append((ir.Comment(text=''), ir.Pragma(keyword='acc', content='end data')))
if argnames:
routine.body.prepend(ir.Pragma(keyword='acc', content=f'data present({", ".join(argnames)})'))
# Add comment to prevent false-attachment in case it is preceded by an "END DO" statement
routine.body.append((ir.Comment(text=''), ir.Pragma(keyword='acc', content='end data')))

def transform_subroutine(self, routine, **kwargs):
"""
Expand Down
37 changes: 37 additions & 0 deletions loki/transformations/single_column/tests/test_scc.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,43 @@ def test_scc_annotate_routine_seq_pragma(frontend, horizontal, blocking):
assert pragmas[0].content == 'routine seq'


@pytest.mark.parametrize('frontend', available_frontends())
def test_scc_annotate_empty_data_clause(frontend, horizontal, blocking):
"""
Test that we do not generate empty `!$acc data` clauses.
"""

fcode = """
subroutine some_kernel(n)
implicit none
! Scalars should not show up in `!$acc data` clause
integer, intent(inout) :: n
!$loki routine seq
integer :: k
k = n
do k=1, 3
n = k + 1.
enddo
end subroutine some_kernel
"""
routine = Subroutine.from_source(fcode, frontend=frontend)

pragmas = FindNodes(Pragma).visit(routine.ir)
assert len(pragmas) == 1
assert pragmas[0].keyword == 'loki'
assert pragmas[0].content == 'routine seq'

transformation = SCCAnnotateTransformation(directive='openacc', block_dim=blocking)
transformation.transform_subroutine(routine, role='kernel', targets=['some_kernel',])

# Ensure the routine pragma is in the first pragma in the spec
pragmas = FindNodes(Pragma).visit(routine.ir)
assert len(pragmas) == 1
assert pragmas[0].keyword == 'acc'
assert pragmas[0].content == 'routine seq'


@pytest.mark.parametrize('frontend', available_frontends())
def test_scc_vector_reduction(frontend, horizontal, blocking):
"""
Expand Down

0 comments on commit fc17ad2

Please sign in to comment.