Repository navigation
feat: build and run mlxcel natively on Windows with ROCm - #2603
Conversation
The rocm feature was Linux-only. ROCm 10 on Windows is an MSVC-ABI stack, so the backend can build with the same toolchain as MLX and the Rust binary once the build script, the overlay CMake and a few backend sources learn the Windows layout. - Build script: the feature gate accepts Linux or Windows x86_64 MSVC. New build_support/windows_rocm.rs resolves the SDK from ROCM_PATH or HIP_PATH, checks hipcc.exe and lib/amdhip64.lib, detects gfx targets with amdgpu-arch (bin/ or lib/llvm/bin/), and reuses windows_cuda::cmake_path. build_mlx selects Ninja and passes forward-slash paths; link_rocm links the import libraries and copies libhipblaslt.dll.a to OUT_DIR/hipblaslt.lib. Tests run in mlxcel-mlx-pin. - build_mlx adds /EHsc for every MSVC build, since cmake-rs replaces CMake's MSVC defaults (C4530). This also affects the Windows CUDA build. - ROCm overlay: a WIN32 branch in the backend CMake, MSVC portability fixes, an LLP64-safe hiprtc size_t, and a host-shadow memory model for device memory the CPU cannot map on Windows. raw_ptr now marks the shadow dirty, which also fixes CPU writes never reaching VRAM on the discrete-GPU path. - binary_two.hip is dropped: it duplicated DivMod::eval_gpu, which ld resolved to binary.hip and lib.exe reported as LNK4006. - Docs: Windows with AMD ROCm in installation.md and the Windows build variables in environment-variables.md. Verified on a Radeon 8060S (gfx1151) with ROCm 10.1.0: both binaries build and generate is correct on Qwen3-0.6B-4bit and Qwen3-1.7B-bf16. Linux is covered by the CI ROCm job; the Windows CUDA build is unverified (no NVIDIA host). Refs #2601
With device memory not host-visible on Windows, every buffer read on the CPU gets a pinned host shadow, and raw_ptr now marks it dirty. Three allocator paths did not account for that. A scalar-pool slot released by the buffer cache kept its shadow, and SmallSizePool::malloc reset the pointer when handing the slot out again, leaking one pinned allocation per recycled scalar. A cache hit handed the next owner the previous owner's dirty shadow, so the next gpu_ptr spent a full-buffer copy writing stale bytes to the device before the new owner's kernel overwrote them. free_async freed the shadow but left host_dirty set, so a cache hit that retags a buffer would skip the device-to-host copy into a fresh shadow. free_rocm_buffer now releases a pool slot's shadow, free_async clears host_dirty with the shadow, and a cache hit in malloc_async clears host_dirty. On Linux none of these paths has a shadow with host_dirty set except the discrete path this PR already changes, so Linux behavior is otherwise unchanged. LOCAL_FIXES entry 55 records it. Verified with cargo clippy -p mlxcel-core --release --features rocm (the overlay allocator recompiled cleanly) and rocm_overlay.py drift and records. Refs #2601
Security and performance reviewNo CRITICAL or HIGH findings, so nothing was changed on the branch. Linux paths: Remaining items (not fixed, for follow-up):
Checked and fine: build-time tools run by absolute path, the Verification: |
rocm_home() in the overlay's jit_module.cpp treated an empty variable as set, because getenv returns "" rather than null. That produced the hiprtc include flag -I/include, which on Windows is the root of the current drive, where any local user can plant hip/hip_runtime.h for the slicing JIT to include. Empty values are now skipped in favor of the next candidate, matching windows_rocm::resolve_rocm_root in the build script. The LOCAL_FIXES.md entry 54 records it, and the environment variable reference notes that an empty value counts as unset. Validation: cargo clippy -p mlxcel-core --release --features rocm -- -D warnings passes; rocm_overlay.py drift and records pass; cargo fmt --all --check and cargo test -p mlxcel-mlx-pin --lib pass. Refs #2601
The Windows ROCm prerequisites listed amdgpu-arch.exe as living in bin or lib\llvm\bin without saying which SDK uses which. The ROCm 10.1 TheRock tarball keeps it in lib\llvm\bin, other SDKs in bin; the build script already falls back between the two. No Korean counterpart exists for docs/installation.md or docs/environment-variables.md (docs/ko and docs/*.ko.md are absent), so nothing to mirror. Refs #2601
Summary
--features rocmnow builds and runs natively on Windows x86_64 MSVC with a ROCm 10 SDK (validated on gfx1151 with ROCm 10.1.0). Linux code paths are unchanged apart from the feature-gate panic text and three fixes that apply on all platforms: the host-shadow dirty flag, the shadow lifetime on free and reuse, and the DivMod duplicate.build_support/windows_rocm.rs(RocmLayout,resolve_rocm_root,parse_amdgpu_arch, reusingwindows_cuda::cmake_path) is wired intobuild_mlx(Ninja, forward-slashROCM_PATH/CMAKE_PREFIX_PATH/CMAKE_HIP_COMPILER),detect_rocm_arch(amdgpu-arch) andlink_rocm(import libs,libhipblaslt.dll.acopied toOUT_DIR/hipblaslt.libso the link reaches the final binaries). ROCm 10.1's tarball keepsamdgpu-arch.exeinlib\llvm\bin\, notbin\; the layout prefersbin\and falls back. Tests live inmlxcel-mlx-pin./EHscfor MLX's own C++ on every MSVC build. cmake-rs replaced CMake's MSVC defaults, so MLX compiled without unwind semantics (C4530). This also applies to the Windows CUDA build; it is flag-only and unverified here (no NVIDIA GPU on this host).LOCAL_FIXES.mdentries 53-56): a WIN32 CMake branch (no libstdc++ probing, no-fPIC,--rocm-path/--rocm-device-lib-path,/MDruntime,.obj,lib.exearchivemlx_rocm_kernels.lib), MSVC portability fixes (sysconf, weak symbol,dup2//dev/null,<iterator>,__builtin_clzshim), and an LLP64-safe hiprtcsize_t.rocm_home()now also triesHIP_PATHon Windows and treats an emptyROCM_HOME/ROCM_PATH/HIP_PATHas unset; before, an empty value produced-I/include, the root of the current drive on Windows, where any local user could plant a header the slicing JIT includes.hipMallocand fine-grained memory fault; managed and pinned memory are capped near the OS-visible RAM). Windows therefore uses plainhipMallocand routes every CPU access through the host shadow (device_memory_host_visible()inallocator.h, constexpr true on Linux).raw_ptr()marks the shadow dirty sogpu_ptrflushes CPU writes back before the next kernel; nothing set that flag before, so CPU writes on the existing discrete-GPU shadow path were never flushed. The shadow is freed with its buffer and its dirty flag cleared on free and on buffer-cache reuse, which closes a pinned-memory leak in the scalar pool and a stale shadow handed to a buffer's next owner.binary_two.hipis dropped andbinary.hipkeeps the only definition ofDivMod::eval_gpu, which was the only non-template definition inbinary_two.hip; nothing else references its kernels or templates. Linux resolvedbinary.ofirst (archive order followsHIP_SOURCES, and pulling both would have been a multiple-definition error), so Linux behavior is unchanged; this was reasoned, not traced with a Linux link.Test plan
Windows 11, Radeon 8060S (gfx1151), Adrenalin 32.0.31041.1004, ROCm 10.1.0, VS 18 x64 developer environment,
ROCM_PATHset:cargo build --release --features rocmbuildsmlxcel.exeandmlxcel-server.exe; the-vvlog has 0 C4530 and 0 LNK4006, with all HIP objects andmlx_rocm_kernels.librebuilt in that run.amdgpu-archdetection givesgfx1151.mlxcel generate -m mlx-community/Qwen3-0.6B-4bit -p "What is the capital of France?" -n 60 --temp 0 --show-reasoningon the final head: correct ("France's capital is Paris"), 113 tok/s; the first token runs a hiprtc fused-kernel JIT.mlx-community/Qwen3-1.7B-bf16 -n 80(hipBLASLt bf16): correct, 28.7 tok/s.ROCM_HOMEandROCM_PATHempty andHIP_PATHset: generation succeeds through theHIP_PATHfallback.cargo test -p mlxcel-mlx-pin --lib(44 pass, 11 new),cargo fmt --all --check,cargo clippy -p mlxcel-core --release --features rocm -- -D warnings,rocm_overlay.py driftandrecords,check_env_var_docs.py.rocm-gfx1151runner, ci(rocm): self-hosted gfx1151 runner that builds, links and smoke-tests ROCm changes #1811), so nothing in CI builds--features rocmon Linux. The implementation and security reviews checked that every Linux-reachable change is either identical or one of the three intended fixes above./EHsc: needs a CUDA host.Running still needs the ROCm
bindirectory on PATH andROCM_PATH(orHIP_PATH) set; #2602 removes both requirements.Closes #2601