Skip to content

Commit

Permalink
[LV] Use SCEV for uniformity analysis across VF
Browse files Browse the repository at this point in the history
This patch uses SCEV to check if a value is uniform across a given VF.

The basic idea is to construct SCEVs where the AddRecs of the loop are
adjusted to reflect the version in the vectorized loop (Step multiplied
by VF). We construct a SCEV for the value of the vector lane 0
(offset 0) compare it to the expressions for lanes 1 to the last vector
lane (VF - 1). If they are equal, consider the expression uniform.

While re-writing expressions, we also need to catch expressions we
cannot determine uniformity (e.g. SCEVUnknown).

Reviewed By: Ayal

Differential Revision: https://reviews.llvm.org/D148841
  • Loading branch information
fhahn committed May 31, 2023
1 parent 4369de7 commit 572cfa3
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 232 deletions.
5 changes: 3 additions & 2 deletions llvm/include/llvm/Analysis/LoopAccessAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,9 @@ class LoopAccessInfo {
static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
DominatorTree *DT);

/// Returns true if the value V is uniform within the loop.
bool isUniform(Value *V) const;
/// Returns true if value \p V is uniform across \p VF lanes, when \p VF is
/// provided, and otherwise if \p V is invariant across all loop iterations.
bool isUniform(Value *V, std::optional<ElementCount> VF = std::nullopt) const;

