[clang] [clang][Driver][HIP] Support Debian multiarch libamdhip64.so layout (PR #208525)

Talha Can Havadar via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 6 05:04:43 PST 2026


https://github.com/talhaHavadar updated https://github.com/llvm/llvm-project/pull/208525

>From b7131566567a75bc01fc533e1cb3ffd77d88ce9c Mon Sep 17 00:00:00 2001
From: Talha Can Havadar <havadartalha at gmail.com>
Date: Mon, 6 Jul 2026 01:43:15 +0200
Subject: [PATCH 1/2] [clang][Driver][HIP] Support Debian multiarch
 libamdhip64.so layout
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

On Debian and derivatives (Ubuntu, Mint, Pop!_OS), libamdhip64.so is
installed under /usr/lib/<multiarch-triple>/ rather than /usr/lib/. As
a result, RocmInstallationDetector::detectHIPRuntime() sets
LibPath = <install>/lib and the driver then emits an absolute path
(<lib>/libamdhip64.so) via Linux::addOffloadRTLibs that does not
exist:

    $ clang++ -x hip --offload-arch=gfx900 hello.hip -lamdhip64 -o hello
    ld.lld: error: cannot open /usr/lib/libamdhip64.so: No such file
    or directory
    clang-linker-wrapper: error: 'ld.lld' failed
    clang++: error: linker command failed with exit code 1

Fix this in Linux::addOffloadRTLibs, where the ToolChain (and therefore
Linux::getMultiarchTriple()) is available: probe
<LibPath>/<multiarch-triple>/libamdhip64.so and, when the probe
succeeds, extend the HIP lib path with the multiarch subdirectory
before it is emitted. Using getMultiarchTriple() rather than the raw
target triple matters — builds whose default triple carries
`-unknown-` (e.g. `x86_64-unknown-linux-gnu`) still map to the correct
Debian directory name (`x86_64-linux-gnu`). The probe is guarded by
FS.exists() so non-multiarch layouts (Fedora, Arch, /opt/rocm) fall
through unchanged.

Doing the adjustment once in Linux::addOffloadRTLibs keeps the fix in
one place: the resulting HIPLibPath is then reused by every consumer
that previously called RocmInstallation->getLibPath() directly:

  * the absolute libamdhip64.so path emitted for the linker,
  * the `-L` search path added for HIP, and
  * `-rpath` (opt-in via -frtlib-add-rpath).

RocmInstallationDetector is left untouched, so MSVC.cpp — which uses
getLibPath() but does not participate in Linux multiarch — is
unaffected.

Reported at https://github.com/ROCm/llvm-project/issues/3169
---
 clang/lib/Driver/ToolChains/Linux.cpp         | 23 +++++++++++++---
 .../Inputs/rocm-multiarch/bin/.hipVersion     |  6 +++++
 .../lib/x86_64-linux-gnu/libamdhip64.so       |  0
 .../test/Driver/hip-multiarch-libs-linux.hip  | 26 +++++++++++++++++++
 4 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Driver/Inputs/rocm-multiarch/bin/.hipVersion
 create mode 100644 clang/test/Driver/Inputs/rocm-multiarch/lib/x86_64-linux-gnu/libamdhip64.so
 create mode 100644 clang/test/Driver/hip-multiarch-libs-linux.hip

diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
index 8d30a5ead3bbbb..b147f123a8b4e0 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -887,9 +887,27 @@ void Linux::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args,
       Args.hasArg(options::OPT_no_hip_rt) || Args.hasArg(options::OPT_r))
     return;
 
+  // On Debian and derivatives, the ROCm runtime ships under
+  // <rocm>/lib/<multiarch-triple>/ (e.g. /usr/lib/x86_64-linux-gnu/) rather
+  // than <rocm>/lib/. Prefer the multiarch subdir when libamdhip64.so lives
+  // there, so the absolute path emitted below, the -L search path, and any
+  // -rpath all resolve to a real file. Non-multiarch layouts (Fedora, Arch,
+  // /opt/rocm, etc.) are unaffected: the probe short-circuits when the
+  // multiarch subdir does not contain libamdhip64.so.
+  SmallString<0> HIPLibPath;
+  if (ActiveKinds & Action::OFK_HIP) {
+    HIPLibPath = RocmInstallation->getLibPath();
+    std::string MultiarchTriple =
+        getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
+    SmallString<0> MultiarchProbe = HIPLibPath;
+    llvm::sys::path::append(MultiarchProbe, MultiarchTriple, "libamdhip64.so");
+    if (getVFS().exists(MultiarchProbe))
+      llvm::sys::path::append(HIPLibPath, MultiarchTriple);
+  }
+
   llvm::SmallVector<std::pair<StringRef, StringRef>> Libraries;
   if (ActiveKinds & Action::OFK_HIP)
