Skip to content

feat: run Windows ROCm builds without the SDK on PATH or ROCM_PATH - #2604

Merged
inureyes merged 4 commits into
mainfrom
feature/issue-2602-rocm-windows-runtime
Oct 25, 2026
Merged

inureyes merged 4 commits into
mainfrom
feature/issue-2602-rocm-windows-runtime

Conversation

@inureyes

@inureyes inureyes commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Summary

A Windows --features rocm binary now starts and JIT-compiles from a plain cmd.exe with neither the ROCm bin on PATH nor ROCM_PATH/HIP_PATH/ROCM_HOME set, loads every ROCm DLL from the SDK it was built against, and ignores same-named DLLs in the working directory. mlxcel-server is validated on this configuration. Linux paths are unchanged; there is no Linux ROCm machine or CI job (#1811) to re-check them, so the Linux side was kept byte-identical in behavior by construction (every change is under _WIN32, if(WIN32) or a Windows-only layout_from_env()).

What changed

  • Delay-load (item 1). windows_rocm::select_delayload_dlls / emit_delayload_link_args mirror the CUDA helpers; mlxcel-core/build.rs (link_rocm, which also links delayimp) and the root build.rs both call it. Excluded from /DELAYLOAD: hiprtc-builtins* and hipfftw (no import library names them, LNK4199 otherwise; the build log has 0 LNK4199). New overlay delayload.cpp loads each DLL by full path from MLXCEL_ROCM_BIN_DIR with LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS.
  • Run-time LoadLibrary of amd_comgr.dll / hiprtc-builtins0716.dll. Mechanism: before the first ROCm DLL loads, the hook calls AddDllDirectory(<bin>) and preloads amd_comgr*.dll and hiprtc-builtins*.dll from <bin> by full path. A later plain-name LoadLibrary from amdhip64 or hiprtc then resolves from the loaded-module list, which Windows checks before the application directory, System32, the current directory and PATH. SetDefaultDllDirectories was rejected because it is process-wide and changes the display driver's own loads too. This is also a correctness fix: the Adrenalin driver installs older amdhip64_7.dll and amd_comgr.dll in System32, and the SDK's amdhip64_7.dll next to the driver's amd_comgr.dll reports zero devices (probe). Before this PR the binaries ran the driver's System32 amdhip64_7.dll. A DLL missing from <bin> prints the expected path and raises the delay-load helper's own exception; returning null (as the issue suggested) would make the helper retry with a plain LoadLibrary that searches CWD and PATH.
  • hiprtc include fallback (item 2). jit_module.cpp resolves through a pure choose_rocm_include_dir(): ROCM_HOME, ROCM_PATH, HIP_PATH (empty = unset), then MLXCEL_ROCM_INCLUDE_DIR if it exists (defined in mlxcelverse/CMakeLists.txt on Windows), then /opt/rocm (Linux).
  • Memory (item 3). Probe result: 1 GiB hipMalloc+hipMemset chunks stopped at 105 GiB with 107.87 GiB reported (31.65 GiB OS RAM), so hipMalloc does not exceed hipMemGetInfo total. Under _WIN32 the integrated limit is total * 0.8. Rust adds a GlobalMemoryStatusEx step-4 probe with tests.
  • Docs (item 5). installation.md and environment-variables.md; LOCAL_FIXES.md item 57.

Changes during review

  • Admin-only SDK directory (security review, HIGH, docs). The recorded SDK directory is now the only source of ROCm DLLs and hiprtc headers, and a directory created under the drive root (such as C:\rocm\10.1.0) inherits write access for every authenticated user. installation.md requires an admin-only directory and gives a tested icacls command; it and environment-variables.md also state that run-time ROCM_HOME/ROCM_PATH/HIP_PATH still take precedence over the baked include dir (as on Linux and CUDA) and need the same protection, and name the optional plain-name DLLs (hsa-amd-aqlprofile64, hsa-ext-finalize64, dtif64a) the hook cannot cover without a process-wide search change.
  • Fail-closed build guards. A relative ROCM_PATH/HIP_PATH and an SDK bin without amdhip64_*.dll now panic in the build script instead of baking a relative directory or silently emitting no /DELAYLOAD. Three new tests.

Test plan

Host: Radeon 8060S gfx1151, ROCm 10.1.0 at C:\rocm\10.1.0, VS 18.

  • cargo test -p mlxcel-mlx-pin --lib: 49 pass (5 new).
  • cargo test --release --features rocm --lib execution::memory_estimate: 64 pass (3 new, one of them a live GlobalMemoryStatusEx call under cfg(windows)).
  • cargo fmt --all --check, cargo clippy -p mlxcel-core --release --features rocm -- -D warnings, rocm_overlay.py records and drift (110 backend files, 57 entries), check_env_var_docs.py.
  • cargo build --release --features rocm (both binaries). llvm-readobj --coff-imports: amdhip64_7.dll, rocblas.dll, hiprtc0716.dll, libhipblaslt.dll, hipfft.dll are all DelayImport in mlxcel.exe and mlxcel-server.exe; only libopenblas.dll and combase.dll remain eager among the non-system imports.
  • Clean environment (Git Bash env -u ROCM_PATH -u HIP_PATH -u ROCM_HOME PATH=/usr/bin:/c/Windows/System32:/c/Windows, and a cmd.exe batch with PATH=C:\Windows\System32;C:\Windows): generate with a fresh MLX_HSACO_CACHE_DIR runs on the GPU, including the JIT compile that the old binary failed with "ROCM_HOME or ROCM_PATH is not set". Qwen3-0.6B-4bit 121 tok/s, Qwen3-1.7B-bf16 28.6 tok/s, correct text ("France's capital is Paris").
  • Decoy check: working directory holding amdhip64_7.dll, amd_comgr.dll, hiprtc0716.dll, hiprtc-builtins0716.dll and rocblas.dll (each a renamed System32\version.dll). New binary: generate succeeds (117 tok/s). Old binary from feat: build and run mlxcel natively on Windows with ROCm (gfx1151) #2601 with the same rocblas.dll decoy and the SDK on PATH: exit 127 at start.
  • mlxcel-server from that cmd.exe batch in the decoy directory. Loaded modules: every ROCm DLL from C:\rocm\10.1.0\bin (amdhip64_7, amd_comgr, hiprtc-builtins0716, hiprtc0716, rocblas, libhipblaslt, rocm_kpack, origami), none from the decoy directory or System32. Greedy /no_think prompt "What is the capital of France?":
    • /v1/chat/completions: "The capital of France is **Paris**.", finish_reason: stop, 13 tokens.
    • /v1/chat/completions with stream: true: deltas reassemble to the same text, then [DONE].
    • /v1/messages: "The capital of France is **Paris**.", stop_reason: end_turn.
  • generate --estimate-memory (Qwen3-1.7B-bf16, clean env): Available 86.29 GiB (= 0.8 x 107.87), FITS.

Not verified: the missing-DLL error path (needs the SDK moved away), the Windows CUDA build (no NVIDIA GPU; its build-script call is unchanged), and Linux ROCm.

Closes #2602

After #2601 a Windows `--features rocm` binary only started with the SDK `bin` on PATH and only JIT-compiled with ROCM_PATH set. It also ran the display driver's System32 amdhip64_7.dll rather than the SDK's, and a same-named DLL in the working directory won over PATH.

- Both build scripts emit /DELAYLOAD for the five imported ROCm DLLs (selected by `windows_rocm::select_delayload_dlls`) and /INCLUDE the notify hook. The new overlay `delayload.cpp` loads each DLL by full path from the build-time SDK `bin` with LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | DEFAULT_DIRS, and first preloads amd_comgr*.dll and hiprtc-builtins*.dll from there, because amdhip64 and hiprtc load them by plain name. Without the preload the SDK amdhip64 picked up the driver's older System32 amd_comgr.dll and found no device. A missing DLL prints the expected path and raises the delay-load exception instead of falling back to the default search.
- hiprtc include resolution falls back to the build-time `<ROCM_PATH>/include` after ROCM_HOME, ROCM_PATH and HIP_PATH, through a pure chooser.
- On Windows the integrated-GPU allocator limit is 80% of the reported device memory: a chunked hipMalloc probe stopped at 105 of 107.87 GiB.
- The memory estimate gains a GlobalMemoryStatusEx probe for Windows.

Linux paths are unchanged. Verified on gfx1151 with ROCm 10.1.0 from a plain cmd.exe with decoy DLLs in the working directory: generate (cold JIT), mlxcel-server chat completions (streaming and not) and messages.

Closes #2602
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:windows Windows (native) specific labels Oct 25, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Windows ROCm binaries run from a plain cmd.exe without the SDK on PATH or ROCM_PATH, load every ROCm DLL from the build-time SDK bin (never CWD or PATH), JIT through a baked include fallback, use a carve-out-only allocator limit on Windows APUs, and serve correctly from mlxcel-server.

Findings Addressed

No CRITICAL, HIGH or MEDIUM findings, so no fixes were committed.

Remaining Items

  • allocator.cpp:16-17 still includes <windows.h> under _WIN32, which nothing uses since the GlobalMemoryStatusEx read was removed (LOW), left because it needs a C++ rebuild for no behavior change.
  • delayload.cpp:59-60 says CMake passes MLXCEL_ROCM_BIN_DIR as UTF-8, but mlx is not compiled with /utf-8, so a non-ASCII SDK path is encoded in the execution charset and the CP_UTF8 conversion is unverified for such paths (LOW). ASCII paths are unaffected.
  • delayload.cpp:120-122 calls path::string(), which can throw inside the hook for a file name not representable in the ANSI code page (LOW, same as the CUDA hook; the SDK bin names are ASCII).
  • A failed preload of amd_comgr*.dll is silent, so a broken SDK falls back to the driver's System32 comgr and reports no device instead of naming the missing file (LOW).
  • The hook comment says amdhip64 loads hiprtc-builtins*.dll; neither amdhip64_7.dll nor hiprtc0716.dll holds that name as a literal, so which module loads it is unconfirmed (LOW, the preload is harmless either way).

Review notes

  • Returning null from dliNotePreLoadLibrary makes __delayLoadHelper2 call LoadLibraryExA(szDll, NULL, 0), the standard search (application dir, System32, CWD, PATH). Raising VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND) with the helper's own pdli is exactly what the helper does after that fallback fails, so the deviation from the issue is correct and keeps any __except filter working.
  • The one-time AddDllDirectory and preloads sit in function-local statics, which MSVC initializes thread-safely; other notify phases and non-ROCm names return null.
  • llvm-readobj --coff-imports on the current mlxcel.exe: the five ROCm DLLs are DelayImport, matching select_delayload_dlls over the 10.1.0 bin (exclusions hiprtc-builtins*, hipfftw are the only other prefix matches). Both build scripts call the same function.
  • Extra decoy run: amdhip64_7.dll references hsa-amd-aqlprofile64.dll, hsa-ext-finalize64.dll and dtif64a.dll, which are in neither the SDK bin nor System32. With renamed version.dll copies of those three plus amd_comgr_3.dll and atidxx64.dll in the CWD, a clean-env cold-JIT generate (400 tokens, 109 tok/s) loaded no decoy; every ROCm module came from C:\rocm\10.1.0\bin.
  • Linux paths: every change is under _WIN32, if(WIN32) or a Windows-only layout_from_env(); the Linux -I string and allocator formula are unchanged. The Windows CUDA block in the root build.rs is untouched.

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass (cargo test -p mlxcel-mlx-pin --lib 46 pass, cargo fmt --all --check, cargo clippy -p mlxcel-core --release --features rocm -- -D warnings)

