[llvm] AlwaysInliner: Factor out some code in preparation for a future change. (PR #145614)

Amara Emerson via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 2 21:03:46 PDT 2025


https://github.com/aemerson updated https://github.com/llvm/llvm-project/pull/145614

>From 2fd7e07400a84561b9c8855c30fbe5cf0b4afc09 Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Tue, 24 Jun 2025 16:06:49 -0700
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5
---
 llvm/lib/Transforms/IPO/AlwaysInliner.cpp | 66 ++++++++++++++---------
 1 file changed, 40 insertions(+), 26 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
index 921fe8c18aa72..8de1467ea47ec 100644
--- a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
+++ b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
@@ -30,6 +30,43 @@ using namespace llvm;
 
 namespace {
 
+bool canInlineCallBase(CallBase *CB) {
+  return CB->hasFnAttr(Attribute::AlwaysInline) &&
+         !CB->getAttributes().hasFnAttr(Attribute::NoInline);
+}
+
+  bool attemptInlineFunction(
+      Function &F, CallBase *CB, bool InsertLifetime,
+      function_ref<AAResults &(Function &)> &GetAAR,
+      function_ref<AssumptionCache &(Function &)> &GetAssumptionCache,
+      ProfileSummaryInfo &PSI) {
+    Function *Caller = CB->getCaller();
+    OptimizationRemarkEmitter ORE(Caller);
+    DebugLoc DLoc = CB->getDebugLoc();
+    BasicBlock *Block = CB->getParent();
+
+    InlineFunctionInfo IFI(GetAssumptionCache, &PSI, nullptr, nullptr);
+    InlineResult Res = InlineFunction(*CB, IFI, /*MergeAttributes=*/true,
+                                      &GetAAR(F), InsertLifetime);
+    if (!Res.isSuccess()) {
+      ORE.emit([&]() {
+        return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
+              << "'" << ore::NV("Callee", &F) << "' is not inlined into '"
+              << ore::NV("Caller", Caller)
+              << "': " << ore::NV("Reason", Res.getFailureReason());
+      });
+      return false;
+    }
+
+    emitInlinedIntoBasedOnCost(ORE, DLoc, Block, F, *Caller,
+                              InlineCost::getAlways("always inline attribute"),
+                              /*ForProfileContext=*/false, DEBUG_TYPE);
+
+    return true;
+  }
+/// This function inlines all functions that are marked with the always_inline
+/// attribute. It also removes the inlined functions if they are dead after the
+/// inlining process.
 bool AlwaysInlineImpl(
     Module &M, bool InsertLifetime, ProfileSummaryInfo &PSI,
     FunctionAnalysisManager *FAM,
@@ -50,36 +87,13 @@ bool AlwaysInlineImpl(
 
     for (User *U : F.users())
       if (auto *CB = dyn_cast<CallBase>(U))
-        if (CB->getCalledFunction() == &F &&
-            CB->hasFnAttr(Attribute::AlwaysInline) &&
-            !CB->getAttributes().hasFnAttr(Attribute::NoInline))
+        if (CB->getCalledFunction() == &F && canInlineCallBase(CB))
           Calls.insert(CB);
 
     for (CallBase *CB : Calls) {
       Function *Caller = CB->getCaller();
-      OptimizationRemarkEmitter ORE(Caller);
-      DebugLoc DLoc = CB->getDebugLoc();
-      BasicBlock *Block = CB->getParent();
-
-      InlineFunctionInfo IFI(GetAssumptionCache, &PSI, nullptr, nullptr);
-      InlineResult Res = InlineFunction(*CB, IFI, /*MergeAttributes=*/true,
-                                        &GetAAR(F), InsertLifetime);
-      if (!Res.isSuccess()) {
-        ORE.emit([&]() {
-          return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
-                 << "'" << ore::NV("Callee", &F) << "' is not inlined into '"
-                 << ore::NV("Caller", Caller)
-                 << "': " << ore::NV("Reason", Res.getFailureReason());
-        });
-        continue;
-      }
-
-      emitInlinedIntoBasedOnCost(
-          ORE, DLoc, Block, F, *Caller,
-          InlineCost::getAlways("always inline attribute"),
-          /*ForProfileContext=*/false, DEBUG_TYPE);
-
-      Changed = true;
+      Changed |= attemptInlineFunction(F, CB, InsertLifetime, GetAAR,
+                                       GetAssumptionCache, PSI);
       if (FAM)
         FAM->invalidate(*Caller, PreservedAnalyses::none());
     }

>From 209fe80964da6985f1554e2202dc9fbcd8de7ae0 Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Tue, 24 Jun 2025 21:25:54 -0700
Subject: [PATCH 2/3] clang-format

Created using spr 1.3.6
---
 llvm/lib/Transforms/IPO/AlwaysInliner.cpp | 55 ++++++++++++-----------
 1 file changed, 28 insertions(+), 27 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
index 8de1467ea47ec..93aaa8c7f49b4 100644
--- a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
+++ b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
@@ -35,35 +35,36 @@ bool canInlineCallBase(CallBase *CB) {
          !CB->getAttributes().hasFnAttr(Attribute::NoInline);
 }
 
-  bool attemptInlineFunction(
-      Function &F, CallBase *CB, bool InsertLifetime,
-      function_ref<AAResults &(Function &)> &GetAAR,
-      function_ref<AssumptionCache &(Function &)> &GetAssumptionCache,
-      ProfileSummaryInfo &PSI) {
-    Function *Caller = CB->getCaller();
-    OptimizationRemarkEmitter ORE(Caller);
-    DebugLoc DLoc = CB->getDebugLoc();
-    BasicBlock *Block = CB->getParent();
-
-    InlineFunctionInfo IFI(GetAssumptionCache, &PSI, nullptr, nullptr);
-    InlineResult Res = InlineFunction(*CB, IFI, /*MergeAttributes=*/true,
-                                      &GetAAR(F), InsertLifetime);
-    if (!Res.isSuccess()) {
-      ORE.emit([&]() {
-        return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
-              << "'" << ore::NV("Callee", &F) << "' is not inlined into '"
-              << ore::NV("Caller", Caller)
-              << "': " << ore::NV("Reason", Res.getFailureReason());
-      });
-      return false;
-    }
+bool attemptInlineFunction(
+    Function &F, CallBase *CB, bool InsertLifetime,
+    function_ref<AAResults &(Function &)> &GetAAR,
+    function_ref<AssumptionCache &(Function &)> &GetAssumptionCache,
+    ProfileSummaryInfo &PSI) {
+  Function *Caller = CB->getCaller();
+  OptimizationRemarkEmitter ORE(Caller);
+  DebugLoc DLoc = CB->getDebugLoc();
+  BasicBlock *Block = CB->getParent();
+
+  InlineFunctionInfo IFI(GetAssumptionCache, &PSI, nullptr, nullptr);
+  InlineResult Res = InlineFunction(*CB, IFI, /*MergeAttributes=*/true,
+                                    &GetAAR(F), InsertLifetime);
+  if (!Res.isSuccess()) {
+    ORE.emit([&]() {
+      return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
+             << "'" << ore::NV("Callee", &F) << "' is not inlined into '"
+             << ore::NV("Caller", Caller)
+             << "': " << ore::NV("Reason", Res.getFailureReason());
+    });
+    return false;
+  }
 
-    emitInlinedIntoBasedOnCost(ORE, DLoc, Block, F, *Caller,
-                              InlineCost::getAlways("always inline attribute"),
-                              /*ForProfileContext=*/false, DEBUG_TYPE);
+  emitInlinedIntoBasedOnCost(ORE, DLoc, Block, F, *Caller,
+                             InlineCost::getAlways("always inline attribute"),
+                             /*ForProfileContext=*/false, DEBUG_TYPE);
+
+  return true;
+}
 
-    return true;
-  }
 /// This function inlines all functions that are marked with the always_inline
 /// attribute. It also removes the inlined functions if they are dead after the
 /// inlining process.

>From b47e93ec235c1b5644255b2dee3c487ad340429f Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Wed, 25 Jun 2025 21:32:35 -0700
Subject: [PATCH 3/3] Address comments.

Created using spr 1.3.6
---
 llvm/lib/Transforms/IPO/AlwaysInliner.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
index 93aaa8c7f49b4..cf85c799fb29b 100644
--- a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
+++ b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp
@@ -30,12 +30,15 @@ using namespace llvm;
 
 namespace {
 
-bool canInlineCallBase(CallBase *CB) {
+/// Sanity check for a call site's inlinability based on inline attributes.
+static bool canInlineCallBase(CallBase *CB) {
   return CB->hasFnAttr(Attribute::AlwaysInline) &&
          !CB->getAttributes().hasFnAttr(Attribute::NoInline);
 }
 
-bool attemptInlineFunction(
+/// Attempt to inline a call site \p CB into its caller.
+/// Returns true if the inlining was successful, false otherwise.
+static bool attemptInlineFunction(
     Function &F, CallBase *CB, bool InsertLifetime,
     function_ref<AAResults &(Function &)> &GetAAR,
     function_ref<AssumptionCache &(Function &)> &GetAssumptionCache,



More information about the llvm-commits mailing list