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 //===----------------------------------------------------------------------===//
14 
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/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/ToolOutputFile.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
38 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
39 #include "llvm/Transforms/Scalar/LoopPassManager.h"
40 #include "llvm/Transforms/Utils/Debugify.h"
41 
42 using namespace llvm;
43 using namespace opt_tool;
44 
45 static cl::opt<bool>
46     DebugPM("debug-pass-manager", cl::Hidden,
47             cl::desc("Print pass management debugging information"));
48 
49 static cl::list<std::string>
50     PassPlugins("load-pass-plugin",
51                 cl::desc("Load passes from plugin library"));
52 
53 // This flag specifies a textual description of the alias analysis pipeline to
54 // use when querying for aliasing information. It only works in concert with
55 // the "passes" flag above.
56 static cl::opt<std::string>
57     AAPipeline("aa-pipeline",
58                cl::desc("A textual description of the alias analysis "
59                         "pipeline for handling managed aliasing queries"),
60                cl::Hidden);
61 
62 /// {{@ These options accept textual pipeline descriptions which will be
63 /// inserted into default pipelines at the respective extension points
64 static cl::opt<std::string> PeepholeEPPipeline(
65     "passes-ep-peephole",
66     cl::desc("A textual description of the function pass pipeline inserted at "
67              "the Peephole extension points into default pipelines"),
68     cl::Hidden);
69 static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
70     "passes-ep-late-loop-optimizations",
71     cl::desc(
72         "A textual description of the loop pass pipeline inserted at "
73         "the LateLoopOptimizations extension point into default pipelines"),
74     cl::Hidden);
75 static cl::opt<std::string> LoopOptimizerEndEPPipeline(
76     "passes-ep-loop-optimizer-end",
77     cl::desc("A textual description of the loop pass pipeline inserted at "
78              "the LoopOptimizerEnd extension point into default pipelines"),
79     cl::Hidden);
80 static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
81     "passes-ep-scalar-optimizer-late",
82     cl::desc("A textual description of the function pass pipeline inserted at "
83              "the ScalarOptimizerLate extension point into default pipelines"),
84     cl::Hidden);
85 static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
86     "passes-ep-cgscc-optimizer-late",
87     cl::desc("A textual description of the cgscc pass pipeline inserted at "
88              "the CGSCCOptimizerLate extension point into default pipelines"),
89     cl::Hidden);
90 static cl::opt<std::string> VectorizerStartEPPipeline(
91     "passes-ep-vectorizer-start",
92     cl::desc("A textual description of the function pass pipeline inserted at "
93              "the VectorizerStart extension point into default pipelines"),
94     cl::Hidden);
95 static cl::opt<std::string> PipelineStartEPPipeline(
96     "passes-ep-pipeline-start",
97     cl::desc("A textual description of the function pass pipeline inserted at "
98              "the PipelineStart extension point into default pipelines"),
99     cl::Hidden);
100 static cl::opt<std::string> OptimizerLastEPPipeline(
101     "passes-ep-optimizer-last",
102     cl::desc("A textual description of the function pass pipeline inserted at "
103              "the OptimizerLast extension point into default pipelines"),
104     cl::Hidden);
105 
106 // Individual pipeline tuning options.
107 extern cl::opt<bool> DisableLoopUnrolling;
108 
109 extern cl::opt<PGOKind> PGOKindFlag;
110 extern cl::opt<std::string> ProfileFile;
111 extern cl::opt<CSPGOKind> CSPGOKindFlag;
112 extern cl::opt<std::string> CSProfileGenFile;
113 extern cl::opt<bool> DisableBasicAA;
114 
115 static cl::opt<std::string>
116     ProfileRemappingFile("profile-remapping-file",
117                          cl::desc("Path to the profile remapping file."),
118                          cl::Hidden);
119 static cl::opt<bool> DebugInfoForProfiling(
120     "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
121     cl::desc("Emit special debug info to enable PGO profile generation."));
122 /// @}}
123 
124 template <typename PassManagerT>
125 bool tryParsePipelineText(PassBuilder &PB,
126                           const cl::opt<std::string> &PipelineOpt) {
127   if (PipelineOpt.empty())
128     return false;
129 
130   // Verify the pipeline is parseable:
131   PassManagerT PM;
132   if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
133     errs() << "Could not parse -" << PipelineOpt.ArgStr
134            << " pipeline: " << toString(std::move(Err))
135            << "... I'm going to ignore it.\n";
136     return false;
137   }
138   return true;
139 }
140 
141 /// If one of the EPPipeline command line options was given, register callbacks
142 /// for parsing and inserting the given pipeline
143 static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
144                                 bool DebugLogging) {
145   if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
146     PB.registerPeepholeEPCallback(
147         [&PB, VerifyEachPass, DebugLogging](
148             FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
149           ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
150           Err(PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
151                                    DebugLogging));
152         });
153   if (tryParsePipelineText<LoopPassManager>(PB,
154                                             LateLoopOptimizationsEPPipeline))
155     PB.registerLateLoopOptimizationsEPCallback(
156         [&PB, VerifyEachPass, DebugLogging](
157             LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
158           ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
159           Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
160                                    VerifyEachPass, DebugLogging));
161         });
162   if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
163     PB.registerLoopOptimizerEndEPCallback(
164         [&PB, VerifyEachPass, DebugLogging](
165             LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
166           ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
167           Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline,
168                                    VerifyEachPass, DebugLogging));
169         });
170   if (tryParsePipelineText<FunctionPassManager>(PB,
171                                                 ScalarOptimizerLateEPPipeline))
172     PB.registerScalarOptimizerLateEPCallback(
173         [&PB, VerifyEachPass, DebugLogging](
174             FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
175           ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
176           Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
177                                    VerifyEachPass, DebugLogging));
178         });
179   if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
180     PB.registerCGSCCOptimizerLateEPCallback(
181         [&PB, VerifyEachPass, DebugLogging](
182             CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
183           ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
184           Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline,
185                                    VerifyEachPass, DebugLogging));
186         });
187   if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
188     PB.registerVectorizerStartEPCallback(
189         [&PB, VerifyEachPass, DebugLogging](
190             FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
191           ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
192           Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline,
193                                    VerifyEachPass, DebugLogging));
194         });
195   if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
196     PB.registerPipelineStartEPCallback(
197         [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) {
198           ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
199           Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass,
200                                    DebugLogging));
201         });
202   if (tryParsePipelineText<FunctionPassManager>(PB, OptimizerLastEPPipeline))
203     PB.registerOptimizerLastEPCallback(
204         [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM,
205                                             PassBuilder::OptimizationLevel) {
206           ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
207           Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline, VerifyEachPass,
208                                    DebugLogging));
209         });
210 }
211 
212 #define HANDLE_EXTENSION(Ext)                                                  \
213   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
214 #include "llvm/Support/Extension.def"
215 
216 bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
217                            TargetLibraryInfoImpl *TLII, ToolOutputFile *Out,
218                            ToolOutputFile *ThinLTOLinkOut,
219                            ToolOutputFile *OptRemarkFile,
220                            StringRef PassPipeline, ArrayRef<StringRef> Passes,
221                            OutputKind OK, VerifierKind VK,
222                            bool ShouldPreserveAssemblyUseListOrder,
223                            bool ShouldPreserveBitcodeUseListOrder,
224                            bool EmitSummaryIndex, bool EmitModuleHash,
225                            bool EnableDebugify, bool Coroutines) {
226   bool VerifyEachPass = VK == VK_VerifyEachPass;
227 
228   Optional<PGOOptions> P;
229   switch (PGOKindFlag) {
230   case InstrGen:
231     P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr);
232     break;
233   case InstrUse:
234     P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse);
235     break;
236   case SampleUse:
237     P = PGOOptions(ProfileFile, "", ProfileRemappingFile,
238                    PGOOptions::SampleUse);
239     break;
240   case NoPGO:
241     if (DebugInfoForProfiling)
242       P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
243                      true);
244     else
245       P = None;
246   }
247   if (CSPGOKindFlag != NoCSPGO) {
248     if (P && (P->Action == PGOOptions::IRInstr ||
249               P->Action == PGOOptions::SampleUse))
250       errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
251     if (CSPGOKindFlag == CSInstrGen) {
252       if (CSProfileGenFile.empty())
253         errs() << "CSInstrGen needs to specify CSProfileGenFile";
254       if (P) {
255         P->CSAction = PGOOptions::CSIRInstr;
256         P->CSProfileGenFile = CSProfileGenFile;
257       } else
258         P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
259                        PGOOptions::NoAction, PGOOptions::CSIRInstr);
260     } else /* CSPGOKindFlag == CSInstrUse */ {
261       if (!P)
262         errs() << "CSInstrUse needs to be together with InstrUse";
263       P->CSAction = PGOOptions::CSIRUse;
264     }
265   }
266   PassInstrumentationCallbacks PIC;
267   StandardInstrumentations SI(DebugPM);
268   SI.registerCallbacks(PIC);
269 
270   PipelineTuningOptions PTO;
271   // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
272   // to false above so we shouldn't necessarily need to check whether or not the
273   // option has been enabled.
274   PTO.LoopUnrolling = !DisableLoopUnrolling;
275   PTO.Coroutines = Coroutines;
276   PassBuilder PB(TM, PTO, P, &PIC);
277   registerEPCallbacks(PB, VerifyEachPass, DebugPM);
278 
279   // Load requested pass plugins and let them register pass builder callbacks
280   for (auto &PluginFN : PassPlugins) {
281     auto PassPlugin = PassPlugin::Load(PluginFN);
282     if (!PassPlugin) {
283       errs() << "Failed to load passes from '" << PluginFN
284              << "'. Request ignored.\n";
285       continue;
286     }
287 
288     PassPlugin->registerPassBuilderCallbacks(PB);
289   }
290 
291   // Register a callback that creates the debugify passes as needed.
292   PB.registerPipelineParsingCallback(
293       [](StringRef Name, ModulePassManager &MPM,
294          ArrayRef<PassBuilder::PipelineElement>) {
295         if (Name == "debugify") {
296           MPM.addPass(NewPMDebugifyPass());
297           return true;
298         } else if (Name == "check-debugify") {
299           MPM.addPass(NewPMCheckDebugifyPass());
300           return true;
301         }
302         return false;
303       });
304   PB.registerPipelineParsingCallback(
305       [](StringRef Name, ModulePassManager &MPM,
306          ArrayRef<PassBuilder::PipelineElement>) {
307         if (Name == "asan-pipeline") {
308           MPM.addPass(
309               RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
310           MPM.addPass(
311               createModuleToFunctionPassAdaptor(AddressSanitizerPass()));
312           MPM.addPass(ModuleAddressSanitizerPass());
313           return true;
314         } else if (Name == "asan-function-pipeline") {
315           MPM.addPass(
316               RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
317           MPM.addPass(
318               createModuleToFunctionPassAdaptor(AddressSanitizerPass()));
319           return true;
320         }
321         return false;
322       });
323 
324 #define HANDLE_EXTENSION(Ext)                                                  \
325   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
326 #include "llvm/Support/Extension.def"
327 
328   // Specially handle the alias analysis manager so that we can register
329   // a custom pipeline of AA passes with it.
330   AAManager AA;
331   if (!AAPipeline.empty()) {
332     assert(Passes.empty() &&
333            "--aa-pipeline and -foo-pass should not both be specified");
334     if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
335       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
336       return false;
337     }
338   }
339   // For compatibility with legacy pass manager.
340   // Alias analyses are not specially specified when using the legacy PM.
341   for (auto PassName : Passes) {
342     if (PB.isAAPassName(PassName)) {
343       if (auto Err = PB.parseAAPipeline(AA, PassName)) {
344         errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
345         return false;
346       }
347     }
348   }
349   // For compatibility with the legacy PM AA pipeline.
350   // AAResultsWrapperPass by default provides basic-aa in the legacy PM
351   // unless -disable-basic-aa is specified.
352   // TODO: remove this once tests implicitly requiring basic-aa use -passes= and
353   // -aa-pipeline=basic-aa.
354   if (!Passes.empty() && !DisableBasicAA) {
355     if (auto Err = PB.parseAAPipeline(AA, "basic-aa")) {
356       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
357       return false;
358     }
359   }
360 
361   LoopAnalysisManager LAM(DebugPM);
362   FunctionAnalysisManager FAM(DebugPM);
363   CGSCCAnalysisManager CGAM(DebugPM);
364   ModuleAnalysisManager MAM(DebugPM);
365 
366   // Register the AA manager first so that our version is the one used.
367   FAM.registerPass([&] { return std::move(AA); });
368   // Register our TargetLibraryInfoImpl.
369   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
370 
371   // Register all the basic analyses with the managers.
372   PB.registerModuleAnalyses(MAM);
373   PB.registerCGSCCAnalyses(CGAM);
374   PB.registerFunctionAnalyses(FAM);
375   PB.registerLoopAnalyses(LAM);
376   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
377 
378   ModulePassManager MPM(DebugPM);
379   if (VK > VK_NoVerifier)
380     MPM.addPass(VerifierPass());
381   if (EnableDebugify)
382     MPM.addPass(NewPMDebugifyPass());
383 
384   if (!PassPipeline.empty()) {
385     assert(Passes.empty() &&
386            "PassPipeline and Passes should not both contain passes");
387     if (auto Err =
388             PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
389       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
390       return false;
391     }
392   }
393   for (auto PassName : Passes) {
394     std::string ModifiedPassName(PassName.begin(), PassName.end());
395     if (PB.isAnalysisPassName(PassName))
396       ModifiedPassName = "require<" + ModifiedPassName + ">";
397     if (auto Err = PB.parsePassPipeline(MPM, ModifiedPassName, VerifyEachPass,
398                                         DebugPM)) {
399       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
400       return false;
401     }
402   }
403 
404   if (VK > VK_NoVerifier)
405     MPM.addPass(VerifierPass());
406   if (EnableDebugify)
407     MPM.addPass(NewPMCheckDebugifyPass());
408 
409   // Add any relevant output pass at the end of the pipeline.
410   switch (OK) {
411   case OK_NoOutput:
412     break; // No output pass needed.
413   case OK_OutputAssembly:
414     MPM.addPass(
415         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
416     break;
417   case OK_OutputBitcode:
418     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
419                                   EmitSummaryIndex, EmitModuleHash));
420     break;
421   case OK_OutputThinLTOBitcode:
422     MPM.addPass(ThinLTOBitcodeWriterPass(
423         Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
424     break;
425   }
426 
427   // Before executing passes, print the final values of the LLVM options.
428   cl::PrintOptionValues();
429 
430   // Now that we have all of the passes ready, run them.
431   MPM.run(M, MAM);
432 
433   // Declare success.
434   if (OK != OK_NoOutput) {
435     Out->keep();
436     if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
437       ThinLTOLinkOut->keep();
438   }
439 
440   if (OptRemarkFile)
441     OptRemarkFile->keep();
442 
443   return true;
444 }
445