Skip to content

Commit 6cf1ed5

Browse files
committed
[VPlan] Implement printing VPIRMetadata.
Implement and use debug printing for VPIRMetadata.
1 parent 138e0ff commit 6cf1ed5

File tree

5 files changed

+83
-28
lines changed

5 files changed

+83
-28
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,12 @@ class VPIRMetadata {
968968
/// Intersect this VPIRMetada object with \p MD, keeping only metadata
969969
/// nodes that are common to both.
970970
void intersect(const VPIRMetadata &MD);
971+
972+
/// Print metadata with node IDs.
973+
void print(raw_ostream &O, const Module &M) const;
974+
975+
/// Return true if there is any metadata to print.
976+
bool empty() const { return Metadata.empty(); }
971977
};
972978

973979
/// This is a concrete Recipe that models a single VPlan-level instruction.
@@ -4282,6 +4288,11 @@ class VPlan {
42824288
/// Return the VPIRBasicBlock wrapping the header of the scalar loop.
42834289
VPIRBasicBlock *getScalarHeader() const { return ScalarHeader; }
42844290

4291+
/// Return the Module from the scalar header.
4292+
const Module &getModule() const {
4293+
return *ScalarHeader->getIRBasicBlock()->getModule();
4294+
}
4295+
42854296
/// Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of
42864297
/// the original scalar loop.
42874298
ArrayRef<VPIRBasicBlock *> getExitBlocks() const { return ExitBlocks; }

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,8 @@ void VPInstruction::print(raw_ostream &O, const Twine &Indent,
14431443

14441444
printFlags(O);
14451445
printOperands(O, SlotTracker);
1446+
if (!VPIRMetadata::empty())
1447+
VPIRMetadata::print(O, getParent()->getPlan()->getModule());
14461448

14471449
if (auto DL = getDebugLoc()) {
14481450
O << ", !dbg ";
@@ -1669,6 +1671,25 @@ void VPIRMetadata::intersect(const VPIRMetadata &Other) {
16691671
Metadata = std::move(MetadataIntersection);
16701672
}
16711673

1674+
void VPIRMetadata::print(raw_ostream &O, const Module &M) const {
1675+
if (Metadata.empty())
1676+
return;
1677+
1678+
SmallVector<StringRef, 8> MDNames;
1679+
M.getContext().getMDKindNames(MDNames);
1680+
1681+
O << " (";
1682+
interleaveComma(Metadata, O, [&](const auto &KindNodePair) {
1683+
auto [Kind, Node] = KindNodePair;
1684+
assert(Kind != 0 && "Debug metadata should not be managed by VPIRMetadata");
1685+
assert(Kind < MDNames.size() && !MDNames[Kind].empty() &&
1686+
"Unexpected unnamed metadata kind");
1687+
O << "!" << MDNames[Kind] << " ";
1688+
Node->printAsOperand(O, &M);
1689+
});
1690+
O << ")";
1691+
}
1692+
16721693
void VPWidenCallRecipe::execute(VPTransformState &State) {
16731694
assert(State.VF.isVector() && "not widening");
16741695
assert(Variant != nullptr && "Can't create vector function.");
@@ -1729,6 +1750,8 @@ void VPWidenCallRecipe::print(raw_ostream &O, const Twine &Indent,
17291750
Op->printAsOperand(O, SlotTracker);
17301751
});
17311752
O << ")";
1753+
if (!VPIRMetadata::empty())
1754+
VPIRMetadata::print(O, getParent()->getPlan()->getModule());
17321755

17331756
O << " (using library function";
17341757
if (Variant->hasName())
@@ -1863,6 +1886,8 @@ void VPWidenIntrinsicRecipe::print(raw_ostream &O, const Twine &Indent,
18631886
Op->printAsOperand(O, SlotTracker);
18641887
});
18651888
O << ")";
1889+
if (!VPIRMetadata::empty())
1890+
VPIRMetadata::print(O, getParent()->getPlan()->getModule());
18661891
}
18671892
#endif
18681893

@@ -2255,6 +2280,8 @@ void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent,
22552280
O << " = " << Instruction::getOpcodeName(Opcode);
22562281
printFlags(O);
22572282
printOperands(O, SlotTracker);
2283+
if (!VPIRMetadata::empty())
2284+
VPIRMetadata::print(O, getParent()->getPlan()->getModule());
22582285
}
22592286
#endif
22602287

