[llvm] dca328e - [Passes] Add new runCodeGenPipeline abstraction

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 3 09:53:23 PDT 2026


Author: Aiden Grossman
Date: 2026-09-03T09:53:16-07:00
New Revision: dca328efcdeebf40ef936152a6d7bc25d0ed6f70

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

LOG: [Passes] Add new runCodeGenPipeline abstraction

This is intended to replace this logic in clang/flang other frontends
which currently write essentially the same thing.

Reviewers: arsenm, nikic, aengelke

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

Added: 
    llvm/include/llvm/Passes/RunCodeGen.h
    llvm/lib/Passes/RunCodeGen.cpp

Modified: 
    llvm/lib/Passes/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Passes/RunCodeGen.h b/llvm/include/llvm/Passes/RunCodeGen.h
new file mode 100644
index 0000000000000..713b46b465ceb
--- /dev/null
+++ b/llvm/include/llvm/Passes/RunCodeGen.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_PASSES_RUN_CODEGEN_H
+#define LLVM_PASSES_RUN_CODEGEN_H
+
+#include "llvm/IR/Module.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include "llvm/Target/TargetMachine.h"
+
+namespace llvm {
+
+Error runCodeGenPipeline(
+    TargetMachine &TM, Module &M, raw_pwrite_stream &OS,
+    std::unique_ptr<ToolOutputFile> &DwoOS, CodeGenFileType CGFT,
+    bool PrintPipelinePasses = false, bool DisableVerify = true,
+    IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem());
+
+} // namespace llvm
+
+#endif // LLVM_PASSES_RUN_CODEGEN_H

diff  --git a/llvm/lib/Passes/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt
index 20fea0bd15029..6a966d582b63b 100644
--- a/llvm/lib/Passes/CMakeLists.txt
+++ b/llvm/lib/Passes/CMakeLists.txt
@@ -3,6 +3,7 @@ add_llvm_component_library(LLVMPasses
   PassBuilder.cpp
   PassBuilderBindings.cpp
   PassBuilderPipelines.cpp
+  RunCodeGen.cpp
   StandardInstrumentations.cpp
   TriggerCrashPasses.cpp
 

diff  --git a/llvm/lib/Passes/RunCodeGen.cpp b/llvm/lib/Passes/RunCodeGen.cpp
new file mode 100644
index 0000000000000..66e48b802416d
--- /dev/null
+++ b/llvm/lib/Passes/RunCodeGen.cpp
@@ -0,0 +1,111 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Passes/RunCodeGen.h"
+#include "llvm/Analysis/CGSCCPassManager.h"
+#include "llvm/Analysis/LoopAnalysisManager.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/PassManager.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Support/CodeGen.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/CGPassBuilderOption.h"
+
+using namespace llvm;
+
+static cl::opt<cl::boolOrDefault>
+    ForceNewPM("force-new-pm-codegen",
+               cl::desc("Whether to force the NewPM on/off. Not setting the "
+                        "option will default to what the target prefers."),
+               cl::init(cl::boolOrDefault::BOU_UNSET));
+
+static Error runCodeGenPipelineLegacy(TargetMachine &TM, Module &M,
+                                      raw_pwrite_stream &OS,
+                                      std::unique_ptr<ToolOutputFile> &DwoOS,
+                                      CodeGenFileType CGFT,
+                                      bool PrintPipelinePasses,
+                                      bool DisableVerify) {
+  legacy::PassManager CodeGenPasses;
+  CodeGenPasses.add(
+      createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
+  // Add LibraryInfo.
+  TargetLibraryInfoImpl TLII(TM.getTargetTriple(), TM.Options.VecLib);
+  CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
+
+  const 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))
+    return createStringError("Failed to construct CodeGen pipeline");
+  CodeGenPasses.run(M);
+
+  return Error::success();
+}
+
+static Error runCodeGenPipelineNewPM(TargetMachine &TM, Module &M,
+                                     raw_pwrite_stream &OS,
+                                     std::unique_ptr<ToolOutputFile> &DwoOS,
+                                     CodeGenFileType CGFT, bool DisableVerify,
+                                     IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
+  ModulePassManager MPM;
+  MachineFunctionAnalysisManager MFAM;
+  LoopAnalysisManager LAM;
+  FunctionAnalysisManager FAM;
+  CGSCCAnalysisManager CGAM;
+  ModuleAnalysisManager MAM;
+  CGPassBuilderOption Opt = getCGPassBuilderOption();
+  Opt.DisableVerify = DisableVerify;
+  MachineModuleInfo MMI(&TM);
+  PassInstrumentationCallbacks PIC;
+  PipelineTuningOptions PTOptions;
+  TargetMachine *TMPointer = &TM;
+  PassBuilder PB(TMPointer, PTOptions, std::nullopt, &PIC, VFS);
+  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)
+    return BuildPipelineError;
+
+  MPM.run(M, MAM);
+  return Error::success();
+}
+
+Error llvm::runCodeGenPipeline(TargetMachine &TM, Module &M,
+                               raw_pwrite_stream &OS,
+                               std::unique_ptr<ToolOutputFile> &DwoOS,
+                               CodeGenFileType CGFT, bool PrintPipelinePasses,
+                               bool DisableVerify,
+                               IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
+  if (ForceNewPM == cl::boolOrDefault::BOU_TRUE ||
+      (TM.shouldDefaultToNewPM() &&
+       ForceNewPM != cl::boolOrDefault::BOU_FALSE)) {
+    return runCodeGenPipelineNewPM(TM, M, OS, DwoOS, CGFT, DisableVerify, VFS);
+  }
+
+  return runCodeGenPipelineLegacy(TM, M, OS, DwoOS, CGFT, PrintPipelinePasses,
+                                  DisableVerify);
+}


        


More information about the llvm-commits mailing list