Skip to content

Commit

Permalink
[OpenMP] Add support for linking AMDGPU images
Browse files Browse the repository at this point in the history
This patch adds support for linking AMDGPU images using the LLD binary.
AMDGPU files are always bitcode images and will always use the LTO
backend. Additionally we now pass the default architecture found with
the `amdgpu-arch` tool to the argument list.

Depends on D117156

Differential Revision: https://reviews.llvm.org/D117246
  • Loading branch information
jhuber6 committed Feb 1, 2022
1 parent cb7cfae commit ce16ca3
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Expand Up @@ -501,7 +501,7 @@ Expected<std::string> assemble(StringRef InputFile, Triple TheTriple,
// Create a new file to write the linked device image to.
SmallString<128> TempFile;
if (std::error_code EC = sys::fs::createTemporaryFile(
TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
"lto-" + TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
return createFileError(TempFile, EC);
TempFiles.push_back(static_cast<std::string>(TempFile));

Expand Down Expand Up @@ -576,6 +576,50 @@ Expected<std::string> link(ArrayRef<std::string> InputFiles,
return static_cast<std::string>(TempFile);
}
} // namespace nvptx
namespace amdgcn {
Expected<std::string> link(ArrayRef<std::string> InputFiles,
ArrayRef<std::string> LinkerArgs, Triple TheTriple,
StringRef Arch) {
// AMDGPU uses the lld binary to link device object files.
ErrorOr<std::string> LLDPath =
sys::findProgramByName("lld", sys::path::parent_path(LinkerExecutable));
if (!LLDPath)
LLDPath = sys::findProgramByName("lld");
if (!LLDPath)
return createStringError(LLDPath.getError(),
"Unable to find 'lld' in path");

// Create a new file to write the linked device image to.
SmallString<128> TempFile;
if (std::error_code EC = sys::fs::createTemporaryFile(
TheTriple.getArchName() + "-" + Arch + "-image", "out", TempFile))
return createFileError(TempFile, EC);
TempFiles.push_back(static_cast<std::string>(TempFile));

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);

// Copy system library paths used by the host linker.
for (StringRef Arg : LinkerArgs)
if (Arg.startswith("-L"))
CmdArgs.push_back(Arg);

// Add extracted input files.
for (StringRef Input : InputFiles)
CmdArgs.push_back(Input);

if (sys::ExecuteAndWait(*LLDPath, CmdArgs))
return createStringError(inconvertibleErrorCode(), "'lld' failed");

return static_cast<std::string>(TempFile);
}
} // namespace amdgcn

Expected<std::string> linkDevice(ArrayRef<std::string> InputFiles,
ArrayRef<std::string> LinkerArgs,
Expand All @@ -585,7 +629,7 @@ Expected<std::string> linkDevice(ArrayRef<std::string> InputFiles,
case Triple::nvptx64:
return nvptx::link(InputFiles, LinkerArgs, TheTriple, Arch);
case Triple::amdgcn:
// TODO: AMDGCN linking support.
return amdgcn::link(InputFiles, LinkerArgs, TheTriple, Arch);
case Triple::x86:
case Triple::x86_64:
// TODO: x86 linking support.
Expand Down

0 comments on commit ce16ca3

Please sign in to comment.