[llvm-branch-commits] [llvm] [NPM] Port ImmutableModuleSummaryAnalysis to NPM (PR #203510)

Vikram Hegde via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jun 17 23:17:41 PDT 2026


https://github.com/vikramRH updated https://github.com/llvm/llvm-project/pull/203510

>From 62e5c56f7539b73c22d1f7a51e37f6939556d4d9 Mon Sep 17 00:00:00 2001
From: vikhegde <vikram.hegde at amd.com>
Date: Thu, 4 Jun 2026 14:14:38 +0530
Subject: [PATCH] [NPM] Port ImmutableModuleSummaryAnalysis to NPM

---
 .../llvm/Analysis/ModuleSummaryAnalysis.h     | 29 +++++++++++++++++++
 llvm/lib/Analysis/ModuleSummaryAnalysis.cpp   |  1 +
 llvm/lib/Analysis/StackSafetyAnalysis.cpp     |  7 +++--
 llvm/lib/Passes/PassRegistry.def              |  2 ++
 4 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Analysis/ModuleSummaryAnalysis.h b/llvm/include/llvm/Analysis/ModuleSummaryAnalysis.h
index 7e78e9b9f2262..8d79ae418751a 100644
--- a/llvm/include/llvm/Analysis/ModuleSummaryAnalysis.h
+++ b/llvm/include/llvm/Analysis/ModuleSummaryAnalysis.h
@@ -92,6 +92,35 @@ class LLVM_ABI ImmutableModuleSummaryIndexWrapperPass : public ImmutablePass {
   void getAnalysisUsage(AnalysisUsage &AU) const override;
 };
 
+/// NPM analysis to provide an externally-built ModuleSummaryIndex (e.g. the
+/// combined index from LTO).
+class ImmutableModuleSummaryIndexAnalysis
+    : public AnalysisInfoMixin<ImmutableModuleSummaryIndexAnalysis> {
+  friend AnalysisInfoMixin<ImmutableModuleSummaryIndexAnalysis>;
+  LLVM_ABI static AnalysisKey Key;
+  const ModuleSummaryIndex *Index = nullptr;
+
+public:
+  class Result {
+    const ModuleSummaryIndex *Index;
+    Result(const ModuleSummaryIndex *Index) : Index(Index) {}
+    friend class ImmutableModuleSummaryIndexAnalysis;
+
+  public:
+    const ModuleSummaryIndex *getIndex() const { return Index; }
+    bool invalidate(Module &, const PreservedAnalyses &,
+                    ModuleAnalysisManager::Invalidator &) {
+      return false;
+    }
+  };
+
+  ImmutableModuleSummaryIndexAnalysis() = default;
+  ImmutableModuleSummaryIndexAnalysis(const ModuleSummaryIndex *Index)
+      : Index(Index) {}
+
+  Result run(Module &M, ModuleAnalysisManager &AM) { return Result(Index); }
+};
+
 //===--------------------------------------------------------------------===//
 //
 // ImmutableModuleSummaryIndexWrapperPass - This pass wrap provided
diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
index 0879fa627c709..eaeda67efcd20 100644
--- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
+++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
@@ -1151,6 +1151,7 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
 }
 
 AnalysisKey ModuleSummaryIndexAnalysis::Key;
+AnalysisKey ImmutableModuleSummaryIndexAnalysis::Key;
 
 ModuleSummaryIndex
 ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
diff --git a/llvm/lib/Analysis/StackSafetyAnalysis.cpp b/llvm/lib/Analysis/StackSafetyAnalysis.cpp
index 024a30f10e8f0..9c20019677895 100644
--- a/llvm/lib/Analysis/StackSafetyAnalysis.cpp
+++ b/llvm/lib/Analysis/StackSafetyAnalysis.cpp
@@ -1068,14 +1068,17 @@ AnalysisKey StackSafetyGlobalAnalysis::Key;
 
 StackSafetyGlobalInfo
 StackSafetyGlobalAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
-  // FIXME: Lookup Module Summary.
   FunctionAnalysisManager &FAM =
       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
+  const ModuleSummaryIndex *Index = nullptr;
+  if (auto *IndexPass =
+          AM.getCachedResult<ImmutableModuleSummaryIndexAnalysis>(M))
+    Index = IndexPass->getIndex();
   return {&M,
           [&FAM](Function &F) -> const StackSafetyInfo & {
             return FAM.getResult<StackSafetyAnalysis>(F);
           },
-          nullptr};
+          Index};
 }
 
 PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M,
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 8f70c7cefa408..19b03187642ae 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -31,6 +31,8 @@ MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
 MODULE_ANALYSIS("last-run-tracking", LastRunTrackingAnalysis())
 MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
 MODULE_ANALYSIS("libcall-lowering-info", LibcallLoweringModuleAnalysis())
+MODULE_ANALYSIS("immutable-module-summary",
+                ImmutableModuleSummaryIndexAnalysis())
 MODULE_ANALYSIS("module-summary", ModuleSummaryIndexAnalysis())
 MODULE_ANALYSIS("no-op-module", NoOpModuleAnalysis())
 MODULE_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))



More information about the llvm-branch-commits mailing list