@@ -653,12 +653,8 @@ struct LoopInterchange {
653
653
if (!AddLoopIfEnabled (LoopList[I]))
654
654
return Changed;
655
655
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 ;
662
658
663
659
// Process the loops. An exchange is applied to two loops, but a metadata
664
660
// replacement can be applied to three loops: the two loops plus the next
@@ -676,7 +672,7 @@ struct LoopInterchange {
676
672
// In this case we will exchange the innermost two loops at first, the
677
673
// follow-up metadata including enabling interchange is attached on the
678
674
// outermost loop, and it is enqueued as the next candidate to be processed.
679
- while (!Worklist.empty () && Attempts < MaxAttemptsCount ) {
675
+ while (!Worklist.empty ()) {
680
676
Loop *TargetLoop = Worklist.pop_back_val ();
681
677
assert (findMetadata (TargetLoop) == true &&
682
678
" Some metadata was unexpectedlly removed" );
@@ -710,7 +706,6 @@ struct LoopInterchange {
710
706
NextOuterLoop = LoopList[OuterLoopId - 1 ];
711
707
Loop *OuterLoop = LoopList[OuterLoopId];
712
708
Loop *InnerLoop = LoopList[InnerLoopId];
713
- Attempts++;
714
709
Changed = true ;
715
710
Loop2Index[OuterLoop] = OuterLoopId;
716
711
Loop2Index[InnerLoop] = InnerLoopId;
@@ -746,13 +741,21 @@ struct LoopInterchange {
746
741
Valid &= AddLoopIfEnabled (InnerLoop);
747
742
if (!Valid)
748
743
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
+ });
749
757
}
750
758
751
- LLVM_DEBUG ({
752
- if (!Worklist.empty ())
753
- dbgs () << " Some metadata was ignored because the maximum number of "
754
- " attempts was reached.\n " ;
755
- });
756
759
return Changed;
757
760
}
758
761
};
0 commit comments