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/Statistic.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Analysis/CGSCCPassManager.h"
21#include "llvm/Analysis/RuntimeLibcallInfo.h"
22#include "llvm/Analysis/TargetLibraryInfo.h"
23#include "llvm/Bitcode/BitcodeWriterPass.h"
24#include "llvm/CodeGen/LibcallLoweringInfo.h"
25#include "llvm/Config/llvm-config.h"
26#include "llvm/IR/Dominators.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/PassManager.h"
30#include "llvm/IR/Verifier.h"
31#include "llvm/IRPrinter/IRPrintingPasses.h"
32#include "llvm/Passes/PassBuilder.h"
33#include "llvm/Passes/StandardInstrumentations.h"
34#include "llvm/Plugins/PassPlugin.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/Timer.h"
37#include "llvm/Support/ToolOutputFile.h"
38#include "llvm/Support/VirtualFileSystem.h"
39#include "llvm/Support/raw_ostream.h"
40#include "llvm/Target/TargetMachine.h"
41#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
42#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
43#include "llvm/Transforms/Scalar/LoopPassManager.h"
44#include "llvm/Transforms/Utils/AssignGUID.h"
45#include "llvm/Transforms/Utils/Debugify.h"
46#include "llvm/Transforms/Utils/ProfileVerify.h"
47
48using namespace llvm;
49using namespace opt_tool;
50
51cl::opt<bool> llvm::DebugifyEach(
52 "debugify-each",
53 cl::desc("Start each pass with debugify and end it with check-debugify"));
54
55cl::opt<std::string> llvm::DebugifyExport(
56 "debugify-export",
57 cl::desc("Export per-pass debugify statistics to this file"),
58 cl::value_desc("filename"));
59
60cl::opt<bool> llvm::VerifyEachDebugInfoPreserve(
61 "verify-each-debuginfo-preserve",
62 cl::desc("Start each pass with collecting and end it with checking of "
63 "debug info preservation."));
64
65cl::opt<std::string> llvm::VerifyDIPreserveExport(
66 "verify-di-preserve-export",
67 cl::desc("Export debug info preservation failures into "
68 "specified (JSON) file (should be abs path as we use"
69 " append mode to insert new JSON objects)"),
70 cl::value_desc("filename"), cl::init(Val: ""));
71
72static cl::opt<bool> EnableLoopFusion("enable-loopfusion", cl::init(Val: false),
73 cl::Hidden,
74 cl::desc("Enable the LoopFuse Pass"));
75
76enum class DebugLogging { None, Normal, Verbose, Quiet };
77
78static cl::opt<DebugLogging> DebugPM(
79 "debug-pass-manager", cl::Hidden, cl::ValueOptional,
80 cl::desc("Print pass management debugging information"),
81 cl::init(Val: DebugLogging::None),
82 cl::values(
83 clEnumValN(DebugLogging::Normal, "", ""),
84 clEnumValN(DebugLogging::Quiet, "quiet",
85 "Skip printing info about analyses"),
86 clEnumValN(
87 DebugLogging::Verbose, "verbose",
88 "Print extra information about adaptors and pass managers")));
89
90// This flag specifies a textual description of the alias analysis pipeline to
91// use when querying for aliasing information. It only works in concert with
92// the "passes" flag above.
93static cl::opt<std::string>
94 AAPipeline("aa-pipeline",
95 cl::desc("A textual description of the alias analysis "
96 "pipeline for handling managed aliasing queries"),
97 cl::Hidden, cl::init(Val: "default"));
98
99/// {{@ These options accept textual pipeline descriptions which will be
100/// inserted into default pipelines at the respective extension points
101static cl::opt<std::string> PeepholeEPPipeline(
102 "passes-ep-peephole",
103 cl::desc("A textual description of the function pass pipeline inserted at "
104 "the Peephole extension points into default pipelines"),
105 cl::Hidden);
106static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
107 "passes-ep-late-loop-optimizations",
108 cl::desc(
109 "A textual description of the loop pass pipeline inserted at "
110 "the LateLoopOptimizations extension point into default pipelines"),
111 cl::Hidden);
112static cl::opt<std::string> LoopOptimizerEndEPPipeline(
113 "passes-ep-loop-optimizer-end",
114 cl::desc("A textual description of the loop pass pipeline inserted at "
115 "the LoopOptimizerEnd extension point into default pipelines"),
116 cl::Hidden);
117static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
118 "passes-ep-scalar-optimizer-late",
119 cl::desc("A textual description of the function pass pipeline inserted at "
120 "the ScalarOptimizerLate extension point into default pipelines"),
121 cl::Hidden);
122static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
123 "passes-ep-cgscc-optimizer-late",
124 cl::desc("A textual description of the cgscc pass pipeline inserted at "
125 "the CGSCCOptimizerLate extension point into default pipelines"),
126 cl::Hidden);
127static cl::opt<std::string> VectorizerStartEPPipeline(
128 "passes-ep-vectorizer-start",
129 cl::desc("A textual description of the function pass pipeline inserted at "
130 "the VectorizerStart extension point into default pipelines"),
131 cl::Hidden);
132static cl::opt<std::string> VectorizerEndEPPipeline(
133 "passes-ep-vectorizer-end",
134 cl::desc("A textual description of the function pass pipeline inserted at "
135 "the VectorizerEnd extension point into default pipelines"),
136 cl::Hidden);
137static cl::opt<std::string> PipelineStartEPPipeline(
138 "passes-ep-pipeline-start",
139 cl::desc("A textual description of the module pass pipeline inserted at "
140 "the PipelineStart extension point into default pipelines"),
141 cl::Hidden);
142static cl::opt<std::string> PipelineEarlySimplificationEPPipeline(
143 "passes-ep-pipeline-early-simplification",
144 cl::desc("A textual description of the module pass pipeline inserted at "
145 "the EarlySimplification extension point into default pipelines"),
146 cl::Hidden);
147static cl::opt<std::string> OptimizerEarlyEPPipeline(
148 "passes-ep-optimizer-early",
149 cl::desc("A textual description of the module pass pipeline inserted at "
150 "the OptimizerEarly extension point into default pipelines"),
151 cl::Hidden);
152static cl::opt<std::string> OptimizerLastEPPipeline(
153 "passes-ep-optimizer-last",
154 cl::desc("A textual description of the module pass pipeline inserted at "
155 "the OptimizerLast extension point into default pipelines"),
156 cl::Hidden);
157static cl::opt<std::string> FullLinkTimeOptimizationEarlyEPPipeline(
158 "passes-ep-full-link-time-optimization-early",
159 cl::desc("A textual description of the module pass pipeline inserted at "
160 "the FullLinkTimeOptimizationEarly extension point into default "
161 "pipelines"),
162 cl::Hidden);
163static cl::opt<std::string> FullLinkTimeOptimizationLastEPPipeline(
164 "passes-ep-full-link-time-optimization-last",
165 cl::desc("A textual description of the module pass pipeline inserted at "
166 "the FullLinkTimeOptimizationLast extension point into default "
167 "pipelines"),
168 cl::Hidden);
169/// @}}
170
171static cl::opt<bool> DisablePipelineVerification(
172 "disable-pipeline-verification",
173 cl::desc("Only has an effect when specified with -print-pipeline-passes. "
174 "Disables verifying that the textual pipeline generated by "
175 "-print-pipeline-passes can be used to create a pipeline."),
176 cl::Hidden);
177
178
179static cl::opt<PGOKind>
180 PGOKindFlag("pgo-kind", cl::init(Val: NoPGO), cl::Hidden,
181 cl::desc("The kind of profile guided optimization"),
182 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
183 clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
184 "Instrument the IR to generate profile."),
185 clEnumValN(InstrUse, "pgo-instr-use-pipeline",
186 "Use instrumented profile to guide PGO."),
187 clEnumValN(SampleUse, "pgo-sample-use-pipeline",
188 "Use sampled profile to guide PGO.")));
189static cl::opt<std::string> ProfileFile("profile-file",
190 cl::desc("Path to the profile."), cl::Hidden);
191static cl::opt<std::string>
192 MemoryProfileFile("memory-profile-file",
193 cl::desc("Path to the memory profile."), cl::Hidden);
194
195static cl::opt<CSPGOKind> CSPGOKindFlag(
196 "cspgo-kind", cl::init(Val: NoCSPGO), cl::Hidden,
197 cl::desc("The kind of context sensitive profile guided optimization"),
198 cl::values(
199 clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
200 clEnumValN(
201 CSInstrGen, "cspgo-instr-gen-pipeline",
202 "Instrument (context sensitive) the IR to generate profile."),
203 clEnumValN(
204 CSInstrUse, "cspgo-instr-use-pipeline",
205 "Use instrumented (context sensitive) profile to guide PGO.")));
206
207static cl::opt<std::string> CSProfileGenFile(
208 "cs-profilegen-file",
209 cl::desc("Path to the instrumented context sensitive profile."),
210 cl::Hidden);
211
212static cl::opt<std::string>
213 ProfileRemappingFile("profile-remapping-file",
214 cl::desc("Path to the profile remapping file."),
215 cl::Hidden);
216
217static cl::opt<PGOOptions::ColdFuncOpt> PGOColdFuncAttr(
218 "pgo-cold-func-opt", cl::init(Val: PGOOptions::ColdFuncOpt::Default), cl::Hidden,
219 cl::desc(
220 "Function attribute to apply to cold functions as determined by PGO"),
221 cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default",
222 "Default (no attribute)"),
223 clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize",
224 "Mark cold functions with optsize."),
225 clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize",
226 "Mark cold functions with minsize."),
227 clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone",
228 "Mark cold functions with optnone.")));
229
230static cl::opt<bool> DebugInfoForProfiling(
231 "debug-info-for-profiling", cl::init(Val: false), cl::Hidden,
232 cl::desc("Emit special debug info to enable PGO profile generation."));
233
234static cl::opt<bool> PseudoProbeForProfiling(
235 "pseudo-probe-for-profiling", cl::init(Val: false), cl::Hidden,
236 cl::desc("Emit pseudo probes to enable PGO profile generation."));
237
238static cl::opt<bool> DisableLoopUnrolling(
239 "disable-loop-unrolling",
240 cl::desc("Disable loop unrolling in all relevant passes"), cl::init(Val: false));
241
242template <typename PassManagerT>
243bool tryParsePipelineText(PassBuilder &PB,
244 const cl::opt<std::string> &PipelineOpt) {
245 if (PipelineOpt.empty())
246 return false;
247
248 // Verify the pipeline is parseable:
249 PassManagerT PM;
250 if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
251 errs() << "Could not parse -" << PipelineOpt.ArgStr
252 << " pipeline: " << toString(std::move(Err))
253 << "... I'm going to ignore it.\n";
254 return false;
255 }
256 return true;
257}
258
259/// If one of the EPPipeline command line options was given, register callbacks
260/// for parsing and inserting the given pipeline
261static void registerEPCallbacks(PassBuilder &PB) {
262 if (tryParsePipelineText<FunctionPassManager>(PB, PipelineOpt: PeepholeEPPipeline))
263 PB.registerPeepholeEPCallback(
264 C: [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
265 ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
266 Err(PB.parsePassPipeline(FPM&: PM, PipelineText: PeepholeEPPipeline));
267 });
268 if (tryParsePipelineText<LoopPassManager>(PB,
269 PipelineOpt: LateLoopOptimizationsEPPipeline))
270 PB.registerLateLoopOptimizationsEPCallback(
271 C: [&PB](LoopPassManager &PM, OptimizationLevel Level) {
272 ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
273 Err(PB.parsePassPipeline(LPM&: PM, PipelineText: LateLoopOptimizationsEPPipeline));
274 });
275 if (tryParsePipelineText<LoopPassManager>(PB, PipelineOpt: LoopOptimizerEndEPPipeline))
276 PB.registerLoopOptimizerEndEPCallback(
277 C: [&PB](LoopPassManager &PM, OptimizationLevel Level) {
278 ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
279 Err(PB.parsePassPipeline(LPM&: PM, PipelineText: LoopOptimizerEndEPPipeline));
280 });
281 if (tryParsePipelineText<FunctionPassManager>(PB,
282 PipelineOpt: ScalarOptimizerLateEPPipeline))
283 PB.registerScalarOptimizerLateEPCallback(
284 C: [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
285 ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
286 Err(PB.parsePassPipeline(FPM&: PM, PipelineText: ScalarOptimizerLateEPPipeline));
287 });
288 if (tryParsePipelineText<CGSCCPassManager>(PB, PipelineOpt: CGSCCOptimizerLateEPPipeline))
289 PB.registerCGSCCOptimizerLateEPCallback(
290 C: [&PB](CGSCCPassManager &PM, OptimizationLevel Level) {
291 ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
292 Err(PB.parsePassPipeline(CGPM&: PM, PipelineText: CGSCCOptimizerLateEPPipeline));
293 });
294 if (tryParsePipelineText<FunctionPassManager>(PB, PipelineOpt: VectorizerStartEPPipeline))
295 PB.registerVectorizerStartEPCallback(
296 C: [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
297 ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
298 Err(PB.parsePassPipeline(FPM&: PM, PipelineText: VectorizerStartEPPipeline));
299 });
300 if (tryParsePipelineText<FunctionPassManager>(PB, PipelineOpt: VectorizerEndEPPipeline))
301 PB.registerVectorizerEndEPCallback(
302 C: [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
303 ExitOnError Err("Unable to parse VectorizerEndEP pipeline: ");
304 Err(PB.parsePassPipeline(FPM&: PM, PipelineText: VectorizerEndEPPipeline));
305 });
306 if (tryParsePipelineText<ModulePassManager>(PB, PipelineOpt: PipelineStartEPPipeline))
307 PB.registerPipelineStartEPCallback(
308 C: [&PB](ModulePassManager &PM, OptimizationLevel) {
309 ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
310 Err(PB.parsePassPipeline(MPM&: PM, PipelineText: PipelineStartEPPipeline));
311 });
312 if (tryParsePipelineText<ModulePassManager>(
313 PB, PipelineOpt: PipelineEarlySimplificationEPPipeline))
314 PB.registerPipelineEarlySimplificationEPCallback(
315 C: [&PB](ModulePassManager &PM, OptimizationLevel, ThinOrFullLTOPhase) {
316 ExitOnError Err("Unable to parse EarlySimplification pipeline: ");
317 Err(PB.parsePassPipeline(MPM&: PM, PipelineText: PipelineEarlySimplificationEPPipeline));
318 });
319 if (tryParsePipelineText<ModulePassManager>(PB, PipelineOpt: OptimizerEarlyEPPipeline))
320 PB.registerOptimizerEarlyEPCallback(
321 C: [&PB](ModulePassManager &PM, OptimizationLevel, ThinOrFullLTOPhase) {
322 ExitOnError Err("Unable to parse OptimizerEarlyEP pipeline: ");
323 Err(PB.parsePassPipeline(MPM&: PM, PipelineText: OptimizerEarlyEPPipeline));
324 });
325 if (tryParsePipelineText<ModulePassManager>(PB, PipelineOpt: OptimizerLastEPPipeline))
326 PB.registerOptimizerLastEPCallback(
327 C: [&PB](ModulePassManager &PM, OptimizationLevel, ThinOrFullLTOPhase) {
328 ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
329 Err(PB.parsePassPipeline(MPM&: PM, PipelineText: OptimizerLastEPPipeline));
330 });
331 if (tryParsePipelineText<ModulePassManager>(
332 PB, PipelineOpt: FullLinkTimeOptimizationEarlyEPPipeline))
333 PB.registerFullLinkTimeOptimizationEarlyEPCallback(
334 C: [&PB](ModulePassManager &PM, OptimizationLevel) {
335 ExitOnError Err(
336 "Unable to parse FullLinkTimeOptimizationEarlyEP pipeline: ");
337 Err(PB.parsePassPipeline(MPM&: PM,
338 PipelineText: FullLinkTimeOptimizationEarlyEPPipeline));
339 });
340 if (tryParsePipelineText<ModulePassManager>(
341 PB, PipelineOpt: FullLinkTimeOptimizationLastEPPipeline))
342 PB.registerFullLinkTimeOptimizationLastEPCallback(
343 C: [&PB](ModulePassManager &PM, OptimizationLevel) {
344 ExitOnError Err(
345 "Unable to parse FullLinkTimeOptimizationLastEP pipeline: ");
346 Err(PB.parsePassPipeline(MPM&: PM, PipelineText: FullLinkTimeOptimizationLastEPPipeline));
347 });
348}
349
350#define HANDLE_EXTENSION(Ext) \
351 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
352#include "llvm/Support/Extension.def"
353#undef HANDLE_EXTENSION
354
355bool llvm::runPassPipeline(
356 StringRef Arg0, Module &M, TargetMachine *TM, TargetLibraryInfoImpl *TLII,
357 ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
358 ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
359 ArrayRef<PassPlugin> PassPlugins,
360 ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks,
361 OutputKind OK, VerifierKind VK, bool ShouldPreserveAssemblyUseListOrder,
362 bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,
363 bool EmitModuleHash, bool EnableDebugify, bool VerifyDIPreserve,
364 bool EnableProfcheck, bool UnifiedLTO) {
365 std::optional<PGOOptions> P;
366 switch (PGOKindFlag) {
367 case InstrGen:
368 P = PGOOptions(ProfileFile, "", "", MemoryProfileFile, PGOOptions::IRInstr,
369 PGOOptions::NoCSAction, PGOColdFuncAttr);
370 break;
371 case InstrUse:
372 P = PGOOptions(ProfileFile, "", ProfileRemappingFile, MemoryProfileFile,
373 PGOOptions::IRUse, PGOOptions::NoCSAction, PGOColdFuncAttr);
374 break;
375 case SampleUse:
376 P = PGOOptions(ProfileFile, "", ProfileRemappingFile, MemoryProfileFile,
377 PGOOptions::SampleUse, PGOOptions::NoCSAction,
378 PGOColdFuncAttr);
379 break;
380 case NoPGO:
381 if (DebugInfoForProfiling || PseudoProbeForProfiling ||
382 !MemoryProfileFile.empty())
383 P = PGOOptions("", "", "", MemoryProfileFile, PGOOptions::NoAction,
384 PGOOptions::NoCSAction, PGOColdFuncAttr,
385 DebugInfoForProfiling, PseudoProbeForProfiling);
386 else
387 P = std::nullopt;
388 }
389 if (CSPGOKindFlag != NoCSPGO) {
390 if (P && (P->Action == PGOOptions::IRInstr ||
391 P->Action == PGOOptions::SampleUse)) {
392 errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
393 return false;
394 }
395 if (CSPGOKindFlag == CSInstrGen) {
396 if (CSProfileGenFile.empty()) {
397 errs() << "CSInstrGen needs to specify CSProfileGenFile";
398 return false;
399 }
400 if (P) {
401 P->CSAction = PGOOptions::CSIRInstr;
402 P->CSProfileGenFile = CSProfileGenFile;
403 } else
404 P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
405 /*MemoryProfile=*/"", PGOOptions::NoAction,
406 PGOOptions::CSIRInstr);
407 } else /* CSPGOKindFlag == CSInstrUse */ {
408 if (!P) {
409 errs() << "CSInstrUse needs to be together with InstrUse";
410 return false;
411 }
412 P->CSAction = PGOOptions::CSIRUse;
413 }
414 }
415
416 LoopAnalysisManager LAM;
417 FunctionAnalysisManager FAM;
418 CGSCCAnalysisManager CGAM;
419 ModuleAnalysisManager MAM;
420
421 if (TM) {
422 TM->setPGOOption(P);
423
424 MAM.registerPass(PassBuilder: [&] {
425 const TargetOptions &Options = TM->Options;
426 return RuntimeLibraryAnalysis(M.getTargetTriple(), Options.ExceptionModel,
427 Options.FloatABIType, Options.EABIVersion,
428 Options.MCOptions.ABIName, Options.VecLib);
429 });
430 }
431
432 PassInstrumentationCallbacks PIC;
433 PrintPassOptions PrintPassOpts;
434 PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
435 PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
436 StandardInstrumentations SI(M.getContext(), DebugPM != DebugLogging::None,
437 VK == VerifierKind::EachPass, PrintPassOpts);
438 SI.registerCallbacks(PIC, MAM: &MAM);
439 DebugifyEachInstrumentation Debugify;
440 DebugifyStatsMap DIStatsMap;
441 DebugInfoPerPass DebugInfoBeforePass;
442 if (DebugifyEach) {
443 Debugify.setDIStatsMap(DIStatsMap);
444 Debugify.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
445 Debugify.registerCallbacks(PIC, MAM);
446 } else if (VerifyEachDebugInfoPreserve) {
447 Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
448 Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
449 Debugify.setOrigDIVerifyBugsReportFilePath(
450 VerifyDIPreserveExport);
451 Debugify.registerCallbacks(PIC, MAM);
452 }
453
454 PipelineTuningOptions PTO;
455 // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
456 // to false above so we shouldn't necessarily need to check whether or not the
457 // option has been enabled.
458 PTO.LoopUnrolling = !DisableLoopUnrolling;
459 PTO.UnifiedLTO = UnifiedLTO;
460 PTO.LoopFusion = EnableLoopFusion;
461 PassBuilder PB(TM, PTO, P, &PIC);
462 registerEPCallbacks(PB);
463
464 // For any loaded plugins, let them register pass builder callbacks.
465 for (auto &PassPlugin : PassPlugins)
466 PassPlugin.registerPassBuilderCallbacks(PB);
467
468 // Load any explicitly specified plugins.
469 for (auto &PassCallback : PassBuilderCallbacks)
470 PassCallback(PB);
471
472#define HANDLE_EXTENSION(Ext) \
473 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
474#include "llvm/Support/Extension.def"
475#undef HANDLE_EXTENSION
476
477 // Specially handle the alias analysis manager so that we can register
478 // a custom pipeline of AA passes with it.
479 AAManager AA;
480 if (auto Err = PB.parseAAPipeline(AA, PipelineText: AAPipeline)) {
481 errs() << Arg0 << ": " << toString(E: std::move(Err)) << "\n";
482 return false;
483 }
484
485 // Register the AA manager first so that our version is the one used.
486 FAM.registerPass(PassBuilder: [&] { return std::move(AA); });
487 // Register our TargetLibraryInfoImpl.
488 FAM.registerPass(PassBuilder: [&] { return TargetLibraryAnalysis(*TLII); });
489
490 // Register all the basic analyses with the managers.
491 PB.registerModuleAnalyses(MAM);
492 PB.registerCGSCCAnalyses(CGAM);
493 PB.registerFunctionAnalyses(FAM);
494 PB.registerLoopAnalyses(LAM);
495 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
496
497 ModulePassManager MPM;
498 if (EnableDebugify)
499 MPM.addPass(Pass: NewPMDebugifyPass());
500 if (VerifyDIPreserve)
501 MPM.addPass(Pass: NewPMDebugifyPass(DebugifyMode::OriginalDebugInfo, "",
502 &DebugInfoBeforePass));
503 if (EnableProfcheck)
504 MPM.addPass(Pass: createModuleToFunctionPassAdaptor(Pass: ProfileInjectorPass()));
505 // Add passes according to the -passes options.
506 if (!PassPipeline.empty()) {
507 if (auto Err = PB.parsePassPipeline(MPM, PipelineText: PassPipeline)) {
508 errs() << Arg0 << ": " << toString(E: std::move(Err)) << "\n";
509 return false;
510 }
511 }
512
513 if (VK != VerifierKind::None)
514 MPM.addPass(Pass: VerifierPass());
515 if (EnableDebugify)
516 MPM.addPass(Pass: NewPMCheckDebugifyPass(false, "", &DIStatsMap));
517 if (VerifyDIPreserve)
518 MPM.addPass(Pass: NewPMCheckDebugifyPass(
519 false, "", nullptr, DebugifyMode::OriginalDebugInfo,
520 &DebugInfoBeforePass, VerifyDIPreserveExport));
521 if (EnableProfcheck)
522 MPM.addPass(Pass: ProfileVerifierPass());
523
524 // Add any relevant output pass at the end of the pipeline.
525 switch (OK) {
526 case OK_NoOutput:
527 break; // No output pass needed.
528 case OK_OutputAssembly:
529 if (EmitSummaryIndex) {
530 MPM.addPass(Pass: AssignGUIDPass());
531 }
532 MPM.addPass(Pass: PrintModulePass(
533 Out->os(), "", ShouldPreserveAssemblyUseListOrder, EmitSummaryIndex));
534 break;
535 case OK_OutputBitcode:
536 if (EmitSummaryIndex) {
537 MPM.addPass(Pass: AssignGUIDPass());
538 }
539 MPM.addPass(Pass: BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
540 EmitSummaryIndex, EmitModuleHash));
541 break;
542 case OK_OutputThinLTOBitcode:
543 MPM.addPass(Pass: AssignGUIDPass());
544 MPM.addPass(Pass: ThinLTOBitcodeWriterPass(
545 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr,
546 ShouldPreserveBitcodeUseListOrder));
547 break;
548 }
549
550 // Before executing passes, print the final values of the LLVM options.
551 cl::PrintOptionValues();
552
553 // Print a textual, '-passes=' compatible, representation of pipeline if
554 // requested.
555 if (PrintPipelinePasses) {
556 std::string Pipeline;
557 raw_string_ostream SOS(Pipeline);
558 MPM.printPipeline(OS&: SOS, MapClassName2PassName: [&PIC](StringRef ClassName) {
559 auto PassName = PIC.getPassNameForClassName(ClassName);
560 return PassName.empty() ? ClassName : PassName;
561 });
562 printFormattedPipelinePasses(OS&: outs(), Pipeline, Format: *PrintPipelinePasses);
563 outs() << "\n";
564
565 if (!DisablePipelineVerification) {
566 // Check that we can parse the returned pipeline string as an actual
567 // pipeline.
568 ModulePassManager TempPM;
569 if (auto Err = PB.parsePassPipeline(MPM&: TempPM, PipelineText: Pipeline)) {
570 errs() << "Could not parse dumped pass pipeline: "
571 << toString(E: std::move(Err)) << "\n";
572 return false;
573 }
574 }
575
576 return true;
577 }
578
579 // Now that we have all of the passes ready, run them.
580 MPM.run(IR&: M, AM&: MAM);
581
582 // If a pass reported an error via LLVMContext::emitError, fail without
583 // writing the output module.
584 if (auto *DH = M.getContext().getDiagHandlerPtr()) {
585 if (DH->HasErrors)
586 return false;
587 }
588
589 // Declare success.
590 if (OK != OK_NoOutput) {
591 Out->keep();
592 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
593 ThinLTOLinkOut->keep();
594 }
595
596 if (OptRemarkFile)
597 OptRemarkFile->keep();
598
599 if (DebugifyEach && !DebugifyExport.empty())
600 exportDebugifyStats(Path: DebugifyExport, Map: Debugify.getDebugifyStatsMap());
601
602 TimerGroup::printAll(OS&: *CreateInfoOutputFile());
603 TimerGroup::clearAll();
604
605 return true;
606}
607
608void llvm::printPasses(raw_ostream &OS) {
609 PassBuilder PB;
610 PB.printPassNames(OS);
611}
612