xref: /freebsd/contrib/llvm-project/llvm/tools/opt/NewPMDriver.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric ///
100b57cec5SDimitry Andric /// This file is just a split of the code that logically belongs in opt.cpp but
110b57cec5SDimitry Andric /// that includes the new pass manager headers.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "NewPMDriver.h"
165ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
190b57cec5SDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h"
20e8d8bef9SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
210b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
220b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
230b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
240b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
250b57cec5SDimitry Andric #include "llvm/IR/Module.h"
260b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
270b57cec5SDimitry Andric #include "llvm/IR/Verifier.h"
28bdd1243dSDimitry Andric #include "llvm/IRPrinter/IRPrintingPasses.h"
290b57cec5SDimitry Andric #include "llvm/Passes/PassBuilder.h"
300b57cec5SDimitry Andric #include "llvm/Passes/PassPlugin.h"
310b57cec5SDimitry Andric #include "llvm/Passes/StandardInstrumentations.h"
320b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
330b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
34*06c3fb27SDimitry Andric #include "llvm/Support/VirtualFileSystem.h"
35bdd1243dSDimitry Andric #include "llvm/Support/raw_ostream.h"
360b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
370b57cec5SDimitry Andric #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
38e8d8bef9SDimitry Andric #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
390b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h"
40480093f4SDimitry Andric #include "llvm/Transforms/Utils/Debugify.h"
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric using namespace llvm;
430b57cec5SDimitry Andric using namespace opt_tool;
440b57cec5SDimitry Andric 
45e8d8bef9SDimitry Andric namespace llvm {
46e8d8bef9SDimitry Andric cl::opt<bool> DebugifyEach(
47e8d8bef9SDimitry Andric     "debugify-each",
48e8d8bef9SDimitry Andric     cl::desc("Start each pass with debugify and end it with check-debugify"));
49e8d8bef9SDimitry Andric 
50e8d8bef9SDimitry Andric cl::opt<std::string>
51e8d8bef9SDimitry Andric     DebugifyExport("debugify-export",
52e8d8bef9SDimitry Andric                    cl::desc("Export per-pass debugify statistics to this file"),
53e8d8bef9SDimitry Andric                    cl::value_desc("filename"));
54753f127fSDimitry Andric 
55753f127fSDimitry Andric cl::opt<bool> VerifyEachDebugInfoPreserve(
56753f127fSDimitry Andric     "verify-each-debuginfo-preserve",
57753f127fSDimitry Andric     cl::desc("Start each pass with collecting and end it with checking of "
58753f127fSDimitry Andric              "debug info preservation."));
59753f127fSDimitry Andric 
60753f127fSDimitry Andric cl::opt<std::string>
61753f127fSDimitry Andric     VerifyDIPreserveExport("verify-di-preserve-export",
62753f127fSDimitry Andric                    cl::desc("Export debug info preservation failures into "
63753f127fSDimitry Andric                             "specified (JSON) file (should be abs path as we use"
64753f127fSDimitry Andric                             " append mode to insert new JSON objects)"),
65753f127fSDimitry Andric                    cl::value_desc("filename"), cl::init(""));
66753f127fSDimitry Andric 
67e8d8bef9SDimitry Andric } // namespace llvm
68e8d8bef9SDimitry Andric 
69fe6060f1SDimitry Andric enum class DebugLogging { None, Normal, Verbose, Quiet };
70fe6060f1SDimitry Andric 
71fe6060f1SDimitry Andric static cl::opt<DebugLogging> DebugPM(
72fe6060f1SDimitry Andric     "debug-pass-manager", cl::Hidden, cl::ValueOptional,
73fe6060f1SDimitry Andric     cl::desc("Print pass management debugging information"),
74fe6060f1SDimitry Andric     cl::init(DebugLogging::None),
75fe6060f1SDimitry Andric     cl::values(
76fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Normal, "", ""),
77fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Quiet, "quiet",
78fe6060f1SDimitry Andric                    "Skip printing info about analyses"),
79fe6060f1SDimitry Andric         clEnumValN(
80fe6060f1SDimitry Andric             DebugLogging::Verbose, "verbose",
81fe6060f1SDimitry Andric             "Print extra information about adaptors and pass managers")));
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric // This flag specifies a textual description of the alias analysis pipeline to
840b57cec5SDimitry Andric // use when querying for aliasing information. It only works in concert with
850b57cec5SDimitry Andric // the "passes" flag above.
860b57cec5SDimitry Andric static cl::opt<std::string>
870b57cec5SDimitry Andric     AAPipeline("aa-pipeline",
880b57cec5SDimitry Andric                cl::desc("A textual description of the alias analysis "
890b57cec5SDimitry Andric                         "pipeline for handling managed aliasing queries"),
90e8d8bef9SDimitry Andric                cl::Hidden, cl::init("default"));
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric /// {{@ These options accept textual pipeline descriptions which will be
930b57cec5SDimitry Andric /// inserted into default pipelines at the respective extension points
940b57cec5SDimitry Andric static cl::opt<std::string> PeepholeEPPipeline(
950b57cec5SDimitry Andric     "passes-ep-peephole",
960b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
970b57cec5SDimitry Andric              "the Peephole extension points into default pipelines"),
980b57cec5SDimitry Andric     cl::Hidden);
990b57cec5SDimitry Andric static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
1000b57cec5SDimitry Andric     "passes-ep-late-loop-optimizations",
1010b57cec5SDimitry Andric     cl::desc(
1020b57cec5SDimitry Andric         "A textual description of the loop pass pipeline inserted at "
1030b57cec5SDimitry Andric         "the LateLoopOptimizations extension point into default pipelines"),
1040b57cec5SDimitry Andric     cl::Hidden);
1050b57cec5SDimitry Andric static cl::opt<std::string> LoopOptimizerEndEPPipeline(
1060b57cec5SDimitry Andric     "passes-ep-loop-optimizer-end",
1070b57cec5SDimitry Andric     cl::desc("A textual description of the loop pass pipeline inserted at "
1080b57cec5SDimitry Andric              "the LoopOptimizerEnd extension point into default pipelines"),
1090b57cec5SDimitry Andric     cl::Hidden);
1100b57cec5SDimitry Andric static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
1110b57cec5SDimitry Andric     "passes-ep-scalar-optimizer-late",
1120b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1130b57cec5SDimitry Andric              "the ScalarOptimizerLate extension point into default pipelines"),
1140b57cec5SDimitry Andric     cl::Hidden);
1150b57cec5SDimitry Andric static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
1160b57cec5SDimitry Andric     "passes-ep-cgscc-optimizer-late",
1170b57cec5SDimitry Andric     cl::desc("A textual description of the cgscc pass pipeline inserted at "
1180b57cec5SDimitry Andric              "the CGSCCOptimizerLate extension point into default pipelines"),
1190b57cec5SDimitry Andric     cl::Hidden);
1200b57cec5SDimitry Andric static cl::opt<std::string> VectorizerStartEPPipeline(
1210b57cec5SDimitry Andric     "passes-ep-vectorizer-start",
1220b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1230b57cec5SDimitry Andric              "the VectorizerStart extension point into default pipelines"),
1240b57cec5SDimitry Andric     cl::Hidden);
1250b57cec5SDimitry Andric static cl::opt<std::string> PipelineStartEPPipeline(
1260b57cec5SDimitry Andric     "passes-ep-pipeline-start",
127e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
1280b57cec5SDimitry Andric              "the PipelineStart extension point into default pipelines"),
1290b57cec5SDimitry Andric     cl::Hidden);
130e8d8bef9SDimitry Andric static cl::opt<std::string> PipelineEarlySimplificationEPPipeline(
131e8d8bef9SDimitry Andric     "passes-ep-pipeline-early-simplification",
132e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
133e8d8bef9SDimitry Andric              "the EarlySimplification extension point into default pipelines"),
134e8d8bef9SDimitry Andric     cl::Hidden);
13581ad6265SDimitry Andric static cl::opt<std::string> OptimizerEarlyEPPipeline(
13681ad6265SDimitry Andric     "passes-ep-optimizer-early",
13781ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
13881ad6265SDimitry Andric              "the OptimizerEarly extension point into default pipelines"),
13981ad6265SDimitry Andric     cl::Hidden);
1400b57cec5SDimitry Andric static cl::opt<std::string> OptimizerLastEPPipeline(
1410b57cec5SDimitry Andric     "passes-ep-optimizer-last",
142e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
1430b57cec5SDimitry Andric              "the OptimizerLast extension point into default pipelines"),
1440b57cec5SDimitry Andric     cl::Hidden);
14581ad6265SDimitry Andric static cl::opt<std::string> FullLinkTimeOptimizationEarlyEPPipeline(
14681ad6265SDimitry Andric     "passes-ep-full-link-time-optimization-early",
14781ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
14881ad6265SDimitry Andric              "the FullLinkTimeOptimizationEarly extension point into default "
14981ad6265SDimitry Andric              "pipelines"),
15081ad6265SDimitry Andric     cl::Hidden);
15181ad6265SDimitry Andric static cl::opt<std::string> FullLinkTimeOptimizationLastEPPipeline(
15281ad6265SDimitry Andric     "passes-ep-full-link-time-optimization-last",
15381ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
15481ad6265SDimitry Andric              "the FullLinkTimeOptimizationLast extension point into default "
15581ad6265SDimitry Andric              "pipelines"),
15681ad6265SDimitry Andric     cl::Hidden);
157bdd1243dSDimitry Andric /// @}}
1580b57cec5SDimitry Andric 
159bdd1243dSDimitry Andric static cl::opt<bool> DisablePipelineVerification(
160bdd1243dSDimitry Andric     "disable-pipeline-verification",
161bdd1243dSDimitry Andric     cl::desc("Only has an effect when specified with -print-pipeline-passes. "
162bdd1243dSDimitry Andric              "Disables verifying that the textual pipeline generated by "
163bdd1243dSDimitry Andric              "-print-pipeline-passes can be used to create a pipeline."),
164bdd1243dSDimitry Andric     cl::Hidden);
1655ffd83dbSDimitry Andric 
166bdd1243dSDimitry Andric 
167bdd1243dSDimitry Andric static cl::opt<PGOKind>
168bdd1243dSDimitry Andric     PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
169bdd1243dSDimitry Andric                 cl::desc("The kind of profile guided optimization"),
170bdd1243dSDimitry Andric                 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
171bdd1243dSDimitry Andric                            clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
172bdd1243dSDimitry Andric                                       "Instrument the IR to generate profile."),
173bdd1243dSDimitry Andric                            clEnumValN(InstrUse, "pgo-instr-use-pipeline",
174bdd1243dSDimitry Andric                                       "Use instrumented profile to guide PGO."),
175bdd1243dSDimitry Andric                            clEnumValN(SampleUse, "pgo-sample-use-pipeline",
176bdd1243dSDimitry Andric                                       "Use sampled profile to guide PGO.")));
177bdd1243dSDimitry Andric static cl::opt<std::string> ProfileFile("profile-file",
178bdd1243dSDimitry Andric                                  cl::desc("Path to the profile."), cl::Hidden);
179*06c3fb27SDimitry Andric static cl::opt<std::string>
180*06c3fb27SDimitry Andric     MemoryProfileFile("memory-profile-file",
181*06c3fb27SDimitry Andric                       cl::desc("Path to the memory profile."), cl::Hidden);
182bdd1243dSDimitry Andric 
183bdd1243dSDimitry Andric static cl::opt<CSPGOKind> CSPGOKindFlag(
184bdd1243dSDimitry Andric     "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
185bdd1243dSDimitry Andric     cl::desc("The kind of context sensitive profile guided optimization"),
186bdd1243dSDimitry Andric     cl::values(
187bdd1243dSDimitry Andric         clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
188bdd1243dSDimitry Andric         clEnumValN(
189bdd1243dSDimitry Andric             CSInstrGen, "cspgo-instr-gen-pipeline",
190bdd1243dSDimitry Andric             "Instrument (context sensitive) the IR to generate profile."),
191bdd1243dSDimitry Andric         clEnumValN(
192bdd1243dSDimitry Andric             CSInstrUse, "cspgo-instr-use-pipeline",
193bdd1243dSDimitry Andric             "Use instrumented (context sensitive) profile to guide PGO.")));
194bdd1243dSDimitry Andric 
195bdd1243dSDimitry Andric static cl::opt<std::string> CSProfileGenFile(
196bdd1243dSDimitry Andric     "cs-profilegen-file",
197bdd1243dSDimitry Andric     cl::desc("Path to the instrumented context sensitive profile."),
198bdd1243dSDimitry Andric     cl::Hidden);
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric static cl::opt<std::string>
2010b57cec5SDimitry Andric     ProfileRemappingFile("profile-remapping-file",
2020b57cec5SDimitry Andric                          cl::desc("Path to the profile remapping file."),
2030b57cec5SDimitry Andric                          cl::Hidden);
204bdd1243dSDimitry Andric 
2050b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling(
206bdd1243dSDimitry Andric     "debug-info-for-profiling", cl::init(false), cl::Hidden,
2070b57cec5SDimitry Andric     cl::desc("Emit special debug info to enable PGO profile generation."));
208bdd1243dSDimitry Andric 
209e8d8bef9SDimitry Andric static cl::opt<bool> PseudoProbeForProfiling(
210bdd1243dSDimitry Andric     "pseudo-probe-for-profiling", cl::init(false), cl::Hidden,
211e8d8bef9SDimitry Andric     cl::desc("Emit pseudo probes to enable PGO profile generation."));
212bdd1243dSDimitry Andric 
213bdd1243dSDimitry Andric static cl::opt<bool> DisableLoopUnrolling(
214bdd1243dSDimitry Andric     "disable-loop-unrolling",
215bdd1243dSDimitry Andric     cl::desc("Disable loop unrolling in all relevant passes"), cl::init(false));
216bdd1243dSDimitry Andric 
217bdd1243dSDimitry Andric namespace llvm {
218bdd1243dSDimitry Andric extern cl::opt<bool> PrintPipelinePasses;
219bdd1243dSDimitry Andric } // namespace llvm
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric template <typename PassManagerT>
2220b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB,
2230b57cec5SDimitry Andric                           const cl::opt<std::string> &PipelineOpt) {
2240b57cec5SDimitry Andric   if (PipelineOpt.empty())
2250b57cec5SDimitry Andric     return false;
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   // Verify the pipeline is parseable:
2280b57cec5SDimitry Andric   PassManagerT PM;
2290b57cec5SDimitry Andric   if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
2300b57cec5SDimitry Andric     errs() << "Could not parse -" << PipelineOpt.ArgStr
2310b57cec5SDimitry Andric            << " pipeline: " << toString(std::move(Err))
2320b57cec5SDimitry Andric            << "... I'm going to ignore it.\n";
2330b57cec5SDimitry Andric     return false;
2340b57cec5SDimitry Andric   }
2350b57cec5SDimitry Andric   return true;
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks
2390b57cec5SDimitry Andric /// for parsing and inserting the given pipeline
240e8d8bef9SDimitry Andric static void registerEPCallbacks(PassBuilder &PB) {
2410b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
2420b57cec5SDimitry Andric     PB.registerPeepholeEPCallback(
243349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2440b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
245e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PeepholeEPPipeline));
2460b57cec5SDimitry Andric         });
2470b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB,
2480b57cec5SDimitry Andric                                             LateLoopOptimizationsEPPipeline))
2490b57cec5SDimitry Andric     PB.registerLateLoopOptimizationsEPCallback(
250349cc55cSDimitry Andric         [&PB](LoopPassManager &PM, OptimizationLevel Level) {
2510b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
252e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline));
2530b57cec5SDimitry Andric         });
2540b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
2550b57cec5SDimitry Andric     PB.registerLoopOptimizerEndEPCallback(
256349cc55cSDimitry Andric         [&PB](LoopPassManager &PM, OptimizationLevel Level) {
2570b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
258e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline));
2590b57cec5SDimitry Andric         });
2600b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB,
2610b57cec5SDimitry Andric                                                 ScalarOptimizerLateEPPipeline))
2620b57cec5SDimitry Andric     PB.registerScalarOptimizerLateEPCallback(
263349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2640b57cec5SDimitry Andric           ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
265e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline));
2660b57cec5SDimitry Andric         });
2670b57cec5SDimitry Andric   if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
2680b57cec5SDimitry Andric     PB.registerCGSCCOptimizerLateEPCallback(
269349cc55cSDimitry Andric         [&PB](CGSCCPassManager &PM, OptimizationLevel Level) {
2700b57cec5SDimitry Andric           ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
271e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline));
2720b57cec5SDimitry Andric         });
2730b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
2740b57cec5SDimitry Andric     PB.registerVectorizerStartEPCallback(
275349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2760b57cec5SDimitry Andric           ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
277e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline));
2780b57cec5SDimitry Andric         });
2790b57cec5SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
2800b57cec5SDimitry Andric     PB.registerPipelineStartEPCallback(
281349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
2820b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
283e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline));
284e8d8bef9SDimitry Andric         });
285e8d8bef9SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
286e8d8bef9SDimitry Andric           PB, PipelineEarlySimplificationEPPipeline))
287e8d8bef9SDimitry Andric     PB.registerPipelineEarlySimplificationEPCallback(
288349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
289e8d8bef9SDimitry Andric           ExitOnError Err("Unable to parse EarlySimplification pipeline: ");
290e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline));
2910b57cec5SDimitry Andric         });
29281ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, OptimizerEarlyEPPipeline))
29381ad6265SDimitry Andric     PB.registerOptimizerEarlyEPCallback(
29481ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
29581ad6265SDimitry Andric           ExitOnError Err("Unable to parse OptimizerEarlyEP pipeline: ");
29681ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerEarlyEPPipeline));
29781ad6265SDimitry Andric         });
29881ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, OptimizerLastEPPipeline))
2990b57cec5SDimitry Andric     PB.registerOptimizerLastEPCallback(
300349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
3010b57cec5SDimitry Andric           ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
302e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline));
3030b57cec5SDimitry Andric         });
30481ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
30581ad6265SDimitry Andric           PB, FullLinkTimeOptimizationEarlyEPPipeline))
30681ad6265SDimitry Andric     PB.registerFullLinkTimeOptimizationEarlyEPCallback(
30781ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
30881ad6265SDimitry Andric           ExitOnError Err(
30981ad6265SDimitry Andric               "Unable to parse FullLinkTimeOptimizationEarlyEP pipeline: ");
31081ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM,
31181ad6265SDimitry Andric                                    FullLinkTimeOptimizationEarlyEPPipeline));
31281ad6265SDimitry Andric         });
31381ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
31481ad6265SDimitry Andric           PB, FullLinkTimeOptimizationLastEPPipeline))
31581ad6265SDimitry Andric     PB.registerFullLinkTimeOptimizationLastEPCallback(
31681ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
31781ad6265SDimitry Andric           ExitOnError Err(
31881ad6265SDimitry Andric               "Unable to parse FullLinkTimeOptimizationLastEP pipeline: ");
31981ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM, FullLinkTimeOptimizationLastEPPipeline));
32081ad6265SDimitry Andric         });
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
323480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
324480093f4SDimitry Andric   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
325480093f4SDimitry Andric #include "llvm/Support/Extension.def"
3260b57cec5SDimitry Andric 
327*06c3fb27SDimitry Andric bool llvm::runPassPipeline(
328*06c3fb27SDimitry Andric     StringRef Arg0, Module &M, TargetMachine *TM, TargetLibraryInfoImpl *TLII,
329*06c3fb27SDimitry Andric     ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
330*06c3fb27SDimitry Andric     ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
331*06c3fb27SDimitry Andric     ArrayRef<PassPlugin> PassPlugins, OutputKind OK, VerifierKind VK,
3320b57cec5SDimitry Andric     bool ShouldPreserveAssemblyUseListOrder,
333*06c3fb27SDimitry Andric     bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,
334*06c3fb27SDimitry Andric     bool EmitModuleHash, bool EnableDebugify, bool VerifyDIPreserve,
335*06c3fb27SDimitry Andric     bool UnifiedLTO) {
3360b57cec5SDimitry Andric   bool VerifyEachPass = VK == VK_VerifyEachPass;
3370b57cec5SDimitry Andric 
338*06c3fb27SDimitry Andric   auto FS = vfs::getRealFileSystem();
339bdd1243dSDimitry Andric   std::optional<PGOOptions> P;
3400b57cec5SDimitry Andric   switch (PGOKindFlag) {
3410b57cec5SDimitry Andric   case InstrGen:
342*06c3fb27SDimitry Andric     P = PGOOptions(ProfileFile, "", "", MemoryProfileFile, FS,
343*06c3fb27SDimitry Andric                    PGOOptions::IRInstr);
3440b57cec5SDimitry Andric     break;
3450b57cec5SDimitry Andric   case InstrUse:
346*06c3fb27SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile, MemoryProfileFile, FS,
347*06c3fb27SDimitry Andric                    PGOOptions::IRUse);
3480b57cec5SDimitry Andric     break;
3490b57cec5SDimitry Andric   case SampleUse:
350*06c3fb27SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile, MemoryProfileFile, FS,
3510b57cec5SDimitry Andric                    PGOOptions::SampleUse);
3520b57cec5SDimitry Andric     break;
3530b57cec5SDimitry Andric   case NoPGO:
354*06c3fb27SDimitry Andric     if (DebugInfoForProfiling || PseudoProbeForProfiling ||
355*06c3fb27SDimitry Andric         !MemoryProfileFile.empty())
356*06c3fb27SDimitry Andric       P = PGOOptions("", "", "", MemoryProfileFile, FS, PGOOptions::NoAction,
357*06c3fb27SDimitry Andric                      PGOOptions::NoCSAction, DebugInfoForProfiling,
358*06c3fb27SDimitry Andric                      PseudoProbeForProfiling);
3590b57cec5SDimitry Andric     else
360bdd1243dSDimitry Andric       P = std::nullopt;
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric   if (CSPGOKindFlag != NoCSPGO) {
3630b57cec5SDimitry Andric     if (P && (P->Action == PGOOptions::IRInstr ||
364bdd1243dSDimitry Andric               P->Action == PGOOptions::SampleUse)) {
3650b57cec5SDimitry Andric       errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
366bdd1243dSDimitry Andric       return false;
367bdd1243dSDimitry Andric     }
3680b57cec5SDimitry Andric     if (CSPGOKindFlag == CSInstrGen) {
369bdd1243dSDimitry Andric       if (CSProfileGenFile.empty()) {
3700b57cec5SDimitry Andric         errs() << "CSInstrGen needs to specify CSProfileGenFile";
371bdd1243dSDimitry Andric         return false;
372bdd1243dSDimitry Andric       }
3730b57cec5SDimitry Andric       if (P) {
3740b57cec5SDimitry Andric         P->CSAction = PGOOptions::CSIRInstr;
3750b57cec5SDimitry Andric         P->CSProfileGenFile = CSProfileGenFile;
3760b57cec5SDimitry Andric       } else
3770b57cec5SDimitry Andric         P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
378*06c3fb27SDimitry Andric                        /*MemoryProfile=*/"", FS, PGOOptions::NoAction,
379*06c3fb27SDimitry Andric                        PGOOptions::CSIRInstr);
3800b57cec5SDimitry Andric     } else /* CSPGOKindFlag == CSInstrUse */ {
381bdd1243dSDimitry Andric       if (!P) {
3820b57cec5SDimitry Andric         errs() << "CSInstrUse needs to be together with InstrUse";
383bdd1243dSDimitry Andric         return false;
384bdd1243dSDimitry Andric       }
3850b57cec5SDimitry Andric       P->CSAction = PGOOptions::CSIRUse;
3860b57cec5SDimitry Andric     }
3870b57cec5SDimitry Andric   }
388349cc55cSDimitry Andric   if (TM)
389349cc55cSDimitry Andric     TM->setPGOOption(P);
390349cc55cSDimitry Andric 
391fe6060f1SDimitry Andric   LoopAnalysisManager LAM;
392fe6060f1SDimitry Andric   FunctionAnalysisManager FAM;
393fe6060f1SDimitry Andric   CGSCCAnalysisManager CGAM;
394fe6060f1SDimitry Andric   ModuleAnalysisManager MAM;
395fe6060f1SDimitry Andric 
3960b57cec5SDimitry Andric   PassInstrumentationCallbacks PIC;
397fe6060f1SDimitry Andric   PrintPassOptions PrintPassOpts;
398fe6060f1SDimitry Andric   PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
399fe6060f1SDimitry Andric   PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
400bdd1243dSDimitry Andric   StandardInstrumentations SI(M.getContext(), DebugPM != DebugLogging::None,
401bdd1243dSDimitry Andric                               VerifyEachPass, PrintPassOpts);
402*06c3fb27SDimitry Andric   SI.registerCallbacks(PIC, &MAM);
403e8d8bef9SDimitry Andric   DebugifyEachInstrumentation Debugify;
404753f127fSDimitry Andric   DebugifyStatsMap DIStatsMap;
405753f127fSDimitry Andric   DebugInfoPerPass DebugInfoBeforePass;
406753f127fSDimitry Andric   if (DebugifyEach) {
407753f127fSDimitry Andric     Debugify.setDIStatsMap(DIStatsMap);
408753f127fSDimitry Andric     Debugify.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
409*06c3fb27SDimitry Andric     Debugify.registerCallbacks(PIC, MAM);
410753f127fSDimitry Andric   } else if (VerifyEachDebugInfoPreserve) {
411753f127fSDimitry Andric     Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
412753f127fSDimitry Andric     Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
413753f127fSDimitry Andric     Debugify.setOrigDIVerifyBugsReportFilePath(
414753f127fSDimitry Andric       VerifyDIPreserveExport);
415*06c3fb27SDimitry Andric     Debugify.registerCallbacks(PIC, MAM);
416753f127fSDimitry Andric   }
4170b57cec5SDimitry Andric 
4185ffd83dbSDimitry Andric   PipelineTuningOptions PTO;
4195ffd83dbSDimitry Andric   // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
4205ffd83dbSDimitry Andric   // to false above so we shouldn't necessarily need to check whether or not the
4215ffd83dbSDimitry Andric   // option has been enabled.
4225ffd83dbSDimitry Andric   PTO.LoopUnrolling = !DisableLoopUnrolling;
423*06c3fb27SDimitry Andric   PTO.UnifiedLTO = UnifiedLTO;
424fe6060f1SDimitry Andric   PassBuilder PB(TM, PTO, P, &PIC);
425e8d8bef9SDimitry Andric   registerEPCallbacks(PB);
4260b57cec5SDimitry Andric 
42781ad6265SDimitry Andric   // For any loaded plugins, let them register pass builder callbacks.
42881ad6265SDimitry Andric   for (auto &PassPlugin : PassPlugins)
42981ad6265SDimitry Andric     PassPlugin.registerPassBuilderCallbacks(PB);
4300b57cec5SDimitry Andric 
431480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
432480093f4SDimitry Andric   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
433480093f4SDimitry Andric #include "llvm/Support/Extension.def"
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   // Specially handle the alias analysis manager so that we can register
4360b57cec5SDimitry Andric   // a custom pipeline of AA passes with it.
4370b57cec5SDimitry Andric   AAManager AA;
4380b57cec5SDimitry Andric   if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
4390b57cec5SDimitry Andric     errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4400b57cec5SDimitry Andric     return false;
4410b57cec5SDimitry Andric   }
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   // Register the AA manager first so that our version is the one used.
4440b57cec5SDimitry Andric   FAM.registerPass([&] { return std::move(AA); });
445e8d8bef9SDimitry Andric   // Register our TargetLibraryInfoImpl.
446e8d8bef9SDimitry Andric   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   // Register all the basic analyses with the managers.
4490b57cec5SDimitry Andric   PB.registerModuleAnalyses(MAM);
4500b57cec5SDimitry Andric   PB.registerCGSCCAnalyses(CGAM);
4510b57cec5SDimitry Andric   PB.registerFunctionAnalyses(FAM);
4520b57cec5SDimitry Andric   PB.registerLoopAnalyses(LAM);
4530b57cec5SDimitry Andric   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
4540b57cec5SDimitry Andric 
455fe6060f1SDimitry Andric   ModulePassManager MPM;
4560b57cec5SDimitry Andric   if (EnableDebugify)
4570b57cec5SDimitry Andric     MPM.addPass(NewPMDebugifyPass());
458753f127fSDimitry Andric   if (VerifyDIPreserve)
459753f127fSDimitry Andric     MPM.addPass(NewPMDebugifyPass(DebugifyMode::OriginalDebugInfo, "",
460753f127fSDimitry Andric                                   &DebugInfoBeforePass));
4610b57cec5SDimitry Andric 
462349cc55cSDimitry Andric   // Add passes according to the -passes options.
4635ffd83dbSDimitry Andric   if (!PassPipeline.empty()) {
464e8d8bef9SDimitry Andric     if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
4650b57cec5SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4660b57cec5SDimitry Andric       return false;
4670b57cec5SDimitry Andric     }
4685ffd83dbSDimitry Andric   }
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
4710b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
4720b57cec5SDimitry Andric   if (EnableDebugify)
473753f127fSDimitry Andric     MPM.addPass(NewPMCheckDebugifyPass(false, "", &DIStatsMap));
474753f127fSDimitry Andric   if (VerifyDIPreserve)
475753f127fSDimitry Andric     MPM.addPass(NewPMCheckDebugifyPass(
476bdd1243dSDimitry Andric         false, "", nullptr, DebugifyMode::OriginalDebugInfo,
477bdd1243dSDimitry Andric         &DebugInfoBeforePass, VerifyDIPreserveExport));
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   // Add any relevant output pass at the end of the pipeline.
4800b57cec5SDimitry Andric   switch (OK) {
4810b57cec5SDimitry Andric   case OK_NoOutput:
4820b57cec5SDimitry Andric     break; // No output pass needed.
4830b57cec5SDimitry Andric   case OK_OutputAssembly:
484bdd1243dSDimitry Andric     MPM.addPass(PrintModulePass(
485bdd1243dSDimitry Andric         Out->os(), "", ShouldPreserveAssemblyUseListOrder, EmitSummaryIndex));
4860b57cec5SDimitry Andric     break;
4870b57cec5SDimitry Andric   case OK_OutputBitcode:
4880b57cec5SDimitry Andric     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
4890b57cec5SDimitry Andric                                   EmitSummaryIndex, EmitModuleHash));
4900b57cec5SDimitry Andric     break;
4910b57cec5SDimitry Andric   case OK_OutputThinLTOBitcode:
4920b57cec5SDimitry Andric     MPM.addPass(ThinLTOBitcodeWriterPass(
4930b57cec5SDimitry Andric         Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
4940b57cec5SDimitry Andric     break;
4950b57cec5SDimitry Andric   }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   // Before executing passes, print the final values of the LLVM options.
4980b57cec5SDimitry Andric   cl::PrintOptionValues();
4990b57cec5SDimitry Andric 
500349cc55cSDimitry Andric   // Print a textual, '-passes=' compatible, representation of pipeline if
501349cc55cSDimitry Andric   // requested.
502349cc55cSDimitry Andric   if (PrintPipelinePasses) {
503bdd1243dSDimitry Andric     std::string Pipeline;
504bdd1243dSDimitry Andric     raw_string_ostream SOS(Pipeline);
505bdd1243dSDimitry Andric     MPM.printPipeline(SOS, [&PIC](StringRef ClassName) {
506349cc55cSDimitry Andric       auto PassName = PIC.getPassNameForClassName(ClassName);
507349cc55cSDimitry Andric       return PassName.empty() ? ClassName : PassName;
508349cc55cSDimitry Andric     });
509bdd1243dSDimitry Andric     outs() << Pipeline;
510349cc55cSDimitry Andric     outs() << "\n";
511bdd1243dSDimitry Andric 
512bdd1243dSDimitry Andric     if (!DisablePipelineVerification) {
513bdd1243dSDimitry Andric       // Check that we can parse the returned pipeline string as an actual
514bdd1243dSDimitry Andric       // pipeline.
515bdd1243dSDimitry Andric       ModulePassManager TempPM;
516bdd1243dSDimitry Andric       if (auto Err = PB.parsePassPipeline(TempPM, Pipeline)) {
517bdd1243dSDimitry Andric         errs() << "Could not parse dumped pass pipeline: "
518bdd1243dSDimitry Andric                << toString(std::move(Err)) << "\n";
519bdd1243dSDimitry Andric         return false;
520bdd1243dSDimitry Andric       }
521bdd1243dSDimitry Andric     }
522bdd1243dSDimitry Andric 
523349cc55cSDimitry Andric     return true;
524349cc55cSDimitry Andric   }
525349cc55cSDimitry Andric 
5260b57cec5SDimitry Andric   // Now that we have all of the passes ready, run them.
5270b57cec5SDimitry Andric   MPM.run(M, MAM);
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric   // Declare success.
5300b57cec5SDimitry Andric   if (OK != OK_NoOutput) {
5310b57cec5SDimitry Andric     Out->keep();
5320b57cec5SDimitry Andric     if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
5330b57cec5SDimitry Andric       ThinLTOLinkOut->keep();
5340b57cec5SDimitry Andric   }
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   if (OptRemarkFile)
5370b57cec5SDimitry Andric     OptRemarkFile->keep();
5380b57cec5SDimitry Andric 
539e8d8bef9SDimitry Andric   if (DebugifyEach && !DebugifyExport.empty())
540753f127fSDimitry Andric     exportDebugifyStats(DebugifyExport, Debugify.getDebugifyStatsMap());
541e8d8bef9SDimitry Andric 
5420b57cec5SDimitry Andric   return true;
5430b57cec5SDimitry Andric }
544fe6060f1SDimitry Andric 
545fe6060f1SDimitry Andric void llvm::printPasses(raw_ostream &OS) {
546fe6060f1SDimitry Andric   PassBuilder PB;
547fe6060f1SDimitry Andric   PB.printPassNames(OS);
548fe6060f1SDimitry Andric }
549