-    Libraries.emplace_back(RocmInstallation->getLibPath(), "libamdhip64.so");
+    Libraries.emplace_back(HIPLibPath, "libamdhip64.so");
   else if ((ActiveKinds & Action::OFK_SYCL) &&
            !Args.hasArg(options::OPT_nolibsycl))
     Libraries.emplace_back(SYCLInstallation->getSYCLRTLibPath(),
@@ -910,8 +928,7 @@ void Linux::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args,
 
   // FIXME: The ROCm builds implicitly depends on this being present.
   if (ActiveKinds & Action::OFK_HIP)
-    CmdArgs.push_back(
-        Args.MakeArgString(StringRef("-L") + RocmInstallation->getLibPath()));
+    CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + HIPLibPath));
 
   // For HIP device PGO, link clang_rt.profile_rocm when available. It is a
   // self-contained superset of clang_rt.profile, emitted first so the base
diff --git a/clang/test/Driver/Inputs/rocm-multiarch/bin/.hipVersion b/clang/test/Driver/Inputs/rocm-multiarch/bin/.hipVersion
new file mode 100644
index 00000000000000..677293c09139f5
--- /dev/null
+++ b/clang/test/Driver/Inputs/rocm-multiarch/bin/.hipVersion
@@ -0,0 +1,6 @@
+# Auto-generated by cmake
+# NOTE: The trailing whitespace is added on purpose to verify that these
+# whitespaces are trimmed before paring.
+HIP_VERSION_MAJOR=3 
+HIP_VERSION_MINOR=6
+HIP_VERSION_PATCH=20214-a2917cd
diff --git a/clang/test/Driver/Inputs/rocm-multiarch/lib/x86_64-linux-gnu/libamdhip64.so b/clang/test/Driver/Inputs/rocm-multiarch/lib/x86_64-linux-gnu/libamdhip64.so
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/hip-multiarch-libs-linux.hip b/clang/test/Driver/hip-multiarch-libs-linux.hip
new file mode 100644
index 00000000000000..a404e0fdc7f899
--- /dev/null
+++ b/clang/test/Driver/hip-multiarch-libs-linux.hip
@@ -0,0 +1,26 @@
+// UNSUPPORTED: system-windows
+
+// Test that Debian-style multiarch layouts, in which libamdhip64.so is
+// installed under <rocm>/lib/<multiarch-triple>/ rather than <rocm>/lib/, are
+// detected by the driver and reflected in the linker command line.
+
+// RUN: touch %t.o
+
+// Inputs/rocm-multiarch mimics a Debian multiarch layout:
+//   bin/.hipVersion signals a valid HIP install,
+//   lib/x86_64-linux-gnu/libamdhip64.so is the runtime the driver must find.
+// RUN: %clang -### --hip-link --target=x86_64-linux-gnu \
+// RUN:   --rocm-path=%S/Inputs/rocm-multiarch %t.o 2>&1 \
+// RUN:   | FileCheck -check-prefixes=MULTIARCH %s
+
+// MULTIARCH: "{{.*/Inputs/rocm-multiarch/lib/x86_64-linux-gnu/libamdhip64.so}}"
+// MULTIARCH: "-L{{.*/Inputs/rocm-multiarch/lib/x86_64-linux-gnu}}"
+
+// Sanity check: when libamdhip64.so is *not* under lib/<multiarch-triple>/,
+// the driver falls back to <rocm>/lib/ so non-Debian layouts are unchanged.
+// RUN: %clang -### --hip-link --target=x86_64-linux-gnu \
+// RUN:   --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
+// RUN:   | FileCheck -check-prefixes=FLAT %s
+
+// FLAT: "{{.*/Inputs/rocm/lib/libamdhip64.so}}"
+// FLAT-NOT: "{{.*/Inputs/rocm/lib/x86_64-linux-gnu/libamdhip64.so}}"

>From 5b9019f5f15f7770f7294c5644f98339037877d8 Mon Sep 17 00:00:00 2001
From: Talha Can Havadar <havadartalha at gmail.com>
Date: Sun, 6 Dec 2026 14:04:11 +0100
Subject: [PATCH 2/2] [clang][Driver][HIP] Merge HIP lib path checks, test
 -frtlib-add-rpath

Move the libamdhip64.so entry into the block that computes HIPLibPath,
so addOffloadRTLibs checks OFK_HIP once before the library loop.

Add a test that -frtlib-add-rpath emits the multiarch directory as the
-rpath. Trim the comments in the test and in addOffloadRTLibs.
---
 clang/lib/Driver/ToolChains/Linux.cpp         | 21 +++++++------------
 .../test/Driver/hip-multiarch-libs-linux.hip  | 16 +++++++-------
 2 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
index b147f123a8b4e0..c2ca5de508f067 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -887,14 +887,12 @@ void Linux::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args,
       Args.hasArg(options::OPT_no_hip_rt) || Args.hasArg(options::OPT_r))
     return;
 
