Skip to content

Commit

Permalink
[HIP] support --offload-arch=native
Browse files Browse the repository at this point in the history
This patch detects system GPU and use them
in --offload-arch if 'native' is specified. If system GPU
cannot be detected clang will fall back to the default GPU arch.

Reviewed by: Artem Belevich

Differential Revision: https://reviews.llvm.org/D139045
  • Loading branch information
yxsamliu committed Dec 13, 2022
1 parent 5a288fa commit e8fd998
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions clang/lib/Driver/Driver.cpp
Expand Up @@ -3067,6 +3067,17 @@ class OffloadingActionBuilder final {
if (A->getOption().matches(options::OPT_no_offload_arch_EQ) &&
ArchStr == "all") {
GpuArchs.clear();
} else if (ArchStr == "native" &&
ToolChains.front()->getTriple().isAMDGPU()) {
auto *TC = static_cast<const toolchains::HIPAMDToolChain *>(
ToolChains.front());
SmallVector<std::string, 1> GPUs;
auto Err = TC->detectSystemGPUs(Args, GPUs);
if (!Err) {
for (auto GPU : GPUs)
GpuArchs.insert(Args.MakeArgString(GPU));
} else
llvm::consumeError(std::move(Err));
} else {
ArchStr = getCanonicalOffloadArch(ArchStr);
if (ArchStr.empty()) {
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Driver/ToolChains/AMDGPU.h
Expand Up @@ -107,6 +107,9 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
llvm::Error getSystemGPUArch(const llvm::opt::ArgList &Args,
std::string &GPUArch) const;

llvm::Error detectSystemGPUs(const llvm::opt::ArgList &Args,
SmallVector<std::string, 1> &GPUArchs) const;

protected:
/// Check and diagnose invalid target ID specified by -mcpu.
virtual void checkTargetID(const llvm::opt::ArgList &DriverArgs) const;
Expand All @@ -126,8 +129,6 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
/// Get GPU arch from -mcpu without checking.
StringRef getGPUArch(const llvm::opt::ArgList &DriverArgs) const;

llvm::Error detectSystemGPUs(const llvm::opt::ArgList &Args,
SmallVector<std::string, 1> &GPUArchs) const;
};

class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
Expand Down

0 comments on commit e8fd998

Please sign in to comment.