The Windows ROCm binaries now load their DLLs and hiprtc headers only from the SDK directory recorded at build time, so that directory is the trust root. The documented example, C:\rocm, inherits Modify access for Authenticated Users from the drive root (checked with icacls on the validation host), so any local account could replace amdhip64_7.dll there and run code in every mlxcel process. The installation guide now says the directory must be admin-only and gives an icacls command (tested on a scratch tree) that removes the inherited access, notes that run-time ROCM_HOME/ROCM_PATH/HIP_PATH take precedence for headers and need the same protection, and records the three optional DLLs the HIP runtime names but that no SDK or driver directory ships.

Refs #2602
@inureyes

Copy link
Copy Markdown
Member Author

Security and Performance Review

Findings addressed

  • HIGH (docs): the SDK directory recorded at build time is now the only DLL and hiprtc header source, but the documented location (C:/rocm/10.1.0, created under the drive root) inherits Modify for Authenticated Users. icacls on this host confirms it for bin, so any local account can replace amdhip64_7.dll and run code in every mlxcel process. Not a regression (the same directory was on PATH before), but it defeats the hijack protection on multi-user machines. Fixed in 05a3e61: docs/installation.md requires an admin-only SDK directory and gives an icacls /inheritance:r command (tested on a scratch tree: children lose the inherited ACE, the owner can no longer write), notes that run-time ROCM_HOME/ROCM_PATH/HIP_PATH win over the baked include dir and need the same protection, and names the optional DLLs below. docs/environment-variables.md gets the same trust note. Docs only, no rebuild needed.