@@ -2336,6 +2363,8 @@ void VPWidenCastRecipe::print(raw_ostream &O, const Twine &Indent,
23362363
printFlags(O);
23372364
printOperands(O, SlotTracker);
23382365
O << " to " << *getResultType();
2366+
if (!VPIRMetadata::empty())
2367+
VPIRMetadata::print(O, getParent()->getPlan()->getModule());
23392368
}
23402369
#endif
23412370

@@ -3617,6 +3646,8 @@ void VPWidenLoadRecipe::print(raw_ostream &O, const Twine &Indent,
36173646
printAsOperand(O, SlotTracker);
36183647
O << " = load ";
36193648
printOperands(O, SlotTracker);
3649+
if (!VPIRMetadata::empty())
3650+
VPIRMetadata::print(O, getParent()->getPlan()->getModule());
36203651
}
36213652
#endif
36223653

@@ -3738,6 +3769,8 @@ void VPWidenStoreRecipe::print(raw_ostream &O, const Twine &Indent,
37383769
VPSlotTracker &SlotTracker) const {
37393770
O << Indent << "WIDEN store ";
37403771
printOperands(O, SlotTracker);
3772+
if (!VPIRMetadata::empty())
3773+
VPIRMetadata::print(O, getParent()->getPlan()->getModule());
37413774
}
37423775
#endif
37433776

