1//===-- clang-nvlink-wrapper/ClangNVLinkWrapper.cpp - NVIDIA linker util --===//
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 tool wraps around the NVIDIA linker called 'nvlink'. The NVIDIA linker
10// is required to create NVPTX applications, but does not support common
11// features like LTO or archives. This utility wraps around the tool to cover
12// its deficiencies. This tool can be removed once NVIDIA improves their linker
13// or ports it to `ld.lld`.
14//
15//===---------------------------------------------------------------------===//
16
17#include "clang/Basic/Version.h"
18
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/BinaryFormat/Magic.h"
21#include "llvm/Bitcode/BitcodeWriter.h"
22#include "llvm/CodeGen/CommandFlags.h"
23#include "llvm/IR/DiagnosticPrinter.h"
24#include "llvm/LTO/LTO.h"
25#include "llvm/Object/Archive.h"
26#include "llvm/Object/ArchiveWriter.h"
27#include "llvm/Object/Binary.h"
28#include "llvm/Object/ELFObjectFile.h"
29#include "llvm/Object/IRObjectFile.h"
30#include "llvm/Object/ObjectFile.h"
31#include "llvm/Object/OffloadBinary.h"
32#include "llvm/Option/ArgList.h"
33#include "llvm/Option/OptTable.h"
34#include "llvm/Option/Option.h"
35#include "llvm/Remarks/HotnessThresholdParser.h"
36#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/FileOutputBuffer.h"
38#include "llvm/Support/FileSystem.h"
39#include "llvm/Support/InitLLVM.h"
40#include "llvm/Support/MemoryBuffer.h"
41#include "llvm/Support/Path.h"
42#include "llvm/Support/Program.h"
43#include "llvm/Support/Signals.h"
44#include "llvm/Support/StringSaver.h"
45#include "llvm/Support/TargetSelect.h"
46#include "llvm/Support/WithColor.h"
47
48using namespace llvm;
49using namespace llvm::opt;
50using namespace llvm::object;
51
52// Various tools (e.g., llc and opt) duplicate this series of declarations for
53// options related to passes and remarks.
54static cl::opt<bool> RemarksWithHotness(
55 "pass-remarks-with-hotness",
56 cl::desc("With PGO, include profile count in optimization remarks"),
57 cl::Hidden);
58
59static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
60 RemarksHotnessThreshold(
61 "pass-remarks-hotness-threshold",
62 cl::desc("Minimum profile count required for "
63 "an optimization remark to be output. "
64 "Use 'auto' to apply the threshold from profile summary."),
65 cl::value_desc("N or 'auto'"), cl::init(Val: 0), cl::Hidden);
66
67static cl::opt<std::string>
68 RemarksFilename("pass-remarks-output",
69 cl::desc("Output filename for pass remarks"),
70 cl::value_desc("filename"));
71
72static cl::opt<std::string>
73 RemarksPasses("pass-remarks-filter",
74 cl::desc("Only record optimization remarks from passes whose "
75 "names match the given regular expression"),
76 cl::value_desc("regex"));
77
78static cl::opt<std::string> RemarksFormat(
79 "pass-remarks-format",
80 cl::desc("The format used for serializing remarks (default: YAML)"),
81 cl::value_desc("format"), cl::init(Val: "yaml"));
82
83static cl::list<std::string>
84 PassPlugins("load-pass-plugin",
85 cl::desc("Load passes from plugin library"));
86
87static void printVersion(raw_ostream &OS) {
88 OS << clang::getClangToolFullVersion(ToolName: "clang-nvlink-wrapper") << '\n';
89}
90
91/// The value of `argv[0]` when run.
92static const char *Executable;
93
94/// Temporary files to be cleaned up.
95static SmallVector<SmallString<128>> TempFiles;
96
97/// Codegen flags for LTO backend.
98static codegen::RegisterCodeGenFlags CodeGenFlags;
99
100namespace {
101// Must not overlap with llvm::opt::DriverFlag.
102enum WrapperFlags { WrapperOnlyOption = (1 << 4) };
103
104enum ID {
105 OPT_INVALID = 0, // This is not an option ID.
106#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
107#include "NVLinkOpts.inc"
108 LastOption
109#undef OPTION
110};
111
112#define OPTTABLE_STR_TABLE_CODE
113#include "NVLinkOpts.inc"
114#undef OPTTABLE_STR_TABLE_CODE
115
116#define OPTTABLE_PREFIXES_TABLE_CODE
117#include "NVLinkOpts.inc"
118#undef OPTTABLE_PREFIXES_TABLE_CODE
119
120static constexpr OptTable::Info InfoTable[] = {
121#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
122#include "NVLinkOpts.inc"
123#undef OPTION
124};
125
126class WrapperOptTable : public opt::GenericOptTable {
127public:
128 WrapperOptTable()
129 : opt::GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable) {}
130};
131
132const OptTable &getOptTable() {
133 static const WrapperOptTable *Table = []() {
134 auto Result = std::make_unique<WrapperOptTable>();
135 return Result.release();
136 }();
137 return *Table;
138}
139
140[[noreturn]] void reportError(Error E) {
141 outs().flush();
142 logAllUnhandledErrors(E: std::move(E), OS&: WithColor::error(OS&: errs(), Prefix: Executable));
143 exit(EXIT_FAILURE);
144}
145
146void diagnosticHandler(const DiagnosticInfo &DI) {
147 std::string ErrStorage;
148 raw_string_ostream OS(ErrStorage);
149 DiagnosticPrinterRawOStream DP(OS);
150 DI.print(DP);
151
152 switch (DI.getSeverity()) {
153 case DS_Error:
154 WithColor::error(OS&: errs(), Prefix: Executable) << ErrStorage << "\n";
155 break;
156 case DS_Warning:
157 WithColor::warning(OS&: errs(), Prefix: Executable) << ErrStorage << "\n";
158 break;
159 case DS_Note:
160 WithColor::note(OS&: errs(), Prefix: Executable) << ErrStorage << "\n";
161 break;
162 case DS_Remark:
163 WithColor::remark(OS&: errs()) << ErrStorage << "\n";
164 break;
165 }
166}
167
168bool hasFatBinary(const ArgList &Args, MemoryBufferRef Buffer) {
169 if (Args.hasArg(Ids: OPT_dry_run) && Args.hasArg(Ids: OPT_assume_device_object))
170 return false;
171 if (identify_magic(magic: Buffer.getBuffer()) != file_magic::elf_relocatable)
172 return false;
173 Expected<std::unique_ptr<ObjectFile>> ObjFile =
174 ObjectFile::createObjectFile(Object: Buffer);
175 if (!ObjFile) // Assume fatbin if the object creation fails.
176 return !errorToBool(Err: ObjFile.takeError());
177 return (*ObjFile)->getArch() != Triple::nvptx &&
178 (*ObjFile)->getArch() != Triple::nvptx64;
179}
180
181Expected<StringRef> createTempFile(const ArgList &Args, const Twine &Prefix,
182 StringRef Extension) {
183 SmallString<128> OutputFile;
184 if (Args.hasArg(Ids: OPT_save_temps)) {
185 (Prefix + "." + Extension).toNullTerminatedStringRef(Out&: OutputFile);
186 } else {
187 if (std::error_code EC =
188 sys::fs::createTemporaryFile(Prefix, Suffix: Extension, ResultPath&: OutputFile))
189 return createFileError(F: OutputFile, EC);
190 }
191
192 TempFiles.emplace_back(Args: std::move(OutputFile));
193 return TempFiles.back();
194}
195
196Expected<std::string> findProgram(const ArgList &Args, StringRef Name,
197 ArrayRef<StringRef> Paths) {
198 if (Args.hasArg(Ids: OPT_dry_run))
199 return Name.str();
200 ErrorOr<std::string> Path = sys::findProgramByName(Name, Paths);
201 if (!Path)
202 Path = sys::findProgramByName(Name);
203 if (!Path)
204 return createStringError(EC: Path.getError(),
205 S: "Unable to find '" + Name + "' in path");
206 return *Path;
207}
208
209std::optional<std::string> findFile(StringRef Dir, StringRef Root,
210 const Twine &Name) {
211 SmallString<128> Path;
212 if (Dir.starts_with(Prefix: "="))
213 sys::path::append(path&: Path, a: Root, b: Dir.substr(Start: 1), c: Name);
214 else
215 sys::path::append(path&: Path, a: Dir, b: Name);
216
217 if (sys::fs::exists(Path))
218 return static_cast<std::string>(Path);
219 return std::nullopt;
220}
221
222std::optional<std::string>
223findFromSearchPaths(StringRef Name, StringRef Root,
224 ArrayRef<StringRef> SearchPaths) {
225 for (StringRef Dir : SearchPaths)
226 if (std::optional<std::string> File = findFile(Dir, Root, Name))
227 return File;
228 return std::nullopt;
229}
230
231std::optional<std::string>
232searchLibraryBaseName(StringRef Name, StringRef Root,
233 ArrayRef<StringRef> SearchPaths) {
234 for (StringRef Dir : SearchPaths)
235 if (std::optional<std::string> File =
236 findFile(Dir, Root, Name: "lib" + Name + ".a"))
237 return File;
238 return std::nullopt;
239}
240
241/// Search for static libraries in the linker's library path given input like
242/// `-lfoo` or `-l:libfoo.a`.
243std::optional<std::string> searchLibrary(StringRef Input, StringRef Root,
244 ArrayRef<StringRef> SearchPaths) {
245 if (Input.starts_with(Prefix: ":"))
246 return findFromSearchPaths(Name: Input.drop_front(), Root, SearchPaths);
247 return searchLibraryBaseName(Name: Input, Root, SearchPaths);
248}
249
250void printCommands(ArrayRef<StringRef> CmdArgs) {
251 if (CmdArgs.empty())
252 return;
253
254 errs() << " \"" << CmdArgs.front() << "\" ";
255 errs() << join(Begin: std::next(x: CmdArgs.begin()), End: CmdArgs.end(), Separator: " ") << "\n";
256}
257
258/// A minimum symbol interface that provides the necessary information to
259/// extract archive members and resolve LTO symbols.
260struct Symbol {
261 enum Flags {
262 None = 0,
263 Undefined = 1 << 0,
264 Weak = 1 << 1,
265 };
266
267 Symbol() : File(), Flags(None), UsedInRegularObj(false) {}
268 Symbol(Symbol::Flags Flags) : File(), Flags(Flags), UsedInRegularObj(true) {}
269
270 Symbol(MemoryBufferRef File, const irsymtab::Reader::SymbolRef Sym)
271 : File(File), Flags(0), UsedInRegularObj(false) {
272 if (Sym.isUndefined())
273 Flags |= Undefined;
274 if (Sym.isWeak())
275 Flags |= Weak;
276 }
277
278 Symbol(MemoryBufferRef File, const SymbolRef Sym)
279 : File(File), Flags(0), UsedInRegularObj(false) {
280 auto FlagsOrErr = Sym.getFlags();
281 if (!FlagsOrErr)
282 reportError(E: FlagsOrErr.takeError());
283 if (*FlagsOrErr & SymbolRef::SF_Undefined)
284 Flags |= Undefined;
285 if (*FlagsOrErr & SymbolRef::SF_Weak)
286 Flags |= Weak;
287
288 auto NameOrErr = Sym.getName();
289 if (!NameOrErr)
290 reportError(E: NameOrErr.takeError());
291 }
292
293 bool isWeak() const { return Flags & Weak; }
294 bool isUndefined() const { return Flags & Undefined; }
295
296 MemoryBufferRef File;
297 uint32_t Flags;
298 bool UsedInRegularObj;
299};
300
301Expected<StringRef> runPTXAs(StringRef File, const ArgList &Args) {
302 SmallVector<StringRef, 1> SearchPaths;
303 if (Arg *A = Args.getLastArg(Ids: OPT_cuda_path_EQ))
304 SearchPaths.push_back(Elt: Args.MakeArgString(Str: A->getValue() + Twine("/bin")));
305 if (Arg *A = Args.getLastArg(Ids: OPT_ptxas_path_EQ))
306 SearchPaths.push_back(Elt: Args.MakeArgString(Str: A->getValue()));
307
308 Expected<std::string> PTXAsPath = findProgram(Args, Name: "ptxas", Paths: SearchPaths);
309 if (!PTXAsPath)
310 return PTXAsPath.takeError();
311
312 if (!Args.hasArg(Ids: OPT_arch))
313 return createStringError(
314 Fmt: "must pass in an explicit nvptx64 gpu architecture to 'ptxas'");
315
316 auto TempFileOrErr = createTempFile(
317 Args, Prefix: sys::path::stem(path: Args.getLastArgValue(Id: OPT_o, Default: "a.out")), Extension: "cubin");
318 if (!TempFileOrErr)
319 return TempFileOrErr.takeError();
320
321 SmallVector<StringRef> AssemblerArgs({*PTXAsPath, "-m64", "-c", File});
322 if (Args.hasArg(Ids: OPT_verbose))
323 AssemblerArgs.push_back(Elt: "-v");
324 if (Args.hasArg(Ids: OPT_g)) {
325 if (Args.getLastArgValue(Id: OPT_O, Default: "3") != "0")
326 WithColor::warning(OS&: errs(), Prefix: Executable)
327 << "Optimized debugging not supported, overriding to '-O0'\n";
328 AssemblerArgs.push_back(Elt: "-O0");
329 AssemblerArgs.push_back(Elt: "-g");
330 } else {
331 AssemblerArgs.push_back(
332 Elt: Args.MakeArgString(Str: "-O" + Args.getLastArgValue(Id: OPT_O, Default: "3")));
333 }
334 AssemblerArgs.append(IL: {"-arch", Args.getLastArgValue(Id: OPT_arch)});
335 AssemblerArgs.append(IL: {"-o", *TempFileOrErr});
336
337 if (Args.hasArg(Ids: OPT_dry_run) || Args.hasArg(Ids: OPT_verbose))
338 printCommands(CmdArgs: AssemblerArgs);
339 if (Args.hasArg(Ids: OPT_dry_run))
340 return Args.MakeArgString(Str: *TempFileOrErr);
341 if (sys::ExecuteAndWait(Program: *PTXAsPath, Args: AssemblerArgs))
342 return createStringError(S: "'" + sys::path::filename(path: *PTXAsPath) + "'" +
343 " failed");
344 return Args.MakeArgString(Str: *TempFileOrErr);
345}
346
347Expected<std::unique_ptr<lto::LTO>> createLTO(const ArgList &Args) {
348 const llvm::Triple Triple("nvptx64-nvidia-cuda");
349 lto::Config Conf;
350 lto::ThinBackend Backend;
351 unsigned Jobs = 0;
352 if (auto *Arg = Args.getLastArg(Ids: OPT_jobs))
353 if (!to_integer(S: Arg->getValue(), Num&: Jobs) || Jobs == 0)
354 reportError(E: createStringError(Fmt: "%s: expected a positive integer, got '%s'",
355 Vals: Arg->getSpelling().data(),
356 Vals: Arg->getValue()));
357 Backend =
358 lto::createInProcessThinBackend(Parallelism: heavyweight_hardware_concurrency(ThreadCount: Jobs));
359
360 Conf.CPU = Args.getLastArgValue(Id: OPT_arch);
361 Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple: Triple);
362
363 Conf.RemarksFilename =
364 Args.getLastArgValue(Id: OPT_opt_remarks_filename, Default: RemarksFilename);
365 Conf.RemarksPasses =
366 Args.getLastArgValue(Id: OPT_opt_remarks_filter, Default: RemarksPasses);
367 Conf.RemarksFormat =
368 Args.getLastArgValue(Id: OPT_opt_remarks_format, Default: RemarksFormat);
369
370 Conf.RemarksWithHotness =
371 Args.hasArg(Ids: OPT_opt_remarks_with_hotness) || RemarksWithHotness;
372 Conf.RemarksHotnessThreshold = RemarksHotnessThreshold;
373
374 Conf.MAttrs = llvm::codegen::getMAttrs();
375 std::optional<CodeGenOptLevel> CGOptLevelOrNone =
376 CodeGenOpt::parseLevel(C: Args.getLastArgValue(Id: OPT_O, Default: "2")[0]);
377 assert(CGOptLevelOrNone && "Invalid optimization level");
378 Conf.CGOptLevel = *CGOptLevelOrNone;
379 Conf.OptLevel = Args.getLastArgValue(Id: OPT_O, Default: "2")[0] - '0';
380 Conf.DefaultTriple = Triple.getTriple();
381
382 Conf.OptPipeline = Args.getLastArgValue(Id: OPT_lto_newpm_passes, Default: "");
383 Conf.PassPlugins = PassPlugins;
384 Conf.DebugPassManager = Args.hasArg(Ids: OPT_lto_debug_pass_manager);
385
386 Conf.DiagHandler = diagnosticHandler;
387 Conf.CGFileType = CodeGenFileType::AssemblyFile;
388
389 if (Args.hasArg(Ids: OPT_lto_emit_llvm)) {
390 Conf.PreCodeGenModuleHook = [&](size_t, const Module &M) {
391 std::error_code EC;
392 raw_fd_ostream LinkedBitcode(Args.getLastArgValue(Id: OPT_o, Default: "a.out"), EC);
393 if (EC)
394 reportError(E: errorCodeToError(EC));
395 WriteBitcodeToFile(M, Out&: LinkedBitcode);
396 return false;
397 };
398 }
399
400 if (Args.hasArg(Ids: OPT_save_temps))
401 if (Error Err = Conf.addSaveTemps(
402 OutputFileName: (Args.getLastArgValue(Id: OPT_o, Default: "a.out") + ".").str()))
403 return Err;
404
405 unsigned Partitions = 1;
406 if (auto *Arg = Args.getLastArg(Ids: OPT_lto_partitions))
407 if (!to_integer(S: Arg->getValue(), Num&: Partitions) || Partitions == 0)
408 reportError(E: createStringError(Fmt: "%s: expected a positive integer, got '%s'",
409 Vals: Arg->getSpelling().data(),
410 Vals: Arg->getValue()));
411 lto::LTO::LTOKind Kind = Args.hasArg(Ids: OPT_thinlto) ? lto::LTO::LTOK_UnifiedThin
412 : lto::LTO::LTOK_Default;
413 return std::make_unique<lto::LTO>(args: std::move(Conf), args&: Backend, args&: Partitions, args&: Kind);
414}
415
416Expected<bool> getSymbolsFromBitcode(MemoryBufferRef Buffer,
417 StringMap<Symbol> &SymTab, bool IsLazy) {
418 Expected<IRSymtabFile> IRSymtabOrErr = readIRSymtab(MBRef: Buffer);
419 if (!IRSymtabOrErr)
420 return IRSymtabOrErr.takeError();
421 bool Extracted = !IsLazy;
422 StringMap<Symbol> PendingSymbols;
423 for (unsigned I = 0; I != IRSymtabOrErr->Mods.size(); ++I) {
424 for (const auto &IRSym : IRSymtabOrErr->TheReader.module_symbols(I)) {
425 if (IRSym.isFormatSpecific() || !IRSym.isGlobal())
426 continue;
427
428 Symbol &OldSym = !SymTab.count(Key: IRSym.getName()) && IsLazy
429 ? PendingSymbols[IRSym.getName()]
430 : SymTab[IRSym.getName()];
431 Symbol Sym = Symbol(Buffer, IRSym);
432 if (OldSym.File.getBuffer().empty())
433 OldSym = Sym;
434
435 bool ResolvesReference =
436 !Sym.isUndefined() &&
437 (OldSym.isUndefined() || (OldSym.isWeak() && !Sym.isWeak())) &&
438 !(OldSym.isWeak() && OldSym.isUndefined() && IsLazy);
439 Extracted |= ResolvesReference;
440
441 Sym.UsedInRegularObj = OldSym.UsedInRegularObj;
442 if (ResolvesReference)
443 OldSym = Sym;
444 }
445 }
446 if (Extracted)
447 for (const auto &[Name, Symbol] : PendingSymbols)
448 SymTab[Name] = Symbol;
449 return Extracted;
450}
451
452Expected<bool> getSymbolsFromObject(ObjectFile &ObjFile,
453 StringMap<Symbol> &SymTab, bool IsLazy) {
454 bool Extracted = !IsLazy;
455 StringMap<Symbol> PendingSymbols;
456 for (SymbolRef ObjSym : ObjFile.symbols()) {
457 auto NameOrErr = ObjSym.getName();
458 if (!NameOrErr)
459 return NameOrErr.takeError();
460
461 Symbol &OldSym = !SymTab.count(Key: *NameOrErr) && IsLazy
462 ? PendingSymbols[*NameOrErr]
463 : SymTab[*NameOrErr];
464 Symbol Sym = Symbol(ObjFile.getMemoryBufferRef(), ObjSym);
465 if (OldSym.File.getBuffer().empty())
466 OldSym = Sym;
467
468 bool ResolvesReference = OldSym.isUndefined() && !Sym.isUndefined() &&
469 (!OldSym.isWeak() || !IsLazy);
470 Extracted |= ResolvesReference;
471
472 if (ResolvesReference)
473 OldSym = Sym;
474 OldSym.UsedInRegularObj = true;
475 }
476 if (Extracted)
477 for (const auto &[Name, Symbol] : PendingSymbols)
478 SymTab[Name] = Symbol;
479 return Extracted;
480}
481
482Expected<bool> getSymbols(MemoryBufferRef Buffer, StringMap<Symbol> &SymTab,
483 bool IsLazy) {
484 switch (identify_magic(magic: Buffer.getBuffer())) {
485 case file_magic::bitcode: {
486 return getSymbolsFromBitcode(Buffer, SymTab, IsLazy);
487 }
488 case file_magic::elf_relocatable: {
489 Expected<std::unique_ptr<ObjectFile>> ObjFile =
490 ObjectFile::createObjectFile(Object: Buffer);
491 if (!ObjFile)
492 return ObjFile.takeError();
493 return getSymbolsFromObject(ObjFile&: **ObjFile, SymTab, IsLazy);
494 }
495 default:
496 return createStringError(Fmt: "Unsupported file type");
497 }
498}
499
500Expected<SmallVector<StringRef>> getInput(const ArgList &Args) {
501 SmallVector<StringRef> LibraryPaths;
502 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_library_path))
503 LibraryPaths.push_back(Elt: Arg->getValue());
504
505 bool WholeArchive = false;
506 SmallVector<std::pair<std::unique_ptr<MemoryBuffer>, bool>> InputFiles;
507 for (const opt::Arg *Arg : Args.filtered(
508 Ids: OPT_INPUT, Ids: OPT_library, Ids: OPT_whole_archive, Ids: OPT_no_whole_archive)) {
509 if (Arg->getOption().matches(ID: OPT_whole_archive) ||
510 Arg->getOption().matches(ID: OPT_no_whole_archive)) {
511 WholeArchive = Arg->getOption().matches(ID: OPT_whole_archive);
512 continue;
513 }
514
515 std::optional<std::string> Filename =
516 Arg->getOption().matches(ID: OPT_library)
517 ? searchLibrary(Input: Arg->getValue(), /*Root=*/"", SearchPaths: LibraryPaths)
518 : std::string(Arg->getValue());
519
520 if (!Filename && Arg->getOption().matches(ID: OPT_library))
521 return createStringError(Fmt: "unable to find library -l%s", Vals: Arg->getValue());
522
523 if (!Filename || !sys::fs::exists(Path: *Filename) ||
524 sys::fs::is_directory(Path: *Filename))
525 continue;
526
527 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
528 MemoryBuffer::getFileOrSTDIN(Filename: *Filename);
529 if (std::error_code EC = BufferOrErr.getError())
530 return createFileError(F: *Filename, EC);
531
532 MemoryBufferRef Buffer = **BufferOrErr;
533 switch (identify_magic(magic: Buffer.getBuffer())) {
534 case file_magic::bitcode:
535 case file_magic::elf_relocatable:
536 InputFiles.emplace_back(Args: std::move(*BufferOrErr), /*IsLazy=*/Args: false);
537 break;
538 case file_magic::archive: {
539 Expected<std::unique_ptr<object::Archive>> LibFile =
540 object::Archive::create(Source: Buffer);
541 if (!LibFile)
542 return LibFile.takeError();
543 Error Err = Error::success();
544 for (auto Child : (*LibFile)->children(Err)) {
545 auto ChildBufferOrErr = Child.getMemoryBufferRef();
546 if (!ChildBufferOrErr)
547 return ChildBufferOrErr.takeError();
548 std::unique_ptr<MemoryBuffer> ChildBuffer =
549 MemoryBuffer::getMemBufferCopy(
550 InputData: ChildBufferOrErr->getBuffer(),
551 BufferName: ChildBufferOrErr->getBufferIdentifier());
552 InputFiles.emplace_back(Args: std::move(ChildBuffer), Args: !WholeArchive);
553 }
554 if (Err)
555 return Err;
556 break;
557 }
558 default:
559 return createStringError(Fmt: "Unsupported file type");
560 }
561 }
562
563 bool Extracted = true;
564 StringMap<Symbol> SymTab;
565 for (auto &Sym : Args.getAllArgValues(Id: OPT_u))
566 SymTab[Sym] = Symbol(Symbol::Undefined);
567 SmallVector<std::unique_ptr<MemoryBuffer>> LinkerInput;
568 while (Extracted) {
569 Extracted = false;
570 for (auto &[Input, IsLazy] : InputFiles) {
571 if (!Input)
572 continue;
573
574 if (hasFatBinary(Args, Buffer: *Input)) {
575 LinkerInput.emplace_back(Args: std::move(Input));
576 continue;
577 }
578
579 // Archive members only extract if they define needed symbols. We will
580 // re-scan all the inputs if any files were extracted for the link job.
581 Expected<bool> ExtractOrErr = getSymbols(Buffer: *Input, SymTab, IsLazy);
582 if (!ExtractOrErr)
583 return ExtractOrErr.takeError();
584
585 Extracted |= *ExtractOrErr;
586 if (!*ExtractOrErr)
587 continue;
588
589 LinkerInput.emplace_back(Args: std::move(Input));
590 }
591 }
592 InputFiles.clear();
593
594 // Extract any bitcode files to be passed to the LTO pipeline.
595 SmallVector<std::unique_ptr<MemoryBuffer>> BitcodeFiles;
596 for (auto &Input : LinkerInput)
597 if (identify_magic(magic: Input->getBuffer()) == file_magic::bitcode)
598 BitcodeFiles.emplace_back(Args: std::move(Input));
599 erase_if(C&: LinkerInput, P: [](const auto &F) { return !F; });
600
601 // Run the LTO pipeline on the extracted inputs.
602 SmallVector<StringRef> Files;
603 if (!BitcodeFiles.empty()) {
604 auto LTOBackendOrErr = createLTO(Args);
605 if (!LTOBackendOrErr)
606 return LTOBackendOrErr.takeError();
607 lto::LTO &LTOBackend = **LTOBackendOrErr;
608 for (auto &BitcodeFile : BitcodeFiles) {
609 Expected<std::unique_ptr<lto::InputFile>> BitcodeFileOrErr =
610 lto::InputFile::create(Object: *BitcodeFile);
611 if (!BitcodeFileOrErr)
612 return BitcodeFileOrErr.takeError();
613
614 const auto Symbols = (*BitcodeFileOrErr)->symbols();
615 SmallVector<lto::SymbolResolution, 16> Resolutions(Symbols.size());
616 size_t Idx = 0;
617 for (auto &Sym : Symbols) {
618 lto::SymbolResolution &Res = Resolutions[Idx++];
619 Symbol ObjSym = SymTab[Sym.getName()];
620 // We will use this as the prevailing symbol in LTO if it is not
621 // undefined and it is from the file that contained the canonical
622 // definition.
623 Res.Prevailing = !Sym.isUndefined() && ObjSym.File == *BitcodeFile;
624
625 // We need LTO to preseve the following global symbols:
626 // 1) All symbols during a relocatable link.
627 // 2) Symbols used in regular objects.
628 // 3) Prevailing symbols that are needed visible to the gpu runtime.
629 Res.VisibleToRegularObj =
630 Args.hasArg(Ids: OPT_relocatable) || ObjSym.UsedInRegularObj ||
631 (Res.Prevailing &&
632 (Sym.getVisibility() != GlobalValue::HiddenVisibility &&
633 !Sym.canBeOmittedFromSymbolTable()));
634
635 // Identify symbols that must be exported dynamically and can be
636 // referenced by other files, (i.e. the runtime).
637 Res.ExportDynamic =
638 Sym.getVisibility() != GlobalValue::HiddenVisibility &&
639 !Sym.canBeOmittedFromSymbolTable();
640
641 // The NVIDIA platform does not support any symbol preemption.
642 Res.FinalDefinitionInLinkageUnit = true;
643
644 // We do not support linker redefined symbols (e.g. --wrap) for device
645 // image linking, so the symbols will not be changed after LTO.
646 Res.LinkerRedefined = false;
647 }
648
649 // Add the bitcode file with its resolved symbols to the LTO job.
650 if (Error Err = LTOBackend.add(Obj: std::move(*BitcodeFileOrErr), Res: Resolutions))
651 return Err;
652 }
653
654 // Run the LTO job to compile the bitcode.
655 size_t MaxTasks = LTOBackend.getMaxTasks();
656 SmallVector<StringRef> LTOFiles(MaxTasks);
657 auto AddStream =
658 [&](size_t Task,
659 const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
660 int FD = -1;
661 auto &TempFile = LTOFiles[Task];
662 if (Args.hasArg(Ids: OPT_lto_emit_asm))
663 TempFile = Args.getLastArgValue(Id: OPT_o, Default: "a.out");
664 else {
665 auto TempFileOrErr = createTempFile(
666 Args, Prefix: sys::path::stem(path: Args.getLastArgValue(Id: OPT_o, Default: "a.out")), Extension: "s");
667 if (!TempFileOrErr)
668 reportError(E: TempFileOrErr.takeError());
669 TempFile = Args.MakeArgString(Str: *TempFileOrErr);
670 }
671 if (std::error_code EC = sys::fs::openFileForWrite(Name: TempFile, ResultFD&: FD))
672 reportError(E: errorCodeToError(EC));
673 return std::make_unique<CachedFileStream>(
674 args: std::make_unique<raw_fd_ostream>(args&: FD, args: true));
675 };
676
677 if (Error Err = LTOBackend.run(AddStream))
678 return Err;
679
680 if (Args.hasArg(Ids: OPT_lto_emit_llvm) || Args.hasArg(Ids: OPT_lto_emit_asm))
681 return Files;
682
683 for (StringRef LTOFile : LTOFiles) {
684 auto FileOrErr = runPTXAs(File: LTOFile, Args);
685 if (!FileOrErr)
686 return FileOrErr.takeError();
687 Files.emplace_back(Args&: *FileOrErr);
688 }
689 }
690
691 // Create a copy for each file to a new file ending in `.cubin`. The 'nvlink'
692 // linker requires all NVPTX inputs to have this extension for some reason.
693 // We don't use a symbolic link because it's not supported on Windows and some
694 // of this input files could be extracted from an archive.
695 for (auto &Input : LinkerInput) {
696 auto TempFileOrErr = createTempFile(
697 Args, Prefix: sys::path::stem(path: Input->getBufferIdentifier()),
698 Extension: hasFatBinary(Args, Buffer: Input->getMemBufferRef()) ? "o" : "cubin");
699 if (!TempFileOrErr)
700 return TempFileOrErr.takeError();
701 Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
702 FileOutputBuffer::create(FilePath: *TempFileOrErr, Size: Input->getBuffer().size());
703 if (!OutputOrErr)
704 return OutputOrErr.takeError();
705 std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
706 copy(Range: Input->getBuffer(), Out: Output->getBufferStart());
707 if (Error E = Output->commit())
708 return E;
709 Files.emplace_back(Args: Args.MakeArgString(Str: *TempFileOrErr));
710 }
711
712 return Files;
713}
714
715Error runNVLink(ArrayRef<StringRef> Files, const ArgList &Args) {
716 if (Args.hasArg(Ids: OPT_lto_emit_asm) || Args.hasArg(Ids: OPT_lto_emit_llvm))
717 return Error::success();
718
719 SmallVector<StringRef, 1> SearchPaths;
720 if (Arg *A = Args.getLastArg(Ids: OPT_cuda_path_EQ))
721 SearchPaths.push_back(Elt: Args.MakeArgString(Str: A->getValue() + Twine("/bin")));
722
723 Expected<std::string> NVLinkPath = findProgram(Args, Name: "nvlink", Paths: SearchPaths);
724 if (!NVLinkPath)
725 return NVLinkPath.takeError();
726
727 if (!Args.hasArg(Ids: OPT_arch))
728 return createStringError(
729 Fmt: "must pass in an explicit nvptx64 gpu architecture to 'nvlink'");
730
731 ArgStringList NewLinkerArgs;
732 for (const opt::Arg *Arg : Args) {
733 // Do not forward arguments only intended for the linker wrapper.
734 if (Arg->getOption().hasFlag(Val: WrapperOnlyOption))
735 continue;
736
737 // Do not forward any inputs that we have processed.
738 if (Arg->getOption().matches(ID: OPT_INPUT) ||
739 Arg->getOption().matches(ID: OPT_library))
740 continue;
741
742 Arg->render(Args, Output&: NewLinkerArgs);
743 }
744
745 transform(Range&: Files, d_first: std::back_inserter(x&: NewLinkerArgs),
746 F: [&](StringRef Arg) { return Args.MakeArgString(Str: Arg); });
747
748 SmallVector<StringRef> LinkerArgs({*NVLinkPath});
749 if (!Args.hasArg(Ids: OPT_o))
750 LinkerArgs.append(IL: {"-o", "a.out"});
751 for (StringRef Arg : NewLinkerArgs)
752 LinkerArgs.push_back(Elt: Arg);
753
754 if (Args.hasArg(Ids: OPT_dry_run) || Args.hasArg(Ids: OPT_verbose))
755 printCommands(CmdArgs: LinkerArgs);
756 if (Args.hasArg(Ids: OPT_dry_run))
757 return Error::success();
758 if (sys::ExecuteAndWait(Program: *NVLinkPath, Args: LinkerArgs))
759 return createStringError(S: "'" + sys::path::filename(path: *NVLinkPath) + "'" +
760 " failed");
761 return Error::success();
762}
763
764} // namespace
765
766int main(int argc, char **argv) {
767 InitLLVM X(argc, argv);
768 InitializeAllTargetInfos();
769 InitializeAllTargets();
770 InitializeAllTargetMCs();
771 InitializeAllAsmParsers();
772 InitializeAllAsmPrinters();
773
774 Executable = argv[0];
775 sys::PrintStackTraceOnErrorSignal(Argv0: argv[0]);
776
777 const OptTable &Tbl = getOptTable();
778 BumpPtrAllocator Alloc;
779 StringSaver Saver(Alloc);
780 auto Args = Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_INVALID, Saver, ErrorFn: [&](StringRef Err) {
781 reportError(E: createStringError(EC: inconvertibleErrorCode(), S: Err));
782 });
783
784 if (Args.hasArg(Ids: OPT_help) || Args.hasArg(Ids: OPT_help_hidden)) {
785 Tbl.printHelp(
786 OS&: outs(), Usage: "clang-nvlink-wrapper [options] <options to passed to nvlink>",
787 Title: "A utility that wraps around the NVIDIA 'nvlink' linker.\n"
788 "This enables static linking and LTO handling for NVPTX targets.",
789 ShowHidden: Args.hasArg(Ids: OPT_help_hidden), ShowAllAliases: Args.hasArg(Ids: OPT_help_hidden));
790 return EXIT_SUCCESS;
791 }
792
793 if (Args.hasArg(Ids: OPT_version))
794 printVersion(OS&: outs());
795
796 // This forwards '-mllvm' arguments to LLVM if present.
797 SmallVector<const char *> NewArgv = {argv[0]};
798 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_mllvm))
799 NewArgv.push_back(Elt: Arg->getValue());
800 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_plugin_opt))
801 NewArgv.push_back(Elt: Arg->getValue());
802 cl::ParseCommandLineOptions(argc: NewArgv.size(), argv: &NewArgv[0]);
803
804 // Get the input files to pass to 'nvlink'.
805 auto FilesOrErr = getInput(Args);
806 if (!FilesOrErr)
807 reportError(E: FilesOrErr.takeError());
808
809 // Run 'nvlink' on the generated inputs.
810 if (Error Err = runNVLink(Files: *FilesOrErr, Args))
811 reportError(E: std::move(Err));
812
813 // Remove the temporary files created.
814 if (!Args.hasArg(Ids: OPT_save_temps))
815 for (const auto &TempFile : TempFiles)
816 if (std::error_code EC = sys::fs::remove(path: TempFile))
817 reportError(E: createFileError(F: TempFile, EC));
818
819 return EXIT_SUCCESS;
820}
821