Skip to content

Commit

Permalink
[HIP] search fatbin symbols for libs passed by -l
Browse files Browse the repository at this point in the history
For -fgpu-rdc linking, clang needs to collect undefined fatbin
symbols and resolve them to the embedded fatbin.

This has been done for object files and archive files passed
as input files to clang.

However, the same action is not performed for archive files passed
through -l options, which causes missing fatbin symbols.
  • Loading branch information
yxsamliu committed Aug 18, 2024
1 parent d082f1f commit 6e6bb35
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
75 changes: 70 additions & 5 deletions clang/lib/Driver/ToolChains/HIPUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ static std::string normalizeForBundler(const llvm::Triple &T,
// input object or archive files.
class HIPUndefinedFatBinSymbols {
public:
HIPUndefinedFatBinSymbols(const Compilation &C)
: C(C), DiagID(C.getDriver().getDiags().getCustomDiagID(
DiagnosticsEngine::Error,
"Error collecting HIP undefined fatbin symbols: %0")),
HIPUndefinedFatBinSymbols(const Compilation &C,
const llvm::opt::ArgList &Args_)
: C(C), Args(Args_),
DiagID(C.getDriver().getDiags().getCustomDiagID(
DiagnosticsEngine::Error,
"Error collecting HIP undefined fatbin symbols: %0")),
Quiet(C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)),
Verbose(C.getArgs().hasArg(options::OPT_v)) {
populateSymbols();
processStaticLibraries();
if (Verbose) {
for (const auto &Name : FatBinSymbols)
llvm::errs() << "Found undefined HIP fatbin symbol: " << Name << "\n";
Expand All @@ -76,8 +79,70 @@ class HIPUndefinedFatBinSymbols {
return GPUBinHandleSymbols;
}

// Collect symbols from static libraries specified by -l options.
void processStaticLibraries() {
llvm::SmallVector<llvm::StringRef, 16> LibNames;
llvm::SmallVector<llvm::StringRef, 16> LibPaths;
llvm::SmallVector<llvm::StringRef, 16> ExactLibNames;
llvm::Triple Triple(C.getDriver().getTargetTriple());
bool IsMSVC = Triple.isWindowsMSVCEnvironment();
llvm::StringRef Ext = IsMSVC ? ".lib" : ".a";

for (const auto *Arg : Args.filtered(options::OPT_l)) {
llvm::StringRef Value = Arg->getValue();
if (Value.starts_with(":"))
ExactLibNames.push_back(Value.drop_front());
else
LibNames.push_back(Value);
}
for (const auto *Arg : Args.filtered(options::OPT_L)) {
auto Path = Arg->getValue();
LibPaths.push_back(Path);
if (Verbose)
llvm::errs() << "HIP fatbin symbol search uses library path: " << Path
<< "\n";
}

auto ProcessLib = [&](llvm::StringRef LibName, bool IsExact) {
llvm::SmallString<256> FullLibName(
IsExact ? Twine(LibName).str()
: IsMSVC ? (Twine(LibName) + Ext).str()
: (Twine("lib") + LibName + Ext).str());

bool Found = false;
for (const auto Path : LibPaths) {
llvm::SmallString<256> FullPath = Path;
llvm::sys::path::append(FullPath, FullLibName);

if (llvm::sys::fs::exists(FullPath)) {
if (Verbose)
llvm::errs() << "HIP fatbin symbol search found library: "
<< FullPath << "\n";
auto BufferOrErr = llvm::MemoryBuffer::getFile(FullPath);
if (!BufferOrErr) {
errorHandler(llvm::errorCodeToError(BufferOrErr.getError()));
continue;
}
processInput(BufferOrErr.get()->getMemBufferRef());
Found = true;
break;
}
}
if (!Found && Verbose)
llvm::errs() << "HIP fatbin symbol search could not find library: "
<< FullLibName << "\n";
};

for (const auto LibName : ExactLibNames)
ProcessLib(LibName, true);

for (const auto LibName : LibNames)
ProcessLib(LibName, false);
}

private:
const Compilation &C;
const llvm::opt::ArgList &Args;
unsigned DiagID;
bool Quiet;
bool Verbose;
Expand Down Expand Up @@ -301,7 +366,7 @@ void HIP::constructGenerateObjFileFromHIPFatBinary(
auto HostTriple =
C.getSingleOffloadToolChain<Action::OFK_Host>()->getTriple();

HIPUndefinedFatBinSymbols Symbols(C);
HIPUndefinedFatBinSymbols Symbols(C, Args);

std::string PrimaryHipFatbinSymbol;
std::string PrimaryGpuBinHandleSymbol;
Expand Down
23 changes: 23 additions & 0 deletions clang/test/Driver/hip-toolchain-rdc.hip
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
// RUN: 2>&1 | FileCheck -check-prefixes=CHECK,MSVC %s

// Test fatbin symbol search for -l libraries.

// RUN: touch librdctest.a rdctest2.bin
// RUN: %clang -### --target=x86_64-linux-gnu -v \
// RUN: -fgpu-rdc -nogpuinc -nogpulib \
// RUN: --no-offload-new-driver -L. -lrdctest -l:rdctest2.bin \
// RUN: %s \
// RUN: 2>&1 | FileCheck -check-prefixes=LIB,LNX-LIB %s
// RUN: rm librdctest.a rdctest2.bin

// RUN: touch rdctest.lib rdctest2.bin
// RUN: %clang -### --target=x86_64-pc-windows-msvc -v \
// RUN: -fgpu-rdc -nogpuinc -nogpulib \
// RUN: --no-offload-new-driver -L. -lrdctest -l:rdctest2.bin \
// RUN: %s \
// RUN: 2>&1 | FileCheck -check-prefixes=LIB,MSVC-LIB %s
// RUN: rm rdctest.lib rdctest2.bin

// LIB: HIP fatbin symbol search uses library path: .
// LIB: HIP fatbin symbol search found library: .{{/|\\}}rdctest2.bin
// LNX-LIB: HIP fatbin symbol search found library: .{{/|\\}}librdctest.a
// MSVC-LIB: HIP fatbin symbol search found library: .{{/|\\}}rdctest.lib

// check HIP fatbin and gpubin handle symbols and code object alignment in dumped llvm-mc input
// CHECK: Found undefined HIP fatbin symbol: __hip_fatbin_[[ID1:[0-9a-f]+]]
// CHECK: Found undefined HIP fatbin symbol: __hip_fatbin_[[ID2:[0-9a-f]+]]
Expand Down

0 comments on commit 6e6bb35

Please sign in to comment.