Skip to content

Commit 0e954b3

Browse files
committed
Add an infinite loop assertion
1 parent 6e05f92 commit 0e954b3

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,8 @@ struct LoopInterchange {
653653
if (!AddLoopIfEnabled(LoopList[I]))
654654
return Changed;
655655

656-
// Set an upper bound of the number of transformations to avoid infinite
657-
// loop. There is no deep meaning behind the current value (square of the
658-
// size of LoopList).
659-
// TODO: Is this really necessary?
660-
const unsigned MaxAttemptsCount = LoopList.size() * LoopList.size();
661-
unsigned Attempts = 0;
656+
// The number of attempts of exchanges. Used for debug build.
657+
[[maybe_unused]] unsigned Attempts = 0;
662658

663659
// Process the loops. An exchange is applied to two loops, but a metadata
664660
// replacement can be applied to three loops: the two loops plus the next
@@ -676,7 +672,7 @@ struct LoopInterchange {
676672
// In this case we will exchange the innermost two loops at first, the
677673
// follow-up metadata including enabling interchange is attached on the
678674
// outermost loop, and it is enqueued as the next candidate to be processed.
679-
while (!Worklist.empty() && Attempts < MaxAttemptsCount) {
675+
while (!Worklist.empty()) {
680676
Loop *TargetLoop = Worklist.pop_back_val();
681677
assert(findMetadata(TargetLoop) == true &&
682678
"Some metadata was unexpectedlly removed");
@@ -710,7 +706,6 @@ struct LoopInterchange {
710706
NextOuterLoop = LoopList[OuterLoopId - 1];
711707
Loop *OuterLoop = LoopList[OuterLoopId];
712708
Loop *InnerLoop = LoopList[InnerLoopId];
713-
Attempts++;
714709
Changed = true;
715710
Loop2Index[OuterLoop] = OuterLoopId;
716711
Loop2Index[InnerLoop] = InnerLoopId;
@@ -746,13 +741,21 @@ struct LoopInterchange {
746741
Valid &= AddLoopIfEnabled(InnerLoop);
747742
if (!Valid)
748743
break;
744+
745+
// Check that the number of attempts of interchanges hasn't exceeded the
746+
// upper limit. It would lead an infinite loops.
747+
LLVM_DEBUG({
748+
// There is no deep meaning behind the current value (square of the size
749+
// of LoopList).
750+
unsigned MaxAttemptsCount = LoopList.size() * LoopList.size();
751+
Attempts++;
752+
assert(Attempts <= MaxAttemptsCount &&
753+
"The number of attempts of interchanges exceeded the limit. An "
754+
"infinite loop may have occured because the metadata was not "
755+
"properly deleted after each exchange.");
756+
});
749757
}
750758

751-
LLVM_DEBUG({
752-
if (!Worklist.empty())
753-
dbgs() << "Some metadata was ignored because the maximum number of "
754-
"attempts was reached.\n";
755-
});
756759
return Changed;
757760
}
758761
};

0 commit comments

Comments
 (0)