Remaining items (LOW, not fixed)

  • amdhip64_7.dll holds the names hsa-amd-aqlprofile64.dll, hsa-ext-finalize64.dll and dtif64a.dll, which neither the SDK nor System32 provide. Decoys in CWD are not loaded during generate, but a profiling or other optional path that opens them by plain name gets the standard search (CWD, PATH). Fixing that needs SetDefaultDllDirectories, which the PR rejected for good reason. Now documented.
  • windows_rocm::layout_from_env accepts a relative ROCM_PATH, and both CMake defines (MLXCEL_ROCM_BIN_DIR, MLXCEL_ROCM_INCLUDE_DIR) copy it verbatim, so a relative path would be resolved against the run-time CWD. In practice the build fails first, because the root and mlxcel-core build scripts run from different directories. A standalone CMake build of the overlay with no ROCM_PATH would bake /bin, the root of the current drive, which any user can create. Suggested follow-up: panic on a non-absolute root in layout_from_env, and FATAL_ERROR unless IS_ABSOLUTE "${ROCM_PATH}" in the overlay's if(WIN32) block.
  • emit_delayload_link_args fails open: if the bin listing is unreadable or matches nothing, the build emits no /DELAYLOAD and the binaries import the ROCm DLLs eagerly by name, silently bringing back the CWD-before-PATH order. Today this can't trigger because layout_from_env has already checked the SDK, but a panic when no amdhip64_*.dll is selected would make it fail closed. Same shape as the CUDA helper.
  • Pre-existing, outside this PR: the hsaco JIT cache defaults to temp_directory_path()/mlx/<version>/hsaco, which is the shared /tmp on Linux, so another local user could create it first and plant code objects. On Windows %TEMP% is per user.

