[llvm-branch-commits] [llvm] ab326ac - [llvm][NFC] Cache FAM in InlineAdvisor

Mircea Trofin via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jun 1 12:39:25 PDT 2020


Author: Mircea Trofin
Date: 2020-06-01T12:38:53-07:00
New Revision: ab326ac96ecb0379b8fe268b43e65a9115bb634f

URL: https://github.com/llvm/llvm-project/commit/ab326ac96ecb0379b8fe268b43e65a9115bb634f
DIFF: https://github.com/llvm/llvm-project/commit/ab326ac96ecb0379b8fe268b43e65a9115bb634f.diff

LOG: [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

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/InlineAdvisor.h
    llvm/include/llvm/Transforms/IPO/Inliner.h
    llvm/lib/Analysis/InlineAdvisor.cpp
    llvm/lib/Transforms/IPO/Inliner.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/InlineAdvisor.h b/llvm/include/llvm/Analysis/InlineAdvisor.h
index ac8e7c20429d..e9f08a69a5af 100644
--- a/llvm/include/llvm/Analysis/InlineAdvisor.h
+++ b/llvm/include/llvm/Analysis/InlineAdvisor.h
@@ -116,8 +116,7 @@ class InlineAdvisor {
   /// 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 @@ class InlineAdvisor {
   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 @@ class InlineAdvisor {
 /// 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 @@ class InlineAdvisorAnalysis : public AnalysisInfoMixin<InlineAdvisorAnalysis> {
   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 @@ class InlineAdvisorAnalysis : public AnalysisInfoMixin<InlineAdvisorAnalysis> {
     void clear() { Advisor.reset(); }
 
   private:
+    Module &M;
+    ModuleAnalysisManager &MAM;
     std::unique_ptr<InlineAdvisor> Advisor;
   };
 

diff  --git a/llvm/include/llvm/Transforms/IPO/Inliner.h b/llvm/include/llvm/Transforms/IPO/Inliner.h
index 447616102c0d..3454b0af0d9f 100644
--- a/llvm/include/llvm/Transforms/IPO/Inliner.h
+++ b/llvm/include/llvm/Transforms/IPO/Inliner.h
@@ -106,7 +106,7 @@ class InlinerPass : public PassInfoMixin<InlinerPass> {
 
 private:
   InlineAdvisor &getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
-                            Module &M);
+                            FunctionAnalysisManager &FAM, Module &M);
   std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
   Optional<DefaultInlineAdvisor> OwnedDefaultAdvisor;
 };

diff  --git a/llvm/lib/Analysis/InlineAdvisor.cpp b/llvm/lib/Analysis/InlineAdvisor.cpp
index ac3ba451aa3f..7af8a4dffcba 100644
--- a/llvm/lib/Analysis/InlineAdvisor.cpp
+++ b/llvm/lib/Analysis/InlineAdvisor.cpp
@@ -90,8 +90,7 @@ class DefaultInlineAdvice : public InlineAdvice {
 
 } // 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.

diff  --git a/llvm/lib/Transforms/IPO/Inliner.cpp b/llvm/lib/Transforms/IPO/Inliner.cpp
index 862385d04481..b0958199ef22 100644
--- a/llvm/lib/Transforms/IPO/Inliner.cpp
+++ b/llvm/lib/Transforms/IPO/Inliner.cpp
@@ -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();


        


More information about the llvm-branch-commits mailing list