uint64_t getMaxSafeDepDistBytes() const { return MaxSafeDepDistBytes; }
unsigned getNumStores() const { return NumStores; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,15 @@ class LoopVectorizationLegality {
/// loop. Do not use after invoking 'createVectorizedLoopSkeleton' (PR34965).
int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;

/// Returns true if the value V is uniform within the loop.
bool isUniform(Value *V) const;
/// Returns true if value V is uniform across \p VF lanes, when \p VF is
/// provided, and otherwise if \p V is invariant across all loop iterations.
bool isUniform(Value *V, std::optional<ElementCount> VF = std::nullopt) const;

/// A uniform memory op is a load or store which accesses the same memory
/// location on all lanes.
bool isUniformMemOp(Instruction &I) const;
/// location on all \p VF lanes, if \p VF is provided and otherwise if the
/// memory location is invariant.
bool isUniformMemOp(Instruction &I,
std::optional<ElementCount> VF = std::nullopt) const;

/// Returns the information that we collected about runtime memory check.
const RuntimePointerChecking *getRuntimePointerChecking() const {
Expand Down
113 changes: 111 additions & 2 deletions llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2532,15 +2532,124 @@ OptimizationRemarkAnalysis &LoopAccessInfo::recordAnalysis(StringRef RemarkName,
return *Report;
}

bool LoopAccessInfo::isUniform(Value *V) const {
namespace {
/// A rewriter to build the SCEVs for each of the VF lanes in the expected
/// vectorized loop, which can then be compared to detect their uniformity. This
/// is done by replacing the AddRec SCEVs of the original scalar loop (TheLoop)
/// with new AddRecs where the step is multiplied by StepMultiplier and Offset *
/// Step is added. Also checks if all sub-expressions are analyzable w.r.t.
/// uniformity.
class SCEVAddRecForUniformityRewriter
: public SCEVRewriteVisitor<SCEVAddRecForUniformityRewriter> {
/// Multiplier to be applied to the step of AddRecs in TheLoop.
unsigned StepMultiplier;

/// Offset to be added to the AddRecs in TheLoop.
unsigned Offset;

/// Loop for which to rewrite AddRecsFor.
Loop *TheLoop;

/// Is any sub-expressions not analyzable w.r.t. uniformity?
bool CannotAnalyze = false;

bool canAnalyze() const { return !CannotAnalyze; }

public:
SCEVAddRecForUniformityRewriter(ScalarEvolution &SE, unsigned StepMultiplier,
unsigned Offset, Loop *TheLoop)
: SCEVRewriteVisitor(SE), StepMultiplier(StepMultiplier), Offset(Offset),
TheLoop(TheLoop) {}

const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
assert(Expr->getLoop() == TheLoop &&
"addrec outside of TheLoop must be invariant and should have been "
"handled earlier");
// Build a new AddRec by multiplying the step by StepMultiplier and
// incrementing the start by Offset * step.
Type *Ty = Expr->getType();
auto *Step = Expr->getStepRecurrence(SE);
auto *NewStep = SE.getMulExpr(Step, SE.getConstant(Ty, StepMultiplier));
auto *ScaledOffset = SE.getMulExpr(Step, SE.getConstant(Ty, Offset));
auto *NewStart = SE.getAddExpr(Expr->getStart(), ScaledOffset);
return SE.getAddRecExpr(NewStart, NewStep, TheLoop, SCEV::FlagAnyWrap);
}

const SCEV *visit(const SCEV *S) {
if (CannotAnalyze || SE.isLoopInvariant(S, TheLoop))
return S;
return SCEVRewriteVisitor<SCEVAddRecForUniformityRewriter>::visit(S);
}

const SCEV *visitUnknown(const SCEVUnknown *S) {
if (SE.isLoopInvariant(S, TheLoop))
return S;
// The value could vary across iterations.
CannotAnalyze = true;
return S;
}

const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *S) {
// Could not analyze the expression.
CannotAnalyze = true;
return S;
}

static const SCEV *rewrite(const SCEV *S, ScalarEvolution &SE,
unsigned StepMultiplier, unsigned Offset,
Loop *TheLoop) {
/// Bail out if the expression does not contain an UDiv expression.
/// Uniform values which are not loop invariant require operations to strip
/// out the lowest bits. For now just look for UDivs and use it to avoid
/// re-writing UDIV-free expressions for other lanes to limit compile time.
if (!SCEVExprContains(S,
[](const SCEV *S) { return isa<SCEVUDivExpr>(S); }))
return SE.getCouldNotCompute();

SCEVAddRecForUniformityRewriter Rewriter(SE, StepMultiplier, Offset,
TheLoop);
const SCEV *Result = Rewriter.visit(S);

if (Rewriter.canAnalyze())
return Result;
return SE.getCouldNotCompute();
}
};

} // namespace

bool LoopAccessInfo::isUniform(Value *V, std::optional<ElementCount> VF) const {
auto *SE = PSE->getSE();
// Since we rely on SCEV for uniformity, if the type is not SCEVable, it is
// never considered uniform.
// TODO: Is this really what we want? Even without FP SCEV, we may want some
// trivially loop-invariant FP values to be considered uniform.
if (!SE->isSCEVable(V->getType()))
return false;
return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
const SCEV *S = SE->getSCEV(V);
if (SE->isLoopInvariant(S, TheLoop))
return true;
if (!VF || VF->isScalable())
return false;
if (VF->isScalar())
return true;

// Rewrite AddRecs in TheLoop to step by VF and check if the expression for
// lane 0 matches the expressions for all other lanes.
unsigned FixedVF = VF->getKnownMinValue();
const SCEV *FirstLaneExpr =
SCEVAddRecForUniformityRewriter::rewrite(S, *SE, FixedVF, 0, TheLoop);
if (isa<SCEVCouldNotCompute>(FirstLaneExpr))
return false;

// Make sure the expressions for lanes FixedVF-1..1 match the expression for
// lane 0. We check lanes in reverse order for compile-time, as frequently
// checking the last lane is sufficient to rule out uniformity.
return all_of(reverse(seq<unsigned>(1, FixedVF)), [&](unsigned I) {
const SCEV *IthLaneExpr =
SCEVAddRecForUniformityRewriter::rewrite(S, *SE, FixedVF, I, TheLoop);
return FirstLaneExpr == IthLaneExpr;
});
}

/// Find the operand of the GEP that should be checked for consecutive
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,19 +471,21 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
return 0;
}

bool LoopVectorizationLegality::isUniform(Value *V) const {
return LAI->isUniform(V);
bool LoopVectorizationLegality::isUniform(
Value *V, std::optional<ElementCount> VF) const {
return LAI->isUniform(V, VF);
}

bool LoopVectorizationLegality::isUniformMemOp(Instruction &I) const {
bool LoopVectorizationLegality::isUniformMemOp(
Instruction &I, std::optional<ElementCount> VF) const {
Value *Ptr = getLoadStorePointerOperand(&I);
if (!Ptr)
return false;
// Note: There's nothing inherent which prevents predicated loads and
// stores from being uniform. The current lowering simply doesn't handle
// it; in particular, the cost model distinguishes scatter/gather from
// scalar w/predication, and we currently rely on the scalar path.
return isUniform(Ptr) && !blockNeedsPredication(I.getParent());
return isUniform(Ptr, VF) && !blockNeedsPredication(I.getParent());
}

bool LoopVectorizationLegality::canVectorizeOuterLoop() {
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4674,7 +4674,7 @@ void LoopVectorizationCostModel::collectLoopUniforms(ElementCount VF) {
// Return true if all lanes perform the same memory operation, and we can
// thus chose to execute only one.
auto isUniformMemOpUse = [&](Instruction *I) {
if (!Legal->isUniformMemOp(*I))
if (!Legal->isUniformMemOp(*I, VF))
return false;
if (isa<LoadInst>(I))
// Loading the same address always produces the same result - at least
Expand Down Expand Up @@ -6496,7 +6496,7 @@ LoopVectorizationCostModel::getConsecutiveMemOpCost(Instruction *I,
InstructionCost
LoopVectorizationCostModel::getUniformMemOpCost(Instruction *I,
ElementCount VF) {
assert(Legal->isUniformMemOp(*I));
assert(Legal->isUniformMemOp(*I, VF));

Type *ValTy = getLoadStoreType(I);
auto *VectorTy = cast<VectorType>(ToVectorTy(ValTy, VF));
Expand Down Expand Up @@ -6872,7 +6872,7 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
if (isa<StoreInst>(&I) && isScalarWithPredication(&I, VF))
NumPredStores++;

if (Legal->isUniformMemOp(I)) {
if (Legal->isUniformMemOp(I, VF)) {
auto isLegalToScalarize = [&]() {
if (!VF.isScalable())
// Scalarization of fixed length vectors "just works".
Expand Down
Loading

0 comments on commit 572cfa3

Please sign in to comment.