Skip to content

Commit 3b1618e

Browse files
committed
!fixup address latest comments, thanks
1 parent 67a30f1 commit 3b1618e

File tree

8 files changed

+75
-72
lines changed

8 files changed

+75
-72
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9317,9 +9317,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
93179317
VPlanHCFGBuilder HCFGBuilder(OrigLoop, LI, *Plan);
93189318
HCFGBuilder.buildPlainCFG();
93199319

9320-
VPlanTransforms::introduceRegions(*Plan, Legal->getWidestInductionType(), PSE,
9321-
RequiresScalarEpilogueCheck,
9322-
CM.foldTailByMasking(), OrigLoop);
9320+
VPlanTransforms::createLoopRegions(*Plan, Legal->getWidestInductionType(),
9321+
PSE, RequiresScalarEpilogueCheck,
9322+
CM.foldTailByMasking(), OrigLoop);
93239323

93249324
// Don't use getDecisionAndClampRange here, because we don't know the UF
93259325
// so this function is better to be conservative, rather than to split
@@ -9621,8 +9621,8 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
96219621
VPlanHCFGBuilder HCFGBuilder(OrigLoop, LI, *Plan);
96229622
HCFGBuilder.buildPlainCFG();
96239623

9624-
VPlanTransforms::introduceRegions(*Plan, Legal->getWidestInductionType(), PSE,
9625-
true, false, OrigLoop);
9624+
VPlanTransforms::createLoopRegions(*Plan, Legal->getWidestInductionType(),
9625+
PSE, true, false, OrigLoop);
96269626

96279627
for (ElementCount VF : Range)
96289628
Plan->addVF(VF);

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -652,22 +652,6 @@ bool VPBasicBlock::isExiting() const {
652652
return getParent() && getParent()->getExitingBasicBlock() == this;
653653
}
654654

655-
std::optional<std::pair<VPBasicBlock *, VPBasicBlock *>>
656-
VPBasicBlock::isHeader(const VPDominatorTree &VPDT) const {
657-
ArrayRef<VPBlockBase *> Preds = getPredecessors();
658-
if (Preds.size() != 2)
659-
return std::nullopt;
660-
661-
for (unsigned Idx : {0, 1}) {
662-
auto *PreheaderVPBB = cast<VPBasicBlock>(Preds[Idx]);
663-
auto *LatchVPBB = cast<VPBasicBlock>(Preds[1 - Idx]);
664-
if (VPDT.dominates(PreheaderVPBB, this) && VPDT.dominates(this, LatchVPBB))
665-
return {std::make_pair(PreheaderVPBB, LatchVPBB)};
666-
}
667-
668-
return std::nullopt;
669-
}
670-
671655
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
672656
void VPBlockBase::print(raw_ostream &O) const {
673657
VPSlotTracker SlotTracker(getPlan());

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,14 +3255,6 @@ class VPBasicBlock : public VPBlockBase {
32553255
/// Returns true if the block is exiting it's parent region.
32563256
bool isExiting() const;
32573257

3258-
/// Checks if the block is a loop header block in the plain CFG; that is, it
3259-
/// has exactly 2 predecessors (preheader and latch), where the block
3260-
/// dominates the latch and the preheader dominates the block. If it is a
3261-
/// header block, returns a pair with the corresponding preheader and latch
3262-
/// blocks. Otherwise return std::nullopt.
3263-
std::optional<std::pair<VPBasicBlock *, VPBasicBlock *>>
3264-
isHeader(const VPDominatorTree &VPDT) const;
3265-
32663258
/// Clone the current block and it's recipes, without updating the operands of
32673259
/// the cloned recipes.
32683260
VPBasicBlock *clone() override;

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,79 @@
2121

2222
using namespace llvm;
2323

24-
/// Create and return a new VPRegionBlock for loop starting at \p HeaderVPBB and
25-
/// return it.
26-
static VPRegionBlock *introduceRegion(VPlan &Plan, VPBasicBlock *PreheaderVPBB,
27-
VPBasicBlock *HeaderVPBB,
28-
VPBasicBlock *LatchVPBB) {
29-
VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPBB);
30-
VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPBB);
24+
/// Checks if \p HeaderVPB is a loop header block in the plain CFG; that is, it
25+
/// has exactly 2 predecessors (preheader and latch), where the block
26+
/// dominates the latch and the preheader dominates the block. If it is a
27+
/// header block, returns a pair with the corresponding preheader and latch
28+
/// blocks. Otherwise return std::nullopt.
29+
static std::optional<std::pair<VPBasicBlock *, VPBasicBlock *>>
30+
getPreheaderAndLatch(VPBlockBase *HeaderVPB, const VPDominatorTree &VPDT) {
31+
ArrayRef<VPBlockBase *> Preds = HeaderVPB->getPredecessors();
32+
if (Preds.size() != 2)
33+
return std::nullopt;
34+
35+
auto *PreheaderVPBB = cast<VPBasicBlock>(Preds[0]);
36+
auto *LatchVPBB = cast<VPBasicBlock>(Preds[1]);
37+
if (VPDT.dominates(PreheaderVPBB, HeaderVPB) &&
38+
VPDT.dominates(HeaderVPB, LatchVPBB))
39+
return {std::make_pair(PreheaderVPBB, LatchVPBB)};
40+
41+
std::swap(PreheaderVPBB, LatchVPBB);
42+
if (VPDT.dominates(PreheaderVPBB, HeaderVPB) &&
43+
VPDT.dominates(HeaderVPB, LatchVPBB))
44+
return {std::make_pair(PreheaderVPBB, LatchVPBB)};
45+
46+
return std::nullopt;
47+
}
48+
49+
/// Create a new VPRegionBlock if there is a loop starting at \p HeaderVPB.
50+
static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB,
51+
VPDominatorTree &VPDT) {
52+
auto Res = getPreheaderAndLatch(HeaderVPB, VPDT);
53+
if (!Res)
54+
return;
55+
56+
const auto &[PreheaderVPBB, LatchVPBB] = *Res;
57+
58+
// Swap the operands of header phis if needed. After creating the region, the
59+
// incoming value from the preheader must be the first operand and the one
60+
// from the latch must be the second operand.
61+
if (HeaderVPB->getPredecessors()[0] != PreheaderVPBB) {
62+
for (VPRecipeBase &R : cast<VPBasicBlock>(HeaderVPB)->phis()) {
63+
VPValue *Inc0 = R.getOperand(0);
64+
R.setOperand(0, R.getOperand(1));
65+
R.setOperand(1, Inc0);
66+
}
67+
}
68+
VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPB);
69+
VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPB);
3170
VPBlockBase *Succ = LatchVPBB->getSingleSuccessor();
3271
assert(LatchVPBB->getNumSuccessors() <= 1 &&
3372
"Latch has more than one successor");
3473
if (Succ)
3574
VPBlockUtils::disconnectBlocks(LatchVPBB, Succ);
3675

