Skip to content

Commit 5652db7

Browse files
committed
[VPlan] Add exit opernds early.
1 parent fd4bcc7 commit 5652db7

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -9167,11 +9167,7 @@ collectUsersInExitBlocks(Loop *OrigLoop, VPRecipeBuilder &Builder,
91679167
continue;
91689168
}
91699169

9170-
PHINode &ExitPhi = ExitIRI->getIRPhi();
9171-
BasicBlock *ExitingBB = OrigLoop->getLoopLatch();
9172-
Value *IncomingValue = ExitPhi.getIncomingValueForBlock(ExitingBB);
9173-
VPValue *V = Builder.getVPValueOrAddLiveIn(IncomingValue);
9174-
ExitIRI->addOperand(V);
9170+
VPValue *V = ExitIRI->getOperand(0);
91759171
if (V->isLiveIn())
91769172
continue;
91779173
assert(V->getDefiningRecipe()->getParent()->getEnclosingLoopRegion() &&

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,23 @@ void PlainCFGBuilder::buildPlainCFG(
365365
Plan.getEntry()->setOneSuccessor(getOrCreateVPBB(TheLoop->getHeader()));
366366
Plan.getEntry()->setPlan(&Plan);
367367

368+
for (auto *EB : Plan.getExitBlocks()) {
369+
for (VPRecipeBase &R : *EB) {
370+
auto *PhiR = cast<VPIRInstruction>(&R);
371+
auto *Phi = dyn_cast<PHINode>(&PhiR->getInstruction());
372+
if (!Phi)
373+
break;
374+
for (Value *Inc : Phi->incoming_values())
375+
PhiR->addOperand(getOrCreateVPOperand(Inc));
376+
if (R.getNumOperands() > 1 &&
377+
Phi->getIncomingBlock(0) != TheLoop->getLoopLatch()) {
378+
VPValue *Tmp = R.getOperand(0);
379+
R.setOperand(0, R.getOperand(1));
380+
R.setOperand(1, Tmp);
381+
}
382+
}
383+
}
384+
368385
for (const auto &[IRBB, VPB] : BB2VPBB)
369386
VPB2IRBB[VPB] = IRBB;
370387

@@ -451,6 +468,11 @@ void VPlanTransforms::introduceRegions(VPlan &Plan, Type *InductionTy,
451468
VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader());
452469
if (!RequiresScalarEpilogueCheck) {
453470
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
471+
for (auto *EB : Plan.getExitBlocks()) {
472+
for (VPRecipeBase &R : *EB)
473+
for (unsigned Idx = 0; Idx != R.getNumOperands(); ++Idx)
474+
R.setOperand(Idx, Plan.getOrAddLiveIn(PoisonValue::get(InductionTy)));
475+
}
454476
return;
455477
}
456478

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ InstructionCost VPIRInstruction::computeCost(ElementCount VF,
10961096
void VPIRInstruction::extractLastLaneOfOperand(VPBuilder &Builder) {
10971097
assert(isa<PHINode>(getInstruction()) &&
10981098
"can only add exiting operands to phi nodes");
1099-
assert(getNumOperands() == 1 && "must have a single operand");
1099+
// assert(getNumOperands() == 1 && "must have a single operand");
11001100
VPValue *Exiting = getOperand(0);
11011101
if (!Exiting->isLiveIn()) {
11021102
LLVMContext &Ctx = getInstruction().getContext();

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -2410,29 +2410,27 @@ void VPlanTransforms::handleUncountableEarlyExit(
24102410
if (!ExitIRI)
24112411
break;
24122412

2413-
PHINode &ExitPhi = ExitIRI->getIRPhi();
2414-
VPValue *IncomingFromEarlyExit = RecipeBuilder.getVPValueOrAddLiveIn(
2415-
ExitPhi.getIncomingValueForBlock(UncountableExitingBlock));
2413+
unsigned EarlyExitIdx = 0;
24162414

24172415
if (OrigLoop->getUniqueExitBlock()) {
2416+
EarlyExitIdx = 1;
24182417
// If there's a unique exit block, VPEarlyExitBlock has 2 predecessors
24192418
// (MiddleVPBB and NewMiddle). Add the incoming value from MiddleVPBB
24202419
// which is coming from the original latch.
2421-
VPValue *IncomingFromLatch = RecipeBuilder.getVPValueOrAddLiveIn(
2422-
ExitPhi.getIncomingValueForBlock(OrigLoop->getLoopLatch()));
2423-
ExitIRI->addOperand(IncomingFromLatch);
24242420
ExitIRI->extractLastLaneOfOperand(MiddleBuilder);
24252421
}
2422+
VPValue *IncomingFromEarlyExit = ExitIRI->getOperand(EarlyExitIdx);
24262423
// Add the incoming value from the early exit.
24272424
if (!IncomingFromEarlyExit->isLiveIn() && !Plan.hasScalarVFOnly()) {
24282425
VPValue *FirstActiveLane = EarlyExitB.createNaryOp(
24292426
VPInstruction::FirstActiveLane, {EarlyExitTakenCond}, nullptr,
24302427
"first.active.lane");
2431-
IncomingFromEarlyExit = EarlyExitB.createNaryOp(
2432-
Instruction::ExtractElement, {IncomingFromEarlyExit, FirstActiveLane},
2433-
nullptr, "early.exit.value");
2428+
ExitIRI->setOperand(
2429+
EarlyExitIdx,
2430+
EarlyExitB.createNaryOp(Instruction::ExtractElement,
2431+
{IncomingFromEarlyExit, FirstActiveLane},
2432+
nullptr, "early.exit.value"));
24342433
}
2435-
ExitIRI->addOperand(IncomingFromEarlyExit);
24362434
}
24372435
MiddleBuilder.createNaryOp(VPInstruction::BranchOnCond, {IsEarlyExitTaken});
24382436

0 commit comments

Comments
 (0)