Skip to content

Commit 8e67056

Browse files
author
git apple-llvm automerger
committed
Merge commit '1156bd4fc3a7' from llvm.org/main into next
2 parents 4835676 + 1156bd4 commit 8e67056

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,8 @@ class InnerLoopVectorizer {
544544
/// vectorized loop.
545545
void vectorizeMemoryInstruction(Instruction *Instr, VPTransformState &State,
546546
VPValue *Def, VPValue *Addr,
547-
VPValue *StoredValue, VPValue *BlockInMask);
547+
VPValue *StoredValue, VPValue *BlockInMask,
548+
bool ConsecutiveStride, bool Reverse);
548549

549550
/// Set the debug location in the builder \p Ptr using the debug location in
550551
/// \p V. If \p Ptr is None then it uses the class member's Builder.
@@ -2900,7 +2901,8 @@ void InnerLoopVectorizer::vectorizeInterleaveGroup(
29002901

29012902
void InnerLoopVectorizer::vectorizeMemoryInstruction(
29022903
Instruction *Instr, VPTransformState &State, VPValue *Def, VPValue *Addr,
2903-
VPValue *StoredValue, VPValue *BlockInMask) {
2904+
VPValue *StoredValue, VPValue *BlockInMask, bool ConsecutiveStride,
2905+
bool Reverse) {
29042906
// Attempt to issue a wide load.
29052907
LoadInst *LI = dyn_cast<LoadInst>(Instr);
29062908
StoreInst *SI = dyn_cast<StoreInst>(Instr);
@@ -2909,31 +2911,11 @@ void InnerLoopVectorizer::vectorizeMemoryInstruction(
29092911
assert((!SI || StoredValue) && "No stored value provided for widened store");
29102912
assert((!LI || !StoredValue) && "Stored value provided for widened load");
29112913

2912-
LoopVectorizationCostModel::InstWidening Decision =
2913-
Cost->getWideningDecision(Instr, VF);
2914-
assert((Decision == LoopVectorizationCostModel::CM_Widen ||
2915-
Decision == LoopVectorizationCostModel::CM_Widen_Reverse ||
2916-
Decision == LoopVectorizationCostModel::CM_GatherScatter) &&
2917-
"CM decision is not to widen the memory instruction");
2918-
29192914
Type *ScalarDataTy = getLoadStoreType(Instr);
29202915

29212916
auto *DataTy = VectorType::get(ScalarDataTy, VF);
29222917
const Align Alignment = getLoadStoreAlignment(Instr);
2923-
2924-
// Determine if the pointer operand of the access is either consecutive or
2925-
// reverse consecutive.
2926-
bool Reverse = (Decision == LoopVectorizationCostModel::CM_Widen_Reverse);
2927-
bool ConsecutiveStride =
2928-
Reverse || (Decision == LoopVectorizationCostModel::CM_Widen);
2929-
bool CreateGatherScatter =
2930-
(Decision == LoopVectorizationCostModel::CM_GatherScatter);
2931-
2932-
// Either Ptr feeds a vector load/store, or a vector GEP should feed a vector
2933-
// gather/scatter. Otherwise Decision should have been to Scalarize.
2934-
assert((ConsecutiveStride || CreateGatherScatter) &&
2935-
"The instruction should be scalarized");
2936-
(void)ConsecutiveStride;
2918+
bool CreateGatherScatter = !ConsecutiveStride;
29372919

29382920
VectorParts BlockInMaskParts(UF);
29392921
bool isMaskRequired = BlockInMask;
@@ -8808,12 +8790,21 @@ VPRecipeBase *VPRecipeBuilder::tryToWidenMemory(Instruction *I,
88088790
if (Legal->isMaskRequired(I))
88098791
Mask = createBlockInMask(I->getParent(), Plan);
88108792

8793+
// Determine if the pointer operand of the access is either consecutive or
8794+
// reverse consecutive.
8795+
LoopVectorizationCostModel::InstWidening Decision =
8796+
CM.getWideningDecision(I, Range.Start);
8797+
bool Reverse = Decision == LoopVectorizationCostModel::CM_Widen_Reverse;
8798+
bool Consecutive =
8799+
Reverse || Decision == LoopVectorizationCostModel::CM_Widen;
8800+
88118801
if (LoadInst *Load = dyn_cast<LoadInst>(I))
8812-
return new VPWidenMemoryInstructionRecipe(*Load, Operands[0], Mask);
8802+
return new VPWidenMemoryInstructionRecipe(*Load, Operands[0], Mask,
8803+
Consecutive, Reverse);
88138804

88148805
StoreInst *Store = cast<StoreInst>(I);
88158806
return new VPWidenMemoryInstructionRecipe(*Store, Operands[1], Operands[0],
8816-
Mask);
8807+
Mask, Consecutive, Reverse);
88178808
}
88188809

88198810
VPWidenIntOrFpInductionRecipe *
@@ -9912,7 +9903,7 @@ void VPWidenMemoryInstructionRecipe::execute(VPTransformState &State) {
99129903
VPValue *StoredValue = isStore() ? getStoredValue() : nullptr;
99139904
State.ILV->vectorizeMemoryInstruction(
99149905
&Ingredient, State, StoredValue ? nullptr : getVPSingleValue(), getAddr(),
9915-
StoredValue, getMask());
9906+
StoredValue, getMask(), Consecutive, Reverse);
99169907
}
99179908

99189909
// Determine how to lower the scalar epilogue, which depends on 1) optimising

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,12 @@ class VPPredInstPHIRecipe : public VPRecipeBase, public VPValue {
15141514
class VPWidenMemoryInstructionRecipe : public VPRecipeBase {
15151515
Instruction &Ingredient;
15161516

1517+
// Whether the loaded-from / stored-to addresses are consecutive.
1518+
bool Consecutive;
1519+
1520+
// Whether the consecutive loaded/stored addresses are in reverse order.
1521+
bool Reverse;
1522+
15171523
void setMask(VPValue *Mask) {
15181524
if (!Mask)
15191525
return;
@@ -1525,16 +1531,21 @@ class VPWidenMemoryInstructionRecipe : public VPRecipeBase {
15251531
}
15261532

15271533
public:
1528-
VPWidenMemoryInstructionRecipe(LoadInst &Load, VPValue *Addr, VPValue *Mask)
1529-
: VPRecipeBase(VPWidenMemoryInstructionSC, {Addr}), Ingredient(Load) {
1534+
VPWidenMemoryInstructionRecipe(LoadInst &Load, VPValue *Addr, VPValue *Mask,
1535+
bool Consecutive, bool Reverse)
1536+
: VPRecipeBase(VPWidenMemoryInstructionSC, {Addr}), Ingredient(Load),
1537+
Consecutive(Consecutive), Reverse(Reverse) {
1538+
assert((Consecutive || !Reverse) && "Reverse implies consecutive");
15301539
new VPValue(VPValue::VPVMemoryInstructionSC, &Load, this);
15311540
setMask(Mask);
15321541
}
15331542

15341543
VPWidenMemoryInstructionRecipe(StoreInst &Store, VPValue *Addr,
1535-
VPValue *StoredValue, VPValue *Mask)
1544+
VPValue *StoredValue, VPValue *Mask,
1545+
bool Consecutive, bool Reverse)
15361546
: VPRecipeBase(VPWidenMemoryInstructionSC, {Addr, StoredValue}),
1537-
Ingredient(Store) {
1547+
Ingredient(Store), Consecutive(Consecutive), Reverse(Reverse) {
1548+
assert((Consecutive || !Reverse) && "Reverse implies consecutive");
15381549
setMask(Mask);
15391550
}
15401551

@@ -1564,6 +1575,13 @@ class VPWidenMemoryInstructionRecipe : public VPRecipeBase {
15641575
return getOperand(1); // Stored value is the 2nd, mandatory operand.
15651576
}
15661577

1578+
// Return whether the loaded-from / stored-to addresses are consecutive.
1579+
bool isConsecutive() const { return Consecutive; }
1580+
1581+
// Return whether the consecutive loaded/stored addresses are in reverse
1582+
// order.
1583+
bool isReverse() const { return Reverse; }
1584+
15671585
/// Generate the wide load/store.
15681586
void execute(VPTransformState &State) override;
15691587

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ void VPlanTransforms::VPInstructionsToVPRecipes(
6161
if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
6262
NewRecipe = new VPWidenMemoryInstructionRecipe(
6363
*Load, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
64-
nullptr /*Mask*/);
64+
nullptr /*Mask*/, false /*Consecutive*/, false /*Reverse*/);
6565
} else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
6666
NewRecipe = new VPWidenMemoryInstructionRecipe(
6767
*Store, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
68-
Plan->getOrAddVPValue(Store->getValueOperand()),
69-
nullptr /*Mask*/);
68+
Plan->getOrAddVPValue(Store->getValueOperand()), nullptr /*Mask*/,
69+
false /*Consecutive*/, false /*Reverse*/);
7070
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
7171
NewRecipe = new VPWidenGEPRecipe(
7272
GEP, Plan->mapToVPValues(GEP->operands()), OrigLoop);

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ TEST(VPRecipeTest, CastVPWidenMemoryInstructionRecipeToVPUserAndVPDef) {
936936
new LoadInst(Int32, UndefValue::get(Int32Ptr), "", false, Align(1));
937937
VPValue Addr;
938938
VPValue Mask;
939-
VPWidenMemoryInstructionRecipe Recipe(*Load, &Addr, &Mask);
939+
VPWidenMemoryInstructionRecipe Recipe(*Load, &Addr, &Mask, true, false);
940940
EXPECT_TRUE(isa<VPUser>(&Recipe));
941941
VPRecipeBase *BaseR = &Recipe;
942942
EXPECT_TRUE(isa<VPUser>(BaseR));
@@ -1032,7 +1032,7 @@ TEST(VPRecipeTest, MayHaveSideEffectsAndMayReadWriteMemory) {
10321032
new LoadInst(Int32, UndefValue::get(Int32Ptr), "", false, Align(1));
10331033
VPValue Addr;
10341034
VPValue Mask;
1035-
VPWidenMemoryInstructionRecipe Recipe(*Load, &Addr, &Mask);
1035+
VPWidenMemoryInstructionRecipe Recipe(*Load, &Addr, &Mask, true, false);
10361036
EXPECT_TRUE(Recipe.mayHaveSideEffects());
10371037
EXPECT_TRUE(Recipe.mayReadFromMemory());
10381038
EXPECT_FALSE(Recipe.mayWriteToMemory());
@@ -1046,7 +1046,8 @@ TEST(VPRecipeTest, MayHaveSideEffectsAndMayReadWriteMemory) {
10461046
VPValue Addr;
10471047
VPValue Mask;
10481048
VPValue StoredV;
1049-
VPWidenMemoryInstructionRecipe Recipe(*Store, &Addr, &StoredV, &Mask);
1049+
VPWidenMemoryInstructionRecipe Recipe(*Store, &Addr, &StoredV, &Mask, false,
1050+
false);
10501051
EXPECT_TRUE(Recipe.mayHaveSideEffects());
10511052
EXPECT_FALSE(Recipe.mayReadFromMemory());
10521053
EXPECT_TRUE(Recipe.mayWriteToMemory());

0 commit comments

Comments
 (0)