@@ -873,6 +873,9 @@ void DependenceInfo::collectCommonLoops(const SCEV *Expression,
873873 SmallBitVector &Loops) const {
874874 while (LoopNest) {
875875 unsigned Level = LoopNest->getLoopDepth ();
876+ LLVM_DEBUG (dbgs () << " MaxLevels = " << MaxLevels << " \n " );
877+ LLVM_DEBUG (dbgs () << " Level = " << Level << " \n " );
878+ assert (Level <= MaxLevels && " Level larger than MaxLevels." );
876879 if (Level <= CommonLevels && !SE->isLoopInvariant (Expression, LoopNest))
877880 Loops.set (Level);
878881 LoopNest = LoopNest->getParentLoop ();
@@ -959,6 +962,10 @@ bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
959962 if (!AddRec)
960963 return isLoopInvariant (Expr, LoopNest);
961964
965+ const SCEV *Step = AddRec->getStepRecurrence (*SE);
966+ if (!isLoopInvariant (Step, LoopNest))
967+ return false ;
968+
962969 // The AddRec must depend on one of the containing loops. Otherwise,
963970 // mapSrcLoop and mapDstLoop return indices outside the intended range. This
964971 // can happen when a subscript in one loop references an IV from a sibling
@@ -970,14 +977,16 @@ bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
970977 if (!L)
971978 return false ;
972979
980+ unsigned Level = IsSrc ? mapSrcLoop (L) : mapDstLoop (L);
981+ // Check that the mapped loop index is within bounds for the SmallBitVector.
982+ // This can happen when loop depths exceed MaxLevels due to the mapping
983+ // algorithm.
984+
985+ LLVM_DEBUG (dbgs () << " MaxLevels = " << MaxLevels << " \n " );
986+ LLVM_DEBUG (dbgs () << " Level = " << Level << " \n " );
987+ assert (Level <= MaxLevels && " Level larger than MaxLevels." );
988+ Loops.set (Level);
973989 const SCEV *Start = AddRec->getStart ();
974- const SCEV *Step = AddRec->getStepRecurrence (*SE);
975- if (!isLoopInvariant (Step, LoopNest))
976- return false ;
977- if (IsSrc)
978- Loops.set (mapSrcLoop (AddRec->getLoop ()));
979- else
980- Loops.set (mapDstLoop (AddRec->getLoop ()));
981990 return checkSubscript (Start, LoopNest, Loops, IsSrc);
982991}
983992
@@ -2281,8 +2290,14 @@ bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
22812290 Result, NewConstraint) ||
22822291 gcdMIVtest (Src, Dst, Result);
22832292 }
2284- llvm_unreachable (" SIV test expected at least one AddRec" );
2285- return false ;
2293+ // If neither expression is an AddRec, this means propagation has simplified
2294+ // them to non-AddRec forms. In this case, fall back to ZIV analysis since
2295+ // the expressions are effectively loop-invariant.
2296+ LLVM_DEBUG (dbgs () << " falling back to ZIV test due to no AddRec\n " );
2297+ // Set to first valid level to avoid Level=0 causing DV[-1] access.
2298+ // See comment in establishNestingLevels.
2299+ Level = 1 ;
2300+ return testZIV (Src, Dst, Result);
22862301}
22872302
22882303// testRDIV -
@@ -2343,8 +2358,14 @@ bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
23432358 SrcLoop = DstAddRec->getLoop ();
23442359 } else
23452360 llvm_unreachable (" RDIV reached by surprising SCEVs" );
2346- } else
2347- llvm_unreachable (" RDIV expected at least one AddRec" );
2361+ } else {
2362+ // If neither expression is an AddRec, this means propagation has simplified
2363+ // them to non-AddRec forms. Fall back to ZIV analysis since the expressions
2364+ // are effectively loop-invariant.
2365+ LLVM_DEBUG (
2366+ dbgs () << " RDIV falling back to ZIV test due to no AddRec\n " );
2367+ return testZIV (Src, Dst, Result);
2368+ }
23482369 return exactRDIVtest (SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop, DstLoop,
23492370 Result) ||
23502371 gcdMIVtest (Src, Dst, Result) ||
@@ -3821,7 +3842,7 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
38213842 break ;
38223843 case Subscript::SIV: {
38233844 LLVM_DEBUG (dbgs () << " , SIV\n " );
3824- unsigned Level;
3845+ unsigned Level = 0 ;
38253846 const SCEV *SplitIter = nullptr ;
38263847 if (testSIV (Pair[SI].Src , Pair[SI].Dst , Level, Result, NewConstraint,
38273848 SplitIter))
@@ -3872,12 +3893,17 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
38723893 for (unsigned SJ : Sivs.set_bits ()) {
38733894 LLVM_DEBUG (dbgs () << " testing subscript " << SJ << " , SIV\n " );
38743895 // SJ is an SIV subscript that's part of the current coupled group
3875- unsigned Level;
3896+ unsigned Level = 0 ;
38763897 const SCEV *SplitIter = nullptr ;
38773898 LLVM_DEBUG (dbgs () << " SIV\n " );
38783899 if (testSIV (Pair[SJ].Src , Pair[SJ].Dst , Level, Result, NewConstraint,
38793900 SplitIter))
38803901 return nullptr ;
3902+
3903+ LLVM_DEBUG (dbgs () << " MaxLevels = " << MaxLevels << " \n " );
3904+ LLVM_DEBUG (dbgs () << " Level = " << Level << " \n " );
3905+ assert (Level <= MaxLevels && " Level larger than MaxLevels." );
3906+
38813907 ConstrainedLevels.set (Level);
38823908 if (intersectConstraints (&Constraints[Level], &NewConstraint)) {
38833909 if (Constraints[Level].isEmpty ()) {
@@ -4155,7 +4181,7 @@ const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
41554181 for (unsigned SI : Separable.set_bits ()) {
41564182 switch (Pair[SI].Classification ) {
41574183 case Subscript::SIV: {
4158- unsigned Level;
4184+ unsigned Level = 0 ;
41594185 const SCEV *SplitIter = nullptr ;
41604186 (void )testSIV (Pair[SI].Src , Pair[SI].Dst , Level, Result, NewConstraint,
41614187 SplitIter);
@@ -4195,12 +4221,17 @@ const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
41954221 bool Changed = false ;
41964222 for (unsigned SJ : Sivs.set_bits ()) {
41974223 // SJ is an SIV subscript that's part of the current coupled group
4198- unsigned Level;
4224+ unsigned Level = 0 ;
41994225 const SCEV *SplitIter = nullptr ;
42004226 (void )testSIV (Pair[SJ].Src , Pair[SJ].Dst , Level, Result, NewConstraint,
42014227 SplitIter);
42024228 if (Level == SplitLevel && SplitIter)
42034229 return SplitIter;
4230+
4231+ LLVM_DEBUG (dbgs () << " MaxLevels = " << MaxLevels << " \n " );
4232+ LLVM_DEBUG (dbgs () << " Level = " << Level << " \n " );
4233+ assert (Level <= MaxLevels && " Level larger than MaxLevels." );
4234+
42044235 ConstrainedLevels.set (Level);
42054236 if (intersectConstraints (&Constraints[Level], &NewConstraint))
42064237 Changed = true ;
0 commit comments