1//===-- llcdriver.cpp - Implement the LLVM Native Code Generator ----------===//
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//
9// This is the llc code generator driver. It provides a convenient
10// command-line interface for generating an assembly file or a relocatable file,
11// given LLVM bitcode.
12//
13//===----------------------------------------------------------------------===//
14
15#include "NewPMDriver.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/ScopeExit.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/Analysis/RuntimeLibcallInfo.h"
20#include "llvm/Analysis/TargetLibraryInfo.h"
21#include "llvm/CodeGen/CommandFlags.h"
22#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
23#include "llvm/CodeGen/LinkAllCodegenComponents.h"
24#include "llvm/CodeGen/MIRParser/MIRParser.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
27#include "llvm/CodeGen/TargetPassConfig.h"
28#include "llvm/CodeGen/TargetSubtargetInfo.h"
29#include "llvm/IR/AutoUpgrade.h"
30#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/DiagnosticInfo.h"
32#include "llvm/IR/DiagnosticPrinter.h"
33#include "llvm/IR/LLVMContext.h"
34#include "llvm/IR/LLVMRemarkStreamer.h"
35#include "llvm/IR/LegacyPassManager.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/Verifier.h"
38#include "llvm/IRReader/IRReader.h"
39#include "llvm/InitializePasses.h"
40#include "llvm/MC/MCTargetOptionsCommandFlags.h"
41#include "llvm/MC/TargetRegistry.h"
42#include "llvm/Pass.h"
43#include "llvm/Plugins/PassPlugin.h"
44#include "llvm/Remarks/HotnessThresholdParser.h"
45#include "llvm/Support/CommandLine.h"
46#include "llvm/Support/Debug.h"
47#include "llvm/Support/FileSystem.h"
48#include "llvm/Support/FormattedStream.h"
49#include "llvm/Support/PGOOptions.h"
50#include "llvm/Support/Path.h"
51#include "llvm/Support/SourceMgr.h"
52#include "llvm/Support/TargetSelect.h"
53#include "llvm/Support/TimeProfiler.h"
54#include "llvm/Support/ToolOutputFile.h"
55#include "llvm/Support/WithColor.h"
56#include "llvm/Target/TargetLoweringObjectFile.h"
57#include "llvm/Target/TargetMachine.h"
58#include "llvm/TargetParser/Host.h"
59#include "llvm/TargetParser/SubtargetFeature.h"
60#include "llvm/TargetParser/Triple.h"
61#include "llvm/Transforms/Utils/Cloning.h"
62#include <cassert>
63#include <memory>
64#include <optional>
65using namespace llvm;
66
67static codegen::RegisterCodeGenFlags CGF;
68static codegen::RegisterMTuneFlag MTF;
69static codegen::RegisterSaveStatsFlag SSF;
70
71// General options for llc. Other pass-specific options are specified
72// within the corresponding llc passes, and target-specific options
73// and back-end code generation options are specified with the target machine.
74//
75static cl::opt<std::string>
76 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init(Val: "-"));
77
78static cl::list<std::string>
79 InstPrinterOptions("M", cl::desc("InstPrinter options"));
80
81static cl::opt<std::string>
82 InputLanguage("x", cl::desc("Input language ('ir' or 'mir')"));
83
84static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
85 cl::value_desc("filename"));
86
87static cl::opt<std::string>
88 SplitDwarfOutputFile("split-dwarf-output", cl::desc(".dwo output filename"),
89 cl::value_desc("filename"));
90
91static cl::opt<unsigned>
92 TimeCompilations("time-compilations", cl::Hidden, cl::init(Val: 1u),
93 cl::value_desc("N"),
94 cl::desc("Repeat compilation N times for timing"));
95
96static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
97
98static cl::opt<unsigned> TimeTraceGranularity(
99 "time-trace-granularity",
100 cl::desc(
101 "Minimum time granularity (in microseconds) traced by time profiler"),
102 cl::init(Val: 500), cl::Hidden);
103
104static cl::opt<std::string>
105 TimeTraceFile("time-trace-file",
106 cl::desc("Specify time trace file destination"),
107 cl::value_desc("filename"));
108
109static cl::opt<std::string>
110 BinutilsVersion("binutils-version", cl::Hidden,
111 cl::desc("Produced object files can use all ELF features "
112 "supported by this binutils version and newer."
113 "If -no-integrated-as is specified, the generated "
114 "assembly will consider GNU as support."
115 "'none' means that all ELF features can be used, "
116 "regardless of binutils support"));
117
118static cl::opt<bool>
119 PreserveComments("preserve-as-comments", cl::Hidden,
120 cl::desc("Preserve Comments in outputted assembly"),
121 cl::init(Val: true));
122
123// Determine optimization level.
124static cl::opt<char>
125 OptLevel("O",
126 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
127 "(default = '-O2')"),
128 cl::Prefix, cl::init(Val: '2'));
129
130static cl::opt<std::string>
131 TargetTriple("mtriple", cl::desc("Override target triple for module"));
132
133static cl::opt<std::string> SplitDwarfFile(
134 "split-dwarf-file",
135 cl::desc(
136 "Specify the name of the .dwo file to encode in the DWARF output"));
137
138static cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
139 cl::desc("Do not verify input module"));
140
141static cl::opt<bool> VerifyEach("verify-each",
142 cl::desc("Verify after each transform"));
143
144static cl::opt<bool>
145 DisableSimplifyLibCalls("disable-simplify-libcalls",
146 cl::desc("Disable simplify-libcalls"));
147
148static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
149 cl::desc("Show encoding in .s output"));
150
151static cl::opt<unsigned>
152 OutputAsmVariant("output-asm-variant",
153 cl::desc("Syntax variant to use for output printing"));
154
155static cl::opt<bool>
156 DwarfDirectory("dwarf-directory", cl::Hidden,
157 cl::desc("Use .file directives with an explicit directory"),
158 cl::init(Val: true));
159
160static cl::opt<bool> AsmVerbose("asm-verbose",
161 cl::desc("Add comments to directives."),
162 cl::init(Val: true));
163
164static cl::opt<bool>
165 CompileTwice("compile-twice", cl::Hidden,
166 cl::desc("Run everything twice, re-using the same pass "
167 "manager and verify the result is the same."),
168 cl::init(Val: false));
169
170static cl::opt<bool> DiscardValueNames(
171 "discard-value-names",
172 cl::desc("Discard names from Value (other than GlobalValue)."),
173 cl::init(Val: false), cl::Hidden);
174
175static cl::opt<bool>
176 PrintMIR2VecVocab("print-mir2vec-vocab", cl::Hidden,
177 cl::desc("Print MIR2Vec vocabulary contents"),
178 cl::init(Val: false));
179
180static cl::opt<bool>
181 PrintMIR2Vec("print-mir2vec", cl::Hidden,
182 cl::desc("Print MIR2Vec embeddings for functions"),
183 cl::init(Val: false));
184
185static cl::list<std::string> IncludeDirs("I", cl::desc("include search path"));
186
187static cl::opt<bool> RemarksWithHotness(
188 "pass-remarks-with-hotness",
189 cl::desc("With PGO, include profile count in optimization remarks"),
190 cl::Hidden);
191
192static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
193 RemarksHotnessThreshold(
194 "pass-remarks-hotness-threshold",
195 cl::desc("Minimum profile count required for "
196 "an optimization remark to be output. "
197 "Use 'auto' to apply the threshold from profile summary."),
198 cl::value_desc("N or 'auto'"), cl::init(Val: 0), cl::Hidden);
199
200static cl::opt<std::string>
201 RemarksFilename("pass-remarks-output",
202 cl::desc("Output filename for pass remarks"),
203 cl::value_desc("filename"));
204
205static cl::opt<std::string>
206 RemarksPasses("pass-remarks-filter",
207 cl::desc("Only record optimization remarks from passes whose "
208 "names match the given regular expression"),
209 cl::value_desc("regex"));
210
211static cl::opt<std::string> RemarksFormat(
212 "pass-remarks-format",
213 cl::desc("The format used for serializing remarks (default: YAML)"),
214 cl::value_desc("format"), cl::init(Val: "yaml"));
215
216static cl::list<std::string> PassPlugins("load-pass-plugin",
217 cl::desc("Load plugin library"));
218
219static cl::opt<bool> EnableNewPassManager(
220 "enable-new-pm", cl::desc("Enable the new pass manager"), cl::init(Val: false));
221
222// This flag specifies a textual description of the optimization pass pipeline
223// to run over the module. This flag switches opt to use the new pass manager
224// infrastructure, completely disabling all of the flags specific to the old
225// pass management.
226static cl::opt<std::string> PassPipeline(
227 "passes",
228 cl::desc(
229 "A textual description of the pass pipeline. To have analysis passes "
230 "available before a certain pass, add 'require<foo-analysis>'."));
231static cl::alias PassPipeline2("p", cl::aliasopt(PassPipeline),
232 cl::desc("Alias for -passes"));
233
234static std::vector<std::string> &getRunPassNames() {
235 static std::vector<std::string> RunPassNames;
236 return RunPassNames;
237}
238
239namespace {
240struct RunPassOption {
241 void operator=(const std::string &Val) const {
242 if (Val.empty())
243 return;
244 SmallVector<StringRef, 8> PassNames;
245 StringRef(Val).split(A&: PassNames, Separator: ',', MaxSplit: -1, KeepEmpty: false);
246 for (auto PassName : PassNames)
247 getRunPassNames().push_back(x: std::string(PassName));
248 }
249};
250} // namespace
251
252static RunPassOption RunPassOpt;
253
254static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass(
255 "run-pass",
256 cl::desc("Run compiler only for specified passes (comma separated list)"),
257 cl::value_desc("pass-name"), cl::location(L&: RunPassOpt));
258
259// PGO command line options
260enum PGOKind {
261 NoPGO,
262 SampleUse,
263};
264
265static cl::opt<PGOKind>
266 PGOKindFlag("pgo-kind", cl::init(Val: NoPGO), cl::Hidden,
267 cl::desc("The kind of profile guided optimization"),
268 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
269 clEnumValN(SampleUse, "pgo-sample-use-pipeline",
270 "Use sampled profile to guide PGO.")));
271
272// Function to set PGO options on TargetMachine based on command line flags.
273static void setPGOOptions(TargetMachine &TM) {
274 std::optional<PGOOptions> PGOOpt;
275
276 switch (PGOKindFlag) {
277 case SampleUse:
278 // Use default values for other PGOOptions parameters. This parameter
279 // is used to test that PGO data is preserved at -O0.
280 PGOOpt = PGOOptions("", "", "", "", PGOOptions::SampleUse,
281 PGOOptions::NoCSAction);
282 break;
283 case NoPGO:
284 PGOOpt = std::nullopt;
285 break;
286 }
287
288 if (PGOOpt)
289 TM.setPGOOption(PGOOpt);
290}
291
292static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &,
293 LLVMContext &Context, std::string &OutputFilename);
294
295[[noreturn]] static void reportError(Twine Msg, StringRef Filename = "") {
296 SmallString<256> Prefix;
297 if (!Filename.empty()) {
298 if (Filename == "-")
299 Filename = "<stdin>";
300 ("'" + Twine(Filename) + "': ").toStringRef(Out&: Prefix);
301 }
302 WithColor::error(OS&: errs(), Prefix: "llc") << Prefix << Msg << "\n";
303 exit(status: 1);
304}
305
306[[noreturn]] static void reportError(Error Err, StringRef Filename) {
307 assert(Err);
308 handleAllErrors(E: createFileError(F: Filename, E: std::move(Err)),
309 Handlers: [&](const ErrorInfoBase &EI) { reportError(Msg: EI.message()); });
310 llvm_unreachable("reportError() should not return");
311}
312
313static std::unique_ptr<ToolOutputFile> GetOutputStream(Triple::OSType OS) {
314 // If we don't yet have an output filename, make one.
315 if (OutputFilename.empty()) {
316 if (InputFilename == "-")
317 OutputFilename = "-";
318 else {
319 // If InputFilename ends in .bc or .ll, remove it.
320 StringRef IFN = InputFilename;
321 if (IFN.ends_with(Suffix: ".bc") || IFN.ends_with(Suffix: ".ll"))
322 OutputFilename = std::string(IFN.drop_back(N: 3));
323 else if (IFN.ends_with(Suffix: ".mir"))
324 OutputFilename = std::string(IFN.drop_back(N: 4));
325 else
326 OutputFilename = std::string(IFN);
327
328 switch (codegen::getFileType()) {
329 case CodeGenFileType::AssemblyFile:
330 OutputFilename += ".s";
331 break;
332 case CodeGenFileType::ObjectFile:
333 if (OS == Triple::Win32)
334 OutputFilename += ".obj";
335 else
336 OutputFilename += ".o";
337 break;
338 case CodeGenFileType::Null:
339 OutputFilename = "-";
340 break;
341 }
342 }
343 }
344
345 // Decide if we need "binary" output.
346 bool Binary = false;
347 switch (codegen::getFileType()) {
348 case CodeGenFileType::AssemblyFile:
349 break;
350 case CodeGenFileType::ObjectFile:
351 case CodeGenFileType::Null:
352 Binary = true;
353 break;
354 }
355
356 // Open the file.
357 std::error_code EC;
358 sys::fs::OpenFlags OpenFlags = sys::fs::OF_None;
359 if (!Binary)
360 OpenFlags |= sys::fs::OF_TextWithCRLF;
361 auto FDOut = std::make_unique<ToolOutputFile>(args&: OutputFilename, args&: EC, args&: OpenFlags);
362 if (EC)
363 reportError(Msg: EC.message());
364 return FDOut;
365}
366
367// Entry point for the llc compiler.
368//
369extern "C" int llcMain(int argc, char **argv) {
370 // Enable debug stream buffering.
371 EnableDebugBuffering = true;
372
373 // Initialize targets first, so that --version shows registered targets.
374 InitializeAllTargets();
375 InitializeAllTargetMCs();
376 InitializeAllAsmPrinters();
377 InitializeAllAsmParsers();
378
379 // Initialize codegen and IR passes used by llc so that the -print-after,
380 // -print-before, and -stop-after options work.
381 PassRegistry *Registry = PassRegistry::getPassRegistry();
382 initializeCore(*Registry);
383 initializeCodeGen(*Registry);
384 initializeLoopStrengthReducePass(*Registry);
385 initializePostInlineEntryExitInstrumenterPass(*Registry);
386 initializeUnreachableBlockElimLegacyPassPass(*Registry);
387 initializeConstantHoistingLegacyPassPass(*Registry);
388 initializeScalarOpts(*Registry);
389 initializeIPO(*Registry);
390 initializeVectorization(*Registry);
391 initializeScalarizeMaskedMemIntrinLegacyPassPass(*Registry);
392 initializeTransformUtils(*Registry);
393
394 // Initialize debugging passes.
395 initializeScavengerTestPass(*Registry);
396
397 SmallVector<PassPlugin, 1> PluginList;
398 PassPlugins.setCallback([&](const std::string &PluginPath) {
399 auto Plugin = PassPlugin::Load(Filename: PluginPath);
400 if (!Plugin)
401 reportFatalUsageError(Err: Plugin.takeError());
402 PluginList.emplace_back(Args&: Plugin.get());
403 });
404
405 // Register the Target and CPU printer for --version.
406 cl::AddExtraVersionPrinter(func: sys::printDefaultTargetAndDetectedCPU);
407 // Register the target printer for --version.
408 cl::AddExtraVersionPrinter(func: TargetRegistry::printRegisteredTargetsForVersion);
409
410 cl::ParseCommandLineOptions(argc, argv, Overview: "llvm system compiler\n");
411
412 if (!PassPipeline.empty() && !getRunPassNames().empty()) {
413 errs() << "The `llc -run-pass=...` syntax for the new pass manager is "
414 "not supported, please use `llc -passes=<pipeline>` (or the `-p` "
415 "alias for a more concise version).\n";
416 return 1;
417 }
418
419 if (TimeTrace)
420 timeTraceProfilerInitialize(TimeTraceGranularity, ProcName: argv[0]);
421 llvm::scope_exit TimeTraceScopeExit([]() {
422 if (TimeTrace) {
423 if (auto E = timeTraceProfilerWrite(PreferredFileName: TimeTraceFile, FallbackFileName: OutputFilename)) {
424 handleAllErrors(E: std::move(E), Handlers: [&](const StringError &SE) {
425 errs() << SE.getMessage() << "\n";
426 });
427 return;
428 }
429 timeTraceProfilerCleanup();
430 }
431 });
432
433 LLVMContext Context;
434 Context.setDiscardValueNames(DiscardValueNames);
435
436 // Set a diagnostic handler that doesn't exit on the first error
437 Context.setDiagnosticHandler(DH: std::make_unique<LLCDiagnosticHandler>());
438
439 Expected<LLVMRemarkFileHandle> RemarksFileOrErr =
440 setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
441 RemarksFormat, RemarksWithHotness,
442 RemarksHotnessThreshold);
443 if (Error E = RemarksFileOrErr.takeError())
444 reportError(Err: std::move(E), Filename: RemarksFilename);
445 LLVMRemarkFileHandle RemarksFile = std::move(*RemarksFileOrErr);
446
447 codegen::MaybeEnableStatistics();
448 std::string OutputFilename;
449
450 if (InputLanguage != "" && InputLanguage != "ir" && InputLanguage != "mir")
451 reportError(Msg: "input language must be '', 'IR' or 'MIR'");
452
453 // Compile the module TimeCompilations times to give better compile time
454 // metrics.
455 for (unsigned I = TimeCompilations; I; --I)
456 if (int RetVal = compileModule(argv, PluginList, Context, OutputFilename))
457 return RetVal;
458
459 if (RemarksFile)
460 RemarksFile->keep();
461
462 return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "llc");
463}
464
465static bool addPass(PassManagerBase &PM, const char *argv0, StringRef PassName,
466 TargetPassConfig &TPC) {
467 if (PassName == "none")
468 return false;
469
470 const PassRegistry *PR = PassRegistry::getPassRegistry();
471 const PassInfo *PI = PR->getPassInfo(Arg: PassName);
472 if (!PI) {
473 WithColor::error(OS&: errs(), Prefix: argv0)
474 << "run-pass " << PassName << " is not registered.\n";
475 return true;
476 }
477
478 Pass *P;
479 if (PI->getNormalCtor())
480 P = PI->getNormalCtor()();
481 else {
482 WithColor::error(OS&: errs(), Prefix: argv0)
483 << "cannot create pass: " << PI->getPassName() << "\n";
484 return true;
485 }
486 std::string Banner = std::string("After ") + std::string(P->getPassName());
487 TPC.addMachinePrePasses();
488 PM.add(P);
489 TPC.addMachinePostPasses(Banner);
490
491 return false;
492}
493
494static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList,
495 LLVMContext &Context, std::string &OutputFilename) {
496 // Load the module to be compiled...
497 SMDiagnostic Err;
498 std::unique_ptr<Module> M;
499 std::unique_ptr<MIRParser> MIR;
500 Triple TheTriple;
501 std::string CPUStr = codegen::getCPUStr();
502 std::string TuneCPUStr = codegen::getTuneCPUStr();
503 std::string FeaturesStr = codegen::getFeaturesStr();
504
505 // Set attributes on functions as loaded from MIR from command line arguments.
506 auto setMIRFunctionAttributes = [&CPUStr, &TuneCPUStr,
507 &FeaturesStr](Function &F) {
508 codegen::setFunctionAttributes(F, CPU: CPUStr, Features: FeaturesStr, TuneCPU: TuneCPUStr);
509 };
510
511 CodeGenOptLevel OLvl;
512 if (auto Level = CodeGenOpt::parseLevel(C: OptLevel)) {
513 OLvl = *Level;
514 } else {
515 WithColor::error(OS&: errs(), Prefix: argv[0]) << "invalid optimization level.\n";
516 return 1;
517 }
518
519 // Parse 'none' or '$major.$minor'. Disallow -binutils-version=0 because we
520 // use that to indicate the MC default.
521 if (!BinutilsVersion.empty() && BinutilsVersion != "none") {
522 StringRef V = BinutilsVersion.getValue();
523 unsigned Num;
524 if (V.consumeInteger(Radix: 10, Result&: Num) || Num == 0 ||
525 !(V.empty() ||
526 (V.consume_front(Prefix: ".") && !V.consumeInteger(Radix: 10, Result&: Num) && V.empty()))) {
527 WithColor::error(OS&: errs(), Prefix: argv[0])
528 << "invalid -binutils-version, accepting 'none' or major.minor\n";
529 return 1;
530 }
531 }
532 TargetOptions Options;
533 auto InitializeOptions = [&](const Triple &TheTriple) {
534 Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
535
536 if (Options.XCOFFReadOnlyPointers) {
537 if (!TheTriple.isOSAIX())
538 reportError(Msg: "-mxcoff-roptr option is only supported on AIX",
539 Filename: InputFilename);
540
541 // Since the storage mapping class is specified per csect,
542 // without using data sections, it is less effective to use read-only
543 // pointers. Using read-only pointers may cause other RO variables in the
544 // same csect to become RW when the linker acts upon `-bforceimprw`;
545 // therefore, we require that separate data sections are used in the
546 // presence of ReadOnlyPointers. We respect the setting of data-sections
547 // since we have not found reasons to do otherwise that overcome the user
548 // surprise of not respecting the setting.
549 if (!Options.DataSections)
550 reportError(Msg: "-mxcoff-roptr option must be used with -data-sections",
551 Filename: InputFilename);
552 }
553
554 Options.MCOptions.BinutilsVersion =
555 MCTargetOptions::parseBinutilsVersion(Version: BinutilsVersion);
556 Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
557 Options.MCOptions.AsmVerbose = AsmVerbose;
558 Options.MCOptions.PreserveAsmComments = PreserveComments;
559 if (OutputAsmVariant.getNumOccurrences())
560 Options.MCOptions.OutputAsmVariant = OutputAsmVariant;
561 Options.MCOptions.IASSearchPaths = IncludeDirs;
562 Options.MCOptions.InstPrinterOptions = InstPrinterOptions;
563 Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
564 if (DwarfDirectory.getPosition()) {
565 Options.MCOptions.MCUseDwarfDirectory =
566 DwarfDirectory ? MCTargetOptions::EnableDwarfDirectory
567 : MCTargetOptions::DisableDwarfDirectory;
568 } else {
569 // -dwarf-directory is not set explicitly. Some assemblers
570 // (e.g. GNU as or ptxas) do not support `.file directory'
571 // syntax prior to DWARFv5. Let the target decide the default
572 // value.
573 Options.MCOptions.MCUseDwarfDirectory =
574 MCTargetOptions::DefaultDwarfDirectory;
575 }
576 };
577
578 std::optional<Reloc::Model> RM = codegen::getExplicitRelocModel();
579 std::optional<CodeModel::Model> CM = codegen::getExplicitCodeModel();
580
581 const Target *TheTarget = nullptr;
582 std::unique_ptr<TargetMachine> Target;
583
584 // If user just wants to list available options, skip module loading
585 auto MAttrs = codegen::getMAttrs();
586 bool SkipModule =
587 CPUStr == "help" || TuneCPUStr == "help" || is_contained(Range&: MAttrs, Element: "help");
588 if (SkipModule) {
589 if (!TargetTriple.empty())
590 TheTriple = Triple(Triple::normalize(Str: TargetTriple));
591 else
592 TheTriple = Triple(sys::getDefaultTargetTriple());
593
594 // Get the target specific parser.
595 std::string Error;
596 TheTarget =
597 TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error);
598 if (!TheTarget) {
599 WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n";
600 return 1;
601 }
602
603 InitializeOptions(TheTriple);
604 // Pass "help" as CPU for -mtune=help
605 std::string SkipModuleCPU = (TuneCPUStr == "help" ? "help" : CPUStr);
606 // Create the target machine just to print the help info. Use unique_ptr
607 // to avoid a memory leak.
608 Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
609 TT: TheTriple, CPU: SkipModuleCPU, Features: FeaturesStr, Options, RM, CM, OL: OLvl));
610 if (!Target) {
611 WithColor::error(OS&: errs(), Prefix: argv[0])
612 << "could not allocate target machine\n";
613 return 1;
614 }
615
616 // If we don't have a module then just exit now. We do this down
617 // here since the CPU/Feature help is underneath the target machine
618 // creation.
619 return 0;
620 }
621
622 auto SetDataLayout = [&](StringRef DataLayoutTargetTriple,
623 StringRef OldDLStr) -> std::optional<std::string> {
624 // If we are supposed to override the target triple, do so now.
625 std::string IRTargetTriple = DataLayoutTargetTriple.str();
626 if (!TargetTriple.empty())
627 IRTargetTriple = Triple::normalize(Str: TargetTriple);
628 TheTriple = Triple(IRTargetTriple);
629 if (TheTriple.getTriple().empty())
630 TheTriple.setTriple(sys::getDefaultTargetTriple());
631
632 std::string Error;
633 TheTarget =
634 TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error);
635 if (!TheTarget) {
636 WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n";
637 exit(status: 1);
638 }
639
640 InitializeOptions(TheTriple);
641 Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
642 TT: TheTriple, CPU: CPUStr, Features: FeaturesStr, Options, RM, CM, OL: OLvl));
643 if (!Target) {
644 WithColor::error(OS&: errs(), Prefix: argv[0])
645 << "could not allocate target machine\n";
646 exit(status: 1);
647 }
648
649 // Set PGO options based on command line flags
650 setPGOOptions(*Target);
651
652 return Target->createDataLayout().getStringRepresentation();
653 };
654 if (InputLanguage == "mir" ||
655 (InputLanguage == "" && StringRef(InputFilename).ends_with(Suffix: ".mir"))) {
656 MIR = createMIRParserFromFile(Filename: InputFilename, Error&: Err, Context,
657 ProcessIRFunction: setMIRFunctionAttributes);
658 if (MIR)
659 M = MIR->parseIRModule(DataLayoutCallback: SetDataLayout);
660 } else {
661 M = parseIRFile(Filename: InputFilename, Err, Context,
662 Callbacks: ParserCallbacks(SetDataLayout));
663 }
664 if (!M) {
665 Err.print(ProgName: argv[0], S&: WithColor::error(OS&: errs(), Prefix: argv[0]));
666 return 1;
667 }
668
669 M->setTargetTriple(TheTriple);
670
671 std::optional<CodeModel::Model> CM_IR = M->getCodeModel();
672 if (!CM && CM_IR)
673 Target->setCodeModel(*CM_IR);
674 if (std::optional<uint64_t> LDT = codegen::getExplicitLargeDataThreshold())
675 Target->setLargeDataThreshold(*LDT);
676
677 // Figure out where we are going to send the output.
678 std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OS: TheTriple.getOS());
679 if (!Out)
680 return 1;
681
682 // Ensure the filename is passed down to CodeViewDebug.
683 Target->Options.ObjectFilenameForDebug = Out->outputFilename();
684
685 // Return a copy of the output filename via the output param
686 OutputFilename = Out->outputFilename();
687
688 // Tell target that this tool is not necessarily used with argument ABI
689 // compliance (i.e. narrow integer argument extensions).
690 Target->Options.VerifyArgABICompliance = 0;
691
692 std::unique_ptr<ToolOutputFile> DwoOut;
693 if (!SplitDwarfOutputFile.empty()) {
694 std::error_code EC;
695 DwoOut = std::make_unique<ToolOutputFile>(args&: SplitDwarfOutputFile, args&: EC,
696 args: sys::fs::OF_None);
697 if (EC)
698 reportError(Msg: EC.message(), Filename: SplitDwarfOutputFile);
699 }
700
701 // Add an appropriate TargetLibraryInfo pass for the module's triple.
702 TargetLibraryInfoImpl TLII(M->getTargetTriple(), Target->Options.VecLib);
703
704 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
705 if (DisableSimplifyLibCalls)
706 TLII.disableAllFunctions();
707
708 // Verify module immediately to catch problems before doInitialization() is
709 // called on any passes.
710 if (!NoVerify && verifyModule(M: *M, OS: &errs()))
711 reportError(Msg: "input module cannot be verified", Filename: InputFilename);
712
713 // Override function attributes based on CPUStr, TuneCPUStr, FeaturesStr, and
714 // command line flags.
715 codegen::setFunctionAttributes(M&: *M, CPU: CPUStr, Features: FeaturesStr, TuneCPU: TuneCPUStr);
716
717 for (auto &Plugin : PluginList) {
718 CodeGenFileType CGFT = codegen::getFileType();
719 if (Plugin.invokePreCodeGenCallback(M&: *M, TM&: *Target, CGFT, OS&: Out->os())) {
720 // TODO: Deduplicate code with below and the NewPMDriver.
721 if (Context.getDiagHandlerPtr()->HasErrors)
722 exit(status: 1);
723 Out->keep();
724 return 0;
725 }
726 }
727
728 if (mc::getExplicitRelaxAll() &&
729 codegen::getFileType() != CodeGenFileType::ObjectFile)
730 WithColor::warning(OS&: errs(), Prefix: argv[0])
731 << ": warning: ignoring -mc-relax-all because filetype != obj";
732
733 VerifierKind VK = VerifierKind::InputOutput;
734 if (NoVerify)
735 VK = VerifierKind::None;
736 else if (VerifyEach)
737 VK = VerifierKind::EachPass;
738
739 // Use the NewPM if the user specifies -passes (NewPM specific), specifically
740 // requests the NewPM with -enable-new-pm, or the target defaults to the
741 // NewPM, the user has not explicitly disabled the NewPM with
742 // -enable-new-pm=false, and the user has not specified -run-pass.
743 if (!PassPipeline.empty() ||
744 (EnableNewPassManager.getNumOccurrences() > 0 && EnableNewPassManager) ||
745 (Target->shouldDefaultToNewPM() &&
746 !(EnableNewPassManager.getNumOccurrences() && !EnableNewPassManager) &&
747 getRunPassNames().empty())) {
748 return compileModuleWithNewPM(
749 Arg0: argv[0], M: std::move(M), MIR: std::move(MIR), Target: std::move(Target),
750 Out: std::move(Out), DwoOut: std::move(DwoOut), Context, TLII, VK, PassPipeline,
751 PassPlugins: PluginList, FileType: codegen::getFileType());
752 }
753
754 // Build up all of the passes that we want to do to the module.
755 legacy::PassManager PM;
756 PM.add(P: new TargetLibraryInfoWrapperPass(TLII));
757 PM.add(P: new RuntimeLibraryInfoWrapper(Options.MCOptions.ABIName,
758 Target->Options.VecLib));
759
760 {
761 raw_pwrite_stream *OS = &Out->os();
762
763 // Manually do the buffering rather than using buffer_ostream,
764 // so we can memcmp the contents in CompileTwice mode
765 SmallVector<char, 0> Buffer;
766 std::unique_ptr<raw_svector_ostream> BOS;
767 if ((codegen::getFileType() != CodeGenFileType::AssemblyFile &&
768 !Out->os().supportsSeeking()) ||
769 CompileTwice) {
770 BOS = std::make_unique<raw_svector_ostream>(args&: Buffer);
771 OS = BOS.get();
772 }
773
774 const char *argv0 = argv[0];
775 MachineModuleInfoWrapperPass *MMIWP =
776 new MachineModuleInfoWrapperPass(Target.get());
777
778 // Set a temporary diagnostic handler. This is used before
779 // MachineModuleInfoWrapperPass::doInitialization for features like -M.
780 bool HasMCErrors = false;
781 MCContext &MCCtx = MMIWP->getMMI().getContext();
782 MCCtx.setDiagnosticHandler([&](const SMDiagnostic &SMD, bool IsInlineAsm,
783 const SourceMgr &SrcMgr,
784 std::vector<const MDNode *> &LocInfos) {
785 WithColor::error(OS&: errs(), Prefix: argv0) << SMD.getMessage() << '\n';
786 HasMCErrors = true;
787 });
788
789 // Construct a custom pass pipeline that starts after instruction
790 // selection.
791 if (!getRunPassNames().empty()) {
792 if (!MIR) {
793 WithColor::error(OS&: errs(), Prefix: argv[0])
794 << "run-pass is for .mir file only.\n";
795 delete MMIWP;
796 return 1;
797 }
798 TargetPassConfig *PTPC = Target->createPassConfig(PM);
799 TargetPassConfig &TPC = *PTPC;
800 if (TPC.hasLimitedCodeGenPipeline()) {
801 WithColor::error(OS&: errs(), Prefix: argv[0])
802 << "run-pass cannot be used with "
803 << TPC.getLimitedCodeGenPipelineReason() << ".\n";
804 delete PTPC;
805 delete MMIWP;
806 return 1;
807 }
808
809 TPC.setDisableVerify(NoVerify);
810 PM.add(P: &TPC);
811 PM.add(P: MMIWP);
812 TPC.printAndVerify(Banner: "");
813 for (const std::string &RunPassName : getRunPassNames()) {
814 if (addPass(PM, argv0, PassName: RunPassName, TPC))
815 return 1;
816 }
817 TPC.setInitialized();
818 PM.add(P: createPrintMIRPass(OS&: *OS));
819
820 // Add MIR2Vec vocabulary printer if requested
821 if (PrintMIR2VecVocab) {
822 PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs()));
823 }
824
825 // Add MIR2Vec printer if requested
826 if (PrintMIR2Vec) {
827 PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs()));
828 }
829
830 PM.add(P: createFreeMachineFunctionPass());
831 } else {
832 if (Target->addPassesToEmitFile(PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
833 codegen::getFileType(), NoVerify,
834 MMIWP)) {
835 if (!HasMCErrors)
836 reportError(Msg: "target does not support generation of this file type");
837 }
838
839 // Add MIR2Vec vocabulary printer if requested
840 if (PrintMIR2VecVocab) {
841 PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs()));
842 }
843
844 // Add MIR2Vec printer if requested
845 if (PrintMIR2Vec) {
846 PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs()));
847 }
848 }
849
850 Target->getObjFileLowering()->Initialize(ctx&: MMIWP->getMMI().getContext(),
851 TM: *Target);
852 if (MIR) {
853 assert(MMIWP && "Forgot to create MMIWP?");
854 if (MIR->parseMachineFunctions(M&: *M, MMI&: MMIWP->getMMI()))
855 return 1;
856 }
857
858 // Before executing passes, print the final values of the LLVM options.
859 cl::PrintOptionValues();
860
861 // If requested, run the pass manager over the same module again,
862 // to catch any bugs due to persistent state in the passes. Note that
863 // opt has the same functionality, so it may be worth abstracting this out
864 // in the future.
865 SmallVector<char, 0> CompileTwiceBuffer;
866 if (CompileTwice) {
867 std::unique_ptr<Module> M2(llvm::CloneModule(M: *M));
868 PM.run(M&: *M2);
869 CompileTwiceBuffer = Buffer;
870 Buffer.clear();
871 }
872
873 PM.run(M&: *M);
874
875 if (Context.getDiagHandlerPtr()->HasErrors || HasMCErrors)
876 return 1;
877
878 // Compare the two outputs and make sure they're the same
879 if (CompileTwice) {
880 if (Buffer.size() != CompileTwiceBuffer.size() ||
881 (memcmp(s1: Buffer.data(), s2: CompileTwiceBuffer.data(), n: Buffer.size()) !=
882 0)) {
883 errs()
884 << "Running the pass manager twice changed the output.\n"
885 "Writing the result of the second run to the specified output\n"
886 "To generate the one-run comparison binary, just run without\n"
887 "the compile-twice option\n";
888 Out->os() << Buffer;
889 Out->keep();
890 return 1;
891 }
892 }
893
894 if (BOS) {
895 Out->os() << Buffer;
896 }
897 }
898
899 // Declare success.
900 Out->keep();
901 if (DwoOut)
902 DwoOut->keep();
903
904 return 0;
905}
906