LLVM 24.0.0git
RunCodeGen.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
18#include "llvm/IR/PassManager.h"
21#include "llvm/Support/Error.h"
25
26using namespace llvm;
27
29 ForceNewPM("force-new-pm-codegen",
30 cl::desc("Whether to force the NewPM on/off. Not setting the "
31 "option will default to what the target prefers."),
33
36 std::unique_ptr<ToolOutputFile> &DwoOS,
37 CodeGenFileType CGFT,
39 bool DisableVerify) {
40 legacy::PassManager CodeGenPasses;
41 CodeGenPasses.add(
43 // Add LibraryInfo.
45 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
46
47 const TargetOptions &Options = TM.Options;
48 CodeGenPasses.add(
49 new RuntimeLibraryInfoWrapper(Options.ExceptionModel, Options.EABIVersion,
50 Options.MCOptions.ABIName, Options.VecLib));
51
52 if (TM.addPassesToEmitFile(CodeGenPasses, OS, DwoOS ? &DwoOS->os() : nullptr,
53 CGFT, DisableVerify))
54 return createStringError("Failed to construct CodeGen pipeline");
55 CodeGenPasses.run(M);
56
57 return Error::success();
58}
59
62 std::unique_ptr<ToolOutputFile> &DwoOS,
63 CodeGenFileType CGFT, bool DisableVerify,
72 Opt.DisableVerify = DisableVerify;
73 MachineModuleInfo MMI(&TM);
75 PipelineTuningOptions PTOptions;
76 TargetMachine *TMPointer = &TM;
77 PassBuilder PB(TMPointer, PTOptions, std::nullopt, &PIC, VFS);
78 PB.registerModuleAnalyses(MAM);
79 PB.registerCGSCCAnalyses(CGAM);
80 PB.registerFunctionAnalyses(FAM);
81 PB.registerLoopAnalyses(LAM);
82 PB.registerMachineFunctionAnalyses(MFAM);
83 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
84
85 MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
86
87 Error BuildPipelineError =
88 TM.buildCodeGenPipeline(MPM, MAM, OS, DwoOS ? &DwoOS->os() : nullptr,
89 CGFT, Opt, MMI.getContext(), &PIC);
90 if (BuildPipelineError)
91 return BuildPipelineError;
92
93 MPM.run(M, MAM);
94 return Error::success();
95}
96
99 std::unique_ptr<ToolOutputFile> &DwoOS,
101 bool DisableVerify,
104 (TM.shouldDefaultToNewPM() &&
106 return runCodeGenPipelineNewPM(TM, M, OS, DwoOS, CGFT, DisableVerify, VFS);
107 }
108
109 return runCodeGenPipelineLegacy(TM, M, OS, DwoOS, CGFT, PrintPipelinePasses,
110 DisableVerify);
111}
This header provides classes for managing passes over SCCs of the call graph.
This header defines various interfaces for pass management in LLVM.
static LVOptions Options
Definition LVOptions.cpp:25
This header provides classes for managing per-loop analyses.
CGSCCAnalysisManager CGAM
LoopAnalysisManager LAM
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
PassInstrumentationCallbacks PIC
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
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)
static Error runCodeGenPipelineNewPM(TargetMachine &TM, Module &M, raw_pwrite_stream &OS, std::unique_ptr< ToolOutputFile > &DwoOS, CodeGenFileType CGFT, bool DisableVerify, IntrusiveRefCntPtr< vfs::FileSystem > VFS)
This pass exposes codegen information to IR-level passes.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
An analysis that produces MachineModuleInfo for a module.
This class contains meta information specific to a module.
const MCContext & getContext() const
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This class provides access to building LLVM's passes.
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Tunable parameters for passes in the default pipelines.
Definition PassBuilder.h:41
Implementation of the target library information.
Primary interface to the complete machine description for the target machine.
virtual bool shouldDefaultToNewPM() const
Returns true if frontends should default to using the NewPM for this specific target.
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, bool=true, MachineModuleInfoWrapperPass *MMIWP=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
virtual Error buildCodeGenPipeline(ModulePassManager &MPM, ModuleAnalysisManager &MAM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, const CGPassBuilderOption &Opt, MCContext &Ctx, PassInstrumentationCallbacks *PIC)
const Triple & getTargetTriple() const
TargetIRAnalysis getTargetIRAnalysis() const
Get a TargetIRAnalysis appropriate for the target.
TargetOptions Options
VectorLibrary VecLib
Vector math library to use.
PassManager manages ModulePassManagers.
void add(Pass *P) override
Add a pass to the queue of passes to run.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
An abstract base class for streams implementations that also support a pwrite operation.
Interfaces for registering analysis passes, producing common pass manager configurations,...
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI cl::opt< std::optional< PrintPipelinePassesFormat >, false, PrintPipelinePassesFormatParser > PrintPipelinePasses
Common option used by multiple tools to print pipeline passes.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
AnalysisManager< LazyCallGraph::SCC, LazyCallGraph & > CGSCCAnalysisManager
The CGSCC analysis manager.
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:178
PassManager< Module > ModulePassManager
Convenience typedef for a pass manager over modules.
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())
LLVM_ABI ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI CGPassBuilderOption getCGPassBuilderOption()