@@ -3429,22 +3429,10 @@ SCEVSignedMonotonicityChecker::SCEVSignedMonotonicityChecker(
34293429 // into the index type, so the GEP value would be poison and any memory
34303430 // access using it would be immediate UB when executed.
34313431 //
3432- // TODO: We don't check if the result of the GEP is always used. Maybe we
3433- // should check the reachability from the GEP to the target instruction.
3434- // E.g., in the following case, no-wrap would not trigger immediate UB:
3435- //
3436- // entry:
3437- // ...
3438- // %gep = getelementptr inbounds i32, ptr %ptr, i32 %addrec
3439- // br i1 %cond, label %store, label %sink
3440- //
3441- // store:
3442- // store i32 42, ptr %ptr
3443- // br label %sink
3444- //
3445- // sink:
3446- // ...
3447- //
3432+ // TODO: The monotonicity check ensures that the given SCEV does not wrap
3433+ // in "any" iteration. Thus, inference from nusw should be valid only if
3434+ // the GEP is executed and its result is used in every iteration of the
3435+ // loop.
34483436 auto *GEP = dyn_cast<GetElementPtrInst>(Ptr);
34493437 if (GEP && GEP->hasNoUnsignedSignedWrap ())
34503438 NoWrapFromGEP = true ;
@@ -3807,16 +3795,25 @@ bool DependenceInfo::tryDelinearizeParametricSize(
38073795 const Loop *OutermostLoop =
38083796 LI->getLoopFor (Src->getParent ())->getOutermostLoop ();
38093797
3810- // TODO: In general, reasoning about monotonicity of a subscript from the
3811- // base pointer would lead incorrect result. Probably we need to check
3812- // the loops associated with this subscript are disjoint from those
3813- // associated with the other subscripts. The validation would be
3814- // something like:
3798+ // TODO: Inferring a subscript's monotonicity from the base pointer can
3799+ // lead to incorrect results. Consider the following code:
3800+ //
3801+ // %offset = ...
3802+ // %gep = getelementptr nusw i8, ptr %base, i64 %offset
38153803 //
3816- // LoopsI = collectCommonLoops(SrcSubscripts[I])
3817- // LoopsOthers = collectCommonLoops(SrcSCEV - SrcSubscripts[I])
3818- // CanUsePtr = (LoopsI intersect LoopsOthers) is empty.
3804+ // We might infer the monotonicity of %offset from nusw on the GEP (see
3805+ // the implementation of checkMonotonicity for details). This inference
3806+ // may be valid, but the same does not necessarily hold for each
3807+ // subscript. For example, assume %offset is {0,+,(%m * %n)}<%loop> where
3808+ // %m and %n are loop invariants. Delinearization can "decompose" this
3809+ // SCEV into something like:
38193810 //
3811+ // Size: [UnknownSize][%m]
3812+ // Subscripts: [{0,+,%n}][{0,+,1}]
3813+ //
3814+ // Here, if (%m * %n) wraps, %n can be larger than (%m * %n). Hence even
3815+ // if we know {0,+,(%m * %n)} doesn't wrap, we cannot conclude the same
3816+ // for {0,+,%n}.
38203817 MonotonicityType SrcMonotonicity =
38213818 SCEVSignedMonotonicityChecker::checkMonotonicity (
38223819 SE, SrcSubscripts[I], OutermostLoop, SrcPtr);
@@ -3852,6 +3849,22 @@ bool DependenceInfo::tryDelinearizeParametricSize(
38523849 return false ;
38533850 }
38543851
3852+ // TODO: Probably we need to prove that the "offset calculation" doesn't
3853+ // wrap. Here the offset calculation is:
3854+ //
3855+ // Offset =
3856+ // Subscripts[0] +
3857+ // Subscripts[1]*Sizes[0] +
3858+ // Subscripts[2]*Sizes[0]*Sizes[1] +
3859+ // ...
3860+ // Subscripts[N-1]*Sizes[0]*Sizes[1]*...*Sizes[N-2]
3861+ //
3862+ // where N is the number of dimensions. The subsequent dependence tests assume
3863+ // that different subscript values result in different offset values. If the
3864+ // above calculation wraps, this assumption is violated. Note that if every
3865+ // element of Subscripts is positive, the situation would be simple. However,
3866+ // the subscript for the outermost dimension (Subscripts[0]) can be negative.
3867+
38553868 return true ;
38563869}
38573870
0 commit comments