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"
16 #include "PassPrinters.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/CGSCCPassManager.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/Bitcode/BitcodeWriterPass.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/IRPrintingPasses.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/PassManager.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/Passes/PassBuilder.h"
31 #include "llvm/Passes/PassPlugin.h"
32 #include "llvm/Passes/StandardInstrumentations.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/Instrumentation/AddressSanitizer.h"
38 #include "llvm/Transforms/Scalar/LoopPassManager.h"
39 #include "llvm/Transforms/Utils/Debugify.h"
42 using namespace opt_tool
;
45 cl::opt
<bool> DebugifyEach(
47 cl::desc("Start each pass with debugify and end it with check-debugify"));
50 DebugifyExport("debugify-export",
51 cl::desc("Export per-pass debugify statistics to this file"),
52 cl::value_desc("filename"));
55 enum class DebugLogging
{ None
, Normal
, Verbose
, Quiet
};
57 static cl::opt
<DebugLogging
> DebugPM(
58 "debug-pass-manager", cl::Hidden
, cl::ValueOptional
,
59 cl::desc("Print pass management debugging information"),
60 cl::init(DebugLogging::None
),
62 clEnumValN(DebugLogging::Normal
, "", ""),
63 clEnumValN(DebugLogging::Quiet
, "quiet",
64 "Skip printing info about analyses"),
66 DebugLogging::Verbose
, "verbose",
67 "Print extra information about adaptors and pass managers")));
69 static cl::list
<std::string
>
70 PassPlugins("load-pass-plugin",
71 cl::desc("Load passes from plugin library"));
73 // This flag specifies a textual description of the alias analysis pipeline to
74 // use when querying for aliasing information. It only works in concert with
75 // the "passes" flag above.
76 static cl::opt
<std::string
>
77 AAPipeline("aa-pipeline",
78 cl::desc("A textual description of the alias analysis "
79 "pipeline for handling managed aliasing queries"),
80 cl::Hidden
, cl::init("default"));
82 /// {{@ These options accept textual pipeline descriptions which will be
83 /// inserted into default pipelines at the respective extension points
84 static cl::opt
<std::string
> PeepholeEPPipeline(
86 cl::desc("A textual description of the function pass pipeline inserted at "
87 "the Peephole extension points into default pipelines"),
89 static cl::opt
<std::string
> LateLoopOptimizationsEPPipeline(
90 "passes-ep-late-loop-optimizations",
92 "A textual description of the loop pass pipeline inserted at "
93 "the LateLoopOptimizations extension point into default pipelines"),
95 static cl::opt
<std::string
> LoopOptimizerEndEPPipeline(
96 "passes-ep-loop-optimizer-end",
97 cl::desc("A textual description of the loop pass pipeline inserted at "
98 "the LoopOptimizerEnd extension point into default pipelines"),
100 static cl::opt
<std::string
> ScalarOptimizerLateEPPipeline(
101 "passes-ep-scalar-optimizer-late",
102 cl::desc("A textual description of the function pass pipeline inserted at "
103 "the ScalarOptimizerLate extension point into default pipelines"),
105 static cl::opt
<std::string
> CGSCCOptimizerLateEPPipeline(
106 "passes-ep-cgscc-optimizer-late",
107 cl::desc("A textual description of the cgscc pass pipeline inserted at "
108 "the CGSCCOptimizerLate extension point into default pipelines"),
110 static cl::opt
<std::string
> VectorizerStartEPPipeline(
111 "passes-ep-vectorizer-start",
112 cl::desc("A textual description of the function pass pipeline inserted at "
113 "the VectorizerStart extension point into default pipelines"),
115 static cl::opt
<std::string
> PipelineStartEPPipeline(
116 "passes-ep-pipeline-start",
117 cl::desc("A textual description of the module pass pipeline inserted at "
118 "the PipelineStart extension point into default pipelines"),
120 static cl::opt
<std::string
> PipelineEarlySimplificationEPPipeline(
121 "passes-ep-pipeline-early-simplification",
122 cl::desc("A textual description of the module pass pipeline inserted at "
123 "the EarlySimplification extension point into default pipelines"),
125 static cl::opt
<std::string
> OptimizerLastEPPipeline(
126 "passes-ep-optimizer-last",
127 cl::desc("A textual description of the module pass pipeline inserted at "
128 "the OptimizerLast extension point into default pipelines"),
131 // Individual pipeline tuning options.
132 extern cl::opt
<bool> DisableLoopUnrolling
;
135 extern cl::opt
<PGOKind
> PGOKindFlag
;
136 extern cl::opt
<std::string
> ProfileFile
;
137 extern cl::opt
<CSPGOKind
> CSPGOKindFlag
;
138 extern cl::opt
<std::string
> CSProfileGenFile
;
139 extern cl::opt
<bool> DisableBasicAA
;
140 extern cl::opt
<bool> PrintPipelinePasses
;
143 static cl::opt
<std::string
>
144 ProfileRemappingFile("profile-remapping-file",
145 cl::desc("Path to the profile remapping file."),
147 static cl::opt
<bool> DebugInfoForProfiling(
148 "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden
,
149 cl::desc("Emit special debug info to enable PGO profile generation."));
150 static cl::opt
<bool> PseudoProbeForProfiling(
151 "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden
,
152 cl::desc("Emit pseudo probes to enable PGO profile generation."));
155 template <typename PassManagerT
>
156 bool tryParsePipelineText(PassBuilder
&PB
,
157 const cl::opt
<std::string
> &PipelineOpt
) {
158 if (PipelineOpt
.empty())
161 // Verify the pipeline is parseable:
163 if (auto Err
= PB
.parsePassPipeline(PM
, PipelineOpt
)) {
164 errs() << "Could not parse -" << PipelineOpt
.ArgStr
165 << " pipeline: " << toString(std::move(Err
))
166 << "... I'm going to ignore it.\n";
172 /// If one of the EPPipeline command line options was given, register callbacks
173 /// for parsing and inserting the given pipeline
174 static void registerEPCallbacks(PassBuilder
&PB
) {
175 if (tryParsePipelineText
<FunctionPassManager
>(PB
, PeepholeEPPipeline
))
176 PB
.registerPeepholeEPCallback(
177 [&PB
](FunctionPassManager
&PM
, OptimizationLevel Level
) {
178 ExitOnError
Err("Unable to parse PeepholeEP pipeline: ");
179 Err(PB
.parsePassPipeline(PM
, PeepholeEPPipeline
));
181 if (tryParsePipelineText
<LoopPassManager
>(PB
,
182 LateLoopOptimizationsEPPipeline
))
183 PB
.registerLateLoopOptimizationsEPCallback(
184 [&PB
](LoopPassManager
&PM
, OptimizationLevel Level
) {
185 ExitOnError
Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
186 Err(PB
.parsePassPipeline(PM
, LateLoopOptimizationsEPPipeline
));
188 if (tryParsePipelineText
<LoopPassManager
>(PB
, LoopOptimizerEndEPPipeline
))
189 PB
.registerLoopOptimizerEndEPCallback(
190 [&PB
](LoopPassManager
&PM
, OptimizationLevel Level
) {
191 ExitOnError
Err("Unable to parse LoopOptimizerEndEP pipeline: ");
192 Err(PB
.parsePassPipeline(PM
, LoopOptimizerEndEPPipeline
));
194 if (tryParsePipelineText
<FunctionPassManager
>(PB
,
195 ScalarOptimizerLateEPPipeline
))
196 PB
.registerScalarOptimizerLateEPCallback(
197 [&PB
](FunctionPassManager
&PM
, OptimizationLevel Level
) {
198 ExitOnError
Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
199 Err(PB
.parsePassPipeline(PM
, ScalarOptimizerLateEPPipeline
));
201 if (tryParsePipelineText
<CGSCCPassManager
>(PB
, CGSCCOptimizerLateEPPipeline
))
202 PB
.registerCGSCCOptimizerLateEPCallback(
203 [&PB
](CGSCCPassManager
&PM
, OptimizationLevel Level
) {
204 ExitOnError
Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
205 Err(PB
.parsePassPipeline(PM
, CGSCCOptimizerLateEPPipeline
));
207 if (tryParsePipelineText
<FunctionPassManager
>(PB
, VectorizerStartEPPipeline
))
208 PB
.registerVectorizerStartEPCallback(
209 [&PB
](FunctionPassManager
&PM
, OptimizationLevel Level
) {
210 ExitOnError
Err("Unable to parse VectorizerStartEP pipeline: ");
211 Err(PB
.parsePassPipeline(PM
, VectorizerStartEPPipeline
));
213 if (tryParsePipelineText
<ModulePassManager
>(PB
, PipelineStartEPPipeline
))
214 PB
.registerPipelineStartEPCallback(
215 [&PB
](ModulePassManager
&PM
, OptimizationLevel
) {
216 ExitOnError
Err("Unable to parse PipelineStartEP pipeline: ");
217 Err(PB
.parsePassPipeline(PM
, PipelineStartEPPipeline
));
219 if (tryParsePipelineText
<ModulePassManager
>(
220 PB
, PipelineEarlySimplificationEPPipeline
))
221 PB
.registerPipelineEarlySimplificationEPCallback(
222 [&PB
](ModulePassManager
&PM
, OptimizationLevel
) {
223 ExitOnError
Err("Unable to parse EarlySimplification pipeline: ");
224 Err(PB
.parsePassPipeline(PM
, PipelineEarlySimplificationEPPipeline
));
226 if (tryParsePipelineText
<FunctionPassManager
>(PB
, OptimizerLastEPPipeline
))
227 PB
.registerOptimizerLastEPCallback(
228 [&PB
](ModulePassManager
&PM
, OptimizationLevel
) {
229 ExitOnError
Err("Unable to parse OptimizerLastEP pipeline: ");
230 Err(PB
.parsePassPipeline(PM
, OptimizerLastEPPipeline
));
234 #define HANDLE_EXTENSION(Ext) \
235 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
236 #include "llvm/Support/Extension.def"
238 bool llvm::runPassPipeline(StringRef Arg0
, Module
&M
, TargetMachine
*TM
,
239 TargetLibraryInfoImpl
*TLII
, ToolOutputFile
*Out
,
240 ToolOutputFile
*ThinLTOLinkOut
,
241 ToolOutputFile
*OptRemarkFile
,
242 StringRef PassPipeline
, ArrayRef
<StringRef
> Passes
,
243 OutputKind OK
, VerifierKind VK
,
244 bool ShouldPreserveAssemblyUseListOrder
,
245 bool ShouldPreserveBitcodeUseListOrder
,
246 bool EmitSummaryIndex
, bool EmitModuleHash
,
247 bool EnableDebugify
) {
248 bool VerifyEachPass
= VK
== VK_VerifyEachPass
;
250 Optional
<PGOOptions
> P
;
251 switch (PGOKindFlag
) {
253 P
= PGOOptions(ProfileFile
, "", "", PGOOptions::IRInstr
);
256 P
= PGOOptions(ProfileFile
, "", ProfileRemappingFile
, PGOOptions::IRUse
);
259 P
= PGOOptions(ProfileFile
, "", ProfileRemappingFile
,
260 PGOOptions::SampleUse
);
263 if (DebugInfoForProfiling
|| PseudoProbeForProfiling
)
264 P
= PGOOptions("", "", "", PGOOptions::NoAction
, PGOOptions::NoCSAction
,
265 DebugInfoForProfiling
, PseudoProbeForProfiling
);
269 if (CSPGOKindFlag
!= NoCSPGO
) {
270 if (P
&& (P
->Action
== PGOOptions::IRInstr
||
271 P
->Action
== PGOOptions::SampleUse
))
272 errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
273 if (CSPGOKindFlag
== CSInstrGen
) {
274 if (CSProfileGenFile
.empty())
275 errs() << "CSInstrGen needs to specify CSProfileGenFile";
277 P
->CSAction
= PGOOptions::CSIRInstr
;
278 P
->CSProfileGenFile
= CSProfileGenFile
;
280 P
= PGOOptions("", CSProfileGenFile
, ProfileRemappingFile
,
281 PGOOptions::NoAction
, PGOOptions::CSIRInstr
);
282 } else /* CSPGOKindFlag == CSInstrUse */ {
284 errs() << "CSInstrUse needs to be together with InstrUse";
285 P
->CSAction
= PGOOptions::CSIRUse
;
291 LoopAnalysisManager LAM
;
292 FunctionAnalysisManager FAM
;
293 CGSCCAnalysisManager CGAM
;
294 ModuleAnalysisManager MAM
;
296 PassInstrumentationCallbacks PIC
;
297 PrintPassOptions PrintPassOpts
;
298 PrintPassOpts
.Verbose
= DebugPM
== DebugLogging::Verbose
;
299 PrintPassOpts
.SkipAnalyses
= DebugPM
== DebugLogging::Quiet
;
300 StandardInstrumentations
SI(DebugPM
!= DebugLogging::None
, VerifyEachPass
,
302 SI
.registerCallbacks(PIC
, &FAM
);
303 DebugifyEachInstrumentation Debugify
;
305 Debugify
.registerCallbacks(PIC
);
307 PipelineTuningOptions PTO
;
308 // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
309 // to false above so we shouldn't necessarily need to check whether or not the
310 // option has been enabled.
311 PTO
.LoopUnrolling
= !DisableLoopUnrolling
;
312 PassBuilder
PB(TM
, PTO
, P
, &PIC
);
313 registerEPCallbacks(PB
);
315 // Load requested pass plugins and let them register pass builder callbacks
316 for (auto &PluginFN
: PassPlugins
) {
317 auto PassPlugin
= PassPlugin::Load(PluginFN
);
319 errs() << "Failed to load passes from '" << PluginFN
320 << "'. Request ignored.\n";
324 PassPlugin
->registerPassBuilderCallbacks(PB
);
327 // Register a callback that creates the debugify passes as needed.
328 PB
.registerPipelineParsingCallback(
329 [](StringRef Name
, ModulePassManager
&MPM
,
330 ArrayRef
<PassBuilder::PipelineElement
>) {
331 if (Name
== "debugify") {
332 MPM
.addPass(NewPMDebugifyPass());
334 } else if (Name
== "check-debugify") {
335 MPM
.addPass(NewPMCheckDebugifyPass());
340 PB
.registerPipelineParsingCallback(
341 [](StringRef Name
, ModulePassManager
&MPM
,
342 ArrayRef
<PassBuilder::PipelineElement
>) {
343 AddressSanitizerOptions Opts
;
344 if (Name
== "asan-pipeline") {
346 RequireAnalysisPass
<ASanGlobalsMetadataAnalysis
, Module
>());
347 MPM
.addPass(ModuleAddressSanitizerPass(Opts
));
349 } else if (Name
== "asan-function-pipeline") {
351 RequireAnalysisPass
<ASanGlobalsMetadataAnalysis
, Module
>());
353 createModuleToFunctionPassAdaptor(AddressSanitizerPass(Opts
)));
359 #define HANDLE_EXTENSION(Ext) \
360 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
361 #include "llvm/Support/Extension.def"
363 // Specially handle the alias analysis manager so that we can register
364 // a custom pipeline of AA passes with it.
366 if (Passes
.empty()) {
367 if (auto Err
= PB
.parseAAPipeline(AA
, AAPipeline
)) {
368 errs() << Arg0
<< ": " << toString(std::move(Err
)) << "\n";
373 // For compatibility with the legacy PM AA pipeline.
374 // AAResultsWrapperPass by default provides basic-aa in the legacy PM
375 // unless -disable-basic-aa is specified.
376 // TODO: remove this once tests implicitly requiring basic-aa use -passes= and
377 // -aa-pipeline=basic-aa.
378 if (!Passes
.empty() && !DisableBasicAA
) {
379 if (auto Err
= PB
.parseAAPipeline(AA
, "basic-aa")) {
380 errs() << Arg0
<< ": " << toString(std::move(Err
)) << "\n";
385 // For compatibility with legacy pass manager.
386 // Alias analyses are not specially specified when using the legacy PM.
387 for (auto PassName
: Passes
) {
388 if (PB
.isAAPassName(PassName
)) {
389 if (auto Err
= PB
.parseAAPipeline(AA
, PassName
)) {
390 errs() << Arg0
<< ": " << toString(std::move(Err
)) << "\n";
396 // Register the AA manager first so that our version is the one used.
397 FAM
.registerPass([&] { return std::move(AA
); });
398 // Register our TargetLibraryInfoImpl.
399 FAM
.registerPass([&] { return TargetLibraryAnalysis(*TLII
); });
401 // Register all the basic analyses with the managers.
402 PB
.registerModuleAnalyses(MAM
);
403 PB
.registerCGSCCAnalyses(CGAM
);
404 PB
.registerFunctionAnalyses(FAM
);
405 PB
.registerLoopAnalyses(LAM
);
406 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
408 ModulePassManager MPM
;
409 if (VK
> VK_NoVerifier
)
410 MPM
.addPass(VerifierPass());
412 MPM
.addPass(NewPMDebugifyPass());
414 // Add passes according to the -passes options.
415 if (!PassPipeline
.empty()) {
416 assert(Passes
.empty() &&
417 "PassPipeline and Passes should not both contain passes");
418 if (auto Err
= PB
.parsePassPipeline(MPM
, PassPipeline
)) {
419 errs() << Arg0
<< ": " << toString(std::move(Err
)) << "\n";
423 // Add passes specified using the legacy PM syntax (i.e. not using
424 // -passes). This should be removed later when such support has been
425 // deprecated, i.e. when all lit tests running opt (and not using
426 // -enable-new-pm=0) have been updated to use -passes.
427 for (auto PassName
: Passes
) {
428 std::string
ModifiedPassName(PassName
.begin(), PassName
.end());
429 if (PB
.isAnalysisPassName(PassName
))
430 ModifiedPassName
= "require<" + ModifiedPassName
+ ">";
431 // FIXME: These translations are supposed to be removed when lit tests that
432 // use these names have been updated to use the -passes syntax (and when the
433 // support for using the old syntax to specify passes is considered as
434 // deprecated for the new PM).
435 if (ModifiedPassName
== "early-cse-memssa")
436 ModifiedPassName
= "early-cse<memssa>";
437 else if (ModifiedPassName
== "post-inline-ee-instrument")
438 ModifiedPassName
= "ee-instrument<post-inline>";
439 else if (ModifiedPassName
== "loop-extract-single")
440 ModifiedPassName
= "loop-extract<single>";
441 else if (ModifiedPassName
== "lower-matrix-intrinsics-minimal")
442 ModifiedPassName
= "lower-matrix-intrinsics<minimal>";
443 if (auto Err
= PB
.parsePassPipeline(MPM
, ModifiedPassName
)) {
444 errs() << Arg0
<< ": " << toString(std::move(Err
)) << "\n";
449 if (VK
> VK_NoVerifier
)
450 MPM
.addPass(VerifierPass());
452 MPM
.addPass(NewPMCheckDebugifyPass());
454 // Add any relevant output pass at the end of the pipeline.
457 break; // No output pass needed.
458 case OK_OutputAssembly
:
460 PrintModulePass(Out
->os(), "", ShouldPreserveAssemblyUseListOrder
));
462 case OK_OutputBitcode
:
463 MPM
.addPass(BitcodeWriterPass(Out
->os(), ShouldPreserveBitcodeUseListOrder
,
464 EmitSummaryIndex
, EmitModuleHash
));
466 case OK_OutputThinLTOBitcode
:
467 MPM
.addPass(ThinLTOBitcodeWriterPass(
468 Out
->os(), ThinLTOLinkOut
? &ThinLTOLinkOut
->os() : nullptr));
472 // Before executing passes, print the final values of the LLVM options.
473 cl::PrintOptionValues();
475 // Print a textual, '-passes=' compatible, representation of pipeline if
477 if (PrintPipelinePasses
) {
478 MPM
.printPipeline(outs(), [&PIC
](StringRef ClassName
) {
479 auto PassName
= PIC
.getPassNameForClassName(ClassName
);
480 return PassName
.empty() ? ClassName
: PassName
;
486 // Now that we have all of the passes ready, run them.
490 if (OK
!= OK_NoOutput
) {
492 if (OK
== OK_OutputThinLTOBitcode
&& ThinLTOLinkOut
)
493 ThinLTOLinkOut
->keep();
497 OptRemarkFile
->keep();
499 if (DebugifyEach
&& !DebugifyExport
.empty())
500 exportDebugifyStats(DebugifyExport
, Debugify
.StatsMap
);
505 void llvm::printPasses(raw_ostream
&OS
) {
507 PB
.printPassNames(OS
);