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

The compiler was crashing when the collapse loop nest could not be
retrieved because of extra acc loop directive inside it.
This commit is contained in:
Valentin Clement (バレンタイン クレメン) 2023-11-22 13:01:12 -08:00 committed by GitHub
parent 53f912480f
commit 575c9bf940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -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;
}
@ -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);
}

View File

@ -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