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" 160b57cec5SDimitry Andric #include "PassPrinters.h" 175ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h" 180b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 190b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 200b57cec5SDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h" 21*e8d8bef9SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 220b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h" 230b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 240b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 250b57cec5SDimitry Andric #include "llvm/IR/IRPrintingPasses.h" 260b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 270b57cec5SDimitry Andric #include "llvm/IR/Module.h" 280b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 290b57cec5SDimitry Andric #include "llvm/IR/Verifier.h" 300b57cec5SDimitry Andric #include "llvm/Passes/PassBuilder.h" 310b57cec5SDimitry Andric #include "llvm/Passes/PassPlugin.h" 320b57cec5SDimitry Andric #include "llvm/Passes/StandardInstrumentations.h" 330b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 340b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h" 350b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 360b57cec5SDimitry Andric #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 37*e8d8bef9SDimitry Andric #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" 380b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h" 39480093f4SDimitry Andric #include "llvm/Transforms/Utils/Debugify.h" 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric using namespace llvm; 420b57cec5SDimitry Andric using namespace opt_tool; 430b57cec5SDimitry Andric 44*e8d8bef9SDimitry Andric namespace llvm { 45*e8d8bef9SDimitry Andric cl::opt<bool> DebugifyEach( 46*e8d8bef9SDimitry Andric "debugify-each", 47*e8d8bef9SDimitry Andric cl::desc("Start each pass with debugify and end it with check-debugify")); 48*e8d8bef9SDimitry Andric 49*e8d8bef9SDimitry Andric cl::opt<std::string> 50*e8d8bef9SDimitry Andric DebugifyExport("debugify-export", 51*e8d8bef9SDimitry Andric cl::desc("Export per-pass debugify statistics to this file"), 52*e8d8bef9SDimitry Andric cl::value_desc("filename")); 53*e8d8bef9SDimitry Andric } // namespace llvm 54*e8d8bef9SDimitry Andric 550b57cec5SDimitry Andric static cl::opt<bool> 560b57cec5SDimitry Andric DebugPM("debug-pass-manager", cl::Hidden, 570b57cec5SDimitry Andric cl::desc("Print pass management debugging information")); 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric static cl::list<std::string> 600b57cec5SDimitry Andric PassPlugins("load-pass-plugin", 610b57cec5SDimitry Andric cl::desc("Load passes from plugin library")); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric // This flag specifies a textual description of the alias analysis pipeline to 640b57cec5SDimitry Andric // use when querying for aliasing information. It only works in concert with 650b57cec5SDimitry Andric // the "passes" flag above. 660b57cec5SDimitry Andric static cl::opt<std::string> 670b57cec5SDimitry Andric AAPipeline("aa-pipeline", 680b57cec5SDimitry Andric cl::desc("A textual description of the alias analysis " 690b57cec5SDimitry Andric "pipeline for handling managed aliasing queries"), 70*e8d8bef9SDimitry Andric cl::Hidden, cl::init("default")); 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric /// {{@ These options accept textual pipeline descriptions which will be 730b57cec5SDimitry Andric /// inserted into default pipelines at the respective extension points 740b57cec5SDimitry Andric static cl::opt<std::string> PeepholeEPPipeline( 750b57cec5SDimitry Andric "passes-ep-peephole", 760b57cec5SDimitry Andric cl::desc("A textual description of the function pass pipeline inserted at " 770b57cec5SDimitry Andric "the Peephole extension points into default pipelines"), 780b57cec5SDimitry Andric cl::Hidden); 790b57cec5SDimitry Andric static cl::opt<std::string> LateLoopOptimizationsEPPipeline( 800b57cec5SDimitry Andric "passes-ep-late-loop-optimizations", 810b57cec5SDimitry Andric cl::desc( 820b57cec5SDimitry Andric "A textual description of the loop pass pipeline inserted at " 830b57cec5SDimitry Andric "the LateLoopOptimizations extension point into default pipelines"), 840b57cec5SDimitry Andric cl::Hidden); 850b57cec5SDimitry Andric static cl::opt<std::string> LoopOptimizerEndEPPipeline( 860b57cec5SDimitry Andric "passes-ep-loop-optimizer-end", 870b57cec5SDimitry Andric cl::desc("A textual description of the loop pass pipeline inserted at " 880b57cec5SDimitry Andric "the LoopOptimizerEnd extension point into default pipelines"), 890b57cec5SDimitry Andric cl::Hidden); 900b57cec5SDimitry Andric static cl::opt<std::string> ScalarOptimizerLateEPPipeline( 910b57cec5SDimitry Andric "passes-ep-scalar-optimizer-late", 920b57cec5SDimitry Andric cl::desc("A textual description of the function pass pipeline inserted at " 930b57cec5SDimitry Andric "the ScalarOptimizerLate extension point into default pipelines"), 940b57cec5SDimitry Andric cl::Hidden); 950b57cec5SDimitry Andric static cl::opt<std::string> CGSCCOptimizerLateEPPipeline( 960b57cec5SDimitry Andric "passes-ep-cgscc-optimizer-late", 970b57cec5SDimitry Andric cl::desc("A textual description of the cgscc pass pipeline inserted at " 980b57cec5SDimitry Andric "the CGSCCOptimizerLate extension point into default pipelines"), 990b57cec5SDimitry Andric cl::Hidden); 1000b57cec5SDimitry Andric static cl::opt<std::string> VectorizerStartEPPipeline( 1010b57cec5SDimitry Andric "passes-ep-vectorizer-start", 1020b57cec5SDimitry Andric cl::desc("A textual description of the function pass pipeline inserted at " 1030b57cec5SDimitry Andric "the VectorizerStart extension point into default pipelines"), 1040b57cec5SDimitry Andric cl::Hidden); 1050b57cec5SDimitry Andric static cl::opt<std::string> PipelineStartEPPipeline( 1060b57cec5SDimitry Andric "passes-ep-pipeline-start", 107*e8d8bef9SDimitry Andric cl::desc("A textual description of the module pass pipeline inserted at " 1080b57cec5SDimitry Andric "the PipelineStart extension point into default pipelines"), 1090b57cec5SDimitry Andric cl::Hidden); 110*e8d8bef9SDimitry Andric static cl::opt<std::string> PipelineEarlySimplificationEPPipeline( 111*e8d8bef9SDimitry Andric "passes-ep-pipeline-early-simplification", 112*e8d8bef9SDimitry Andric cl::desc("A textual description of the module pass pipeline inserted at " 113*e8d8bef9SDimitry Andric "the EarlySimplification extension point into default pipelines"), 114*e8d8bef9SDimitry Andric cl::Hidden); 1150b57cec5SDimitry Andric static cl::opt<std::string> OptimizerLastEPPipeline( 1160b57cec5SDimitry Andric "passes-ep-optimizer-last", 117*e8d8bef9SDimitry Andric cl::desc("A textual description of the module pass pipeline inserted at " 1180b57cec5SDimitry Andric "the OptimizerLast extension point into default pipelines"), 1190b57cec5SDimitry Andric cl::Hidden); 1200b57cec5SDimitry Andric 1215ffd83dbSDimitry Andric // Individual pipeline tuning options. 1225ffd83dbSDimitry Andric extern cl::opt<bool> DisableLoopUnrolling; 1235ffd83dbSDimitry Andric 1240b57cec5SDimitry Andric extern cl::opt<PGOKind> PGOKindFlag; 1250b57cec5SDimitry Andric extern cl::opt<std::string> ProfileFile; 1260b57cec5SDimitry Andric extern cl::opt<CSPGOKind> CSPGOKindFlag; 1270b57cec5SDimitry Andric extern cl::opt<std::string> CSProfileGenFile; 128*e8d8bef9SDimitry Andric extern cl::opt<bool> DisableBasicAA; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric static cl::opt<std::string> 1310b57cec5SDimitry Andric ProfileRemappingFile("profile-remapping-file", 1320b57cec5SDimitry Andric cl::desc("Path to the profile remapping file."), 1330b57cec5SDimitry Andric cl::Hidden); 1340b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling( 1350b57cec5SDimitry Andric "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden, 1360b57cec5SDimitry Andric cl::desc("Emit special debug info to enable PGO profile generation.")); 137*e8d8bef9SDimitry Andric static cl::opt<bool> PseudoProbeForProfiling( 138*e8d8bef9SDimitry Andric "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden, 139*e8d8bef9SDimitry Andric cl::desc("Emit pseudo probes to enable PGO profile generation.")); 140*e8d8bef9SDimitry Andric static cl::opt<bool> UniqueInternalLinkageNames( 141*e8d8bef9SDimitry Andric "new-pm-unique-internal-linkage-names", cl::init(false), cl::Hidden, 142*e8d8bef9SDimitry Andric cl::desc("Uniqueify Internal Linkage Symbol Names by appending the MD5 " 143*e8d8bef9SDimitry Andric "hash of the module path.")); 1440b57cec5SDimitry Andric /// @}} 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric template <typename PassManagerT> 1470b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB, 1480b57cec5SDimitry Andric const cl::opt<std::string> &PipelineOpt) { 1490b57cec5SDimitry Andric if (PipelineOpt.empty()) 1500b57cec5SDimitry Andric return false; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric // Verify the pipeline is parseable: 1530b57cec5SDimitry Andric PassManagerT PM; 1540b57cec5SDimitry Andric if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) { 1550b57cec5SDimitry Andric errs() << "Could not parse -" << PipelineOpt.ArgStr 1560b57cec5SDimitry Andric << " pipeline: " << toString(std::move(Err)) 1570b57cec5SDimitry Andric << "... I'm going to ignore it.\n"; 1580b57cec5SDimitry Andric return false; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric return true; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks 1640b57cec5SDimitry Andric /// for parsing and inserting the given pipeline 165*e8d8bef9SDimitry Andric static void registerEPCallbacks(PassBuilder &PB) { 1660b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline)) 1670b57cec5SDimitry Andric PB.registerPeepholeEPCallback( 168*e8d8bef9SDimitry Andric [&PB](FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { 1690b57cec5SDimitry Andric ExitOnError Err("Unable to parse PeepholeEP pipeline: "); 170*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PeepholeEPPipeline)); 1710b57cec5SDimitry Andric }); 1720b57cec5SDimitry Andric if (tryParsePipelineText<LoopPassManager>(PB, 1730b57cec5SDimitry Andric LateLoopOptimizationsEPPipeline)) 1740b57cec5SDimitry Andric PB.registerLateLoopOptimizationsEPCallback( 175*e8d8bef9SDimitry Andric [&PB](LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { 1760b57cec5SDimitry Andric ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: "); 177*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline)); 1780b57cec5SDimitry Andric }); 1790b57cec5SDimitry Andric if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline)) 1800b57cec5SDimitry Andric PB.registerLoopOptimizerEndEPCallback( 181*e8d8bef9SDimitry Andric [&PB](LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { 1820b57cec5SDimitry Andric ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: "); 183*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline)); 1840b57cec5SDimitry Andric }); 1850b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, 1860b57cec5SDimitry Andric ScalarOptimizerLateEPPipeline)) 1870b57cec5SDimitry Andric PB.registerScalarOptimizerLateEPCallback( 188*e8d8bef9SDimitry Andric [&PB](FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { 1890b57cec5SDimitry Andric ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: "); 190*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline)); 1910b57cec5SDimitry Andric }); 1920b57cec5SDimitry Andric if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline)) 1930b57cec5SDimitry Andric PB.registerCGSCCOptimizerLateEPCallback( 194*e8d8bef9SDimitry Andric [&PB](CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) { 1950b57cec5SDimitry Andric ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: "); 196*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline)); 1970b57cec5SDimitry Andric }); 1980b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline)) 1990b57cec5SDimitry Andric PB.registerVectorizerStartEPCallback( 200*e8d8bef9SDimitry Andric [&PB](FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { 2010b57cec5SDimitry Andric ExitOnError Err("Unable to parse VectorizerStartEP pipeline: "); 202*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline)); 2030b57cec5SDimitry Andric }); 2040b57cec5SDimitry Andric if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline)) 2050b57cec5SDimitry Andric PB.registerPipelineStartEPCallback( 206*e8d8bef9SDimitry Andric [&PB](ModulePassManager &PM, PassBuilder::OptimizationLevel) { 2070b57cec5SDimitry Andric ExitOnError Err("Unable to parse PipelineStartEP pipeline: "); 208*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline)); 209*e8d8bef9SDimitry Andric }); 210*e8d8bef9SDimitry Andric if (tryParsePipelineText<ModulePassManager>( 211*e8d8bef9SDimitry Andric PB, PipelineEarlySimplificationEPPipeline)) 212*e8d8bef9SDimitry Andric PB.registerPipelineEarlySimplificationEPCallback( 213*e8d8bef9SDimitry Andric [&PB](ModulePassManager &PM, PassBuilder::OptimizationLevel) { 214*e8d8bef9SDimitry Andric ExitOnError Err("Unable to parse EarlySimplification pipeline: "); 215*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline)); 2160b57cec5SDimitry Andric }); 2170b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, OptimizerLastEPPipeline)) 2180b57cec5SDimitry Andric PB.registerOptimizerLastEPCallback( 219*e8d8bef9SDimitry Andric [&PB](ModulePassManager &PM, PassBuilder::OptimizationLevel) { 2200b57cec5SDimitry Andric ExitOnError Err("Unable to parse OptimizerLastEP pipeline: "); 221*e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline)); 2220b57cec5SDimitry Andric }); 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 225480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext) \ 226480093f4SDimitry Andric llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); 227480093f4SDimitry Andric #include "llvm/Support/Extension.def" 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, 230*e8d8bef9SDimitry Andric TargetLibraryInfoImpl *TLII, ToolOutputFile *Out, 231*e8d8bef9SDimitry Andric ToolOutputFile *ThinLTOLinkOut, 2320b57cec5SDimitry Andric ToolOutputFile *OptRemarkFile, 2335ffd83dbSDimitry Andric StringRef PassPipeline, ArrayRef<StringRef> Passes, 2345ffd83dbSDimitry Andric OutputKind OK, VerifierKind VK, 2350b57cec5SDimitry Andric bool ShouldPreserveAssemblyUseListOrder, 2360b57cec5SDimitry Andric bool ShouldPreserveBitcodeUseListOrder, 2370b57cec5SDimitry Andric bool EmitSummaryIndex, bool EmitModuleHash, 2385ffd83dbSDimitry Andric bool EnableDebugify, bool Coroutines) { 2390b57cec5SDimitry Andric bool VerifyEachPass = VK == VK_VerifyEachPass; 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric Optional<PGOOptions> P; 2420b57cec5SDimitry Andric switch (PGOKindFlag) { 2430b57cec5SDimitry Andric case InstrGen: 2440b57cec5SDimitry Andric P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr); 2450b57cec5SDimitry Andric break; 2460b57cec5SDimitry Andric case InstrUse: 2470b57cec5SDimitry Andric P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse); 2480b57cec5SDimitry Andric break; 2490b57cec5SDimitry Andric case SampleUse: 2500b57cec5SDimitry Andric P = PGOOptions(ProfileFile, "", ProfileRemappingFile, 2510b57cec5SDimitry Andric PGOOptions::SampleUse); 2520b57cec5SDimitry Andric break; 2530b57cec5SDimitry Andric case NoPGO: 2540b57cec5SDimitry Andric if (DebugInfoForProfiling) 2550b57cec5SDimitry Andric P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction, 2560b57cec5SDimitry Andric true); 257*e8d8bef9SDimitry Andric else if (PseudoProbeForProfiling) 258*e8d8bef9SDimitry Andric P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction, 259*e8d8bef9SDimitry Andric false, true); 2600b57cec5SDimitry Andric else 2610b57cec5SDimitry Andric P = None; 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric if (CSPGOKindFlag != NoCSPGO) { 2640b57cec5SDimitry Andric if (P && (P->Action == PGOOptions::IRInstr || 2650b57cec5SDimitry Andric P->Action == PGOOptions::SampleUse)) 2660b57cec5SDimitry Andric errs() << "CSPGOKind cannot be used with IRInstr or SampleUse"; 2670b57cec5SDimitry Andric if (CSPGOKindFlag == CSInstrGen) { 2680b57cec5SDimitry Andric if (CSProfileGenFile.empty()) 2690b57cec5SDimitry Andric errs() << "CSInstrGen needs to specify CSProfileGenFile"; 2700b57cec5SDimitry Andric if (P) { 2710b57cec5SDimitry Andric P->CSAction = PGOOptions::CSIRInstr; 2720b57cec5SDimitry Andric P->CSProfileGenFile = CSProfileGenFile; 2730b57cec5SDimitry Andric } else 2740b57cec5SDimitry Andric P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile, 2750b57cec5SDimitry Andric PGOOptions::NoAction, PGOOptions::CSIRInstr); 2760b57cec5SDimitry Andric } else /* CSPGOKindFlag == CSInstrUse */ { 2770b57cec5SDimitry Andric if (!P) 2780b57cec5SDimitry Andric errs() << "CSInstrUse needs to be together with InstrUse"; 2790b57cec5SDimitry Andric P->CSAction = PGOOptions::CSIRUse; 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric PassInstrumentationCallbacks PIC; 283*e8d8bef9SDimitry Andric StandardInstrumentations SI(DebugPM, VerifyEachPass); 2840b57cec5SDimitry Andric SI.registerCallbacks(PIC); 285*e8d8bef9SDimitry Andric DebugifyEachInstrumentation Debugify; 286*e8d8bef9SDimitry Andric if (DebugifyEach) 287*e8d8bef9SDimitry Andric Debugify.registerCallbacks(PIC); 2880b57cec5SDimitry Andric 2895ffd83dbSDimitry Andric PipelineTuningOptions PTO; 2905ffd83dbSDimitry Andric // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized 2915ffd83dbSDimitry Andric // to false above so we shouldn't necessarily need to check whether or not the 2925ffd83dbSDimitry Andric // option has been enabled. 2935ffd83dbSDimitry Andric PTO.LoopUnrolling = !DisableLoopUnrolling; 2945ffd83dbSDimitry Andric PTO.Coroutines = Coroutines; 295*e8d8bef9SDimitry Andric PTO.UniqueLinkageNames = UniqueInternalLinkageNames; 296*e8d8bef9SDimitry Andric PassBuilder PB(DebugPM, TM, PTO, P, &PIC); 297*e8d8bef9SDimitry Andric registerEPCallbacks(PB); 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric // Load requested pass plugins and let them register pass builder callbacks 3000b57cec5SDimitry Andric for (auto &PluginFN : PassPlugins) { 3010b57cec5SDimitry Andric auto PassPlugin = PassPlugin::Load(PluginFN); 3020b57cec5SDimitry Andric if (!PassPlugin) { 3030b57cec5SDimitry Andric errs() << "Failed to load passes from '" << PluginFN 3040b57cec5SDimitry Andric << "'. Request ignored.\n"; 3050b57cec5SDimitry Andric continue; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric PassPlugin->registerPassBuilderCallbacks(PB); 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric // Register a callback that creates the debugify passes as needed. 3120b57cec5SDimitry Andric PB.registerPipelineParsingCallback( 3130b57cec5SDimitry Andric [](StringRef Name, ModulePassManager &MPM, 3140b57cec5SDimitry Andric ArrayRef<PassBuilder::PipelineElement>) { 3150b57cec5SDimitry Andric if (Name == "debugify") { 3160b57cec5SDimitry Andric MPM.addPass(NewPMDebugifyPass()); 3170b57cec5SDimitry Andric return true; 3180b57cec5SDimitry Andric } else if (Name == "check-debugify") { 3190b57cec5SDimitry Andric MPM.addPass(NewPMCheckDebugifyPass()); 3200b57cec5SDimitry Andric return true; 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric return false; 3230b57cec5SDimitry Andric }); 324*e8d8bef9SDimitry Andric PB.registerPipelineParsingCallback( 325*e8d8bef9SDimitry Andric [](StringRef Name, ModulePassManager &MPM, 326*e8d8bef9SDimitry Andric ArrayRef<PassBuilder::PipelineElement>) { 327*e8d8bef9SDimitry Andric if (Name == "asan-pipeline") { 328*e8d8bef9SDimitry Andric MPM.addPass( 329*e8d8bef9SDimitry Andric RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>()); 330*e8d8bef9SDimitry Andric MPM.addPass( 331*e8d8bef9SDimitry Andric createModuleToFunctionPassAdaptor(AddressSanitizerPass())); 332*e8d8bef9SDimitry Andric MPM.addPass(ModuleAddressSanitizerPass()); 333*e8d8bef9SDimitry Andric return true; 334*e8d8bef9SDimitry Andric } else if (Name == "asan-function-pipeline") { 335*e8d8bef9SDimitry Andric MPM.addPass( 336*e8d8bef9SDimitry Andric RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>()); 337*e8d8bef9SDimitry Andric MPM.addPass( 338*e8d8bef9SDimitry Andric createModuleToFunctionPassAdaptor(AddressSanitizerPass())); 339*e8d8bef9SDimitry Andric return true; 340*e8d8bef9SDimitry Andric } 341*e8d8bef9SDimitry Andric return false; 342*e8d8bef9SDimitry Andric }); 3430b57cec5SDimitry Andric 344480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext) \ 345480093f4SDimitry Andric get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); 346480093f4SDimitry Andric #include "llvm/Support/Extension.def" 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // Specially handle the alias analysis manager so that we can register 3490b57cec5SDimitry Andric // a custom pipeline of AA passes with it. 3500b57cec5SDimitry Andric AAManager AA; 351*e8d8bef9SDimitry Andric if (Passes.empty()) { 3520b57cec5SDimitry Andric if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) { 3530b57cec5SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 3540b57cec5SDimitry Andric return false; 3550b57cec5SDimitry Andric } 3565ffd83dbSDimitry Andric } 357*e8d8bef9SDimitry Andric 358*e8d8bef9SDimitry Andric // For compatibility with the legacy PM AA pipeline. 359*e8d8bef9SDimitry Andric // AAResultsWrapperPass by default provides basic-aa in the legacy PM 360*e8d8bef9SDimitry Andric // unless -disable-basic-aa is specified. 361*e8d8bef9SDimitry Andric // TODO: remove this once tests implicitly requiring basic-aa use -passes= and 362*e8d8bef9SDimitry Andric // -aa-pipeline=basic-aa. 363*e8d8bef9SDimitry Andric if (!Passes.empty() && !DisableBasicAA) { 364*e8d8bef9SDimitry Andric if (auto Err = PB.parseAAPipeline(AA, "basic-aa")) { 365*e8d8bef9SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 366*e8d8bef9SDimitry Andric return false; 367*e8d8bef9SDimitry Andric } 368*e8d8bef9SDimitry Andric } 369*e8d8bef9SDimitry Andric 3705ffd83dbSDimitry Andric // For compatibility with legacy pass manager. 3715ffd83dbSDimitry Andric // Alias analyses are not specially specified when using the legacy PM. 3725ffd83dbSDimitry Andric for (auto PassName : Passes) { 3735ffd83dbSDimitry Andric if (PB.isAAPassName(PassName)) { 3745ffd83dbSDimitry Andric if (auto Err = PB.parseAAPipeline(AA, PassName)) { 3755ffd83dbSDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 3765ffd83dbSDimitry Andric return false; 3775ffd83dbSDimitry Andric } 3785ffd83dbSDimitry Andric } 3795ffd83dbSDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric LoopAnalysisManager LAM(DebugPM); 3820b57cec5SDimitry Andric FunctionAnalysisManager FAM(DebugPM); 3830b57cec5SDimitry Andric CGSCCAnalysisManager CGAM(DebugPM); 3840b57cec5SDimitry Andric ModuleAnalysisManager MAM(DebugPM); 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric // Register the AA manager first so that our version is the one used. 3870b57cec5SDimitry Andric FAM.registerPass([&] { return std::move(AA); }); 388*e8d8bef9SDimitry Andric // Register our TargetLibraryInfoImpl. 389*e8d8bef9SDimitry Andric FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); }); 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric // Register all the basic analyses with the managers. 3920b57cec5SDimitry Andric PB.registerModuleAnalyses(MAM); 3930b57cec5SDimitry Andric PB.registerCGSCCAnalyses(CGAM); 3940b57cec5SDimitry Andric PB.registerFunctionAnalyses(FAM); 3950b57cec5SDimitry Andric PB.registerLoopAnalyses(LAM); 3960b57cec5SDimitry Andric PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric ModulePassManager MPM(DebugPM); 3990b57cec5SDimitry Andric if (VK > VK_NoVerifier) 4000b57cec5SDimitry Andric MPM.addPass(VerifierPass()); 4010b57cec5SDimitry Andric if (EnableDebugify) 4020b57cec5SDimitry Andric MPM.addPass(NewPMDebugifyPass()); 4030b57cec5SDimitry Andric 4045ffd83dbSDimitry Andric if (!PassPipeline.empty()) { 4055ffd83dbSDimitry Andric assert(Passes.empty() && 4065ffd83dbSDimitry Andric "PassPipeline and Passes should not both contain passes"); 407*e8d8bef9SDimitry Andric if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) { 4080b57cec5SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 4090b57cec5SDimitry Andric return false; 4100b57cec5SDimitry Andric } 4115ffd83dbSDimitry Andric } 412*e8d8bef9SDimitry Andric for (auto PassName : Passes) { 4135ffd83dbSDimitry Andric std::string ModifiedPassName(PassName.begin(), PassName.end()); 4145ffd83dbSDimitry Andric if (PB.isAnalysisPassName(PassName)) 4155ffd83dbSDimitry Andric ModifiedPassName = "require<" + ModifiedPassName + ">"; 416*e8d8bef9SDimitry Andric if (auto Err = PB.parsePassPipeline(MPM, ModifiedPassName)) { 4175ffd83dbSDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 4185ffd83dbSDimitry Andric return false; 4195ffd83dbSDimitry Andric } 4205ffd83dbSDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric if (VK > VK_NoVerifier) 4230b57cec5SDimitry Andric MPM.addPass(VerifierPass()); 4240b57cec5SDimitry Andric if (EnableDebugify) 4250b57cec5SDimitry Andric MPM.addPass(NewPMCheckDebugifyPass()); 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric // Add any relevant output pass at the end of the pipeline. 4280b57cec5SDimitry Andric switch (OK) { 4290b57cec5SDimitry Andric case OK_NoOutput: 4300b57cec5SDimitry Andric break; // No output pass needed. 4310b57cec5SDimitry Andric case OK_OutputAssembly: 4320b57cec5SDimitry Andric MPM.addPass( 4330b57cec5SDimitry Andric PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder)); 4340b57cec5SDimitry Andric break; 4350b57cec5SDimitry Andric case OK_OutputBitcode: 4360b57cec5SDimitry Andric MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder, 4370b57cec5SDimitry Andric EmitSummaryIndex, EmitModuleHash)); 4380b57cec5SDimitry Andric break; 4390b57cec5SDimitry Andric case OK_OutputThinLTOBitcode: 4400b57cec5SDimitry Andric MPM.addPass(ThinLTOBitcodeWriterPass( 4410b57cec5SDimitry Andric Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr)); 4420b57cec5SDimitry Andric break; 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric // Before executing passes, print the final values of the LLVM options. 4460b57cec5SDimitry Andric cl::PrintOptionValues(); 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric // Now that we have all of the passes ready, run them. 4490b57cec5SDimitry Andric MPM.run(M, MAM); 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric // Declare success. 4520b57cec5SDimitry Andric if (OK != OK_NoOutput) { 4530b57cec5SDimitry Andric Out->keep(); 4540b57cec5SDimitry Andric if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut) 4550b57cec5SDimitry Andric ThinLTOLinkOut->keep(); 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric if (OptRemarkFile) 4590b57cec5SDimitry Andric OptRemarkFile->keep(); 4600b57cec5SDimitry Andric 461*e8d8bef9SDimitry Andric if (DebugifyEach && !DebugifyExport.empty()) 462*e8d8bef9SDimitry Andric exportDebugifyStats(DebugifyExport, Debugify.StatsMap); 463*e8d8bef9SDimitry Andric 4640b57cec5SDimitry Andric return true; 4650b57cec5SDimitry Andric } 466