[lit] Remove LitTestCase
[llvm-complete.git] / tools / opt / NewPMDriver.cpp
blobe032c40a67ed2660cb62c014314021a83aec0224
1 //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
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 /// \file
9 ///
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.
12 ///
13 //===----------------------------------------------------------------------===//
15 #include "NewPMDriver.h"
16 #include "Debugify.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"
39 using namespace llvm;
40 using namespace opt_tool;
42 static cl::opt<bool>
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"),
57 cl::Hidden);
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(
62 "passes-ep-peephole",
63 cl::desc("A textual description of the function pass pipeline inserted at "
64 "the Peephole extension points into default pipelines"),
65 cl::Hidden);
66 static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
67 "passes-ep-late-loop-optimizations",
68 cl::desc(
69 "A textual description of the loop pass pipeline inserted at "
70 "the LateLoopOptimizations extension point into default pipelines"),
71 cl::Hidden);
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"),
76 cl::Hidden);
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"),
81 cl::Hidden);
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"),
86 cl::Hidden);
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"),
91 cl::Hidden);
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"),
96 cl::Hidden);
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"),
101 cl::Hidden);
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."),
108 cl::Hidden);
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."));
112 /// @}}
114 template <typename PassManagerT>
115 bool tryParsePipelineText(PassBuilder &PB,
116 const cl::opt<std::string> &PipelineOpt) {
117 if (PipelineOpt.empty())
118 return false;
120 // Verify the pipeline is parseable:
121 PassManagerT PM;
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";
126 return false;
128 return true;
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,
134 bool DebugLogging) {
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,
141 DebugLogging));
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,
190 DebugLogging));
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,
198 DebugLogging));
202 #ifdef LINK_POLLY_INTO_TOOLS
203 namespace polly {
204 void RegisterPollyPasses(PassBuilder &);
206 #endif
208 bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
209 ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
210 ToolOutputFile *OptRemarkFile,
211 StringRef PassPipeline, OutputKind OK,
212 VerifierKind VK,
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) {
221 case InstrGen:
222 P = PGOOptions(ProfileFile, "", "", "", true);
223 break;
224 case InstrUse:
225 P = PGOOptions("", ProfileFile, "", ProfileRemappingFile, false);
226 break;
227 case SampleUse:
228 P = PGOOptions("", "", ProfileFile, ProfileRemappingFile, false);
229 break;
230 case NoPGO:
231 if (DebugInfoForProfiling)
232 P = PGOOptions("", "", "", "", false, true);
233 else
234 P = None;
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);
246 if (!PassPlugin) {
247 errs() << "Failed to load passes from '" << PluginFN
248 << "'. Request ignored.\n";
249 continue;
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());
261 return true;
262 } else if (Name == "check-debugify") {
263 MPM.addPass(NewPMCheckDebugifyPass());
264 return true;
266 return false;
269 #ifdef LINK_POLLY_INTO_TOOLS
270 polly::RegisterPollyPasses(PB);
271 #endif
273 // Specially handle the alias analysis manager so that we can register
274 // a custom pipeline of AA passes with it.
275 AAManager AA;
276 if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
277 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
278 return false;
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());
299 if (EnableDebugify)
300 MPM.addPass(NewPMDebugifyPass());
302 if (auto Err =
303 PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
304 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
305 return false;
308 if (VK > VK_NoVerifier)
309 MPM.addPass(VerifierPass());
310 if (EnableDebugify)
311 MPM.addPass(NewPMCheckDebugifyPass());
313 // Add any relevant output pass at the end of the pipeline.
314 switch (OK) {
315 case OK_NoOutput:
316 break; // No output pass needed.
317 case OK_OutputAssembly:
318 MPM.addPass(
319 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
320 break;
321 case OK_OutputBitcode:
322 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
323 EmitSummaryIndex, EmitModuleHash));
324 break;
325 case OK_OutputThinLTOBitcode:
326 MPM.addPass(ThinLTOBitcodeWriterPass(
327 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
328 break;
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.
335 MPM.run(M, MAM);
337 // Declare success.
338 if (OK != OK_NoOutput) {
339 Out->keep();
340 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
341 ThinLTOLinkOut->keep();
344 if (OptRemarkFile)
345 OptRemarkFile->keep();
347 return true;