2021-12-31 05:41:36 +08:00
|
|
|
//===-- clang-linker-wrapper/ClangLinkerWrapper.cpp - wrapper over linker-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
2022-01-04 01:31:52 +08:00
|
|
|
//
|
|
|
|
// This tool works as a wrapper over a linking job. This tool is used to create
|
|
|
|
// linked device images for offloading. It scans the linker's input for embedded
|
|
|
|
// device offloading data stored in sections `.llvm.offloading.<triple>.<arch>`
|
|
|
|
// and extracts it as a temporary file. The extracted device files will then be
|
|
|
|
// passed to a device linking job to create a final device image.
|
|
|
|
//
|
2021-12-31 05:41:36 +08:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2022-01-26 06:46:01 +08:00
|
|
|
#include "OffloadWrapper.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2022-01-08 06:12:51 +08:00
|
|
|
#include "llvm/CodeGen/CommandFlags.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2022-01-08 06:12:51 +08:00
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2022-01-08 06:12:51 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2022-01-26 03:25:39 +08:00
|
|
|
#include "llvm/MC/TargetRegistry.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
|
|
|
#include "llvm/Object/Binary.h"
|
2022-06-06 23:36:45 +08:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2022-03-25 23:03:02 +08:00
|
|
|
#include "llvm/Object/OffloadBinary.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2022-01-05 06:20:04 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2022-01-04 01:31:52 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2022-01-08 06:12:51 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2022-01-26 03:25:39 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2021-12-31 05:41:36 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2022-01-04 01:31:52 +08:00
|
|
|
using namespace llvm::object;
|
2021-12-31 05:41:36 +08:00
|
|
|
|
|
|
|
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
|
|
|
|
|
2022-01-13 05:14:52 +08:00
|
|
|
enum DebugKind {
|
|
|
|
NoDebugInfo,
|
|
|
|
DirectivesOnly,
|
|
|
|
FullDebugInfo,
|
|
|
|
};
|
|
|
|
|
2021-12-31 05:41:36 +08:00
|
|
|
// Mark all our options with this category, everything else (except for -help)
|
|
|
|
// will be hidden.
|
|
|
|
static cl::OptionCategory
|
|
|
|
ClangLinkerWrapperCategory("clang-linker-wrapper options");
|
|
|
|
|
2022-01-13 05:14:52 +08:00
|
|
|
static cl::opt<std::string> LinkerUserPath("linker-path", cl::Required,
|
2021-12-31 05:41:36 +08:00
|
|
|
cl::desc("Path of linker binary"),
|
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
2022-01-08 06:12:51 +08:00
|
|
|
static cl::opt<std::string>
|
2022-06-06 23:36:45 +08:00
|
|
|
TargetFeatures("target-feature", cl::desc("Target features for triple"),
|
2022-01-08 06:12:51 +08:00
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
2022-06-04 13:02:11 +08:00
|
|
|
static cl::opt<std::string> OptLevel("opt-level",
|
2022-01-08 06:12:51 +08:00
|
|
|
cl::desc("Optimization level for LTO"),
|
2022-01-11 23:53:59 +08:00
|
|
|
cl::init("O2"),
|
2022-01-08 06:12:51 +08:00
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
2022-01-26 00:23:27 +08:00
|
|
|
static cl::list<std::string>
|
2022-06-05 14:51:12 +08:00
|
|
|
BitcodeLibraries("target-library",
|
2022-01-26 00:23:27 +08:00
|
|
|
cl::desc("Path for the target bitcode library"),
|
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
2022-01-11 23:53:59 +08:00
|
|
|
|
2022-01-16 12:10:52 +08:00
|
|
|
static cl::opt<bool> EmbedBitcode(
|
2022-06-04 13:02:11 +08:00
|
|
|
"target-embed-bc",
|
2022-01-13 05:14:52 +08:00
|
|
|
cl::desc("Embed linked bitcode instead of an executable device image"),
|
2022-06-04 13:02:11 +08:00
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
2022-01-12 04:50:39 +08:00
|
|
|
|
2022-04-20 02:14:16 +08:00
|
|
|
static cl::opt<bool> DryRun(
|
2022-06-04 13:02:11 +08:00
|
|
|
"dry-run",
|
2022-04-20 02:14:16 +08:00
|
|
|
cl::desc("List the linker commands to be run without executing them"),
|
2022-06-04 13:02:11 +08:00
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
2022-04-20 02:14:16 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
2022-06-04 13:02:11 +08:00
|
|
|
PrintWrappedModule("print-wrapped-module",
|
2022-04-20 02:14:16 +08:00
|
|
|
cl::desc("Print the wrapped module's IR for testing"),
|
2022-06-04 13:02:11 +08:00
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
2022-04-20 02:14:16 +08:00
|
|
|
|
2022-01-13 05:14:52 +08:00
|
|
|
static cl::opt<std::string>
|
2022-06-04 13:02:11 +08:00
|
|
|
HostTriple("host-triple",
|
2022-01-13 05:14:52 +08:00
|
|
|
cl::desc("Triple to use for the host compilation"),
|
|
|
|
cl::init(sys::getDefaultTargetTriple()),
|
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
2022-01-26 00:23:27 +08:00
|
|
|
static cl::list<std::string>
|
2022-06-05 14:51:12 +08:00
|
|
|
PtxasArgs("ptxas-args",
|
2022-02-04 05:41:47 +08:00
|
|
|
cl::desc("Argument to pass to the ptxas invocation"),
|
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
2022-01-13 05:14:52 +08:00
|
|
|
|
2022-05-24 05:51:30 +08:00
|
|
|
static cl::list<std::string>
|
2022-06-05 16:07:50 +08:00
|
|
|
LinkerArgs("device-linker",
|
2022-05-24 05:51:30 +08:00
|
|
|
cl::desc("Arguments to pass to the device linker invocation"),
|
|
|
|
cl::value_desc("<value> or <triple>=<value>"),
|
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
static cl::opt<bool> Verbose("v", cl::desc("Verbose output from tools"),
|
|
|
|
|
2022-01-13 05:14:52 +08:00
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
|
|
|
static cl::opt<DebugKind> DebugInfo(
|
|
|
|
cl::desc("Choose debugging level:"), cl::init(NoDebugInfo),
|
|
|
|
cl::values(clEnumValN(NoDebugInfo, "g0", "No debug information"),
|
|
|
|
clEnumValN(DirectivesOnly, "gline-directives-only",
|
|
|
|
"Direction information"),
|
|
|
|
clEnumValN(FullDebugInfo, "g", "Full debugging support")));
|
|
|
|
|
2022-06-04 13:02:11 +08:00
|
|
|
static cl::opt<bool> SaveTemps("save-temps",
|
2022-01-17 05:06:59 +08:00
|
|
|
cl::desc("Save intermediary results."),
|
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
2022-06-04 13:02:11 +08:00
|
|
|
static cl::opt<std::string> CudaPath("cuda-path",
|
2022-02-04 05:41:47 +08:00
|
|
|
cl::desc("Save intermediary results."),
|
|
|
|
cl::cat(ClangLinkerWrapperCategory));
|
|
|
|
|
2022-01-04 01:31:52 +08:00
|
|
|
// Do not parse linker options.
|
2021-12-31 05:41:36 +08:00
|
|
|
static cl::list<std::string>
|
2022-01-12 04:50:39 +08:00
|
|
|
HostLinkerArgs(cl::Positional,
|
|
|
|
cl::desc("<options to be passed to linker>..."));
|
2021-12-31 05:41:36 +08:00
|
|
|
|
2022-01-04 01:31:52 +08:00
|
|
|
/// Path of the current binary.
|
2022-01-12 04:50:39 +08:00
|
|
|
static const char *LinkerExecutable;
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-01-18 23:56:12 +08:00
|
|
|
/// Filename of the executable being created.
|
|
|
|
static StringRef ExecutableName;
|
|
|
|
|
2022-03-03 01:38:29 +08:00
|
|
|
/// System root if passed in to the linker via. '--sysroot='.
|
|
|
|
static StringRef Sysroot = "";
|
|
|
|
|
2022-02-04 05:41:47 +08:00
|
|
|
/// Binary path for the CUDA installation.
|
|
|
|
static std::string CudaBinaryPath;
|
|
|
|
|
2022-01-06 02:21:03 +08:00
|
|
|
/// Temporary files created by the linker wrapper.
|
2022-01-05 06:20:04 +08:00
|
|
|
static SmallVector<std::string, 16> TempFiles;
|
2022-01-06 02:21:03 +08:00
|
|
|
|
2022-01-08 06:12:51 +08:00
|
|
|
/// Codegen flags for LTO backend.
|
|
|
|
static codegen::RegisterCodeGenFlags CodeGenFlags;
|
|
|
|
|
2022-01-04 01:31:52 +08:00
|
|
|
/// Magic section string that marks the existence of offloading data. The
|
2022-03-25 23:03:02 +08:00
|
|
|
/// section will contain one or more offloading binaries stored contiguously.
|
|
|
|
#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading"
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-02-22 03:39:58 +08:00
|
|
|
/// The magic offset for the first object inside CUDA's fatbinary. This can be
|
|
|
|
/// different but it should work for what is passed here.
|
|
|
|
static constexpr unsigned FatbinaryOffset = 0x50;
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
using OffloadingImage = OffloadBinary::OffloadingImage;
|
|
|
|
|
|
|
|
/// A class to contain the binary information for a single OffloadBinary.
|
|
|
|
class OffloadFile : public OwningBinary<OffloadBinary> {
|
|
|
|
public:
|
|
|
|
using TargetID = std::pair<StringRef, StringRef>;
|
|
|
|
|
|
|
|
OffloadFile(std::unique_ptr<OffloadBinary> Binary,
|
|
|
|
std::unique_ptr<MemoryBuffer> Buffer)
|
|
|
|
: OwningBinary<OffloadBinary>(std::move(Binary), std::move(Buffer)) {}
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
/// We use the Triple and Architecture pair to group linker inputs together.
|
|
|
|
/// This conversion function lets us use these files in a hash-map.
|
|
|
|
operator TargetID() const {
|
|
|
|
return std::make_pair(getBinary()->getTriple(), getBinary()->getArch());
|
|
|
|
}
|
2022-02-21 23:08:06 +08:00
|
|
|
};
|
2022-01-05 06:20:04 +08:00
|
|
|
|
2022-02-21 23:08:06 +08:00
|
|
|
namespace llvm {
|
2022-06-06 23:36:45 +08:00
|
|
|
// Provide DenseMapInfo so that OffloadKind can be used in a DenseMap.
|
2022-04-12 23:21:36 +08:00
|
|
|
template <> struct DenseMapInfo<OffloadKind> {
|
|
|
|
static inline OffloadKind getEmptyKey() { return OFK_LAST; }
|
|
|
|
static inline OffloadKind getTombstoneKey() {
|
|
|
|
return static_cast<OffloadKind>(OFK_LAST + 1);
|
|
|
|
}
|
2022-06-06 23:36:45 +08:00
|
|
|
static unsigned getHashValue(const OffloadKind &Val) { return Val; }
|
2022-04-12 23:21:36 +08:00
|
|
|
|
|
|
|
static bool isEqual(const OffloadKind &LHS, const OffloadKind &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
|
|
|
};
|
2022-02-21 23:08:06 +08:00
|
|
|
} // namespace llvm
|
2022-01-04 01:31:52 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-05-13 00:39:15 +08:00
|
|
|
Error extractFromBuffer(std::unique_ptr<MemoryBuffer> Buffer,
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVectorImpl<OffloadFile> &DeviceFiles);
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-03-01 02:14:54 +08:00
|
|
|
void printCommands(ArrayRef<StringRef> CmdArgs) {
|
|
|
|
if (CmdArgs.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::errs() << " \"" << CmdArgs.front() << "\" ";
|
2022-03-03 01:38:29 +08:00
|
|
|
for (auto IC = std::next(CmdArgs.begin()), IE = CmdArgs.end(); IC != IE; ++IC)
|
|
|
|
llvm::errs() << *IC << (std::next(IC) != IE ? " " : "\n");
|
2022-03-01 02:14:54 +08:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
/// Forward user requested arguments to the device linking job.
|
2022-05-24 05:51:30 +08:00
|
|
|
void renderXLinkerArgs(SmallVectorImpl<StringRef> &Args, StringRef Triple) {
|
|
|
|
for (StringRef Arg : LinkerArgs) {
|
|
|
|
auto TripleAndValue = Arg.split('=');
|
|
|
|
if (TripleAndValue.second.empty())
|
|
|
|
Args.push_back(TripleAndValue.first);
|
|
|
|
else if (TripleAndValue.first == Triple)
|
|
|
|
Args.push_back(TripleAndValue.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
/// Create an extra user-specified \p OffloadFile.
|
|
|
|
/// TODO: We should find a way to wrap these as libraries instead.
|
|
|
|
Expected<OffloadFile> getInputBitcodeLibrary(StringRef Input) {
|
|
|
|
auto DeviceAndPath = StringRef(Input).split('=');
|
|
|
|
auto StringAndArch = DeviceAndPath.first.rsplit('-');
|
|
|
|
auto KindAndTriple = StringAndArch.first.split('-');
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> ImageOrError =
|
|
|
|
llvm::MemoryBuffer::getFileOrSTDIN(DeviceAndPath.second);
|
|
|
|
if (std::error_code EC = ImageOrError.getError())
|
|
|
|
return createFileError(DeviceAndPath.second, EC);
|
|
|
|
|
|
|
|
OffloadingImage Image{};
|
|
|
|
Image.TheImageKind = IMG_Bitcode;
|
|
|
|
Image.TheOffloadKind = getOffloadKind(KindAndTriple.first);
|
|
|
|
Image.StringData = {{"triple", KindAndTriple.second},
|
|
|
|
{"arch", StringAndArch.second}};
|
|
|
|
Image.Image = std::move(*ImageOrError);
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> Binary = OffloadBinary::write(Image);
|
|
|
|
auto NewBinaryOrErr = OffloadBinary::create(*Binary);
|
|
|
|
if (!NewBinaryOrErr)
|
|
|
|
return NewBinaryOrErr.takeError();
|
|
|
|
return OffloadFile(std::move(*NewBinaryOrErr), std::move(Binary));
|
|
|
|
}
|
|
|
|
|
2022-02-08 03:59:03 +08:00
|
|
|
std::string getMainExecutable(const char *Name) {
|
|
|
|
void *Ptr = (void *)(intptr_t)&getMainExecutable;
|
|
|
|
auto COWPath = sys::fs::getMainExecutable(Name, Ptr);
|
|
|
|
return sys::path::parent_path(COWPath).str();
|
|
|
|
}
|
|
|
|
|
2022-01-26 00:23:27 +08:00
|
|
|
/// Get a temporary filename suitable for output.
|
2022-01-17 05:06:59 +08:00
|
|
|
Error createOutputFile(const Twine &Prefix, StringRef Extension,
|
|
|
|
SmallString<128> &NewFilename) {
|
|
|
|
if (!SaveTemps) {
|
|
|
|
if (std::error_code EC =
|
|
|
|
sys::fs::createTemporaryFile(Prefix, Extension, NewFilename))
|
|
|
|
return createFileError(NewFilename, EC);
|
|
|
|
TempFiles.push_back(static_cast<std::string>(NewFilename));
|
|
|
|
} else {
|
|
|
|
const Twine &Filename = Prefix + "." + Extension;
|
|
|
|
Filename.toNullTerminatedStringRef(NewFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2022-04-20 02:14:16 +08:00
|
|
|
/// Execute the command \p ExecutablePath with the arguments \p Args.
|
|
|
|
Error executeCommands(StringRef ExecutablePath, ArrayRef<StringRef> Args) {
|
|
|
|
if (Verbose || DryRun)
|
|
|
|
printCommands(Args);
|
|
|
|
|
|
|
|
if (!DryRun)
|
|
|
|
if (sys::ExecuteAndWait(ExecutablePath, Args))
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"'" + sys::path::filename(ExecutablePath) + "'" +
|
|
|
|
" failed");
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<std::string> findProgram(StringRef Name, ArrayRef<StringRef> Paths) {
|
|
|
|
|
|
|
|
ErrorOr<std::string> Path = sys::findProgramByName(Name, Paths);
|
|
|
|
if (!Path)
|
|
|
|
Path = sys::findProgramByName(Name);
|
|
|
|
if (!Path && DryRun)
|
|
|
|
return Name.str();
|
|
|
|
if (!Path)
|
|
|
|
return createStringError(Path.getError(),
|
|
|
|
"Unable to find '" + Name + "' in path");
|
|
|
|
return *Path;
|
|
|
|
}
|
|
|
|
|
2022-01-04 01:31:52 +08:00
|
|
|
Error runLinker(std::string &LinkerPath, SmallVectorImpl<std::string> &Args) {
|
2021-12-31 05:41:36 +08:00
|
|
|
std::vector<StringRef> LinkerArgs;
|
|
|
|
LinkerArgs.push_back(LinkerPath);
|
|
|
|
for (auto &Arg : Args)
|
|
|
|
LinkerArgs.push_back(Arg);
|
|
|
|
|
2022-04-20 02:14:16 +08:00
|
|
|
if (Error Err = executeCommands(LinkerPath, LinkerArgs))
|
|
|
|
return Err;
|
2021-12-31 05:41:36 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2022-01-04 01:31:52 +08:00
|
|
|
void PrintVersion(raw_ostream &OS) {
|
2021-12-31 05:41:36 +08:00
|
|
|
OS << clang::getClangToolFullVersion("clang-linker-wrapper") << '\n';
|
|
|
|
}
|
|
|
|
|
2022-03-25 23:03:02 +08:00
|
|
|
/// Attempts to extract all the embedded device images contained inside the
|
|
|
|
/// buffer \p Contents. The buffer is expected to contain a valid offloading
|
|
|
|
/// binary format.
|
2022-06-06 23:36:45 +08:00
|
|
|
Error extractOffloadFiles(MemoryBufferRef Contents,
|
|
|
|
SmallVectorImpl<OffloadFile> &DeviceFiles) {
|
2022-03-25 23:03:02 +08:00
|
|
|
uint64_t Offset = 0;
|
|
|
|
// There could be multiple offloading binaries stored at this section.
|
2022-06-06 23:36:45 +08:00
|
|
|
while (Offset < Contents.getBuffer().size()) {
|
2022-03-25 23:03:02 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buffer =
|
2022-06-06 23:36:45 +08:00
|
|
|
MemoryBuffer::getMemBuffer(Contents.getBuffer().drop_front(Offset), "",
|
2022-03-25 23:03:02 +08:00
|
|
|
/*RequiresNullTerminator*/ false);
|
|
|
|
auto BinaryOrErr = OffloadBinary::create(*Buffer);
|
|
|
|
if (!BinaryOrErr)
|
|
|
|
return BinaryOrErr.takeError();
|
|
|
|
OffloadBinary &Binary = **BinaryOrErr;
|
|
|
|
|
|
|
|
if (Binary.getVersion() != 1)
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"Incompatible device image version");
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// Create a new owned binary with a copy of the original memory.
|
|
|
|
std::unique_ptr<MemoryBuffer> BufferCopy = MemoryBuffer::getMemBufferCopy(
|
|
|
|
Binary.getData().take_front(Binary.getSize()),
|
|
|
|
Contents.getBufferIdentifier());
|
|
|
|
auto NewBinaryOrErr = OffloadBinary::create(*BufferCopy);
|
|
|
|
if (!NewBinaryOrErr)
|
|
|
|
return NewBinaryOrErr.takeError();
|
|
|
|
DeviceFiles.emplace_back(std::move(*NewBinaryOrErr), std::move(BufferCopy));
|
2022-03-25 23:03:02 +08:00
|
|
|
|
|
|
|
Offset += Binary.getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// Extract offloading binaries from an Object file \p Obj.
|
2022-05-13 00:39:15 +08:00
|
|
|
Error extractFromBinary(const ObjectFile &Obj,
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVectorImpl<OffloadFile> &DeviceFiles) {
|
2022-01-04 01:31:52 +08:00
|
|
|
for (const SectionRef &Sec : Obj.sections()) {
|
|
|
|
Expected<StringRef> Name = Sec.getName();
|
2022-03-25 23:03:02 +08:00
|
|
|
if (!Name || !Name->equals(OFFLOAD_SECTION_MAGIC_STR))
|
2022-01-04 01:31:52 +08:00
|
|
|
continue;
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
Expected<StringRef> Buffer = Sec.getContents();
|
|
|
|
if (!Buffer)
|
|
|
|
return Buffer.takeError();
|
|
|
|
|
|
|
|
MemoryBufferRef Contents(*Buffer, Obj.getFileName());
|
2022-03-25 23:03:02 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
if (Error Err = extractOffloadFiles(Contents, DeviceFiles))
|
2022-05-13 00:39:15 +08:00
|
|
|
return Err;
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
|
2022-05-13 00:39:15 +08:00
|
|
|
return Error::success();
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
|
2022-05-13 00:39:15 +08:00
|
|
|
Error extractFromBitcode(std::unique_ptr<MemoryBuffer> Buffer,
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVectorImpl<OffloadFile> &DeviceFiles) {
|
2022-01-04 01:31:52 +08:00
|
|
|
LLVMContext Context;
|
|
|
|
SMDiagnostic Err;
|
|
|
|
std::unique_ptr<Module> M = getLazyIRModule(std::move(Buffer), Err, Context);
|
|
|
|
if (!M)
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"Failed to create module");
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// Extract offloading data from globals with the `.llvm.offloading` section.
|
2022-01-04 01:31:52 +08:00
|
|
|
for (GlobalVariable &GV : M->globals()) {
|
2022-03-25 23:03:02 +08:00
|
|
|
if (!GV.hasSection() || !GV.getSection().equals(OFFLOAD_SECTION_MAGIC_STR))
|
2022-01-04 01:31:52 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
auto *CDS = dyn_cast<ConstantDataSequential>(GV.getInitializer());
|
|
|
|
if (!CDS)
|
|
|
|
continue;
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
MemoryBufferRef Contents(CDS->getAsString(), M->getName());
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
if (Error Err = extractOffloadFiles(Contents, DeviceFiles))
|
2022-05-13 00:39:15 +08:00
|
|
|
return Err;
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
|
2022-05-13 00:39:15 +08:00
|
|
|
return Error::success();
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
|
2022-05-13 00:39:15 +08:00
|
|
|
Error extractFromArchive(const Archive &Library,
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVectorImpl<OffloadFile> &DeviceFiles) {
|
2022-01-04 01:31:52 +08:00
|
|
|
// Try to extract device code from each file stored in the static archive.
|
|
|
|
Error Err = Error::success();
|
|
|
|
for (auto Child : Library.children(Err)) {
|
2022-05-12 04:53:36 +08:00
|
|
|
auto ChildBufferOrErr = Child.getMemoryBufferRef();
|
|
|
|
if (!ChildBufferOrErr)
|
|
|
|
return ChildBufferOrErr.takeError();
|
2022-01-04 01:31:52 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> ChildBuffer =
|
2022-05-12 04:53:36 +08:00
|
|
|
MemoryBuffer::getMemBuffer(*ChildBufferOrErr, false);
|
|
|
|
|
|
|
|
// Check if the buffer has the required alignment.
|
|
|
|
if (!isAddrAligned(Align(OffloadBinary::getAlignment()),
|
|
|
|
ChildBuffer->getBufferStart()))
|
|
|
|
ChildBuffer = MemoryBuffer::getMemBufferCopy(
|
|
|
|
ChildBufferOrErr->getBuffer(),
|
|
|
|
ChildBufferOrErr->getBufferIdentifier());
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-05-13 01:22:08 +08:00
|
|
|
if (Error Err = extractFromBuffer(std::move(ChildBuffer), DeviceFiles))
|
2022-05-13 00:39:15 +08:00
|
|
|
return Err;
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Err)
|
2022-05-13 00:39:15 +08:00
|
|
|
return Err;
|
|
|
|
return Error::success();
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts embedded device offloading code from a memory \p Buffer to a list
|
2022-05-13 00:39:15 +08:00
|
|
|
/// of \p DeviceFiles.
|
|
|
|
Error extractFromBuffer(std::unique_ptr<MemoryBuffer> Buffer,
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVectorImpl<OffloadFile> &DeviceFiles) {
|
2022-01-04 01:31:52 +08:00
|
|
|
file_magic Type = identify_magic(Buffer->getBuffer());
|
|
|
|
switch (Type) {
|
|
|
|
case file_magic::bitcode:
|
2022-05-13 01:22:08 +08:00
|
|
|
return extractFromBitcode(std::move(Buffer), DeviceFiles);
|
2022-01-04 01:31:52 +08:00
|
|
|
case file_magic::elf_relocatable:
|
|
|
|
case file_magic::macho_object:
|
|
|
|
case file_magic::coff_object: {
|
|
|
|
Expected<std::unique_ptr<ObjectFile>> ObjFile =
|
|
|
|
ObjectFile::createObjectFile(*Buffer, Type);
|
|
|
|
if (!ObjFile)
|
|
|
|
return ObjFile.takeError();
|
2022-05-13 01:22:08 +08:00
|
|
|
return extractFromBinary(*ObjFile->get(), DeviceFiles);
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
case file_magic::archive: {
|
|
|
|
Expected<std::unique_ptr<llvm::object::Archive>> LibFile =
|
|
|
|
object::Archive::create(*Buffer);
|
|
|
|
if (!LibFile)
|
|
|
|
return LibFile.takeError();
|
|
|
|
return extractFromArchive(*LibFile->get(), DeviceFiles);
|
|
|
|
}
|
|
|
|
default:
|
2022-05-13 00:39:15 +08:00
|
|
|
return Error::success();
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
2022-01-05 06:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace nvptx {
|
2022-01-08 06:12:51 +08:00
|
|
|
Expected<std::string> assemble(StringRef InputFile, Triple TheTriple,
|
2022-04-23 01:19:16 +08:00
|
|
|
StringRef Arch, bool RDC = true) {
|
2022-02-04 05:41:47 +08:00
|
|
|
// NVPTX uses the ptxas binary to create device object files.
|
2022-04-20 02:14:16 +08:00
|
|
|
Expected<std::string> PtxasPath = findProgram("ptxas", {CudaBinaryPath});
|
2022-01-08 06:12:51 +08:00
|
|
|
if (!PtxasPath)
|
2022-04-20 02:14:16 +08:00
|
|
|
return PtxasPath.takeError();
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
// Create a new file to write the linked device image to.
|
|
|
|
SmallString<128> TempFile;
|
2022-01-18 23:56:12 +08:00
|
|
|
if (Error Err =
|
|
|
|
createOutputFile(sys::path::filename(ExecutableName) + "-device-" +
|
|
|
|
TheTriple.getArchName() + "-" + Arch,
|
|
|
|
"cubin", TempFile))
|
2022-01-17 05:06:59 +08:00
|
|
|
return std::move(Err);
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
SmallVector<StringRef, 16> CmdArgs;
|
|
|
|
std::string Opt = "-" + OptLevel;
|
|
|
|
CmdArgs.push_back(*PtxasPath);
|
|
|
|
CmdArgs.push_back(TheTriple.isArch64Bit() ? "-m64" : "-m32");
|
2022-01-13 05:14:52 +08:00
|
|
|
if (Verbose)
|
|
|
|
CmdArgs.push_back("-v");
|
|
|
|
if (DebugInfo == DirectivesOnly && OptLevel[1] == '0')
|
|
|
|
CmdArgs.push_back("-lineinfo");
|
|
|
|
else if (DebugInfo == FullDebugInfo && OptLevel[1] == '0')
|
|
|
|
CmdArgs.push_back("-g");
|
2022-01-26 00:23:27 +08:00
|
|
|
for (auto &Arg : PtxasArgs)
|
|
|
|
CmdArgs.push_back(Arg);
|
2022-01-08 06:12:51 +08:00
|
|
|
CmdArgs.push_back("-o");
|
|
|
|
CmdArgs.push_back(TempFile);
|
|
|
|
CmdArgs.push_back(Opt);
|
|
|
|
CmdArgs.push_back("--gpu-name");
|
|
|
|
CmdArgs.push_back(Arch);
|
2022-04-23 01:19:16 +08:00
|
|
|
if (RDC)
|
|
|
|
CmdArgs.push_back("-c");
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
CmdArgs.push_back(InputFile);
|
|
|
|
|
2022-04-20 02:14:16 +08:00
|
|
|
if (Error Err = executeCommands(*PtxasPath, CmdArgs))
|
2022-04-20 06:40:15 +08:00
|
|
|
return std::move(Err);
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
return static_cast<std::string>(TempFile);
|
|
|
|
}
|
|
|
|
|
2022-01-26 00:23:27 +08:00
|
|
|
Expected<std::string> link(ArrayRef<std::string> InputFiles, Triple TheTriple,
|
2022-01-05 06:20:04 +08:00
|
|
|
StringRef Arch) {
|
|
|
|
// NVPTX uses the nvlink binary to link device object files.
|
2022-04-20 02:14:16 +08:00
|
|
|
Expected<std::string> NvlinkPath = findProgram("nvlink", {CudaBinaryPath});
|
2022-02-04 05:41:47 +08:00
|
|
|
if (!NvlinkPath)
|
2022-04-20 02:14:16 +08:00
|
|
|
return NvlinkPath.takeError();
|
2022-01-05 06:20:04 +08:00
|
|
|
|
|
|
|
// Create a new file to write the linked device image to.
|
|
|
|
SmallString<128> TempFile;
|
2022-01-18 23:56:12 +08:00
|
|
|
if (Error Err =
|
|
|
|
createOutputFile(sys::path::filename(ExecutableName) + "-device-" +
|
|
|
|
TheTriple.getArchName() + "-" + Arch,
|
|
|
|
"out", TempFile))
|
2022-01-17 05:06:59 +08:00
|
|
|
return std::move(Err);
|
2022-01-05 06:20:04 +08:00
|
|
|
|
|
|
|
SmallVector<StringRef, 16> CmdArgs;
|
|
|
|
CmdArgs.push_back(*NvlinkPath);
|
|
|
|
CmdArgs.push_back(TheTriple.isArch64Bit() ? "-m64" : "-m32");
|
2022-01-13 05:14:52 +08:00
|
|
|
if (Verbose)
|
|
|
|
CmdArgs.push_back("-v");
|
|
|
|
if (DebugInfo != NoDebugInfo)
|
|
|
|
CmdArgs.push_back("-g");
|
2022-01-05 06:20:04 +08:00
|
|
|
CmdArgs.push_back("-o");
|
|
|
|
CmdArgs.push_back(TempFile);
|
|
|
|
CmdArgs.push_back("-arch");
|
|
|
|
CmdArgs.push_back(Arch);
|
|
|
|
|
|
|
|
// Add extracted input files.
|
2022-01-12 04:50:39 +08:00
|
|
|
for (StringRef Input : InputFiles)
|
2022-01-05 06:20:04 +08:00
|
|
|
CmdArgs.push_back(Input);
|
|
|
|
|
2022-05-24 05:51:30 +08:00
|
|
|
renderXLinkerArgs(CmdArgs, TheTriple.getTriple());
|
2022-04-20 02:14:16 +08:00
|
|
|
if (Error Err = executeCommands(*NvlinkPath, CmdArgs))
|
2022-04-20 06:40:15 +08:00
|
|
|
return std::move(Err);
|
2022-01-05 06:20:04 +08:00
|
|
|
|
|
|
|
return static_cast<std::string>(TempFile);
|
|
|
|
}
|
2022-04-12 23:21:36 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
Expected<std::string>
|
|
|
|
fatbinary(ArrayRef<std::pair<StringRef, StringRef>> InputFiles,
|
|
|
|
Triple TheTriple) {
|
2022-04-12 23:21:36 +08:00
|
|
|
// NVPTX uses the fatbinary program to bundle the linked images.
|
|
|
|
Expected<std::string> FatBinaryPath =
|
|
|
|
findProgram("fatbinary", {CudaBinaryPath});
|
|
|
|
if (!FatBinaryPath)
|
|
|
|
return FatBinaryPath.takeError();
|
|
|
|
|
|
|
|
// Create a new file to write the linked device image to.
|
|
|
|
SmallString<128> TempFile;
|
|
|
|
if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
|
|
|
|
"-device-" + TheTriple.getArchName(),
|
|
|
|
"fatbin", TempFile))
|
|
|
|
return std::move(Err);
|
|
|
|
|
|
|
|
BumpPtrAllocator Alloc;
|
|
|
|
StringSaver Saver(Alloc);
|
|
|
|
|
|
|
|
SmallVector<StringRef, 16> CmdArgs;
|
|
|
|
CmdArgs.push_back(*FatBinaryPath);
|
|
|
|
CmdArgs.push_back(TheTriple.isArch64Bit() ? "-64" : "-32");
|
|
|
|
CmdArgs.push_back("--create");
|
|
|
|
CmdArgs.push_back(TempFile);
|
2022-06-06 23:36:45 +08:00
|
|
|
for (const auto &FileAndArch : InputFiles)
|
2022-04-12 23:21:36 +08:00
|
|
|
CmdArgs.push_back(Saver.save("--image=profile=" + std::get<1>(FileAndArch) +
|
|
|
|
",file=" + std::get<0>(FileAndArch)));
|
|
|
|
|
|
|
|
if (Error Err = executeCommands(*FatBinaryPath, CmdArgs))
|
|
|
|
return std::move(Err);
|
|
|
|
|
|
|
|
return static_cast<std::string>(TempFile);
|
|
|
|
}
|
2022-01-05 06:20:04 +08:00
|
|
|
} // namespace nvptx
|
2022-01-14 01:42:02 +08:00
|
|
|
namespace amdgcn {
|
2022-01-26 00:23:27 +08:00
|
|
|
Expected<std::string> link(ArrayRef<std::string> InputFiles, Triple TheTriple,
|
2022-01-14 01:42:02 +08:00
|
|
|
StringRef Arch) {
|
2022-02-08 03:59:03 +08:00
|
|
|
// AMDGPU uses lld to link device object files.
|
2022-04-26 22:49:31 +08:00
|
|
|
Expected<std::string> LLDPath =
|
|
|
|
findProgram("lld", {getMainExecutable("lld")});
|
2022-01-14 01:42:02 +08:00
|
|
|
if (!LLDPath)
|
2022-04-20 02:14:16 +08:00
|
|
|
return LLDPath.takeError();
|
2022-01-14 01:42:02 +08:00
|
|
|
|
|
|
|
// Create a new file to write the linked device image to.
|
|
|
|
SmallString<128> TempFile;
|
2022-01-18 23:56:12 +08:00
|
|
|
if (Error Err = createOutputFile(sys::path::filename(ExecutableName) + "-" +
|
|
|
|
TheTriple.getArchName() + "-" + Arch,
|
|
|
|
"out", TempFile))
|
2022-01-17 05:06:59 +08:00
|
|
|
return std::move(Err);
|
2022-01-14 01:42:02 +08:00
|
|
|
|
|
|
|
SmallVector<StringRef, 16> CmdArgs;
|
|
|
|
CmdArgs.push_back(*LLDPath);
|
|
|
|
CmdArgs.push_back("-flavor");
|
|
|
|
CmdArgs.push_back("gnu");
|
|
|
|
CmdArgs.push_back("--no-undefined");
|
|
|
|
CmdArgs.push_back("-shared");
|
|
|
|
CmdArgs.push_back("-o");
|
|
|
|
CmdArgs.push_back(TempFile);
|
|
|
|
|
|
|
|
// Add extracted input files.
|
|
|
|
for (StringRef Input : InputFiles)
|
|
|
|
CmdArgs.push_back(Input);
|
|
|
|
|
2022-05-24 05:51:30 +08:00
|
|
|
renderXLinkerArgs(CmdArgs, TheTriple.getTriple());
|
2022-04-20 02:14:16 +08:00
|
|
|
if (Error Err = executeCommands(*LLDPath, CmdArgs))
|
2022-04-20 06:40:15 +08:00
|
|
|
return std::move(Err);
|
2022-01-14 01:42:02 +08:00
|
|
|
|
|
|
|
return static_cast<std::string>(TempFile);
|
|
|
|
}
|
|
|
|
} // namespace amdgcn
|
2022-01-05 06:20:04 +08:00
|
|
|
|
2022-02-12 10:58:15 +08:00
|
|
|
namespace generic {
|
|
|
|
|
|
|
|
const char *getLDMOption(const llvm::Triple &T) {
|
|
|
|
switch (T.getArch()) {
|
|
|
|
case llvm::Triple::x86:
|
|
|
|
if (T.isOSIAMCU())
|
|
|
|
return "elf_iamcu";
|
|
|
|
return "elf_i386";
|
|
|
|
case llvm::Triple::aarch64:
|
|
|
|
return "aarch64linux";
|
|
|
|
case llvm::Triple::aarch64_be:
|
|
|
|
return "aarch64linuxb";
|
|
|
|
case llvm::Triple::ppc64:
|
|
|
|
return "elf64ppc";
|
|
|
|
case llvm::Triple::ppc64le:
|
|
|
|
return "elf64lppc";
|
|
|
|
case llvm::Triple::x86_64:
|
|
|
|
if (T.isX32())
|
|
|
|
return "elf32_x86_64";
|
|
|
|
return "elf_x86_64";
|
|
|
|
case llvm::Triple::ve:
|
|
|
|
return "elf64ve";
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<std::string> link(ArrayRef<std::string> InputFiles, Triple TheTriple,
|
|
|
|
StringRef Arch) {
|
|
|
|
// Create a new file to write the linked device image to.
|
|
|
|
SmallString<128> TempFile;
|
|
|
|
if (Error Err = createOutputFile(sys::path::filename(ExecutableName) + "-" +
|
|
|
|
TheTriple.getArchName() + "-" + Arch,
|
|
|
|
"out", TempFile))
|
|
|
|
return std::move(Err);
|
|
|
|
|
|
|
|
// Use the host linker to perform generic offloading. Use the same libraries
|
|
|
|
// and paths as the host application does.
|
|
|
|
SmallVector<StringRef, 16> CmdArgs;
|
|
|
|
CmdArgs.push_back(LinkerUserPath);
|
|
|
|
CmdArgs.push_back("-m");
|
|
|
|
CmdArgs.push_back(getLDMOption(TheTriple));
|
|
|
|
CmdArgs.push_back("-shared");
|
|
|
|
for (auto AI = HostLinkerArgs.begin(), AE = HostLinkerArgs.end(); AI != AE;
|
|
|
|
++AI) {
|
|
|
|
StringRef Arg = *AI;
|
|
|
|
if (Arg.startswith("-L"))
|
|
|
|
CmdArgs.push_back(Arg);
|
|
|
|
else if (Arg.startswith("-l"))
|
|
|
|
CmdArgs.push_back(Arg);
|
|
|
|
else if (Arg.startswith("--as-needed"))
|
|
|
|
CmdArgs.push_back(Arg);
|
|
|
|
else if (Arg.startswith("--no-as-needed"))
|
|
|
|
CmdArgs.push_back(Arg);
|
|
|
|
else if (Arg.startswith("-rpath")) {
|
|
|
|
CmdArgs.push_back(Arg);
|
2022-03-03 01:38:29 +08:00
|
|
|
CmdArgs.push_back(*std::next(AI));
|
2022-02-12 10:58:15 +08:00
|
|
|
} else if (Arg.startswith("-dynamic-linker")) {
|
|
|
|
CmdArgs.push_back(Arg);
|
2022-03-03 01:38:29 +08:00
|
|
|
CmdArgs.push_back(*std::next(AI));
|
2022-02-12 10:58:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CmdArgs.push_back("-Bsymbolic");
|
|
|
|
CmdArgs.push_back("-o");
|
|
|
|
CmdArgs.push_back(TempFile);
|
|
|
|
|
|
|
|
// Add extracted input files.
|
|
|
|
for (StringRef Input : InputFiles)
|
|
|
|
CmdArgs.push_back(Input);
|
|
|
|
|
2022-05-24 05:51:30 +08:00
|
|
|
renderXLinkerArgs(CmdArgs, TheTriple.getTriple());
|
2022-04-20 02:14:16 +08:00
|
|
|
if (Error Err = executeCommands(LinkerUserPath, CmdArgs))
|
2022-04-20 06:40:15 +08:00
|
|
|
return std::move(Err);
|
2022-02-12 10:58:15 +08:00
|
|
|
|
|
|
|
return static_cast<std::string>(TempFile);
|
|
|
|
}
|
|
|
|
} // namespace generic
|
|
|
|
|
2022-01-12 04:50:39 +08:00
|
|
|
Expected<std::string> linkDevice(ArrayRef<std::string> InputFiles,
|
2022-01-05 06:20:04 +08:00
|
|
|
Triple TheTriple, StringRef Arch) {
|
|
|
|
switch (TheTriple.getArch()) {
|
|
|
|
case Triple::nvptx:
|
|
|
|
case Triple::nvptx64:
|
2022-01-26 00:23:27 +08:00
|
|
|
return nvptx::link(InputFiles, TheTriple, Arch);
|
2022-01-05 06:20:04 +08:00
|
|
|
case Triple::amdgcn:
|
2022-01-26 00:23:27 +08:00
|
|
|
return amdgcn::link(InputFiles, TheTriple, Arch);
|
2022-01-05 06:20:04 +08:00
|
|
|
case Triple::x86:
|
|
|
|
case Triple::x86_64:
|
2022-02-12 10:58:15 +08:00
|
|
|
case Triple::aarch64:
|
|
|
|
case Triple::aarch64_be:
|
|
|
|
case Triple::ppc64:
|
|
|
|
case Triple::ppc64le:
|
|
|
|
return generic::link(InputFiles, TheTriple, Arch);
|
2022-01-05 06:20:04 +08:00
|
|
|
default:
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
TheTriple.getArchName() +
|
|
|
|
" linking is not supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 06:12:51 +08:00
|
|
|
void diagnosticHandler(const DiagnosticInfo &DI) {
|
|
|
|
std::string ErrStorage;
|
|
|
|
raw_string_ostream OS(ErrStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
|
|
|
|
|
|
|
switch (DI.getSeverity()) {
|
|
|
|
case DS_Error:
|
2022-01-13 05:14:52 +08:00
|
|
|
WithColor::error(errs(), LinkerExecutable) << ErrStorage << "\n";
|
2022-01-08 06:12:51 +08:00
|
|
|
break;
|
|
|
|
case DS_Warning:
|
2022-01-13 05:14:52 +08:00
|
|
|
WithColor::warning(errs(), LinkerExecutable) << ErrStorage << "\n";
|
2022-01-08 06:12:51 +08:00
|
|
|
break;
|
|
|
|
case DS_Note:
|
2022-01-13 05:14:52 +08:00
|
|
|
WithColor::note(errs(), LinkerExecutable) << ErrStorage << "\n";
|
2022-01-08 06:12:51 +08:00
|
|
|
break;
|
|
|
|
case DS_Remark:
|
2022-01-16 12:10:52 +08:00
|
|
|
WithColor::remark(errs()) << ErrStorage << "\n";
|
2022-01-08 06:12:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the target features passed in from the driver as <triple>=<features>.
|
|
|
|
std::vector<std::string> getTargetFeatures(const Triple &TheTriple) {
|
|
|
|
std::vector<std::string> Features;
|
|
|
|
auto TargetAndFeatures = StringRef(TargetFeatures).split('=');
|
|
|
|
if (TargetAndFeatures.first != TheTriple.getTriple())
|
|
|
|
return Features;
|
|
|
|
|
|
|
|
for (auto Feature : llvm::split(TargetAndFeatures.second, ','))
|
|
|
|
Features.push_back(Feature.str());
|
|
|
|
return Features;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenOpt::Level getCGOptLevel(unsigned OptLevel) {
|
|
|
|
switch (OptLevel) {
|
|
|
|
case 0:
|
|
|
|
return CodeGenOpt::None;
|
|
|
|
case 1:
|
|
|
|
return CodeGenOpt::Less;
|
|
|
|
case 2:
|
|
|
|
return CodeGenOpt::Default;
|
|
|
|
case 3:
|
|
|
|
return CodeGenOpt::Aggressive;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid optimization level");
|
|
|
|
}
|
|
|
|
|
2022-01-12 04:50:39 +08:00
|
|
|
template <typename ModuleHook = function_ref<bool(size_t, const Module &)>>
|
|
|
|
std::unique_ptr<lto::LTO> createLTO(
|
|
|
|
const Triple &TheTriple, StringRef Arch, bool WholeProgram,
|
|
|
|
ModuleHook Hook = [](size_t, const Module &) { return true; }) {
|
2022-01-08 06:12:51 +08:00
|
|
|
lto::Config Conf;
|
|
|
|
lto::ThinBackend Backend;
|
|
|
|
// TODO: Handle index-only thin-LTO
|
2022-04-01 21:42:21 +08:00
|
|
|
Backend =
|
|
|
|
lto::createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
Conf.CPU = Arch.str();
|
|
|
|
Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
|
|
|
|
|
|
|
|
Conf.MAttrs = getTargetFeatures(TheTriple);
|
|
|
|
Conf.CGOptLevel = getCGOptLevel(OptLevel[1] - '0');
|
|
|
|
Conf.OptLevel = OptLevel[1] - '0';
|
2022-04-12 05:26:33 +08:00
|
|
|
if (Conf.OptLevel > 0)
|
|
|
|
Conf.UseDefaultPipeline = true;
|
2022-01-08 06:12:51 +08:00
|
|
|
Conf.DefaultTriple = TheTriple.getTriple();
|
|
|
|
Conf.DiagHandler = diagnosticHandler;
|
|
|
|
|
|
|
|
Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
|
|
|
|
Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
|
|
|
|
|
2022-01-17 05:06:59 +08:00
|
|
|
if (SaveTemps) {
|
2022-06-06 23:36:45 +08:00
|
|
|
auto HandleError = [=](Error Err) {
|
2022-01-17 05:06:59 +08:00
|
|
|
logAllUnhandledErrors(std::move(Err),
|
|
|
|
WithColor::error(errs(), LinkerExecutable));
|
|
|
|
exit(1);
|
|
|
|
};
|
2022-06-06 23:36:45 +08:00
|
|
|
Conf.PostInternalizeModuleHook = [&, Arch](size_t, const Module &M) {
|
2022-01-17 05:06:59 +08:00
|
|
|
SmallString<128> TempFile;
|
2022-06-06 23:36:45 +08:00
|
|
|
if (Error Err =
|
|
|
|
createOutputFile(sys::path::filename(ExecutableName) + "-" +
|
|
|
|
TheTriple.getTriple() + "-" + Arch,
|
|
|
|
"bc", TempFile))
|
2022-01-17 05:06:59 +08:00
|
|
|
HandleError(std::move(Err));
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream LinkedBitcode(TempFile, EC, sys::fs::OF_None);
|
|
|
|
if (EC)
|
|
|
|
HandleError(errorCodeToError(EC));
|
|
|
|
WriteBitcodeToFile(M, LinkedBitcode);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
2022-01-16 12:10:52 +08:00
|
|
|
Conf.PostOptModuleHook = Hook;
|
2022-01-08 06:12:51 +08:00
|
|
|
if (TheTriple.isNVPTX())
|
|
|
|
Conf.CGFileType = CGFT_AssemblyFile;
|
|
|
|
else
|
|
|
|
Conf.CGFileType = CGFT_ObjectFile;
|
|
|
|
|
|
|
|
// TODO: Handle remark files
|
|
|
|
Conf.HasWholeProgramVisibility = WholeProgram;
|
|
|
|
|
|
|
|
return std::make_unique<lto::LTO>(std::move(Conf), Backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if \p S is valid as a C language identifier and will be given
|
|
|
|
// `__start_` and `__stop_` symbols.
|
|
|
|
bool isValidCIdentifier(StringRef S) {
|
|
|
|
return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
|
|
|
|
std::all_of(S.begin() + 1, S.end(),
|
|
|
|
[](char C) { return C == '_' || isAlnum(C); });
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
Error linkBitcodeFiles(SmallVectorImpl<OffloadFile> &InputFiles,
|
|
|
|
SmallVectorImpl<std::string> &OutputFiles,
|
|
|
|
const Triple &TheTriple, StringRef Arch) {
|
|
|
|
SmallVector<OffloadFile, 4> BitcodeInputFiles;
|
2022-02-17 05:30:45 +08:00
|
|
|
DenseSet<StringRef> UsedInRegularObj;
|
|
|
|
DenseSet<StringRef> UsedInSharedLib;
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
// Search for bitcode files in the input and create an LTO input file. If it
|
2022-06-06 23:36:45 +08:00
|
|
|
// is not a bitcode file, scan its symbol table for symbols we need to save.
|
|
|
|
for (OffloadFile &File : InputFiles) {
|
|
|
|
MemoryBufferRef Buffer = MemoryBufferRef(File.getBinary()->getImage(), "");
|
|
|
|
|
|
|
|
file_magic Type = identify_magic(Buffer.getBuffer());
|
2022-02-22 03:39:58 +08:00
|
|
|
switch (Type) {
|
|
|
|
case file_magic::bitcode: {
|
2022-06-06 23:36:45 +08:00
|
|
|
BitcodeInputFiles.emplace_back(std::move(File));
|
2022-02-22 03:39:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case file_magic::cuda_fatbinary: {
|
|
|
|
// Cuda fatbinaries made by Clang almost almost have an object eighty
|
|
|
|
// bytes from the beginning. This should be sufficient to identify the
|
|
|
|
// symbols.
|
2022-06-06 23:36:45 +08:00
|
|
|
Buffer =
|
|
|
|
MemoryBufferRef(Buffer.getBuffer().drop_front(FatbinaryOffset), "");
|
2022-02-22 03:39:58 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
}
|
|
|
|
case file_magic::elf_relocatable:
|
|
|
|
case file_magic::elf_shared_object:
|
|
|
|
case file_magic::macho_object:
|
|
|
|
case file_magic::coff_object: {
|
2022-01-08 06:12:51 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> ObjFile =
|
2022-02-22 03:39:58 +08:00
|
|
|
ObjectFile::createObjectFile(Buffer);
|
2022-01-08 06:12:51 +08:00
|
|
|
if (!ObjFile)
|
2022-02-22 03:39:58 +08:00
|
|
|
continue;
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
for (auto &Sym : (*ObjFile)->symbols()) {
|
|
|
|
Expected<StringRef> Name = Sym.getName();
|
|
|
|
if (!Name)
|
|
|
|
return Name.takeError();
|
|
|
|
|
2022-01-14 11:59:05 +08:00
|
|
|
// Record if we've seen these symbols in any object or shared libraries.
|
2022-01-26 00:23:27 +08:00
|
|
|
if ((*ObjFile)->isRelocatableObject())
|
2022-06-06 23:36:45 +08:00
|
|
|
UsedInRegularObj.insert(*Name);
|
2022-01-26 00:23:27 +08:00
|
|
|
else
|
2022-06-06 23:36:45 +08:00
|
|
|
UsedInSharedLib.insert(*Name);
|
2022-01-08 06:12:51 +08:00
|
|
|
}
|
2022-02-22 03:39:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue;
|
2022-01-08 06:12:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
if (BitcodeInputFiles.empty())
|
2022-01-12 04:50:39 +08:00
|
|
|
return Error::success();
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// Remove all the bitcode files that we moved from the original input.
|
|
|
|
llvm::erase_if(InputFiles, [](OffloadFile &F) { return !F.getBinary(); });
|
|
|
|
|
2022-01-17 05:06:59 +08:00
|
|
|
auto HandleError = [&](Error Err) {
|
|
|
|
logAllUnhandledErrors(std::move(Err),
|
2022-01-12 04:50:39 +08:00
|
|
|
WithColor::error(errs(), LinkerExecutable));
|
|
|
|
exit(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
// LTO Module hook to output bitcode without running the backend.
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVector<std::string, 4> BitcodeOutput;
|
2022-01-17 05:06:59 +08:00
|
|
|
auto OutputBitcode = [&](size_t Task, const Module &M) {
|
2022-01-12 04:50:39 +08:00
|
|
|
SmallString<128> TempFile;
|
2022-01-18 23:56:12 +08:00
|
|
|
if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
|
|
|
|
"-jit-" + TheTriple.getTriple(),
|
|
|
|
"bc", TempFile))
|
2022-01-17 05:06:59 +08:00
|
|
|
HandleError(std::move(Err));
|
|
|
|
|
2022-01-12 04:50:39 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream LinkedBitcode(TempFile, EC, sys::fs::OF_None);
|
|
|
|
if (EC)
|
2022-01-17 05:06:59 +08:00
|
|
|
HandleError(errorCodeToError(EC));
|
2022-01-12 04:50:39 +08:00
|
|
|
WriteBitcodeToFile(M, LinkedBitcode);
|
2022-06-06 23:36:45 +08:00
|
|
|
BitcodeOutput.push_back(static_cast<std::string>(TempFile));
|
2022-01-12 04:50:39 +08:00
|
|
|
return false;
|
|
|
|
};
|
2022-01-08 06:12:51 +08:00
|
|
|
|
2022-01-14 11:59:05 +08:00
|
|
|
// We assume visibility of the whole program if every input file was bitcode.
|
2022-06-06 23:36:45 +08:00
|
|
|
bool WholeProgram = InputFiles.empty();
|
2022-01-17 05:06:59 +08:00
|
|
|
auto LTOBackend =
|
|
|
|
(EmbedBitcode) ? createLTO(TheTriple, Arch, WholeProgram, OutputBitcode)
|
|
|
|
: createLTO(TheTriple, Arch, WholeProgram);
|
2022-01-08 06:12:51 +08:00
|
|
|
|
2022-01-14 11:59:05 +08:00
|
|
|
// We need to resolve the symbols so the LTO backend knows which symbols need
|
|
|
|
// to be kept or can be internalized. This is a simplified symbol resolution
|
|
|
|
// scheme to approximate the full resolution a linker would do.
|
|
|
|
DenseSet<StringRef> PrevailingSymbols;
|
2022-06-06 23:36:45 +08:00
|
|
|
for (auto &BitcodeInput : BitcodeInputFiles) {
|
|
|
|
MemoryBufferRef Buffer =
|
|
|
|
MemoryBufferRef(BitcodeInput.getBinary()->getImage(), "");
|
|
|
|
Expected<std::unique_ptr<lto::InputFile>> BitcodeFileOrErr =
|
|
|
|
llvm::lto::InputFile::create(Buffer);
|
|
|
|
if (!BitcodeFileOrErr)
|
|
|
|
return BitcodeFileOrErr.takeError();
|
|
|
|
|
|
|
|
// Save the input file and the buffer associated with its memory.
|
|
|
|
const auto Symbols = (*BitcodeFileOrErr)->symbols();
|
2022-01-08 06:12:51 +08:00
|
|
|
SmallVector<lto::SymbolResolution, 16> Resolutions(Symbols.size());
|
|
|
|
size_t Idx = 0;
|
|
|
|
for (auto &Sym : Symbols) {
|
|
|
|
lto::SymbolResolution &Res = Resolutions[Idx++];
|
|
|
|
|
|
|
|
// We will use this as the prevailing symbol definition in LTO unless
|
2022-01-14 11:59:05 +08:00
|
|
|
// it is undefined or another definition has already been used.
|
|
|
|
Res.Prevailing =
|
2022-06-06 23:36:45 +08:00
|
|
|
!Sym.isUndefined() && PrevailingSymbols.insert(Sym.getName()).second;
|
2022-01-14 11:59:05 +08:00
|
|
|
|
|
|
|
// We need LTO to preseve the following global symbols:
|
|
|
|
// 1) Symbols used in regular objects.
|
|
|
|
// 2) Sections that will be given a __start/__stop symbol.
|
2022-02-17 05:30:45 +08:00
|
|
|
// 3) Prevailing symbols that are needed visible to external libraries.
|
2022-01-08 06:12:51 +08:00
|
|
|
Res.VisibleToRegularObj =
|
2022-02-17 05:30:45 +08:00
|
|
|
UsedInRegularObj.contains(Sym.getName()) ||
|
2022-01-08 06:12:51 +08:00
|
|
|
isValidCIdentifier(Sym.getSectionName()) ||
|
2022-01-14 11:59:05 +08:00
|
|
|
(Res.Prevailing &&
|
|
|
|
(Sym.getVisibility() != GlobalValue::HiddenVisibility &&
|
|
|
|
!Sym.canBeOmittedFromSymbolTable()));
|
|
|
|
|
|
|
|
// Identify symbols that must be exported dynamically and can be
|
|
|
|
// referenced by other files.
|
|
|
|
Res.ExportDynamic =
|
|
|
|
Sym.getVisibility() != GlobalValue::HiddenVisibility &&
|
2022-02-17 05:30:45 +08:00
|
|
|
(UsedInSharedLib.contains(Sym.getName()) ||
|
2022-01-14 11:59:05 +08:00
|
|
|
!Sym.canBeOmittedFromSymbolTable());
|
|
|
|
|
|
|
|
// The final definition will reside in this linkage unit if the symbol is
|
|
|
|
// defined and local to the module. This only checks for bitcode files,
|
|
|
|
// full assertion will require complete symbol resolution.
|
|
|
|
Res.FinalDefinitionInLinkageUnit =
|
|
|
|
Sym.getVisibility() != GlobalValue::DefaultVisibility &&
|
|
|
|
(!Sym.isUndefined() && !Sym.isCommon());
|
2022-01-08 06:12:51 +08:00
|
|
|
|
|
|
|
// We do not support linker redefined symbols (e.g. --wrap) for device
|
|
|
|
// image linking, so the symbols will not be changed after LTO.
|
|
|
|
Res.LinkerRedefined = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the bitcode file with its resolved symbols to the LTO job.
|
2022-06-06 23:36:45 +08:00
|
|
|
if (Error Err = LTOBackend->add(std::move(*BitcodeFileOrErr), Resolutions))
|
2022-01-14 11:59:05 +08:00
|
|
|
return Err;
|
2022-01-08 06:12:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the LTO job to compile the bitcode.
|
|
|
|
size_t MaxTasks = LTOBackend->getMaxTasks();
|
|
|
|
std::vector<SmallString<128>> Files(MaxTasks);
|
|
|
|
auto AddStream = [&](size_t Task) -> std::unique_ptr<CachedFileStream> {
|
|
|
|
int FD = -1;
|
|
|
|
auto &TempFile = Files[Task];
|
|
|
|
StringRef Extension = (TheTriple.isNVPTX()) ? "s" : "o";
|
2022-01-18 23:56:12 +08:00
|
|
|
if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
|
2022-01-26 00:23:27 +08:00
|
|
|
"-device-" + TheTriple.getTriple(),
|
2022-01-18 23:56:12 +08:00
|
|
|
Extension, TempFile))
|
2022-01-17 05:06:59 +08:00
|
|
|
HandleError(std::move(Err));
|
|
|
|
if (std::error_code EC = sys::fs::openFileForWrite(TempFile, FD))
|
|
|
|
HandleError(errorCodeToError(EC));
|
2022-01-08 06:12:51 +08:00
|
|
|
return std::make_unique<CachedFileStream>(
|
|
|
|
std::make_unique<llvm::raw_fd_ostream>(FD, true));
|
|
|
|
};
|
2022-01-12 04:50:39 +08:00
|
|
|
|
2022-01-08 06:12:51 +08:00
|
|
|
if (Error Err = LTOBackend->run(AddStream))
|
2022-01-14 11:59:05 +08:00
|
|
|
return Err;
|
2022-01-08 06:12:51 +08:00
|
|
|
|
2022-05-25 01:43:52 +08:00
|
|
|
// If we are embedding bitcode we only need the intermediate output.
|
|
|
|
if (EmbedBitcode) {
|
2022-06-06 23:36:45 +08:00
|
|
|
if (BitcodeOutput.size() != 1 || !WholeProgram)
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"Cannot embed bitcode with multiple files.");
|
|
|
|
OutputFiles.push_back(static_cast<std::string>(BitcodeOutput.front()));
|
2022-05-25 01:43:52 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2022-01-12 04:50:39 +08:00
|
|
|
// Is we are compiling for NVPTX we need to run the assembler first.
|
2022-05-25 01:43:52 +08:00
|
|
|
if (TheTriple.isNVPTX()) {
|
2022-01-16 12:10:52 +08:00
|
|
|
for (auto &File : Files) {
|
2022-04-23 01:19:16 +08:00
|
|
|
auto FileOrErr = nvptx::assemble(File, TheTriple, Arch, !WholeProgram);
|
2022-01-16 12:10:52 +08:00
|
|
|
if (!FileOrErr)
|
|
|
|
return FileOrErr.takeError();
|
|
|
|
File = *FileOrErr;
|
|
|
|
}
|
2022-01-08 06:12:51 +08:00
|
|
|
}
|
|
|
|
|
2022-01-12 04:50:39 +08:00
|
|
|
// Append the new inputs to the device linker input.
|
|
|
|
for (auto &File : Files)
|
2022-06-06 23:36:45 +08:00
|
|
|
OutputFiles.push_back(static_cast<std::string>(File));
|
2022-01-12 04:50:39 +08:00
|
|
|
|
|
|
|
return Error::success();
|
2022-01-08 06:12:51 +08:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
Expected<std::string> writeOffloadFile(const OffloadFile &File) {
|
|
|
|
const OffloadBinary &Binary = *File.getBinary();
|
2022-05-06 21:37:08 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
StringRef Prefix =
|
|
|
|
sys::path::stem(Binary.getMemoryBufferRef().getBufferIdentifier());
|
|
|
|
StringRef Suffix = getImageKindName(Binary.getImageKind());
|
2022-01-12 04:50:39 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallString<128> TempFile;
|
|
|
|
if (Error Err = createOutputFile(Prefix + "-" + Binary.getTriple() + "-" +
|
|
|
|
Binary.getArch(),
|
|
|
|
Suffix, TempFile))
|
2022-06-22 21:37:54 +08:00
|
|
|
return std::move(Err);
|
2022-04-23 01:19:16 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
|
|
|
|
FileOutputBuffer::create(TempFile, Binary.getImage().size());
|
|
|
|
if (!OutputOrErr)
|
|
|
|
return OutputOrErr.takeError();
|
|
|
|
std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
|
|
|
|
std::copy(Binary.getImage().bytes_begin(), Binary.getImage().bytes_end(),
|
|
|
|
Output->getBufferStart());
|
|
|
|
if (Error E = Output->commit())
|
2022-06-22 21:37:54 +08:00
|
|
|
return std::move(E);
|
2022-01-05 06:20:04 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
return static_cast<std::string>(TempFile);
|
2022-01-05 06:20:04 +08:00
|
|
|
}
|
|
|
|
|
2022-01-26 03:25:39 +08:00
|
|
|
// Compile the module to an object file using the appropriate target machine for
|
|
|
|
// the host triple.
|
|
|
|
Expected<std::string> compileModule(Module &M) {
|
|
|
|
std::string Msg;
|
|
|
|
const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
|
|
|
|
if (!T)
|
|
|
|
return createStringError(inconvertibleErrorCode(), Msg);
|
|
|
|
|
|
|
|
auto Options =
|
|
|
|
codegen::InitTargetOptionsFromCodeGenFlags(Triple(M.getTargetTriple()));
|
|
|
|
StringRef CPU = "";
|
|
|
|
StringRef Features = "";
|
|
|
|
std::unique_ptr<TargetMachine> TM(T->createTargetMachine(
|
|
|
|
HostTriple, CPU, Features, Options, Reloc::PIC_, M.getCodeModel()));
|
|
|
|
|
|
|
|
if (M.getDataLayout().isDefault())
|
|
|
|
M.setDataLayout(TM->createDataLayout());
|
|
|
|
|
|
|
|
SmallString<128> ObjectFile;
|
|
|
|
int FD = -1;
|
2022-05-04 08:50:26 +08:00
|
|
|
if (Error Err = createOutputFile(
|
|
|
|
sys::path::filename(ExecutableName) + "-wrapper", "o", ObjectFile))
|
2022-01-26 03:25:39 +08:00
|
|
|
return std::move(Err);
|
|
|
|
if (std::error_code EC = sys::fs::openFileForWrite(ObjectFile, FD))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
|
|
|
auto OS = std::make_unique<llvm::raw_fd_ostream>(FD, true);
|
|
|
|
|
|
|
|
legacy::PassManager CodeGenPasses;
|
|
|
|
TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
|
|
|
|
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
|
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, *OS, nullptr, CGFT_ObjectFile))
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"Failed to execute host backend");
|
|
|
|
CodeGenPasses.run(M);
|
|
|
|
|
|
|
|
return static_cast<std::string>(ObjectFile);
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
/// Creates the object file containing the device image and runtime
|
|
|
|
/// registration code from the device images stored in \p Images.
|
|
|
|
Expected<std::string>
|
|
|
|
wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
|
|
|
|
OffloadKind Kind) {
|
|
|
|
SmallVector<ArrayRef<char>, 4> BuffersToWrap;
|
|
|
|
for (const auto &Buffer : Buffers)
|
|
|
|
BuffersToWrap.emplace_back(
|
|
|
|
ArrayRef<char>(Buffer->getBufferStart(), Buffer->getBufferSize()));
|
|
|
|
|
|
|
|
LLVMContext Context;
|
|
|
|
Module M("offload.wrapper.module", Context);
|
|
|
|
M.setTargetTriple(HostTriple);
|
|
|
|
|
|
|
|
switch (Kind) {
|
|
|
|
case OFK_OpenMP:
|
|
|
|
if (Error Err = wrapOpenMPBinaries(M, BuffersToWrap))
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case OFK_Cuda:
|
|
|
|
if (Error Err = wrapCudaBinary(M, BuffersToWrap.front()))
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
getOffloadKindName(Kind) +
|
|
|
|
" wrapping is not supported");
|
2022-01-26 06:46:01 +08:00
|
|
|
}
|
2022-01-05 06:20:04 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
if (PrintWrappedModule)
|
|
|
|
llvm::errs() << M;
|
|
|
|
|
|
|
|
auto FileOrErr = compileModule(M);
|
|
|
|
if (!FileOrErr)
|
|
|
|
return FileOrErr.takeError();
|
|
|
|
return *FileOrErr;
|
2022-04-12 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
|
|
|
|
bundleOpenMP(ArrayRef<OffloadingImage> Images) {
|
|
|
|
SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
|
|
|
|
for (const OffloadingImage &Image : Images)
|
|
|
|
Buffers.emplace_back(
|
|
|
|
MemoryBuffer::getMemBufferCopy(Image.Image->getBuffer()));
|
|
|
|
|
|
|
|
return std::move(Buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
|
|
|
|
bundleCuda(ArrayRef<OffloadingImage> Images) {
|
|
|
|
SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
|
2022-04-12 23:21:36 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVector<std::pair<StringRef, StringRef>, 4> InputFiles;
|
|
|
|
for (const OffloadingImage &Image : Images)
|
|
|
|
InputFiles.emplace_back(std::make_pair(Image.Image->getBufferIdentifier(),
|
|
|
|
Image.StringData.lookup("arch")));
|
|
|
|
|
|
|
|
Triple TheTriple = Triple(Images.front().StringData.lookup("triple"));
|
|
|
|
auto FileOrErr = nvptx::fatbinary(InputFiles, TheTriple);
|
2022-04-12 23:21:36 +08:00
|
|
|
if (!FileOrErr)
|
|
|
|
return FileOrErr.takeError();
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ImageOrError =
|
|
|
|
llvm::MemoryBuffer::getFileOrSTDIN(*FileOrErr);
|
|
|
|
if (std::error_code EC = ImageOrError.getError())
|
|
|
|
return createFileError(*FileOrErr, EC);
|
2022-06-06 23:36:45 +08:00
|
|
|
Buffers.emplace_back(std::move(*ImageOrError));
|
2022-04-12 23:21:36 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
return std::move(Buffers);
|
|
|
|
}
|
2022-01-05 06:20:04 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
/// Transforms the input \p Images into the binary format the runtime expects
|
|
|
|
/// for the given \p Kind.
|
|
|
|
Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
|
|
|
|
bundleLinkedOutput(ArrayRef<OffloadingImage> Images, OffloadKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case OFK_OpenMP:
|
|
|
|
return bundleOpenMP(Images);
|
|
|
|
case OFK_Cuda:
|
|
|
|
return bundleCuda(Images);
|
|
|
|
default:
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
getOffloadKindName(Kind) +
|
|
|
|
" bundling is not supported");
|
|
|
|
}
|
2022-04-12 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
/// Transforms all the extracted offloading input files into an image that can
|
|
|
|
/// be registered by the runtime.
|
|
|
|
Expected<SmallVector<std::string>>
|
|
|
|
linkAndWrapDeviceFiles(SmallVectorImpl<OffloadFile> &LinkerInputFiles) {
|
|
|
|
DenseMap<OffloadFile::TargetID, SmallVector<OffloadFile, 4>> InputsForTarget;
|
|
|
|
for (auto &File : LinkerInputFiles)
|
|
|
|
InputsForTarget[File].emplace_back(std::move(File));
|
|
|
|
LinkerInputFiles.clear();
|
|
|
|
|
|
|
|
BumpPtrAllocator Alloc;
|
|
|
|
UniqueStringSaver Saver(Alloc);
|
|
|
|
DenseMap<OffloadKind, SmallVector<OffloadingImage, 2>> Images;
|
|
|
|
for (auto &InputForTarget : InputsForTarget) {
|
|
|
|
SmallVector<OffloadFile, 4> &Input = InputForTarget.getSecond();
|
|
|
|
StringRef TripleStr = Saver.save(InputForTarget.getFirst().first);
|
|
|
|
StringRef Arch = Saver.save(InputForTarget.getFirst().second);
|
|
|
|
llvm::Triple Triple(TripleStr);
|
|
|
|
|
|
|
|
DenseSet<OffloadKind> ActiveOffloadKinds;
|
|
|
|
for (const auto &File : Input)
|
|
|
|
ActiveOffloadKinds.insert(File.getBinary()->getOffloadKind());
|
|
|
|
|
|
|
|
// First link and remove all the input files containing bitcode.
|
|
|
|
SmallVector<std::string> InputFiles;
|
|
|
|
if (Error Err = linkBitcodeFiles(Input, InputFiles, Triple, Arch))
|
2022-06-22 21:37:54 +08:00
|
|
|
return std::move(Err);
|
2022-06-06 23:36:45 +08:00
|
|
|
|
|
|
|
// Write any remaining device inputs to an output file for the linker job.
|
|
|
|
for (const OffloadFile &File : Input) {
|
|
|
|
auto FileNameOrErr = writeOffloadFile(File);
|
|
|
|
if (!FileNameOrErr)
|
|
|
|
return FileNameOrErr.takeError();
|
|
|
|
InputFiles.emplace_back(*FileNameOrErr);
|
2022-04-12 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// Link the remaining device files, if necessary, using the device linker.
|
|
|
|
bool RequiresLinking =
|
|
|
|
!Input.empty() || (!EmbedBitcode && !Triple.isNVPTX());
|
|
|
|
auto OutputOrErr = (RequiresLinking) ? linkDevice(InputFiles, Triple, Arch)
|
|
|
|
: InputFiles.front();
|
|
|
|
if (!OutputOrErr)
|
|
|
|
return OutputOrErr.takeError();
|
2022-04-12 23:21:36 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// Store the offloading image for each linked output file.
|
|
|
|
for (OffloadKind Kind : ActiveOffloadKinds) {
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
|
|
|
|
llvm::MemoryBuffer::getFileOrSTDIN(*OutputOrErr);
|
|
|
|
if (std::error_code EC = FileOrErr.getError())
|
|
|
|
return createFileError(*OutputOrErr, EC);
|
|
|
|
|
|
|
|
OffloadingImage TheImage{};
|
|
|
|
TheImage.TheImageKind = IMG_Object;
|
|
|
|
TheImage.TheOffloadKind = Kind;
|
|
|
|
TheImage.StringData = {{"triple", TripleStr}, {"arch", Arch}};
|
|
|
|
TheImage.Image = std::move(*FileOrErr);
|
|
|
|
Images[Kind].emplace_back(std::move(TheImage));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a binary image of each offloading image and embed it into a new
|
|
|
|
// object file.
|
|
|
|
SmallVector<std::string> WrappedOutput;
|
|
|
|
for (const auto &KindAndImages : Images) {
|
|
|
|
OffloadKind Kind = KindAndImages.first;
|
|
|
|
auto BundledImagesOrErr =
|
|
|
|
bundleLinkedOutput(KindAndImages.second, KindAndImages.first);
|
|
|
|
if (!BundledImagesOrErr)
|
|
|
|
return BundledImagesOrErr.takeError();
|
|
|
|
auto OutputOrErr = wrapDeviceImages(*BundledImagesOrErr, Kind);
|
|
|
|
if (!OutputOrErr)
|
|
|
|
return OutputOrErr.takeError();
|
|
|
|
WrappedOutput.push_back(*OutputOrErr);
|
2022-04-12 23:21:36 +08:00
|
|
|
}
|
2022-04-20 02:14:16 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
return WrappedOutput;
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
|
2022-01-06 02:21:03 +08:00
|
|
|
Optional<std::string> findFile(StringRef Dir, const Twine &Name) {
|
|
|
|
SmallString<128> Path;
|
2022-03-03 01:38:29 +08:00
|
|
|
if (Dir.startswith("="))
|
|
|
|
sys::path::append(Path, Sysroot, Dir.substr(1), Name);
|
|
|
|
else
|
|
|
|
sys::path::append(Path, Dir, Name);
|
|
|
|
|
2022-01-06 02:21:03 +08:00
|
|
|
if (sys::fs::exists(Path))
|
|
|
|
return static_cast<std::string>(Path);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<std::string> findFromSearchPaths(StringRef Name,
|
|
|
|
ArrayRef<StringRef> SearchPaths) {
|
|
|
|
for (StringRef Dir : SearchPaths)
|
|
|
|
if (Optional<std::string> File = findFile(Dir, Name))
|
|
|
|
return File;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<std::string> searchLibraryBaseName(StringRef Name,
|
|
|
|
ArrayRef<StringRef> SearchPaths) {
|
|
|
|
for (StringRef Dir : SearchPaths) {
|
2022-01-08 06:12:51 +08:00
|
|
|
if (Optional<std::string> File = findFile(Dir, "lib" + Name + ".so"))
|
|
|
|
return None;
|
2022-01-06 02:21:03 +08:00
|
|
|
if (Optional<std::string> File = findFile(Dir, "lib" + Name + ".a"))
|
|
|
|
return File;
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Search for static libraries in the linker's library path given input like
|
|
|
|
/// `-lfoo` or `-l:libfoo.a`.
|
|
|
|
Optional<std::string> searchLibrary(StringRef Input,
|
|
|
|
ArrayRef<StringRef> SearchPaths) {
|
|
|
|
if (!Input.startswith("-l"))
|
|
|
|
return None;
|
|
|
|
StringRef Name = Input.drop_front(2);
|
|
|
|
if (Name.startswith(":"))
|
|
|
|
return findFromSearchPaths(Name.drop_front(), SearchPaths);
|
|
|
|
return searchLibraryBaseName(Name, SearchPaths);
|
|
|
|
}
|
|
|
|
|
2022-01-04 01:31:52 +08:00
|
|
|
} // namespace
|
|
|
|
|
2021-12-31 05:41:36 +08:00
|
|
|
int main(int argc, const char **argv) {
|
2022-01-04 01:31:52 +08:00
|
|
|
InitLLVM X(argc, argv);
|
2022-01-08 06:12:51 +08:00
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
InitializeAllAsmPrinters();
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-01-05 06:20:04 +08:00
|
|
|
LinkerExecutable = argv[0];
|
2021-12-31 05:41:36 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
|
|
|
cl::SetVersionPrinter(PrintVersion);
|
|
|
|
cl::HideUnrelatedOptions(ClangLinkerWrapperCategory);
|
|
|
|
cl::ParseCommandLineOptions(
|
|
|
|
argc, argv,
|
|
|
|
"A wrapper utility over the host linker. It scans the input files for\n"
|
|
|
|
"sections that require additional processing prior to linking. The tool\n"
|
|
|
|
"will then transparently pass all arguments and input to the specified\n"
|
|
|
|
"host linker to create the final binary.\n");
|
|
|
|
|
|
|
|
if (Help) {
|
|
|
|
cl::PrintHelpMessage();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto reportError = [argv](Error E) {
|
|
|
|
logAllUnhandledErrors(std::move(E), WithColor::error(errs(), argv[0]));
|
2022-01-05 06:20:04 +08:00
|
|
|
return EXIT_FAILURE;
|
2021-12-31 05:41:36 +08:00
|
|
|
};
|
|
|
|
|
2022-02-04 05:41:47 +08:00
|
|
|
if (!CudaPath.empty())
|
|
|
|
CudaBinaryPath = CudaPath + "/bin";
|
|
|
|
|
2022-03-03 01:38:29 +08:00
|
|
|
auto RootIt = llvm::find_if(HostLinkerArgs, [](StringRef Arg) {
|
|
|
|
return Arg.startswith("--sysroot=");
|
|
|
|
});
|
|
|
|
if (RootIt != HostLinkerArgs.end())
|
|
|
|
Sysroot = StringRef(*RootIt).split('=').second;
|
|
|
|
|
|
|
|
ExecutableName = *std::next(llvm::find(HostLinkerArgs, "-o"));
|
2022-01-05 06:20:04 +08:00
|
|
|
SmallVector<std::string, 16> LinkerArgs;
|
|
|
|
for (const std::string &Arg : HostLinkerArgs)
|
|
|
|
LinkerArgs.push_back(Arg);
|
|
|
|
|
2022-01-06 02:21:03 +08:00
|
|
|
SmallVector<StringRef, 16> LibraryPaths;
|
2022-01-26 00:23:27 +08:00
|
|
|
for (StringRef Arg : LinkerArgs) {
|
2022-01-06 02:21:03 +08:00
|
|
|
if (Arg.startswith("-L"))
|
|
|
|
LibraryPaths.push_back(Arg.drop_front(2));
|
2022-01-18 23:56:12 +08:00
|
|
|
}
|
2022-01-06 02:21:03 +08:00
|
|
|
|
2022-05-13 00:39:15 +08:00
|
|
|
// Try to extract device code from the linker input.
|
2022-06-06 23:36:45 +08:00
|
|
|
SmallVector<OffloadFile, 4> InputFiles;
|
|
|
|
SmallVector<OffloadFile, 4> LazyInputFiles;
|
2022-05-13 01:22:08 +08:00
|
|
|
for (StringRef Arg : LinkerArgs) {
|
2022-01-18 23:56:12 +08:00
|
|
|
if (Arg == ExecutableName)
|
|
|
|
continue;
|
|
|
|
|
2022-05-13 01:22:08 +08:00
|
|
|
// Search the inpuot argument for embedded device files if it is a static
|
|
|
|
// library or regular input file.
|
|
|
|
if (Optional<std::string> Library = searchLibrary(Arg, LibraryPaths)) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(*Library);
|
|
|
|
if (std::error_code EC = BufferOrErr.getError())
|
|
|
|
return reportError(createFileError(*Library, EC));
|
2022-01-06 02:21:03 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
if (Error Err =
|
|
|
|
extractFromBuffer(std::move(*BufferOrErr), LazyInputFiles))
|
2022-05-13 01:22:08 +08:00
|
|
|
return reportError(std::move(Err));
|
|
|
|
} else if (sys::fs::exists(Arg) && !sys::fs::is_directory(Arg)) {
|
2022-01-04 01:31:52 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
2022-05-13 01:22:08 +08:00
|
|
|
MemoryBuffer::getFileOrSTDIN(Arg);
|
2022-01-04 01:31:52 +08:00
|
|
|
if (std::error_code EC = BufferOrErr.getError())
|
2022-05-13 01:22:08 +08:00
|
|
|
return reportError(createFileError(Arg, EC));
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
if (sys::path::extension(Arg).endswith(".a")) {
|
|
|
|
if (Error Err =
|
|
|
|
extractFromBuffer(std::move(*BufferOrErr), LazyInputFiles))
|
|
|
|
return reportError(std::move(Err));
|
|
|
|
} else {
|
|
|
|
if (Error Err = extractFromBuffer(std::move(*BufferOrErr), InputFiles))
|
|
|
|
return reportError(std::move(Err));
|
|
|
|
}
|
2022-01-04 01:31:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
for (StringRef Library : BitcodeLibraries) {
|
|
|
|
auto FileOrErr = getInputBitcodeLibrary(Library);
|
|
|
|
if (!FileOrErr)
|
|
|
|
return reportError(FileOrErr.takeError());
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseSet<OffloadFile::TargetID> IsTargetUsed;
|
|
|
|
for (const auto &File : InputFiles)
|
|
|
|
IsTargetUsed.insert(File);
|
2022-01-11 23:53:59 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// We should only include input files that are used.
|
|
|
|
// TODO: Only load a library if it defined undefined symbols in the input.
|
|
|
|
for (auto &LazyFile : LazyInputFiles)
|
|
|
|
if (IsTargetUsed.contains(LazyFile))
|
|
|
|
InputFiles.emplace_back(std::move(LazyFile));
|
|
|
|
LazyInputFiles.clear();
|
2022-01-04 01:31:52 +08:00
|
|
|
|
2022-06-06 23:36:45 +08:00
|
|
|
// Link and wrap the device images extracted from the linker input.
|
|
|
|
auto FilesOrErr = linkAndWrapDeviceFiles(InputFiles);
|
|
|
|
if (!FilesOrErr)
|
|
|
|
return reportError(FilesOrErr.takeError());
|
2022-04-13 23:48:07 +08:00
|
|
|
|
|
|
|
// We need to insert the new files next to the old ones to make sure they're
|
|
|
|
// linked with the same libraries / arguments.
|
2022-06-06 23:36:45 +08:00
|
|
|
if (!FilesOrErr->empty()) {
|
2022-05-13 00:39:15 +08:00
|
|
|
auto *FirstInput = std::next(llvm::find_if(LinkerArgs, [](StringRef Str) {
|
2022-04-13 23:48:07 +08:00
|
|
|
return sys::fs::exists(Str) && !sys::fs::is_directory(Str) &&
|
|
|
|
Str != ExecutableName;
|
|
|
|
}));
|
2022-06-06 23:36:45 +08:00
|
|
|
LinkerArgs.insert(FirstInput, FilesOrErr->begin(), FilesOrErr->end());
|
2022-04-13 23:48:07 +08:00
|
|
|
}
|
2021-12-31 05:41:36 +08:00
|
|
|
|
2022-01-04 01:31:52 +08:00
|
|
|
// Run the host linking job.
|
2022-01-05 06:20:04 +08:00
|
|
|
if (Error Err = runLinker(LinkerUserPath, LinkerArgs))
|
|
|
|
return reportError(std::move(Err));
|
2021-12-31 05:41:36 +08:00
|
|
|
|
2022-01-05 06:20:04 +08:00
|
|
|
// Remove the temporary files created.
|
|
|
|
for (const auto &TempFile : TempFiles)
|
2022-01-04 01:31:52 +08:00
|
|
|
if (std::error_code EC = sys::fs::remove(TempFile))
|
|
|
|
reportError(createFileError(TempFile, EC));
|
|
|
|
|
2021-12-31 05:41:36 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|