Skip to content

Commit 2477756

Browse files
committed
[VPlan] Add exit opernds early.
1 parent 1bb836a commit 2477756

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -9141,11 +9141,7 @@ collectUsersInExitBlocks(Loop *OrigLoop, VPRecipeBuilder &Builder,
91419141
continue;
91429142
}
91439143

9144-
PHINode &ExitPhi = ExitIRI->getIRPhi();
9145-
BasicBlock *ExitingBB = OrigLoop->getLoopLatch();
9146-
Value *IncomingValue = ExitPhi.getIncomingValueForBlock(ExitingBB);
9147-
VPValue *V = Builder.getVPValueOrAddLiveIn(IncomingValue);
9148-
ExitIRI->addOperand(V);
9144+
VPValue *V = ExitIRI->getOperand(0);
91499145
if (V->isLiveIn())
91509146
continue;
91519147
assert(V->getDefiningRecipe()->getParent()->getEnclosingLoopRegion() &&

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,24 @@ void PlainCFGBuilder::buildPlainCFG(
375375
VPBlockUtils::connectBlocks(Plan.getEntry(),
376376
getOrCreateVPBB(TheLoop->getHeader()));
377377

378+
for (auto *EB : Plan.getExitBlocks()) {
379+
BasicBlock *IRBB = EB->getIRBasicBlock();
380+
for (VPRecipeBase &R : *EB) {
381+
auto *PhiR = cast<VPIRInstruction>(&R);
382+
auto *Phi = dyn_cast<PHINode>(&PhiR->getInstruction());
383+
if (!Phi)
384+
break;
385+
for (Value *Inc : Phi->incoming_values())
386+
PhiR->addOperand(getOrCreateVPOperand(Inc));
387+
if (R.getNumOperands() > 1 &&
388+
Phi->getIncomingBlock(0) != TheLoop->getLoopLatch()) {
389+
VPValue *Tmp = R.getOperand(0);
390+
R.setOperand(0, R.getOperand(1));
391+
R.setOperand(1, Tmp);
392+
}
393+
}
394+
}
395+
378396
for (const auto &[IRBB, VPB] : BB2VPBB)
379397
VPB2IRBB[VPB] = IRBB;
380398

@@ -466,6 +484,11 @@ void VPlanTransforms::introduceTopLevelVectorLoopRegion(
466484
VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader());
467485
if (!RequiresScalarEpilogueCheck) {
468486
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
487+
for (auto *EB : Plan.getExitBlocks()) {
488+
for (VPRecipeBase &R : *EB)
489+
for (unsigned Idx = 0; Idx != R.getNumOperands(); ++Idx)
490+
R.setOperand(Idx, Plan.getOrAddLiveIn(PoisonValue::get(InductionTy)));
491+
}
469492
return;
470493
}
471494

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ InstructionCost VPIRInstruction::computeCost(ElementCount VF,
10891089
void VPIRInstruction::extractLastLaneOfOperand(VPBuilder &Builder) {
10901090
assert(isa<PHINode>(getInstruction()) &&
10911091
"can only add exiting operands to phi nodes");
1092-
assert(getNumOperands() == 1 && "must have a single operand");
1092+
// assert(getNumOperands() == 1 && "must have a single operand");
10931093
VPValue *Exiting = getOperand(0);
10941094
if (!Exiting->isLiveIn()) {
10951095
LLVMContext &Ctx = getInstruction().getContext();

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -2317,29 +2317,27 @@ void VPlanTransforms::handleUncountableEarlyExit(
23172317
if (!ExitIRI)
23182318
break;
23192319

2320-
PHINode &ExitPhi = ExitIRI->getIRPhi();
2321-
VPValue *IncomingFromEarlyExit = RecipeBuilder.getVPValueOrAddLiveIn(
2322-
ExitPhi.getIncomingValueForBlock(UncountableExitingBlock));
2320+
unsigned EarlyExitIdx = 0;
23232321

23242322
if (OrigLoop->getUniqueExitBlock()) {
2323+
EarlyExitIdx = 1;
23252324
// If there's a unique exit block, VPEarlyExitBlock has 2 predecessors
23262325
// (MiddleVPBB and NewMiddle). Add the incoming value from MiddleVPBB
23272326
// which is coming from the original latch.
2328-
VPValue *IncomingFromLatch = RecipeBuilder.getVPValueOrAddLiveIn(
2329-
ExitPhi.getIncomingValueForBlock(OrigLoop->getLoopLatch()));
2330-
ExitIRI->addOperand(IncomingFromLatch);
23312327
ExitIRI->extractLastLaneOfOperand(MiddleBuilder);
23322328
}
2329+
VPValue *IncomingFromEarlyExit = ExitIRI->getOperand(EarlyExitIdx);
23332330
// Add the incoming value from the early exit.
23342331
if (!IncomingFromEarlyExit->isLiveIn() && !Plan.hasScalarVFOnly()) {
23352332
VPValue *FirstActiveLane = EarlyExitB.createNaryOp(
23362333
VPInstruction::FirstActiveLane, {EarlyExitTakenCond}, nullptr,
23372334
"first.active.lane");
2338-
IncomingFromEarlyExit = EarlyExitB.createNaryOp(
2339-
Instruction::ExtractElement, {IncomingFromEarlyExit, FirstActiveLane},
2340-
nullptr, "early.exit.value");
2335+
ExitIRI->setOperand(
2336+
EarlyExitIdx,
2337+
EarlyExitB.createNaryOp(Instruction::ExtractElement,
2338+
{IncomingFromEarlyExit, FirstActiveLane},
2339+
nullptr, "early.exit.value"));
23412340
}
2342-
ExitIRI->addOperand(IncomingFromEarlyExit);
23432341
}
23442342
MiddleBuilder.createNaryOp(VPInstruction::BranchOnCond, {IsEarlyExitTaken});
23452343

0 commit comments

Comments
 (0)