llvm/test/Transforms/LoopVectorize/vplan-printing-metadata.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ define void @test_widen_metadata(ptr noalias %A, ptr noalias %B, i32 %n) {
77
; CHECK: VPlan 'Initial VPlan for VF={4},UF>=1' {
88
; CHECK: <x1> vector loop: {
99
; CHECK: vector.body:
10-
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}>
11-
; CHECK: WIDEN-CAST ir<%conv> = sitofp ir<%lv> to float
12-
; CHECK: WIDEN ir<%mul> = fmul ir<%conv>, ir<2.000000e+00>
10+
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}> (!tbaa !{{[0-9]+}})
11+
; CHECK: WIDEN-CAST ir<%conv> = sitofp ir<%lv> to float (!fpmath !{{[0-9]+}})
12+
; CHECK: WIDEN ir<%mul> = fmul ir<%conv>, ir<2.000000e+00> (!fpmath !{{[0-9]+}})
1313
; CHECK: WIDEN-CAST ir<%conv.back> = fptosi ir<%mul> to i32
14-
; CHECK: WIDEN store vp<{{.*}}>, ir<%conv.back>
14+
; CHECK: WIDEN store vp<{{.*}}>, ir<%conv.back> (!tbaa !{{[0-9]+}})
1515
;
1616
entry:
1717
br label %loop
@@ -40,9 +40,9 @@ define void @test_intrinsic_with_metadata(ptr noalias %A, ptr noalias %B, i32 %n
4040
; CHECK: VPlan 'Initial VPlan for VF={4},UF>=1' {
4141
; CHECK: <x1> vector loop: {
4242
; CHECK: vector.body:
43-
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}>
44-
; CHECK: WIDEN-INTRINSIC ir<%sqrt> = call llvm.sqrt(ir<%lv>)
45-
; CHECK: WIDEN store vp<{{.*}}>, ir<%sqrt>
43+
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}> (!tbaa !{{[0-9]+}})
44+
; CHECK: WIDEN-INTRINSIC ir<%sqrt> = call llvm.sqrt(ir<%lv>) (!fpmath !{{[0-9]+}})
45+
; CHECK: WIDEN store vp<{{.*}}>, ir<%sqrt> (!tbaa !{{[0-9]+}})
4646
;
4747
entry:
4848
br label %loop
@@ -67,11 +67,11 @@ define void @test_widen_with_multiple_metadata(ptr noalias %A, ptr noalias %B, i
6767
; CHECK: VPlan 'Initial VPlan for VF={4},UF>=1' {
6868
; CHECK: <x1> vector loop: {
6969
; CHECK: vector.body:
70-
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}>
70+
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}> (!tbaa !{{[0-9]+}})
7171
; CHECK: WIDEN-CAST ir<%conv> = sitofp ir<%lv> to float
7272
; CHECK: WIDEN ir<%mul> = fmul ir<%conv>, ir<2.000000e+00>
7373
; CHECK: WIDEN-CAST ir<%conv.back> = fptosi ir<%mul> to i32
74-
; CHECK: WIDEN store vp<{{.*}}>, ir<%conv.back>
74+
; CHECK: WIDEN store vp<{{.*}}>, ir<%conv.back> (!tbaa !{{[0-9]+}})
7575
;
7676
entry:
7777
br label %loop

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -731,18 +731,18 @@ TEST_F(VPBasicBlockTest, print) {
731731
VPBB2->setName("bb2");
732732

733733
VPBlockUtils::connectBlocks(VPBB1, VPBB2);
734+
VPBlockUtils::connectBlocks(VPBB2, Plan.getScalarHeader());
735+
VPBlockUtils::connectBlocks(VPBB0, VPBB1);
734736

735-
// Check printing an instruction without associated VPlan.
737+
// Check printing an instruction with associated VPlan.
736738
{
737739
std::string I3Dump;
738740
raw_string_ostream OS(I3Dump);
739-
VPSlotTracker SlotTracker;
741+
VPSlotTracker SlotTracker(&Plan);
740742
I3->print(OS, "", SlotTracker);
741-
EXPECT_EQ("EMIT br <badref>, <badref>", I3Dump);
743+
EXPECT_EQ("EMIT br vp<%2>, vp<%3>", I3Dump);
742744
}
743745

744-
VPBlockUtils::connectBlocks(VPBB2, Plan.getScalarHeader());
745-
VPBlockUtils::connectBlocks(VPBB0, VPBB1);
746746
std::string FullDump;
747747
raw_string_ostream OS(FullDump);
748748
Plan.printDOT(OS);
@@ -1509,12 +1509,18 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
15091509
auto *AI = BinaryOperator::CreateAdd(PoisonValue::get(Int32),
15101510
PoisonValue::get(Int32));
15111511
AI->setName("a");
1512-
VPValue *ExtVPV1 = getPlan().getOrAddLiveIn(ConstantInt::get(Int32, 1));
1513-
VPValue *ExtVPV2 = getPlan().getOrAddLiveIn(AI);
1512+
VPlan &Plan = getPlan();
1513+
VPValue *ExtVPV1 = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
1514+
VPValue *ExtVPV2 = Plan.getOrAddLiveIn(AI);
15141515

15151516
VPInstruction *I1 = new VPInstruction(Instruction::Add, {ExtVPV1, ExtVPV2});
15161517
VPInstruction *I2 = new VPInstruction(Instruction::Mul, {I1, I1});
15171518

1519+
// Add instructions to a block in the plan so they have access to Module
1520+
VPBasicBlock *VPBB = Plan.getEntry();
1521+
VPBB->appendRecipe(I1);
1522+
VPBB->appendRecipe(I2);
1523+
15181524
// Check printing I1.
15191525
{
15201526
// Use EXPECT_EXIT to capture stderr and compare against expected output.
@@ -1526,7 +1532,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
15261532
VPV->dump();
15271533
exit(0);
15281534
},
1529-
testing::ExitedWithCode(0), "EMIT <badref> = add ir<1>, ir<%a>");
1535+
testing::ExitedWithCode(0), "EMIT vp<%1> = add ir<1>, ir<%a>");
15301536

15311537
// Test VPRecipeBase::dump().
15321538
VPRecipeBase *R = I1;
@@ -1535,7 +1541,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
15351541
R->dump();
15361542
exit(0);
15371543
},
1538-
testing::ExitedWithCode(0), "EMIT <badref> = add ir<1>, ir<%a>");
1544+
testing::ExitedWithCode(0), "EMIT vp<%1> = add ir<1>, ir<%a>");
15391545