-  // On Debian and derivatives, the ROCm runtime ships under
-  // <rocm>/lib/<multiarch-triple>/ (e.g. /usr/lib/x86_64-linux-gnu/) rather
-  // than <rocm>/lib/. Prefer the multiarch subdir when libamdhip64.so lives
-  // there, so the absolute path emitted below, the -L search path, and any
-  // -rpath all resolve to a real file. Non-multiarch layouts (Fedora, Arch,
-  // /opt/rocm, etc.) are unaffected: the probe short-circuits when the
-  // multiarch subdir does not contain libamdhip64.so.
+  // Debian multiarch packages install libamdhip64.so under
+  // <rocm>/lib/<multiarch-triple>/ instead of <rocm>/lib/. Use that directory
+  // when the library exists there, so the library path, -L and -rpath point
+  // to a real file.
   SmallString<0> HIPLibPath;
+  llvm::SmallVector<std::pair<StringRef, StringRef>> Libraries;
   if (ActiveKinds & Action::OFK_HIP) {
     HIPLibPath = RocmInstallation->getLibPath();
     std::string MultiarchTriple =
@@ -903,15 +901,12 @@ void Linux::addOffloadRTLibs(unsigned ActiveKinds, const ArgList &Args,
     llvm::sys::path::append(MultiarchProbe, MultiarchTriple, "libamdhip64.so");
     if (getVFS().exists(MultiarchProbe))
       llvm::sys::path::append(HIPLibPath, MultiarchTriple);
-  }
-
-  llvm::SmallVector<std::pair<StringRef, StringRef>> Libraries;
-  if (ActiveKinds & Action::OFK_HIP)
     Libraries.emplace_back(HIPLibPath, "libamdhip64.so");
-  else if ((ActiveKinds & Action::OFK_SYCL) &&
-           !Args.hasArg(options::OPT_nolibsycl))
+  } else if ((ActiveKinds & Action::OFK_SYCL) &&
+             !Args.hasArg(options::OPT_nolibsycl)) {
     Libraries.emplace_back(SYCLInstallation->getSYCLRTLibPath(),
                            "libLLVMSYCL.so");
+  }
 
   for (auto [Path, Library] : Libraries) {
     if (Args.hasFlag(options::OPT_frtlib_add_rpath,
diff --git a/clang/test/Driver/hip-multiarch-libs-linux.hip b/clang/test/Driver/hip-multiarch-libs-linux.hip
index a404e0fdc7f899..3ad83b2072d96a 100644
--- a/clang/test/Driver/hip-multiarch-libs-linux.hip
+++ b/clang/test/Driver/hip-multiarch-libs-linux.hip
@@ -1,14 +1,7 @@
 // UNSUPPORTED: system-windows
 
-// Test that Debian-style multiarch layouts, in which libamdhip64.so is
-// installed under <rocm>/lib/<multiarch-triple>/ rather than <rocm>/lib/, are
-// detected by the driver and reflected in the linker command line.
-
 // RUN: touch %t.o
 
-// Inputs/rocm-multiarch mimics a Debian multiarch layout:
-//   bin/.hipVersion signals a valid HIP install,
-//   lib/x86_64-linux-gnu/libamdhip64.so is the runtime the driver must find.
 // RUN: %clang -### --hip-link --target=x86_64-linux-gnu \
 // RUN:   --rocm-path=%S/Inputs/rocm-multiarch %t.o 2>&1 \
 // RUN:   | FileCheck -check-prefixes=MULTIARCH %s
@@ -16,8 +9,13 @@
 // MULTIARCH: "{{.*/Inputs/rocm-multiarch/lib/x86_64-linux-gnu/libamdhip64.so}}"
 // MULTIARCH: "-L{{.*/Inputs/rocm-multiarch/lib/x86_64-linux-gnu}}"
 
-// Sanity check: when libamdhip64.so is *not* under lib/<multiarch-triple>/,
-// the driver falls back to <rocm>/lib/ so non-Debian layouts are unchanged.
+// RUN: %clang -### --hip-link --target=x86_64-linux-gnu -frtlib-add-rpath \
+// RUN:   --rocm-path=%S/Inputs/rocm-multiarch %t.o 2>&1 \
+// RUN:   | FileCheck -check-prefixes=MULTIARCH-RPATH %s
+
+// MULTIARCH-RPATH: "-rpath" "{{.*/Inputs/rocm-multiarch/lib/x86_64-linux-gnu}}"
+// MULTIARCH-RPATH-SAME: "{{.*/Inputs/rocm-multiarch/lib/x86_64-linux-gnu/libamdhip64.so}}"
+
 // RUN: %clang -### --hip-link --target=x86_64-linux-gnu \
 // RUN:   --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
 // RUN:   | FileCheck -check-prefixes=FLAT %s



More information about the cfe-commits mailing list