llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Aiden Grossman (boomanaiden154)

<details>
<summary>Changes</summary>

This abstracts across the LegacyPM and the NewPM. We add a new optional
parameter to runCodeGenPipeline to take in a ModuleSummaryIndex to
properly handle pipeline setup.


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


4 Files Affected:

- (modified) clang/lib/CodeGen/BackendUtil.cpp (+5-4) 
- (modified) llvm/include/llvm/Passes/RunCodeGen.h (+2) 
- (modified) llvm/lib/LTO/LTOBackend.cpp (+10-28) 
- (modified) llvm/lib/Passes/RunCodeGen.cpp (+19-4) 


``````````diff
diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index f226878f64f6f8..f1f03716bdff58 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1288,10 +1288,11 @@ void EmitAssemblyHelper::RunCodegenPipeline(
   }
 
   TimeCodegenPasses([&]() {
-    Error CodeGenError = runCodeGenPipeline(
-        *TM, *TheModule, *OS, DwoOS ? &DwoOS->os() : nullptr, CGFT,
-        !CodeGenOpts.VerifyModule,
-        /*DisableSimplifyLibCalls=*/false, CI.getVirtualFileSystemPtr());
+    Error CodeGenError =
+        runCodeGenPipeline(*TM, *TheModule, *OS, DwoOS ? &DwoOS->os() : 
nullptr,
+                           CGFT, !CodeGenOpts.VerifyModule,
+                           /*DisableSimplifyLibCalls=*/false, /*MSI=*/nullptr,
+                           CI.getVirtualFileSystemPtr());
     if (CodeGenError)
       Diags.Report(diag::err_fe_unable_to_interface_with_target);
   });
diff --git a/llvm/include/llvm/Passes/RunCodeGen.h 
b/llvm/include/llvm/Passes/RunCodeGen.h
index 87db2562b7acae..5ee8026765fc2c 100644
--- a/llvm/include/llvm/Passes/RunCodeGen.h
+++ b/llvm/include/llvm/Passes/RunCodeGen.h
@@ -10,6 +10,7 @@
 #define LLVM_PASSES_RUN_CODEGEN_H
 
 #include "llvm/IR/Module.h"
+#include "llvm/IR/ModuleSummaryIndex.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
@@ -20,6 +21,7 @@ LLVM_ABI Error runCodeGenPipeline(
     TargetMachine &TM, Module &M, raw_pwrite_stream &OS,
     raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, bool DisableVerify = true,
     bool DisableSimplifyLibCalls = false,
+    const ModuleSummaryIndex *MSI = nullptr,
     IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem());
 
 } // namespace llvm
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index 4fa1a29aaa22f6..f544da3117e70a 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -30,6 +30,7 @@
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Object/ModuleSymbolTable.h"
 #include "llvm/Passes/PassBuilder.h"
+#include "llvm/Passes/RunCodeGen.h"
 #include "llvm/Passes/StandardInstrumentations.h"
 #include "llvm/Plugins/PassPlugin.h"
 #include "llvm/Support/Error.h"
@@ -482,34 +483,15 @@ static void codegen(const Config &Conf, TargetMachine *TM,
   std::unique_ptr<CachedFileStream> &Stream = *StreamOrErr;
   TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName;
 
-  // Create the codegen pipeline in its own scope so it gets deleted before
-  // Stream->commit() is called. The commit function of CacheStream deletes
-  // the raw stream, which is too early as streamers (e.g. MCAsmStreamer)
-  // keep the pointer and may use it until their destruction. See #138194.
-  {
-    legacy::PassManager CodeGenPasses;
-    TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);
-    CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
-    CodeGenPasses.add(new RuntimeLibraryInfoWrapper(
-        TM->Options.MCOptions.ABIName, TM->Options.VecLib));
-
-    // No need to make index available if the module is empty.
-    // In theory these passes should not use the index for an empty
-    // module, however, this guards against doing any unnecessary summary-based
-    // analysis in the case of a ThinLTO build where this might be an empty
-    // regular LTO combined module, with a large combined index from ThinLTO.
-    if (!isEmptyModule(Mod))
-      CodeGenPasses.add(
-          createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
-    if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
-                                DwoOut ? &DwoOut->os() : nullptr,
-                                Conf.CGFileType))
-      report_fatal_error("Failed to setup codegen");
-    CodeGenPasses.run(Mod);
-
-    if (DwoOut)
-      DwoOut->keep();
-  }
+  const ModuleSummaryIndex *MSI =
+      !isEmptyModule(Mod) ? &CombinedIndex : nullptr;
+  if (runCodeGenPipeline(*TM, Mod, *Stream->OS,
+                         DwoOut ? &DwoOut->os() : nullptr, Conf.CGFileType,
+                         /*DisableVerify=*/true,
+                         /*DisableSimplifyLibCalls=*/false, MSI))
+    report_fatal_error("Failed to run codegen");
+  if (DwoOut)
+    DwoOut->keep();
 
   if (Error Err = Stream->commit())
     report_fatal_error(std::move(Err));
