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