Author: Aiden Grossman
Date: 2026-09-03T10:47:54-07:00
New Revision: 452f0dd3d02429c305e28d58cb6e3912f40cb9f9

URL: 
https://github.com/llvm/llvm-project/commit/452f0dd3d02429c305e28d58cb6e3912f40cb9f9
DIFF: 
https://github.com/llvm/llvm-project/commit/452f0dd3d02429c305e28d58cb6e3912f40cb9f9.diff

LOG: [Clang] Switch to runCodeGenPipeline abstraction

This moves some code to LLVM where it can be shared with other
frontends. It also enables a common spot for switching between PMs, so
we can drop the clang specific option.

Reviewers: efriedma-quic, arsenm, aengelke, jansvoboda11

Pull Request: https://github.com/llvm/llvm-project/pull/220306

Added: 
    

Modified: 
    clang/include/clang/Basic/CodeGenOptions.def
    clang/include/clang/Options/Options.td
    clang/lib/CodeGen/BackendUtil.cpp
    clang/test/CodeGen/X86/newpm.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index bf3e61f2f036f..2a04538677005 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -351,8 +351,6 @@ CODEGENOPT(TimeTrace         , 1, 0, Benign) ///< Set when 
-ftime-trace is enabl
 VALUE_CODEGENOPT(TimeTraceGranularity, 32, 500, Benign) ///< Minimum time 
granularity (in microseconds),
                                                         ///< traced by time 
profiler
 CODEGENOPT(InterchangeLoops  , 1, 0, Benign) ///< Run loop-interchange.
-ENUM_CODEGENOPT(EnableNewPMCodeGen, NewPMEnablementLevel, 2,
-                NewPMEnablementLevel::Auto, Benign) ///< Use NewPM for the 
CodeGen pipeline.
 CODEGENOPT(FuseLoops         , 1, 0, Benign) ///< Run loop-fusion.
 CODEGENOPT(UnrollLoops       , 1, 0, Benign) ///< Control whether loops are 
unrolled.
 CODEGENOPT(RerollLoops       , 1, 0, Benign) ///< Control whether loops are 
rerolled.

diff  --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index 9ef012dfe1d03..aa64353e1bce9 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -4825,22 +4825,6 @@ def floop_interchange : Flag<["-"], 
"floop-interchange">, Group<f_Group>,
   HelpText<"Enable the loop interchange pass">, Visibility<[ClangOption, 
CC1Option, FlangOption, FC1Option]>;
 def fno_loop_interchange: Flag<["-"], "fno-loop-interchange">, Group<f_Group>,
   HelpText<"Disable the loop interchange pass">, Visibility<[ClangOption, 
CC1Option, FlangOption, FC1Option]>;
-defm enable_new_pm_codegen
-    : BoolFOption<"enable-new-pm-codegen", CodeGenOpts<"EnableNewPMCodeGen">, 
DefaultFalse,
-                  NegFlag<SetFalse, [], [CC1Option],
-                          "Do not use the NewPM for the Codegen Pipeline">,
-                  PosFlag<SetTrue, [], [CC1Option],
-                          "Use the NewPM for the Codegen Pipeline">>,
-      DocBrief<
-          [{When enabled, use the NewPM to drive the Codegen pipeline.}]>;
-def enable_new_pm_codegen : Joined<["-"], "fenable-new-pm-codegen=">,
-    Group<f_Group>,
-    Visibility<[CC1Option]>,
-    HelpText<"When enabled, use the NewPM to drive the CodeGen pipeline.">,
-    Values<"auto,force-on,force-disable">,
-    NormalizedValuesScope<"CodeGenOptions::NewPMEnablementLevel">,
-    NormalizedValues<["Auto","ForceEnable","ForceDisable"]>,
-    MarshallingInfoEnum<CodeGenOpts<"EnableNewPMCodeGen">, "Auto">;
 defm experimental_loop_fusion
     : OptInCC1FFlag<"experimental-loop-fusion", "Enable", "Disable",
                     "Enable the loop fusion pass",

diff  --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index 086f886be4390..ed369d360c48b 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -43,6 +43,7 @@
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Object/OffloadBinary.h"
 #include "llvm/Passes/PassBuilder.h"
+#include "llvm/Passes/RunCodeGen.h"
 #include "llvm/Passes/StandardInstrumentations.h"
 #include "llvm/Plugins/PassPlugin.h"
 #include "llvm/ProfileData/InstrProfCorrelator.h"