37-
auto *R = Plan.createVPRegionBlock(HeaderVPBB, LatchVPBB, "",
76+
auto *R = Plan.createVPRegionBlock(HeaderVPB, LatchVPBB, "",
3877
false /*isReplicator*/);
39-
R->setParent(HeaderVPBB->getParent());
40-
// All VPBB's reachable shallowly from HeaderVPBB belong to top level loop,
78+
R->setParent(HeaderVPB->getParent());
79+
// All VPBB's reachable shallowly from HeaderVPB belong to top level loop,
4180
// because VPlan is expected to end at top level latch disconnected above.
42-
for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPBB))
81+
for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPB))
4382
VPBB->setParent(R);
4483

4584
VPBlockUtils::insertBlockAfter(R, PreheaderVPBB);
4685
if (Succ)
4786
VPBlockUtils::connectBlocks(R, Succ);
48-
return R;
4987
}
5088

51-
void VPlanTransforms::introduceRegions(VPlan &Plan, Type *InductionTy,
52-
PredicatedScalarEvolution &PSE,
53-
bool RequiresScalarEpilogueCheck,
54-
bool TailFolded, Loop *TheLoop) {
89+
void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
90+
PredicatedScalarEvolution &PSE,
91+
bool RequiresScalarEpilogueCheck,
92+
bool TailFolded, Loop *TheLoop) {
5593
VPDominatorTree VPDT;
5694
VPDT.recalculate(Plan);
57-
for (VPBasicBlock *HeaderVPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
58-
vp_depth_first_shallow(Plan.getEntry()))) {
59-
auto Res = HeaderVPBB->isHeader(VPDT);
60-
if (!Res)
61-
continue;
62-
const auto &[PreheaderVPBB, LatchVPBB] = *Res;
63-
introduceRegion(Plan, PreheaderVPBB, HeaderVPBB, LatchVPBB);
64-
}
95+
for (VPBlockBase *HeaderVPB : vp_depth_first_shallow(Plan.getEntry()))
96+
createLoopRegion(Plan, HeaderVPB, VPDT);
6597

