Skip to content

Commit e98b107

Browse files
committed
[HIP] change default offload archs
Currently, HIP uses gfx906 as the default offload arch, which only works on systems with gfx906. For non-interactive uses, this is less of a concern since they all set explicit offload archs for supported GPU's. The only use of default offload arch is when the clang wrapper hipcc is used as a C++ compiler during compiler detection of cmake, where the default offload arch is used to compile a C++ program as HIP program and executed. However since there is no kernel, the default offload arch just works. However, gfx906 as default offload arch is very inconenient for interactive users since in most cases they would like to compile for the GPU on the system. With this patch, if AMD GPU's are detected on the system, they will be used as default offload archs for HIP. Otherwise, if amd-llvm-spirv is found and executable, amdgcnspirv will be used as the default offload arch, since it works for all AMD GPU's supporting HIP. Otherwise, the original default offload arch is used, which is gfx906.
1 parent b249b49 commit e98b107

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ class ToolChain {
780780
virtual Expected<SmallVector<std::string>>
781781
getSystemGPUArchs(const llvm::opt::ArgList &Args) const;
782782

783+
/// getHIPDefaultOffloadArchs - Get the default offload arch's for HIP.
784+
virtual SmallVector<StringRef>
785+
getHIPDefaultOffloadArchs(const llvm::opt::ArgList &Args) const;
786+
783787
/// addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass
784788
/// a suitable profile runtime library to the linker.
785789
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args,

clang/lib/Driver/Driver.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3503,6 +3503,9 @@ class OffloadingActionBuilder final {
35033503
GpuArchList.push_back(OffloadArch::AMDGCNSPIRV);
35043504
else
35053505
GpuArchList.push_back(OffloadArch::Generic);
3506+
} else if (AssociatedOffloadKind == Action::OFK_HIP) {
3507+
for (auto A : ToolChains.front()->getHIPDefaultOffloadArchs(Args))
3508+
GpuArchList.push_back(A.data());
35063509
} else {
35073510
GpuArchList.push_back(DefaultOffloadArch);
35083511
}
@@ -4825,7 +4828,8 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
48254828
if (Kind == Action::OFK_Cuda) {
48264829
Archs.insert(OffloadArchToString(OffloadArch::CudaDefault));
48274830
} else if (Kind == Action::OFK_HIP) {
4828-
Archs.insert(OffloadArchToString(OffloadArch::HIPDefault));
4831+
for (auto A : TC->getHIPDefaultOffloadArchs(Args))
4832+
Archs.insert(A);
48294833
} else if (Kind == Action::OFK_SYCL) {
48304834
Archs.insert(StringRef());
48314835
} else if (Kind == Action::OFK_OpenMP) {

clang/lib/Driver/ToolChain.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,32 @@ ToolChain::getSystemGPUArchs(const llvm::opt::ArgList &Args) const {
15741574
return SmallVector<std::string>();
15751575
}
15761576

1577+
SmallVector<StringRef>
1578+
ToolChain::getHIPDefaultOffloadArchs(const llvm::opt::ArgList &Args) const {
1579+
if (getTriple().isSPIRV()) {
1580+
if (getTriple().getVendor() == llvm::Triple::AMD)
1581+
return {OffloadArchToString(OffloadArch::AMDGCNSPIRV)};
1582+
return {OffloadArchToString(OffloadArch::Generic)};
1583+
}
1584+
1585+
if (!getTriple().isAMDGPU())
1586+
return {};
1587+
1588+
SmallVector<StringRef> GpuArchList;
1589+
auto GPUsOrErr = getSystemGPUArchs(Args);
1590+
if (GPUsOrErr) {
1591+
for (auto &G : *GPUsOrErr)
1592+
GpuArchList.push_back(Args.MakeArgString(G));
1593+
return GpuArchList;
1594+
}
1595+
llvm::consumeError(GPUsOrErr.takeError());
1596+
auto Prog = GetProgramPath("amd-llvm-spirv");
1597+
if (!Prog.empty() && llvm::sys::fs::can_execute(Prog))
1598+
return {OffloadArchToString(OffloadArch::AMDGCNSPIRV)};
1599+
1600+
return {OffloadArchToString(OffloadArch::HIPDefault)};
1601+
}
1602+
15771603
SanitizerMask ToolChain::getSupportedSanitizers() const {
15781604
// Return sanitizers which don't require runtime support and are not
15791605
// platform dependent.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// RUN: %clang -### -nogpulib -nogpuinc -c %s 2>&1 | FileCheck %s
22

3-
// CHECK: {{.*}}clang{{.*}}"-target-cpu" "gfx906"
3+
// CHECK: {{.*}}clang{{.*}}"-triple" "{{amdgcn|spirv64}}-amd-amdhsa"{{.*}} "-target-cpu" "{{amdgcnspirv|gfx.*}}"

0 commit comments

Comments
 (0)