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