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