Skip to content

Commit 0e11a92

Browse files
authored
[VPlan] Implement printing VPIRMetadata. (#168385)
mplement printing for VPIRMetadata, using generic dyn_cast to VPIRMetadata. Depends on #166245 PR: #168385
1 parent f6cbd7a commit 0e11a92

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,11 @@ class VPIRMetadata {
10201020
find_if(Metadata, [Kind](const auto &P) { return P.first == Kind; });
10211021
return It != Metadata.end() ? It->second : nullptr;
10221022
}
1023+
1024+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1025+
/// Print metadata with node IDs.
1026+
void print(raw_ostream &O, VPSlotTracker &SlotTracker) const;
1027+
#endif
10231028
};
10241029

10251030
/// This is a concrete Recipe that models a single VPlan-level instruction.

llvm/lib/Transforms/Vectorize/VPlanHelpers.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,21 +394,40 @@ class VPSlotTracker {
394394
/// require slot tracking.
395395
std::unique_ptr<ModuleSlotTracker> MST;
396396

397+
/// Cached metadata kind names from the Module's LLVMContext.
398+
SmallVector<StringRef> MDNames;
399+
400+
/// Cached Module pointer for printing metadata.
401+
const Module *M = nullptr;
402+
397403
void assignName(const VPValue *V);
398404
LLVM_ABI_FOR_TEST void assignNames(const VPlan &Plan);
399405
void assignNames(const VPBasicBlock *VPBB);
400406
std::string getName(const Value *V);
401407

402408
public:
403409
VPSlotTracker(const VPlan *Plan = nullptr) {
404-
if (Plan)
410+
if (Plan) {
405411
assignNames(*Plan);
412+
if (auto *ScalarHeader = Plan->getScalarHeader())
413+
M = ScalarHeader->getIRBasicBlock()->getModule();
414+
}
406415
}
407416

408417
/// Returns the name assigned to \p V, if there is one, otherwise try to
409418
/// construct one from the underlying value, if there's one; else return
410419
/// <badref>.
411420
std::string getOrCreateName(const VPValue *V) const;
421+
422+
/// Returns the cached metadata kind names.
423+
ArrayRef<StringRef> getMDNames() {
424+
if (MDNames.empty() && M)
425+
M->getContext().getMDKindNames(MDNames);
426+
return MDNames;
427+
}
428+
429+
/// Returns the cached Module pointer.
430+
const Module *getModule() const { return M; }
412431
};
413432

414433
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ void VPRecipeBase::print(raw_ostream &O, const Twine &Indent,
377377
O << ", !dbg ";
378378
DL.print(O);
379379
}
380+
381+
if (auto *Metadata = dyn_cast<VPIRMetadata>(this))
382+
Metadata->print(O, SlotTracker);
380383
}
381384
#endif
382385

@@ -1599,6 +1602,25 @@ void VPIRMetadata::intersect(const VPIRMetadata &Other) {
15991602
Metadata = std::move(MetadataIntersection);
16001603
}
16011604

