Skip to content

Commit 672e926

Browse files
committed
Reapply "[VPlan] Support cloning initial VPlan (NFC)."
This reverts commit 204252e. Recommit with a fix for the leak in a unit test.
1 parent 35c15e4 commit 672e926

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,11 +1168,16 @@ VPlan *VPlan::duplicate() {
11681168
const auto &[NewEntry, __] = cloneFrom(Entry);
11691169

11701170
BasicBlock *ScalarHeaderIRBB = getScalarHeader()->getIRBasicBlock();
1171-
VPIRBasicBlock *NewScalarHeader = cast<VPIRBasicBlock>(*find_if(
1172-
vp_depth_first_shallow(NewEntry), [ScalarHeaderIRBB](VPBlockBase *VPB) {
1173-
auto *VPIRBB = dyn_cast<VPIRBasicBlock>(VPB);
1174-
return VPIRBB && VPIRBB->getIRBasicBlock() == ScalarHeaderIRBB;
1175-
}));
1171+
VPIRBasicBlock *NewScalarHeader = nullptr;
1172+
if (getScalarHeader()->getNumPredecessors() == 0) {
1173+
NewScalarHeader = createVPIRBasicBlock(ScalarHeaderIRBB);
1174+
} else {
1175+
NewScalarHeader = cast<VPIRBasicBlock>(*find_if(
1176+
vp_depth_first_shallow(NewEntry), [ScalarHeaderIRBB](VPBlockBase *VPB) {
1177+
auto *VPIRBB = dyn_cast<VPIRBasicBlock>(VPB);
1178+
return VPIRBB && VPIRBB->getIRBasicBlock() == ScalarHeaderIRBB;
1179+
}));
1180+
}
11761181
// Create VPlan, clone live-ins and remap operands in the cloned blocks.
11771182
auto *NewPlan = new VPlan(cast<VPBasicBlock>(NewEntry), NewScalarHeader);
11781183
DenseMap<VPValue *, VPValue *> Old2NewVPValues;
@@ -1187,8 +1192,7 @@ VPlan *VPlan::duplicate() {
11871192
NewPlan->BackedgeTakenCount = new VPValue();
11881193
Old2NewVPValues[BackedgeTakenCount] = NewPlan->BackedgeTakenCount;
11891194
}
1190-
assert(TripCount && "trip count must be set");
1191-
if (TripCount->isLiveIn())
1195+
if (TripCount && TripCount->isLiveIn())
11921196
Old2NewVPValues[TripCount] =
11931197
NewPlan->getOrAddLiveIn(TripCount->getLiveInIRValue());
11941198
// else NewTripCount will be created and inserted into Old2NewVPValues when
@@ -1201,9 +1205,11 @@ VPlan *VPlan::duplicate() {
12011205
NewPlan->UFs = UFs;
12021206
// TODO: Adjust names.
12031207
NewPlan->Name = Name;
1204-
assert(Old2NewVPValues.contains(TripCount) &&
1205-
"TripCount must have been added to Old2NewVPValues");
1206-
NewPlan->TripCount = Old2NewVPValues[TripCount];
1208+
if (TripCount) {
1209+
assert(Old2NewVPValues.contains(TripCount) &&
1210+
"TripCount must have been added to Old2NewVPValues");
1211+
NewPlan->TripCount = Old2NewVPValues[TripCount];
1212+
}
12071213

12081214
// Transfer all cloned blocks (the second half of all current blocks) from
12091215
// current to new VPlan.

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,49 @@ No successors
904904
EXPECT_EQ(ExpectedStr, FullDump);
905905
}
906906
}
907+
908+
TEST_F(VPBasicBlockTest, cloneAndPrint) {
909+
VPlan &Plan = getPlan(nullptr);
910+
VPBasicBlock *VPBB0 = Plan.getEntry();
911+
912+
VPInstruction *I1 = new VPInstruction(Instruction::Add, {});
913+
VPInstruction *I2 = new VPInstruction(Instruction::Sub, {I1});
914+
VPInstruction *I3 = new VPInstruction(Instruction::Br, {I1, I2});
915+
916+
VPBasicBlock *VPBB1 = Plan.createVPBasicBlock("");
917+
VPBB1->appendRecipe(I1);
918+
VPBB1->appendRecipe(I2);
919+
VPBB1->appendRecipe(I3);
920+
VPBB1->setName("bb1");
921+
VPBlockUtils::connectBlocks(VPBB0, VPBB1);
922+
923+
const char *ExpectedStr = R"(digraph VPlan {
924+
graph [labelloc=t, fontsize=30; label="Vectorization Plan\n for UF\>=1\n"]
925+
node [shape=rect, fontname=Courier, fontsize=30]
926+
edge [fontname=Courier, fontsize=30]
927+
compound=true
928+
N0 [label =
929+
"preheader:\l" +
930+
"Successor(s): bb1\l"
931+
]
932+
N0 -> N1 [ label=""]
933+
N1 [label =
934+
"bb1:\l" +
935+
" EMIT vp\<%1\> = add\l" +
936+
" EMIT vp\<%2\> = sub vp\<%1\>\l" +
937+
" EMIT br vp\<%1\>, vp\<%2\>\l" +
938+
"No successors\l"
939+
]
940+
}
941+
)";
942+
// Check that printing a cloned plan produces the same output.
943+
std::string FullDump;
944+
raw_string_ostream OS(FullDump);
945+
VPlan *Clone = Plan.duplicate();
946+
Clone->printDOT(OS);
947+
EXPECT_EQ(ExpectedStr, FullDump);
948+
delete Clone;
949+
}
907950
#endif
908951

909952
using VPRecipeTest = VPlanTestBase;

0 commit comments

Comments
 (0)