1 //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// 11 /// This file is just a split of the code that logically belongs in opt.cpp but 12 /// that includes the new pass manager headers. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "NewPMDriver.h" 17 #include "Debugify.h" 18 #include "PassPrinters.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/Analysis/AliasAnalysis.h" 21 #include "llvm/Analysis/CGSCCPassManager.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/CommandLine.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/ToolOutputFile.h" 36 #include "llvm/Target/TargetMachine.h" 37 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 38 #include "llvm/Transforms/Scalar/LoopPassManager.h" 39 40 using namespace llvm; 41 using namespace opt_tool; 42 43 static cl::opt<bool> 44 DebugPM("debug-pass-manager", cl::Hidden, 45 cl::desc("Print pass management debugging information")); 46 47 static cl::list<std::string> 48 PassPlugins("load-pass-plugin", 49 cl::desc("Load passes from plugin library")); 50 51 // This flag specifies a textual description of the alias analysis pipeline to 52 // use when querying for aliasing information. It only works in concert with 53 // the "passes" flag above. 54 static cl::opt<std::string> 55 AAPipeline("aa-pipeline", 56 cl::desc("A textual description of the alias analysis " 57 "pipeline for handling managed aliasing queries"), 58 cl::Hidden); 59 60 /// {{@ These options accept textual pipeline descriptions which will be 61 /// inserted into default pipelines at the respective extension points 62 static cl::opt<std::string> PeepholeEPPipeline( 63 "passes-ep-peephole", 64 cl::desc("A textual description of the function pass pipeline inserted at " 65 "the Peephole extension points into default pipelines"), 66 cl::Hidden); 67 static cl::opt<std::string> LateLoopOptimizationsEPPipeline( 68 "passes-ep-late-loop-optimizations", 69 cl::desc( 70 "A textual description of the loop pass pipeline inserted at " 71 "the LateLoopOptimizations extension point into default pipelines"), 72 cl::Hidden); 73 static cl::opt<std::string> LoopOptimizerEndEPPipeline( 74 "passes-ep-loop-optimizer-end", 75 cl::desc("A textual description of the loop pass pipeline inserted at " 76 "the LoopOptimizerEnd extension point into default pipelines"), 77 cl::Hidden); 78 static cl::opt<std::string> ScalarOptimizerLateEPPipeline( 79 "passes-ep-scalar-optimizer-late", 80 cl::desc("A textual description of the function pass pipeline inserted at " 81 "the ScalarOptimizerLate extension point into default pipelines"), 82 cl::Hidden); 83 static cl::opt<std::string> CGSCCOptimizerLateEPPipeline( 84 "passes-ep-cgscc-optimizer-late", 85 cl::desc("A textual description of the cgscc pass pipeline inserted at " 86 "the CGSCCOptimizerLate extension point into default pipelines"), 87 cl::Hidden); 88 static cl::opt<std::string> VectorizerStartEPPipeline( 89 "passes-ep-vectorizer-start", 90 cl::desc("A textual description of the function pass pipeline inserted at " 91 "the VectorizerStart extension point into default pipelines"), 92 cl::Hidden); 93 static cl::opt<std::string> PipelineStartEPPipeline( 94 "passes-ep-pipeline-start", 95 cl::desc("A textual description of the function pass pipeline inserted at " 96 "the PipelineStart extension point into default pipelines"), 97 cl::Hidden); 98 enum PGOKind { NoPGO, InstrGen, InstrUse, SampleUse }; 99 static cl::opt<PGOKind> PGOKindFlag( 100 "pgo-kind", cl::init(NoPGO), cl::Hidden, 101 cl::desc("The kind of profile guided optimization"), 102 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."), 103 clEnumValN(InstrGen, "new-pm-pgo-instr-gen-pipeline", 104 "Instrument the IR to generate profile."), 105 clEnumValN(InstrUse, "new-pm-pgo-instr-use-pipeline", 106 "Use instrumented profile to guide PGO."), 107 clEnumValN(SampleUse, "new-pm-pgo-sample-use-pipeline", 108 "Use sampled profile to guide PGO."))); 109 static cl::opt<std::string> ProfileFile( 110 "profile-file", cl::desc("Path to the profile."), cl::Hidden); 111 static cl::opt<std::string> 112 ProfileRemappingFile("profile-remapping-file", 113 cl::desc("Path to the profile remapping file."), 114 cl::Hidden); 115 static cl::opt<bool> DebugInfoForProfiling( 116 "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden, 117 cl::desc("Emit special debug info to enable PGO profile generation.")); 118 /// @}} 119 120 template <typename PassManagerT> 121 bool tryParsePipelineText(PassBuilder &PB, 122 const cl::opt<std::string> &PipelineOpt) { 123 if (PipelineOpt.empty()) 124 return false; 125 126 // Verify the pipeline is parseable: 127 PassManagerT PM; 128 if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) { 129 errs() << "Could not parse -" << PipelineOpt.ArgStr 130 << " pipeline: " << toString(std::move(Err)) 131 << "... I'm going to ignore it.\n"; 132 return false; 133 } 134 return true; 135 } 136 137 /// If one of the EPPipeline command line options was given, register callbacks 138 /// for parsing and inserting the given pipeline 139 static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass, 140 bool DebugLogging) { 141 if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline)) 142 PB.registerPeepholeEPCallback( 143 [&PB, VerifyEachPass, DebugLogging]( 144 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { 145 ExitOnError Err("Unable to parse PeepholeEP pipeline: "); 146 Err(PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass, 147 DebugLogging)); 148 }); 149 if (tryParsePipelineText<LoopPassManager>(PB, 150 LateLoopOptimizationsEPPipeline)) 151 PB.registerLateLoopOptimizationsEPCallback( 152 [&PB, VerifyEachPass, DebugLogging]( 153 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { 154 ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: "); 155 Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline, 156 VerifyEachPass, DebugLogging)); 157 }); 158 if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline)) 159 PB.registerLoopOptimizerEndEPCallback( 160 [&PB, VerifyEachPass, DebugLogging]( 161 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { 162 ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: "); 163 Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline, 164 VerifyEachPass, DebugLogging)); 165 }); 166 if (tryParsePipelineText<FunctionPassManager>(PB, 167 ScalarOptimizerLateEPPipeline)) 168 PB.registerScalarOptimizerLateEPCallback( 169 [&PB, VerifyEachPass, DebugLogging]( 170 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { 171 ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: "); 172 Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline, 173 VerifyEachPass, DebugLogging)); 174 }); 175 if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline)) 176 PB.registerCGSCCOptimizerLateEPCallback( 177 [&PB, VerifyEachPass, DebugLogging]( 178 CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) { 179 ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: "); 180 Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline, 181 VerifyEachPass, DebugLogging)); 182 }); 183 if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline)) 184 PB.registerVectorizerStartEPCallback( 185 [&PB, VerifyEachPass, DebugLogging]( 186 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { 187 ExitOnError Err("Unable to parse VectorizerStartEP pipeline: "); 188 Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline, 189 VerifyEachPass, DebugLogging)); 190 }); 191 if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline)) 192 PB.registerPipelineStartEPCallback( 193 [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) { 194 ExitOnError Err("Unable to parse PipelineStartEP pipeline: "); 195 Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass, 196 DebugLogging)); 197 }); 198 } 199 200 #ifdef LINK_POLLY_INTO_TOOLS 201 namespace polly { 202 void RegisterPollyPasses(PassBuilder &); 203 } 204 #endif 205 206 bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, 207 ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut, 208 ToolOutputFile *OptRemarkFile, 209 StringRef PassPipeline, OutputKind OK, 210 VerifierKind VK, 211 bool ShouldPreserveAssemblyUseListOrder, 212 bool ShouldPreserveBitcodeUseListOrder, 213 bool EmitSummaryIndex, bool EmitModuleHash, 214 bool EnableDebugify) { 215 bool VerifyEachPass = VK == VK_VerifyEachPass; 216 217 Optional<PGOOptions> P; 218 switch (PGOKindFlag) { 219 case InstrGen: 220 P = PGOOptions(ProfileFile, "", "", "", true); 221 break; 222 case InstrUse: 223 P = PGOOptions("", ProfileFile, "", ProfileRemappingFile, false); 224 break; 225 case SampleUse: 226 P = PGOOptions("", "", ProfileFile, ProfileRemappingFile, false); 227 break; 228 case NoPGO: 229 if (DebugInfoForProfiling) 230 P = PGOOptions("", "", "", "", false, true); 231 else 232 P = None; 233 } 234 PassInstrumentationCallbacks PIC; 235 StandardInstrumentations SI; 236 SI.registerCallbacks(PIC); 237 238 PassBuilder PB(TM, P, &PIC); 239 registerEPCallbacks(PB, VerifyEachPass, DebugPM); 240 241 // Load requested pass plugins and let them register pass builder callbacks 242 for (auto &PluginFN : PassPlugins) { 243 auto PassPlugin = PassPlugin::Load(PluginFN); 244 if (!PassPlugin) { 245 errs() << "Failed to load passes from '" << PluginFN 246 << "'. Request ignored.\n"; 247 continue; 248 } 249 250 PassPlugin->registerPassBuilderCallbacks(PB); 251 } 252 253 // Register a callback that creates the debugify passes as needed. 254 PB.registerPipelineParsingCallback( 255 [](StringRef Name, ModulePassManager &MPM, 256 ArrayRef<PassBuilder::PipelineElement>) { 257 if (Name == "debugify") { 258 MPM.addPass(NewPMDebugifyPass()); 259 return true; 260 } else if (Name == "check-debugify") { 261 MPM.addPass(NewPMCheckDebugifyPass()); 262 return true; 263 } 264 return false; 265 }); 266 267 #ifdef LINK_POLLY_INTO_TOOLS 268 polly::RegisterPollyPasses(PB); 269 #endif 270 271 // Specially handle the alias analysis manager so that we can register 272 // a custom pipeline of AA passes with it. 273 AAManager AA; 274 if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) { 275 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 276 return false; 277 } 278 279 LoopAnalysisManager LAM(DebugPM); 280 FunctionAnalysisManager FAM(DebugPM); 281 CGSCCAnalysisManager CGAM(DebugPM); 282 ModuleAnalysisManager MAM(DebugPM); 283 284 // Register the AA manager first so that our version is the one used. 285 FAM.registerPass([&] { return std::move(AA); }); 286 287 // Register all the basic analyses with the managers. 288 PB.registerModuleAnalyses(MAM); 289 PB.registerCGSCCAnalyses(CGAM); 290 PB.registerFunctionAnalyses(FAM); 291 PB.registerLoopAnalyses(LAM); 292 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 293 294 ModulePassManager MPM(DebugPM); 295 if (VK > VK_NoVerifier) 296 MPM.addPass(VerifierPass()); 297 if (EnableDebugify) 298 MPM.addPass(NewPMDebugifyPass()); 299 300 if (auto Err = 301 PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) { 302 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; 303 return false; 304 } 305 306 if (VK > VK_NoVerifier) 307 MPM.addPass(VerifierPass()); 308 if (EnableDebugify) 309 MPM.addPass(NewPMCheckDebugifyPass()); 310 311 // Add any relevant output pass at the end of the pipeline. 312 switch (OK) { 313 case OK_NoOutput: 314 break; // No output pass needed. 315 case OK_OutputAssembly: 316 MPM.addPass( 317 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder)); 318 break; 319 case OK_OutputBitcode: 320 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder, 321 EmitSummaryIndex, EmitModuleHash)); 322 break; 323 case OK_OutputThinLTOBitcode: 324 MPM.addPass(ThinLTOBitcodeWriterPass( 325 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr)); 326 break; 327 } 328 329 // Before executing passes, print the final values of the LLVM options. 330 cl::PrintOptionValues(); 331 332 // Now that we have all of the passes ready, run them. 333 MPM.run(M, MAM); 334 335 // Declare success. 336 if (OK != OK_NoOutput) { 337 Out->keep(); 338 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut) 339 ThinLTOLinkOut->keep(); 340 } 341 342 if (OptRemarkFile) 343 OptRemarkFile->keep(); 344 345 return true; 346 } 347