Skip to content

Commit 17912f3

Browse files
authored
LAA: refactor dependence class to prep for scaled strides (NFC) (#122113)
Rearrange the DepDistanceAndSizeInfo struct in preparation to scale strides. getDependenceDistanceStrideAndSize now returns the data of CommonStride, MaxStride, and clarifies when to retry with runtime checks, in place of (unscaled) strides.
1 parent 762f1b1 commit 17912f3

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

llvm/include/llvm/Analysis/LoopAccessAnalysis.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,26 @@ class MemoryDepChecker {
366366

367367
struct DepDistanceStrideAndSizeInfo {
368368
const SCEV *Dist;
369-
uint64_t StrideA;
370-
uint64_t StrideB;
369+
370+
/// Strides could either be scaled (in bytes, taking the size of the
371+
/// underlying type into account), or unscaled (in indexing units; unscaled
372+
/// stride = scaled stride / size of underlying type). Here, strides are
373+
/// unscaled.
374+
uint64_t MaxStride;
375+
std::optional<uint64_t> CommonStride;
376+
377+
bool ShouldRetryWithRuntimeCheck;
371378
uint64_t TypeByteSize;
372379
bool AIsWrite;
373380
bool BIsWrite;
374381

375-
DepDistanceStrideAndSizeInfo(const SCEV *Dist, uint64_t StrideA,
376-
uint64_t StrideB, uint64_t TypeByteSize,
377-
bool AIsWrite, bool BIsWrite)
378-
: Dist(Dist), StrideA(StrideA), StrideB(StrideB),
382+
DepDistanceStrideAndSizeInfo(const SCEV *Dist, uint64_t MaxStride,
383+
std::optional<uint64_t> CommonStride,
384+
bool ShouldRetryWithRuntimeCheck,
385+
uint64_t TypeByteSize, bool AIsWrite,
386+
bool BIsWrite)
387+
: Dist(Dist), MaxStride(MaxStride), CommonStride(CommonStride),
388+
ShouldRetryWithRuntimeCheck(ShouldRetryWithRuntimeCheck),
379389
TypeByteSize(TypeByteSize), AIsWrite(AIsWrite), BIsWrite(BIsWrite) {}
380390
};
381391

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,8 +1994,23 @@ MemoryDepChecker::getDependenceDistanceStrideAndSize(
19941994
DL.getTypeStoreSizeInBits(ATy) == DL.getTypeStoreSizeInBits(BTy);
19951995
if (!HasSameSize)
19961996
TypeByteSize = 0;
1997-
return DepDistanceStrideAndSizeInfo(Dist, std::abs(StrideAPtrInt),
1998-
std::abs(StrideBPtrInt), TypeByteSize,
1997+
1998+
StrideAPtrInt = std::abs(StrideAPtrInt);
1999+
StrideBPtrInt = std::abs(StrideBPtrInt);
2000+
2001+
uint64_t MaxStride = std::max(StrideAPtrInt, StrideBPtrInt);
2002+
2003+
std::optional<uint64_t> CommonStride;
2004+
if (StrideAPtrInt == StrideBPtrInt)
2005+
CommonStride = StrideAPtrInt;
2006+
2007+
// TODO: Historically, we don't retry with runtime checks unless the
2008+
// (unscaled) strides are the same. Fix this once the condition for runtime
2009+
// checks in isDependent is fixed.
2010+
bool ShouldRetryWithRuntimeCheck = CommonStride.has_value();
2011+
2012+
return DepDistanceStrideAndSizeInfo(Dist, MaxStride, CommonStride,
2013+
ShouldRetryWithRuntimeCheck, TypeByteSize,
19992014
AIsWrite, BIsWrite);
20002015
}
20012016

@@ -2011,23 +2026,21 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
20112026
if (std::holds_alternative<Dependence::DepType>(Res))
20122027
return std::get<Dependence::DepType>(Res);
20132028

2014-
auto &[Dist, StrideA, StrideB, TypeByteSize, AIsWrite, BIsWrite] =
2029+
auto &[Dist, MaxStride, CommonStride, ShouldRetryWithRuntimeCheck,
2030+
TypeByteSize, AIsWrite, BIsWrite] =
20152031
std::get<DepDistanceStrideAndSizeInfo>(Res);
20162032
bool HasSameSize = TypeByteSize > 0;
20172033

2018-
std::optional<uint64_t> CommonStride =
2019-
StrideA == StrideB ? std::make_optional(StrideA) : std::nullopt;
20202034
if (isa<SCEVCouldNotCompute>(Dist)) {
2021-
// TODO: Relax requirement that there is a common stride to retry with
2022-
// non-constant distance dependencies.
2023-
FoundNonConstantDistanceDependence |= CommonStride.has_value();
2035+
// TODO: Relax requirement that there is a common unscaled stride to retry
2036+
// with non-constant distance dependencies.
2037+
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
20242038
LLVM_DEBUG(dbgs() << "LAA: Dependence because of uncomputable distance.\n");
20252039
return Dependence::Unknown;
20262040
}
20272041

20282042
ScalarEvolution &SE = *PSE.getSE();
20292043
auto &DL = InnermostLoop->getHeader()->getDataLayout();
2030-
uint64_t MaxStride = std::max(StrideA, StrideB);
20312044

20322045
// If the distance between the acecsses is larger than their maximum absolute
20332046
// stride multiplied by the symbolic maximum backedge taken count (which is an
@@ -2086,7 +2099,7 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
20862099
// condition to consider retrying with runtime checks. Historically, we
20872100
// did not set it when strides were different but there is no inherent
20882101
// reason to.
2089-
FoundNonConstantDistanceDependence |= CommonStride.has_value();
2102+
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
20902103
return Dependence::Unknown;
20912104
}
20922105
if (!HasSameSize ||
@@ -2105,7 +2118,7 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
21052118
int64_t MinDistance = SE.getSignedRangeMin(Dist).getSExtValue();
21062119
// Below we only handle strictly positive distances.
21072120
if (MinDistance <= 0) {
2108-
FoundNonConstantDistanceDependence |= CommonStride.has_value();
2121+
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
21092122
return Dependence::Unknown;
21102123
}
21112124

@@ -2118,7 +2131,7 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
21182131
// condition to consider retrying with runtime checks. Historically, we
21192132
// did not set it when strides were different but there is no inherent
21202133
// reason to.
2121-
FoundNonConstantDistanceDependence |= CommonStride.has_value();
2134+
FoundNonConstantDistanceDependence |= ShouldRetryWithRuntimeCheck;
21222135
}
21232136

21242137
if (!HasSameSize) {

0 commit comments

Comments
 (0)