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"
3406c3fb27SDimitry 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);
17906c3fb27SDimitry Andric static cl::opt<std::string>
18006c3fb27SDimitry Andric MemoryProfileFile("memory-profile-file",
18106c3fb27SDimitry 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
205*0fca6ea1SDimitry Andric static cl::opt<PGOOptions::ColdFuncOpt> PGOColdFuncAttr(
206*0fca6ea1SDimitry Andric "pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), cl::Hidden,
207*0fca6ea1SDimitry Andric cl::desc(
208*0fca6ea1SDimitry Andric "Function attribute to apply to cold functions as determined by PGO"),
209*0fca6ea1SDimitry Andric cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default",
210*0fca6ea1SDimitry Andric "Default (no attribute)"),
211*0fca6ea1SDimitry Andric clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize",
212*0fca6ea1SDimitry Andric "Mark cold functions with optsize."),
213*0fca6ea1SDimitry Andric clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize",
214*0fca6ea1SDimitry Andric "Mark cold functions with minsize."),
215*0fca6ea1SDimitry Andric clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone",
216*0fca6ea1SDimitry Andric "Mark cold functions with optnone.")));
217*0fca6ea1SDimitry Andric
2180b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling(
219bdd1243dSDimitry Andric "debug-info-for-profiling", cl::init(false), cl::Hidden,
2200b57cec5SDimitry Andric cl::desc("Emit special debug info to enable PGO profile generation."));
221bdd1243dSDimitry Andric
222e8d8bef9SDimitry Andric static cl::opt<bool> PseudoProbeForProfiling(
223bdd1243dSDimitry Andric "pseudo-probe-for-profiling", cl::init(false), cl::Hidden,
224e8d8bef9SDimitry Andric cl::desc("Emit pseudo probes to enable PGO profile generation."));
225bdd1243dSDimitry Andric
226bdd1243dSDimitry Andric static cl::opt<bool> DisableLoopUnrolling(
227bdd1243dSDimitry Andric "disable-loop-unrolling",
228bdd1243dSDimitry Andric cl::desc("Disable loop unrolling in all relevant passes"), cl::init(false));
229bdd1243dSDimitry Andric
230bdd1243dSDimitry Andric namespace llvm {
231bdd1243dSDimitry Andric extern cl::opt<bool> PrintPipelinePasses;
232bdd1243dSDimitry Andric } // namespace llvm
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andric template <typename PassManagerT>
tryParsePipelineText(PassBuilder & PB,const cl::opt<std::string> & PipelineOpt)2350b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB,
2360b57cec5SDimitry Andric const cl::opt<std::string> &PipelineOpt) {
2370b57cec5SDimitry Andric if (PipelineOpt.empty())
2380b57cec5SDimitry Andric return false;
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric // Verify the pipeline is parseable:
2410b57cec5SDimitry Andric PassManagerT PM;
2420b57cec5SDimitry Andric if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
2430b57cec5SDimitry Andric errs() << "Could not parse -" << PipelineOpt.ArgStr
2440b57cec5SDimitry Andric << " pipeline: " << toString(std::move(Err))
2450b57cec5SDimitry Andric << "... I'm going to ignore it.\n";
2460b57cec5SDimitry Andric return false;
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric return true;
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks
2520b57cec5SDimitry Andric /// for parsing and inserting the given pipeline
registerEPCallbacks(PassBuilder & PB)253e8d8bef9SDimitry Andric static void registerEPCallbacks(PassBuilder &PB) {
2540b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
2550b57cec5SDimitry Andric PB.registerPeepholeEPCallback(
256349cc55cSDimitry Andric [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2570b57cec5SDimitry Andric ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
258e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PeepholeEPPipeline));
2590b57cec5SDimitry Andric });
2600b57cec5SDimitry Andric if (tryParsePipelineText<LoopPassManager>(PB,
2610b57cec5SDimitry Andric LateLoopOptimizationsEPPipeline))
2620b57cec5SDimitry Andric PB.registerLateLoopOptimizationsEPCallback(
263349cc55cSDimitry Andric [&PB](LoopPassManager &PM, OptimizationLevel Level) {
2640b57cec5SDimitry Andric ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
265e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline));
2660b57cec5SDimitry Andric });
2670b57cec5SDimitry Andric if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
2680b57cec5SDimitry Andric PB.registerLoopOptimizerEndEPCallback(
269349cc55cSDimitry Andric [&PB](LoopPassManager &PM, OptimizationLevel Level) {
2700b57cec5SDimitry Andric ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
271e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline));
2720b57cec5SDimitry Andric });
2730b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB,
2740b57cec5SDimitry Andric ScalarOptimizerLateEPPipeline))
2750b57cec5SDimitry Andric PB.registerScalarOptimizerLateEPCallback(
276349cc55cSDimitry Andric [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2770b57cec5SDimitry Andric ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
278e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline));
2790b57cec5SDimitry Andric });
2800b57cec5SDimitry Andric if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
2810b57cec5SDimitry Andric PB.registerCGSCCOptimizerLateEPCallback(
282349cc55cSDimitry Andric [&PB](CGSCCPassManager &PM, OptimizationLevel Level) {
2830b57cec5SDimitry Andric ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
284e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline));
2850b57cec5SDimitry Andric });
2860b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
2870b57cec5SDimitry Andric PB.registerVectorizerStartEPCallback(
288349cc55cSDimitry Andric [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2890b57cec5SDimitry Andric ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
290e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline));
2910b57cec5SDimitry Andric });
2920b57cec5SDimitry Andric if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
2930b57cec5SDimitry Andric PB.registerPipelineStartEPCallback(
294349cc55cSDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) {
2950b57cec5SDimitry Andric ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
296e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline));
297e8d8bef9SDimitry Andric });
298e8d8bef9SDimitry Andric if (tryParsePipelineText<ModulePassManager>(
299e8d8bef9SDimitry Andric PB, PipelineEarlySimplificationEPPipeline))
300e8d8bef9SDimitry Andric PB.registerPipelineEarlySimplificationEPCallback(
301349cc55cSDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) {
302e8d8bef9SDimitry Andric ExitOnError Err("Unable to parse EarlySimplification pipeline: ");
303e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline));
3040b57cec5SDimitry Andric });
30581ad6265SDimitry Andric if (tryParsePipelineText<ModulePassManager>(PB, OptimizerEarlyEPPipeline))
30681ad6265SDimitry Andric PB.registerOptimizerEarlyEPCallback(
30781ad6265SDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) {
30881ad6265SDimitry Andric ExitOnError Err("Unable to parse OptimizerEarlyEP pipeline: ");
30981ad6265SDimitry Andric Err(PB.parsePassPipeline(PM, OptimizerEarlyEPPipeline));
31081ad6265SDimitry Andric });
31181ad6265SDimitry Andric if (tryParsePipelineText<ModulePassManager>(PB, OptimizerLastEPPipeline))
3120b57cec5SDimitry Andric PB.registerOptimizerLastEPCallback(
313349cc55cSDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) {
3140b57cec5SDimitry Andric ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
315e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline));
3160b57cec5SDimitry Andric });
31781ad6265SDimitry Andric if (tryParsePipelineText<ModulePassManager>(
31881ad6265SDimitry Andric PB, FullLinkTimeOptimizationEarlyEPPipeline))
31981ad6265SDimitry Andric PB.registerFullLinkTimeOptimizationEarlyEPCallback(
32081ad6265SDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) {
32181ad6265SDimitry Andric ExitOnError Err(
32281ad6265SDimitry Andric "Unable to parse FullLinkTimeOptimizationEarlyEP pipeline: ");
32381ad6265SDimitry Andric Err(PB.parsePassPipeline(PM,
32481ad6265SDimitry Andric FullLinkTimeOptimizationEarlyEPPipeline));
32581ad6265SDimitry Andric });
32681ad6265SDimitry Andric if (tryParsePipelineText<ModulePassManager>(
32781ad6265SDimitry Andric PB, FullLinkTimeOptimizationLastEPPipeline))
32881ad6265SDimitry Andric PB.registerFullLinkTimeOptimizationLastEPCallback(
32981ad6265SDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) {
33081ad6265SDimitry Andric ExitOnError Err(
33181ad6265SDimitry Andric "Unable to parse FullLinkTimeOptimizationLastEP pipeline: ");
33281ad6265SDimitry Andric Err(PB.parsePassPipeline(PM, FullLinkTimeOptimizationLastEPPipeline));
33381ad6265SDimitry Andric });
3340b57cec5SDimitry Andric }
3350b57cec5SDimitry Andric
336480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext) \
337480093f4SDimitry Andric llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
338480093f4SDimitry Andric #include "llvm/Support/Extension.def"
3390b57cec5SDimitry Andric
runPassPipeline(StringRef Arg0,Module & M,TargetMachine * TM,TargetLibraryInfoImpl * TLII,ToolOutputFile * Out,ToolOutputFile * ThinLTOLinkOut,ToolOutputFile * OptRemarkFile,StringRef PassPipeline,ArrayRef<PassPlugin> PassPlugins,ArrayRef<std::function<void (llvm::PassBuilder &)>> PassBuilderCallbacks,OutputKind OK,VerifierKind VK,bool ShouldPreserveAssemblyUseListOrder,bool ShouldPreserveBitcodeUseListOrder,bool EmitSummaryIndex,bool EmitModuleHash,bool EnableDebugify,bool VerifyDIPreserve,bool UnifiedLTO)34006c3fb27SDimitry Andric bool llvm::runPassPipeline(
34106c3fb27SDimitry Andric StringRef Arg0, Module &M, TargetMachine *TM, TargetLibraryInfoImpl *TLII,
34206c3fb27SDimitry Andric ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
34306c3fb27SDimitry Andric ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
344*0fca6ea1SDimitry Andric ArrayRef<PassPlugin> PassPlugins,
345*0fca6ea1SDimitry Andric ArrayRef<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks,
346*0fca6ea1SDimitry Andric OutputKind OK, VerifierKind VK, bool ShouldPreserveAssemblyUseListOrder,
34706c3fb27SDimitry Andric bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,
34806c3fb27SDimitry Andric bool EmitModuleHash, bool EnableDebugify, bool VerifyDIPreserve,
34906c3fb27SDimitry Andric bool UnifiedLTO) {
3500b57cec5SDimitry Andric bool VerifyEachPass = VK == VK_VerifyEachPass;
3510b57cec5SDimitry Andric
35206c3fb27SDimitry Andric auto FS = vfs::getRealFileSystem();
353bdd1243dSDimitry Andric std::optional<PGOOptions> P;
3540b57cec5SDimitry Andric switch (PGOKindFlag) {
3550b57cec5SDimitry Andric case InstrGen:
35606c3fb27SDimitry Andric P = PGOOptions(ProfileFile, "", "", MemoryProfileFile, FS,
357*0fca6ea1SDimitry Andric PGOOptions::IRInstr, PGOOptions::NoCSAction,
358*0fca6ea1SDimitry Andric PGOColdFuncAttr);
3590b57cec5SDimitry Andric break;
3600b57cec5SDimitry Andric case InstrUse:
36106c3fb27SDimitry Andric P = PGOOptions(ProfileFile, "", ProfileRemappingFile, MemoryProfileFile, FS,
362*0fca6ea1SDimitry Andric PGOOptions::IRUse, PGOOptions::NoCSAction, PGOColdFuncAttr);
3630b57cec5SDimitry Andric break;
3640b57cec5SDimitry Andric case SampleUse:
36506c3fb27SDimitry Andric P = PGOOptions(ProfileFile, "", ProfileRemappingFile, MemoryProfileFile, FS,
366*0fca6ea1SDimitry Andric PGOOptions::SampleUse, PGOOptions::NoCSAction,
367*0fca6ea1SDimitry Andric PGOColdFuncAttr);
3680b57cec5SDimitry Andric break;
3690b57cec5SDimitry Andric case NoPGO:
37006c3fb27SDimitry Andric if (DebugInfoForProfiling || PseudoProbeForProfiling ||
37106c3fb27SDimitry Andric !MemoryProfileFile.empty())
37206c3fb27SDimitry Andric P = PGOOptions("", "", "", MemoryProfileFile, FS, PGOOptions::NoAction,
373*0fca6ea1SDimitry Andric PGOOptions::NoCSAction, PGOColdFuncAttr,
374*0fca6ea1SDimitry Andric DebugInfoForProfiling, PseudoProbeForProfiling);
3750b57cec5SDimitry Andric else
376bdd1243dSDimitry Andric P = std::nullopt;
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric if (CSPGOKindFlag != NoCSPGO) {
3790b57cec5SDimitry Andric if (P && (P->Action == PGOOptions::IRInstr ||
380bdd1243dSDimitry Andric P->Action == PGOOptions::SampleUse)) {
3810b57cec5SDimitry Andric errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
382bdd1243dSDimitry Andric return false;
383bdd1243dSDimitry Andric }
3840b57cec5SDimitry Andric if (CSPGOKindFlag == CSInstrGen) {
385bdd1243dSDimitry Andric if (CSProfileGenFile.empty()) {
3860b57cec5SDimitry Andric errs() << "CSInstrGen needs to specify CSProfileGenFile";
387bdd1243dSDimitry Andric return false;
388bdd1243dSDimitry Andric }
3890b57cec5SDimitry Andric if (P) {
3900b57cec5SDimitry Andric P->CSAction = PGOOptions::CSIRInstr;
3910b57cec5SDimitry Andric P->CSProfileGenFile = CSProfileGenFile;
3920b57cec5SDimitry Andric } else
3930b57cec5SDimitry Andric P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
39406c3fb27SDimitry Andric /*MemoryProfile=*/"", FS, PGOOptions::NoAction,
39506c3fb27SDimitry Andric PGOOptions::CSIRInstr);
3960b57cec5SDimitry Andric } else /* CSPGOKindFlag == CSInstrUse */ {
397bdd1243dSDimitry Andric if (!P) {
3980b57cec5SDimitry Andric errs() << "CSInstrUse needs to be together with InstrUse";
399bdd1243dSDimitry Andric return false;
400bdd1243dSDimitry Andric }
4010b57cec5SDimitry Andric P->CSAction = PGOOptions::CSIRUse;
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric }
404349cc55cSDimitry Andric if (TM)
405349cc55cSDimitry Andric TM->setPGOOption(P);
406349cc55cSDimitry Andric
407fe6060f1SDimitry Andric LoopAnalysisManager LAM;
408fe6060f1SDimitry Andric FunctionAnalysisManager FAM;
409fe6060f1SDimitry Andric CGSCCAnalysisManager CGAM;
410fe6060f1SDimitry Andric ModuleAnalysisManager MAM;
411fe6060f1SDimitry Andric
4120b57cec5SDimitry Andric PassInstrumentationCallbacks PIC;
413fe6060f1SDimitry Andric PrintPassOptions PrintPassOpts;
414fe6060f1SDimitry Andric PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
415fe6060f1SDimitry Andric PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
416bdd1243dSDimitry Andric StandardInstrumentations SI(M.getContext(), DebugPM != DebugLogging::None,
417bdd1243dSDimitry Andric VerifyEachPass, PrintPassOpts);
41806c3fb27SDimitry Andric SI.registerCallbacks(PIC, &MAM);
419e8d8bef9SDimitry Andric DebugifyEachInstrumentation Debugify;
420753f127fSDimitry Andric DebugifyStatsMap DIStatsMap;
421753f127fSDimitry Andric DebugInfoPerPass DebugInfoBeforePass;
422753f127fSDimitry Andric if (DebugifyEach) {
423753f127fSDimitry Andric Debugify.setDIStatsMap(DIStatsMap);
424753f127fSDimitry Andric Debugify.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
42506c3fb27SDimitry Andric Debugify.registerCallbacks(PIC, MAM);
426753f127fSDimitry Andric } else if (VerifyEachDebugInfoPreserve) {
427753f127fSDimitry Andric Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
428753f127fSDimitry Andric Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
429753f127fSDimitry Andric Debugify.setOrigDIVerifyBugsReportFilePath(
430753f127fSDimitry Andric VerifyDIPreserveExport);
43106c3fb27SDimitry Andric Debugify.registerCallbacks(PIC, MAM);
432753f127fSDimitry Andric }
4330b57cec5SDimitry Andric
4345ffd83dbSDimitry Andric PipelineTuningOptions PTO;
4355ffd83dbSDimitry Andric // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
4365ffd83dbSDimitry Andric // to false above so we shouldn't necessarily need to check whether or not the
4375ffd83dbSDimitry Andric // option has been enabled.
4385ffd83dbSDimitry Andric PTO.LoopUnrolling = !DisableLoopUnrolling;
43906c3fb27SDimitry Andric PTO.UnifiedLTO = UnifiedLTO;
440fe6060f1SDimitry Andric PassBuilder PB(TM, PTO, P, &PIC);
441e8d8bef9SDimitry Andric registerEPCallbacks(PB);
4420b57cec5SDimitry Andric
44381ad6265SDimitry Andric // For any loaded plugins, let them register pass builder callbacks.
44481ad6265SDimitry Andric for (auto &PassPlugin : PassPlugins)
44581ad6265SDimitry Andric PassPlugin.registerPassBuilderCallbacks(PB);
4460b57cec5SDimitry Andric
447*0fca6ea1SDimitry Andric // Load any explicitly specified plugins.
448*0fca6ea1SDimitry Andric for (auto &PassCallback : PassBuilderCallbacks)
449*0fca6ea1SDimitry Andric PassCallback(PB);
450*0fca6ea1SDimitry Andric
451480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext) \
452480093f4SDimitry Andric get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
453480093f4SDimitry Andric #include "llvm/Support/Extension.def"
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric // Specially handle the alias analysis manager so that we can register
4560b57cec5SDimitry Andric // a custom pipeline of AA passes with it.
4570b57cec5SDimitry Andric AAManager AA;
4580b57cec5SDimitry Andric if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
4590b57cec5SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4600b57cec5SDimitry Andric return false;
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andric // Register the AA manager first so that our version is the one used.
4640b57cec5SDimitry Andric FAM.registerPass([&] { return std::move(AA); });
465e8d8bef9SDimitry Andric // Register our TargetLibraryInfoImpl.
466e8d8bef9SDimitry Andric FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
4670b57cec5SDimitry Andric
4680b57cec5SDimitry Andric // Register all the basic analyses with the managers.
4690b57cec5SDimitry Andric PB.registerModuleAnalyses(MAM);
4700b57cec5SDimitry Andric PB.registerCGSCCAnalyses(CGAM);
4710b57cec5SDimitry Andric PB.registerFunctionAnalyses(FAM);
4720b57cec5SDimitry Andric PB.registerLoopAnalyses(LAM);
4730b57cec5SDimitry Andric PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
4740b57cec5SDimitry Andric
475fe6060f1SDimitry Andric ModulePassManager MPM;
4760b57cec5SDimitry Andric if (EnableDebugify)
4770b57cec5SDimitry Andric MPM.addPass(NewPMDebugifyPass());
478753f127fSDimitry Andric if (VerifyDIPreserve)
479753f127fSDimitry Andric MPM.addPass(NewPMDebugifyPass(DebugifyMode::OriginalDebugInfo, "",
480753f127fSDimitry Andric &DebugInfoBeforePass));
4810b57cec5SDimitry Andric
482349cc55cSDimitry Andric // Add passes according to the -passes options.
4835ffd83dbSDimitry Andric if (!PassPipeline.empty()) {
484e8d8bef9SDimitry Andric if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
4850b57cec5SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4860b57cec5SDimitry Andric return false;
4870b57cec5SDimitry Andric }
4885ffd83dbSDimitry Andric }
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andric if (VK > VK_NoVerifier)
4910b57cec5SDimitry Andric MPM.addPass(VerifierPass());
4920b57cec5SDimitry Andric if (EnableDebugify)
493753f127fSDimitry Andric MPM.addPass(NewPMCheckDebugifyPass(false, "", &DIStatsMap));
494753f127fSDimitry Andric if (VerifyDIPreserve)
495753f127fSDimitry Andric MPM.addPass(NewPMCheckDebugifyPass(
496bdd1243dSDimitry Andric false, "", nullptr, DebugifyMode::OriginalDebugInfo,
497bdd1243dSDimitry Andric &DebugInfoBeforePass, VerifyDIPreserveExport));
4980b57cec5SDimitry Andric
4990b57cec5SDimitry Andric // Add any relevant output pass at the end of the pipeline.
5000b57cec5SDimitry Andric switch (OK) {
5010b57cec5SDimitry Andric case OK_NoOutput:
5020b57cec5SDimitry Andric break; // No output pass needed.
5030b57cec5SDimitry Andric case OK_OutputAssembly:
504bdd1243dSDimitry Andric MPM.addPass(PrintModulePass(
505bdd1243dSDimitry Andric Out->os(), "", ShouldPreserveAssemblyUseListOrder, EmitSummaryIndex));
5060b57cec5SDimitry Andric break;
5070b57cec5SDimitry Andric case OK_OutputBitcode:
5080b57cec5SDimitry Andric MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
5090b57cec5SDimitry Andric EmitSummaryIndex, EmitModuleHash));
5100b57cec5SDimitry Andric break;
5110b57cec5SDimitry Andric case OK_OutputThinLTOBitcode:
5120b57cec5SDimitry Andric MPM.addPass(ThinLTOBitcodeWriterPass(
5130b57cec5SDimitry Andric Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
5140b57cec5SDimitry Andric break;
5150b57cec5SDimitry Andric }
5160b57cec5SDimitry Andric
5170b57cec5SDimitry Andric // Before executing passes, print the final values of the LLVM options.
5180b57cec5SDimitry Andric cl::PrintOptionValues();
5190b57cec5SDimitry Andric
520349cc55cSDimitry Andric // Print a textual, '-passes=' compatible, representation of pipeline if
521349cc55cSDimitry Andric // requested.
522349cc55cSDimitry Andric if (PrintPipelinePasses) {
523bdd1243dSDimitry Andric std::string Pipeline;
524bdd1243dSDimitry Andric raw_string_ostream SOS(Pipeline);
525bdd1243dSDimitry Andric MPM.printPipeline(SOS, [&PIC](StringRef ClassName) {
526349cc55cSDimitry Andric auto PassName = PIC.getPassNameForClassName(ClassName);
527349cc55cSDimitry Andric return PassName.empty() ? ClassName : PassName;
528349cc55cSDimitry Andric });
529bdd1243dSDimitry Andric outs() << Pipeline;
530349cc55cSDimitry Andric outs() << "\n";
531bdd1243dSDimitry Andric
532bdd1243dSDimitry Andric if (!DisablePipelineVerification) {
533bdd1243dSDimitry Andric // Check that we can parse the returned pipeline string as an actual
534bdd1243dSDimitry Andric // pipeline.
535bdd1243dSDimitry Andric ModulePassManager TempPM;
536bdd1243dSDimitry Andric if (auto Err = PB.parsePassPipeline(TempPM, Pipeline)) {
537bdd1243dSDimitry Andric errs() << "Could not parse dumped pass pipeline: "
538bdd1243dSDimitry Andric << toString(std::move(Err)) << "\n";
539bdd1243dSDimitry Andric return false;
540bdd1243dSDimitry Andric }
541bdd1243dSDimitry Andric }
542bdd1243dSDimitry Andric
543349cc55cSDimitry Andric return true;
544349cc55cSDimitry Andric }
545349cc55cSDimitry Andric
5460b57cec5SDimitry Andric // Now that we have all of the passes ready, run them.
5470b57cec5SDimitry Andric MPM.run(M, MAM);
5480b57cec5SDimitry Andric
5490b57cec5SDimitry Andric // Declare success.
5500b57cec5SDimitry Andric if (OK != OK_NoOutput) {
5510b57cec5SDimitry Andric Out->keep();
5520b57cec5SDimitry Andric if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
5530b57cec5SDimitry Andric ThinLTOLinkOut->keep();
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric if (OptRemarkFile)
5570b57cec5SDimitry Andric OptRemarkFile->keep();
5580b57cec5SDimitry Andric
559e8d8bef9SDimitry Andric if (DebugifyEach && !DebugifyExport.empty())
560753f127fSDimitry Andric exportDebugifyStats(DebugifyExport, Debugify.getDebugifyStatsMap());
561e8d8bef9SDimitry Andric
5620b57cec5SDimitry Andric return true;
5630b57cec5SDimitry Andric }
564fe6060f1SDimitry Andric
printPasses(raw_ostream & OS)565fe6060f1SDimitry Andric void llvm::printPasses(raw_ostream &OS) {
566fe6060f1SDimitry Andric PassBuilder PB;
567fe6060f1SDimitry Andric PB.printPassNames(OS);
568fe6060f1SDimitry Andric }
569