https://github.com/vikramRH created 
https://github.com/llvm/llvm-project/pull/210249

None

>From 21debf8f853cef64680d3e412cee2f88ac2f9290 Mon Sep 17 00:00:00 2001
From: vikhegde <[email protected]>
Date: Thu, 16 Jul 2026 16:23:49 +0530
Subject: [PATCH] [LTO] Add support for NewPM CodeGen

---
 llvm/lib/LTO/LTOBackend.cpp | 68 +++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index 73697a9d0d446..8676fdcf638b5 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -22,8 +22,10 @@
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/CGData/CodeGenData.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/IR/LLVMRemarkStreamer.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/ModuleSummaryIndex.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/LTO/LTO.h"
@@ -74,6 +76,10 @@ static cl::opt<bool> ThinLTOAssumeMerged(
     cl::desc("Assume the input has already undergone ThinLTO function "
              "importing and the other pre-optimization pipeline changes."));
 
+static cl::opt<bool> EnableNPMForBackend("enable-npm-for-backend",
+                                         cl::init(false),
+                                         cl::desc("Disable NPM for backend"));
+
 static cl::list<std::string>
     SaveModulesList("filter-save-modules", cl::value_desc("module names"),
                     cl::desc("Only save bitcode for module whose name without "
@@ -434,6 +440,56 @@ bool lto::opt(const Config &Conf, TargetMachine *TM, 
unsigned Task, Module &Mod,
   return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
 }
 
+Error runBackendWithNewPM(TargetMachine *TM, Module &Mod,
+                          std::unique_ptr<CachedFileStream> &Stream,
+                          std::unique_ptr<ToolOutputFile> &DwoOut,
+                          const Config &Conf,
+                          const ModuleSummaryIndex &CombinedIndex) {
+  MachineModuleInfo MMI(TM);
+  PassInstrumentationCallbacks PIC;
+  MachineFunctionAnalysisManager MFAM;
+  LoopAnalysisManager LAM;
+  FunctionAnalysisManager FAM;
+  CGSCCAnalysisManager CGAM;
+  ModuleAnalysisManager MAM;
+  PassBuilder PB(TM, PipelineTuningOptions(), std::nullopt, &PIC);
+
+  TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);
+  FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
+  MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
+  MAM.registerPass([&] {
+    return RuntimeLibraryAnalysis(
+        Mod.getTargetTriple(), TM->Options.ExceptionModel,
+        TM->Options.FloatABIType, TM->Options.EABIVersion,
+        TM->Options.MCOptions.ABIName, TM->Options.VecLib);
+  });
+
+  if (!isEmptyModule(Mod))
+    MAM.registerPass(
+        [&] { return ImmutableModuleSummaryIndexAnalysis(&CombinedIndex); });
+
+  PB.registerModuleAnalyses(MAM);
+  PB.registerCGSCCAnalyses(CGAM);
+  PB.registerFunctionAnalyses(FAM);
+  PB.registerLoopAnalyses(LAM);
+  PB.registerMachineFunctionAnalyses(MFAM);
+  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
+
+  ModulePassManager MPM;
+  FunctionPassManager FPM;
+
+  if (Error Err = TM->buildCodeGenPipeline(
+          MPM, MAM, *Stream->OS, DwoOut ? &DwoOut->os() : nullptr,
+          Conf.CGFileType, CGPassBuilderOption(), MMI.getContext(), &PIC))
+    return Err;
+
+  MPM.run(Mod, MAM);
+
+  if (DwoOut)
+    DwoOut->keep();
+  return Error::success();
+}
+
 static void codegen(const Config &Conf, TargetMachine *TM,
                     AddStreamFn AddStream, unsigned Task, Module &Mod,
                     const ModuleSummaryIndex &CombinedIndex) {
@@ -480,6 +536,18 @@ static void codegen(const Config &Conf, TargetMachine *TM,
   // 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.
+  if (EnableNPMForBackend) {
+    if (!runBackendWithNewPM(TM, Mod, Stream, DwoOut, Conf, CombinedIndex)) {
+      // NPM success
+      if (Error Err = Stream->commit())
+        report_fatal_error(std::move(Err));
+      return;
+    }
+
+    llvm::errs()
+        << "warning: New pass manager failed, falling back to legacy PM\n";
+  }
+
   {
     legacy::PassManager CodeGenPasses;
     TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);

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

Reply via email to