Skip to content

Commit c5ee379

Browse files
committed
[ctx_prof] API to get the instrumentation of a BB
1 parent 1951b39 commit c5ee379

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

llvm/include/llvm/Analysis/CtxProfAnalysis.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ class CtxProfAnalysis : public AnalysisInfoMixin<CtxProfAnalysis> {
9595

9696
PGOContextualProfile run(Module &M, ModuleAnalysisManager &MAM);
9797

98+
/// Get the instruction instrumenting a callsite, or nullptr if that cannot be
99+
/// found.
98100
static InstrProfCallsite *getCallsiteInstrumentation(CallBase &CB);
101+
102+
/// Get the instruction instrumenting a BB, or nullptr if not present.
103+
static InstrProfIncrementInst *getBBInstrumentation(BasicBlock &BB);
99104
};
100105

101106
class CtxProfAnalysisPrinterPass

llvm/lib/Analysis/CtxProfAnalysis.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ InstrProfCallsite *CtxProfAnalysis::getCallsiteInstrumentation(CallBase &CB) {
202202
return nullptr;
203203
}
204204

205+
InstrProfIncrementInst *CtxProfAnalysis::getBBInstrumentation(BasicBlock &BB) {
206+
for (auto &I : BB)
207+
if (auto *Incr = dyn_cast<InstrProfIncrementInst>(&I))
208+
return Incr;
209+
return nullptr;
210+
}
211+
205212
static void
206213
preorderVisit(const PGOCtxProfContext::CallTargetMapTy &Profiles,
207214
function_ref<void(const PGOCtxProfContext &)> Visitor) {

llvm/unittests/Analysis/CtxProfAnalysisTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,26 @@ TEST_F(CtxProfAnalysisTest, GetCallsiteIDNegativeTest) {
132132
EXPECT_EQ(IndIns, nullptr);
133133
}
134134

135+
TEST_F(CtxProfAnalysisTest, GetBBIDTest) {
136+
ModulePassManager MPM;
137+
MPM.addPass(PGOInstrumentationGen(PGOInstrumentationType::CTXPROF));
138+
EXPECT_FALSE(MPM.run(*M, MAM).areAllPreserved());
139+
auto *F = M->getFunction("foo");
140+
ASSERT_NE(F, nullptr);
141+
std::map<std::string, int> BBNameAndID;
142+
143+
for (auto &BB : *F) {
144+
auto *Ins = CtxProfAnalysis::getBBInstrumentation(BB);
145+
if (Ins)
146+
BBNameAndID[BB.getName().str()] =
147+
static_cast<int>(Ins->getIndex()->getZExtValue());
148+
else
149+
BBNameAndID[BB.getName().str()] = -1;
150+
}
151+
152+
EXPECT_THAT(BBNameAndID,
153+
testing::UnorderedElementsAre(
154+
testing::Pair("", 0), testing::Pair("yes", 1),
155+
testing::Pair("no", -1), testing::Pair("exit", -1)));
156+
}
135157
} // namespace

0 commit comments

Comments
 (0)