fix(rocm): prefer system LLVM runtime on native Linux (#7448)

* fix(rocm): prefer system LLVM runtime on native Linux

* Fix/adjust the nested LLVM probe for PR #7448: lib64 hosts and non-directories

Two gaps found while simulating the fix against real ROCm layouts.

1. lib64 hosts got no LLVM dir. The candidate was built from the HSA dir, so a
   host with libhsa-runtime64 under lib64 probed <root>/lib64/llvm/lib. ROCm
   installs LLVM under <root>/lib/llvm regardless, so that host kept binding
   system libamd_comgr to the bundle's libLLVM: exactly the bug #7446 reports.
   Probe both spellings, the HSA dir's own first so a genuine lib64 layout still
   wins. When lib_sub is already "lib" the seen set collapses them.

2. os.path.exists accepted a non-directory. The serve-time caller joins these
   straight into LD_LIBRARY_PATH with no is-dir filter, so a file named
   llvm/lib reached the loader. os.path.isdir instead.

Verified on a 27-case matrix built from real directory trees (not mocks), run on
both Windows and Linux against three revisions: main, this PR as-is, and this
commit. Zero regressions and zero reorderings of the pre-existing entries in
every case, and the installer and launcher copies never disagree. The lib64 case
goes [lib64] -> [lib64, lib/llvm/lib]; the file case drops the bogus entry; a
symlinked llvm/lib resolves correctly on Linux.

End-to-end loader check: built real ELF objects mirroring the shipped bundle
(RUNPATH=$ORIGIN, an incomplete libLLVM.so.23.0git next to llama-server, system
comgr from /opt/rocm/lib) and reproduced the reported failure verbatim, then
confirmed the prepend clears it:
  before  undefined symbol: LLVMInitializeSPIRVTarget  ->  after  exit 0

Test helper now patches os.path.isdir alongside os.path.exists, else every fake
host reports its nested llvm dir as missing. New cases: lib64 finding llvm under
lib, lib64 preferring its own when both exist, and a real-filesystem check that a
non-directory is not prepended. Removing the lib fallback from one copy reddens
three tests including the two-copy parity guard.

tests/studio/install: 1361 passed on Linux, 4 pre-existing environmental
failures unchanged (3 managed-node-runtime under root, 1 the real /opt/rocm case
already covered by #7397). 30/30 on the helper suite on Windows and Linux.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Daniel Han <danielhanchen@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Willow Lopez 2026-07-28 21:19:29 +08:00 committed by GitHub
commit 77971d0deb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 96 additions and 1 deletions

View file

@ -309,6 +309,15 @@ def _native_linux_system_rocm_lib_dirs(binary_dir: str = "") -> "list[str]":
os.path.join(d, "libhsa-runtime64.so.1")
):
out.append(d)
# ROCm keeps LLVM's versioned runtime under <root>/lib/llvm, so a
# lib64 host still finds it under lib. Probe both and keep them
# ahead of the bundle, else system libamd_comgr binds to the
# bundle's incompatible libLLVM.so.*.
for _sub in (lib_sub, "lib"):
llvm_lib = os.path.join(base, _sub, "llvm", "lib")
if llvm_lib not in seen and os.path.isdir(llvm_lib):
seen.add(llvm_lib)
out.append(llvm_lib)
return out

View file

@ -4807,6 +4807,15 @@ def _native_linux_system_rocm_lib_dirs(binary_dir: str = "") -> list[str]:
os.path.join(d, "libhsa-runtime64.so.1")
):
out.append(d)
# ROCm keeps LLVM's versioned runtime under <root>/lib/llvm, so a
# lib64 host still finds it under lib. Probe both and keep them
# ahead of the bundle, else system libamd_comgr binds to the
# bundle's incompatible libLLVM.so.*.
for _sub in (lib_sub, "lib"):
llvm_lib = os.path.join(base, _sub, "llvm", "lib")
if llvm_lib not in seen and os.path.isdir(llvm_lib):
seen.add(llvm_lib)
out.append(llvm_lib)
return out