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