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