Skip to content

Commit 4b9cc62

Browse files
committed
[VPlan] Thread DL, use TargetFolder
1 parent 9982bb2 commit 4b9cc62

File tree

8 files changed

+97
-68
lines changed

8 files changed

+97
-68
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ class VPBuilder {
7676
}
7777

7878
public:
79-
VPBuilder() = default;
80-
VPBuilder(VPBasicBlock *InsertBB) { setInsertPoint(InsertBB); }
81-
VPBuilder(VPRecipeBase *InsertPt) { setInsertPoint(InsertPt); }
82-
VPBuilder(VPBasicBlock *TheBB, VPBasicBlock::iterator IP) {
79+
VPBuilder(const DataLayout &DL) : Folder(DL) {}
80+
VPBuilder(const DataLayout &DL, VPBasicBlock *InsertBB) : Folder(DL) {
81+
setInsertPoint(InsertBB);
82+
}
83+
VPBuilder(const DataLayout &DL, VPRecipeBase *InsertPt) : Folder(DL) {
84+
setInsertPoint(InsertPt);
85+
}
86+
VPBuilder(const DataLayout &DL, VPBasicBlock *TheBB,
87+
VPBasicBlock::iterator IP)
88+
: Folder(DL) {
8389
setInsertPoint(TheBB, IP);
8490
}
8591

@@ -94,8 +100,8 @@ class VPBuilder {
94100
VPBasicBlock::iterator getInsertPoint() const { return InsertPt; }
95101

96102
/// Create a VPBuilder to insert after \p R.
97-
static VPBuilder getToInsertAfter(VPRecipeBase *R) {
98-
VPBuilder B;
103+
static VPBuilder getToInsertAfter(const DataLayout &DL, VPRecipeBase *R) {
104+
VPBuilder B(DL);
99105
B.setInsertPoint(R->getParent(), std::next(R->getIterator()));
100106
return B;
101107
}
@@ -457,9 +463,9 @@ class LoopVectorizationPlanner {
457463
const TargetTransformInfo &TTI, LoopVectorizationLegality *Legal,
458464
LoopVectorizationCostModel &CM, InterleavedAccessInfo &IAI,
459465
PredicatedScalarEvolution &PSE, const LoopVectorizeHints &Hints,
460-
OptimizationRemarkEmitter *ORE)
466+
OptimizationRemarkEmitter *ORE, const DataLayout &DL)
461467
: OrigLoop(L), LI(LI), DT(DT), TLI(TLI), TTI(TTI), Legal(Legal), CM(CM),
462-
IAI(IAI), PSE(PSE), Hints(Hints), ORE(ORE) {}
468+
IAI(IAI), PSE(PSE), Hints(Hints), ORE(ORE), Builder(DL) {}
463469

464470
/// Build VPlans for the specified \p UserVF and \p UserIC if they are
465471
/// non-zero or all applicable candidate VFs otherwise. If vectorization and

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7673,7 +7673,8 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
76737673
// cost model is complete for better cost estimates.
76747674
VPlanTransforms::runPass(VPlanTransforms::unrollByUF, BestVPlan, BestUF,
76757675
OrigLoop->getHeader()->getContext());
7676-
VPlanTransforms::materializeBroadcasts(BestVPlan);
7676+
VPlanTransforms::materializeBroadcasts(
7677+
BestVPlan, OrigLoop->getHeader()->getDataLayout());
76777678
VPlanTransforms::optimizeForVFAndUF(BestVPlan, BestVF, BestUF, PSE);
76787679
VPlanTransforms::simplifyRecipes(BestVPlan, *Legal->getWidestInductionType());
76797680
VPlanTransforms::narrowInterleaveGroups(
@@ -8966,7 +8967,8 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
89668967
// Discard the plan if it is not EVL-compatible
89678968
if (CM.foldTailWithEVL() && !HasScalarVF &&
89688969
!VPlanTransforms::runPass(VPlanTransforms::tryAddExplicitVectorLength,
8969-
*Plan, CM.getMaxSafeElements()))
8970+
*Plan, CM.getMaxSafeElements(),
8971+
OrigLoop->getHeader()->getDataLayout()))
89708972
break;
89718973
assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
89728974
VPlans.push_back(std::move(Plan));
@@ -8978,7 +8980,7 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
89788980
// Add the necessary canonical IV and branch recipes required to control the
89798981
// loop.
89808982
static void addCanonicalIVRecipes(VPlan &Plan, Type *IdxTy, bool HasNUW,
8981-
DebugLoc DL) {
8983+
DebugLoc DL, const DataLayout &Layout) {
89828984
Value *StartIdx = ConstantInt::get(IdxTy, 0);
89838985
auto *StartV = Plan.getOrAddLiveIn(StartIdx);
89848986

@@ -8988,7 +8990,7 @@ static void addCanonicalIVRecipes(VPlan &Plan, Type *IdxTy, bool HasNUW,
89888990
VPBasicBlock *Header = TopRegion->getEntryBasicBlock();
89898991
Header->insert(CanonicalIVPHI, Header->begin());
89908992

8991-
VPBuilder Builder(TopRegion->getExitingBasicBlock());
8993+
VPBuilder Builder(Layout, TopRegion->getExitingBasicBlock());
89928994
// Add a VPInstruction to increment the scalar canonical IV by VF * UF.
89938995
auto *CanonicalIVIncrement = Builder.createOverflowingOp(
89948996
Instruction::Add, {CanonicalIVPHI, &Plan.getVFxUF()}, {HasNUW, false}, DL,
@@ -9042,15 +9044,16 @@ static VPInstruction *addResumePhiRecipeForInduction(
90429044
/// original phis in the scalar header. End values for inductions are added to
90439045
/// \p IVEndValues.
90449046
static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan,
9045-
DenseMap<VPValue *, VPValue *> &IVEndValues) {
9047+
DenseMap<VPValue *, VPValue *> &IVEndValues,
9048+
const DataLayout &DL) {
90469049
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType());
90479050
auto *ScalarPH = Plan.getScalarPreheader();
90489051
auto *MiddleVPBB = cast<VPBasicBlock>(ScalarPH->getSinglePredecessor());
90499052
VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
90509053
VPBuilder VectorPHBuilder(
9051-
cast<VPBasicBlock>(VectorRegion->getSinglePredecessor()));
9052-
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9053-
VPBuilder ScalarPHBuilder(ScalarPH);
9054+
DL, cast<VPBasicBlock>(VectorRegion->getSinglePredecessor()));
9055+
VPBuilder MiddleBuilder(DL, MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9056+
VPBuilder ScalarPHBuilder(DL, ScalarPH);
90549057
VPValue *OneVPV = Plan.getOrAddLiveIn(
90559058
ConstantInt::get(Plan.getCanonicalIV()->getScalarType(), 1));
90569059
for (VPRecipeBase &ScalarPhiR : *Plan.getScalarHeader()) {
@@ -9141,12 +9144,13 @@ collectUsersInExitBlocks(Loop *OrigLoop, VPRecipeBuilder &Builder,
91419144
// ExitUsersToFix if needed and their operands are updated.
91429145
static void
91439146
addUsersInExitBlocks(VPlan &Plan,
9144-
const SetVector<VPIRInstruction *> &ExitUsersToFix) {
9147+
const SetVector<VPIRInstruction *> &ExitUsersToFix,
9148+
const DataLayout &DL) {
91459149
if (ExitUsersToFix.empty())
91469150
return;
91479151

91489152
auto *MiddleVPBB = Plan.getMiddleBlock();
9149-
VPBuilder B(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9153+
VPBuilder B(DL, MiddleVPBB, MiddleVPBB->getFirstNonPhi());
91509154

91519155
// Introduce extract for exiting values and update the VPIRInstructions
91529156
// modeling the corresponding LCSSA phis.
@@ -9164,12 +9168,13 @@ addUsersInExitBlocks(VPlan &Plan,
91649168
/// users in the original exit block using the VPIRInstruction wrapping to the
91659169
/// LCSSA phi.
91669170
static void addExitUsersForFirstOrderRecurrences(
9167-
VPlan &Plan, SetVector<VPIRInstruction *> &ExitUsersToFix) {
9171+
VPlan &Plan, SetVector<VPIRInstruction *> &ExitUsersToFix,
9172+
const DataLayout &DL) {
91689173
VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
91699174
auto *ScalarPHVPBB = Plan.getScalarPreheader();
91709175
auto *MiddleVPBB = Plan.getMiddleBlock();
9171-
VPBuilder ScalarPHBuilder(ScalarPHVPBB);
9172-
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9176+
VPBuilder ScalarPHBuilder(DL, ScalarPHVPBB);
9177+
VPBuilder MiddleBuilder(DL, MiddleVPBB, MiddleVPBB->getFirstNonPhi());
91739178
VPValue *TwoVPV = Plan.getOrAddLiveIn(
91749179
ConstantInt::get(Plan.getCanonicalIV()->getScalarType(), 2));
91759180

@@ -9312,7 +9317,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
93129317
// that the canonical induction increment will not overflow as the vector trip
93139318
// count is >= increment and a multiple of the increment.
93149319
bool HasNUW = !IVUpdateMayOverflow || Style == TailFoldingStyle::None;
9315-
addCanonicalIVRecipes(*Plan, Legal->getWidestInductionType(), HasNUW, DL);
9320+
const DataLayout &Layout = OrigLoop->getHeader()->getDataLayout();
9321+
addCanonicalIVRecipes(*Plan, Legal->getWidestInductionType(), HasNUW, DL,
9322+
Layout);
93169323

93179324
VPRecipeBuilder RecipeBuilder(*Plan, OrigLoop, TLI, &TTI, Legal, CM, PSE,
93189325
Builder);
@@ -9499,11 +9506,11 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
94999506
RecipeBuilder);
95009507
}
95019508
DenseMap<VPValue *, VPValue *> IVEndValues;
9502-
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues);
9509+
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues, Layout);
95039510
SetVector<VPIRInstruction *> ExitUsersToFix =
95049511
collectUsersInExitBlocks(OrigLoop, RecipeBuilder, *Plan);
9505-
addExitUsersForFirstOrderRecurrences(*Plan, ExitUsersToFix);
9506-
addUsersInExitBlocks(*Plan, ExitUsersToFix);
9512+
addExitUsersForFirstOrderRecurrences(*Plan, ExitUsersToFix, Layout);
9513+
addUsersInExitBlocks(*Plan, ExitUsersToFix, Layout);
95079514

95089515
// ---------------------------------------------------------------------------
95099516
// Transform initial VPlan: Apply previously taken decisions, in order, to
@@ -9575,7 +9582,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
95759582
bool WithoutRuntimeCheck =
95769583
Style == TailFoldingStyle::DataAndControlFlowWithoutRuntimeCheck;
95779584
VPlanTransforms::addActiveLaneMask(*Plan, ForControlFlow,
9578-
WithoutRuntimeCheck);
9585+
WithoutRuntimeCheck, Layout);
95799586
}
95809587
VPlanTransforms::optimizeInductionExitUsers(*Plan, IVEndValues);
95819588

@@ -9614,8 +9621,9 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
96149621
// Tail folding is not supported for outer loops, so the induction increment
96159622
// is guaranteed to not wrap.
96169623
bool HasNUW = true;
9624+
const DataLayout &DL = OrigLoop->getHeader()->getDataLayout();
96179625
addCanonicalIVRecipes(*Plan, Legal->getWidestInductionType(), HasNUW,
9618-
DebugLoc());
9626+
DebugLoc(), DL);
96199627

96209628
// Collect mapping of IR header phis to header phi recipes, to be used in
96219629
// addScalarResumePhis.
@@ -9630,7 +9638,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
96309638
DenseMap<VPValue *, VPValue *> IVEndValues;
96319639
// TODO: IVEndValues are not used yet in the native path, to optimize exit
96329640
// values.
9633-
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues);
9641+
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues, DL);
96349642

96359643
assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
96369644
return Plan;
@@ -10070,7 +10078,7 @@ static bool processLoopInVPlanNativePath(
1007010078
// TODO: CM is not used at this point inside the planner. Turn CM into an
1007110079
// optional argument if we don't need it in the future.
1007210080
LoopVectorizationPlanner LVP(L, LI, DT, TLI, *TTI, LVL, CM, IAI, PSE, Hints,
10073-
ORE);
10081+
ORE, F->getDataLayout());
1007410082

1007510083
// Get user vectorization factor.
1007610084
ElementCount UserVF = Hints.getWidth();
@@ -10298,7 +10306,8 @@ LoopVectorizePass::LoopVectorizePass(LoopVectorizeOptions Opts)
1029810306
/// Prepare \p MainPlan for vectorizing the main vector loop during epilogue
1029910307
/// vectorization. Remove ResumePhis from \p MainPlan for inductions that
1030010308
/// don't have a corresponding wide induction in \p EpiPlan.
10301-
static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan) {
10309+
static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan,
10310+
const DataLayout &DL) {
1030210311
// Collect PHI nodes of widened phis in the VPlan for the epilogue. Those
1030310312
// will need their resume-values computed in the main vector loop. Others
1030410313
// can be removed from the main VPlan.
@@ -10338,7 +10347,7 @@ static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan) {
1033810347
m_Specific(VectorTC), m_SpecificInt(0)));
1033910348
}))
1034010349
return;
10341-
VPBuilder ScalarPHBuilder(MainScalarPH, MainScalarPH->begin());
10350+
VPBuilder ScalarPHBuilder(DL, MainScalarPH, MainScalarPH->begin());
1034210351
ScalarPHBuilder.createNaryOp(
1034310352
VPInstruction::ResumePhi,
1034410353
{VectorTC, MainPlan.getCanonicalIV()->getStartValue()}, {},
@@ -10671,7 +10680,7 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1067110680
F, &Hints, IAI, PSI, BFI);
1067210681
// Use the planner for vectorization.
1067310682
LoopVectorizationPlanner LVP(L, LI, DT, TLI, *TTI, &LVL, CM, IAI, PSE, Hints,
10674-
ORE);
10683+
ORE, F->getDataLayout());
1067510684

