1 //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file is just a split of the code that logically belongs in opt.cpp but
11 /// that includes the new pass manager headers.
13 //===----------------------------------------------------------------------===//
15 #include "NewPMDriver.h"
17 #include "PassPrinters.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/CGSCCPassManager.h"
21 #include "llvm/Bitcode/BitcodeWriterPass.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/IRPrintingPasses.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/PassManager.h"
28 #include "llvm/IR/Verifier.h"
29 #include "llvm/Passes/PassBuilder.h"
30 #include "llvm/Passes/PassPlugin.h"
31 #include "llvm/Passes/StandardInstrumentations.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/ToolOutputFile.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
37 #include "llvm/Transforms/Scalar/LoopPassManager.h"
40 using namespace opt_tool
;
43 DebugPM("debug-pass-manager", cl::Hidden
,
44 cl::desc("Print pass management debugging information"));
46 static cl::list
<std::string
>
47 PassPlugins("load-pass-plugin",
48 cl::desc("Load passes from plugin library"));
50 // This flag specifies a textual description of the alias analysis pipeline to
51 // use when querying for aliasing information. It only works in concert with
52 // the "passes" flag above.
53 static cl::opt
<std::string
>
54 AAPipeline("aa-pipeline",
55 cl::desc("A textual description of the alias analysis "
56 "pipeline for handling managed aliasing queries"),
59 /// {{@ These options accept textual pipeline descriptions which will be
60 /// inserted into default pipelines at the respective extension points
61 static cl::opt
<std::string
> PeepholeEPPipeline(
63 cl::desc("A textual description of the function pass pipeline inserted at "
64 "the Peephole extension points into default pipelines"),
66 static cl::opt
<std::string
> LateLoopOptimizationsEPPipeline(
67 "passes-ep-late-loop-optimizations",
69 "A textual description of the loop pass pipeline inserted at "
70 "the LateLoopOptimizations extension point into default pipelines"),
72 static cl::opt
<std::string
> LoopOptimizerEndEPPipeline(
73 "passes-ep-loop-optimizer-end",
74 cl::desc("A textual description of the loop pass pipeline inserted at "
75 "the LoopOptimizerEnd extension point into default pipelines"),
77 static cl::opt
<std::string
> ScalarOptimizerLateEPPipeline(
78 "passes-ep-scalar-optimizer-late",
79 cl::desc("A textual description of the function pass pipeline inserted at "
80 "the ScalarOptimizerLate extension point into default pipelines"),
82 static cl::opt
<std::string
> CGSCCOptimizerLateEPPipeline(
83 "passes-ep-cgscc-optimizer-late",
84 cl::desc("A textual description of the cgscc pass pipeline inserted at "
85 "the CGSCCOptimizerLate extension point into default pipelines"),
87 static cl::opt
<std::string
> VectorizerStartEPPipeline(
88 "passes-ep-vectorizer-start",
89 cl::desc("A textual description of the function pass pipeline inserted at "
90 "the VectorizerStart extension point into default pipelines"),
92 static cl::opt
<std::string
> PipelineStartEPPipeline(
93 "passes-ep-pipeline-start",
94 cl::desc("A textual description of the function pass pipeline inserted at "
95 "the PipelineStart extension point into default pipelines"),
97 static cl::opt
<std::string
> OptimizerLastEPPipeline(
98 "passes-ep-optimizer-last",
99 cl::desc("A textual description of the function pass pipeline inserted at "
100 "the OptimizerLast extension point into default pipelines"),
103 extern cl::opt
<PGOKind
> PGOKindFlag
;
104 extern cl::opt
<std::string
> ProfileFile
;
105 static cl::opt
<std::string
>
106 ProfileRemappingFile("profile-remapping-file",
107 cl::desc("Path to the profile remapping file."),
109 static cl::opt
<bool> DebugInfoForProfiling(
110 "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden
,
111 cl::desc("Emit special debug info to enable PGO profile generation."));
114 template <typename PassManagerT
>
115 bool tryParsePipelineText(PassBuilder
&PB
,
116 const cl::opt
<std::string
> &PipelineOpt
) {
117 if (PipelineOpt
.empty())
120 // Verify the pipeline is parseable:
122 if (auto Err
= PB
.parsePassPipeline(PM
, PipelineOpt
)) {
123 errs() << "Could not parse -" << PipelineOpt
.ArgStr
124 << " pipeline: " << toString(std::move(Err
))
125 << "... I'm going to ignore it.\n";
131 /// If one of the EPPipeline command line options was given, register callbacks
132 /// for parsing and inserting the given pipeline
133 static void registerEPCallbacks(PassBuilder
&PB
, bool VerifyEachPass
,
135 if (tryParsePipelineText
<FunctionPassManager
>(PB
, PeepholeEPPipeline
))
136 PB
.registerPeepholeEPCallback(
137 [&PB
, VerifyEachPass
, DebugLogging
](
138 FunctionPassManager
&PM
, PassBuilder::OptimizationLevel Level
) {
139 ExitOnError
Err("Unable to parse PeepholeEP pipeline: ");
140 Err(PB
.parsePassPipeline(PM
, PeepholeEPPipeline
, VerifyEachPass
,
143 if (tryParsePipelineText
<LoopPassManager
>(PB
,
144 LateLoopOptimizationsEPPipeline
))
145 PB
.registerLateLoopOptimizationsEPCallback(
146 [&PB
, VerifyEachPass
, DebugLogging
](
147 LoopPassManager
&PM
, PassBuilder::OptimizationLevel Level
) {
148 ExitOnError
Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
149 Err(PB
.parsePassPipeline(PM
, LateLoopOptimizationsEPPipeline
,
150 VerifyEachPass
, DebugLogging
));
152 if (tryParsePipelineText
<LoopPassManager
>(PB
, LoopOptimizerEndEPPipeline
))
153 PB
.registerLoopOptimizerEndEPCallback(
154 [&PB
, VerifyEachPass
, DebugLogging
](
155 LoopPassManager
&PM
, PassBuilder::OptimizationLevel Level
) {
156 ExitOnError
Err("Unable to parse LoopOptimizerEndEP pipeline: ");
157 Err(PB
.parsePassPipeline(PM
, LoopOptimizerEndEPPipeline
,
158 VerifyEachPass
, DebugLogging
));
160 if (tryParsePipelineText
<FunctionPassManager
>(PB
,
161 ScalarOptimizerLateEPPipeline
))
162 PB
.registerScalarOptimizerLateEPCallback(
163 [&PB
, VerifyEachPass
, DebugLogging
](
164 FunctionPassManager
&PM
, PassBuilder::OptimizationLevel Level
) {
165 ExitOnError
Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
166 Err(PB
.parsePassPipeline(PM
, ScalarOptimizerLateEPPipeline
,
167 VerifyEachPass
, DebugLogging
));
169 if (tryParsePipelineText
<CGSCCPassManager
>(PB
, CGSCCOptimizerLateEPPipeline
))
170 PB
.registerCGSCCOptimizerLateEPCallback(
171 [&PB
, VerifyEachPass
, DebugLogging
](
172 CGSCCPassManager
&PM
, PassBuilder::OptimizationLevel Level
) {
173 ExitOnError
Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
174 Err(PB
.parsePassPipeline(PM
, CGSCCOptimizerLateEPPipeline
,
175 VerifyEachPass
, DebugLogging
));
177 if (tryParsePipelineText
<FunctionPassManager
>(PB
, VectorizerStartEPPipeline
))
178 PB
.registerVectorizerStartEPCallback(
179 [&PB
, VerifyEachPass
, DebugLogging
](
180 FunctionPassManager
&PM
, PassBuilder::OptimizationLevel Level
) {
181 ExitOnError
Err("Unable to parse VectorizerStartEP pipeline: ");
182 Err(PB
.parsePassPipeline(PM
, VectorizerStartEPPipeline
,
183 VerifyEachPass
, DebugLogging
));
185 if (tryParsePipelineText
<ModulePassManager
>(PB
, PipelineStartEPPipeline
))
186 PB
.registerPipelineStartEPCallback(
187 [&PB
, VerifyEachPass
, DebugLogging
](ModulePassManager
&PM
) {
188 ExitOnError
Err("Unable to parse PipelineStartEP pipeline: ");
189 Err(PB
.parsePassPipeline(PM
, PipelineStartEPPipeline
, VerifyEachPass
,
192 if (tryParsePipelineText
<FunctionPassManager
>(PB
, OptimizerLastEPPipeline
))
193 PB
.registerOptimizerLastEPCallback(
194 [&PB
, VerifyEachPass
, DebugLogging
](FunctionPassManager
&PM
,
195 PassBuilder::OptimizationLevel
) {
196 ExitOnError
Err("Unable to parse OptimizerLastEP pipeline: ");
197 Err(PB
.parsePassPipeline(PM
, OptimizerLastEPPipeline
, VerifyEachPass
,
202 #ifdef LINK_POLLY_INTO_TOOLS
204 void RegisterPollyPasses(PassBuilder
&);
208 bool llvm::runPassPipeline(StringRef Arg0
, Module
&M
, TargetMachine
*TM
,
209 ToolOutputFile
*Out
, ToolOutputFile
*ThinLTOLinkOut
,
210 ToolOutputFile
*OptRemarkFile
,
211 StringRef PassPipeline
, OutputKind OK
,
213 bool ShouldPreserveAssemblyUseListOrder
,
214 bool ShouldPreserveBitcodeUseListOrder
,
215 bool EmitSummaryIndex
, bool EmitModuleHash
,
216 bool EnableDebugify
) {
217 bool VerifyEachPass
= VK
== VK_VerifyEachPass
;
219 Optional
<PGOOptions
> P
;
220 switch (PGOKindFlag
) {
222 P
= PGOOptions(ProfileFile
, "", "", "", true);
225 P
= PGOOptions("", ProfileFile
, "", ProfileRemappingFile
, false);
228 P
= PGOOptions("", "", ProfileFile
, ProfileRemappingFile
, false);
231 if (DebugInfoForProfiling
)
232 P
= PGOOptions("", "", "", "", false, true);
236 PassInstrumentationCallbacks PIC
;
237 StandardInstrumentations SI
;
238 SI
.registerCallbacks(PIC
);
240 PassBuilder
PB(TM
, P
, &PIC
);
241 registerEPCallbacks(PB
, VerifyEachPass
, DebugPM
);
243 // Load requested pass plugins and let them register pass builder callbacks
244 for (auto &PluginFN
: PassPlugins
) {
245 auto PassPlugin
= PassPlugin::Load(PluginFN
);
247 errs() << "Failed to load passes from '" << PluginFN
248 << "'. Request ignored.\n";
252 PassPlugin
->registerPassBuilderCallbacks(PB
);
255 // Register a callback that creates the debugify passes as needed.
256 PB
.registerPipelineParsingCallback(
257 [](StringRef Name
, ModulePassManager
&MPM
,
258 ArrayRef
<PassBuilder::PipelineElement
>) {
259 if (Name
== "debugify") {
260 MPM
.addPass(NewPMDebugifyPass());
262 } else if (Name
== "check-debugify") {
263 MPM
.addPass(NewPMCheckDebugifyPass());
269 #ifdef LINK_POLLY_INTO_TOOLS
270 polly::RegisterPollyPasses(PB
);
273 // Specially handle the alias analysis manager so that we can register
274 // a custom pipeline of AA passes with it.
276 if (auto Err
= PB
.parseAAPipeline(AA
, AAPipeline
)) {
277 errs() << Arg0
<< ": " << toString(std::move(Err
)) << "\n";
281 LoopAnalysisManager
LAM(DebugPM
);
282 FunctionAnalysisManager
FAM(DebugPM
);
283 CGSCCAnalysisManager
CGAM(DebugPM
);
284 ModuleAnalysisManager
MAM(DebugPM
);
286 // Register the AA manager first so that our version is the one used.
287 FAM
.registerPass([&] { return std::move(AA
); });
289 // Register all the basic analyses with the managers.
290 PB
.registerModuleAnalyses(MAM
);
291 PB
.registerCGSCCAnalyses(CGAM
);
292 PB
.registerFunctionAnalyses(FAM
);
293 PB
.registerLoopAnalyses(LAM
);
294 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
296 ModulePassManager
MPM(DebugPM
);
297 if (VK
> VK_NoVerifier
)
298 MPM
.addPass(VerifierPass());
300 MPM
.addPass(NewPMDebugifyPass());
303 PB
.parsePassPipeline(MPM
, PassPipeline
, VerifyEachPass
, DebugPM
)) {
304 errs() << Arg0
<< ": " << toString(std::move(Err
)) << "\n";
308 if (VK
> VK_NoVerifier
)
309 MPM
.addPass(VerifierPass());
311 MPM
.addPass(NewPMCheckDebugifyPass());
313 // Add any relevant output pass at the end of the pipeline.
316 break; // No output pass needed.
317 case OK_OutputAssembly
:
319 PrintModulePass(Out
->os(), "", ShouldPreserveAssemblyUseListOrder
));
321 case OK_OutputBitcode
:
322 MPM
.addPass(BitcodeWriterPass(Out
->os(), ShouldPreserveBitcodeUseListOrder
,
323 EmitSummaryIndex
, EmitModuleHash
));
325 case OK_OutputThinLTOBitcode
:
326 MPM
.addPass(ThinLTOBitcodeWriterPass(
327 Out
->os(), ThinLTOLinkOut
? &ThinLTOLinkOut
->os() : nullptr));
331 // Before executing passes, print the final values of the LLVM options.
332 cl::PrintOptionValues();
334 // Now that we have all of the passes ready, run them.
338 if (OK
!= OK_NoOutput
) {
340 if (OK
== OK_OutputThinLTOBitcode
&& ThinLTOLinkOut
)
341 ThinLTOLinkOut
->keep();
345 OptRemarkFile
->keep();