Skip to content

Commit f4f50e6

Browse files
committed
Add TII::preferStridedLoadStore to control the behavior
1 parent 627fbaa commit f4f50e6

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,10 @@ class TargetTransformInfo {
17761776
/// otherwise scalar epilogue loop.
17771777
bool preferEpilogueVectorization() const;
17781778

1779+
/// Return true if the loop vectorizer prefer strided load/store when
1780+
/// vectorizing reversed load/store.
1781+
bool preferStridedLoadStore() const;
1782+
17791783
/// \returns True if the target wants to expand the given reduction intrinsic
17801784
/// into a shuffle sequence.
17811785
bool shouldExpandReduction(const IntrinsicInst *II) const;
@@ -2301,6 +2305,7 @@ class TargetTransformInfo::Concept {
23012305
virtual bool preferPredicatedReductionSelect(unsigned Opcode, Type *Ty,
23022306
ReductionFlags) const = 0;
23032307
virtual bool preferEpilogueVectorization() const = 0;
2308+
virtual bool preferStridedLoadStore() const = 0;
23042309

23052310
virtual bool shouldExpandReduction(const IntrinsicInst *II) const = 0;
23062311
virtual ReductionShuffle
@@ -3105,6 +3110,10 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
31053110
return Impl.preferEpilogueVectorization();
31063111
}
31073112

3113+
bool preferStridedLoadStore() const override {
3114+
return Impl.preferStridedLoadStore();
3115+
}
3116+
31083117
bool shouldExpandReduction(const IntrinsicInst *II) const override {
31093118
return Impl.shouldExpandReduction(II);
31103119
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,8 @@ class TargetTransformInfoImplBase {
10031003
return true;
10041004
}
10051005

1006+
bool preferStridedLoadStore() const { return false; }
1007+
10061008
bool shouldExpandReduction(const IntrinsicInst *II) const { return true; }
10071009

10081010
TTI::ReductionShuffle

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,10 @@ bool TargetTransformInfo::preferEpilogueVectorization() const {
13741374
return TTIImpl->preferEpilogueVectorization();
13751375
}
13761376

1377+
bool TargetTransformInfo::preferStridedLoadStore() const {
1378+
return TTIImpl->preferStridedLoadStore();
1379+
}
1380+
13771381
TargetTransformInfo::VPLegalization
13781382
TargetTransformInfo::getVPLegalizationStrategy(const VPIntrinsic &VPI) const {
13791383
return TTIImpl->getVPLegalizationStrategy(VPI);

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
118118
return false;
119119
}
120120

121+
bool preferStridedLoadStore() const { return true; }
122+
121123
InstructionCost getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
122124
Align Alignment, unsigned AddressSpace,
123125
TTI::TargetCostKind CostKind);

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,13 +2603,25 @@ void VPWidenLoadRecipe::print(raw_ostream &O, const Twine &Indent,
26032603
}
26042604
#endif
26052605

2606+
/// Use all-true mask for reverse rather than actual mask, as it avoids a
2607+
/// dependence w/o affecting the result.
2608+
static Instruction *createReverseEVL(IRBuilderBase &Builder, Value *Operand,
2609+
Value *EVL, const Twine &Name) {
2610+
VectorType *ValTy = cast<VectorType>(Operand->getType());
2611+
Value *AllTrueMask =
2612+
Builder.CreateVectorSplat(ValTy->getElementCount(), Builder.getTrue());
2613+
return Builder.CreateIntrinsic(ValTy, Intrinsic::experimental_vp_reverse,
2614+
{Operand, AllTrueMask, EVL}, nullptr, Name);
2615+
}
2616+
26062617
void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {
26072618
auto *LI = cast<LoadInst>(&Ingredient);
26082619

26092620
Type *ScalarDataTy = getLoadStoreType(&Ingredient);
26102621
auto *DataTy = VectorType::get(ScalarDataTy, State.VF);
26112622
const Align Alignment = getLoadStoreAlignment(&Ingredient);
26122623
bool CreateGather = !isConsecutive();
2624+
bool UseStridedLoadStore = State.TTI->preferStridedLoadStore();
26132625

26142626
auto &Builder = State.Builder;
26152627
State.setDebugLocFrom(getDebugLoc());
@@ -2619,6 +2631,8 @@ void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {
26192631
Value *Mask = nullptr;
26202632
if (VPValue *VPMask = getMask()) {
26212633
Mask = State.get(VPMask);
2634+
if (isReverse() && !UseStridedLoadStore)
2635+
Mask = createReverseEVL(Builder, Mask, EVL, "vp.reverse.mask");
26222636
} else {
26232637
Mask = Builder.CreateVectorSplat(State.VF, Builder.getTrue());
26242638
}
@@ -2627,7 +2641,7 @@ void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {
26272641
NewLI =
26282642
Builder.CreateIntrinsic(DataTy, Intrinsic::vp_gather, {Addr, Mask, EVL},
26292643
nullptr, "wide.masked.gather");
2630-
} else if (isReverse()) {
2644+
} else if (isReverse() && UseStridedLoadStore) {
26312645
auto *EltTy = DataTy->getElementType();
26322646
auto *PtrTy = Addr->getType();
26332647
Value *Operands[] = {
@@ -2648,6 +2662,8 @@ void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {
26482662
0, Attribute::getWithAlignment(NewLI->getContext(), Alignment));
26492663
State.addMetadata(NewLI, LI);
26502664
Instruction *Res = NewLI;
2665+
if (isReverse() && !UseStridedLoadStore)
2666+
Res = createReverseEVL(Builder, Res, EVL, "vp.reverse");
26512667
State.set(this, Res);
26522668
}
26532669

@@ -2738,16 +2754,21 @@ void VPWidenStoreEVLRecipe::execute(VPTransformState &State) {
27382754
VPValue *StoredValue = getStoredValue();
27392755
bool CreateScatter = !isConsecutive();
27402756
const Align Alignment = getLoadStoreAlignment(&Ingredient);
2757+
bool UseStridedLoadStore = State.TTI->preferStridedLoadStore();
27412758

27422759
auto &Builder = State.Builder;
27432760
State.setDebugLocFrom(getDebugLoc());
27442761

27452762
CallInst *NewSI = nullptr;
27462763
Value *StoredVal = State.get(StoredValue);
27472764
Value *EVL = State.get(getEVL(), VPLane(0));
2765+
if (isReverse() && !UseStridedLoadStore)
2766+
StoredVal = createReverseEVL(Builder, StoredVal, EVL, "vp.reverse");
27482767
Value *Mask = nullptr;
27492768
if (VPValue *VPMask = getMask()) {
27502769
Mask = State.get(VPMask);
2770+
if (isReverse() && !UseStridedLoadStore)
2771+
Mask = createReverseEVL(Builder, Mask, EVL, "vp.reverse.mask");
27512772
} else {
27522773
Mask = Builder.CreateVectorSplat(State.VF, Builder.getTrue());
27532774
}
@@ -2756,7 +2777,7 @@ void VPWidenStoreEVLRecipe::execute(VPTransformState &State) {
27562777
NewSI = Builder.CreateIntrinsic(Type::getVoidTy(EVL->getContext()),
27572778
Intrinsic::vp_scatter,
27582779
{StoredVal, Addr, Mask, EVL});
2759-
} else if (isReverse()) {
2780+
} else if (isReverse() && UseStridedLoadStore) {
27602781
Type *StoredValTy = StoredVal->getType();
27612782
auto *EltTy = cast<VectorType>(StoredValTy)->getElementType();
27622783
auto *PtrTy = Addr->getType();

0 commit comments

Comments
 (0)