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" 21e8d8bef9SDimitry 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" 37e8d8bef9SDimitry 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 44e8d8bef9SDimitry Andric namespace llvm { 45e8d8bef9SDimitry Andric cl::opt<bool> DebugifyEach( 46e8d8bef9SDimitry Andric "debugify-each", 47e8d8bef9SDimitry Andric cl::desc("Start each pass with debugify and end it with check-debugify")); 48e8d8bef9SDimitry Andric 49e8d8bef9SDimitry Andric cl::opt<std::string> 50e8d8bef9SDimitry Andric DebugifyExport("debugify-export", 51e8d8bef9SDimitry Andric cl::desc("Export per-pass debugify statistics to this file"), 52e8d8bef9SDimitry Andric cl::value_desc("filename")); 53e8d8bef9SDimitry Andric } // namespace llvm 54e8d8bef9SDimitry Andric 55fe6060f1SDimitry Andric enum class DebugLogging { None, Normal, Verbose, Quiet }; 56fe6060f1SDimitry Andric 57fe6060f1SDimitry Andric static cl::opt<DebugLogging> DebugPM( 58fe6060f1SDimitry Andric "debug-pass-manager", cl::Hidden, cl::ValueOptional, 59fe6060f1SDimitry Andric cl::desc("Print pass management debugging information"), 60fe6060f1SDimitry Andric cl::init(DebugLogging::None), 61fe6060f1SDimitry Andric cl::values( 62fe6060f1SDimitry Andric clEnumValN(DebugLogging::Normal, "", ""), 63fe6060f1SDimitry Andric clEnumValN(DebugLogging::Quiet, "quiet", 64fe6060f1SDimitry Andric "Skip printing info about analyses"), 65fe6060f1SDimitry Andric clEnumValN( 66fe6060f1SDimitry Andric DebugLogging::Verbose, "verbose", 67fe6060f1SDimitry Andric "Print extra information about adaptors and pass managers"))); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric static cl::list<std::string> 700b57cec5SDimitry Andric PassPlugins("load-pass-plugin", 710b57cec5SDimitry Andric cl::desc("Load passes from plugin library")); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric // This flag specifies a textual description of the alias analysis pipeline to 740b57cec5SDimitry Andric // use when querying for aliasing information. It only works in concert with 750b57cec5SDimitry Andric // the "passes" flag above. 760b57cec5SDimitry Andric static cl::opt<std::string> 770b57cec5SDimitry Andric AAPipeline("aa-pipeline", 780b57cec5SDimitry Andric cl::desc("A textual description of the alias analysis " 790b57cec5SDimitry Andric "pipeline for handling managed aliasing queries"), 80e8d8bef9SDimitry Andric cl::Hidden, cl::init("default")); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric /// {{@ These options accept textual pipeline descriptions which will be 830b57cec5SDimitry Andric /// inserted into default pipelines at the respective extension points 840b57cec5SDimitry Andric static cl::opt<std::string> PeepholeEPPipeline( 850b57cec5SDimitry Andric "passes-ep-peephole", 860b57cec5SDimitry Andric cl::desc("A textual description of the function pass pipeline inserted at " 870b57cec5SDimitry Andric "the Peephole extension points into default pipelines"), 880b57cec5SDimitry Andric cl::Hidden); 890b57cec5SDimitry Andric static cl::opt<std::string> LateLoopOptimizationsEPPipeline( 900b57cec5SDimitry Andric "passes-ep-late-loop-optimizations", 910b57cec5SDimitry Andric cl::desc( 920b57cec5SDimitry Andric "A textual description of the loop pass pipeline inserted at " 930b57cec5SDimitry Andric "the LateLoopOptimizations extension point into default pipelines"), 940b57cec5SDimitry Andric cl::Hidden); 950b57cec5SDimitry Andric static cl::opt<std::string> LoopOptimizerEndEPPipeline( 960b57cec5SDimitry Andric "passes-ep-loop-optimizer-end", 970b57cec5SDimitry Andric cl::desc("A textual description of the loop pass pipeline inserted at " 980b57cec5SDimitry Andric "the LoopOptimizerEnd extension point into default pipelines"), 990b57cec5SDimitry Andric cl::Hidden); 1000b57cec5SDimitry Andric static cl::opt<std::string> ScalarOptimizerLateEPPipeline( 1010b57cec5SDimitry Andric "passes-ep-scalar-optimizer-late", 1020b57cec5SDimitry Andric cl::desc("A textual description of the function pass pipeline inserted at " 1030b57cec5SDimitry Andric "the ScalarOptimizerLate extension point into default pipelines"), 1040b57cec5SDimitry Andric cl::Hidden); 1050b57cec5SDimitry Andric static cl::opt<std::string> CGSCCOptimizerLateEPPipeline( 1060b57cec5SDimitry Andric "passes-ep-cgscc-optimizer-late", 1070b57cec5SDimitry Andric cl::desc("A textual description of the cgscc pass pipeline inserted at " 1080b57cec5SDimitry Andric "the CGSCCOptimizerLate extension point into default pipelines"), 1090b57cec5SDimitry Andric cl::Hidden); 1100b57cec5SDimitry Andric static cl::opt<std::string> VectorizerStartEPPipeline( 1110b57cec5SDimitry Andric "passes-ep-vectorizer-start", 1120b57cec5SDimitry Andric cl::desc("A textual description of the function pass pipeline inserted at " 1130b57cec5SDimitry Andric "the VectorizerStart extension point into default pipelines"), 1140b57cec5SDimitry Andric cl::Hidden); 1150b57cec5SDimitry Andric static cl::opt<std::string> PipelineStartEPPipeline( 1160b57cec5SDimitry Andric "passes-ep-pipeline-start", 117e8d8bef9SDimitry Andric cl::desc("A textual description of the module pass pipeline inserted at " 1180b57cec5SDimitry Andric "the PipelineStart extension point into default pipelines"), 1190b57cec5SDimitry Andric cl::Hidden); 120e8d8bef9SDimitry Andric static cl::opt<std::string> PipelineEarlySimplificationEPPipeline( 121e8d8bef9SDimitry Andric "passes-ep-pipeline-early-simplification", 122e8d8bef9SDimitry Andric cl::desc("A textual description of the module pass pipeline inserted at " 123e8d8bef9SDimitry Andric "the EarlySimplification extension point into default pipelines"), 124e8d8bef9SDimitry Andric cl::Hidden); 1250b57cec5SDimitry Andric static cl::opt<std::string> OptimizerLastEPPipeline( 1260b57cec5SDimitry Andric "passes-ep-optimizer-last", 127e8d8bef9SDimitry Andric cl::desc("A textual description of the module pass pipeline inserted at " 1280b57cec5SDimitry Andric "the OptimizerLast extension point into default pipelines"), 1290b57cec5SDimitry Andric cl::Hidden); 1300b57cec5SDimitry Andric 1315ffd83dbSDimitry Andric // Individual pipeline tuning options. 1325ffd83dbSDimitry Andric extern cl::opt<bool> DisableLoopUnrolling; 1335ffd83dbSDimitry Andric 134fe6060f1SDimitry Andric namespace llvm { 1350b57cec5SDimitry Andric extern cl::opt<PGOKind> PGOKindFlag; 1360b57cec5SDimitry Andric extern cl::opt<std::string> ProfileFile; 1370b57cec5SDimitry Andric extern cl::opt<CSPGOKind> CSPGOKindFlag; 1380b57cec5SDimitry Andric extern cl::opt<std::string> CSProfileGenFile; 139e8d8bef9SDimitry Andric extern cl::opt<bool> DisableBasicAA; 140*349cc55cSDimitry Andric extern cl::opt<bool> PrintPipelinePasses; 141fe6060f1SDimitry Andric } // namespace llvm 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric static cl::opt<std::string> 1440b57cec5SDimitry Andric ProfileRemappingFile("profile-remapping-file", 1450b57cec5SDimitry Andric cl::desc("Path to the profile remapping file."), 1460b57cec5SDimitry Andric cl::Hidden); 1470b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling( 1480b57cec5SDimitry Andric "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden, 1490b57cec5SDimitry Andric cl::desc("Emit special debug info to enable PGO profile generation.")); 150e8d8bef9SDimitry Andric static cl::opt<bool> PseudoProbeForProfiling( 151e8d8bef9SDimitry Andric "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden, 152e8d8bef9SDimitry Andric cl::desc("Emit pseudo probes to enable PGO profile generation.")); 1530b57cec5SDimitry Andric /// @}} 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric template <typename PassManagerT> 1560b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB, 1570b57cec5SDimitry Andric const cl::opt<std::string> &PipelineOpt) { 1580b57cec5SDimitry Andric if (PipelineOpt.empty()) 1590b57cec5SDimitry Andric return false; 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric // Verify the pipeline is parseable: 1620b57cec5SDimitry Andric PassManagerT PM; 1630b57cec5SDimitry Andric if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) { 1640b57cec5SDimitry Andric errs() << "Could not parse -" << PipelineOpt.ArgStr 1650b57cec5SDimitry Andric << " pipeline: " << toString(std::move(Err)) 1660b57cec5SDimitry Andric << "... I'm going to ignore it.\n"; 1670b57cec5SDimitry Andric return false; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric return true; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks 1730b57cec5SDimitry Andric /// for parsing and inserting the given pipeline 174e8d8bef9SDimitry Andric static void registerEPCallbacks(PassBuilder &PB) { 1750b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline)) 1760b57cec5SDimitry Andric PB.registerPeepholeEPCallback( 177*349cc55cSDimitry Andric [&PB](FunctionPassManager &PM, OptimizationLevel Level) { 1780b57cec5SDimitry Andric ExitOnError Err("Unable to parse PeepholeEP pipeline: "); 179e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PeepholeEPPipeline)); 1800b57cec5SDimitry Andric }); 1810b57cec5SDimitry Andric if (tryParsePipelineText<LoopPassManager>(PB, 1820b57cec5SDimitry Andric LateLoopOptimizationsEPPipeline)) 1830b57cec5SDimitry Andric PB.registerLateLoopOptimizationsEPCallback( 184*349cc55cSDimitry Andric [&PB](LoopPassManager &PM, OptimizationLevel Level) { 1850b57cec5SDimitry Andric ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: "); 186e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline)); 1870b57cec5SDimitry Andric }); 1880b57cec5SDimitry Andric if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline)) 1890b57cec5SDimitry Andric PB.registerLoopOptimizerEndEPCallback( 190*349cc55cSDimitry Andric [&PB](LoopPassManager &PM, OptimizationLevel Level) { 1910b57cec5SDimitry Andric ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: "); 192e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline)); 1930b57cec5SDimitry Andric }); 1940b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, 1950b57cec5SDimitry Andric ScalarOptimizerLateEPPipeline)) 1960b57cec5SDimitry Andric PB.registerScalarOptimizerLateEPCallback( 197*349cc55cSDimitry Andric [&PB](FunctionPassManager &PM, OptimizationLevel Level) { 1980b57cec5SDimitry Andric ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: "); 199e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline)); 2000b57cec5SDimitry Andric }); 2010b57cec5SDimitry Andric if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline)) 2020b57cec5SDimitry Andric PB.registerCGSCCOptimizerLateEPCallback( 203*349cc55cSDimitry Andric [&PB](CGSCCPassManager &PM, OptimizationLevel Level) { 2040b57cec5SDimitry Andric ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: "); 205e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline)); 2060b57cec5SDimitry Andric }); 2070b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline)) 2080b57cec5SDimitry Andric PB.registerVectorizerStartEPCallback( 209*349cc55cSDimitry Andric [&PB](FunctionPassManager &PM, OptimizationLevel Level) { 2100b57cec5SDimitry Andric ExitOnError Err("Unable to parse VectorizerStartEP pipeline: "); 211e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline)); 2120b57cec5SDimitry Andric }); 2130b57cec5SDimitry Andric if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline)) 2140b57cec5SDimitry Andric PB.registerPipelineStartEPCallback( 215*349cc55cSDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) { 2160b57cec5SDimitry Andric ExitOnError Err("Unable to parse PipelineStartEP pipeline: "); 217e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline)); 218e8d8bef9SDimitry Andric }); 219e8d8bef9SDimitry Andric if (tryParsePipelineText<ModulePassManager>( 220e8d8bef9SDimitry Andric PB, PipelineEarlySimplificationEPPipeline)) 221e8d8bef9SDimitry Andric PB.registerPipelineEarlySimplificationEPCallback( 222*349cc55cSDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) { 223e8d8bef9SDimitry Andric ExitOnError Err("Unable to parse EarlySimplification pipeline: "); 224e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline)); 2250b57cec5SDimitry Andric }); 2260b57cec5SDimitry Andric if (tryParsePipelineText<FunctionPassManager>(PB, OptimizerLastEPPipeline)) 2270b57cec5SDimitry Andric PB.registerOptimizerLastEPCallback( 228*349cc55cSDimitry Andric [&PB](ModulePassManager &PM, OptimizationLevel) { 2290b57cec5SDimitry Andric ExitOnError Err("Unable to parse OptimizerLastEP pipeline: "); 230e8d8bef9SDimitry Andric Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline)); 2310b57cec5SDimitry Andric }); 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 234480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext) \ 235480093f4SDimitry Andric llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); 236480093f4SDimitry Andric #include "llvm/Support/Extension.def" 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, 239e8d8bef9SDimitry Andric TargetLibraryInfoImpl *TLII, ToolOutputFile *Out, 240e8d8bef9SDimitry Andric ToolOutputFile *ThinLTOLinkOut, 2410b57cec5SDimitry Andric ToolOutputFile *OptRemarkFile, 2425ffd83dbSDimitry Andric StringRef PassPipeline, ArrayRef<StringRef> Passes, 2435ffd83dbSDimitry Andric OutputKind OK, VerifierKind VK, 2440b57cec5SDimitry Andric bool ShouldPreserveAssemblyUseListOrder, 2450b57cec5SDimitry Andric bool ShouldPreserveBitcodeUseListOrder, 2460b57cec5SDimitry Andric bool EmitSummaryIndex, bool EmitModuleHash, 247fe6060f1SDimitry Andric bool EnableDebugify) { 2480b57cec5SDimitry Andric bool VerifyEachPass = VK == VK_VerifyEachPass; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric Optional<PGOOptions> P; 2510b57cec5SDimitry Andric switch (PGOKindFlag) { 2520b57cec5SDimitry Andric case InstrGen: 2530b57cec5SDimitry Andric P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr); 2540b57cec5SDimitry Andric break; 2550b57cec5SDimitry Andric case InstrUse: 2560b57cec5SDimitry Andric P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse); 2570b57cec5SDimitry Andric break; 2580b57cec5SDimitry Andric case SampleUse: 2590b57cec5SDimitry Andric P = PGOOptions(ProfileFile, "", ProfileRemappingFile, 2600b57cec5SDimitry Andric PGOOptions::SampleUse); 2610b57cec5SDimitry Andric break; 2620b57cec5SDimitry Andric case NoPGO: 263*349cc55cSDimitry Andric if (DebugInfoForProfiling || PseudoProbeForProfiling) 2640b57cec5SDimitry Andric P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction, 265*349cc55cSDimitry Andric DebugInfoForProfiling, PseudoProbeForProfiling); 2660b57cec5SDimitry Andric else 2670b57cec5SDimitry Andric P = None; 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric if (CSPGOKindFlag != NoCSPGO) { 2700b57cec5SDimitry Andric if (P && (P->Action == PGOOptions::IRInstr || 2710b57cec5SDimitry Andric P->Action == PGOOptions::SampleUse)) 2720b57cec5SDimitry Andric errs() << "CSPGOKind cannot be used with IRInstr or SampleUse"; 2730b57cec5SDimitry Andric if (CSPGOKindFlag == CSInstrGen) { 2740b57cec5SDimitry Andric if (CSProfileGenFile.empty()) 2750b57cec5SDimitry Andric errs() << "CSInstrGen needs to specify CSProfileGenFile"; 2760b57cec5SDimitry Andric if (P) { 2770b57cec5SDimitry Andric P->CSAction = PGOOptions::CSIRInstr; 2780b57cec5SDimitry Andric P->CSProfileGenFile = CSProfileGenFile; 2790b57cec5SDimitry Andric } else 2800b57cec5SDimitry Andric P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile, 2810b57cec5SDimitry Andric PGOOptions::NoAction, PGOOptions::CSIRInstr); 2820b57cec5SDimitry Andric } else /* CSPGOKindFlag == CSInstrUse */ { 2830b57cec5SDimitry Andric if (!P) 2840b57cec5SDimitry Andric errs() << "CSInstrUse needs to be together with InstrUse"; 2850b57cec5SDimitry Andric P->CSAction = PGOOptions::CSIRUse; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric } 288*349cc55cSDimitry Andric if (TM) 289*349cc55cSDimitry Andric TM->setPGOOption(P); 290*349cc55cSDimitry Andric 291fe6060f1SDimitry Andric LoopAnalysisManager LAM; 292fe6060f1SDimitry Andric FunctionAnalysisManager FAM; 293fe6060f1SDimitry Andric CGSCCAnalysisManager CGAM; 294fe6060f1SDimitry Andric ModuleAnalysisManager MAM; 295fe6060f1SDimitry Andric 2960b57cec5SDimitry Andric PassInstrumentationCallbacks PIC; 297fe6060f1SDimitry Andric PrintPassOptions PrintPassOpts; 298fe6060f1SDimitry Andric PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose; 299fe6060f1SDimitry Andric PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet; 300fe6060f1SDimitry Andric StandardInstrumentations SI(DebugPM != DebugLogging::None, VerifyEachPass, 301fe6060f1SDimitry Andric PrintPassOpts); 302fe6060f1SDimitry Andric SI.registerCallbacks(PIC, &FAM); 303e8d8bef9SDimitry Andric DebugifyEachInstrumentation Debugify; 304e8d8bef9SDimitry Andric if (DebugifyEach) 305e8d8bef9SDimitry Andric Debugify.registerCallbacks(PIC); 3060b57cec5SDimitry Andric 3075ffd83dbSDimitry Andric PipelineTuningOptions PTO; 3085ffd83dbSDimitry Andric // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized 3095ffd83dbSDimitry Andric // to false above so we shouldn't necessarily need to check whether or not the 3105ffd83dbSDimitry Andric // option has been enabled. 3115ffd83dbSDimitry Andric PTO.LoopUnrolling = !DisableLoopUnrolling; 312fe6060f1SDimitry Andric PassBuilder PB(TM, PTO, P, &PIC); 313e8d8bef9SDimitry Andric registerEPCallbacks(PB); 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric // Load requested pass plugins and let them register pass builder callbacks 3160b57cec5SDimitry Andric for (auto &PluginFN : PassPlugins) { 3170b57cec5SDimitry Andric auto PassPlugin = PassPlugin::Load(PluginFN); 3180b57cec5SDimitry Andric if (!PassPlugin) { 3190b57cec5SDimitry Andric errs() << "Failed to load passes from '" << PluginFN 3200b57cec5SDimitry Andric << "'. Request ignored.\n"; 3210b57cec5SDimitry Andric continue; 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric PassPlugin->registerPassBuilderCallbacks(PB); 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric // Register a callback that creates the debugify passes as needed. 3280b57cec5SDimitry Andric PB.registerPipelineParsingCallback( 3290b57cec5SDimitry Andric [](StringRef Name, ModulePassManager &MPM, 3300b57cec5SDimitry Andric ArrayRef<PassBuilder::PipelineElement>) { 3310b57cec5SDimitry Andric if (Name == "debugify") { 3320b57cec5SDimitry Andric MPM.addPass(NewPMDebugifyPass()); 3330b57cec5SDimitry Andric return true; 3340b57cec5SDimitry Andric } else if (Name == "check-debugify") { 3350b57cec5SDimitry Andric MPM.addPass(NewPMCheckDebugifyPass()); 3360b57cec5SDimitry Andric return true; 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric return false; 3390b57cec5SDimitry Andric }); 340e8d8bef9SDimitry Andric PB.registerPipelineParsingCallback( 341e8d8bef9SDimitry Andric [](StringRef Name, ModulePassManager &MPM, 342e8d8bef9SDimitry Andric ArrayRef<PassBuilder::PipelineElement>) { 343*349cc55cSDimitry Andric AddressSanitizerOptions Opts; 344e8d8bef9SDimitry Andric if (Name == "asan-pipeline") { 345e8d8bef9SDimitry Andric MPM.addPass( 346e8d8bef9SDimitry Andric RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>()); 347*349cc55cSDimitry Andric MPM.addPass(ModuleAddressSanitizerPass(Opts)); 348e8d8bef9SDimitry Andric return true; 349e8d8bef9SDimitry Andric } else if (Name == "asan-function-pipeline") { 350e8d8bef9SDimitry Andric MPM.addPass( 351e8d8bef9SDimitry Andric RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>()); 352e8d8bef9SDimitry Andric MPM.addPass( 353*349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(AddressSanitizerPass(Opts))); 354e8d8bef9SDimitry Andric return true; 355e8d8bef9SDimitry Andric } 356e8d8bef9SDimitry Andric return false; 357e8d8bef9SDimitry Andric }); 3580b57cec5SDimitry Andric 359480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext) \ 360480093f4SDimitry Andric get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); 361480093f4SDimitry Andric #include "llvm/Support/Extension.def" 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric // Specially handle the alias analysis manager so that we can register 3640b57cec5SDimitry Andric // a custom pipeline of AA passes with it. 3650b57cec5SDimitry Andric AAManager AA; 366e8d8bef9SDimitry Andric if (Passes.empty()) { 3670b57cec5SDimitry Andric if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) { 3680b57cec5SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 3690b57cec5SDimitry Andric return false; 3700b57cec5SDimitry Andric } 3715ffd83dbSDimitry Andric } 372e8d8bef9SDimitry Andric 373e8d8bef9SDimitry Andric // For compatibility with the legacy PM AA pipeline. 374e8d8bef9SDimitry Andric // AAResultsWrapperPass by default provides basic-aa in the legacy PM 375e8d8bef9SDimitry Andric // unless -disable-basic-aa is specified. 376e8d8bef9SDimitry Andric // TODO: remove this once tests implicitly requiring basic-aa use -passes= and 377e8d8bef9SDimitry Andric // -aa-pipeline=basic-aa. 378e8d8bef9SDimitry Andric if (!Passes.empty() && !DisableBasicAA) { 379e8d8bef9SDimitry Andric if (auto Err = PB.parseAAPipeline(AA, "basic-aa")) { 380e8d8bef9SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 381e8d8bef9SDimitry Andric return false; 382e8d8bef9SDimitry Andric } 383e8d8bef9SDimitry Andric } 384e8d8bef9SDimitry Andric 3855ffd83dbSDimitry Andric // For compatibility with legacy pass manager. 3865ffd83dbSDimitry Andric // Alias analyses are not specially specified when using the legacy PM. 3875ffd83dbSDimitry Andric for (auto PassName : Passes) { 3885ffd83dbSDimitry Andric if (PB.isAAPassName(PassName)) { 3895ffd83dbSDimitry Andric if (auto Err = PB.parseAAPipeline(AA, PassName)) { 3905ffd83dbSDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 3915ffd83dbSDimitry Andric return false; 3925ffd83dbSDimitry Andric } 3935ffd83dbSDimitry Andric } 3945ffd83dbSDimitry Andric } 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric // Register the AA manager first so that our version is the one used. 3970b57cec5SDimitry Andric FAM.registerPass([&] { return std::move(AA); }); 398e8d8bef9SDimitry Andric // Register our TargetLibraryInfoImpl. 399e8d8bef9SDimitry Andric FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); }); 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric // Register all the basic analyses with the managers. 4020b57cec5SDimitry Andric PB.registerModuleAnalyses(MAM); 4030b57cec5SDimitry Andric PB.registerCGSCCAnalyses(CGAM); 4040b57cec5SDimitry Andric PB.registerFunctionAnalyses(FAM); 4050b57cec5SDimitry Andric PB.registerLoopAnalyses(LAM); 4060b57cec5SDimitry Andric PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 4070b57cec5SDimitry Andric 408fe6060f1SDimitry Andric ModulePassManager MPM; 4090b57cec5SDimitry Andric if (VK > VK_NoVerifier) 4100b57cec5SDimitry Andric MPM.addPass(VerifierPass()); 4110b57cec5SDimitry Andric if (EnableDebugify) 4120b57cec5SDimitry Andric MPM.addPass(NewPMDebugifyPass()); 4130b57cec5SDimitry Andric 414*349cc55cSDimitry Andric // Add passes according to the -passes options. 4155ffd83dbSDimitry Andric if (!PassPipeline.empty()) { 4165ffd83dbSDimitry Andric assert(Passes.empty() && 4175ffd83dbSDimitry Andric "PassPipeline and Passes should not both contain passes"); 418e8d8bef9SDimitry Andric if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) { 4190b57cec5SDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 4200b57cec5SDimitry Andric return false; 4210b57cec5SDimitry Andric } 4225ffd83dbSDimitry Andric } 423*349cc55cSDimitry Andric // Add passes specified using the legacy PM syntax (i.e. not using 424*349cc55cSDimitry Andric // -passes). This should be removed later when such support has been 425*349cc55cSDimitry Andric // deprecated, i.e. when all lit tests running opt (and not using 426*349cc55cSDimitry Andric // -enable-new-pm=0) have been updated to use -passes. 427e8d8bef9SDimitry Andric for (auto PassName : Passes) { 4285ffd83dbSDimitry Andric std::string ModifiedPassName(PassName.begin(), PassName.end()); 4295ffd83dbSDimitry Andric if (PB.isAnalysisPassName(PassName)) 4305ffd83dbSDimitry Andric ModifiedPassName = "require<" + ModifiedPassName + ">"; 431*349cc55cSDimitry Andric // FIXME: These translations are supposed to be removed when lit tests that 432*349cc55cSDimitry Andric // use these names have been updated to use the -passes syntax (and when the 433*349cc55cSDimitry Andric // support for using the old syntax to specify passes is considered as 434*349cc55cSDimitry Andric // deprecated for the new PM). 435*349cc55cSDimitry Andric if (ModifiedPassName == "early-cse-memssa") 436*349cc55cSDimitry Andric ModifiedPassName = "early-cse<memssa>"; 437*349cc55cSDimitry Andric else if (ModifiedPassName == "post-inline-ee-instrument") 438*349cc55cSDimitry Andric ModifiedPassName = "ee-instrument<post-inline>"; 439*349cc55cSDimitry Andric else if (ModifiedPassName == "loop-extract-single") 440*349cc55cSDimitry Andric ModifiedPassName = "loop-extract<single>"; 441*349cc55cSDimitry Andric else if (ModifiedPassName == "lower-matrix-intrinsics-minimal") 442*349cc55cSDimitry Andric ModifiedPassName = "lower-matrix-intrinsics<minimal>"; 443e8d8bef9SDimitry Andric if (auto Err = PB.parsePassPipeline(MPM, ModifiedPassName)) { 4445ffd83dbSDimitry Andric errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 4455ffd83dbSDimitry Andric return false; 4465ffd83dbSDimitry Andric } 4475ffd83dbSDimitry Andric } 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric if (VK > VK_NoVerifier) 4500b57cec5SDimitry Andric MPM.addPass(VerifierPass()); 4510b57cec5SDimitry Andric if (EnableDebugify) 4520b57cec5SDimitry Andric MPM.addPass(NewPMCheckDebugifyPass()); 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric // Add any relevant output pass at the end of the pipeline. 4550b57cec5SDimitry Andric switch (OK) { 4560b57cec5SDimitry Andric case OK_NoOutput: 4570b57cec5SDimitry Andric break; // No output pass needed. 4580b57cec5SDimitry Andric case OK_OutputAssembly: 4590b57cec5SDimitry Andric MPM.addPass( 4600b57cec5SDimitry Andric PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder)); 4610b57cec5SDimitry Andric break; 4620b57cec5SDimitry Andric case OK_OutputBitcode: 4630b57cec5SDimitry Andric MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder, 4640b57cec5SDimitry Andric EmitSummaryIndex, EmitModuleHash)); 4650b57cec5SDimitry Andric break; 4660b57cec5SDimitry Andric case OK_OutputThinLTOBitcode: 4670b57cec5SDimitry Andric MPM.addPass(ThinLTOBitcodeWriterPass( 4680b57cec5SDimitry Andric Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr)); 4690b57cec5SDimitry Andric break; 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric // Before executing passes, print the final values of the LLVM options. 4730b57cec5SDimitry Andric cl::PrintOptionValues(); 4740b57cec5SDimitry Andric 475*349cc55cSDimitry Andric // Print a textual, '-passes=' compatible, representation of pipeline if 476*349cc55cSDimitry Andric // requested. 477*349cc55cSDimitry Andric if (PrintPipelinePasses) { 478*349cc55cSDimitry Andric MPM.printPipeline(outs(), [&PIC](StringRef ClassName) { 479*349cc55cSDimitry Andric auto PassName = PIC.getPassNameForClassName(ClassName); 480*349cc55cSDimitry Andric return PassName.empty() ? ClassName : PassName; 481*349cc55cSDimitry Andric }); 482*349cc55cSDimitry Andric outs() << "\n"; 483*349cc55cSDimitry Andric return true; 484*349cc55cSDimitry Andric } 485*349cc55cSDimitry Andric 4860b57cec5SDimitry Andric // Now that we have all of the passes ready, run them. 4870b57cec5SDimitry Andric MPM.run(M, MAM); 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric // Declare success. 4900b57cec5SDimitry Andric if (OK != OK_NoOutput) { 4910b57cec5SDimitry Andric Out->keep(); 4920b57cec5SDimitry Andric if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut) 4930b57cec5SDimitry Andric ThinLTOLinkOut->keep(); 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric if (OptRemarkFile) 4970b57cec5SDimitry Andric OptRemarkFile->keep(); 4980b57cec5SDimitry Andric 499e8d8bef9SDimitry Andric if (DebugifyEach && !DebugifyExport.empty()) 500e8d8bef9SDimitry Andric exportDebugifyStats(DebugifyExport, Debugify.StatsMap); 501e8d8bef9SDimitry Andric 5020b57cec5SDimitry Andric return true; 5030b57cec5SDimitry Andric } 504fe6060f1SDimitry Andric 505fe6060f1SDimitry Andric void llvm::printPasses(raw_ostream &OS) { 506fe6060f1SDimitry Andric PassBuilder PB; 507fe6060f1SDimitry Andric PB.printPassNames(OS); 508fe6060f1SDimitry Andric } 509