[llvm][NFC] Cache FAM in InlineAdvisor

Summary:
This simplifies the interface by storing the function analysis manager
with the InlineAdvisor, and, thus, not requiring it be passed each time
we inquire for an advice.

Reviewers: davidxl, asbirlea

Subscribers: eraman, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D80405
This commit is contained in:
Mircea Trofin 2020-05-21 08:40:49 -07:00
parent a05f1e5ae4
commit 999ea25a9e
4 changed files with 27 additions and 19 deletions

View File

@ -116,8 +116,7 @@ public:
/// inline or not. \p CB is assumed to be a direct call. \p FAM is assumed to
/// be up-to-date wrt previous inlining decisions.
/// Returns an InlineAdvice with the inlining recommendation.
virtual std::unique_ptr<InlineAdvice>
getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) = 0;
virtual std::unique_ptr<InlineAdvice> getAdvice(CallBase &CB) = 0;
/// This must be called when the Inliner pass is entered, to allow the
/// InlineAdvisor update internal state, as result of function passes run
@ -130,7 +129,9 @@ public:
virtual void onPassExit() {}
protected:
InlineAdvisor() = default;
InlineAdvisor(FunctionAnalysisManager &FAM) : FAM(FAM) {}
FunctionAnalysisManager &FAM;
/// We may want to defer deleting functions to after the inlining for a whole
/// module has finished. This allows us to reliably use function pointers as
@ -156,13 +157,14 @@ private:
/// reusable as-is for inliner pass test scenarios, as well as for regular use.
class DefaultInlineAdvisor : public InlineAdvisor {
public:
DefaultInlineAdvisor(InlineParams Params) : Params(Params) {}
DefaultInlineAdvisor(FunctionAnalysisManager &FAM, InlineParams Params)
: InlineAdvisor(FAM), Params(Params) {}
private:
std::unique_ptr<InlineAdvice>
getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) override;
std::unique_ptr<InlineAdvice> getAdvice(CallBase &CB) override;
void onPassExit() override { freeDeletedFunctions(); }
InlineParams Params;
};
@ -173,7 +175,7 @@ public:
static AnalysisKey Key;
InlineAdvisorAnalysis() = default;
struct Result {
Result(Module &M, ModuleAnalysisManager &MAM) {}
Result(Module &M, ModuleAnalysisManager &MAM) : M(M), MAM(MAM) {}
bool invalidate(Module &, const PreservedAnalyses &,
ModuleAnalysisManager::Invalidator &) {
// InlineAdvisor must be preserved across analysis invalidations.
@ -184,6 +186,8 @@ public:
void clear() { Advisor.reset(); }
private:
Module &M;
ModuleAnalysisManager &MAM;
std::unique_ptr<InlineAdvisor> Advisor;
};

View File

@ -106,7 +106,7 @@ public:
private:
InlineAdvisor &getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
Module &M);
FunctionAnalysisManager &FAM, Module &M);
std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
Optional<DefaultInlineAdvisor> OwnedDefaultAdvisor;
};

View File

@ -90,8 +90,7 @@ private:
} // namespace
std::unique_ptr<InlineAdvice>
DefaultInlineAdvisor::getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) {
std::unique_ptr<InlineAdvice> DefaultInlineAdvisor::getAdvice(CallBase &CB) {
Function &Caller = *CB.getCaller();
ProfileSummaryInfo *PSI =
FAM.getResult<ModuleAnalysisManagerFunctionProxy>(Caller)
@ -149,9 +148,10 @@ AnalysisKey InlineAdvisorAnalysis::Key;
bool InlineAdvisorAnalysis::Result::tryCreate(InlineParams Params,
InliningAdvisorMode Mode) {
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
switch (Mode) {
case InliningAdvisorMode::Default:
Advisor.reset(new DefaultInlineAdvisor(Params));
Advisor.reset(new DefaultInlineAdvisor(FAM, Params));
break;
case InliningAdvisorMode::Development:
// To be added subsequently under conditional compilation.

View File

@ -668,14 +668,18 @@ InlinerPass::~InlinerPass() {
InlineAdvisor &
InlinerPass::getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
Module &M) {
FunctionAnalysisManager &FAM, Module &M) {
auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
if (!IAA) {
// It should still be possible to run the inliner as a stand-alone SCC pass,
// for test scenarios. In that case, we default to the
// DefaultInlineAdvisor, which doesn't need to keep state between SCC pass
// runs. It also uses just the default InlineParams.
OwnedDefaultAdvisor.emplace(getInlineParams());
// In this case, we need to use the provided FAM, which is valid for the
// duration of the inliner pass, and thus the lifetime of the owned advisor.
// The one we would get from the MAM can be invalidated as a result of the
// inliner's activity.
OwnedDefaultAdvisor.emplace(FAM, getInlineParams());
return *OwnedDefaultAdvisor;
}
assert(IAA->getAdvisor() &&
@ -695,7 +699,11 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
Module &M = *InitialC.begin()->getFunction().getParent();
ProfileSummaryInfo *PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(M);
InlineAdvisor &Advisor = getAdvisor(MAMProxy, M);
FunctionAnalysisManager &FAM =
AM.getResult<FunctionAnalysisManagerCGSCCProxy>(InitialC, CG)
.getManager();
InlineAdvisor &Advisor = getAdvisor(MAMProxy, FAM, M);
Advisor.onPassEntry();
auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
@ -733,10 +741,6 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
// incrementally maknig a single function grow in a super linear fashion.
SmallVector<std::pair<CallBase *, int>, 16> Calls;
FunctionAnalysisManager &FAM =
AM.getResult<FunctionAnalysisManagerCGSCCProxy>(InitialC, CG)
.getManager();
// Populate the initial list of calls in this SCC.
for (auto &N : InitialC) {
auto &ORE =
@ -838,7 +842,7 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
continue;
}
auto Advice = Advisor.getAdvice(*CB, FAM);
auto Advice = Advisor.getAdvice(*CB);
// Check whether we want to inline this callsite.
if (!Advice->isInliningRecommended()) {
Advice->recordUnattemptedInlining();