Skip to content

Commit beae906

Browse files
committed
[VPlan] Implement printing VPIRMetadata.
Implement and use debug printing for VPIRMetadata. !fixup address comments, thanks!
1 parent 6756e73 commit beae906

File tree

5 files changed

+70
-25
lines changed

5 files changed

+70
-25
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,11 @@ class VPIRMetadata {
983983
/// Intersect this VPIRMetada object with \p MD, keeping only metadata
984984
/// nodes that are common to both.
985985
void intersect(const VPIRMetadata &MD);
986+
987+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
988+
/// Print metadata with node IDs.
989+
void print(raw_ostream &O, const Module &M) const;
990+
#endif
986991
};
987992

988993
/// This is a concrete Recipe that models a single VPlan-level instruction.
@@ -4404,6 +4409,11 @@ class VPlan {
44044409
/// Return the VPIRBasicBlock wrapping the header of the scalar loop.
44054410
VPIRBasicBlock *getScalarHeader() const { return ScalarHeader; }
44064411

4412+
/// Return the Module from the scalar header.
4413+
const Module &getModule() const {
4414+
return *ScalarHeader->getIRBasicBlock()->getModule();
4415+
}
4416+
44074417
/// Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of
44084418
/// the original scalar loop.
44094419
ArrayRef<VPIRBasicBlock *> getExitBlocks() const { return ExitBlocks; }

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ void VPSingleDefRecipe::dump() const { VPDef::dump(); }
489489
void VPRecipeBase::print(raw_ostream &O, const Twine &Indent,
490490
VPSlotTracker &SlotTracker) const {
491491
printRecipe(O, Indent, SlotTracker);
492+
493+
if (auto *Metadata = dyn_cast<VPIRMetadata>(this))
494+
Metadata->print(O, getParent()->getPlan()->getModule());
492495
}
493496
#endif
494497

@@ -1703,6 +1706,27 @@ void VPIRMetadata::intersect(const VPIRMetadata &Other) {
17031706
Metadata = std::move(MetadataIntersection);
17041707
}
17051708

1709+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1710+
void VPIRMetadata::print(raw_ostream &O, const Module &M) const {
1711+
if (Metadata.empty())
1712+
return;
1713+
1714+
SmallVector<StringRef, 8> MDNames;
1715+
M.getContext().getMDKindNames(MDNames);
1716+
1717+
O << " (";
1718+
interleaveComma(Metadata, O, [&](const auto &KindNodePair) {
1719+
auto [Kind, Node] = KindNodePair;
1720+
assert(Kind != 0 && "Debug metadata should not be managed by VPIRMetadata");
1721+
assert(Kind < MDNames.size() && !MDNames[Kind].empty() &&
1722+
"Unexpected unnamed metadata kind");
1723+
O << "!" << MDNames[Kind] << " ";
1724+
Node->printAsOperand(O, &M);
1725+
});
1726+
O << ")";
1727+
}
1728+
#endif
1729+
17061730
void VPWidenCallRecipe::execute(VPTransformState &State) {
17071731
assert(State.VF.isVector() && "not widening");
17081732
assert(Variant != nullptr && "Can't create vector function.");

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: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,10 @@ TEST_F(VPBasicBlockTest, print) {
743743
VPBB2->setName("bb2");
744744

745745
VPBlockUtils::connectBlocks(VPBB1, VPBB2);
746+
VPBlockUtils::connectBlocks(VPBB2, Plan.getScalarHeader());
747+
VPBlockUtils::connectBlocks(VPBB0, VPBB1);
746748

747-
// Check printing an instruction without associated VPlan.
749+
// Check printing an instruction with associated VPlan.
748750
{
749751
std::string I3Dump;
750752
raw_string_ostream OS(I3Dump);
@@ -753,8 +755,6 @@ TEST_F(VPBasicBlockTest, print) {
753755
EXPECT_EQ("EMIT store <badref>, <badref>", I3Dump);
754756
}
755757

756-
VPBlockUtils::connectBlocks(VPBB2, Plan.getScalarHeader());
757-
VPBlockUtils::connectBlocks(VPBB0, VPBB1);
758758
std::string FullDump;
759759
raw_string_ostream OS(FullDump);
760760
Plan.printDOT(OS);
@@ -1612,12 +1612,18 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
16121612
auto *AI = BinaryOperator::CreateAdd(PoisonValue::get(Int32),
16131613
PoisonValue::get(Int32));
16141614
AI->setName("a");
1615-
VPValue *ExtVPV1 = getPlan().getOrAddLiveIn(ConstantInt::get(Int32, 1));
1616-
VPValue *ExtVPV2 = getPlan().getOrAddLiveIn(AI);
1615+
VPlan &Plan = getPlan();
1616+
VPValue *ExtVPV1 = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
1617+
VPValue *ExtVPV2 = Plan.getOrAddLiveIn(AI);
16171618

16181619
VPInstruction *I1 = new VPInstruction(Instruction::Add, {ExtVPV1, ExtVPV2});
16191620
VPInstruction *I2 = new VPInstruction(Instruction::Mul, {I1, I1});
16201621

1622+
// Add instructions to a block in the plan so they have access to Module
1623+
VPBasicBlock *VPBB = Plan.getEntry();
1624+
VPBB->appendRecipe(I1);
1625+
VPBB->appendRecipe(I2);
1626+
16211627
// Check printing I1.
16221628
{
16231629
// Use EXPECT_EXIT to capture stderr and compare against expected output.
@@ -1629,7 +1635,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
16291635
VPV->dump();
16301636
exit(0);
16311637
},
1632-
testing::ExitedWithCode(0), "EMIT <badref> = add ir<1>, ir<%a>");
1638+
testing::ExitedWithCode(0), "EMIT vp<%1> = add ir<1>, ir<%a>");
16331639

16341640
// Test VPRecipeBase::dump().
16351641
VPRecipeBase *R = I1;
@@ -1638,7 +1644,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
16381644
R->dump();
16391645
exit(0);
16401646
},
1641-
testing::ExitedWithCode(0), "EMIT <badref> = add ir<1>, ir<%a>");
1647+
testing::ExitedWithCode(0), "EMIT vp<%1> = add ir<1>, ir<%a>");
16421648