15401546
// Test VPDef::dump().
15411547
VPDef *D = I1;
@@ -1544,7 +1550,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
15441550
D->dump();
15451551
exit(0);
15461552
},
1547-
testing::ExitedWithCode(0), "EMIT <badref> = add ir<1>, ir<%a>");
1553+
testing::ExitedWithCode(0), "EMIT vp<%1> = add ir<1>, ir<%a>");
15481554
}
15491555
// Check printing I2.
15501556
{
@@ -1557,7 +1563,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
15571563
VPV->dump();
15581564
exit(0);
15591565
},
1560-
testing::ExitedWithCode(0), "EMIT <badref> = mul <badref>, <badref>");
1566+
testing::ExitedWithCode(0), "EMIT vp<%2> = mul vp<%1>, vp<%1>");
15611567

15621568
// Test VPRecipeBase::dump().
15631569
VPRecipeBase *R = I2;
@@ -1566,7 +1572,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
15661572
R->dump();
15671573
exit(0);
15681574
},
1569-
testing::ExitedWithCode(0), "EMIT <badref> = mul <badref>, <badref>");
1575+
testing::ExitedWithCode(0), "EMIT vp<%2> = mul vp<%1>, vp<%1>");
15701576

15711577
// Test VPDef::dump().
15721578
VPDef *D = I2;
@@ -1575,11 +1581,9 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
15751581
D->dump();
15761582
exit(0);
15771583
},
1578-
testing::ExitedWithCode(0), "EMIT <badref> = mul <badref>, <badref>");
1584+
testing::ExitedWithCode(0), "EMIT vp<%2> = mul vp<%1>, vp<%1>");
15791585
}
15801586

1581-
delete I2;
1582-
delete I1;
15831587
delete AI;
15841588
}
15851589

llvm/unittests/Transforms/Vectorize/VPlanTestBase.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,22 @@ class VPlanTestIRBase : public testing::Test {
8686
class VPlanTestBase : public testing::Test {
8787
protected:
8888
LLVMContext C;
89-
std::unique_ptr<BasicBlock> ScalarHeader;
89+
std::unique_ptr<Module> M;
90+
Function *F;
91+
BasicBlock *ScalarHeader;
9092
SmallVector<std::unique_ptr<VPlan>> Plans;
9193

92-
VPlanTestBase() : ScalarHeader(BasicBlock::Create(C, "scalar.header")) {
93-
BranchInst::Create(&*ScalarHeader, &*ScalarHeader);
94+
VPlanTestBase() {
95+
M = std::make_unique<Module>("VPlanTest", C);
96+
FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), false);
97+
F = Function::Create(FTy, GlobalValue::ExternalLinkage, "test_function",
98+
M.get());
99+
ScalarHeader = BasicBlock::Create(C, "scalar.header", F);
100+
BranchInst::Create(ScalarHeader, ScalarHeader);
94101
}
95102

96103
VPlan &getPlan(VPValue *TC = nullptr) {
97-
Plans.push_back(std::make_unique<VPlan>(&*ScalarHeader, TC));
104+
Plans.push_back(std::make_unique<VPlan>(ScalarHeader, TC));
98105
return *Plans.back();
99106
}
100107
};

0 commit comments

Comments
 (0)