6698
VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
6799
auto *OrigExiting = TopRegion->getExiting();

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,14 @@ void PlainCFGBuilder::fixHeaderPhis() {
9898
auto *VPPhi = cast<VPWidenPHIRecipe>(VPVal);
9999
assert(VPPhi->getNumOperands() == 0 &&
100100
"Expected VPInstruction with no operands.");
101-
102-
Loop *L = LI->getLoopFor(Phi->getParent());
103-
assert(isHeaderBB(Phi->getParent(), L));
101+
assert(isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent())));
104102
// For header phis, make sure the incoming value from the loop
105103
// predecessor is the first operand of the recipe.
106104
assert(Phi->getNumOperands() == 2 &&
107105
"header phi must have exactly 2 operands");
108-
BasicBlock *LoopPred = L->getLoopPredecessor();
109-
VPPhi->addOperand(
110-
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopPred)));
111-
BasicBlock *LoopLatch = L->getLoopLatch();
112-
VPPhi->addOperand(
113-
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopLatch)));
106+
for (BasicBlock *Pred : predecessors(Phi->getParent()))
107+
VPPhi->addOperand(
108+
getOrCreateVPOperand(Phi->getIncomingValueForBlock(Pred)));
114109
}
115110
}
116111

@@ -344,8 +339,8 @@ void PlainCFGBuilder::buildPlainCFG(
344339
VPBasicBlock *Successor0 = getOrCreateVPBB(IRSucc0);
345340
VPBasicBlock *Successor1 = getOrCreateVPBB(IRSucc1);
346341

347-
// Don't connect any blocks outside the current loop except the latch, which
348-
// is handled below.
342+
// Don't connect any blocks outside the current loop except the latches for
343+
// inner loops.
349344
if (LoopForBB &&
350345
(LoopForBB == TheLoop || BB != LoopForBB->getLoopLatch())) {
351346
if (!LoopForBB->contains(IRSucc0)) {

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ struct VPlanTransforms {
6161
/// VPBasicBlocks for the scalar preheader and exit blocks. \p InductionTy is
6262
/// the type of the canonical induction and used for related values, like the
6363
/// trip count expression.
64-
static void introduceRegions(VPlan &Plan, Type *InductionTy,
65-
PredicatedScalarEvolution &PSE,
66-
bool RequiresScalarEpilogueCheck,
67-
bool TailFolded, Loop *TheLoop);
64+
static void createLoopRegions(VPlan &Plan, Type *InductionTy,
65+
PredicatedScalarEvolution &PSE,
66+
bool RequiresScalarEpilogueCheck,
67+
bool TailFolded, Loop *TheLoop);
6868

6969
/// Replaces the VPInstructions in \p Plan with corresponding
7070
/// widen recipes. Returns false if any VPInstructions could not be converted

llvm/test/Transforms/LoopVectorize/vplan-printing-outer-loop.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ define void @foo(i64 %n) {
1313
; CHECK-NEXT: Successor(s): vector.body
1414
; CHECK-EMPTY:
1515
; CHECK-NEXT: vector.body:
16-
; CHECK-NEXT: WIDEN-PHI ir<%outer.iv> = phi ir<0>, ir<%outer.iv.next>
16+
; CHECK-NEXT: WIDEN-PHI ir<%outer.iv> = phi ir<%outer.iv.next>, ir<0>
1717
; CHECK-NEXT: EMIT ir<%gep.1> = getelementptr ir<@arr2>, ir<0>, ir<%outer.iv>
1818
; CHECK-NEXT: EMIT store ir<%outer.iv>, ir<%gep.1>
1919
; CHECK-NEXT: EMIT ir<%add> = add ir<%outer.iv>, ir<%n>
2020
; CHECK-NEXT: Successor(s): inner
2121
; CHECK-EMPTY:
2222
; CHECK-NEXT: inner:
23-
; CHECK-NEXT: WIDEN-PHI ir<%inner.iv> = phi ir<0>, ir<%inner.iv.next>
23+
; CHECK-NEXT: WIDEN-PHI ir<%inner.iv> = phi ir<%inner.iv.next>, ir<0>
2424
; CHECK-NEXT: EMIT ir<%gep.2> = getelementptr ir<@arr>, ir<0>, ir<%inner.iv>, ir<%outer.iv>
2525
; CHECK-NEXT: EMIT store ir<%add>, ir<%gep.2>
2626
; CHECK-NEXT: EMIT ir<%inner.iv.next> = add ir<%inner.iv>, ir<1>

llvm/unittests/Transforms/Vectorize/VPlanTestBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class VPlanTestIRBase : public testing::Test {
7474
auto Plan = std::make_unique<VPlan>(L);
7575
VPlanHCFGBuilder HCFGBuilder(L, LI.get(), *Plan);
7676
HCFGBuilder.buildPlainCFG();
77-
VPlanTransforms::introduceRegions(*Plan, IntegerType::get(*Ctx, 64), PSE,
78-
true, false, L);
77+
VPlanTransforms::createLoopRegions(*Plan, IntegerType::get(*Ctx, 64), PSE,
78+
true, false, L);
7979
return Plan;
8080
}
8181
};

0 commit comments

Comments
 (0)