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