Skip to content

Commit 28be3f8

Browse files
committed
[LAA] Cache pointer bounds expansions (NFCI).
This avoids expanding the same bounds multiple times, which helps reduce the compile-time impact of removing the restrictions added in 234cc40, notably -0.06% on stage1-O3 and -0.05% on both stage1-ReleaseThinLTO and stage1-ReleaseLTO-g. https://llvm-compile-time-tracker.com/compare.php?from=8b9ebc4bb86cf0979e05908cbb04336f2d01dda5&to=fabd36f96c31e47ea72653f5a404feaadfc7b5b5&stat=instructions:u
1 parent 4002e38 commit 28be3f8

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

llvm/include/llvm/Analysis/LoopAccessAnalysis.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ class MemoryDepChecker {
269269

270270
const Loop *getInnermostLoop() const { return InnermostLoop; }
271271

272+
DenseMap<const SCEV *, std::pair<const SCEV *, const SCEV *>> &
273+
getPointerBounds() {
274+
return PointerBounds;
275+
}
276+
272277
private:
273278
/// A wrapper around ScalarEvolution, used to add runtime SCEV checks, and
274279
/// applies dynamic knowledge to simplify SCEV expressions and convert them
@@ -327,6 +332,10 @@ class MemoryDepChecker {
327332
/// backwards-vectorizable or unknown (triggering a runtime check).
328333
unsigned MaxTargetVectorWidthInBits = 0;
329334

335+
/// Mapping of SCEV expressions to their expanded pointer bounds (pair of
336+
/// start and end pointer expressions).
337+
DenseMap<const SCEV *, std::pair<const SCEV *, const SCEV *>> PointerBounds;
338+
330339
/// Check whether there is a plausible dependence between the two
331340
/// accesses.
332341
///

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,18 @@ RuntimeCheckingPtrGroup::RuntimeCheckingPtrGroup(
203203
///
204204
/// There is no conflict when the intervals are disjoint:
205205
/// NoConflict = (P2.Start >= P1.End) || (P1.Start >= P2.End)
206-
static std::pair<const SCEV *, const SCEV *>
207-
getStartAndEndForAccess(const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy,
208-
PredicatedScalarEvolution &PSE) {
206+
static std::pair<const SCEV *, const SCEV *> getStartAndEndForAccess(
207+
const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy,
208+
PredicatedScalarEvolution &PSE,
209+
DenseMap<const SCEV *, std::pair<const SCEV *, const SCEV *>>
210+
&PointerBounds) {
209211
ScalarEvolution *SE = PSE.getSE();
210212

213+
auto [Iter, Ins] = PointerBounds.insert(
214+
{PtrExpr, {SE->getCouldNotCompute(), SE->getCouldNotCompute()}});
215+
if (!Ins)
216+
return Iter->second;
217+
211218
const SCEV *ScStart;
212219
const SCEV *ScEnd;
213220

@@ -244,7 +251,8 @@ getStartAndEndForAccess(const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy,
244251
const SCEV *EltSizeSCEV = SE->getStoreSizeOfExpr(IdxTy, AccessTy);
245252
ScEnd = SE->getAddExpr(ScEnd, EltSizeSCEV);
246253

247-
return {ScStart, ScEnd};
254+
Iter->second = {ScStart, ScEnd};
255+
return Iter->second;
248256
}
249257

250258
/// Calculate Start and End points of memory access using
@@ -254,8 +262,8 @@ void RuntimePointerChecking::insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr,
254262
unsigned DepSetId, unsigned ASId,
255263
PredicatedScalarEvolution &PSE,
256264
bool NeedsFreeze) {
257-
const auto &[ScStart, ScEnd] =
258-
getStartAndEndForAccess(Lp, PtrExpr, AccessTy, PSE);
265+
const auto &[ScStart, ScEnd] = getStartAndEndForAccess(
266+
Lp, PtrExpr, AccessTy, PSE, DC.getPointerBounds());
259267
assert(!isa<SCEVCouldNotCompute>(ScStart) &&
260268
!isa<SCEVCouldNotCompute>(ScEnd) &&
261269
"must be able to compute both start and end expressions");
@@ -1964,10 +1972,9 @@ MemoryDepChecker::getDependenceDistanceStrideAndSize(
19641972
if (SE.isLoopInvariant(Src, InnermostLoop) ||
19651973
SE.isLoopInvariant(Sink, InnermostLoop)) {
19661974
const auto &[SrcStart, SrcEnd] =
1967-
getStartAndEndForAccess(InnermostLoop, Src, ATy, PSE);
1975+
getStartAndEndForAccess(InnermostLoop, Src, ATy, PSE, PointerBounds);
19681976
const auto &[SinkStart, SinkEnd] =
1969-
getStartAndEndForAccess(InnermostLoop, Sink, BTy, PSE);
1970-
1977+
getStartAndEndForAccess(InnermostLoop, Sink, BTy, PSE, PointerBounds);
19711978
if (!isa<SCEVCouldNotCompute>(SrcStart) &&
19721979
!isa<SCEVCouldNotCompute>(SrcEnd) &&
19731980
!isa<SCEVCouldNotCompute>(SinkStart) &&

0 commit comments

Comments
 (0)