Skip to content

Commit 575c9bf

Browse files
authored
[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.
1 parent 53f9124 commit 575c9bf

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,13 +1183,23 @@ void AccAttributeVisitor::CheckAssociatedLoopIndex(
11831183
}
11841184

11851185
const auto getNextDoConstruct =
1186-
[this](const parser::Block &block) -> const parser::DoConstruct * {
1186+
[this](const parser::Block &block,
1187+
std::int64_t &level) -> const parser::DoConstruct * {
11871188
for (const auto &entry : block) {
11881189
if (const auto *doConstruct = GetDoConstructIf(entry)) {
11891190
return doConstruct;
11901191
} else if (parser::Unwrap<parser::CompilerDirective>(entry)) {
11911192
// It is allowed to have a compiler directive associated with the loop.
11921193
continue;
1194+
} else if (const auto &accLoop{
1195+
parser::Unwrap<parser::OpenACCLoopConstruct>(entry)}) {
1196+
if (level == 0)
1197+
break;
1198+
const auto &beginDir{
1199+
std::get<parser::AccBeginLoopDirective>(accLoop->t)};
1200+
context_.Say(beginDir.source,
1201+
"LOOP directive not expected in COLLAPSE loop nest"_err_en_US);
1202+
level = 0;
11931203
} else {
11941204
break;
11951205
}
@@ -1198,11 +1208,12 @@ void AccAttributeVisitor::CheckAssociatedLoopIndex(
11981208
};
11991209

12001210
const auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t)};
1201-
for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
1211+
for (const parser::DoConstruct *loop{&*outer}; loop && level > 0;) {
12021212
// Go through all nested loops to ensure index variable exists.
12031213
GetLoopIndex(*loop);
12041214
const auto &block{std::get<parser::Block>(loop->t)};
1205-
loop = getNextDoConstruct(block);
1215+
--level;
1216+
loop = getNextDoConstruct(block, level);
12061217
}
12071218
CHECK(level == 0);
12081219
}

flang/test/Semantics/OpenACC/acc-loop.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,12 @@ program openacc_loop_validity
268268
end do
269269
!$acc end loop
270270

271+
!$acc loop collapse(2)
272+
do i = 1, 10
273+
!ERROR: LOOP directive not expected in COLLAPSE loop nest
274+
!$acc loop
275+
do j = 1, 10
276+
end do
277+
end do
278+
271279
end program openacc_loop_validity

0 commit comments

Comments
 (0)