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

Fix edge case for vector section mapping #382

Merged
merged 3 commits into from
Oct 7, 2024
Merged
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
51 changes: 50 additions & 1 deletion loki/transformations/single_column/tests/test_scc_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from loki import Subroutine, Sourcefile, Dimension, fgen
from loki.frontend import available_frontends
from loki.frontend import available_frontends, OMNI
from loki.ir import (
nodes as ir, FindNodes, pragmas_attached, is_loki_pragma
)
Expand Down Expand Up @@ -562,3 +562,52 @@ def test_scc_vector_section_trim_complex(
else:
assert assign in loop.body
assert(len(FindNodes(ir.Assignment).visit(loop.body)) == 4)

@pytest.mark.parametrize('frontend', available_frontends(
skip={OMNI: 'OMNI automatically expands ELSEIF into a nested ELSE=>IF.'}
))
@pytest.mark.parametrize('trim_vector_sections', [False, True])
def test_scc_devector_section_special_case(frontend, horizontal, vertical, blocking, trim_vector_sections):
"""
Test to highlight the limitations of vector-section trimming.
"""

fcode_kernel = """
subroutine some_kernel(start, end, nlon, flag0, flag1)
implicit none

integer, intent(in) :: nlon, start, end
logical, intent(in) :: flag0, flag1
real, dimension(nlon) :: work

integer :: jl

if(flag0)then
call some_other_kernel()
elseif(flag1)then
do jl=start,end
work(jl) = 1.
enddo
endif

end subroutine some_kernel
"""

routine = Subroutine.from_source(fcode_kernel, frontend=frontend)

# check whether pipeline can be applied and works as expected
scc_pipeline = SCCVectorPipeline(
horizontal=horizontal, vertical=vertical, block_dim=blocking,
directive='openacc', trim_vector_sections=trim_vector_sections
)
scc_pipeline.apply(routine, role='kernel', targets=['some_kernel',])

with pragmas_attached(routine, node_type=ir.Loop):
conditional = FindNodes(ir.Conditional).visit(routine.body)[0]
assert isinstance(conditional.body[0], ir.CallStatement)
assert len(conditional.body) == 1
assert isinstance(conditional.else_body[0], ir.Conditional)
assert len(conditional.else_body) == 1
assert isinstance(conditional.else_body[0].body[0], ir.Comment)
assert isinstance(conditional.else_body[0].body[1], ir.Loop)
assert conditional.else_body[0].body[1].pragma[0].content.lower() == 'loop vector'
8 changes: 7 additions & 1 deletion loki/transformations/single_column/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ def extract_vector_sections(cls, section, horizontal):
subsec_body = cls.extract_vector_sections(separator.body, horizontal)
if subsec_body:
subsections += subsec_body
subsec_else = cls.extract_vector_sections(separator.else_body, horizontal)
# we need to prevent that the whole 'else_body' is wrapped in a section,
# as 'Conditional's rely on the fact that the first element of the 'else_body'
# (if 'has_elseif') is a Conditional itself
if separator.has_elseif and separator.else_body:
subsec_else = cls.extract_vector_sections(separator.else_body[0].body, horizontal)
else:
subsec_else = cls.extract_vector_sections(separator.else_body, horizontal)
if subsec_else:
subsections += subsec_else

Expand Down
Loading