https://github.com/talhaHavadar created 
https://github.com/llvm/llvm-project/pull/207624

## Summary

On Debian and derivatives (Ubuntu, Mint, Pop!_OS), the ROCm runtime is
installed under `/usr/lib/<multiarch-triple>/` rather than `/usr/lib/`.
`RocmInstallationDetector::detectHIPRuntime()` currently sets
`LibPath = <install>/lib` unconditionally, so
`Linux::addOffloadRTLibs()` emits an absolute
`<lib>/libamdhip64.so` path that does not exist and the link fails.

This patch probes `<LibPath>/<triple>/libamdhip64.so` after computing
`LibPath` and extends `LibPath` with the triple subdirectory when the
probe succeeds. The change is guarded by `FS.exists()`, so
non-multiarch layouts (Fedora, Arch, `/opt/rocm`) fall through
unchanged.

## Reproduction (before this PR)

On Ubuntu 26.10 with `libamdhip64-dev` installed
(`libamdhip64.so` shipped at `/usr/lib/x86_64-linux-gnu/`):

```sh
cat > /tmp/hello.hip <<'END'
#include <hip/hip_runtime.h>
int main() { return 0; }
END
clang++ -x hip --offload-arch=gfx900 /tmp/hello.hip -lamdhip64 -o /tmp/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
```

With this patch, `<lib>/x86_64-linux-gnu/libamdhip64.so` is picked up
and the link succeeds.

## Impact

Fixes every HIP link on Debian, Ubuntu, and derivatives (Mint,
Pop!_OS, etc.) using the system `libamdhip64-dev`. Non-multiarch
layouts (Fedora, Arch, `/opt/rocm`) are unaffected: the new codepath
is only entered when `<lib>/<triple>/libamdhip64.so` actually exists.

All three consumers of `getLibPath()` are corrected:

- `Linux::addOffloadRTLibs` — the absolute path resolves to a real
  file.
- The `-L` search path added for HIP points at the correct directory.
- `-rpath` (opt-in via `-frtlib-add-rpath`) bakes the correct path.

`MSVC.cpp` is unaffected — the Windows layout does not use multiarch.

## Test plan

`clang/test/Driver/hip-multiarch-libs-linux.hip` covers both:

- A fake ROCm root with `lib/x86_64-linux-gnu/libamdhip64.so` present:
  the driver emits the multiarch path and a `-L` that points at the
  multiarch dir.
- The existing `Inputs/rocm` tree (no multiarch subdir): the driver
  falls back to `<rocm>/lib/libamdhip64.so`.

## Notes

Reported downstream at
https://github.com/ROCm/llvm-project/issues/3169 — @lamb-j asked me
to send the fix here for review.

>From a875147c4020099978946d66f75b15edc4eeb832 Mon Sep 17 00:00:00 2001
From: Talha Can Havadar <[email protected]>
Date: Mon, 6 Jul 2026 01:43:15 +0200
Subject: [PATCH] [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

After computing LibPath, probe <LibPath>/<triple>/libamdhip64.so and
extend LibPath with the triple subdirectory when the probe succeeds.
The change is guarded by FS.exists() so non-multiarch layouts
(Fedora, Arch, /opt/rocm) fall through unchanged.

This fixes all three consumers of getLibPath():

  * Linux::addOffloadRTLibs — the absolute path resolves to a real
    file.
  * The `-L` search path added for HIP now points at the correct
    directory.
  * `-rpath` (opt-in via -frtlib-add-rpath) bakes the correct path.

MSVC.cpp is unaffected (the Windows layout does not use multiarch).

Reported at https://github.com/ROCm/llvm-project/issues/3169.
---
 clang/lib/Driver/ToolChains/AMDGPU.cpp        | 10 ++++++
 .../test/Driver/hip-multiarch-libs-linux.hip  | 33 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 clang/test/Driver/hip-multiarch-libs-linux.hip

diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp 
b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 032c108d3d10d..9fcb309a087ea 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -457,6 +457,16 @@ void RocmInstallationDetector::detectHIPRuntime() {
     llvm::sys::path::append(IncludePath, "include");
     LibPath = InstallPath;
     llvm::sys::path::append(LibPath, "lib");
+    // Debian multiarch: prefer <lib>/<triple> when libamdhip64.so lives
+    // there, so both the absolute path emitted by Linux::addOffloadRTLibs
+    // and the -L flag resolve to a real file. Guarded by FS.exists() so
+    // non-multiarch layouts (Fedora, Arch, /opt/rocm, etc.) fall through
+    // unchanged.
+    SmallString<0> MultiArchProbe = LibPath;
+    llvm::sys::path::append(MultiArchProbe, D.getTargetTriple(),
+                            "libamdhip64.so");
+    if (FS.exists(MultiArchProbe))
+      llvm::sys::path::append(LibPath, D.getTargetTriple());
     SharePath = InstallPath;
     llvm::sys::path::append(SharePath, "share");
 
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 0000000000000..d835bb7f70e38
--- /dev/null
+++ b/clang/test/Driver/hip-multiarch-libs-linux.hip
@@ -0,0 +1,33 @@
+// UNSUPPORTED: system-windows
+
+// Test that Debian-style multiarch layouts, in which libamdhip64.so is
+// installed under <rocm>/lib/<triple>/ rather than <rocm>/lib/, are
+// detected by the driver and reflected in the linker command line.
+
+// RUN: touch %t.o
+
+// Build a fake ROCm tree that mimics a Debian multiarch layout:
+// <rocm>/bin/.hipVersion signals a valid HIP install,
+// <rocm>/lib/x86_64-linux-gnu/libamdhip64.so is the runtime the driver
+// must find.
+// RUN: rm -rf %t.rocm.multiarch
+// RUN: mkdir -p %t.rocm.multiarch/bin
+// RUN: mkdir -p %t.rocm.multiarch/lib/x86_64-linux-gnu
+// RUN: cp %S/Inputs/rocm/bin/.hipVersion %t.rocm.multiarch/bin/.hipVersion
+// RUN: touch %t.rocm.multiarch/lib/x86_64-linux-gnu/libamdhip64.so
+
+// RUN: %clang -### --hip-link --target=x86_64-linux-gnu \
+// RUN:   --rocm-path=%t.rocm.multiarch %t.o 2>&1 \
+// RUN:   | FileCheck -check-prefixes=MULTIARCH %s
+
+// MULTIARCH: "{{.*/lib/x86_64-linux-gnu/libamdhip64.so}}"
+// MULTIARCH: "-L{{.*/lib/x86_64-linux-gnu}}"
+
+// Sanity check: when libamdhip64.so is *not* under lib/<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}}"

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

Reply via email to