Skip to content

Commit 1032016

Browse files
committed
Introduce StrideInfo in CostMdoel
1 parent 0bb1b34 commit 1032016

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,16 @@ class LoopVectorizationCostModel {
13511351
/// that can be vectorized.
13521352
bool stridedAccessCanBeWidened(Instruction *I, ElementCount VF) const;
13531353

1354+
/// Get the stride of the strided memory access instruction \p Instr. Return 0
1355+
/// if the instruction \p Instr is not considered for vectorization as a
1356+
/// strided memory access.
1357+
int64_t getStride(Instruction *Instr) const {
1358+
auto It = StrideInfo.find(Instr);
1359+
if (It != StrideInfo.end())
1360+
return It->second;
1361+
return 0;
1362+
}
1363+
13541364
/// Returns true if we're required to use a scalar epilogue for at least
13551365
/// the final iteration of the original loop.
13561366
bool requiresScalarEpilogue(bool IsVectorizing) const {
@@ -1763,6 +1773,9 @@ class LoopVectorizationCostModel {
17631773
Ops, [this, VF](Value *V) { return this->needsExtract(V, VF); }));
17641774
}
17651775

1776+
/// The mapping of memory access instructions to their stride values.
1777+
DenseMap<Instruction *, int64_t> StrideInfo;
1778+
17661779
public:
17671780
/// The loop that we evaluate.
17681781
Loop *TheLoop;
@@ -6183,6 +6196,7 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
61836196
if (StridedLoadStoreCost < Cost) {
61846197
Decision = CM_Strided;
61856198
Cost = StridedLoadStoreCost;
6199+
StrideInfo[&I] = ConsecutiveStride;
61866200
}
61876201
}
61886202
setWideningDecision(&I, VF, Decision, Cost);
@@ -8427,9 +8441,12 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
84278441
if (Strided) {
84288442
const DataLayout &DL = Load->getDataLayout();
84298443
auto *StrideTy = DL.getIndexType(Load->getPointerOperand()->getType());
8430-
VPValue *Stride = Plan.getOrAddLiveIn(ConstantInt::get(
8431-
StrideTy, -1 * DL.getTypeAllocSize(getLoadStoreType(Load))));
8432-
return new VPWidenStridedLoadRecipe(*Load, Ptr, Stride, &Plan.getVF(),
8444+
int64_t Stride = CM.getStride(Load);
8445+
assert(Stride == -1 &&
8446+
"Only stride memory access with a stride of -1 is supported.");
8447+
VPValue *StrideVPV = Plan.getOrAddLiveIn(ConstantInt::get(
8448+
StrideTy, Stride * DL.getTypeAllocSize(getLoadStoreType(Load))));
8449+
return new VPWidenStridedLoadRecipe(*Load, Ptr, StrideVPV, &Plan.getVF(),
84338450
Mask, I->getDebugLoc());
84348451
}
84358452
return new VPWidenLoadRecipe(*Load, Ptr, Mask, Consecutive, Reverse,

0 commit comments

Comments
 (0)