Skip to content

Commit

Permalink
[ctx_prof] API to get the instrumentation of a BB
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrofin committed Aug 21, 2024
1 parent 2093b4c commit f57aaa6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llvm/include/llvm/Analysis/CtxProfAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ class CtxProfAnalysis : public AnalysisInfoMixin<CtxProfAnalysis> {

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

/// Get the instruction instrumenting a callsite, or nullptr if that cannot be
/// found.
static InstrProfCallsite *getCallsiteInstrumentation(CallBase &CB);

/// Get the instruction instrumenting a BB, or nullptr if not present.
static InstrProfIncrementInst *getBBInstrumentation(BasicBlock &BB);
};

class CtxProfAnalysisPrinterPass
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Analysis/CtxProfAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ InstrProfCallsite *CtxProfAnalysis::getCallsiteInstrumentation(CallBase &CB) {
return nullptr;
}

InstrProfIncrementInst *CtxProfAnalysis::getBBInstrumentation(BasicBlock &BB) {
for (auto &I : BB)
if (auto *Incr = dyn_cast<InstrProfIncrementInst>(&I))
return Incr;
return nullptr;
}

static void
preorderVisit(const PGOCtxProfContext::CallTargetMapTy &Profiles,
function_ref<void(const PGOCtxProfContext &)> Visitor) {
Expand Down
22 changes: 22 additions & 0 deletions llvm/unittests/Analysis/CtxProfAnalysisTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,26 @@ TEST_F(CtxProfAnalysisTest, GetCallsiteIDNegativeTest) {
EXPECT_EQ(IndIns, nullptr);
}

TEST_F(CtxProfAnalysisTest, GetBBIDTest) {
ModulePassManager MPM;
MPM.addPass(PGOInstrumentationGen(PGOInstrumentationType::CTXPROF));
EXPECT_FALSE(MPM.run(*M, MAM).areAllPreserved());
auto *F = M->getFunction("foo");
ASSERT_NE(F, nullptr);
std::map<std::string, int> BBNameAndID;

for (auto &BB : *F) {
auto *Ins = CtxProfAnalysis::getBBInstrumentation(BB);
if (Ins)
BBNameAndID[BB.getName().str()] =
static_cast<int>(Ins->getIndex()->getZExtValue());
else
BBNameAndID[BB.getName().str()] = -1;
}

EXPECT_THAT(BBNameAndID,
testing::UnorderedElementsAre(
testing::Pair("", 0), testing::Pair("yes", 1),
testing::Pair("no", -1), testing::Pair("exit", -1)));
}
} // namespace

0 comments on commit f57aaa6

Please sign in to comment.