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::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 FeaturesStr = codegen::getFeaturesStr();
506
507 // Set attributes on functions as loaded from MIR from command line arguments.
508 auto setMIRFunctionAttributes = [&CPUStr, &FeaturesStr](Function &F) {
509 codegen::setFunctionAttributes(CPU: CPUStr, Features: FeaturesStr, F);
510 };
511
512 auto MAttrs = codegen::getMAttrs();
513 bool SkipModule =
514 CPUStr == "help" || (!MAttrs.empty() && MAttrs.front() == "help");
515
516 CodeGenOptLevel OLvl;
517 if (auto Level = CodeGenOpt::parseLevel(C: OptLevel)) {
518 OLvl = *Level;
519 } else {
520 WithColor::error(OS&: errs(), Prefix: argv[0]) << "invalid optimization level.\n";
521 return 1;
522 }
523
524 // Parse 'none' or '$major.$minor'. Disallow -binutils-version=0 because we
525 // use that to indicate the MC default.
526 if (!BinutilsVersion.empty() && BinutilsVersion != "none") {
527 StringRef V = BinutilsVersion.getValue();
528 unsigned Num;
529 if (V.consumeInteger(Radix: 10, Result&: Num) || Num == 0 ||
530 !(V.empty() ||
531 (V.consume_front(Prefix: ".") && !V.consumeInteger(Radix: 10, Result&: Num) && V.empty()))) {
532 WithColor::error(OS&: errs(), Prefix: argv[0])
533 << "invalid -binutils-version, accepting 'none' or major.minor\n";
534 return 1;
535 }
536 }
537 TargetOptions Options;
538 auto InitializeOptions = [&](const Triple &TheTriple) {
539 Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
540
541 if (Options.XCOFFReadOnlyPointers) {
542 if (!TheTriple.isOSAIX())
543 reportError(Msg: "-mxcoff-roptr option is only supported on AIX",
544 Filename: InputFilename);
545
546 // Since the storage mapping class is specified per csect,
547 // without using data sections, it is less effective to use read-only
548 // pointers. Using read-only pointers may cause other RO variables in the
549 // same csect to become RW when the linker acts upon `-bforceimprw`;
550 // therefore, we require that separate data sections are used in the
551 // presence of ReadOnlyPointers. We respect the setting of data-sections
552 // since we have not found reasons to do otherwise that overcome the user
553 // surprise of not respecting the setting.
554 if (!Options.DataSections)
555 reportError(Msg: "-mxcoff-roptr option must be used with -data-sections",
556 Filename: InputFilename);
557 }
558
559 if (TheTriple.isX86() &&
560 codegen::getFuseFPOps() != FPOpFusion::FPOpFusionMode::Standard)
561 WithColor::warning(OS&: errs(), Prefix: argv[0])
562 << "X86 backend ignores --fp-contract setting; use IR fast-math "
563 "flags instead.";
564
565 Options.BinutilsVersion =
566 TargetMachine::parseBinutilsVersion(Version: BinutilsVersion);
567 Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
568 Options.MCOptions.AsmVerbose = AsmVerbose;
569 Options.MCOptions.PreserveAsmComments = PreserveComments;
570 if (OutputAsmVariant.getNumOccurrences())
571 Options.MCOptions.OutputAsmVariant = OutputAsmVariant;
572 Options.MCOptions.IASSearchPaths = IncludeDirs;
573 Options.MCOptions.InstPrinterOptions = InstPrinterOptions;
574 Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
575 if (DwarfDirectory.getPosition()) {
576 Options.MCOptions.MCUseDwarfDirectory =
577 DwarfDirectory ? MCTargetOptions::EnableDwarfDirectory
578 : MCTargetOptions::DisableDwarfDirectory;
579 } else {
580 // -dwarf-directory is not set explicitly. Some assemblers
581 // (e.g. GNU as or ptxas) do not support `.file directory'
582 // syntax prior to DWARFv5. Let the target decide the default
583 // value.
584 Options.MCOptions.MCUseDwarfDirectory =
585 MCTargetOptions::DefaultDwarfDirectory;
586 }
587 };
588
589 std::optional<Reloc::Model> RM = codegen::getExplicitRelocModel();
590 std::optional<CodeModel::Model> CM = codegen::getExplicitCodeModel();
591
592 const Target *TheTarget = nullptr;
593 std::unique_ptr<TargetMachine> Target;
594
595 // If user just wants to list available options, skip module loading
596 if (!SkipModule) {
597 auto SetDataLayout = [&](StringRef DataLayoutTargetTriple,
598 StringRef OldDLStr) -> std::optional<std::string> {
599 // If we are supposed to override the target triple, do so now.
600 std::string IRTargetTriple = DataLayoutTargetTriple.str();
601 if (!TargetTriple.empty())
602 IRTargetTriple = Triple::normalize(Str: TargetTriple);
603 TheTriple = Triple(IRTargetTriple);
604 if (TheTriple.getTriple().empty())
605 TheTriple.setTriple(sys::getDefaultTargetTriple());
606
607 std::string Error;
608 TheTarget =
609 TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error);
610 if (!TheTarget) {
611 WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n";
612 exit(status: 1);
613 }
614
615 InitializeOptions(TheTriple);
616 Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
617 TT: TheTriple, CPU: CPUStr, Features: FeaturesStr, Options, RM, CM, OL: OLvl));
618 assert(Target && "Could not allocate target machine!");
619
620 // Set PGO options based on command line flags
621 setPGOOptions(*Target);
622
623 return Target->createDataLayout().getStringRepresentation();
624 };
625 if (InputLanguage == "mir" ||
626 (InputLanguage == "" && StringRef(InputFilename).ends_with(Suffix: ".mir"))) {
627 MIR = createMIRParserFromFile(Filename: InputFilename, Error&: Err, Context,
628 ProcessIRFunction: setMIRFunctionAttributes);
629 if (MIR)
630 M = MIR->parseIRModule(DataLayoutCallback: SetDataLayout);
631 } else {
632 M = parseIRFile(Filename: InputFilename, Err, Context,
633 Callbacks: ParserCallbacks(SetDataLayout));
634 }
635 if (!M) {
636 Err.print(ProgName: argv[0], S&: WithColor::error(OS&: errs(), Prefix: argv[0]));
637 return 1;
638 }
639 if (!TargetTriple.empty())
640 M->setTargetTriple(Triple(Triple::normalize(Str: TargetTriple)));
641
642 std::optional<CodeModel::Model> CM_IR = M->getCodeModel();
643 if (!CM && CM_IR)
644 Target->setCodeModel(*CM_IR);
645 if (std::optional<uint64_t> LDT = codegen::getExplicitLargeDataThreshold())
646 Target->setLargeDataThreshold(*LDT);
647 } else {
648 TheTriple = Triple(Triple::normalize(Str: TargetTriple));
649 if (TheTriple.getTriple().empty())
650 TheTriple.setTriple(sys::getDefaultTargetTriple());
651
652 // Get the target specific parser.
653 std::string Error;
654 TheTarget =
655 TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error);
656 if (!TheTarget) {
657 WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n";
658 return 1;
659 }
660
661 InitializeOptions(TheTriple);
662 Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
663 TT: TheTriple, CPU: CPUStr, Features: FeaturesStr, Options, RM, CM, OL: OLvl));
664 assert(Target && "Could not allocate target machine!");
665
666 // Set PGO options based on command line flags
667 setPGOOptions(*Target);
668
669 // If we don't have a module then just exit now. We do this down
670 // here since the CPU/Feature help is underneath the target machine
671 // creation.
672 return 0;
673 }
674
675 assert(M && "Should have exited if we didn't have a module!");
676 if (codegen::getFloatABIForCalls() != FloatABI::Default)
677 Target->Options.FloatABIType = codegen::getFloatABIForCalls();
678
679 // Figure out where we are going to send the output.
680 std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OS: TheTriple.getOS());
681 if (!Out)
682 return 1;
683
684 // Ensure the filename is passed down to CodeViewDebug.
685 Target->Options.ObjectFilenameForDebug = Out->outputFilename();
686
687 // Return a copy of the output filename via the output param
688 OutputFilename = Out->outputFilename();
689
690 // Tell target that this tool is not necessarily used with argument ABI
691 // compliance (i.e. narrow integer argument extensions).
692 Target->Options.VerifyArgABICompliance = 0;
693
694 std::unique_ptr<ToolOutputFile> DwoOut;
695 if (!SplitDwarfOutputFile.empty()) {
696 std::error_code EC;
697 DwoOut = std::make_unique<ToolOutputFile>(args&: SplitDwarfOutputFile, args&: EC,
698 args: sys::fs::OF_None);
699 if (EC)
700 reportError(Msg: EC.message(), Filename: SplitDwarfOutputFile);
701 }
702
703 // Add an appropriate TargetLibraryInfo pass for the module's triple.
704 TargetLibraryInfoImpl TLII(M->getTargetTriple(), Target->Options.VecLib);
705
706 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
707 if (DisableSimplifyLibCalls)
708 TLII.disableAllFunctions();
709
710 // Verify module immediately to catch problems before doInitialization() is
711 // called on any passes.
712 if (!NoVerify && verifyModule(M: *M, OS: &errs()))
713 reportError(Msg: "input module cannot be verified", Filename: InputFilename);
714
715 // Override function attributes based on CPUStr, FeaturesStr, and command line
716 // flags.
717 codegen::setFunctionAttributes(CPU: CPUStr, Features: FeaturesStr, M&: *M);
718
719 for (auto &Plugin : PluginList) {
720 CodeGenFileType CGFT = codegen::getFileType();
721 if (Plugin.invokePreCodeGenCallback(M&: *M, TM&: *Target, CGFT, OS&: Out->os())) {
722 // TODO: Deduplicate code with below and the NewPMDriver.
723 if (Context.getDiagHandlerPtr()->HasErrors)
724 exit(status: 1);
725 Out->keep();
726 return 0;
727 }
728 }
729
730 if (mc::getExplicitRelaxAll() &&
731 codegen::getFileType() != CodeGenFileType::ObjectFile)
732 WithColor::warning(OS&: errs(), Prefix: argv[0])
733 << ": warning: ignoring -mc-relax-all because filetype != obj";
734
735 VerifierKind VK = VerifierKind::InputOutput;
736 if (NoVerify)
737 VK = VerifierKind::None;
738 else if (VerifyEach)
739 VK = VerifierKind::EachPass;
740
741 if (EnableNewPassManager || !PassPipeline.empty()) {
742 return compileModuleWithNewPM(Arg0: argv[0], M: std::move(M), MIR: std::move(MIR),
743 Target: std::move(Target), Out: std::move(Out),
744 DwoOut: std::move(DwoOut), Context, TLII, VK,
745 PassPipeline, FileType: codegen::getFileType());
746 }
747
748 // Build up all of the passes that we want to do to the module.
749 legacy::PassManager PM;
750 PM.add(P: new TargetLibraryInfoWrapperPass(TLII));
751 PM.add(P: new RuntimeLibraryInfoWrapper(
752 TheTriple, Target->Options.ExceptionModel, Target->Options.FloatABIType,
753 Target->Options.EABIVersion, Options.MCOptions.ABIName,
754 Target->Options.VecLib));
755
756 {
757 raw_pwrite_stream *OS = &Out->os();
758
759 // Manually do the buffering rather than using buffer_ostream,
760 // so we can memcmp the contents in CompileTwice mode
761 SmallVector<char, 0> Buffer;
762 std::unique_ptr<raw_svector_ostream> BOS;
763 if ((codegen::getFileType() != CodeGenFileType::AssemblyFile &&
764 !Out->os().supportsSeeking()) ||
765 CompileTwice) {
766 BOS = std::make_unique<raw_svector_ostream>(args&: Buffer);
767 OS = BOS.get();
768 }
769
770 const char *argv0 = argv[0];
771 MachineModuleInfoWrapperPass *MMIWP =
772 new MachineModuleInfoWrapperPass(Target.get());
773
774 // Set a temporary diagnostic handler. This is used before
775 // MachineModuleInfoWrapperPass::doInitialization for features like -M.
776 bool HasMCErrors = false;
777 MCContext &MCCtx = MMIWP->getMMI().getContext();
778 MCCtx.setDiagnosticHandler([&](const SMDiagnostic &SMD, bool IsInlineAsm,
779 const SourceMgr &SrcMgr,
780 std::vector<const MDNode *> &LocInfos) {
781 WithColor::error(OS&: errs(), Prefix: argv0) << SMD.getMessage() << '\n';
782 HasMCErrors = true;
783 });
784
785 // Construct a custom pass pipeline that starts after instruction
786 // selection.
787 if (!getRunPassNames().empty()) {
788 if (!MIR) {
789 WithColor::error(OS&: errs(), Prefix: argv[0])
790 << "run-pass is for .mir file only.\n";
791 delete MMIWP;
792 return 1;
793 }
794 TargetPassConfig *PTPC = Target->createPassConfig(PM);
795 TargetPassConfig &TPC = *PTPC;
796 if (TPC.hasLimitedCodeGenPipeline()) {
797 WithColor::error(OS&: errs(), Prefix: argv[0])
798 << "run-pass cannot be used with "
799 << TPC.getLimitedCodeGenPipelineReason() << ".\n";
800 delete PTPC;
801 delete MMIWP;
802 return 1;
803 }
804
805 TPC.setDisableVerify(NoVerify);
806 PM.add(P: &TPC);
807 PM.add(P: MMIWP);
808 TPC.printAndVerify(Banner: "");
809 for (const std::string &RunPassName : getRunPassNames()) {
810 if (addPass(PM, argv0, PassName: RunPassName, TPC))
811 return 1;
812 }
813 TPC.setInitialized();
814 PM.add(P: createPrintMIRPass(OS&: *OS));
815
816 // Add MIR2Vec vocabulary printer if requested
817 if (PrintMIR2VecVocab) {
818 PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs()));
819 }
820
821 // Add MIR2Vec printer if requested
822 if (PrintMIR2Vec) {
823 PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs()));
824 }
825
826 PM.add(P: createFreeMachineFunctionPass());
827 } else {
828 if (Target->addPassesToEmitFile(PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
829 codegen::getFileType(), NoVerify,
830 MMIWP)) {
831 if (!HasMCErrors)
832 reportError(Msg: "target does not support generation of this file type");
833 }
834
835 // Add MIR2Vec vocabulary printer if requested
836 if (PrintMIR2VecVocab) {
837 PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs()));
838 }
839
840 // Add MIR2Vec printer if requested
841 if (PrintMIR2Vec) {
842 PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs()));
843 }
844 }
845
846 Target->getObjFileLowering()->Initialize(ctx&: MMIWP->getMMI().getContext(),
847 TM: *Target);
848 if (MIR) {
849 assert(MMIWP && "Forgot to create MMIWP?");
850 if (MIR->parseMachineFunctions(M&: *M, MMI&: MMIWP->getMMI()))
851 return 1;
852 }
853
854 // Before executing passes, print the final values of the LLVM options.
855 cl::PrintOptionValues();
856
857 // If requested, run the pass manager over the same module again,
858 // to catch any bugs due to persistent state in the passes. Note that
859 // opt has the same functionality, so it may be worth abstracting this out
860 // in the future.
861 SmallVector<char, 0> CompileTwiceBuffer;
862 if (CompileTwice) {
863 std::unique_ptr<Module> M2(llvm::CloneModule(M: *M));
864 PM.run(M&: *M2);
865 CompileTwiceBuffer = Buffer;
866 Buffer.clear();
867 }
868
869 PM.run(M&: *M);
870
871 if (Context.getDiagHandlerPtr()->HasErrors || HasMCErrors)
872 return 1;
873
874 // Compare the two outputs and make sure they're the same
875 if (CompileTwice) {
876 if (Buffer.size() != CompileTwiceBuffer.size() ||
877 (memcmp(s1: Buffer.data(), s2: CompileTwiceBuffer.data(), n: Buffer.size()) !=
878 0)) {
879 errs()
880 << "Running the pass manager twice changed the output.\n"
881 "Writing the result of the second run to the specified output\n"
882 "To generate the one-run comparison binary, just run without\n"
883 "the compile-twice option\n";
884 Out->os() << Buffer;
885 Out->keep();
886 return 1;
887 }
888 }
889
890 if (BOS) {
891 Out->os() << Buffer;
892 }
893 }
894
895 // Declare success.
896 Out->keep();
897 if (DwoOut)
898 DwoOut->keep();
899
900 return 0;
901}
902