Index: include/llvm/Analysis/ModuleSummaryAnalysis.h =================================================================== --- include/llvm/Analysis/ModuleSummaryAnalysis.h +++ include/llvm/Analysis/ModuleSummaryAnalysis.h @@ -16,6 +16,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/IR/ModuleSummaryIndex.h" +#include "llvm/IR/PassManager.h" #include "llvm/Pass.h" namespace llvm { @@ -55,6 +56,20 @@ void computeVariableSummary(const GlobalVariable &V); }; +/// Analysis pass to provide the ModuleSummaryIndex object. +class ModuleSummaryIndexAnalysis + : public AnalysisInfoMixin { + friend AnalysisInfoMixin; + static char PassID; + + std::unique_ptr IndexBuilder; + +public: + typedef const ModuleSummaryIndex &Result; + + const ModuleSummaryIndex &run(Module &M, ModuleAnalysisManager &AM); +}; + /// Legacy wrapper pass to provide the ModuleSummaryIndex object. class ModuleSummaryIndexWrapperPass : public ModulePass { std::unique_ptr IndexBuilder; Index: lib/Analysis/ModuleSummaryAnalysis.cpp =================================================================== --- lib/Analysis/ModuleSummaryAnalysis.cpp +++ lib/Analysis/ModuleSummaryAnalysis.cpp @@ -174,6 +174,19 @@ } } +char ModuleSummaryIndexAnalysis::PassID; + +const ModuleSummaryIndex & +ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) { + auto &FAM = AM.getResult(M).getManager(); + IndexBuilder = llvm::make_unique( + &M, [&FAM](const Function &F) { + return &( + FAM.getResult(*const_cast(&F))); + }); + return IndexBuilder->getIndex(); +} + char ModuleSummaryIndexWrapperPass::ID = 0; INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis", "Module Summary Analysis", false, true) Index: lib/Bitcode/Writer/BitcodeWriterPass.cpp =================================================================== --- lib/Bitcode/Writer/BitcodeWriterPass.cpp +++ lib/Bitcode/Writer/BitcodeWriterPass.cpp @@ -19,12 +19,11 @@ #include "llvm/Pass.h" using namespace llvm; -PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &) { - std::unique_ptr Index; - if (EmitSummaryIndex) - Index = ModuleSummaryIndexBuilder(&M).takeIndex(); - WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index.get(), - EmitModuleHash); +PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { + const ModuleSummaryIndex *Index = + EmitSummaryIndex ? &(AM.getResult(M)) + : nullptr; + WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash); return PreservedAnalyses::all(); } Index: lib/Passes/PassBuilder.cpp =================================================================== --- lib/Passes/PassBuilder.cpp +++ lib/Passes/PassBuilder.cpp @@ -38,6 +38,7 @@ #include "llvm/Analysis/LoopAccessAnalysis.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/MemoryDependenceAnalysis.h" +#include "llvm/Analysis/ModuleSummaryAnalysis.h" #include "llvm/Analysis/OptimizationDiagnosticInfo.h" #include "llvm/Analysis/PostDominators.h" #include "llvm/Analysis/ProfileSummaryInfo.h" Index: lib/Passes/PassRegistry.def =================================================================== --- lib/Passes/PassRegistry.def +++ lib/Passes/PassRegistry.def @@ -21,6 +21,7 @@ #endif MODULE_ANALYSIS("callgraph", CallGraphAnalysis()) MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis()) +MODULE_ANALYSIS("module-summary", ModuleSummaryIndexAnalysis()) MODULE_ANALYSIS("no-op-module", NoOpModuleAnalysis()) MODULE_ANALYSIS("profile-summary", ProfileSummaryAnalysis()) MODULE_ANALYSIS("targetlibinfo", TargetLibraryAnalysis()) Index: test/Bitcode/thinlto-summary-globalvar.ll =================================================================== --- test/Bitcode/thinlto-summary-globalvar.ll +++ test/Bitcode/thinlto-summary-globalvar.ll @@ -1,4 +1,6 @@ ; RUN: opt -module-summary %s -o - | llvm-bcanalyzer -dump | FileCheck %s +; Check with new pass manager (by enabling a random pass in the new pipeline). +; RUN: opt -passes=gvn -module-summary %s -o - | llvm-bcanalyzer -dump | FileCheck %s ; CHECK: os(), "", ShouldPreserveAssemblyUseListOrder)); break; case OK_OutputBitcode: - MPM.addPass( - BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder)); + MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder, + EmitSummaryIndex, EmitModuleHash)); break; } Index: tools/opt/opt.cpp =================================================================== --- tools/opt/opt.cpp +++ tools/opt/opt.cpp @@ -490,7 +490,8 @@ // layer. return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(), PassPipeline, OK, VK, PreserveAssemblyUseListOrder, - PreserveBitcodeUseListOrder) + PreserveBitcodeUseListOrder, EmitSummaryIndex, + EmitModuleHash) ? 0 : 1; }