Author: Juan Manuel Martinez CaamaƱo
Date: 2026-08-07T11:50:21+02:00
New Revision: cfabc8e1dc710c11736b7007545d39dddb09b2fc

URL: 
https://github.com/llvm/llvm-project/commit/cfabc8e1dc710c11736b7007545d39dddb09b2fc
DIFF: 
https://github.com/llvm/llvm-project/commit/cfabc8e1dc710c11736b7007545d39dddb09b2fc.diff

LOG: [HIP][Driver] Use alternative `/lib64` or `/lib` depending on `amdhip64` 
library location (#211587)

On non-standard ROCm installations, `libamdhip64.so` may be under
`/lib64` instead of `/lib`. To acomodate for these, if
`/lib/libamdhip64.so` does not exists and `/lib64` does, use the later.
On windows we check for `amdhip64.lib`.

If both exist `/lib` is preferred.

By default we conservatively use `/lib`.

Related to LCOMPILER-2495.

Added: 
    clang/test/Driver/Inputs/rocm/lib/amdhip64.lib
    clang/test/Driver/Inputs/rocm/lib/libamdhip64.so
    clang/test/Driver/rocm-detect-libdir.hip

Modified: 
    clang/include/clang/Driver/RocmInstallationDetector.h
    clang/lib/Driver/ToolChains/AMDGPU.cpp

Removed: 
    


################################################################################
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 9718c621698d8..3d8d1e493570a 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());
@@ -456,8 +457,25 @@ void RocmInstallationDetector::detectHIPRuntime() {
     llvm::sys::path::append(BinPath, "bin");
     IncludePath = InstallPath;
     llvm::sys::path::append(IncludePath, "include");
-    LibPath = InstallPath;
-    llvm::sys::path::append(LibPath, "lib");
+
+    // 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";
+    LibPath.clear();
+    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/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..542edfe74f3b9
--- /dev/null
+++ b/clang/test/Driver/rocm-detect-libdir.hip
@@ -0,0 +1,42 @@
+// 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
+
+// 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"
+
+// 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"


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to