Skip to content

AlwaysInliner: Factor out some code in preparation for a future change. #145614

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

aemerson
Copy link
Contributor

No description provided.

Created using spr 1.3.5
@llvmbot
Copy link
Member

llvmbot commented Jun 24, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Amara Emerson (aemerson)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/145614.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/AlwaysInliner.cpp (+40-26)
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());
     }

Copy link

github-actions bot commented Jun 24, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Created using spr 1.3.6
@@ -30,6 +30,44 @@ using namespace llvm;

namespace {

bool canInlineCallBase(CallBase *CB) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bool canInlineCallBase(CallBase *CB) {
static bool canInlineCallBase(CallBase *CB) {

Could you also add a brief doc-comment?

!CB->getAttributes().hasFnAttr(Attribute::NoInline);
}

bool attemptInlineFunction(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bool attemptInlineFunction(
static bool attemptInlineFunction(

Could you also add a brief doc-comment?

Created using spr 1.3.6
Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Created using spr 1.3.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants