Skip to content

[ctx_prof] API to get the instrumentation of a BB #105468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/include/llvm/Analysis/CtxProfAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think explicitly typing Instruction would improve readability a bit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why, this is a typical iteration pattern for BBs (doing a very naive search though llvm/lib for either patterns - i.e. for (auto &I : BB) or for (Instruction &I : BB) reveals about the same nr of instances.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a test and doesn't matter much, but should we more carefully choose the container here? It doesn't seem like the iteration order matters, so something like std::unordered_map might be better.


for (auto &BB : *F) {
auto *Ins = CtxProfAnalysis::getBBInstrumentation(BB);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly type this instruction?

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
Loading