Skip to content

Commit

Permalink
[LAA] Cache pointer bounds expansions (NFCI).
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fhahn committed Jul 4, 2024
1 parent 4002e38 commit 28be3f8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
9 changes: 9 additions & 0 deletions llvm/include/llvm/Analysis/LoopAccessAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ class MemoryDepChecker {

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

DenseMap<const SCEV *, std::pair<const SCEV *, const SCEV *>> &
getPointerBounds() {
return PointerBounds;
}

private:
/// A wrapper around ScalarEvolution, used to add runtime SCEV checks, and
/// applies dynamic knowledge to simplify SCEV expressions and convert them
Expand Down Expand Up @@ -327,6 +332,10 @@ class MemoryDepChecker {
/// backwards-vectorizable or unknown (triggering a runtime check).
unsigned MaxTargetVectorWidthInBits = 0;

/// Mapping of SCEV expressions to their expanded pointer bounds (pair of
/// start and end pointer expressions).
DenseMap<const SCEV *, std::pair<const SCEV *, const SCEV *>> PointerBounds;

/// Check whether there is a plausible dependence between the two
/// accesses.
///
Expand Down
25 changes: 16 additions & 9 deletions llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,18 @@ RuntimeCheckingPtrGroup::RuntimeCheckingPtrGroup(
///
/// There is no conflict when the intervals are disjoint:
/// NoConflict = (P2.Start >= P1.End) || (P1.Start >= P2.End)
static std::pair<const SCEV *, const SCEV *>
getStartAndEndForAccess(const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy,
PredicatedScalarEvolution &PSE) {
static std::pair<const SCEV *, const SCEV *> getStartAndEndForAccess(
const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy,
PredicatedScalarEvolution &PSE,
DenseMap<const SCEV *, std::pair<const SCEV *, const SCEV *>>
&PointerBounds) {
ScalarEvolution *SE = PSE.getSE();

auto [Iter, Ins] = PointerBounds.insert(
{PtrExpr, {SE->getCouldNotCompute(), SE->getCouldNotCompute()}});
if (!Ins)
return Iter->second;

const SCEV *ScStart;
const SCEV *ScEnd;

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

return {ScStart, ScEnd};
Iter->second = {ScStart, ScEnd};
return Iter->second;
}

/// Calculate Start and End points of memory access using
Expand All @@ -254,8 +262,8 @@ void RuntimePointerChecking::insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr,
unsigned DepSetId, unsigned ASId,
PredicatedScalarEvolution &PSE,
bool NeedsFreeze) {
const auto &[ScStart, ScEnd] =
getStartAndEndForAccess(Lp, PtrExpr, AccessTy, PSE);
const auto &[ScStart, ScEnd] = getStartAndEndForAccess(
Lp, PtrExpr, AccessTy, PSE, DC.getPointerBounds());
assert(!isa<SCEVCouldNotCompute>(ScStart) &&
!isa<SCEVCouldNotCompute>(ScEnd) &&
"must be able to compute both start and end expressions");
Expand Down Expand Up @@ -1964,10 +1972,9 @@ MemoryDepChecker::getDependenceDistanceStrideAndSize(
if (SE.isLoopInvariant(Src, InnermostLoop) ||
SE.isLoopInvariant(Sink, InnermostLoop)) {
const auto &[SrcStart, SrcEnd] =
getStartAndEndForAccess(InnermostLoop, Src, ATy, PSE);
getStartAndEndForAccess(InnermostLoop, Src, ATy, PSE, PointerBounds);
const auto &[SinkStart, SinkEnd] =
getStartAndEndForAccess(InnermostLoop, Sink, BTy, PSE);

getStartAndEndForAccess(InnermostLoop, Sink, BTy, PSE, PointerBounds);
if (!isa<SCEVCouldNotCompute>(SrcStart) &&
!isa<SCEVCouldNotCompute>(SrcEnd) &&
!isa<SCEVCouldNotCompute>(SinkStart) &&
Expand Down

0 comments on commit 28be3f8

Please sign in to comment.