[clang] [Draft][HIP][Driver] Use alternative `/lib64` if `/lib` doesn't exist (PR #211587)
Juan Manuel Martinez CaamaƱo via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 24 08:32:25 PDT 2026
https://github.com/jmmartinez updated https://github.com/llvm/llvm-project/pull/211587
>From 4806135f236a8d9d9303ea60e899a388e6daf5b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Thu, 23 Jul 2026 17:05:05 +0200
Subject: [PATCH 1/2] [HIP][Driver] Use alternative `/lib64` if `/lib` doesn't
exist
On non-standard ROCm installations, `libamdhsa64.so` may be under `/lib64`
instead of `/lib`. To accomodate for these, if `/lib` does not exists
and `/lib64` does, use the later.
If both exist `/lib` is preferred.
By default we conservatively use `/lib`.
---
clang/lib/Driver/ToolChains/AMDGPU.cpp | 5 ++++
.../Driver/Inputs/rocm/lib/libamdhip64.so | 1 +
clang/test/Driver/rocm-detect-libdir.hip | 26 +++++++++++++++++++
3 files changed, 32 insertions(+)
create mode 100644 clang/test/Driver/Inputs/rocm/lib/libamdhip64.so
create mode 100644 clang/test/Driver/rocm-detect-libdir.hip
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 5893f6f6b2915..fdec63f301f47 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -456,8 +456,13 @@ void RocmInstallationDetector::detectHIPRuntime() {
llvm::sys::path::append(BinPath, "bin");
IncludePath = InstallPath;
llvm::sys::path::append(IncludePath, "include");
+
+ // Fallback to rocm/lib64 if rocm/lib doesn't exist.
LibPath = InstallPath;
llvm::sys::path::append(LibPath, "lib");
+ if (!D.getVFS().exists(LibPath) && D.getVFS().exists(LibPath + "64"))
+ LibPath.append("64");
+
SharePath = InstallPath;
llvm::sys::path::append(SharePath, "share");
diff --git a/clang/test/Driver/Inputs/rocm/lib/libamdhip64.so b/clang/test/Driver/Inputs/rocm/lib/libamdhip64.so
new file mode 100644
index 0000000000000..f3b868da4afb9
--- /dev/null
+++ b/clang/test/Driver/Inputs/rocm/lib/libamdhip64.so
@@ -0,0 +1 @@
+Intentionally empty.
diff --git a/clang/test/Driver/rocm-detect-libdir.hip b/clang/test/Driver/rocm-detect-libdir.hip
new file mode 100644
index 0000000000000..e7ee753443bf0
--- /dev/null
+++ b/clang/test/Driver/rocm-detect-libdir.hip
@@ -0,0 +1,26 @@
+// UNSUPPORTED: system-windows
+
+// Test that /lib64 is used if /lib doesn't exist.
+// RUN: rm -rf %t/*
+// RUN: mkdir -p %t/rocm
+// RUN: cp -R %S/Inputs/rocm/bin %t/rocm/bin
+// RUN: cp -R %S/Inputs/rocm/amdgcn %t/rocm/amdgcn
+// RUN: cp -R %S/Inputs/rocm/include %t/rocm/include
+// RUN: cp -R %S/Inputs/rocm/lib %t/rocm/lib64
+// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
+// RUN: --rocm-path=%t/rocm --hip-link %s 2>&1 \
+// RUN: | FileCheck -check-prefixes=LIB64 %s
+
+// Test that in the presence of both /lib and /lib64, the former is preferred.
+// RUN: cp -R %S/Inputs/rocm/lib %t/rocm/lib
+// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
+// RUN: --rocm-path=%t/rocm --hip-link %s 2>&1 \
+// RUN: | FileCheck -check-prefixes=LIB %s
+
+// LIB64: "[[ROCM:[^"]+/rocm]]/lib64/libamdhip64.so"
+// LIB64: "-L[[ROCM]]/lib64"
+// LIB64-NOT: "[[ROCM]]/lib/libamdhip64.so"
+
+// LIB: "[[ROCM:[^"]+/rocm]]/lib/libamdhip64.so"
+// LIB: "-L[[ROCM]]/lib"
+// LIB-NOT: "[[ROCM]]/lib64/libamdhip64.so"
>From 6fd30f3117d7555ac9665f3fb4e44f56df387e61 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 24 Jul 2026 17:29:37 +0200
Subject: [PATCH 2/2] Update
---
.../clang/Driver/RocmInstallationDetector.h | 2 +-
clang/lib/Driver/ToolChains/AMDGPU.cpp | 26 ++++++++++++++-----
.../test/Driver/Inputs/rocm/lib/amdhip64.lib | 1 +
clang/test/Driver/rocm-detect-libdir.hip | 16 ++++++++++++
4 files changed, 37 insertions(+), 8 deletions(-)
create mode 100644 clang/test/Driver/Inputs/rocm/lib/amdhip64.lib
diff --git a/clang/include/clang/Driver/RocmInstallationDetector.h b/clang/include/clang/Driver/RocmInstallationDetector.h
index ab669ef315361..6481507692b25 100644
--- a/clang/include/clang/Driver/RocmInstallationDetector.h
+++ b/clang/include/clang/Driver/RocmInstallationDetector.h
@@ -267,7 +267,7 @@ class RocmInstallationDetector {
llvm::opt::ArgStringList &CC1Args) const;
void detectDeviceLibrary();
- void detectHIPRuntime();
+ void detectHIPRuntime(const llvm::Triple &HostTriple);
/// Get the values for --rocm-device-lib-path arguments
ArrayRef<std::string> getRocmDeviceLibPathArg() const {
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index fdec63f301f47..4e669d3695c92 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -368,7 +368,7 @@ RocmInstallationDetector::RocmInstallationDetector(
}
if (DetectHIPRuntime)
- detectHIPRuntime();
+ detectHIPRuntime(HostTriple);
}
void RocmInstallationDetector::detectDeviceLibrary() {
@@ -434,7 +434,8 @@ void RocmInstallationDetector::detectDeviceLibrary() {
}
}
-void RocmInstallationDetector::detectHIPRuntime() {
+void RocmInstallationDetector::detectHIPRuntime(
+ const llvm::Triple &HostTriple) {
SmallVector<Candidate, 4> HIPSearchDirs;
if (!HIPPathArg.empty())
HIPSearchDirs.emplace_back(HIPPathArg.str());
@@ -457,11 +458,22 @@ void RocmInstallationDetector::detectHIPRuntime() {
IncludePath = InstallPath;
llvm::sys::path::append(IncludePath, "include");
- // Fallback to rocm/lib64 if rocm/lib doesn't exist.
- LibPath = InstallPath;
- llvm::sys::path::append(LibPath, "lib");
- if (!D.getVFS().exists(LibPath) && D.getVFS().exists(LibPath + "64"))
- LibPath.append("64");
+ // ROCm's lib path is the place where the amdhsa64 library is located.
+ // Probe for it and fallback to /rocm/lib if we cannot find it.
+ StringRef LibAmdHip64 =
+ HostTriple.isOSMSVCRT() ? "amdhip64.lib" : "libamdhip64.so";
+ for (StringRef LibPathSuffix : {"lib", "lib64"}) {
+ SmallString<0> LibAmdHip64Location;
+ llvm::sys::path::append(LibAmdHip64Location, InstallPath, LibPathSuffix,
+ LibAmdHip64);
+ if (FS.exists(LibAmdHip64Location)) {
+ llvm::sys::path::append(LibPath, InstallPath, LibPathSuffix);
+ break;
+ }
+ }
+
+ if (LibPath.empty())
+ llvm::sys::path::append(LibPath, InstallPath, "lib");
SharePath = InstallPath;
llvm::sys::path::append(SharePath, "share");
diff --git a/clang/test/Driver/Inputs/rocm/lib/amdhip64.lib b/clang/test/Driver/Inputs/rocm/lib/amdhip64.lib
new file mode 100644
index 0000000000000..f3b868da4afb9
--- /dev/null
+++ b/clang/test/Driver/Inputs/rocm/lib/amdhip64.lib
@@ -0,0 +1 @@
+Intentionally empty.
diff --git a/clang/test/Driver/rocm-detect-libdir.hip b/clang/test/Driver/rocm-detect-libdir.hip
index e7ee753443bf0..542edfe74f3b9 100644
--- a/clang/test/Driver/rocm-detect-libdir.hip
+++ b/clang/test/Driver/rocm-detect-libdir.hip
@@ -7,16 +7,26 @@
// RUN: cp -R %S/Inputs/rocm/amdgcn %t/rocm/amdgcn
// RUN: cp -R %S/Inputs/rocm/include %t/rocm/include
// RUN: cp -R %S/Inputs/rocm/lib %t/rocm/lib64
+
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
// RUN: --rocm-path=%t/rocm --hip-link %s 2>&1 \
// RUN: | FileCheck -check-prefixes=LIB64 %s
+// RUN: %clang -### -target x86_64-windows-msvc --offload-arch=gfx1010 \
+// RUN: --rocm-path=%t/rocm --hip-link %s 2>&1 \
+// RUN: | FileCheck -check-prefixes=WIN_LIB64 %s
+
// Test that in the presence of both /lib and /lib64, the former is preferred.
// RUN: cp -R %S/Inputs/rocm/lib %t/rocm/lib
+
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
// RUN: --rocm-path=%t/rocm --hip-link %s 2>&1 \
// RUN: | FileCheck -check-prefixes=LIB %s
+// RUN: %clang -### -target x86_64-windows-msvc --offload-arch=gfx1010 \
+// RUN: --rocm-path=%t/rocm --hip-link %s 2>&1 \
+// RUN: | FileCheck -check-prefixes=WIN_LIB %s
+
// LIB64: "[[ROCM:[^"]+/rocm]]/lib64/libamdhip64.so"
// LIB64: "-L[[ROCM]]/lib64"
// LIB64-NOT: "[[ROCM]]/lib/libamdhip64.so"
@@ -24,3 +34,9 @@
// LIB: "[[ROCM:[^"]+/rocm]]/lib/libamdhip64.so"
// LIB: "-L[[ROCM]]/lib"
// LIB-NOT: "[[ROCM]]/lib64/libamdhip64.so"
+
+// WIN_LIB64: "-libpath:{{.*rocm[/\\]lib64}}" "amdhip64.lib"
+// WIN_LIB64-NOT: "-libpath:{{.*rocm[/\\]lib}}" "amdhip64.lib"
+
+// WIN_LIB: "-libpath:{{.*rocm[/\\]lib}}" "amdhip64.lib"
+// WIN_LIB-NOT: "-libpath:{{.*rocm[/\\]lib64}}" "amdhip64.lib"
More information about the cfe-commits
mailing list