[llvm] [AlwaysInline] Avoid unnecessary BFI fetches (PR #117750)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 26 12:47:28 PST 2024


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/117750

>From 274dcdc2e875ae9af4b332d33833aa6f04b31363 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 26 Nov 2024 16:40:58 +0100
Subject: [PATCH 1/2] avoid bfi calculation in alwaysinline

---
 llvm/lib/Transforms/IPO/AlwaysInliner.cpp | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
index 1f787c733079e9..447a0f492775da 100644
--- a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
+++ b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
@@ -34,7 +34,8 @@ bool AlwaysInlineImpl(
     Module &M, bool InsertLifetime, ProfileSummaryInfo &PSI,
     function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
     function_ref<AAResults &(Function &)> GetAAR,
-    function_ref<BlockFrequencyInfo &(Function &)> GetBFI) {
+    function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
+    function_ref<BlockFrequencyInfo *(Function &)> GetCachedBFI) {
   SmallSetVector<CallBase *, 16> Calls;
   bool Changed = false;
   SmallVector<Function *, 16> InlinedComdatFunctions;
@@ -61,9 +62,11 @@ bool AlwaysInlineImpl(
       DebugLoc DLoc = CB->getDebugLoc();
       BasicBlock *Block = CB->getParent();
 
-      InlineFunctionInfo IFI(GetAssumptionCache, &PSI,
-                             GetBFI ? &GetBFI(*Caller) : nullptr,
-                             GetBFI ? &GetBFI(F) : nullptr);
+      // Only update CallerBFI if already available. The CallerBFI update
+      // requires CalleeBFI.
+      BlockFrequencyInfo *CallerBFI = GetCachedBFI(*Caller);
+      InlineFunctionInfo IFI(GetAssumptionCache, &PSI, CallerBFI,
+                             CallerBFI ? &GetBFI(F) : nullptr);
 
       InlineResult Res = InlineFunction(*CB, IFI, /*MergeAttributes=*/true,
                                         &GetAAR(F), InsertLifetime);
@@ -133,9 +136,12 @@ struct AlwaysInlinerLegacyPass : public ModulePass {
     auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
       return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
     };
+    auto GetCachedBFI = [&](Function &) -> BlockFrequencyInfo * {
+      return nullptr;
+    };
 
     return AlwaysInlineImpl(M, InsertLifetime, PSI, GetAssumptionCache, GetAAR,
-                            /*GetBFI*/ nullptr);
+                            /*GetBFI=*/nullptr, GetCachedBFI);
   }
 
   static char ID; // Pass identification, replacement for typeid
@@ -172,13 +178,16 @@ PreservedAnalyses AlwaysInlinerPass::run(Module &M,
   auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
     return FAM.getResult<BlockFrequencyAnalysis>(F);
   };
+  auto GetCachedBFI = [&](Function &F) -> BlockFrequencyInfo * {
+    return FAM.getCachedResult<BlockFrequencyAnalysis>(F);
+  };
   auto GetAAR = [&](Function &F) -> AAResults & {
     return FAM.getResult<AAManager>(F);
   };
   auto &PSI = MAM.getResult<ProfileSummaryAnalysis>(M);
 
   bool Changed = AlwaysInlineImpl(M, InsertLifetime, PSI, GetAssumptionCache,
-                                  GetAAR, GetBFI);
+                                  GetAAR, GetBFI, GetCachedBFI);
 
   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
 }

>From 589d09d577c37b68c908a6b4d44d8c9e4d3432fd Mon Sep 17 00:00:00 2001
From: Nikita Popov <github at npopov.com>
Date: Tue, 26 Nov 2024 21:47:21 +0100
Subject: [PATCH 2/2] Update llvm/lib/Transforms/IPO/AlwaysInliner.cpp

Co-authored-by: Florian Hahn <flo at fhahn.com>
---
 llvm/lib/Transforms/IPO/AlwaysInliner.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
index 447a0f492775da..0baa34d50abf35 100644
--- a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
+++ b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
@@ -136,7 +136,7 @@ struct AlwaysInlinerLegacyPass : public ModulePass {
     auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
       return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
     };
-    auto GetCachedBFI = [&](Function &) -> BlockFrequencyInfo * {
+    auto GetCachedBFI = [](Function &) -> BlockFrequencyInfo * {
       return nullptr;
     };
 



More information about the llvm-commits mailing list