Skip to content

Commit 0b7d703

Browse files
committed
[VPlan] Thread DL, use TargetFolder
1 parent a86509f commit 0b7d703

33 files changed

+371
-309
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
}
@@ -458,9 +464,9 @@ class LoopVectorizationPlanner {
458464
const TargetTransformInfo &TTI, LoopVectorizationLegality *Legal,
459465
LoopVectorizationCostModel &CM, InterleavedAccessInfo &IAI,
460466
PredicatedScalarEvolution &PSE, const LoopVectorizeHints &Hints,
461-
OptimizationRemarkEmitter *ORE)
467+
OptimizationRemarkEmitter *ORE, const DataLayout &DL)
462468
: OrigLoop(L), LI(LI), DT(DT), TLI(TLI), TTI(TTI), Legal(Legal), CM(CM),
463-
IAI(IAI), PSE(PSE), Hints(Hints), ORE(ORE) {}
469+
IAI(IAI), PSE(PSE), Hints(Hints), ORE(ORE), Builder(DL) {}
464470

465471
/// Build VPlans for the specified \p UserVF and \p UserIC if they are
466472
/// 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
@@ -7698,7 +7698,8 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
76987698
// cost model is complete for better cost estimates.
76997699
VPlanTransforms::runPass(VPlanTransforms::unrollByUF, BestVPlan, BestUF,
77007700
OrigLoop->getHeader()->getContext());
7701-
VPlanTransforms::materializeBroadcasts(BestVPlan);
7701+
VPlanTransforms::materializeBroadcasts(
7702+
BestVPlan, OrigLoop->getHeader()->getDataLayout());
77027703
VPlanTransforms::optimizeForVFAndUF(BestVPlan, BestVF, BestUF, PSE);
77037704
VPlanTransforms::simplifyRecipes(BestVPlan, *Legal->getWidestInductionType());
77047705
VPlanTransforms::narrowInterleaveGroups(
@@ -8991,7 +8992,8 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
89918992
// Discard the plan if it is not EVL-compatible
89928993
if (CM.foldTailWithEVL() && !HasScalarVF &&
89938994
!VPlanTransforms::runPass(VPlanTransforms::tryAddExplicitVectorLength,
8994-
*Plan, CM.getMaxSafeElements()))
8995+
*Plan, CM.getMaxSafeElements(),
8996+
OrigLoop->getHeader()->getDataLayout()))
89958997
break;
89968998
assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
89978999
VPlans.push_back(std::move(Plan));
@@ -9003,7 +9005,7 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
90039005
// Add the necessary canonical IV and branch recipes required to control the
90049006
// loop.
90059007
static void addCanonicalIVRecipes(VPlan &Plan, Type *IdxTy, bool HasNUW,
9006-
DebugLoc DL) {
9008+
DebugLoc DL, const DataLayout &Layout) {
90079009
Value *StartIdx = ConstantInt::get(IdxTy, 0);
90089010
auto *StartV = Plan.getOrAddLiveIn(StartIdx);
90099011

@@ -9013,7 +9015,7 @@ static void addCanonicalIVRecipes(VPlan &Plan, Type *IdxTy, bool HasNUW,
90139015
VPBasicBlock *Header = TopRegion->getEntryBasicBlock();
90149016
Header->insert(CanonicalIVPHI, Header->begin());
90159017

9016-
VPBuilder Builder(TopRegion->getExitingBasicBlock());
9018+
VPBuilder Builder(Layout, TopRegion->getExitingBasicBlock());
90179019
// Add a VPInstruction to increment the scalar canonical IV by VF * UF.
90189020
auto *CanonicalIVIncrement = Builder.createOverflowingOp(
90199021
Instruction::Add, {CanonicalIVPHI, &Plan.getVFxUF()}, {HasNUW, false}, DL,
@@ -9067,15 +9069,16 @@ static VPInstruction *addResumePhiRecipeForInduction(
90679069
/// original phis in the scalar header. End values for inductions are added to
90689070
/// \p IVEndValues.
90699071
static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan,
9070-
DenseMap<VPValue *, VPValue *> &IVEndValues) {
9072+
DenseMap<VPValue *, VPValue *> &IVEndValues,
9073+
const DataLayout &DL) {
90719074
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType());
90729075
auto *ScalarPH = Plan.getScalarPreheader();
90739076
auto *MiddleVPBB = cast<VPBasicBlock>(ScalarPH->getSinglePredecessor());
90749077
VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
90759078
VPBuilder VectorPHBuilder(
9076-
cast<VPBasicBlock>(VectorRegion->getSinglePredecessor()));
9077-
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9078-
VPBuilder ScalarPHBuilder(ScalarPH);
9079+
DL, cast<VPBasicBlock>(VectorRegion->getSinglePredecessor()));
9080+
VPBuilder MiddleBuilder(DL, MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9081+
VPBuilder ScalarPHBuilder(DL, ScalarPH);
90799082
VPValue *OneVPV = Plan.getOrAddLiveIn(
90809083
ConstantInt::get(Plan.getCanonicalIV()->getScalarType(), 1));
90819084
for (VPRecipeBase &ScalarPhiR : *Plan.getScalarHeader()) {
@@ -9165,12 +9168,13 @@ collectUsersInExitBlocks(Loop *OrigLoop, VPRecipeBuilder &Builder,
91659168
// ExitUsersToFix if needed and their operands are updated.
91669169
static void
91679170
addUsersInExitBlocks(VPlan &Plan,
9168-
const SetVector<VPIRInstruction *> &ExitUsersToFix) {
9171+
const SetVector<VPIRInstruction *> &ExitUsersToFix,
9172+
const DataLayout &DL) {
91699173
if (ExitUsersToFix.empty())
91709174
return;
91719175

91729176
auto *MiddleVPBB = Plan.getMiddleBlock();
9173-
VPBuilder B(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9177+
VPBuilder B(DL, MiddleVPBB, MiddleVPBB->getFirstNonPhi());
91749178

91759179
// Introduce extract for exiting values and update the VPIRInstructions
91769180
// modeling the corresponding LCSSA phis.
@@ -9188,12 +9192,13 @@ addUsersInExitBlocks(VPlan &Plan,
91889192
/// users in the original exit block using the VPIRInstruction wrapping to the
91899193
/// LCSSA phi.
91909194
static void addExitUsersForFirstOrderRecurrences(
9191-
VPlan &Plan, SetVector<VPIRInstruction *> &ExitUsersToFix) {
9195+
VPlan &Plan, SetVector<VPIRInstruction *> &ExitUsersToFix,
9196+
const DataLayout &DL) {
91929197
VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
91939198
auto *ScalarPHVPBB = Plan.getScalarPreheader();
91949199
auto *MiddleVPBB = Plan.getMiddleBlock();
9195-
VPBuilder ScalarPHBuilder(ScalarPHVPBB);
9196-
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9200+
VPBuilder ScalarPHBuilder(DL, ScalarPHVPBB);
9201+
VPBuilder MiddleBuilder(DL, MiddleVPBB, MiddleVPBB->getFirstNonPhi());
91979202
VPValue *TwoVPV = Plan.getOrAddLiveIn(
91989203
ConstantInt::get(Plan.getCanonicalIV()->getScalarType(), 2));
91999204

@@ -9336,7 +9341,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
93369341
// that the canonical induction increment will not overflow as the vector trip
93379342
// count is >= increment and a multiple of the increment.
93389343
bool HasNUW = !IVUpdateMayOverflow || Style == TailFoldingStyle::None;
9339-
addCanonicalIVRecipes(*Plan, Legal->getWidestInductionType(), HasNUW, DL);
9344+
const DataLayout &Layout = OrigLoop->getHeader()->getDataLayout();
9345+
addCanonicalIVRecipes(*Plan, Legal->getWidestInductionType(), HasNUW, DL,
9346+
Layout);
93409347

93419348
VPRecipeBuilder RecipeBuilder(*Plan, OrigLoop, TLI, &TTI, Legal, CM, PSE,
93429349
Builder);
@@ -9523,11 +9530,11 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
95239530
RecipeBuilder);
95249531
}
95259532
DenseMap<VPValue *, VPValue *> IVEndValues;
9526-
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues);
9533+
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues, Layout);
95279534
SetVector<VPIRInstruction *> ExitUsersToFix =
95289535
collectUsersInExitBlocks(OrigLoop, RecipeBuilder, *Plan);
9529-
addExitUsersForFirstOrderRecurrences(*Plan, ExitUsersToFix);
9530-
addUsersInExitBlocks(*Plan, ExitUsersToFix);
9536+
addExitUsersForFirstOrderRecurrences(*Plan, ExitUsersToFix, Layout);
9537+
addUsersInExitBlocks(*Plan, ExitUsersToFix, Layout);
95319538

95329539
// ---------------------------------------------------------------------------
95339540
// Transform initial VPlan: Apply previously taken decisions, in order, to
@@ -9599,7 +9606,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
95999606
bool WithoutRuntimeCheck =
96009607
Style == TailFoldingStyle::DataAndControlFlowWithoutRuntimeCheck;
96019608
VPlanTransforms::addActiveLaneMask(*Plan, ForControlFlow,
9602-
WithoutRuntimeCheck);
9609+
WithoutRuntimeCheck, Layout);
96039610
}
96049611
VPlanTransforms::optimizeInductionExitUsers(*Plan, IVEndValues);
96059612

@@ -9638,8 +9645,9 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
96389645
// Tail folding is not supported for outer loops, so the induction increment
96399646
// is guaranteed to not wrap.
96409647
bool HasNUW = true;
9648+
const DataLayout &DL = OrigLoop->getHeader()->getDataLayout();
96419649
addCanonicalIVRecipes(*Plan, Legal->getWidestInductionType(), HasNUW,
9642-
DebugLoc());
9650+
DebugLoc(), DL);
96439651

96449652
// Collect mapping of IR header phis to header phi recipes, to be used in
96459653
// addScalarResumePhis.
@@ -9654,7 +9662,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
96549662
DenseMap<VPValue *, VPValue *> IVEndValues;
96559663
// TODO: IVEndValues are not used yet in the native path, to optimize exit
96569664
// values.
9657-
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues);
9665+
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues, DL);
96589666