1067610685
// Get user vectorization factor and interleave count.
1067710686
ElementCount UserVF = Hints.getWidth();
@@ -10854,7 +10863,8 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1085410863
// factor) again shortly afterwards.
1085510864
VPlan &BestEpiPlan = LVP.getPlanFor(EpilogueVF.Width);
1085610865
BestEpiPlan.getMiddleBlock()->setName("vec.epilog.middle.block");
10857-
preparePlanForMainVectorLoop(*BestMainPlan, BestEpiPlan);
10866+
preparePlanForMainVectorLoop(*BestMainPlan, BestEpiPlan,
10867+
F->getDataLayout());
1085810868
EpilogueLoopVectorizationInfo EPI(VF.Width, IC, EpilogueVF.Width, 1,
1085910869
BestEpiPlan);
1086010870
EpilogueVectorizerMainLoop MainILV(L, PSE, LI, DT, TLI, TTI, AC, ORE,

llvm/lib/Transforms/Vectorize/VPlanConstantFolder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "VPlanValue.h"
10-
#include "llvm/IR/ConstantFolder.h"
10+
#include "llvm/Analysis/TargetFolder.h"
1111

1212
#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLANCONSTANTFOLDER_H
1313
#define LLVM_TRANSFORMS_VECTORIZE_VPLANCONSTANTFOLDER_H
1414

1515
namespace llvm {
1616
class VPConstantFolder {
1717
private:
18-
ConstantFolder Folder;
18+
TargetFolder Folder;
1919

2020
Constant *getIRConstant(VPValue *V) const {
2121
if (!V->isLiveIn())
@@ -33,6 +33,8 @@ class VPConstantFolder {
3333
}
3434

3535
public:
36+
VPConstantFolder(const DataLayout &DL) : Folder(DL) {}
37+
3638
Value *foldAnd(VPValue *LHS, VPValue *RHS) const {
3739
return foldBinOp(Instruction::BinaryOps::And, LHS, RHS);
3840
}

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void VPlanTransforms::introduceTopLevelVectorLoopRegion(
8888
// of the corresponding compare because they may have ended up with
8989
// different line numbers and we want to avoid awkward line stepping while
9090
// debugging. Eg. if the compare has got a line number inside the loop.
91-
VPBuilder Builder(MiddleVPBB);
91+
VPBuilder Builder(TheLoop->getHeader()->getDataLayout(), MiddleVPBB);
9292
VPValue *Cmp =
9393
TailFolded
9494
? Plan.getOrAddLiveIn(ConstantInt::getTrue(

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class PlainCFGBuilder {
7373

7474
public:
7575
PlainCFGBuilder(Loop *Lp, LoopInfo *LI, VPlan &P)
76-
: TheLoop(Lp), LI(LI), Plan(P) {}
76+
: TheLoop(Lp), LI(LI), Plan(P),
77+
VPIRBuilder(Lp->getHeader()->getDataLayout()) {}
7778

7879
/// Build plain CFG for TheLoop and connects it to Plan's entry.
7980
void buildPlainCFG(DenseMap<VPBlockBase *, BasicBlock *> &VPB2IRBB);

0 commit comments

Comments
 (0)