Skip to content

Commit 537f7ef

Browse files
committed
[LAA] hoist setting condition for rt-checks (NFC)
Strip ShouldRetyWithRuntimeCheck from the DepedenceDistanceStrideAndSizeInfo struct, and free isDependent from the responsibility of setting the condition for when runtime-checks are needed, transferring this responsibility to getDependenceDistanceStrideAndSize. The change is simple enough, but proving that this was the original intent of the code, making the change non-functional is non-trivial. The proof is as follows. The condition for runtime-checks is only respected when we return a Dependence::DepType of Dependence::Unknown, and the only possible place that could introduce a functional change with this patch is when Dist is possibly zero, but HasSameSize is false. Previously, we weren't checking that the Dist is not a SCEVConstant, and setting the runtime-check condition. Now, with the runtime-check condition hoisted, we do. We note that for a functional change to occur, the distance must be both non-constant, not provably non-positive and non-negative, and this is an impossibility.
1 parent 3b9f964 commit 537f7ef

File tree

2 files changed

+9
-37
lines changed

2 files changed

+9
-37
lines changed

llvm/include/llvm/Analysis/LoopAccessAnalysis.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,6 @@ class MemoryDepChecker {
372372
uint64_t MaxStride;
373373
std::optional<uint64_t> CommonStride;
374374

375-
bool ShouldRetryWithRuntimeCheck;
376-
377375
/// TypeByteSize is either the common store size of both accesses, or 0 when
378376
/// store sizes mismatch.
379377
uint64_t TypeByteSize;
@@ -383,11 +381,9 @@ class MemoryDepChecker {
383381

384382
DepDistanceStrideAndSizeInfo(const SCEV *Dist, uint64_t MaxStride,
385383
std::optional<uint64_t> CommonStride,
386-
bool ShouldRetryWithRuntimeCheck,
387384
uint64_t TypeByteSize, bool AIsWrite,
388385
bool BIsWrite)
389386
: Dist(Dist), MaxStride(MaxStride), CommonStride(CommonStride),
390-
ShouldRetryWithRuntimeCheck(ShouldRetryWithRuntimeCheck),
391387
TypeByteSize(TypeByteSize), AIsWrite(AIsWrite), BIsWrite(BIsWrite) {}
392388
};
393389

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,14 +2006,14 @@ MemoryDepChecker::getDependenceDistanceStrideAndSize(
20062006
if (StrideAScaled == StrideBScaled)
20072007
CommonStride = StrideAScaled;
20082008

2009-
// TODO: Historically, we don't retry with runtime checks unless the
2010-
// (unscaled) strides are the same. Fix this once the condition for runtime
2011-
// checks in isDependent is fixed.
2012-
bool ShouldRetryWithRuntimeCheck = StrideAPtrInt == StrideBPtrInt;
2009+
// TODO: FoundNonConstantDistanceDependence is used as a necessary condition
2010+
// to consider retrying with runtime checks. Historically, we did not set it
2011+
// when (unscaled) strides were different but there is no inherent reason to.
2012+
if (!isa<SCEVConstant>(Dist))
2013+
FoundNonConstantDistanceDependence |= StrideAPtrInt == StrideBPtrInt;
20132014

20142015
return DepDistanceStrideAndSizeInfo(Dist, MaxStride, CommonStride,
2015-
ShouldRetryWithRuntimeCheck, TypeByteSize,
2016-
AIsWrite, BIsWrite);
2016+
TypeByteSize, AIsWrite, BIsWrite);
20172017
}
20182018

20192019
MemoryDepChecker::Dependence::DepType
@@ -2028,15 +2028,11 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
20282028
if (std::holds_alternative<Dependence::DepType>(Res))
20292029
return std::get<Dependence::DepType>(Res);
20302030

2031-
auto &[Dist, MaxStride, CommonStride, ShouldRetryWithRuntimeCheck,
2032-
TypeByteSize, AIsWrite, BIsWrite] =
2031+
auto &[Dist, MaxStride, CommonStride, TypeByteSize, AIsWrite, BIsWrite] =
20332032
std::get<DepDistanceStrideAndSizeInfo>(Res);
20342033
bool HasSameSize = TypeByteSize > 0;
20352034

20362035
if (isa<SCEVCouldNotCompute>(Dist)) {
2037-
// TODO: Relax requirement that there is a common unscaled stride to retry
2038-
// with non-constant distance dependencies.
2039-
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
20402036
LLVM_DEBUG(dbgs() << "LAA: Dependence because of uncomputable distance.\n");
20412037
return Dependence::Unknown;
20422038
}
@@ -2096,14 +2092,8 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
20962092
// forward dependency will allow vectorization using any width.
20972093

20982094
if (IsTrueDataDependence && EnableForwardingConflictDetection) {
2099-
if (!ConstDist) {
2100-
// TODO: FoundNonConstantDistanceDependence is used as a necessary
2101-
// condition to consider retrying with runtime checks. Historically, we
2102-
// did not set it when strides were different but there is no inherent
2103-
// reason to.
2104-
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
2095+
if (!ConstDist)
21052096
return Dependence::Unknown;
2106-
}
21072097
if (!HasSameSize ||
21082098
couldPreventStoreLoadForward(
21092099
ConstDist->getAPInt().abs().getZExtValue(), TypeByteSize)) {
@@ -2119,22 +2109,8 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
21192109

21202110
int64_t MinDistance = SE.getSignedRangeMin(Dist).getSExtValue();
21212111
// Below we only handle strictly positive distances.
2122-
if (MinDistance <= 0) {
2123-
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
2112+
if (MinDistance <= 0)
21242113
return Dependence::Unknown;
2125-
}
2126-
2127-
if (!ConstDist) {
2128-
// Previously this case would be treated as Unknown, possibly setting
2129-
// FoundNonConstantDistanceDependence to force re-trying with runtime
2130-
// checks. Until the TODO below is addressed, set it here to preserve
2131-
// original behavior w.r.t. re-trying with runtime checks.
2132-
// TODO: FoundNonConstantDistanceDependence is used as a necessary
2133-
// condition to consider retrying with runtime checks. Historically, we
2134-
// did not set it when strides were different but there is no inherent
2135-
// reason to.
2136-
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
2137-
}
21382114

21392115
if (!HasSameSize) {
21402116
LLVM_DEBUG(dbgs() << "LAA: ReadWrite-Write positive dependency with "

0 commit comments

Comments
 (0)