Skip to content

Commit 4a78f18

Browse files
committed
Expand VPVectorPointerRecipe to support stride
1 parent a2ca7fd commit 4a78f18

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
@@ -7521,7 +7521,10 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
75217521
new VPVectorEndPointerRecipe(Ptr, &Plan.getVF(), getLoadStoreType(I),
75227522
/*Stride*/ -1, Flags, I->getDebugLoc());
75237523
} else {
7524-
VectorPtr = new VPVectorPointerRecipe(Ptr, getLoadStoreType(I),
7524+
const DataLayout &DL = I->getDataLayout();
7525+
auto *StrideTy = DL.getIndexType(Ptr->getUnderlyingValue()->getType());
7526+
VPValue *StrideOne = Plan.getOrAddLiveIn(ConstantInt::get(StrideTy, 1));
7527+
VectorPtr = new VPVectorPointerRecipe(Ptr, getLoadStoreType(I), StrideOne,
75257528
GEP ? GEP->getNoWrapFlags()
75267529
: GEPNoWrapFlags::none(),
75277530
I->getDebugLoc());

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,20 +1900,23 @@ class VPVectorEndPointerRecipe : public VPRecipeWithIRFlags,
19001900
#endif
19011901
};
19021902

1903-
/// A recipe to compute the pointers for widened memory accesses of IndexTy.
1903+
/// A recipe to compute the pointers for widened memory accesses of IndexedTy,
1904+
/// with the Stride expressed in units of IndexedTy.
19041905
class VPVectorPointerRecipe : public VPRecipeWithIRFlags,
1905-
public VPUnrollPartAccessor<1> {
1906+
public VPUnrollPartAccessor<2> {
19061907
Type *SourceElementTy;
19071908

19081909
public:
1909-
VPVectorPointerRecipe(VPValue *Ptr, Type *SourceElementTy,
1910+
VPVectorPointerRecipe(VPValue *Ptr, Type *SourceElementTy, VPValue *Stride,
19101911
GEPNoWrapFlags GEPFlags, DebugLoc DL)
1911-
: VPRecipeWithIRFlags(VPDef::VPVectorPointerSC, ArrayRef<VPValue *>(Ptr),
1912-
GEPFlags, DL),
1912+
: VPRecipeWithIRFlags(VPDef::VPVectorPointerSC,
1913+
ArrayRef<VPValue *>({Ptr, Stride}), GEPFlags, DL),
19131914
SourceElementTy(SourceElementTy) {}
19141915

19151916
VP_CLASSOF_IMPL(VPDef::VPVectorPointerSC)
19161917

1918+
VPValue *getStride() const { return getOperand(1); }
1919+
19171920
void execute(VPTransformState &State) override;
19181921

19191922
Type *getSourceElementType() const { return SourceElementTy; }
@@ -1934,7 +1937,8 @@ class VPVectorPointerRecipe : public VPRecipeWithIRFlags,
19341937

19351938
VPVectorPointerRecipe *clone() override {
19361939
return new VPVectorPointerRecipe(getOperand(0), SourceElementTy,
1937-
getGEPNoWrapFlags(), getDebugLoc());
1940+
getStride(), getGEPNoWrapFlags(),
1941+
getDebugLoc());
19381942
}
19391943

19401944
/// Return true if this VPVectorPointerRecipe corresponds to part 0. Note that

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,13 +2606,21 @@ void VPVectorEndPointerRecipe::print(raw_ostream &O, const Twine &Indent,
26062606
void VPVectorPointerRecipe::execute(VPTransformState &State) {
26072607
auto &Builder = State.Builder;
26082608
unsigned CurrentPart = getUnrollPart(*this);
2609-
Type *IndexTy = getGEPIndexTy(State.VF.isScalable(), /*IsReverse*/ false,
2610-
/*IsUnitStride*/ true, CurrentPart, Builder);
2609+
Value *Stride = State.get(getStride(), /*IsScalar*/ true);
2610+
2611+
auto *StrideC = dyn_cast<ConstantInt>(Stride);
2612+
bool IsStrideOne = StrideC && StrideC->isOne();
2613+
bool IsUnitStride = IsStrideOne || (StrideC && StrideC->isMinusOne());
2614+
Type *IndexTy =
2615+
getGEPIndexTy(State.VF.isScalable(),
2616+
/*IsReverse*/ false, IsUnitStride, CurrentPart, Builder);
26112617
Value *Ptr = State.get(getOperand(0), VPLane(0));
26122618

2619+
Stride = Builder.CreateSExtOrTrunc(Stride, IndexTy);
26132620
Value *Increment = createStepForVF(Builder, IndexTy, State.VF, CurrentPart);
2614-
Value *ResultPtr = Builder.CreateGEP(getSourceElementType(), Ptr, Increment,
2615-
"", getGEPNoWrapFlags());
2621+
Value *Index = IsStrideOne ? Increment : Builder.CreateMul(Increment, Stride);
2622+
Value *ResultPtr = Builder.CreateGEP(getSourceElementType(), Ptr, Index, "",
2623+
getGEPNoWrapFlags());
26162624

26172625
State.set(this, ResultPtr, /*IsScalar*/ true);
26182626
}

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)