[llvm-branch-commits] [llvm] FunctionAttrs: Only invalidate analyses of changed functions in norecurse LTO inference (PR #244818)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 20 20:56:15 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

<details>
<summary>Changes</summary>

NoRecurseLTOInferencePass only adds norecurse, so invalidate only the
changed functions and their direct callers instead of every function
analysis.

Co-authored-by: Claude Opus 5.5 <noreply@<!-- -->anthropic.com>

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


2 Files Affected:

- (modified) llvm/lib/Transforms/IPO/FunctionAttrs.cpp (+9-4) 
- (added) llvm/test/Transforms/FunctionAttrs/norecurse-lto-preserve-function-analyses.ll (+38) 


``````````diff
diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index 1cfe62b8cadb1..4c50f45e7e186 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -2500,6 +2500,8 @@ PreservedAnalyses NoRecurseLTOInferencePass::run(Module &M,
   // module.
   bool Changed = false;
   LazyCallGraph &CG = MAM.getResult<LazyCallGraphAnalysis>(M);
+  FunctionAnalysisManager &FAM =
+      MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
   CG.buildRefSCCs();
 
   for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs()) {
@@ -2525,14 +2527,17 @@ PreservedAnalyses NoRecurseLTOInferencePass::run(Module &M,
     if (!mayHaveRecursiveCallee(F, AnyFunctionsAddressIsTaken)) {
       F.setDoesNotRecurse();
       ++NumNoRecurse;
+      invalidateChangedFunction(FAM, F);
       Changed = true;
     }
   }
 
+  if (!Changed)
+    return PreservedAnalyses::all();
+
   PreservedAnalyses PA;
-  if (Changed)
-    PA.preserve<LazyCallGraphAnalysis>();
-  else
-    PA = PreservedAnalyses::all();
+  PA.preserve<LazyCallGraphAnalysis>();
+  PA.preserve<FunctionAnalysisManagerModuleProxy>();
+  PA.preserveSet<AllAnalysesOn<Function>>();
   return PA;
 }
diff --git a/llvm/test/Transforms/FunctionAttrs/norecurse-lto-preserve-function-analyses.ll b/llvm/test/Transforms/FunctionAttrs/norecurse-lto-preserve-function-analyses.ll
new file mode 100644
index 0000000000000..24eb63d2592a5
--- /dev/null
+++ b/llvm/test/Transforms/FunctionAttrs/norecurse-lto-preserve-function-analyses.ll
@@ -0,0 +1,38 @@
+; RUN: opt -disable-output -debug-pass-manager -passes='function(require<domtree>,require<lazy-value-info>,require<target-lib-info>,require<target-ir>),norecurse-lto-inference,function(require<domtree>,require<lazy-value-info>,require<target-lib-info>,require<target-ir>)' %s 2>&1 | FileCheck %s
+
+; Inferring norecurse on @callee and @caller should only invalidate the non-CFG
+; analyses of those functions, other than TargetLibraryInfo and
+; TargetTransformInfo, and not the analyses of @other.
+
+; CHECK: Running pass: NoRecurseLTOInferencePass on [module]{{$}}
+; CHECK-NEXT: Running analysis: LazyCallGraphAnalysis on [module]{{$}}
+; CHECK-NEXT: Invalidating analysis: LazyValueAnalysis on callee{{$}}
+; CHECK-NEXT: Invalidating analysis: LazyValueAnalysis on caller{{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}DominatorTreeAnalysis{{.*}}> on callee (1 instruction){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}LazyValueAnalysis{{.*}}> on callee (1 instruction){{$}}
+; CHECK-NEXT: Running analysis: LazyValueAnalysis on callee{{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}TargetLibraryAnalysis{{.*}}> on callee (1 instruction){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}TargetIRAnalysis{{.*}}> on callee (1 instruction){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}DominatorTreeAnalysis{{.*}}> on caller (2 instructions){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}LazyValueAnalysis{{.*}}> on caller (2 instructions){{$}}
+; CHECK-NEXT: Running analysis: LazyValueAnalysis on caller{{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}TargetLibraryAnalysis{{.*}}> on caller (2 instructions){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}TargetIRAnalysis{{.*}}> on caller (2 instructions){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}DominatorTreeAnalysis{{.*}}> on other (1 instruction){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}LazyValueAnalysis{{.*}}> on other (1 instruction){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}TargetLibraryAnalysis{{.*}}> on other (1 instruction){{$}}
+; CHECK-NEXT: Running pass: RequireAnalysisPass<{{.*}}TargetIRAnalysis{{.*}}> on other (1 instruction){{$}}
+; CHECK-NEXT: Running pass: VerifierPass on [module]{{$}}
+
+define internal void @callee() {
+  ret void
+}
+
+define void @caller() {
+  call void @callee()
+  ret void
+}
+
+define void @other() norecurse {
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/244818


More information about the llvm-branch-commits mailing list