Checked, no finding

  • GlobalMemoryStatusEx FFI: the layout matches MEMORYSTATUSEX (2 x u32 + 7 x u64, 64 bytes), dwLength is set, BOOL is i32, extern "system", and the SAFETY comment is accurate.
  • Allocator: on Windows an integrated device gets total * 0.8 (86 GiB of 107.87 GiB), below the 105 GiB hipMalloc reached, so it does not over-commit the carve-out. The OS-RAM term is gone, so a large OS RAM no longer inflates the limit. Host-shadow pinned memory is still not counted against it (pre-existing, item 55).
  • Delay-load hook: full-path LoadLibraryExW with DLL_LOAD_DIR | DEFAULT_DIRS, comgr and builtins preloaded so plain-name loads hit the loaded-module list, and failure raises ERROR_MOD_NOT_FOUND with no retry. Embedded strings in hiprtc, rocBLAS, hipBLASLt, hipFFT, rocFFT and rocm_kpack name no other run-time DLL.
  • Linux: every change is behind _WIN32, if(WIN32) or the Windows-only layout_from_env. The -I string and the allocator formula are unchanged, and the include dir is resolved once in a static, so there is no performance change.

Verification

cargo test -p mlxcel-mlx-pin --lib (46 pass), cargo fmt --all --check, cargo clippy -p mlxcel-core --release --features rocm -- -D warnings, overlay drift and records OK.

…ime DLL

A relative ROCM_PATH or HIP_PATH made CMake record a relative bin or include directory that resolves against the working directory at run time. The Windows ROCm build now panics when the resolved root is not absolute, naming the value. An empty value still counts as unset.

emit_delayload_link_args now panics, naming the bin directory, when no amdhip64_*.dll is found there. Before, it emitted no /DELAYLOAD for the runtime and the binary imported it eagerly by name, searched from the current directory first. The CUDA helper is unchanged.

Both decisions live in pure functions (require_absolute_root, require_delayload_dlls) with unit tests in windows_rocm_tests.rs. docs/installation.md states both requirements.

Validation:
- cargo test -p mlxcel-mlx-pin --lib: 49 passed
- cargo fmt --all --check and cargo clippy -p mlxcel-core --release --features rocm -- -D warnings: clean

Refs #2602
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 25, 2026
@inureyes
inureyes merged commit 01047d7 into main Oct 25, 2026
29 checks passed
@inureyes
inureyes deleted the feature/issue-2602-rocm-windows-runtime branch October 25, 2026 20:49
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:windows Windows (native) specific priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: self-contained Windows ROCm runtime and server validation

1 participant