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

>From 27afb09a4f25b9da0e9ac4f9942647a44004ad08 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 | 55 ++++++++++++++++++++++++++++++++++---
 1 file changed, 51 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index 73697a9d0d446..35417c423f6c6 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 "
@@ -480,7 +486,48 @@ 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) {
+    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;
+
+    MPM.run(Mod, MAM);
+
+  } else {
     legacy::PassManager CodeGenPasses;
     TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);
     CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
@@ -504,11 +551,11 @@ static void codegen(const Config &Conf, TargetMachine *TM,
                                 Conf.CGFileType))
       report_fatal_error("Failed to setup codegen");
     CodeGenPasses.run(Mod);
-
-    if (DwoOut)
-      DwoOut->keep();
   }
 
+  if (DwoOut)
+    DwoOut->keep();
+
   if (Error Err = Stream->commit())
     report_fatal_error(std::move(Err));
 }

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

Reply via email to