1//===-- llcdriver.cpp - Implement the LLVM Native Code Generator ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is the llc code generator driver. It provides a convenient
10// command-line interface for generating an assembly file or a relocatable file,
11// given LLVM bitcode.
12//
13//===----------------------------------------------------------------------===//
14
15#include "NewPMDriver.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/ScopeExit.h"
18#include "llvm/ADT/SmallVectorExtras.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/Analysis/RuntimeLibcallInfo.h"
21#include "llvm/Analysis/TargetLibraryInfo.h"
22#include "llvm/CodeGen/CommandFlags.h"
23#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
24#include "llvm/CodeGen/LinkAllCodegenComponents.h"
25#include "llvm/CodeGen/MIRParser/MIRParser.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineModuleInfo.h"
28#include "llvm/CodeGen/TargetPassConfig.h"
29#include "llvm/CodeGen/TargetSubtargetInfo.h"
30#include "llvm/IR/AutoUpgrade.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DiagnosticInfo.h"
33#include "llvm/IR/DiagnosticPrinter.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/LLVMRemarkStreamer.h"
36#include "llvm/IR/LegacyPassManager.h"
37#include "llvm/IR/Module.h"
38#include "llvm/IR/Verifier.h"
39#include "llvm/IRReader/IRReader.h"
40#include "llvm/InitializePasses.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/PGOOptions.h"
49#include "llvm/Support/SourceMgr.h"
50#include "llvm/Support/TargetSelect.h"
51#include "llvm/Support/TimeProfiler.h"
52#include "llvm/Support/ToolOutputFile.h"
53#include "llvm/Support/VirtualFileSystem.h"
54#include "llvm/Support/WithColor.h"
55#include "llvm/Target/TargetMachine.h"
56#include "llvm/TargetParser/Host.h"
57#include "llvm/TargetParser/SubtargetFeature.h"
58#include "llvm/TargetParser/Triple.h"
59#include "llvm/Transforms/Utils/Cloning.h"
60#include <cassert>
61#include <memory>
62#include <optional>
63using namespace llvm;
64
65// General options for llc. Other pass-specific options are specified
66// within the corresponding llc passes, and target-specific options
67// and back-end code generation options are specified with the target machine.
68//
69static cl::opt<std::string>
70 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init(Val: "-"));
71
72static cl::list<std::string>
73 InstPrinterOptions("M", cl::desc("InstPrinter options"));
74
75static cl::opt<std::string>
76 InputLanguage("x", cl::desc("Input language ('ir' or 'mir')"));
77
78static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
79 cl::value_desc("filename"));
80
81static cl::opt<std::string>
82 SplitDwarfOutputFile("split-dwarf-output", cl::desc(".dwo output filename"),
83 cl::value_desc("filename"));
84
85static cl::opt<unsigned>
86 TimeCompilations("time-compilations", cl::Hidden, cl::init(Val: 1u),
87 cl::value_desc("N"),
88 cl::desc("Repeat compilation N times for timing"));
89
90static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
91
92static cl::opt<unsigned> TimeTraceGranularity(
93 "time-trace-granularity",
94 cl::desc(
95 "Minimum time granularity (in microseconds) traced by time profiler"),
96 cl::init(Val: 500), cl::Hidden);
97
98static cl::opt<std::string>
99 TimeTraceFile("time-trace-file",
100 cl::desc("Specify time trace file destination"),
101 cl::value_desc("filename"));
102
103static cl::opt<std::string>
104 BinutilsVersion("binutils-version", cl::Hidden,
105 cl::desc("Produced object files can use all ELF features "
106 "supported by this binutils version and newer."
107 "If -no-integrated-as is specified, the generated "
108 "assembly will consider GNU as support."
109 "'none' means that all ELF features can be used, "
110 "regardless of binutils support"));
111
112static cl::opt<bool>
113 PreserveComments("preserve-as-comments", cl::Hidden,
114 cl::desc("Preserve Comments in outputted assembly"),
115 cl::init(Val: true));
116
117// Determine optimization level.
118static cl::opt<char>
119 OptLevel("O",
120 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
121 "(default = '-O2')"),
122 cl::Prefix, cl::init(Val: '2'));
123
124static cl::opt<std::string>
125 TargetTriple("mtriple", cl::desc("Override target triple for module"));
126
127static cl::opt<std::string> SplitDwarfFile(
128 "split-dwarf-file",
129 cl::desc(
130 "Specify the name of the .dwo file to encode in the DWARF output"));
131
132static cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
133 cl::desc("Do not verify input module"));
134
135static cl::opt<bool> VerifyEach("verify-each",
136 cl::desc("Verify after each transform"));
137
138static cl::opt<bool>
139 DisableSimplifyLibCalls("disable-simplify-libcalls",
140 cl::desc("Disable simplify-libcalls"));
141
142static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
143 cl::desc("Show encoding in .s output"));
144
145static cl::opt<unsigned>
146 OutputAsmVariant("output-asm-variant",
147 cl::desc("Syntax variant to use for output printing"));
148
149static cl::opt<bool>
150 DwarfDirectory("dwarf-directory", cl::Hidden,
151 cl::desc("Use .file directives with an explicit directory"),
152 cl::init(Val: true));
153
154static cl::opt<bool> AsmVerbose("asm-verbose",
155 cl::desc("Add comments to directives."),
156 cl::init(Val: true));
157
158static cl::opt<bool>
159 CompileTwice("compile-twice", cl::Hidden,
160 cl::desc("Run everything twice, re-using the same pass "
161 "manager and verify the result is the same."),
162 cl::init(Val: false));
163
164static cl::opt<bool> DiscardValueNames(
165 "discard-value-names",
166 cl::desc("Discard names from Value (other than GlobalValue)."),
167 cl::init(Val: false), cl::Hidden);
168
169static cl::opt<bool>
170 PrintMIR2VecVocab("print-mir2vec-vocab", cl::Hidden,
171 cl::desc("Print MIR2Vec vocabulary contents"),
172 cl::init(Val: false));
173
174static cl::opt<bool>
175 PrintMIR2Vec("print-mir2vec", cl::Hidden,
176 cl::desc("Print MIR2Vec embeddings for functions"),
177 cl::init(Val: false));
178
179static cl::list<std::string> IncludeDirs("I", cl::desc("include search path"));
180
181static cl::opt<bool> RemarksWithHotness(
182 "pass-remarks-with-hotness",
183 cl::desc("With PGO, include profile count in optimization remarks"),
184 cl::Hidden);
185
186static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
187 RemarksHotnessThreshold(
188 "pass-remarks-hotness-threshold",
189 cl::desc("Minimum profile count required for "
190 "an optimization remark to be output. "
191 "Use 'auto' to apply the threshold from profile summary."),
192 cl::value_desc("N or 'auto'"), cl::init(Val: 0), cl::Hidden);
193
194static cl::opt<std::string>
195 RemarksFilename("pass-remarks-output",
196 cl::desc("Output filename for pass remarks"),
197 cl::value_desc("filename"));
198
199static cl::opt<std::string>
200 RemarksPasses("pass-remarks-filter",
201 cl::desc("Only record optimization remarks from passes whose "
202 "names match the given regular expression"),
203 cl::value_desc("regex"));
204
205static cl::opt<std::string> RemarksFormat(
206 "pass-remarks-format",
207 cl::desc("The format used for serializing remarks (default: YAML)"),
208 cl::value_desc("format"), cl::init(Val: "yaml"));
209
210static cl::list<std::string> PassPlugins("load-pass-plugin",
211 cl::desc("Load plugin library"));
212
213static cl::list<std::string>
214 PluginArgs("plugin-arg",
215 cl::desc("Pass <arg> to the pass plugin named <plugin>"),
216 cl::value_desc("plugin>,<arg"));
217
218static cl::opt<bool> EnableNewPassManager(
219 "enable-new-pm", cl::desc("Enable the new pass manager"), cl::init(Val: false));
220
221// This flag specifies a textual description of the optimization pass pipeline
222// to run over the module. This flag switches opt to use the new pass manager
223// infrastructure, completely disabling all of the flags specific to the old
224// pass management.
225static cl::opt<std::string> PassPipeline(
226 "passes",
227 cl::desc(
228 "A textual description of the pass pipeline. To have analysis passes "
229 "available before a certain pass, add 'require<foo-analysis>'."));
230static cl::alias PassPipeline2("p", cl::aliasopt(PassPipeline),
231 cl::desc("Alias for -passes"));
232
233static std::vector<std::string> &getRunPassNames() {
234 static std::vector<std::string> RunPassNames;
235 return RunPassNames;
236}
237
238namespace {
239struct RunPassOption {
240 void operator=(const std::string &Val) const {
241 if (Val.empty())
242 return;
243 SmallVector<StringRef, 8> PassNames;
244 StringRef(Val).split(A&: PassNames, Separator: ',', MaxSplit: -1, KeepEmpty: false);
245 for (auto PassName : PassNames)
246 getRunPassNames().push_back(x: std::string(PassName));
247 }
248};
249} // namespace
250
251static RunPassOption RunPassOpt;
252
253static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass(
254 "run-pass",
255 cl::desc("Run compiler only for specified passes (comma separated list)"),
256 cl::value_desc("pass-name"), cl::location(L&: RunPassOpt));
257
258// PGO command line options
259enum PGOKind {
260 NoPGO,
261 SampleUse,
262};
263
264static cl::opt<PGOKind>
265 PGOKindFlag("pgo-kind", cl::init(Val: NoPGO), cl::Hidden,
266 cl::desc("The kind of profile guided optimization"),
267 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
268 clEnumValN(SampleUse, "pgo-sample-use-pipeline",
269 "Use sampled profile to guide PGO.")));
270
271// Function to set PGO options on TargetMachine based on command line flags.
272static void setPGOOptions(TargetMachine &TM) {
273 std::optional<PGOOptions> PGOOpt;
274
275 switch (PGOKindFlag) {
276 case SampleUse:
277 // Use default values for other PGOOptions parameters. This parameter
278 // is used to test that PGO data is preserved at -O0.
279 PGOOpt = PGOOptions("", "", "", "", PGOOptions::SampleUse,
280 PGOOptions::NoCSAction);
281 break;
282 case NoPGO:
283 PGOOpt = std::nullopt;
284 break;
285 }
286
287 if (PGOOpt)
288 TM.setPGOOption(PGOOpt);
289}
290
291static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &,
292 LLVMContext &Context, std::string &OutputFilename);
293
294[[noreturn]] static void reportError(Twine Msg, StringRef Filename = "") {
295 SmallString<256> Prefix;
296 if (!Filename.empty()) {
297 if (Filename == "-")
298 Filename = "<stdin>";
299 ("'" + Twine(Filename) + "': ").toStringRef(Out&: Prefix);
300 }
301 WithColor::error(OS&: errs(), Prefix: "llc") << Prefix << Msg << "\n";
302 exit(status: 1);
303}
304
305[[noreturn]] static void reportError(Error Err, StringRef Filename) {
306 assert(Err);
307 handleAllErrors(E: createFileError(F: Filename, E: std::move(Err)),
308 Handlers: [&](const ErrorInfoBase &EI) { reportError(Msg: EI.message()); });
309 llvm_unreachable("reportError() should not return");
310}
311
312static std::unique_ptr<ToolOutputFile>
313GetOutputStream(const cl::LibraryOptionsContext &LibraryOptions,
314 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(C: LibraryOptions)) {
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(C: LibraryOptions)) {
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// Entry point for the llc compiler.
369//
370extern "C" int llcMain(int argc, char **argv) {
371 // Enable debug stream buffering.
372 EnableDebugBuffering = true;
373
374 // Initialize targets first, so that --version shows registered targets.
375 InitializeAllTargets();
376 InitializeAllTargetMCs();
377 InitializeAllAsmPrinters();
378 InitializeAllAsmParsers();
379
380 // Initialize codegen and IR passes used by llc so that the -print-after,
381 // -print-before, and -stop-after options work.
382 PassRegistry *Registry = PassRegistry::getPassRegistry();
383 initializeCore(*Registry);
384 initializeCodeGen(*Registry);
385 initializeScalarOpts(*Registry);
386 initializeIPO(*Registry);
387 initializeVectorization(*Registry);
388 initializeTransformUtils(*Registry);
389
390 // Initialize debugging passes.
391 initializeScavengerTestPass(*Registry);
392
393 // Register the Target and CPU printer for --version.
394 cl::AddExtraVersionPrinter(func: sys::printDefaultTargetAndDetectedCPU);
395 // Register the target printer for --version.
396 cl::AddExtraVersionPrinter(func: TargetRegistry::printRegisteredTargetsForVersion);
397
398 LLVMContext Context;
399 cl::parseCommandLineOptions(Context&: Context.getLibraryOptions(),
400 ToolOptions: codegen::getToolOptions(), Args: ArrayRef(argv, argc),
401 Overview: "llvm system compiler\n");
402
403 SmallVector<PassPlugin, 1> PluginList;
404 for (const std::string &Path : PassPlugins) {
405 auto Plugin = PassPlugin::load(Filename: Path);
406 if (!Plugin)
407 reportFatalUsageError(Err: Plugin.takeError());
408 PluginList.emplace_back(Args&: Plugin.get());
409 }
410 if (Error E = passPluginArguments(
411 Infos: map_to_vector(C&: PluginList,
412 F: [](const PassPlugin &P) { return P.getInfo(); }),
413 Args: PluginArgs))
414 reportFatalUsageError(Err: std::move(E));
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 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(C: Context.getLibraryOptions());
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(C: Context.getLibraryOptions(),
466 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(C: Context.getLibraryOptions());
506 std::string TuneCPUStr = codegen::getTuneCPUStr(C: Context.getLibraryOptions());
507 std::string FeaturesStr =
508 codegen::getFeaturesStr(C: Context.getLibraryOptions());
509
510 // Set attributes on functions as loaded from MIR from command line arguments.
511 auto setMIRFunctionAttributes = [&CPUStr, &TuneCPUStr,
512 &FeaturesStr](Function &F) {
513 codegen::setFunctionAttributes(F, CPU: CPUStr, Features: FeaturesStr, TuneCPU: TuneCPUStr);
514 };
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::getTargetOptions(LibraryOptions: Context.getLibraryOptions(), 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 Options.MCOptions.BinutilsVersion =
560 MCTargetOptions::parseBinutilsVersion(Version: BinutilsVersion);
561 Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
562 Options.MCOptions.AsmVerbose = AsmVerbose;
563 Options.MCOptions.PreserveAsmComments = PreserveComments;
564 if (OutputAsmVariant.getNumOccurrences())
565 Options.MCOptions.OutputAsmVariant = OutputAsmVariant;
566 Options.MCOptions.IASSearchPaths = IncludeDirs;
567 Options.MCOptions.InstPrinterOptions = InstPrinterOptions;
568 Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
569 if (DwarfDirectory.getPosition()) {
570 Options.MCOptions.MCUseDwarfDirectory =
571 DwarfDirectory ? MCTargetOptions::EnableDwarfDirectory
572 : MCTargetOptions::DisableDwarfDirectory;
573 } else {
574 // -dwarf-directory is not set explicitly. Some assemblers
575 // (e.g. GNU as or ptxas) do not support `.file directory'
576 // syntax prior to DWARFv5. Let the target decide the default
577 // value.
578 Options.MCOptions.MCUseDwarfDirectory =
579 MCTargetOptions::DefaultDwarfDirectory;
580 }
581 };
582
583 std::optional<Reloc::Model> RM =
584 codegen::getExplicitRelocModel(C: Context.getLibraryOptions());
585 std::optional<CodeModel::Model> CM =
586 codegen::getExplicitCodeModel(C: Context.getLibraryOptions());
587
588 const Target *TheTarget = nullptr;
589 std::unique_ptr<TargetMachine> Target;
590
591 // If user just wants to list available options, skip module loading
592 auto MAttrs = codegen::getMAttrs(C: Context.getLibraryOptions());
593 bool SkipModule =
594 CPUStr == "help" || TuneCPUStr == "help" || is_contained(Range&: MAttrs, Element: "help");
595 if (SkipModule) {
596 if (!TargetTriple.empty())
597 TheTriple = Triple(Triple::normalize(Str: TargetTriple));
598 else
599 TheTriple = Triple(sys::getDefaultTargetTriple());
600
601 // Get the target specific parser.
602 std::string Error;
603 TheTarget = TargetRegistry::lookupTarget(
604 ArchName: codegen::getMArch(C: Context.getLibraryOptions()), TheTriple, Error);
605 if (!TheTarget) {
606 WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n";
607 return 1;
608 }
609
610 InitializeOptions(TheTriple);
611 // Pass "help" as CPU for -mtune=help
612 std::string SkipModuleCPU = (TuneCPUStr == "help" ? "help" : CPUStr);
613 // Create the target machine just to print the help info. Use unique_ptr
614 // to avoid a memory leak.
615 Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
616 TT: TheTriple, CPU: SkipModuleCPU, Features: FeaturesStr, Options, RM, CM, OL: OLvl));
617 if (!Target) {
618 WithColor::error(OS&: errs(), Prefix: argv[0])
619 << "could not allocate target machine\n";
620 return 1;
621 }
622
623 // If we don't have a module then just exit now. We do this down
624 // here since the CPU/Feature help is underneath the target machine
625 // creation.
626 return 0;
627 }
628
629 auto SetDataLayout = [&](StringRef DataLayoutTargetTriple,
630 StringRef OldDLStr) -> std::optional<std::string> {
631 // If we are supposed to override the target triple, do so now.
632 std::string IRTargetTriple = DataLayoutTargetTriple.str();
633 if (!TargetTriple.empty())
634 IRTargetTriple = Triple::normalize(Str: TargetTriple);
635 TheTriple = Triple(IRTargetTriple);
636 if (TheTriple.getTriple().empty())
637 TheTriple.setTriple(sys::getDefaultTargetTriple());
638
639 std::string Error;
640 TheTarget = TargetRegistry::lookupTarget(
641 ArchName: codegen::getMArch(C: Context.getLibraryOptions()), TheTriple, Error);
642 if (!TheTarget) {
643 WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n";
644 exit(status: 1);
645 }
646
647 InitializeOptions(TheTriple);
648 Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
649 TT: TheTriple, CPU: CPUStr, Features: FeaturesStr, Options, RM, CM, OL: OLvl));
650 if (!Target) {
651 WithColor::error(OS&: errs(), Prefix: argv[0])
652 << "could not allocate target machine\n";
653 exit(status: 1);
654 }
655
656 // Set PGO options based on command line flags
657 setPGOOptions(*Target);
658
659 return TheTriple.computeDataLayout(ABIName: Options.MCOptions.getABIName());
660 };
661 if (InputLanguage == "mir" ||
662 (InputLanguage == "" && StringRef(InputFilename).ends_with(Suffix: ".mir"))) {
663 MIR = createMIRParserFromFile(Filename: InputFilename, Error&: Err, Context,
664 ProcessIRFunction: setMIRFunctionAttributes);
665 if (MIR)
666 M = MIR->parseIRModule(DataLayoutCallback: SetDataLayout);
667 } else {
668 M = parseIRFile(Filename: InputFilename, Err, Context,
669 Callbacks: ParserCallbacks(SetDataLayout));
670 }
671 if (!M) {
672 Err.print(ProgName: argv[0], S&: WithColor::error(OS&: errs(), Prefix: argv[0]));
673 return 1;
674 }
675
676 M->setTargetTriple(TheTriple);
677
678 std::optional<CodeModel::Model> CM_IR = M->getCodeModel();
679 if (!CM && CM_IR)
680 Target->setCodeModel(*CM_IR);
681 if (std::optional<uint64_t> LDT =
682 codegen::getExplicitLargeDataThreshold(C: Context.getLibraryOptions()))
683 Target->setLargeDataThreshold(*LDT);
684
685 // Figure out where we are going to send the output.
686 std::unique_ptr<ToolOutputFile> Out =
687 GetOutputStream(LibraryOptions: Context.getLibraryOptions(), 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 std::unique_ptr<ToolOutputFile> DwoOut;
698 if (!SplitDwarfOutputFile.empty()) {
699 std::error_code EC;
700 DwoOut = std::make_unique<ToolOutputFile>(args&: SplitDwarfOutputFile, args&: EC,
701 args: sys::fs::OF_None);
702 if (EC)
703 reportError(Msg: EC.message(), Filename: SplitDwarfOutputFile);
704 }
705
706 // Add an appropriate TargetLibraryInfo pass for the module's triple.
707 TargetLibraryInfoImpl TLII(M->getTargetTriple(), Target->Options.VecLib);
708
709 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
710 if (DisableSimplifyLibCalls)
711 TLII.disableAllFunctions();
712
713 // Verify module immediately to catch problems before doInitialization() is
714 // called on any passes.
715 if (!NoVerify && verifyModule(M: *M, OS: &errs()))
716 reportError(Msg: "input module cannot be verified", Filename: InputFilename);
717
718 // Override function attributes based on CPUStr, TuneCPUStr, FeaturesStr, and
719 // command line flags.
720 codegen::setFunctionAttributes(M&: *M, CPU: CPUStr, Features: FeaturesStr, TuneCPU: TuneCPUStr);
721
722 for (auto &Plugin : PluginList) {
723 CodeGenFileType CGFT = codegen::getFileType(C: Context.getLibraryOptions());
724 if (Plugin.invokePreCodeGenCallback(
725 TM&: *Target, M&: *M, OS&: Out->os(), DwoOS: DwoOut ? &DwoOut->os() : nullptr, CGFT,
726 DisableVerify: NoVerify, DisableSimplifyLibCalls, /*MSI=*/nullptr,
727 VFS: vfs::getRealFileSystem())) {
728 // TODO: Deduplicate code with below and the NewPMDriver.
729 if (Context.getDiagHandlerPtr()->HasErrors)
730 exit(status: 1);
731 Out->keep();
732 if (DwoOut)
733 DwoOut->keep();
734 return 0;
735 }
736 }
737
738 if (Options.MCOptions.MCRelaxAll &&
739 codegen::getFileType(C: Context.getLibraryOptions()) !=
740 CodeGenFileType::ObjectFile)
741 WithColor::warning(OS&: errs(), Prefix: argv[0])
742 << ": warning: ignoring -mc-relax-all because filetype != obj";
743
744 VerifierKind VK = VerifierKind::InputOutput;
745 if (NoVerify)
746 VK = VerifierKind::None;
747 else if (VerifyEach)
748 VK = VerifierKind::EachPass;
749
750 // Use the NewPM if the user specifies -passes (NewPM specific), specifically
751 // requests the NewPM with -enable-new-pm, or the target defaults to the
752 // NewPM, the user has not explicitly disabled the NewPM with
753 // -enable-new-pm=false, and the user has not specified -run-pass.
754 if (!PassPipeline.empty() ||
755 (EnableNewPassManager.getNumOccurrences() > 0 && EnableNewPassManager) ||
756 (Target->shouldDefaultToNewPM() &&
757 !(EnableNewPassManager.getNumOccurrences() && !EnableNewPassManager) &&
758 getRunPassNames().empty())) {
759 return compileModuleWithNewPM(
760 Arg0: argv[0], M: std::move(M), MIR: std::move(MIR), Target: std::move(Target),
761 Out: std::move(Out), DwoOut: std::move(DwoOut), Context, TLII, VK, PassPipeline,
762 PassPlugins: PluginList, FileType: codegen::getFileType(C: Context.getLibraryOptions()));
763 }
764
765 // Build up all of the passes that we want to do to the module.
766 legacy::PassManager PM;
767 PM.add(P: new TargetLibraryInfoWrapperPass(TLII));
768 PM.add(P: new RuntimeLibraryInfoWrapper(Options.MCOptions.ABIName,
769 Target->Options.VecLib));
770
771 {
772 raw_pwrite_stream *OS = &Out->os();
773
774 // Manually do the buffering rather than using buffer_ostream,
775 // so we can memcmp the contents in CompileTwice mode
776 SmallVector<char, 0> Buffer;
777 std::unique_ptr<raw_svector_ostream> BOS;
778 if ((codegen::getFileType(C: Context.getLibraryOptions()) !=
779 CodeGenFileType::AssemblyFile &&
780 !Out->os().supportsSeeking()) ||
781 CompileTwice) {
782 BOS = std::make_unique<raw_svector_ostream>(args&: Buffer);
783 OS = BOS.get();
784 }
785
786 const char *argv0 = argv[0];
787 MachineModuleInfoWrapperPass *MMIWP =
788 new MachineModuleInfoWrapperPass(Target.get());
789
790 // Set a temporary diagnostic handler. This is used before
791 // MachineModuleInfoWrapperPass::doInitialization for features like -M.
792 bool HasMCErrors = false;
793 MCContext &MCCtx = MMIWP->getMMI().getContext();
794 MCCtx.setDiagnosticHandler([&](const SMDiagnostic &SMD, bool IsInlineAsm,
795 const SourceMgr &SrcMgr,
796 std::vector<const MDNode *> &LocInfos) {
797 WithColor::error(OS&: errs(), Prefix: argv0) << SMD.getMessage() << '\n';
798 HasMCErrors = true;
799 });
800
801 // Construct a custom pass pipeline that starts after instruction
802 // selection.
803 if (!getRunPassNames().empty()) {
804 if (!MIR) {
805 WithColor::error(OS&: errs(), Prefix: argv[0])
806 << "run-pass is for .mir file only.\n";
807 delete MMIWP;
808 return 1;
809 }
810 TargetPassConfig *PTPC = Target->createPassConfig(PM);
811 TargetPassConfig &TPC = *PTPC;
812 if (TPC.hasLimitedCodeGenPipeline()) {
813 WithColor::error(OS&: errs(), Prefix: argv[0])
814 << "run-pass cannot be used with "
815 << TPC.getLimitedCodeGenPipelineReason() << ".\n";
816 delete PTPC;
817 delete MMIWP;
818 return 1;
819 }
820
821 TPC.setDisableVerify(NoVerify);
822 PM.add(P: &TPC);
823 PM.add(P: MMIWP);
824 TPC.printAndVerify(Banner: "");
825 for (const std::string &RunPassName : getRunPassNames()) {
826 if (addPass(PM, argv0, PassName: RunPassName, TPC))
827 return 1;
828 }
829 TPC.setInitialized();
830 if (TPC.getMachineVerifierMode() == MachineVerifierMode::End)
831 PM.add(P: createMachineVerifierPass(Banner: "At end of pipeline"));
832 PM.add(P: createPrintMIRPass(OS&: *OS));
833
834 // Add MIR2Vec vocabulary printer if requested
835 if (PrintMIR2VecVocab) {
836 PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs()));
837 }
838
839 // Add MIR2Vec printer if requested
840 if (PrintMIR2Vec) {
841 PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs()));
842 }
843
844 PM.add(P: createFreeMachineFunctionPass());
845 } else {
846 if (Target->addPassesToEmitFile(
847 PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
848 codegen::getFileType(C: Context.getLibraryOptions()), NoVerify,
849 MMIWP)) {
850 if (!HasMCErrors)
851 reportError(Msg: "target does not support generation of this file type");
852 }
853
854 // Add MIR2Vec vocabulary printer if requested
855 if (PrintMIR2VecVocab) {
856 PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs()));
857 }
858
859 // Add MIR2Vec printer if requested
860 if (PrintMIR2Vec) {
861 PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs()));
862 }
863 }
864
865 if (MIR) {
866 assert(MMIWP && "Forgot to create MMIWP?");
867 if (MIR->parseMachineFunctions(M&: *M, MMI&: MMIWP->getMMI()))
868 return 1;
869 }
870
871 // Before executing passes, print the final values of the LLVM options.
872 cl::PrintOptionValues();
873
874 // If requested, run the pass manager over the same module again,
875 // to catch any bugs due to persistent state in the passes. Note that
876 // opt has the same functionality, so it may be worth abstracting this out
877 // in the future.
878 SmallVector<char, 0> CompileTwiceBuffer;
879 if (CompileTwice) {
880 std::unique_ptr<Module> M2(llvm::CloneModule(M: *M));
881 PM.run(M&: *M2);
882 CompileTwiceBuffer = Buffer;
883 Buffer.clear();
884 }
885
886 PM.run(M&: *M);
887
888 if (Context.getDiagHandlerPtr()->HasErrors || HasMCErrors)
889 return 1;
890
891 // Compare the two outputs and make sure they're the same
892 if (CompileTwice) {
893 if (Buffer.size() != CompileTwiceBuffer.size() ||
894 (memcmp(s1: Buffer.data(), s2: CompileTwiceBuffer.data(), n: Buffer.size()) !=
895 0)) {
896 errs()
897 << "Running the pass manager twice changed the output.\n"
898 "Writing the result of the second run to the specified output\n"
899 "To generate the one-run comparison binary, just run without\n"
900 "the compile-twice option\n";
901 Out->os() << Buffer;
902 Out->keep();
903 return 1;
904 }
905 }
906
907 if (BOS) {
908 Out->os() << Buffer;
909 }
910 }
911
912 // Declare success.
913 Out->keep();
914 if (DwoOut)
915 DwoOut->keep();
916
917 return 0;
918}
919