[llvm-branch-commits] [llvm] EliminateAvailableExternally: Preserve the function analysis manager proxy (PR #244023)

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 18 14:52:19 PST 2026


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/244023

>From 5b2b95bed5428721dd33f95645e49f3cf80c31e9 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Wed, 18 Nov 2026 08:00:59 -0800
Subject: [PATCH 1/2] EliminateAvailableExternally: Preserve the function
 analysis manager proxy

Previously all cached function analyses were cleared When only
deleting function bodies, clear the analyses of those functions and
preserve the proxy.

CTMark instructions:u geomean: O3 -0.02% (kimwitu++ -0.12%, tramp3d-v4
-0.12%), ThinLTO -0.01%.

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/lib/Transforms/IPO/ElimAvailExtern.cpp | 25 ++++++++++++++++-----
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
index d41df598580c44..5c11fa90ab0a5e 100644
--- a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
+++ b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
@@ -101,7 +101,8 @@ static void convertToLocalCopy(Module &M, GlobalVariable &GV) {
   ++NumGlobalVariablesConverted;
 }
 
-static bool eliminateAvailableExternally(Module &M, bool Convert) {
+static bool eliminateAvailableExternally(Module &M, bool Convert,
+                                         FunctionAnalysisManager &FAM) {
   const IPOOptions &Opts = getIPOOptions(M);
   bool Changed = false;
 
@@ -133,10 +134,12 @@ static bool eliminateAvailableExternally(Module &M, bool Convert) {
     if (F.isDeclaration() || !F.hasAvailableExternallyLinkage())
       continue;
 
-    if (Convert || Opts.avail_extern_to_local)
+    if (Convert || Opts.avail_extern_to_local) {
       convertToLocalCopy(M, F);
-    else
+    } else {
+      FAM.clear(F, F.getName());
       deleteFunction(F);
+    }
 
     F.removeDeadConstantUsers();
     Changed = true;
@@ -154,8 +157,18 @@ EliminateAvailableExternallyPass::run(Module &M, ModuleAnalysisManager &MAM) {
   // that's imported, its optimizations will, thus, differ, and be specialized
   // for this contextual information. Eliding it in favor of the original would
   // undo these optimizations.
-  if (!eliminateAvailableExternally(
-          M, /*Convert=*/(CtxProf && CtxProf->isInSpecializedModule())))
+  bool Convert = CtxProf && CtxProf->isInSpecializedModule();
+  FunctionAnalysisManager &FAM =
+      MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
+  if (!eliminateAvailableExternally(M, Convert, FAM))
     return PreservedAnalyses::all();
-  return PreservedAnalyses::none();
+
+  if (Convert || getIPOOptions(M).avail_extern_to_local)
+    return PreservedAnalyses::none();
+
+  // Only function bodies were deleted, and their analyses were cleared.
+  PreservedAnalyses PA;
+  PA.preserve<FunctionAnalysisManagerModuleProxy>();
+  PA.preserveSet<CFGAnalyses>();
+  return PA;
 }

>From 0c0511303aa3f90985683b40f400697bbdf16176 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Wed, 18 Nov 2026 14:32:26 -0800
Subject: [PATCH 2/2] Fold avail_extern_to_local into Convert

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/lib/Transforms/IPO/ElimAvailExtern.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
index 5c11fa90ab0a5e..4493a35d7f9b83 100644
--- a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
+++ b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
@@ -134,7 +134,7 @@ static bool eliminateAvailableExternally(Module &M, bool Convert,
     if (F.isDeclaration() || !F.hasAvailableExternallyLinkage())
       continue;
 
-    if (Convert || Opts.avail_extern_to_local) {
+    if (Convert) {
       convertToLocalCopy(M, F);
     } else {
       FAM.clear(F, F.getName());
@@ -157,13 +157,14 @@ EliminateAvailableExternallyPass::run(Module &M, ModuleAnalysisManager &MAM) {
   // that's imported, its optimizations will, thus, differ, and be specialized
   // for this contextual information. Eliding it in favor of the original would
   // undo these optimizations.
-  bool Convert = CtxProf && CtxProf->isInSpecializedModule();
+  bool Convert = (CtxProf && CtxProf->isInSpecializedModule()) ||
+                 getIPOOptions(M).avail_extern_to_local;
   FunctionAnalysisManager &FAM =
       MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
   if (!eliminateAvailableExternally(M, Convert, FAM))
     return PreservedAnalyses::all();
 
-  if (Convert || getIPOOptions(M).avail_extern_to_local)
+  if (Convert)
     return PreservedAnalyses::none();
 
   // Only function bodies were deleted, and their analyses were cleared.



More information about the llvm-branch-commits mailing list