96599667
assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
96609668
return Plan;
@@ -10105,7 +10113,7 @@ static bool processLoopInVPlanNativePath(
1010510113
// TODO: CM is not used at this point inside the planner. Turn CM into an
1010610114
// optional argument if we don't need it in the future.
1010710115
LoopVectorizationPlanner LVP(L, LI, DT, TLI, *TTI, LVL, CM, IAI, PSE, Hints,
10108-
ORE);
10116+
ORE, F->getDataLayout());
1010910117

1011010118
// Get user vectorization factor.
1011110119
ElementCount UserVF = Hints.getWidth();
@@ -10333,7 +10341,8 @@ LoopVectorizePass::LoopVectorizePass(LoopVectorizeOptions Opts)
1033310341
/// Prepare \p MainPlan for vectorizing the main vector loop during epilogue
1033410342
/// vectorization. Remove ResumePhis from \p MainPlan for inductions that
1033510343
/// don't have a corresponding wide induction in \p EpiPlan.
10336-
static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan) {
10344+
static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan,
10345+
const DataLayout &DL) {
1033710346
// Collect PHI nodes of widened phis in the VPlan for the epilogue. Those
1033810347
// will need their resume-values computed in the main vector loop. Others
1033910348
// can be removed from the main VPlan.
@@ -10372,7 +10381,7 @@ static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan) {
1037210381
m_Specific(VectorTC), m_SpecificInt(0)));
1037310382
}))
1037410383
return;
10375-
VPBuilder ScalarPHBuilder(MainScalarPH, MainScalarPH->begin());
10384+
VPBuilder ScalarPHBuilder(DL, MainScalarPH, MainScalarPH->begin());
1037610385
ScalarPHBuilder.createNaryOp(
1037710386
VPInstruction::ResumePhi,
1037810387
{VectorTC, MainPlan.getCanonicalIV()->getStartValue()}, {},
@@ -10694,7 +10703,7 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1069410703
F, &Hints, IAI, PSI, BFI);
1069510704
// Use the planner for vectorization.
1069610705
LoopVectorizationPlanner LVP(L, LI, DT, TLI, *TTI, &LVL, CM, IAI, PSE, Hints,
10697-
ORE);
10706+
ORE, F->getDataLayout());
1069810707

1069910708
// Get user vectorization factor and interleave count.
1070010709
ElementCount UserVF = Hints.getWidth();
@@ -10877,7 +10886,8 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1087710886
// factor) again shortly afterwards.
1087810887
VPlan &BestEpiPlan = LVP.getPlanFor(EpilogueVF.Width);
1087910888
BestEpiPlan.getMiddleBlock()->setName("vec.epilog.middle.block");
10880-
preparePlanForMainVectorLoop(*BestMainPlan, BestEpiPlan);
10889+
preparePlanForMainVectorLoop(*BestMainPlan, BestEpiPlan,
10890+
F->getDataLayout());
1088110891
EpilogueLoopVectorizationInfo EPI(VF.Width, IC, EpilogueVF.Width, 1,
1088210892
BestEpiPlan);
1088310893
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)