1//===-- clang-linker-wrapper/ClangLinkerWrapper.cpp -----------------------===//
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 works as a wrapper over a linking job. This tool is used to create
10// linked device images for offloading. It scans the linker's input for embedded
11// device offloading data stored in sections `.llvm.offloading` and extracts it
12// as a temporary file. The extracted device files will then be passed to a
13// device linking job to create a final device image.
14//
15//===----------------------------------------------------------------------===//
16
17#include "clang/Basic/TargetID.h"
18#include "clang/Basic/Version.h"
19#include "llvm/ADT/MapVector.h"
20#include "llvm/BinaryFormat/Magic.h"
21#include "llvm/Bitcode/BitcodeWriter.h"
22#include "llvm/CodeGen/CommandFlags.h"
23#include "llvm/Frontend/Offloading/OffloadWrapper.h"
24#include "llvm/Frontend/Offloading/Utility.h"
25#include "llvm/IR/DiagnosticPrinter.h"
26#include "llvm/IR/Module.h"
27#include "llvm/IRReader/IRReader.h"
28#include "llvm/LTO/LTO.h"
29#include "llvm/MC/TargetRegistry.h"
30#include "llvm/Object/Binary.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/Parallel.h"
45#include "llvm/Support/Path.h"
46#include "llvm/Support/Program.h"
47#include "llvm/Support/Signals.h"
48#include "llvm/Support/SourceMgr.h"
49#include "llvm/Support/StringSaver.h"
50#include "llvm/Support/TargetSelect.h"
51#include "llvm/Support/TimeProfiler.h"
52#include "llvm/Support/WithColor.h"
53#include "llvm/Support/raw_ostream.h"
54#include "llvm/Target/TargetMachine.h"
55#include "llvm/TargetParser/Host.h"
56#include <optional>
57
58using namespace llvm;
59using namespace llvm::opt;
60using namespace llvm::object;
61
62// Various tools (e.g., llc and opt) duplicate this series of declarations for
63// options related to passes and remarks.
64
65static cl::opt<bool> RemarksWithHotness(
66 "pass-remarks-with-hotness",
67 cl::desc("With PGO, include profile count in optimization remarks"),
68 cl::Hidden);
69
70static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
71 RemarksHotnessThreshold(
72 "pass-remarks-hotness-threshold",
73 cl::desc("Minimum profile count required for "
74 "an optimization remark to be output. "
75 "Use 'auto' to apply the threshold from profile summary."),
76 cl::value_desc("N or 'auto'"), cl::init(Val: 0), cl::Hidden);
77
78static cl::opt<std::string>
79 RemarksFilename("pass-remarks-output",
80 cl::desc("Output filename for pass remarks"),
81 cl::value_desc("filename"));
82
83static cl::opt<std::string>
84 RemarksPasses("pass-remarks-filter",
85 cl::desc("Only record optimization remarks from passes whose "
86 "names match the given regular expression"),
87 cl::value_desc("regex"));
88
89static cl::opt<std::string> RemarksFormat(
90 "pass-remarks-format",
91 cl::desc("The format used for serializing remarks (default: YAML)"),
92 cl::value_desc("format"), cl::init(Val: "yaml"));
93
94static cl::opt<std::string> PassPipeline(
95 "passes",
96 cl::desc(
97 "A textual description of the pass pipeline. To have analysis passes "
98 "available before a certain pass, add 'require<foo-analysis>'. "
99 "'-passes' overrides the pass pipeline (but not all effects) from "
100 "specifying '--opt-level=O?' (O2 is the default) to "
101 "clang-linker-wrapper. Be sure to include the corresponding "
102 "'default<O?>' in '-passes'."));
103static cl::alias PassPipeline2("p", cl::aliasopt(PassPipeline),
104 cl::desc("Alias for -passes"));
105
106/// Path of the current binary.
107static const char *LinkerExecutable;
108
109/// Save intermediary results.
110static bool SaveTemps = false;
111
112/// Print arguments without executing.
113static bool DryRun = false;
114
115/// Print verbose output.
116static bool Verbose = false;
117
118/// Filename of the executable being created.
119static StringRef ExecutableName;
120
121/// Binary path for the CUDA installation.
122static std::string CudaBinaryPath;
123
124/// Mutex lock to protect writes to shared TempFiles in parallel.
125static std::mutex TempFilesMutex;
126
127/// Temporary files created by the linker wrapper.
128static std::list<SmallString<128>> TempFiles;
129
130/// Whether or not to look through symlinks when resolving binaries.
131static bool CanonicalPrefixes = true;
132
133using OffloadingImage = OffloadBinary::OffloadingImage;
134
135static bool usesLLVMOffloadWrapper(ArrayRef<OffloadingImage> Images) {
136 return llvm::any_of(Range&: Images, P: [](const OffloadingImage &Image) {
137 return Triple(Image.StringData.lookup(Key: "triple")).getEnvironment() ==
138 Triple::LLVM;
139 });
140}
141
142namespace llvm {
143// Provide DenseMapInfo so that OffloadKind can be used in a DenseMap.
144template <> struct DenseMapInfo<OffloadKind> {
145 static unsigned getHashValue(const OffloadKind &Val) { return Val; }
146
147 static bool isEqual(const OffloadKind &LHS, const OffloadKind &RHS) {
148 return LHS == RHS;
149 }
150};
151} // namespace llvm
152
153namespace {
154using std::error_code;
155
156/// Must not overlap with llvm::opt::DriverFlag.
157enum WrapperFlags {
158 WrapperOnlyOption = (1 << 4), // Options only used by the linker wrapper.
159 DeviceOnlyOption = (1 << 5), // Options only used for device linking.
160};
161
162enum ID {
163 OPT_INVALID = 0, // This is not an option ID.
164#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
165#include "LinkerWrapperOpts.inc"
166 LastOption
167#undef OPTION
168};
169
170#define OPTTABLE_CODE
171#include "LinkerWrapperOpts.inc"
172
173class WrapperOptTable : public opt::OptTable {
174public:
175 WrapperOptTable() : opt::OptTable(optionTables()) {}
176};
177
178const OptTable &getOptTable() {
179 static const WrapperOptTable Table;
180 return Table;
181}
182
183void printCommands(ArrayRef<StringRef> CmdArgs) {
184 if (CmdArgs.empty())
185 return;
186
187 llvm::errs() << " \"" << CmdArgs.front() << "\" ";
188 for (auto IC = std::next(x: CmdArgs.begin()), IE = CmdArgs.end(); IC != IE; ++IC)
189 llvm::errs() << *IC << (std::next(x: IC) != IE ? " " : "\n");
190}
191
192[[noreturn]] void reportError(Error E) {
193 outs().flush();
194 logAllUnhandledErrors(E: std::move(E),
195 OS&: WithColor::error(OS&: errs(), Prefix: LinkerExecutable));
196 exit(EXIT_FAILURE);
197}
198
199std::string getExecutableDir(const char *Name) {
200 if (!CanonicalPrefixes)
201 return sys::path::parent_path(path: LinkerExecutable).str();
202 void *Ptr = reinterpret_cast<void *>(&getExecutableDir);
203 return sys::path::parent_path(path: sys::fs::getMainExecutable(argv0: Name, MainExecAddr: Ptr)).str();
204}
205
206/// Get a temporary filename suitable for output.
207Expected<StringRef> createOutputFile(const Twine &Prefix, StringRef Extension) {
208 std::scoped_lock<decltype(TempFilesMutex)> Lock(TempFilesMutex);
209 SmallString<128> OutputFile;
210 std::string PrefixStr = clang::sanitizeTargetIDInFileName(TargetID: Prefix.str());
211
212 if (SaveTemps) {
213 (PrefixStr + "." + Extension).toNullTerminatedStringRef(Out&: OutputFile);
214 } else {
215 if (std::error_code EC = sys::fs::createTemporaryFile(
216 Prefix: sys::path::filename(path: PrefixStr), Suffix: Extension, ResultPath&: OutputFile))
217 return createFileError(F: OutputFile, EC);
218 }
219
220 TempFiles.emplace_back(args: std::move(OutputFile));
221 return TempFiles.back();
222}
223
224/// Execute the command \p ExecutablePath with the arguments \p Args.
225/// If \p Proc is set, launch without waiting and store the process handle.
226Error executeCommands(StringRef ExecutablePath, ArrayRef<StringRef> Args,
227 sys::ProcessInfo *Proc = nullptr) {
228 if (Verbose || DryRun)
229 printCommands(CmdArgs: Args);
230
231 if (DryRun)
232 return Error::success();
233
234 auto Launch = [&](ArrayRef<StringRef> ExecArgs) -> Error {
235 if (Proc) {
236 bool ExecutionFailed = false;
237 *Proc = sys::ExecuteNoWait(Program: ExecutablePath, Args: ExecArgs, Env: std::nullopt, Redirects: {}, MemoryLimit: 0,
238 ErrMsg: nullptr, ExecutionFailed: &ExecutionFailed);
239 if (ExecutionFailed)
240 return createStringError(
241 Fmt: "'%s' failed", Vals: sys::path::filename(path: ExecutablePath).str().c_str());
242 return Error::success();
243 }
244 if (sys::ExecuteAndWait(Program: ExecutablePath, Args: ExecArgs))
245 return createStringError(
246 Fmt: "'%s' failed", Vals: sys::path::filename(path: ExecutablePath).str().c_str());
247 return Error::success();
248 };
249
250 // If the command line fits within system limits, execute directly.
251 if (sys::commandLineFitsWithinSystemLimits(Program: ExecutablePath, Args))
252 return Launch(Args);
253
254 // Write the arguments to a response file and pass that instead.
255 auto TempFileOrErr = createOutputFile(Prefix: "response", Extension: "rsp");
256 if (!TempFileOrErr)
257 return TempFileOrErr.takeError();
258
259 SmallString<256> Contents;
260 raw_svector_ostream OS(Contents);
261 for (StringRef Arg : llvm::drop_begin(RangeOrContainer&: Args)) {
262 sys::printArg(OS, Arg, /*Quote=*/true);
263 OS << " ";
264 }
265
266 if (std::error_code EC = sys::writeFileWithEncoding(FileName: *TempFileOrErr, Contents))
267 return createStringError(Fmt: "failed to write response file: %s",
268 Vals: EC.message().c_str());
269
270 std::string ResponseFile = ("@" + *TempFileOrErr).str();
271 SmallVector<StringRef, 2> NewArgs = {Args.front(), ResponseFile};
272 return Launch(NewArgs);
273}
274
275Expected<std::string> findProgram(StringRef Name, ArrayRef<StringRef> Paths) {
276
277 ErrorOr<std::string> Path = sys::findProgramByName(Name, Paths);
278 if (!Path)
279 Path = sys::findProgramByName(Name);
280 if (!Path && DryRun)
281 return Name.str();
282 if (!Path)
283 return createStringError(EC: Path.getError(),
284 S: "Unable to find '" + Name + "' in path");
285 return *Path;
286}
287
288bool linkerSupportsLTO(const ArgList &Args) {
289 llvm::Triple Triple(Args.getLastArgValue(Id: OPT_triple_EQ));
290 return Triple.isNVPTX() || Triple.isAMDGPU() ||
291 (!Triple.isGPU() &&
292 Args.getLastArgValue(Id: OPT_linker_path_EQ).ends_with(Suffix: "lld"));
293}
294
295/// Returns the hashed value for a constant string.
296std::string getHash(StringRef Str) {
297 llvm::MD5 Hasher;
298 llvm::MD5::MD5Result Hash;
299 Hasher.update(Str);
300 Hasher.final(Result&: Hash);
301 return llvm::utohexstr(X: Hash.low(), /*LowerCase=*/true);
302}
303
304/// Renames offloading entry sections in a relocatable link so they do not
305/// conflict with a later link job.
306Error relocateOffloadSection(const ArgList &Args, StringRef Output) {
307 llvm::Triple Triple(
308 Args.getLastArgValue(Id: OPT_host_triple_EQ, Default: sys::getDefaultTargetTriple()));
309 if (Triple.isOSWindows())
310 return createStringError(
311 Fmt: "Relocatable linking is not supported on COFF targets");
312
313 Expected<std::string> ObjcopyPath =
314 findProgram(Name: "llvm-objcopy", Paths: {getExecutableDir(Name: "llvm-objcopy")});
315 if (!ObjcopyPath)
316 return ObjcopyPath.takeError();
317
318 // Use the linker output file to get a unique hash. This creates a unique
319 // identifier to rename the sections to that is deterministic to the contents.
320 auto BufferOrErr = DryRun ? MemoryBuffer::getMemBuffer(InputData: "")
321 : MemoryBuffer::getFileOrSTDIN(Filename: Output);
322 if (!BufferOrErr)
323 return createStringError(Fmt: "Failed to open %s", Vals: Output.str().c_str());
324 std::string Suffix = "_" + getHash(Str: (*BufferOrErr)->getBuffer());
325
326 SmallVector<StringRef> ObjcopyArgs = {
327 *ObjcopyPath,
328 Output,
329 };
330
331 // Remove the old .llvm.offloading section to prevent further linking.
332 ObjcopyArgs.emplace_back(Args: "--remove-section");
333 ObjcopyArgs.emplace_back(Args: ".llvm.offloading");
334 StringRef Prefix = "llvm";
335 auto Section = (Prefix + "_offload_entries").str();
336 // Rename the offloading entries to make them private to this link unit.
337 ObjcopyArgs.emplace_back(Args: "--rename-section");
338 ObjcopyArgs.emplace_back(
339 Args: Args.MakeArgString(Str: Section + "=" + Section + Suffix));
340
341 // Rename the __start_ / __stop_ symbols appropriately to iterate over the
342 // newly renamed section containing the offloading entries.
343 ObjcopyArgs.emplace_back(Args: "--redefine-sym");
344 ObjcopyArgs.emplace_back(Args: Args.MakeArgString(Str: "__start_" + Section + "=" +
345 "__start_" + Section + Suffix));
346 ObjcopyArgs.emplace_back(Args: "--redefine-sym");
347 ObjcopyArgs.emplace_back(Args: Args.MakeArgString(Str: "__stop_" + Section + "=" +
348 "__stop_" + Section + Suffix));
349
350 if (Error Err = executeCommands(ExecutablePath: *ObjcopyPath, Args: ObjcopyArgs))
351 return Err;
352
353 return Error::success();
354}
355
356/// Runs the wrapped linker job with the newly created input.
357Error runLinker(ArrayRef<StringRef> Files, const ArgList &Args) {
358 llvm::TimeTraceScope TimeScope("Execute host linker");
359
360 // Render the linker arguments and add the newly created image. We add it
361 // after the output file to ensure it is linked with the correct libraries.
362 StringRef LinkerPath = Args.getLastArgValue(Id: OPT_linker_path_EQ);
363 if (LinkerPath.empty())
364 return createStringError(Fmt: "linker path missing, must pass 'linker-path'");
365 ArgStringList NewLinkerArgs;
366 for (const opt::Arg *Arg : Args) {
367 // Do not forward arguments only intended for the linker wrapper.
368 if (Arg->getOption().hasFlag(Val: WrapperOnlyOption))
369 continue;
370
371 Arg->render(Args, Output&: NewLinkerArgs);
372 if (Arg->getOption().matches(ID: OPT_o) || Arg->getOption().matches(ID: OPT_out))
373 llvm::transform(Range&: Files, d_first: std::back_inserter(x&: NewLinkerArgs),
374 F: [&](StringRef A) { return Args.MakeArgString(Str: A); });
375 }
376
377 SmallVector<StringRef> LinkerArgs({LinkerPath});
378 for (StringRef Arg : NewLinkerArgs)
379 LinkerArgs.push_back(Elt: Arg);
380 if (Error Err = executeCommands(ExecutablePath: LinkerPath, Args: LinkerArgs))
381 return Err;
382
383 if (Args.hasArg(Ids: OPT_relocatable))
384 return relocateOffloadSection(Args, Output: ExecutableName);
385
386 return Error::success();
387}
388
389void printVersion(raw_ostream &OS) {
390 OS << clang::getClangToolFullVersion(ToolName: "clang-linker-wrapper") << '\n';
391}
392
393namespace nvptx {
394Expected<StringRef>
395fatbinary(ArrayRef<std::pair<StringRef, StringRef>> InputFiles,
396 const ArgList &Args) {
397 llvm::TimeTraceScope TimeScope("NVPTX fatbinary");
398 // NVPTX uses the fatbinary program to bundle the linked images.
399 Expected<std::string> FatBinaryPath =
400 findProgram(Name: "fatbinary", Paths: {CudaBinaryPath + "/bin"});
401 if (!FatBinaryPath)
402 return FatBinaryPath.takeError();
403
404 llvm::Triple Triple(
405 Args.getLastArgValue(Id: OPT_host_triple_EQ, Default: sys::getDefaultTargetTriple()));
406
407 // Create a new file to write the linked device image to.
408 auto TempFileOrErr = createOutputFile(Prefix: ExecutableName, Extension: "fatbin");
409 if (!TempFileOrErr)
410 return TempFileOrErr.takeError();
411
412 SmallVector<StringRef, 16> CmdArgs;
413 CmdArgs.push_back(Elt: *FatBinaryPath);
414 CmdArgs.push_back(Elt: Triple.isArch64Bit() ? "-64" : "-32");
415 CmdArgs.push_back(Elt: "--create");
416 CmdArgs.push_back(Elt: *TempFileOrErr);
417 for (const auto &[File, Arch] : InputFiles)
418 CmdArgs.push_back(Elt: Args.MakeArgString(
419 Str: "--image3=kind=elf,sm=" + Arch.drop_front(N: 3) + ",file=" + File));
420
421 if (Error Err = executeCommands(ExecutablePath: *FatBinaryPath, Args: CmdArgs))
422 return std::move(Err);
423
424 return *TempFileOrErr;
425}
426} // namespace nvptx
427
428namespace amdgcn {
429
430Expected<StringRef>
431fatbinary(ArrayRef<std::tuple<StringRef, StringRef, StringRef>> InputFiles,
432 const ArgList &Args) {
433 llvm::TimeTraceScope TimeScope("AMDGPU Fatbinary");
434
435 // AMDGPU uses the clang-offload-bundler to bundle the linked images.
436 Expected<std::string> OffloadBundlerPath = findProgram(
437 Name: "clang-offload-bundler", Paths: {getExecutableDir(Name: "clang-offload-bundler")});
438 if (!OffloadBundlerPath)
439 return OffloadBundlerPath.takeError();
440
441 // Create a new file to write the linked device image to.
442 auto TempFileOrErr = createOutputFile(Prefix: ExecutableName, Extension: "hipfb");
443 if (!TempFileOrErr)
444 return TempFileOrErr.takeError();
445
446 BumpPtrAllocator Alloc;
447 StringSaver Saver(Alloc);
448
449 SmallVector<StringRef, 16> CmdArgs;
450 CmdArgs.push_back(Elt: *OffloadBundlerPath);
451 CmdArgs.push_back(Elt: "-type=o");
452 CmdArgs.push_back(Elt: "-bundle-align=4096");
453
454 if (Args.hasArg(Ids: OPT_compress))
455 CmdArgs.push_back(Elt: "-compress");
456 if (auto *Arg = Args.getLastArg(Ids: OPT_compression_level_eq))
457 CmdArgs.push_back(
458 Elt: Args.MakeArgString(Str: Twine("-compression-level=") + Arg->getValue()));
459
460 llvm::Triple HostTriple(
461 Args.getLastArgValue(Id: OPT_host_triple_EQ, Default: sys::getDefaultTargetTriple()));
462 SmallVector<StringRef> Targets = {
463 Saver.save(S: "-targets=host-" + HostTriple.normalize())};
464 for (const auto &[File, TripleRef, Arch] : InputFiles) {
465 std::string NormalizedTriple =
466 clang::normalizeForBundler(T: Triple(TripleRef), BoundArch: Arch);
467 Targets.push_back(Elt: Saver.save(S: "hip-" + NormalizedTriple + "-" + Arch));
468 }
469 CmdArgs.push_back(Elt: Saver.save(S: llvm::join(R&: Targets, Separator: ",")));
470
471#ifdef _WIN32
472 CmdArgs.push_back("-input=NUL");
473#else
474 CmdArgs.push_back(Elt: "-input=/dev/null");
475#endif
476 for (const auto &[File, Triple, Arch] : InputFiles)
477 CmdArgs.push_back(Elt: Saver.save(S: "-input=" + File));
478
479 CmdArgs.push_back(Elt: Saver.save(S: "-output=" + *TempFileOrErr));
480
481 if (Error Err = executeCommands(ExecutablePath: *OffloadBundlerPath, Args: CmdArgs))
482 return std::move(Err);
483
484 return *TempFileOrErr;
485}
486} // namespace amdgcn
487
488namespace generic {
489Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
490 uint16_t ActiveOffloadKindMask) {
491 llvm::TimeTraceScope TimeScope("Clang");
492 // Use `clang` to invoke the appropriate device tools.
493 Expected<std::string> ClangPath =
494 findProgram(Name: "clang", Paths: {getExecutableDir(Name: "clang")});
495 if (!ClangPath)
496 return ClangPath.takeError();
497
498 const llvm::Triple Triple(Args.getLastArgValue(Id: OPT_triple_EQ));
499 StringRef Arch = Args.getLastArgValue(Id: OPT_arch_EQ);
500 // Create a new file to write the linked device image to. Assume that the
501 // input filename already has the device and architecture.
502 std::string OutputFileBase =
503 "." + Triple.getArchName().str() + "." + Arch.str();
504 auto TempFileOrErr = createOutputFile(Prefix: ExecutableName + OutputFileBase, Extension: "img");
505 if (!TempFileOrErr)
506 return TempFileOrErr.takeError();
507
508 SmallVector<StringRef, 16> CmdArgs{
509 *ClangPath,
510 "--no-default-config",
511 "-o",
512 *TempFileOrErr,
513 // Without -dumpdir, Clang will place auxiliary output files in the
514 // temporary directory of TempFileOrErr, where they will not easily be
515 // found by the user and might eventually be automatically removed. Tell
516 // Clang to instead place them alongside the final executable.
517 "-dumpdir",
518 Args.MakeArgString(Str: ExecutableName + OutputFileBase + ".img."),
519 Args.MakeArgString(Str: "--target=" + Triple.getTriple()),
520 };
521
522 if (!Arch.empty())
523 Triple.isAMDGPU() ? CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mcpu=" + Arch))
524 : CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-march=" + Arch));
525
526 // Forward all of the `--offload-opt` and `-mllvm` options to the device.
527 for (auto &Arg : Args.filtered(Ids: OPT_offload_opt_eq_minus, Ids: OPT_mllvm))
528 CmdArgs.append(
529 IL: {"-Xlinker",
530 Args.MakeArgString(Str: "--plugin-opt=" + StringRef(Arg->getValue()))});
531
532 if (!Triple.isNVPTX() && !Triple.isSPIRV())
533 CmdArgs.push_back(Elt: "-Wl,--no-undefined");
534
535 for (StringRef InputFile : InputFiles)
536 CmdArgs.push_back(Elt: InputFile);
537
538 // If this is CPU offloading we copy the input libraries.
539 if (!Triple.isGPU()) {
540 CmdArgs.push_back(Elt: "-Wl,-Bsymbolic");
541 CmdArgs.push_back(Elt: "-shared");
542 ArgStringList LinkerArgs;
543 for (const opt::Arg *Arg :
544 Args.filtered(Ids: OPT_INPUT, Ids: OPT_library, Ids: OPT_library_path, Ids: OPT_rpath,
545 Ids: OPT_whole_archive, Ids: OPT_no_whole_archive)) {
546 // Sometimes needed libraries are passed by name, such as when using
547 // sanitizers. We need to check the file magic for any libraries.
548 if (Arg->getOption().matches(ID: OPT_INPUT)) {
549 if (!sys::fs::exists(Path: Arg->getValue()) ||
550 sys::fs::is_directory(Path: Arg->getValue()))
551 continue;
552
553 file_magic Magic;
554 if (auto EC = identify_magic(path: Arg->getValue(), result&: Magic))
555 return createStringError(Fmt: "Failed to open %s", Vals: Arg->getValue());
556 if (Magic != file_magic::archive &&
557 Magic != file_magic::elf_shared_object)
558 continue;
559 }
560 if (Arg->getOption().matches(ID: OPT_whole_archive))
561 LinkerArgs.push_back(Elt: Args.MakeArgString(Str: "-Wl,--whole-archive"));
562 else if (Arg->getOption().matches(ID: OPT_no_whole_archive))
563 LinkerArgs.push_back(Elt: Args.MakeArgString(Str: "-Wl,--no-whole-archive"));
564 else
565 Arg->render(Args, Output&: LinkerArgs);
566 }
567 llvm::append_range(C&: CmdArgs, R&: LinkerArgs);
568 }
569
570 // Pass on -mllvm options to the linker invocation.
571 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_mllvm))
572 CmdArgs.append(IL: {"-Xlinker", Args.MakeArgString(
573 Str: "-mllvm=" + StringRef(Arg->getValue()))});
574
575 if (SaveTemps && linkerSupportsLTO(Args))
576 CmdArgs.push_back(Elt: "-Wl,--save-temps");
577
578 if (Args.hasArg(Ids: OPT_embed_bitcode)) {
579 // SPIR-V does not use the LTO linker path, it links bitcode via llvm-link.
580 if (Triple.isSPIRV())
581 CmdArgs.push_back(Elt: "-emit-llvm");
582 else
583 CmdArgs.push_back(Elt: "-Wl,--lto-emit-llvm");
584 }
585
586 // For linking device code with the SYCL offload kind, special handling is
587 // required. Passing --sycl-link to clang results in a call to
588 // clang-sycl-linker.
589 if (ActiveOffloadKindMask & OFK_SYCL)
590 CmdArgs.push_back(Elt: "--sycl-link");
591
592 for (StringRef Arg : Args.getAllArgValues(Id: OPT_linker_arg_EQ))
593 CmdArgs.append(IL: {"-Xlinker", Args.MakeArgString(Str: Arg)});
594 for (StringRef Arg : Args.getAllArgValues(Id: OPT_compiler_arg_EQ))
595 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Arg));
596
597 // We cannot generate temporary assembly files to satisfy `--save-temps`
598 // through the LTO pipeline. Launch a separate clang job to emit the file.
599 sys::ProcessInfo AsmProc;
600 if (SaveTemps && Triple.isAMDGPU()) {
601 SmallVector<StringRef, 16> AsmArgs(CmdArgs);
602 llvm::erase(C&: AsmArgs, V: "-Wl,--save-temps");
603 AsmArgs.append(IL: {"-Xlinker", "--lto-emit-asm"});
604 if (Error Err = executeCommands(ExecutablePath: *ClangPath, Args: AsmArgs, Proc: &AsmProc))
605 return std::move(Err);
606 }
607
608 Error Err = executeCommands(ExecutablePath: *ClangPath, Args: CmdArgs);
609 if (SaveTemps && Triple.isAMDGPU() && !DryRun) {
610 if (sys::Wait(PI: AsmProc, SecondsToWait: std::nullopt).ReturnCode && !Err)
611 Err = createStringError(Fmt: "'%s' failed",
612 Vals: sys::path::filename(path: *ClangPath).str().c_str());
613 }
614 if (Err)
615 return std::move(Err);
616
617 return *TempFileOrErr;
618}
619} // namespace generic
620
621Expected<StringRef> linkDevice(ArrayRef<StringRef> InputFiles,
622 const ArgList &Args,
623 uint16_t ActiveOffloadKindMask) {
624 const llvm::Triple Triple(Args.getLastArgValue(Id: OPT_triple_EQ));
625 switch (Triple.getArch()) {
626 case Triple::nvptx:
627 case Triple::nvptx64:
628 case Triple::amdgpu:
629 case Triple::x86:
630 case Triple::x86_64:
631 case Triple::aarch64:
632 case Triple::aarch64_be:
633 case Triple::ppc64:
634 case Triple::ppc64le:
635 case Triple::spirv64:
636 case Triple::systemz:
637 case Triple::loongarch64:
638 return generic::clang(InputFiles, Args, ActiveOffloadKindMask);
639 default:
640 return createStringError(S: Triple.getArchName() +
641 " linking is not supported");
642 }
643}
644
645Error containerizeRawImage(std::unique_ptr<MemoryBuffer> &Img, OffloadKind Kind,
646 const ArgList &Args) {
647 llvm::Triple Triple(Args.getLastArgValue(Id: OPT_triple_EQ));
648 if (Kind == OFK_OpenMP && Triple.isSPIRV() &&
649 Triple.getVendor() == llvm::Triple::Intel &&
650 !Args.hasArg(Ids: OPT_embed_bitcode))
651 return offloading::intel::containerizeOpenMPSPIRVImage(Binary&: Img, Triple);
652 return Error::success();
653}
654
655Expected<StringRef> writeOffloadFile(const OffloadFile &File) {
656 const OffloadBinary &Binary = *File.getBinary();
657
658 StringRef Prefix =
659 sys::path::stem(path: Binary.getMemoryBufferRef().getBufferIdentifier());
660 SmallString<128> Filename;
661 (Prefix + "-" + Binary.getTriple() + "-" + Binary.getArch())
662 .toVector(Out&: Filename);
663 auto TempFileOrErr = createOutputFile(Prefix: Filename, Extension: "o");
664 if (!TempFileOrErr)
665 return TempFileOrErr.takeError();
666
667 Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
668 FileOutputBuffer::create(FilePath: *TempFileOrErr, Size: Binary.getImage().size());
669 if (!OutputOrErr)
670 return OutputOrErr.takeError();
671 std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
672 llvm::copy(Range: Binary.getImage(), Out: Output->getBufferStart());
673 if (Error E = Output->commit())
674 return std::move(E);
675
676 return *TempFileOrErr;
677}
678
679// Compile the module to an object file using the appropriate target machine for
680// the host triple.
681Expected<StringRef> compileModule(Module &M, OffloadKind Kind) {
682 llvm::TimeTraceScope TimeScope("Compile module");
683 std::string Msg;
684 const Target *T = TargetRegistry::lookupTarget(TheTriple: M.getTargetTriple(), Error&: Msg);
685 if (!T)
686 return createStringError(S: Msg);
687
688 auto Options = codegen::getTargetOptions(LibraryOptions: M.getContext().getLibraryOptions(),
689 TheTriple: M.getTargetTriple());
690 StringRef CPU = "";
691 StringRef Features = "";
692 std::unique_ptr<TargetMachine> TM(
693 T->createTargetMachine(TT: M.getTargetTriple(), CPU, Features, Options,
694 RM: Reloc::PIC_, CM: M.getCodeModel()));
695
696 int FD = -1;
697 auto TempFileOrErr = createOutputFile(
698 Prefix: ExecutableName + "." + getOffloadKindName(Name: Kind) + ".image.wrapper", Extension: "o");
699 if (!TempFileOrErr)
700 return TempFileOrErr.takeError();
701 if (std::error_code EC = sys::fs::openFileForWrite(Name: *TempFileOrErr, ResultFD&: FD))
702 return errorCodeToError(EC);
703
704 auto OS = std::make_unique<llvm::raw_fd_ostream>(args&: FD, args: true);
705
706 legacy::PassManager CodeGenPasses;
707 TargetLibraryInfoImpl TLII(M.getTargetTriple());
708 CodeGenPasses.add(P: new TargetLibraryInfoWrapperPass(TLII));
709 if (TM->addPassesToEmitFile(CodeGenPasses, *OS, nullptr,
710 CodeGenFileType::ObjectFile))
711 return createStringError(Fmt: "Failed to execute host backend");
712 CodeGenPasses.run(M);
713
714 return *TempFileOrErr;
715}
716
717/// Performs the wrapping stage with individual tool invocations for verbose
718/// printing.
719Expected<StringRef>
720wrapDeviceImagesVerbose(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
721 const ArgList &Args, OffloadKind Kind) {
722 Expected<std::string> WrapperPath = findProgram(
723 Name: "llvm-offload-wrapper", Paths: {getExecutableDir(Name: "llvm-offload-wrapper")});
724 if (!WrapperPath)
725 return WrapperPath.takeError();
726
727 llvm::Triple Triple(
728 Args.getLastArgValue(Id: OPT_host_triple_EQ, Default: sys::getDefaultTargetTriple()));
729
730 // Generate the runtime registration bitcode from the bundled images.
731 auto BitcodeOrErr = createOutputFile(
732 Prefix: ExecutableName + "." + getOffloadKindName(Name: Kind) + ".image.wrapper", Extension: "bc");
733 if (!BitcodeOrErr)
734 return BitcodeOrErr.takeError();
735
736 SmallVector<StringRef> WrapperArgs = {
737 *WrapperPath,
738 Args.MakeArgString(Str: "--kind=" + getOffloadKindName(Name: Kind)),
739 Args.MakeArgString(Str: "--triple=" + Triple.getTriple()),
740 "-o",
741 *BitcodeOrErr,
742 };
743 if ((Kind == OFK_OpenMP || Kind == OFK_OpenACC || Kind == OFK_SYCL) &&
744 Args.hasArg(Ids: OPT_relocatable))
745 WrapperArgs.push_back(Elt: "--relocatable");
746 for (const auto &Buffer : Buffers)
747 WrapperArgs.push_back(Elt: Buffer->getBufferIdentifier());
748
749 if (Error Err = executeCommands(ExecutablePath: *WrapperPath, Args: WrapperArgs))
750 return std::move(Err);
751
752 // Compile the generated registration bitcode into a host object.
753 Expected<std::string> ClangPath =
754 findProgram(Name: "clang", Paths: {getExecutableDir(Name: "clang")});
755 if (!ClangPath)
756 return ClangPath.takeError();
757
758 auto ObjectOrErr = createOutputFile(
759 Prefix: ExecutableName + "." + getOffloadKindName(Name: Kind) + ".image.wrapper", Extension: "o");
760 if (!ObjectOrErr)
761 return ObjectOrErr.takeError();
762
763 const StringRef ClangArgs[] = {
764 *ClangPath,
765 "--no-default-config",
766 Args.MakeArgString(Str: "--target=" + Triple.getTriple()),
767 "-c",
768 "-fPIC",
769 "-o",
770 *ObjectOrErr,
771 *BitcodeOrErr,
772 };
773 if (Error Err = executeCommands(ExecutablePath: *ClangPath, Args: ClangArgs))
774 return std::move(Err);
775
776 return *ObjectOrErr;
777}
778
779/// Creates the object file containing the device image and runtime
780/// registration code from the device images stored in \p Images.
781Expected<StringRef>
782wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
783 const ArgList &Args, OffloadKind Kind) {
784 llvm::TimeTraceScope TimeScope("Wrap bundled images");
785
786 // We use the discrete tools if we are in verbose mode with '--save-temps'.
787 if (Verbose && SaveTemps && !Args.hasArg(Ids: OPT_print_wrapped_module))
788 return wrapDeviceImagesVerbose(Buffers, Args, Kind);
789
790 SmallVector<ArrayRef<char>, 4> BuffersToWrap;
791 for (const auto &Buffer : Buffers)
792 BuffersToWrap.emplace_back(
793 Args: ArrayRef<char>(Buffer->getBufferStart(), Buffer->getBufferSize()));
794
795 LLVMContext Context;
796 Module M("offload.wrapper.module", Context);
797 Triple TheTriple(
798 Args.getLastArgValue(Id: OPT_host_triple_EQ, Default: sys::getDefaultTargetTriple()));
799 M.setTargetTriple(TheTriple);
800 M.setDataLayout(TheTriple.computeDataLayout());
801
802 switch (Kind) {
803 case OFK_OpenMP:
804 if (Error Err = offloading::wrapOpenMPBinaries(
805 M, Images: BuffersToWrap, EntryArray: offloading::getOffloadEntryArray(M),
806 /*Suffix=*/"", /*Relocatable=*/Args.hasArg(Ids: OPT_relocatable)))
807 return std::move(Err);
808 break;
809 case OFK_OpenACC:
810 if (Error Err = offloading::wrapOpenACCBinaries(
811 M, Images: BuffersToWrap, EntryArray: offloading::getOffloadEntryArray(M),
812 /*Relocatable=*/Args.hasArg(Ids: OPT_relocatable)))
813 return std::move(Err);
814 break;
815 case OFK_Cuda:
816 if (Error Err = offloading::wrapCudaBinary(
817 M, Images: BuffersToWrap.front(), EntryArray: offloading::getOffloadEntryArray(M),
818 /*Suffix=*/"", /*EmitSurfacesAndTextures=*/false))
819 return std::move(Err);
820 break;
821 case OFK_HIP:
822 if (Error Err = offloading::wrapHIPBinary(
823 M, Images: BuffersToWrap.front(), EntryArray: offloading::getOffloadEntryArray(M)))
824 return std::move(Err);
825 break;
826 case OFK_SYCL: {
827 // TODO: fill these options once the Driver supports them.
828 offloading::SYCLJITOptions Options;
829 if (Error Err = offloading::wrapSYCLBinaries(
830 M, Buffer: BuffersToWrap.front(), Options,
831 /*IsFinalizedImage=*/Args.hasArg(Ids: OPT_relocatable)))
832 return std::move(Err);
833 break;
834 }
835 default:
836 return createStringError(S: getOffloadKindName(Name: Kind) +
837 " wrapping is not supported");
838 }
839
840 if (Args.hasArg(Ids: OPT_print_wrapped_module))
841 errs() << M;
842 if (Args.hasArg(Ids: OPT_save_temps)) {
843 int FD = -1;
844 auto TempFileOrErr = createOutputFile(
845 Prefix: ExecutableName + "." + getOffloadKindName(Name: Kind) + ".image.wrapper",
846 Extension: "bc");
847 if (!TempFileOrErr)
848 return TempFileOrErr.takeError();
849 if (std::error_code EC = sys::fs::openFileForWrite(Name: *TempFileOrErr, ResultFD&: FD))
850 return errorCodeToError(EC);
851 llvm::raw_fd_ostream OS(FD, true);
852 WriteBitcodeToFile(M, Out&: OS);
853 }
854
855 auto FileOrErr = compileModule(M, Kind);
856 if (!FileOrErr)
857 return FileOrErr.takeError();
858 return *FileOrErr;
859}
860
861/// Perform the OpenMP and OpenACC bundling with 'llvm-offload-binary' in
862/// verbose mode.
863Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
864bundleOmpAccVerbose(ArrayRef<OffloadingImage> Images) {
865 Expected<std::string> OffloadBinaryPath = findProgram(
866 Name: "llvm-offload-binary", Paths: {getExecutableDir(Name: "llvm-offload-binary")});
867 if (!OffloadBinaryPath)
868 return OffloadBinaryPath.takeError();
869
870 BumpPtrAllocator Alloc;
871 StringSaver Saver(Alloc);
872 SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
873 for (const OffloadingImage &Image : Images) {
874 StringRef ImageFile = Image.Image->getBufferIdentifier();
875 auto BinaryOrErr =
876 createOutputFile(Prefix: sys::path::stem(path: ImageFile) + "." +
877 getOffloadKindName(Name: Image.TheOffloadKind),
878 Extension: "offload");
879 if (!BinaryOrErr)
880 return BinaryOrErr.takeError();
881
882 std::string ImageArg = ("--image=file=" + ImageFile +
883 ",kind=" + getOffloadKindName(Name: Image.TheOffloadKind))
884 .str();
885 for (const auto &[Key, Value] : Image.StringData)
886 ImageArg += ("," + Key + "=" + Value).str();
887
888 SmallVector<StringRef> CmdArgs = {*OffloadBinaryPath, "-o", *BinaryOrErr,
889 Saver.save(S: ImageArg)};
890 if (Error Err = executeCommands(ExecutablePath: *OffloadBinaryPath, Args: CmdArgs))
891 return std::move(Err);
892
893 auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename: *BinaryOrErr);
894 if (std::error_code EC = BufferOrErr.getError()) {
895 if (DryRun)
896 BufferOrErr = MemoryBuffer::getMemBuffer(InputData: "", BufferName: *BinaryOrErr);
897 else
898 return createFileError(F: *BinaryOrErr, EC);
899 }
900 Buffers.emplace_back(Args: std::move(*BufferOrErr));
901 }
902
903 return std::move(Buffers);
904}
905
906Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
907bundleOmpAcc(ArrayRef<OffloadingImage> Images) {
908 SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
909 for (const OffloadingImage &Image : Images)
910 Buffers.emplace_back(
911 Args: MemoryBuffer::getMemBufferCopy(InputData: OffloadBinary::write(OffloadingData: Image)));
912
913 return std::move(Buffers);
914}
915
916Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
917bundleSYCL(ArrayRef<OffloadingImage> Images) {
918 SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
919 for (const OffloadingImage &Image : Images) {
920 // clang-sycl-linker packs outputs into one binary blob. Therefore, it is
921 // passed to Offload Wrapper as is.
922 StringRef S(Image.Image->getBufferStart(), Image.Image->getBufferSize());
923 Buffers.emplace_back(
924 Args: MemoryBuffer::getMemBufferCopy(InputData: S, BufferName: Image.Image->getBufferIdentifier()));
925 }
926
927 return std::move(Buffers);
928}
929
930Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
931bundleCuda(ArrayRef<OffloadingImage> Images, const ArgList &Args) {
932 SmallVector<std::pair<StringRef, StringRef>, 4> InputFiles;
933 for (const OffloadingImage &Image : Images)
934 InputFiles.emplace_back(Args: std::make_pair(x: Image.Image->getBufferIdentifier(),
935 y: Image.StringData.lookup(Key: "arch")));
936
937 auto FileOrErr = nvptx::fatbinary(InputFiles, Args);
938 if (!FileOrErr)
939 return FileOrErr.takeError();
940
941 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ImageOrError =
942 llvm::MemoryBuffer::getFileOrSTDIN(Filename: *FileOrErr);
943
944 SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
945 if (std::error_code EC = ImageOrError.getError()) {
946 if (DryRun)
947 ImageOrError = MemoryBuffer::getMemBuffer(InputData: "", BufferName: *FileOrErr);
948 else
949 return createFileError(F: *FileOrErr, EC);
950 }
951 Buffers.emplace_back(Args: std::move(*ImageOrError));
952
953 return std::move(Buffers);
954}
955
956Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
957bundleHIP(ArrayRef<OffloadingImage> Images, const ArgList &Args) {
958 SmallVector<std::tuple<StringRef, StringRef, StringRef>, 4> InputFiles;
959 for (const OffloadingImage &Image : Images)
960 InputFiles.emplace_back(Args: std::make_tuple(args: Image.Image->getBufferIdentifier(),
961 args: Image.StringData.lookup(Key: "triple"),
962 args: Image.StringData.lookup(Key: "arch")));
963
964 auto FileOrErr = amdgcn::fatbinary(InputFiles, Args);
965 if (!FileOrErr)
966 return FileOrErr.takeError();
967
968 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ImageOrError =
969 llvm::MemoryBuffer::getFileOrSTDIN(Filename: *FileOrErr);
970
971 SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
972 if (std::error_code EC = ImageOrError.getError()) {
973 if (DryRun)
974 ImageOrError = MemoryBuffer::getMemBuffer(InputData: "", BufferName: *FileOrErr);
975 else
976 return createFileError(F: *FileOrErr, EC);
977 }
978 Buffers.emplace_back(Args: std::move(*ImageOrError));
979
980 return std::move(Buffers);
981}
982
983/// Transforms the input \p Images into the binary format the runtime expects
984/// for the given \p Kind.
985Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
986bundleLinkedOutput(ArrayRef<OffloadingImage> Images, const ArgList &Args,
987 OffloadKind Kind) {
988 llvm::TimeTraceScope TimeScope("Bundle linked output");
989 switch (Kind) {
990 case OFK_OpenMP:
991 case OFK_OpenACC:
992 return (Verbose && SaveTemps) ? bundleOmpAccVerbose(Images)
993 : bundleOmpAcc(Images);
994 case OFK_SYCL:
995 return bundleSYCL(Images);
996 case OFK_Cuda:
997 return bundleCuda(Images, Args);
998 case OFK_HIP:
999 return bundleHIP(Images, Args);
1000 default:
1001 return createStringError(S: getOffloadKindName(Name: Kind) +
1002 " bundling is not supported");
1003 }
1004}
1005
1006/// Returns a new ArgList containing arguments used for the device linking
1007/// phase.
1008DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
1009 const InputArgList &Args) {
1010 DerivedArgList DAL(Args);
1011 for (Arg *A : Args)
1012 DAL.append(A);
1013
1014 // Set the subarchitecture and target triple for this compilation.
1015 const OptTable &Tbl = getOptTable();
1016 StringRef Arch = Args.MakeArgString(Str: Input.front().getBinary()->getArch());
1017 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_arch_EQ),
1018 Value: Arch == "generic" ? "" : Arch);
1019 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_triple_EQ),
1020 Value: Args.MakeArgString(Str: Input.front().getBinary()->getTriple()));
1021
1022 // If every input file is bitcode we have whole program visibility as we
1023 // do only support static linking with bitcode.
1024 auto ContainsBitcode = [](const OffloadFile &F) {
1025 return identify_magic(magic: F.getBinary()->getImage()) == file_magic::bitcode;
1026 };
1027 if (llvm::all_of(Range&: Input, P: ContainsBitcode))
1028 DAL.AddFlagArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_whole_program));
1029
1030 llvm::Triple CurrentTT(DAL.getLastArgValue(Id: OPT_triple_EQ));
1031
1032 // Forward '-Xoffload-linker' options to the appropriate backend.
1033 for (StringRef Arg : Args.getAllArgValues(Id: OPT_device_linker_args_EQ)) {
1034 auto [Triple, Value] = Arg.split(Separator: '=');
1035 llvm::Triple TT(Triple);
1036 // If this isn't a recognized triple then it's an `arg=value` option.
1037 if (TT.getArch() == Triple::ArchType::UnknownArch)
1038 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_linker_arg_EQ),
1039 Value: Args.MakeArgString(Str: Arg));
1040 else if (Value.empty())
1041 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_linker_arg_EQ),
1042 Value: Args.MakeArgString(Str: Triple));
1043 else if (TT.isCompatibleWith(Other: CurrentTT))
1044 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_linker_arg_EQ),
1045 Value: Args.MakeArgString(Str: Value));
1046 }
1047
1048 // Forward '-Xoffload-compiler' options to the appropriate backend.
1049 for (StringRef Arg : Args.getAllArgValues(Id: OPT_device_compiler_args_EQ)) {
1050 auto [Triple, Value] = Arg.split(Separator: '=');
1051 llvm::Triple TT(Triple);
1052 // If this isn't a recognized triple then it's an `arg=value` option.
1053 if (TT.getArch() == Triple::ArchType::UnknownArch)
1054 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_compiler_arg_EQ),
1055 Value: Args.MakeArgString(Str: Arg));
1056 else if (Value.empty())
1057 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_compiler_arg_EQ),
1058 Value: Args.MakeArgString(Str: Triple));
1059 else if (TT.isCompatibleWith(Other: CurrentTT))
1060 DAL.AddJoinedArg(BaseArg: nullptr, Opt: Tbl.getOption(Opt: OPT_compiler_arg_EQ),
1061 Value: Args.MakeArgString(Str: Value));
1062 }
1063
1064 return DAL;
1065}
1066
1067Error handleOverrideImages(
1068 const InputArgList &Args,
1069 MapVector<OffloadKind, SmallVector<OffloadingImage, 0>> &Images) {
1070 for (StringRef Arg : Args.getAllArgValues(Id: OPT_override_image)) {
1071 OffloadKind Kind = getOffloadKind(Name: Arg.split(Separator: "=").first);
1072 StringRef Filename = Arg.split(Separator: "=").second;
1073
1074 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1075 MemoryBuffer::getFileOrSTDIN(Filename);
1076 if (std::error_code EC = BufferOrErr.getError())
1077 return createFileError(F: Filename, EC);
1078
1079 Expected<std::unique_ptr<ObjectFile>> ElfOrErr =
1080 ObjectFile::createELFObjectFile(Object: **BufferOrErr,
1081 /*InitContent=*/false);
1082 if (!ElfOrErr)
1083 return ElfOrErr.takeError();
1084 ObjectFile &Elf = **ElfOrErr;
1085
1086 OffloadingImage TheImage{};
1087 TheImage.TheImageKind = IMG_Object;
1088 TheImage.TheOffloadKind = Kind;
1089 TheImage.StringData["triple"] =
1090 Args.MakeArgString(Str: Elf.makeTriple().getTriple());
1091 if (std::optional<StringRef> CPU = Elf.tryGetCPUName())
1092 TheImage.StringData["arch"] = Args.MakeArgString(Str: *CPU);
1093 TheImage.Image = std::move(*BufferOrErr);
1094
1095 Images[Kind].emplace_back(Args: std::move(TheImage));
1096 }
1097 return Error::success();
1098}
1099
1100/// Transforms all the extracted offloading input files into an image that can
1101/// be registered by the runtime. If NeedsWrapping is false, writes bundled
1102/// output directly without wrapping or host linking.
1103Expected<SmallVector<StringRef>>
1104linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
1105 const InputArgList &Args, char **Argv, int Argc,
1106 bool NeedsWrapping) {
1107 llvm::TimeTraceScope TimeScope("Handle all device input");
1108
1109 std::mutex ImageMtx;
1110 MapVector<OffloadKind, SmallVector<OffloadingImage, 0>> Images;
1111
1112 // Initialize the images with any overriding inputs.
1113 if (Args.hasArg(Ids: OPT_override_image))
1114 if (Error Err = handleOverrideImages(Args, Images))
1115 return std::move(Err);
1116
1117 auto Err = parallelForEachError(R&: LinkerInputFiles, Fn: [&](auto &Input) -> Error {
1118 llvm::TimeTraceScope TimeScope("Link device input");
1119
1120 // Each thread needs its own copy of the base arguments to maintain
1121 // per-device argument storage of synthetic strings.
1122 const OptTable &Tbl = getOptTable();
1123 BumpPtrAllocator Alloc;
1124 StringSaver Saver(Alloc);
1125 auto BaseArgs =
1126 Tbl.parseArgs(Argc, Argv, Unknown: OPT_INVALID, Saver, ErrorFn: [](StringRef Err) {
1127 reportError(E: createStringError(S: Err));
1128 });
1129 auto LinkerArgs = getLinkerArgs(Input, BaseArgs);
1130
1131 uint16_t ActiveOffloadKindMask = 0u;
1132 for (const auto &File : Input)
1133 ActiveOffloadKindMask |= File.getBinary()->getOffloadKind();
1134
1135 // Linking images of SYCL offload kind with images of other kind is not
1136 // supported.
1137 // TODO: Remove the above limitation.
1138 if ((ActiveOffloadKindMask & OFK_SYCL) &&
1139 ((ActiveOffloadKindMask ^ OFK_SYCL) != 0))
1140 return createStringError(Fmt: "Linking images of SYCL offload kind with "
1141 "images of any other kind is not supported");
1142
1143 // Write any remaining device inputs to an output file.
1144 SmallVector<StringRef> InputFiles;
1145 for (const OffloadFile &File : Input) {
1146 auto FileNameOrErr = writeOffloadFile(File);
1147 if (!FileNameOrErr)
1148 return FileNameOrErr.takeError();
1149 InputFiles.emplace_back(Args&: *FileNameOrErr);
1150 }
1151
1152 // Link the remaining device files using the device linker.
1153 auto OutputOrErr =
1154 linkDevice(InputFiles, LinkerArgs, ActiveOffloadKindMask);
1155 if (!OutputOrErr)
1156 return OutputOrErr.takeError();
1157
1158 // Store the offloading image for each linked output file.
1159 for (OffloadKind Kind = OFK_FIRST; Kind != OFK_LAST;
1160 Kind = static_cast<OffloadKind>((uint16_t)(Kind) << 1)) {
1161 if ((ActiveOffloadKindMask & Kind) == 0)
1162 continue;
1163 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
1164 llvm::MemoryBuffer::getFileOrSTDIN(Filename: *OutputOrErr);
1165 if (std::error_code EC = FileOrErr.getError()) {
1166 if (DryRun)
1167 FileOrErr = MemoryBuffer::getMemBuffer("", *OutputOrErr);
1168 else
1169 return createFileError(*OutputOrErr, EC);
1170 }
1171
1172 // Manually containerize offloading images not in ELF format.
1173 if (Error E = containerizeRawImage(*FileOrErr, Kind, LinkerArgs))
1174 return E;
1175
1176 std::scoped_lock<decltype(ImageMtx)> Guard(ImageMtx);
1177 OffloadingImage TheImage{};
1178 TheImage.TheImageKind =
1179 Args.hasArg(Ids: OPT_embed_bitcode) ? IMG_Bitcode : IMG_Object;
1180 TheImage.TheOffloadKind = Kind;
1181 TheImage.StringData["triple"] =
1182 Args.MakeArgString(Str: LinkerArgs.getLastArgValue(OPT_triple_EQ));
1183 TheImage.StringData["arch"] =
1184 Args.MakeArgString(Str: LinkerArgs.getLastArgValue(OPT_arch_EQ));
1185 TheImage.Image = std::move(*FileOrErr);
1186
1187 Images[Kind].emplace_back(Args: std::move(TheImage));
1188 }
1189 return Error::success();
1190 });
1191 if (Err)
1192 return std::move(Err);
1193
1194 // Create a binary image of each offloading image and either embed it into a
1195 // new object file, or if all inputs were direct offload binaries, emit the
1196 // fat binary directly (e.g. .hipfb / .fatbin).
1197 SmallVector<StringRef> WrappedOutput;
1198 for (auto &[Kind, Input] : Images) {
1199 OffloadKind WrapperKind = usesLLVMOffloadWrapper(Images: Input) ? OFK_OpenMP : Kind;
1200 // We sort the entries before bundling so they appear in a deterministic
1201 // order in the final binary.
1202 llvm::sort(C&: Input, Comp: [](OffloadingImage &A, OffloadingImage &B) {
1203 StringRef TripleA = A.StringData.lookup(Key: "triple");
1204 StringRef TripleB = B.StringData.lookup(Key: "triple");
1205 StringRef ArchA = A.StringData.lookup(Key: "arch");
1206 StringRef ArchB = B.StringData.lookup(Key: "arch");
1207 if (TripleA != TripleB)
1208 return TripleA > TripleB;
1209 if (ArchA != ArchB)
1210 return ArchA > ArchB;
1211 return A.TheOffloadKind < B.TheOffloadKind;
1212 });
1213 auto BundledImagesOrErr = bundleLinkedOutput(Images: Input, Args, Kind: WrapperKind);
1214 if (!BundledImagesOrErr)
1215 return BundledImagesOrErr.takeError();
1216
1217 if (!NeedsWrapping) {
1218 if (BundledImagesOrErr->size() != 1)
1219 return createStringError(
1220 Fmt: "Expected a single bundled image for direct fat binary output");
1221
1222 Expected<std::unique_ptr<FileOutputBuffer>> FOBOrErr =
1223 FileOutputBuffer::create(
1224 FilePath: ExecutableName, Size: BundledImagesOrErr->front()->getBufferSize());
1225 if (!FOBOrErr)
1226 return FOBOrErr.takeError();
1227 std::unique_ptr<FileOutputBuffer> FOB = std::move(*FOBOrErr);
1228 llvm::copy(Range: BundledImagesOrErr->front()->getBuffer(),
1229 Out: FOB->getBufferStart());
1230 if (Error E = FOB->commit())
1231 return std::move(E);
1232
1233 continue;
1234 }
1235
1236 auto OutputOrErr = wrapDeviceImages(Buffers: *BundledImagesOrErr, Args, Kind: WrapperKind);
1237 if (!OutputOrErr)
1238 return OutputOrErr.takeError();
1239 WrappedOutput.push_back(Elt: *OutputOrErr);
1240 }
1241
1242 return WrappedOutput;
1243}
1244
1245std::optional<std::string> findFile(StringRef Dir, StringRef Root,
1246 const Twine &Name) {
1247 SmallString<128> Path;
1248 if (Dir.starts_with(Prefix: "="))
1249 sys::path::append(path&: Path, a: Root, b: Dir.substr(Start: 1), c: Name);
1250 else
1251 sys::path::append(path&: Path, a: Dir, b: Name);
1252
1253 if (sys::fs::exists(Path))
1254 return static_cast<std::string>(Path);
1255 return std::nullopt;
1256}
1257
1258std::optional<std::string>
1259findFromSearchPaths(StringRef Name, StringRef Root,
1260 ArrayRef<StringRef> SearchPaths) {
1261 for (StringRef Dir : SearchPaths)
1262 if (std::optional<std::string> File = findFile(Dir, Root, Name))
1263 return File;
1264 return std::nullopt;
1265}
1266
1267std::optional<std::string>
1268searchLibraryBaseName(StringRef Name, StringRef Root,
1269 ArrayRef<StringRef> SearchPaths, bool IsWindows) {
1270 SmallVector<std::string> Candidates;
1271 if (IsWindows)
1272 Candidates = {"lib" + Name.str() + ".dll.a", Name.str() + ".dll.a",
1273 "lib" + Name.str() + ".a", Name.str() + ".lib"};
1274 else
1275 Candidates = {"lib" + Name.str() + ".so", "lib" + Name.str() + ".a"};
1276
1277 for (StringRef Dir : SearchPaths)
1278 for (StringRef Candidate : Candidates)
1279 if (std::optional<std::string> File = findFile(Dir, Root, Name: Candidate))
1280 return File;
1281 return std::nullopt;
1282}
1283
1284/// Search for static libraries in the linker's library path given input like
1285/// `-lfoo` or `-l:libfoo.a`.
1286std::optional<std::string> searchLibrary(StringRef Input, StringRef Root,
1287 ArrayRef<StringRef> SearchPaths,
1288 bool IsWindows) {
1289 if (Input.starts_with(Prefix: ":"))
1290 return findFromSearchPaths(Name: Input.drop_front(), Root, SearchPaths);
1291 if (Input.ends_with(Suffix: ".lib"))
1292 return findFromSearchPaths(Name: Input, Root, SearchPaths);
1293 return searchLibraryBaseName(Name: Input, Root, SearchPaths, IsWindows);
1294}
1295
1296/// Search for an input file given by name, e.g. `foo.lib`. COFF linkers use
1297/// this in place of `-lfoo` and look it up in \p SearchPaths.
1298std::optional<std::string> searchInput(StringRef Input, StringRef Root,
1299 ArrayRef<StringRef> SearchPaths) {
1300 if (sys::fs::exists(Path: Input))
1301 return std::string(Input);
1302 return findFromSearchPaths(Name: Input, Root, SearchPaths);
1303}
1304
1305/// In verbose mode we need to replay the extracted files so the user can
1306/// reproduce the generated. This only prints the steps that would result in the
1307/// same output files given the input.
1308Error emitExtractCommands(
1309 ArrayRef<SmallVector<OffloadFile>> InputsForTarget,
1310 const DenseMap<StringRef, StringRef> &SourceForImage) {
1311 Expected<std::string> OffloadBinaryPath = findProgram(
1312 Name: "llvm-offload-binary", Paths: {getExecutableDir(Name: "llvm-offload-binary")});
1313 if (!OffloadBinaryPath)
1314 return OffloadBinaryPath.takeError();
1315
1316 BumpPtrAllocator Alloc;
1317 StringSaver Saver(Alloc);
1318 MapVector<StringRef, SmallVector<StringRef>> Commands;
1319 DenseSet<StringRef> Seen;
1320 for (const auto &Input : InputsForTarget) {
1321 for (const OffloadFile &File : Input) {
1322 const OffloadBinary &Binary = *File.getBinary();
1323 StringRef Identifier = Binary.getMemoryBufferRef().getBufferIdentifier();
1324 StringRef Source = SourceForImage.lookup(Val: Identifier);
1325 if (Source.empty())
1326 Source = Identifier;
1327
1328 auto OutputOrErr =
1329 createOutputFile(Prefix: sys::path::stem(path: Identifier) + "-" +
1330 Binary.getTriple() + "-" + Binary.getArch(),
1331 Extension: "o");
1332 if (!OutputOrErr)
1333 return OutputOrErr.takeError();
1334
1335 std::string ImageArg =
1336 ("--image=kind=" + getOffloadKindName(Name: Binary.getOffloadKind()) +
1337 ",triple=" + Binary.getTriple())
1338 .str();
1339 if (!Binary.getArch().empty())
1340 ImageArg += (",arch=" + Binary.getArch()).str();
1341 file_magic Magic;
1342 if (!identify_magic(path: Source, result&: Magic) && Magic == file_magic::archive)
1343 ImageArg += (",member=" + sys::path::filename(path: Identifier)).str();
1344 ImageArg += (",file=" + *OutputOrErr).str();
1345
1346 // Shared images only need to be extracted once per source.
1347 StringRef SavedImage = Saver.save(S: ImageArg);
1348 if (!Seen.insert(V: Saver.save(S: Source + "\x01" + SavedImage)).second)
1349 continue;
1350 Commands[Source].push_back(Elt: SavedImage);
1351 }
1352 }
1353
1354 for (const auto &[Source, Images] : Commands) {
1355 SmallVector<StringRef> CmdArgs = {*OffloadBinaryPath, Source};
1356 llvm::append_range(C&: CmdArgs, R: Images);
1357 printCommands(CmdArgs);
1358 }
1359 return Error::success();
1360}
1361
1362/// Search the input files and libraries for embedded device offloading code
1363/// and add it to the list of files to be linked. Files coming from static
1364/// libraries are only added to the input if they are used by an existing
1365/// input file. Returns a list of input files intended for a single linking job.
1366Expected<SmallVector<SmallVector<OffloadFile>>>
1367getDeviceInput(const ArgList &Args) {
1368 llvm::TimeTraceScope TimeScope("ExtractDeviceCode");
1369
1370 // Skip all the input if the user is overriding the output.
1371 if (Args.hasArg(Ids: OPT_override_image))
1372 return SmallVector<SmallVector<OffloadFile>>();
1373
1374 const llvm::Triple HostTriple(
1375 Args.getLastArgValue(Id: OPT_host_triple_EQ, Default: sys::getDefaultTargetTriple()));
1376
1377 StringRef Root = Args.getLastArgValue(Id: OPT_sysroot_EQ);
1378 SmallVector<StringRef> LibraryPaths;
1379 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_library_path, Ids: OPT_libpath))
1380 LibraryPaths.push_back(Elt: Arg->getValue());
1381
1382 // Only `link.exe` style linkers search for their input files.
1383 SmallVector<StringRef> InputPaths;
1384 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_libpath))
1385 InputPaths.push_back(Elt: Arg->getValue());
1386
1387 BumpPtrAllocator Alloc;
1388 StringSaver Saver(Alloc);
1389
1390 // Try to extract device code from the linker input files.
1391 bool WholeArchive = Args.hasArg(Ids: OPT_wholearchive_flag);
1392 SmallVector<OffloadFile> ObjectFilesToExtract;
1393 SmallVector<OffloadFile> ArchiveFilesToExtract;
1394 DenseMap<StringRef, StringRef> SourceForImage;
1395 for (const opt::Arg *Arg :
1396 Args.filtered(Ids: OPT_INPUT, Ids: OPT_library, Ids: OPT_wholearchive_file,
1397 Ids: OPT_whole_archive, Ids: OPT_no_whole_archive)) {
1398 if (Arg->getOption().matches(ID: OPT_whole_archive) ||
1399 Arg->getOption().matches(ID: OPT_no_whole_archive)) {
1400 WholeArchive = Arg->getOption().matches(ID: OPT_whole_archive);
1401 continue;
1402 }
1403
1404 std::optional<std::string> Filename =
1405 Arg->getOption().matches(ID: OPT_library)
1406 ? searchLibrary(Input: Arg->getValue(), Root, SearchPaths: LibraryPaths,
1407 IsWindows: HostTriple.isOSWindows())
1408 : searchInput(Input: Arg->getValue(), Root, SearchPaths: InputPaths);
1409
1410 if (!Filename && Arg->getOption().matches(ID: OPT_library))
1411 return createStringError(Fmt: "unable to find library -l%s", Vals: Arg->getValue());
1412
1413 if (!Filename || !sys::fs::exists(Path: *Filename) ||
1414 sys::fs::is_directory(Path: *Filename))
1415 continue;
1416
1417 // Unlike `--whole-archive`, `/wholearchive:` applies to a single library.
1418 bool ExtractWholeArchive =
1419 WholeArchive || Arg->getOption().matches(ID: OPT_wholearchive_file);
1420
1421 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1422 MemoryBuffer::getFileOrSTDIN(Filename: *Filename);
1423 if (std::error_code EC = BufferOrErr.getError())
1424 return createFileError(F: *Filename, EC);
1425
1426 MemoryBufferRef Buffer = **BufferOrErr;
1427 if (identify_magic(magic: Buffer.getBuffer()) == file_magic::elf_shared_object)
1428 continue;
1429
1430 SmallVector<OffloadFile> Binaries;
1431 if (Error Err = extractOffloadBinaries(Buffer, Binaries))
1432 return std::move(Err);
1433
1434 for (auto &Binary : Binaries) {
1435 if (Verbose && SaveTemps)
1436 SourceForImage.try_emplace(
1437 Key: Binary.getBinary()->getMemoryBufferRef().getBufferIdentifier(),
1438 Args: Saver.save(S: StringRef(*Filename)));
1439 if (identify_magic(magic: Buffer.getBuffer()) == file_magic::archive &&
1440 !ExtractWholeArchive)
1441 ArchiveFilesToExtract.emplace_back(Args: std::move(Binary));
1442 else
1443 ObjectFilesToExtract.emplace_back(Args: std::move(Binary));
1444 }
1445 }
1446
1447 // Handle the most specific target-ids first so a generic input merges last.
1448 llvm::stable_sort(Range&: ObjectFilesToExtract,
1449 C: [](const OffloadFile &A, const OffloadFile &B) {
1450 return A.getBinary()->getArch().count(C: ':') >
1451 B.getBinary()->getArch().count(C: ':');
1452 });
1453
1454 // Link all standard input files and update the list of symbols.
1455 MapVector<OffloadFile::TargetID, SmallVector<OffloadFile, 0>> InputFiles;
1456 for (OffloadFile &Binary : ObjectFilesToExtract) {
1457 if (!Binary.getBinary())
1458 continue;
1459
1460 OffloadFile::TargetID Target = Binary;
1461 SmallVector<OffloadFile::TargetID> CompatibleTargets;
1462 for (const auto &[ID, Input] : InputFiles)
1463 if (object::areTargetsEquivalent(LHS: Target, RHS: ID))
1464 CompatibleTargets.emplace_back(Args: ID);
1465
1466 // Seed a new image when no existing target can provide for this input.
1467 if (CompatibleTargets.empty())
1468 CompatibleTargets.emplace_back(Args&: Target);
1469
1470 for (const auto &[Index, ID] : llvm::enumerate(First&: CompatibleTargets)) {
1471 // If another target needs this binary it must be copied instead.
1472 if (Index == CompatibleTargets.size() - 1)
1473 InputFiles[ID].emplace_back(Args: std::move(Binary));
1474 else
1475 InputFiles[ID].emplace_back(Args: Binary.copy());
1476 }
1477 }
1478
1479 llvm::DenseSet<StringRef> ShouldExtract;
1480 for (StringRef Arg : Args.getAllArgValues(Id: OPT_should_extract))
1481 ShouldExtract.insert(V: Saver.save(S: Arg));
1482
1483 // We only extract archive members from the fat binary if we find a used or
1484 // requested target. Unlike normal static archive handling, we just extract
1485 // every object file contained in the archive.
1486 for (OffloadFile &Binary : ArchiveFilesToExtract) {
1487 if (!Binary.getBinary())
1488 continue;
1489
1490 SmallVector<OffloadFile::TargetID> CompatibleTargets = {Binary};
1491 for (const auto &[ID, Input] : InputFiles)
1492 if (OffloadFile::TargetID(Binary) != ID &&
1493 object::areTargetsCompatible(Provided: Binary, Requested: ID))
1494 CompatibleTargets.emplace_back(Args: ID);
1495
1496 for (const auto &[Index, ID] : llvm::enumerate(First&: CompatibleTargets)) {
1497 // Only extract if we have an object matching this target or it
1498 // was specifically requested.
1499 if (!InputFiles.count(Key: ID) && !ShouldExtract.contains(V: ID.second))
1500 continue;
1501
1502 // If another target needs this binary it must be copied instead.
1503 if (Index == CompatibleTargets.size() - 1)
1504 InputFiles[ID].emplace_back(Args: std::move(Binary));
1505 else
1506 InputFiles[ID].emplace_back(Args: Binary.copy());
1507 }
1508 }
1509
1510 SmallVector<SmallVector<OffloadFile>> InputsForTarget;
1511 for (auto &[ID, Input] : InputFiles)
1512 InputsForTarget.emplace_back(Args: std::move(Input));
1513
1514 if (Verbose && SaveTemps)
1515 if (Error Err = emitExtractCommands(InputsForTarget, SourceForImage))
1516 return std::move(Err);
1517
1518 return std::move(InputsForTarget);
1519}
1520
1521} // namespace
1522
1523int main(int Argc, char **Argv) {
1524 InitLLVM X(Argc, Argv);
1525 InitializeAllTargetInfos();
1526 InitializeAllTargets();
1527 InitializeAllTargetMCs();
1528 InitializeAllAsmParsers();
1529 InitializeAllAsmPrinters();
1530
1531 LinkerExecutable = Argv[0];
1532 sys::PrintStackTraceOnErrorSignal(Argv0: Argv[0]);
1533
1534 const OptTable &Tbl = getOptTable();
1535 BumpPtrAllocator Alloc;
1536 StringSaver Saver(Alloc);
1537 auto Args = Tbl.parseArgs(Argc, Argv, Unknown: OPT_INVALID, Saver, ErrorFn: [&](StringRef Err) {
1538 reportError(E: createStringError(S: Err));
1539 });
1540
1541 if (Args.hasArg(Ids: OPT_help) || Args.hasArg(Ids: OPT_help_hidden)) {
1542 Tbl.printHelp(
1543 OS&: outs(),
1544 Usage: "clang-linker-wrapper [options] -- <options to pass to the linker>",
1545 Title: "\nA wrapper utility over the host linker. It scans the input files\n"
1546 "for sections that require additional processing prior to linking.\n"
1547 "It will then transparently pass all arguments and input to the\n"
1548 "specified host linker to create the final binary.\n",
1549 ShowHidden: Args.hasArg(Ids: OPT_help_hidden), ShowAllAliases: Args.hasArg(Ids: OPT_help_hidden));
1550 return EXIT_SUCCESS;
1551 }
1552 if (Args.hasArg(Ids: OPT_version)) {
1553 printVersion(OS&: outs());
1554 return EXIT_SUCCESS;
1555 }
1556
1557 // This forwards '-mllvm' arguments to LLVM if present.
1558 SmallVector<const char *> NewArgv = {Argv[0]};
1559 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_mllvm))
1560 NewArgv.push_back(Elt: Arg->getValue());
1561 for (const opt::Arg *Arg : Args.filtered(Ids: OPT_offload_opt_eq_minus))
1562 NewArgv.push_back(Elt: Arg->getValue());
1563 cl::LibraryOptionsContext LibraryOptions;
1564 cl::parseCommandLineOptions(Context&: LibraryOptions, Args: NewArgv);
1565
1566 Verbose = Args.hasArg(Ids: OPT_verbose);
1567 DryRun = Args.hasArg(Ids: OPT_dry_run);
1568 SaveTemps = Args.hasArg(Ids: OPT_save_temps);
1569 CudaBinaryPath = Args.getLastArgValue(Id: OPT_cuda_path_EQ).str();
1570 CanonicalPrefixes = !Args.hasArg(Ids: OPT_no_canonical_prefixes);
1571
1572 llvm::Triple Triple(
1573 Args.getLastArgValue(Id: OPT_host_triple_EQ, Default: sys::getDefaultTargetTriple()));
1574 if (Args.hasArg(Ids: OPT_o))
1575 ExecutableName = Args.getLastArgValue(Id: OPT_o, Default: "a.out");
1576 else if (Args.hasArg(Ids: OPT_out))
1577 ExecutableName = Args.getLastArgValue(Id: OPT_out, Default: "a.exe");
1578 else
1579 ExecutableName = Triple.isOSWindows() ? "a.exe" : "a.out";
1580
1581 parallel::strategy = hardware_concurrency(ThreadCount: 1);
1582 if (auto *Arg = Args.getLastArg(Ids: OPT_wrapper_jobs)) {
1583 StringRef Val = Arg->getValue();
1584 if (Val.equals_insensitive(RHS: "jobserver"))
1585 parallel::strategy = jobserver_concurrency();
1586 else {
1587 unsigned Threads = 0;
1588 if (!llvm::to_integer(S: Val, Num&: Threads) || Threads == 0)
1589 reportError(E: createStringError(
1590 Fmt: "%s: expected a positive integer or 'jobserver', got '%s'",
1591 Vals: Arg->getSpelling().data(), Vals: Val.data()));
1592 else
1593 parallel::strategy = hardware_concurrency(ThreadCount: Threads);
1594 }
1595 }
1596
1597 if (Args.hasArg(Ids: OPT_wrapper_time_trace_eq)) {
1598 unsigned Granularity;
1599 if (Args.getLastArgValue(Id: OPT_wrapper_time_trace_granularity, Default: "500")
1600 .getAsInteger(Radix: 10, Result&: Granularity))
1601 reportError(
1602 E: createStringError(Fmt: "invalid value for time trace granularity"));
1603 timeTraceProfilerInitialize(TimeTraceGranularity: Granularity, ProcName: Argv[0]);
1604 }
1605
1606 {
1607 llvm::TimeTraceScope TimeScope("Execute linker wrapper");
1608
1609 // Extract the device input files stored in the host fat binary.
1610 auto DeviceInputFiles = getDeviceInput(Args);
1611 if (!DeviceInputFiles)
1612 reportError(E: DeviceInputFiles.takeError());
1613
1614 // Check if we should emit fat binary directly without wrapping or host
1615 // linking.
1616 bool EmitFatbinOnly = Args.hasArg(Ids: OPT_emit_fatbin_only);
1617
1618 // Link and process the device images. The function may emit a direct fat
1619 // binary if --emit-fatbin-only is specified.
1620 auto FilesOrErr = linkAndWrapDeviceFiles(LinkerInputFiles: *DeviceInputFiles, Args, Argv,
1621 Argc, NeedsWrapping: !EmitFatbinOnly);
1622 if (!FilesOrErr)
1623 reportError(E: FilesOrErr.takeError());
1624
1625 // Run the host linking job with the rendered arguments.
1626 if (!EmitFatbinOnly) {
1627 if (Error Err = runLinker(Files: *FilesOrErr, Args))
1628 reportError(E: std::move(Err));
1629 }
1630 }
1631
1632 if (const opt::Arg *Arg = Args.getLastArg(Ids: OPT_wrapper_time_trace_eq)) {
1633 if (Error Err = timeTraceProfilerWrite(PreferredFileName: Arg->getValue(), FallbackFileName: ExecutableName))
1634 reportError(E: std::move(Err));
1635 timeTraceProfilerCleanup();
1636 }
1637
1638 // Remove the temporary files created.
1639 if (!SaveTemps)
1640 for (const auto &TempFile : TempFiles)
1641 if (std::error_code EC = sys::fs::remove(path: TempFile))
1642 reportError(E: createFileError(F: TempFile, EC));
1643
1644 return EXIT_SUCCESS;
1645}
1646