diff --git a/llvm/lib/Passes/RunCodeGen.cpp b/llvm/lib/Passes/RunCodeGen.cpp
index f0db7c319784a3..138d70ab3ac6db 100644
--- a/llvm/lib/Passes/RunCodeGen.cpp
+++ b/llvm/lib/Passes/RunCodeGen.cpp
@@ -10,12 +10,14 @@
 #include "PassesOptions.h"
 #include "llvm/Analysis/CGSCCPassManager.h"
 #include "llvm/Analysis/LoopAnalysisManager.h"
+#include "llvm/Analysis/ModuleSummaryAnalysis.h"
 #include "llvm/Analysis/RuntimeLibcallInfo.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/ModuleSummaryIndex.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/StandardInstrumentations.h"
@@ -30,6 +32,7 @@ static Error runCodeGenPipelineLegacy(TargetMachine &TM, 
Module &M,
                                       raw_pwrite_stream &OS,
                                       raw_pwrite_stream *DwoOS,
                                       CodeGenFileType CGFT, bool DisableVerify,
+                                      const ModuleSummaryIndex *MSI,
                                       bool DisableSimplifyLibCalls) {
   legacy::PassManager CodeGenPasses;
   CodeGenPasses.add(
@@ -39,6 +42,8 @@ static Error runCodeGenPipelineLegacy(TargetMachine &TM, 
Module &M,
   if (DisableSimplifyLibCalls)
     TLII.disableAllFunctions();
   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
+  if (MSI)
+    CodeGenPasses.add(createImmutableModuleSummaryIndexWrapperPass(MSI));
 
   const TargetOptions &Options = TM.Options;
   CodeGenPasses.add(
@@ -55,6 +60,7 @@ static Error runCodeGenPipelineNewPM(TargetMachine &TM, 
Module &M,
                                      raw_pwrite_stream &OS,
                                      raw_pwrite_stream *DwoOS,
                                      CodeGenFileType CGFT, bool DisableVerify,
+                                     const ModuleSummaryIndex *MSI,
                                      IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
   ModulePassManager MPM;
   MachineFunctionAnalysisManager MFAM;
@@ -68,6 +74,15 @@ static Error runCodeGenPipelineNewPM(TargetMachine &TM, 
Module &M,
   PassInstrumentationCallbacks PIC;
   PipelineTuningOptions PTOptions;
   PassBuilder PB(M.getContext(), &TM, PTOptions, std::nullopt, &PIC, VFS);
+
+  MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
+  if (MSI) {
+    MAM.registerPass(
+        [&]() { return ImmutableModuleSummaryIndexAnalysis(MSI); });
+    MPM.addPass(
+        RequireAnalysisPass<ImmutableModuleSummaryIndexAnalysis, Module>());
+  }
+
   PB.registerModuleAnalyses(MAM);
   PB.registerCGSCCAnalyses(CGAM);
   PB.registerFunctionAnalyses(FAM);
@@ -78,8 +93,6 @@ static Error runCodeGenPipelineNewPM(TargetMachine &TM, 
Module &M,
   StandardInstrumentations SI(M.getContext(), /*DebugLogging=*/false);
   SI.registerCallbacks(PIC, &MAM);
 
-  MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
-
   Error BuildPipelineError = TM.buildCodeGenPipeline(
       MPM, MAM, OS, DwoOS, CGFT, Opt, MMI.getContext(), &PIC);
   if (BuildPipelineError)
@@ -93,12 +106,14 @@ Error llvm::runCodeGenPipeline(TargetMachine &TM, Module 
&M,
                                raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS,
                                CodeGenFileType CGFT, bool DisableVerify,
                                bool DisableSimplifyLibCalls,
+                               const ModuleSummaryIndex *MSI,
                                IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
   const PassesOptions &Opts = getPassesOptions(M.getContext());
   if (valueOr(Opts.force_new_pm_codegen, TM.shouldDefaultToNewPM())) {
-    return runCodeGenPipelineNewPM(TM, M, OS, DwoOS, CGFT, DisableVerify, VFS);
+    return runCodeGenPipelineNewPM(TM, M, OS, DwoOS, CGFT, DisableVerify, MSI,
+                                   VFS);
   }
 
-  return runCodeGenPipelineLegacy(TM, M, OS, DwoOS, CGFT, DisableVerify,
+  return runCodeGenPipelineLegacy(TM, M, OS, DwoOS, CGFT, DisableVerify, MSI,
                                   DisableSimplifyLibCalls);
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/240252
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to