Skip to content

Commit 9ee98f8

Browse files
committed
Expand VPVectorPointerRecipe to support stride
1 parent 54ced5a commit 9ee98f8

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7770,7 +7770,10 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
77707770
new VPVectorEndPointerRecipe(Ptr, &Plan.getVF(), getLoadStoreType(I),
77717771
/*Stride*/ -1, Flags, I->getDebugLoc());
77727772
} else {
7773-
VectorPtr = new VPVectorPointerRecipe(Ptr, getLoadStoreType(I),
7773+
const DataLayout &DL = I->getDataLayout();
7774+
auto *StrideTy = DL.getIndexType(Ptr->getUnderlyingValue()->getType());
7775+
VPValue *StrideOne = Plan.getOrAddLiveIn(ConstantInt::get(StrideTy, 1));
7776+
VectorPtr = new VPVectorPointerRecipe(Ptr, getLoadStoreType(I), StrideOne,
77747777
GEP ? GEP->getNoWrapFlags()
77757778
: GEPNoWrapFlags::none(),
77767779
I->getDebugLoc());

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,20 +1764,23 @@ class VPVectorEndPointerRecipe : public VPRecipeWithIRFlags,
17641764
#endif
17651765
};
17661766

1767-
/// A recipe to compute the pointers for widened memory accesses of IndexTy.
1767+
/// A recipe to compute the pointers for widened memory accesses of IndexedTy,
1768+
/// with the Stride expressed in units of IndexedTy.
17681769
class VPVectorPointerRecipe : public VPRecipeWithIRFlags,
1769-
public VPUnrollPartAccessor<1> {
1770+
public VPUnrollPartAccessor<2> {
17701771
Type *IndexedTy;
17711772

17721773
public:
1773-
VPVectorPointerRecipe(VPValue *Ptr, Type *IndexedTy, GEPNoWrapFlags GEPFlags,
1774-
DebugLoc DL)
1775-
: VPRecipeWithIRFlags(VPDef::VPVectorPointerSC, ArrayRef<VPValue *>(Ptr),
1776-
GEPFlags, DL),
1774+
VPVectorPointerRecipe(VPValue *Ptr, Type *IndexedTy, VPValue *Stride,
1775+
GEPNoWrapFlags GEPFlags, DebugLoc DL)
1776+
: VPRecipeWithIRFlags(VPDef::VPVectorPointerSC,
1777+
ArrayRef<VPValue *>({Ptr, Stride}), GEPFlags, DL),
17771778
IndexedTy(IndexedTy) {}
17781779

17791780
VP_CLASSOF_IMPL(VPDef::VPVectorPointerSC)
17801781

1782+
VPValue *getStride() const { return getOperand(1); }
1783+
17811784
void execute(VPTransformState &State) override;
17821785

17831786
bool onlyFirstLaneUsed(const VPValue *Op) const override {
@@ -1795,7 +1798,7 @@ class VPVectorPointerRecipe : public VPRecipeWithIRFlags,
17951798
}
17961799

17971800
VPVectorPointerRecipe *clone() override {
1798-
return new VPVectorPointerRecipe(getOperand(0), IndexedTy,
1801+
return new VPVectorPointerRecipe(getOperand(0), IndexedTy, getStride(),
17991802
getGEPNoWrapFlags(), getDebugLoc());
18001803
}
18011804

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,13 +2382,22 @@ void VPVectorEndPointerRecipe::print(raw_ostream &O, const Twine &Indent,
23822382
void VPVectorPointerRecipe::execute(VPTransformState &State) {
23832383
auto &Builder = State.Builder;
23842384
unsigned CurrentPart = getUnrollPart(*this);
2385-
Type *IndexTy = getGEPIndexTy(State.VF.isScalable(), /*IsReverse*/ false,
2386-
/*IsUnitStride*/ true, CurrentPart, Builder);
2385+
Value *Stride = State.get(getStride(), /*IsScalar*/ true);
2386+
2387+
auto *StrideC = dyn_cast<ConstantInt>(Stride);
2388+
bool IsStrideOne = StrideC && StrideC->isOne();
2389+
bool IsUnitStride = IsStrideOne || (StrideC && StrideC->isMinusOne());
2390+
Type *IndexTy =
2391+
getGEPIndexTy(State.VF.isScalable(),
2392+
/*IsReverse*/ false, IsUnitStride, CurrentPart, Builder);
23872393
Value *Ptr = State.get(getOperand(0), VPLane(0));
23882394

2395+
Stride = Builder.CreateSExtOrTrunc(Stride, IndexTy);
23892396
Value *Increment = createStepForVF(Builder, IndexTy, State.VF, CurrentPart);
2397+
Value *Index = IsStrideOne ? Increment : Builder.CreateMul(Increment, Stride);
2398+
23902399
Value *ResultPtr =
2391-
Builder.CreateGEP(IndexedTy, Ptr, Increment, "", getGEPNoWrapFlags());
2400+
Builder.CreateGEP(IndexedTy, Ptr, Index, "", getGEPNoWrapFlags());
23922401

23932402
State.set(this, ResultPtr, /*IsScalar*/ true);
23942403
}

llvm/test/Transforms/LoopVectorize/vplan-dot-printing.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ define void @print_call_and_memory(i64 %n, ptr noalias %y, ptr noalias %x) nounw
4242
; CHECK-NEXT: " EMIT vp\<[[CAN_IV:%.+]]\> = CANONICAL-INDUCTION ir\<0\>, vp\<[[CAN_IV_NEXT:%.+]]\>\l" +
4343
; CHECK-NEXT: " vp\<[[STEPS:%.+]]\> = SCALAR-STEPS vp\<[[CAN_IV]]\>, ir\<1\>, vp\<[[VF]]\>\l" +
4444
; CHECK-NEXT: " CLONE ir\<%arrayidx\> = getelementptr inbounds ir\<%y\>, vp\<[[STEPS]]\>\l" +
45-
; CHECK-NEXT: " vp\<[[VEC_PTR:%.+]]\> = vector-pointer ir\<%arrayidx\>\l" +
45+
; CHECK-NEXT: " vp\<[[VEC_PTR:%.+]]\> = vector-pointer ir\<%arrayidx\>, ir\<1\>\l" +
4646
; CHECK-NEXT: " WIDEN ir\<%lv\> = load vp\<[[VEC_PTR]]\>\l" +
4747
; CHECK-NEXT: " WIDEN-INTRINSIC ir\<%call\> = call llvm.sqrt(ir\<%lv\>)\l" +
4848
; CHECK-NEXT: " CLONE ir\<%arrayidx2\> = getelementptr inbounds ir\<%x\>, vp\<[[STEPS]]\>\l" +
49-
; CHECK-NEXT: " vp\<[[VEC_PTR2:%.+]]\> = vector-pointer ir\<%arrayidx2\>\l" +
49+
; CHECK-NEXT: " vp\<[[VEC_PTR2:%.+]]\> = vector-pointer ir\<%arrayidx2\>, ir\<1\>\l" +
5050
; CHECK-NEXT: " WIDEN store vp\<[[VEC_PTR2]]\>, ir\<%call\>\l" +
5151
; CHECK-NEXT: " EMIT vp\<[[CAN_IV_NEXT]]\> = add nuw vp\<[[CAN_IV]]\>, vp\<[[VFxUF]]\>\l" +
5252
; CHECK-NEXT: " EMIT branch-on-count vp\<[[CAN_IV_NEXT]]\>, vp\<[[VEC_TC]]\>\l" +

0 commit comments

Comments
 (0)