1605+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1606+
void VPIRMetadata::print(raw_ostream &O, VPSlotTracker &SlotTracker) const {
1607+
const Module *M = SlotTracker.getModule();
1608+
if (Metadata.empty() || !M)
1609+
return;
1610+
1611+
ArrayRef<StringRef> MDNames = SlotTracker.getMDNames();
1612+
O << " (";
1613+
interleaveComma(Metadata, O, [&](const auto &KindNodePair) {
1614+
auto [Kind, Node] = KindNodePair;
1615+
assert(Kind < MDNames.size() && !MDNames[Kind].empty() &&
1616+
"Unexpected unnamed metadata kind");
1617+
O << "!" << MDNames[Kind] << " ";
1618+
Node->printAsOperand(O, M);
1619+
});
1620+
O << ")";
1621+
}
1622+
#endif
1623+
16021624
void VPWidenCallRecipe::execute(VPTransformState &State) {
16031625
assert(State.VF.isVector() && "not widening");
16041626
assert(Variant != nullptr && "Can't create vector function.");

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ 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 ![[TBAA:[0-9]+]])
11+
; CHECK: WIDEN-CAST ir<%conv> = sitofp ir<%lv> to float (!fpmath ![[FPMATH:[0-9]+]])
12+
; CHECK: WIDEN ir<%mul> = fmul ir<%conv>, ir<2.000000e+00> (!fpmath ![[FPMATH]])
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 ![[TBAA]])
15+
; CHECK: ir-bb<loop>:
16+
; CHECK: IR %lv = load i32, ptr %gep.A, align 4, !tbaa ![[TBAA]]
17+
; CHECK: IR %conv = sitofp i32 %lv to float, !fpmath ![[FPMATH]]
18+
; CHECK: IR %mul = fmul float %conv, 2.000000e+00, !fpmath ![[FPMATH]]
19+
; CHECK: IR store i32 %conv.back, ptr %gep.B, align 4, !tbaa ![[TBAA]]
1520
;
1621
entry:
1722
br label %loop
@@ -40,9 +45,13 @@ define void @test_intrinsic_with_metadata(ptr noalias %A, ptr noalias %B, i32 %n
4045
; CHECK: VPlan 'Initial VPlan for VF={4},UF>=1' {
4146
; CHECK: <x1> vector loop: {
4247
; 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>
48+
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}> (!tbaa ![[TBAA2:[0-9]+]])
49+
; CHECK: WIDEN-INTRINSIC ir<%sqrt> = call llvm.sqrt(ir<%lv>) (!fpmath ![[FPMATH2:[0-9]+]])
50+
; CHECK: WIDEN store vp<{{.*}}>, ir<%sqrt> (!tbaa ![[TBAA2]])
51+
; CHECK: ir-bb<loop>:
52+
; CHECK: IR %lv = load float, ptr %gep.A, align 4, !tbaa ![[TBAA2]]
53+
; CHECK: IR %sqrt = call float @llvm.sqrt.f32(float %lv), !fpmath ![[FPMATH2]]
54+
; CHECK: IR store float %sqrt, ptr %gep.B, align 4, !tbaa ![[TBAA2]]
4655
;
4756
entry:
4857
br label %loop
@@ -67,11 +76,14 @@ define void @test_widen_with_multiple_metadata(ptr noalias %A, ptr noalias %B, i
6776
; CHECK: VPlan 'Initial VPlan for VF={4},UF>=1' {
6877
; CHECK: <x1> vector loop: {
6978
; CHECK: vector.body:
70-
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}>
79+
; CHECK: WIDEN ir<%lv> = load vp<{{.*}}> (!tbaa ![[TBAA3:[0-9]+]])
7180
; CHECK: WIDEN-CAST ir<%conv> = sitofp ir<%lv> to float
7281
; CHECK: WIDEN ir<%mul> = fmul ir<%conv>, ir<2.000000e+00>
7382
; CHECK: WIDEN-CAST ir<%conv.back> = fptosi ir<%mul> to i32
74-
; CHECK: WIDEN store vp<{{.*}}>, ir<%conv.back>
83+
; CHECK: WIDEN store vp<{{.*}}>, ir<%conv.back> (!tbaa ![[TBAA3]])
84+
; CHECK: ir-bb<loop>:
85+
; CHECK: IR %lv = load i32, ptr %gep.A, align 4, !tbaa ![[TBAA3]]
86+
; CHECK: IR store i32 %conv.back, ptr %gep.B, align 4, !tbaa ![[TBAA3]]
7587
;
7688
entry:
7789
br label %loop

llvm/unittests/Transforms/Vectorize/VPlanTestBase.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,21 @@ 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>("VPlanTestModule", C);
96+
FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), false);
97+
F = Function::Create(FTy, GlobalValue::ExternalLinkage, "f", M.get());
98+
ScalarHeader = BasicBlock::Create(C, "scalar.header", F);
99+
BranchInst::Create(ScalarHeader, ScalarHeader);
94100
}
95101

96102
VPlan &getPlan() {
97-
Plans.push_back(std::make_unique<VPlan>(&*ScalarHeader));
103+
Plans.push_back(std::make_unique<VPlan>(ScalarHeader));
98104
VPlan &Plan = *Plans.back();
99105
VPValue *DefaultTC = Plan.getConstantInt(32, 1024);
100106
Plan.setTripCount(DefaultTC);

0 commit comments

Comments
 (0)