Skip to content

[flang][openacc] Avoid crash when collapse loop nest has extra directive #73166

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

Merged
merged 1 commit into from
Nov 22, 2023
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
17 changes: 14 additions & 3 deletions flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,13 +1183,23 @@ void AccAttributeVisitor::CheckAssociatedLoopIndex(
}

const auto getNextDoConstruct =
[this](const parser::Block &block) -> const parser::DoConstruct * {
[this](const parser::Block &block,
std::int64_t &level) -> const parser::DoConstruct * {
for (const auto &entry : block) {
if (const auto *doConstruct = GetDoConstructIf(entry)) {
return doConstruct;
} else if (parser::Unwrap<parser::CompilerDirective>(entry)) {
// It is allowed to have a compiler directive associated with the loop.
continue;
} else if (const auto &accLoop{
parser::Unwrap<parser::OpenACCLoopConstruct>(entry)}) {
if (level == 0)
break;
const auto &beginDir{
std::get<parser::AccBeginLoopDirective>(accLoop->t)};
context_.Say(beginDir.source,
"LOOP directive not expected in COLLAPSE loop nest"_err_en_US);
level = 0;
} else {
break;
}
Expand All @@ -1198,11 +1208,12 @@ void AccAttributeVisitor::CheckAssociatedLoopIndex(
};

const auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t)};
for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
for (const parser::DoConstruct *loop{&*outer}; loop && level > 0;) {
// Go through all nested loops to ensure index variable exists.
GetLoopIndex(*loop);
const auto &block{std::get<parser::Block>(loop->t)};
loop = getNextDoConstruct(block);
--level;
loop = getNextDoConstruct(block, level);
}
CHECK(level == 0);
}
Expand Down
8 changes: 8 additions & 0 deletions flang/test/Semantics/OpenACC/acc-loop.f90
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,12 @@ program openacc_loop_validity
end do
!$acc end loop

!$acc loop collapse(2)
do i = 1, 10
!ERROR: LOOP directive not expected in COLLAPSE loop nest
!$acc loop
do j = 1, 10
end do
end do

end program openacc_loop_validity