Skip to content

Commit 5e8f93b

Browse files
committed
!fixup use SlotTracker
1 parent e783ed3 commit 5e8f93b

File tree

5 files changed

+58
-29
lines changed

5 files changed

+58
-29
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ class VPIRMetadata {
10231023

10241024
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
10251025
/// Print metadata with node IDs.
1026-
void print(raw_ostream &O, const VPlan *Plan) const;
1026+
void print(raw_ostream &O, VPSlotTracker &SlotTracker) const;
10271027
#endif
10281028
};
10291029

@@ -4438,11 +4438,6 @@ class VPlan {
44384438
/// Return the VPIRBasicBlock wrapping the header of the scalar loop.
44394439
VPIRBasicBlock *getScalarHeader() const { return ScalarHeader; }
44404440

4441-
/// Return the Module from the scalar header.
4442-
const Module &getModule() const {
4443-
return *ScalarHeader->getIRBasicBlock()->getModule();
4444-
}
4445-
44464441
/// Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of
44474442
/// the original scalar loop.
44484443
ArrayRef<VPIRBasicBlock *> getExitBlocks() const { return ExitBlocks; }

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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ void VPRecipeBase::print(raw_ostream &O, const Twine &Indent,
510510

511511
if (auto *Metadata = dyn_cast<VPIRMetadata>(this)) {
512512
if (const VPBasicBlock *Parent = getParent())
513-
Metadata->print(O, Parent->getPlan());
513+
Metadata->print(O, SlotTracker);
514514
}
515515
}
516516
#endif
@@ -1712,22 +1712,19 @@ void VPIRMetadata::intersect(const VPIRMetadata &Other) {
17121712
}
17131713

17141714
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1715-
void VPIRMetadata::print(raw_ostream &O, const VPlan *Plan) const {
1716-
if (Metadata.empty() || !Plan)
1715+
void VPIRMetadata::print(raw_ostream &O, VPSlotTracker &SlotTracker) const {
1716+
const Module *M = SlotTracker.getModule();
1717+
if (Metadata.empty() || !M)
17171718
return;
17181719

1719-
const Module &M = Plan->getModule();
1720-
SmallVector<StringRef, 8> MDNames;
1721-
M.getContext().getMDKindNames(MDNames);
1722-
1720+
ArrayRef<StringRef> MDNames = SlotTracker.getMDNames();
17231721
O << " (";
17241722
interleaveComma(Metadata, O, [&](const auto &KindNodePair) {
17251723
auto [Kind, Node] = KindNodePair;
1726-
assert(Kind != 0 && "Debug metadata should not be managed by VPIRMetadata");
17271724
assert(Kind < MDNames.size() && !MDNames[Kind].empty() &&
17281725
"Unexpected unnamed metadata kind");
17291726
O << "!" << MDNames[Kind] << " ";
1730-
Node->printAsOperand(O, &M);
1727+
Node->printAsOperand(O, M);
17311728
});
17321729
O << ")";
17331730
}

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<{{.*}}> (!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]+}})
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> (!tbaa !{{[0-9]+}})
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<{{.*}}> (!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]+}})
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<{{.*}}> (!tbaa !{{[0-9]+}})
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> (!tbaa !{{[0-9]+}})
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)