@@ -193,14 +194,6 @@ class EmitAssemblyHelper {
   void RunCodegenPipeline(BackendAction Action,
                           std::unique_ptr<raw_pwrite_stream> &OS,
                           std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
-  void RunCodegenPipelineLegacy(BackendAction Action,
-                                std::unique_ptr<raw_pwrite_stream> &OS,
-                                std::unique_ptr<llvm::ToolOutputFile> &DwoOS,
-                                CodeGenFileType CGFT);
-  void RunCodegenPipelineNewPM(BackendAction Action,
-                               std::unique_ptr<raw_pwrite_stream> &OS,
-                               std::unique_ptr<llvm::ToolOutputFile> &DwoOS,
-                               CodeGenFileType CGFT);
   void TimeCodegenPasses(llvm::function_ref<void()> RunPasses);
 
   /// Check whether we should emit a module summary for regular LTO.
@@ -1246,89 +1239,13 @@ void EmitAssemblyHelper::RunCodegenPipeline(
       return;
   }
 
-  if (CodeGenOpts.getEnableNewPMCodeGen() ==
-          CodeGenOptions::NewPMEnablementLevel::ForceEnable ||
-      (CodeGenOpts.getEnableNewPMCodeGen() ==
-           CodeGenOptions::NewPMEnablementLevel::Auto &&
-       TM->shouldDefaultToNewPM())) {
-    RunCodegenPipelineNewPM(Action, OS, DwoOS, CGFT);
-  } else {
-    RunCodegenPipelineLegacy(Action, OS, DwoOS, CGFT);
-  }
-}
-
-void EmitAssemblyHelper::RunCodegenPipelineLegacy(
-    BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
-    std::unique_ptr<llvm::ToolOutputFile> &DwoOS, CodeGenFileType CGFT) {
-  // We still use the legacy PM to run the codegen pipeline since the new PM
-  // does not work with the codegen pipeline.
-  // FIXME: make the new PM work with the codegen pipeline.
-  legacy::PassManager CodeGenPasses;
-
-  CodeGenPasses.add(
-      createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
-  // Add LibraryInfo.
-  std::unique_ptr<TargetLibraryInfoImpl> TLII(
-      llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
-  CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
-
-  const llvm::TargetOptions &Options = TM->Options;
-  CodeGenPasses.add(
-      new RuntimeLibraryInfoWrapper(Options.ExceptionModel, 
Options.EABIVersion,
-                                    Options.MCOptions.ABIName, 
Options.VecLib));
-
-  if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
-                              DwoOS ? &DwoOS->os() : nullptr, CGFT,
-                              /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
-    Diags.Report(diag::err_fe_unable_to_interface_with_target);
-    return;
-  }
-
-  // If -print-pipeline-passes is requested, don't run the legacy pass manager.
-  // FIXME: when codegen is switched to use the new pass manager, it should 
also
-  // emit pass names here.
-  if (PrintPipelinePasses) {
-    return;
-  }
-
-  TimeCodegenPasses([&] { CodeGenPasses.run(*TheModule); });
-}
-
-void EmitAssemblyHelper::RunCodegenPipelineNewPM(
-    BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
-    std::unique_ptr<llvm::ToolOutputFile> &DwoOS, CodeGenFileType CGFT) {
-  ModulePassManager MPM;
-  MachineFunctionAnalysisManager MFAM;
-  LoopAnalysisManager LAM;
-  FunctionAnalysisManager FAM;
-  CGSCCAnalysisManager CGAM;
-  ModuleAnalysisManager MAM;
-  CGPassBuilderOption Opt = getCGPassBuilderOption();
-  Opt.DisableVerify = !CodeGenOpts.VerifyModule;
-  MachineModuleInfo MMI(TM.get());
-  PassInstrumentationCallbacks PIC;
-  PipelineTuningOptions PTOptions;
-  TargetMachine *TMPointer = TM.get();
-  PassBuilder PB(TMPointer, PTOptions, std::nullopt, &PIC,
-                 CI.getVirtualFileSystemPtr());
-  PB.registerModuleAnalyses(MAM);
-  PB.registerCGSCCAnalyses(CGAM);
-  PB.registerFunctionAnalyses(FAM);
-  PB.registerLoopAnalyses(LAM);
-  PB.registerMachineFunctionAnalyses(MFAM);
-  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
-
-  MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
-
-  Error BuildPipelineError =
-      TM->buildCodeGenPipeline(MPM, MAM, *OS, DwoOS ? &DwoOS->os() : nullptr,
-                               CGFT, Opt, MMI.getContext(), &PIC);
-  if (BuildPipelineError) {
-    Diags.Report(diag::err_fe_unable_to_interface_with_target);
-    return;
-  }
-
-  TimeCodegenPasses([&] { MPM.run(*TheModule, MAM); });
+  TimeCodegenPasses([&]() {
+    Error CodeGenError = runCodeGenPipeline(
+        *TM, *TheModule, *OS, DwoOS, CGFT, PrintPipelinePasses.has_value(),
+        !CodeGenOpts.VerifyModule, CI.getVirtualFileSystemPtr());
+    if (CodeGenError)
+      Diags.Report(diag::err_fe_unable_to_interface_with_target);
+  });
 }
 
 void EmitAssemblyHelper::TimeCodegenPasses(

diff  --git a/clang/test/CodeGen/X86/newpm.c b/clang/test/CodeGen/X86/newpm.c
index 6dec8a2e8e24a..b5fabc63ed2a0 100644
--- a/clang/test/CodeGen/X86/newpm.c
+++ b/clang/test/CodeGen/X86/newpm.c
@@ -1,5 +1,5 @@
 // REQUIRES: x86-registered-target
-// RUN: %clang_cc1 -triple=x86_64-unkown-linux-gnu 
-fenable-new-pm-codegen=force-on -S -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple=x86_64-unkown-linux-gnu -mllvm 
-force-new-pm-codegen=true -S -o - %s | FileCheck %s
 
 int foo() {
   // CHECK-LABEL: foo


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

Reply via email to