16431649
// Test VPDef::dump().
16441650
VPDef *D = I1;
@@ -1647,7 +1653,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
16471653
D->dump();
16481654
exit(0);
16491655
},
1650-
testing::ExitedWithCode(0), "EMIT <badref> = add ir<1>, ir<%a>");
1656+
testing::ExitedWithCode(0), "EMIT vp<%1> = add ir<1>, ir<%a>");
16511657
}
16521658
// Check printing I2.
16531659
{
@@ -1660,7 +1666,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
16601666
VPV->dump();
16611667
exit(0);
16621668
},
1663-
testing::ExitedWithCode(0), "EMIT <badref> = mul <badref>, <badref>");
1669+
testing::ExitedWithCode(0), "EMIT vp<%2> = mul vp<%1>, vp<%1>");
16641670

16651671
// Test VPRecipeBase::dump().
16661672
VPRecipeBase *R = I2;
@@ -1669,7 +1675,7 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
16691675
R->dump();
16701676
exit(0);
16711677
},
1672-
testing::ExitedWithCode(0), "EMIT <badref> = mul <badref>, <badref>");
1678+
testing::ExitedWithCode(0), "EMIT vp<%2> = mul vp<%1>, vp<%1>");
16731679

16741680
// Test VPDef::dump().
16751681
VPDef *D = I2;
@@ -1678,11 +1684,9 @@ TEST_F(VPRecipeTest, dumpRecipeUnnamedVPValuesNotInPlanOrBlock) {
16781684
D->dump();
16791685
exit(0);
16801686
},
1681-
testing::ExitedWithCode(0), "EMIT <badref> = mul <badref>, <badref>");
1687+
testing::ExitedWithCode(0), "EMIT vp<%2> = mul vp<%1>, vp<%1>");
16821688
}
16831689

1684-
delete I2;
1685-
delete I1;
16861690
delete AI;
16871691
}
16881692

llvm/unittests/Transforms/Vectorize/VPlanTestBase.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,18 @@ 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() {

0 commit comments

Comments
 (0)