From eecb32abf6a024716301f3d8153e2f8a6bfd1ce4 Mon Sep 17 00:00:00 2001 From: isshin Date: Thu, 1 Oct 2026 20:19:37 +0200 Subject: [PATCH] HIP on Windows: the build with ROCm's clang as the host compiler (#247, #325) CMake refuses to mix cl.exe with Clang for HIP, so on Windows ROCm's clang++ compiles the host code too (targeting the MSVC ABI, with MSVC's headers and linker from vcvars). The CUDA->HIP shim is force-included with /FI when the host front end is MSVC-style (cl / clang-cl) and with -include otherwise, its path in forward slashes; CMAKE_HIP_ARCHITECTURES may also be space-separated. On Windows the shim keeps 's min/max macros out (NOMINMAX, WIN32_LEAN_AND_MEAN). Taken from jagsan-cyber's #247 as ported by dvasdekis in #325 (2373c75); the arch lists and the arch check stay 0.1.32's. Built on this PC with ROCm 10.2.0a20260930 (TheRock wheels, the version #325 ran on an RX 9070 XT) for gfx1100;gfx1101;gfx1102;gfx1200;gfx1201;gfx1030: strata.exe and all 253 targets with the tests. CUDA builds do not read these files. Co-authored-by: Dimitri Vasdekis --- cmake/hip_backend.cmake | 24 +++++++++++++++++++----- include/strata/hip_compat/cuda_runtime.h | 9 +++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmake/hip_backend.cmake b/cmake/hip_backend.cmake index 558bb07c..255ee381 100644 --- a/cmake/hip_backend.cmake +++ b/cmake/hip_backend.cmake @@ -12,8 +12,14 @@ endif() set(_strata_hip_validated gfx1100 gfx1201) set(_strata_hip_community gfx1101 gfx1200) set(_strata_hip_unvalidated gfx1102 gfx1030) +# CMake hands HIP a ';' list, but a -DCMAKE_HIP_ARCHITECTURES typed by hand (or ROCm's own Windows tooling) may use +# spaces, which foreach(IN LISTS) would otherwise treat as one element. +string(REPLACE " " ";" _strata_hip_norm "${CMAKE_HIP_ARCHITECTURES}") set(STRATA_HIP_ARCH_LIST "") -foreach(_arch IN LISTS CMAKE_HIP_ARCHITECTURES) +foreach(_arch IN LISTS _strata_hip_norm) + if(_arch STREQUAL "") + continue() + endif() string(REGEX REPLACE ":.*$" "" _base "${_arch}") # gfx1100:xnack- -> gfx1100 if(_base IN_LIST _strata_hip_validated) elseif(_base IN_LIST _strata_hip_community) @@ -62,11 +68,19 @@ target_include_directories(strata_hip_runtime BEFORE INTERFACE "${STRATA_HIP_COMPAT_INCLUDE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include") target_compile_definitions(strata_hip_runtime INTERFACE STRATA_USE_HIP=1 "STRATA_HIP_ARCHS=\"${STRATA_HIP_ARCHS}\"") target_link_libraries(strata_hip_runtime INTERFACE hip::host) -foreach(_language IN ITEMS CXX HIP) +# The shim renames the CUDA runtime to HIP, force-included into every host and device source. On Windows the host +# compiler is ROCm's clang++ too (tools/hip/build_windows.bat: CMake refuses to mix cl.exe with Clang HIP), which takes +# -include like it does on Linux; an MSVC-style front end (cl / clang-cl) takes /FI instead. Forward slashes, so /FI +# does not read the path's backslashes as escapes. +file(TO_CMAKE_PATH "${STRATA_HIP_COMPAT_INCLUDE_DIR}/cuda_runtime.h" _strata_hip_force) +if(MSVC) + target_compile_options(strata_hip_runtime INTERFACE "$<$:/FI${_strata_hip_force}>") +else() target_compile_options(strata_hip_runtime INTERFACE - "$<$:-include>" - "$<$:${STRATA_HIP_COMPAT_INCLUDE_DIR}/cuda_runtime.h>") -endforeach() + "$<$:-include>" "$<$:${_strata_hip_force}>") +endif() +target_compile_options(strata_hip_runtime INTERFACE + "$<$:-include>" "$<$:${_strata_hip_force}>") # CMake does not infer HIP from Strata's existing CUDA-shaped .cu suffixes. file(GLOB_RECURSE _strata_hip_sources CONFIGURE_DEPENDS diff --git a/include/strata/hip_compat/cuda_runtime.h b/include/strata/hip_compat/cuda_runtime.h index aebe2e8c..feb557a2 100644 --- a/include/strata/hip_compat/cuda_runtime.h +++ b/include/strata/hip_compat/cuda_runtime.h @@ -1,5 +1,14 @@ #pragma once // Included only by STRATA_ENABLE_HIP builds. CUDA builds use NVIDIA headers. +#if defined(_WIN32) +// hip_runtime.h pulls in on Windows: keep its min/max macros and the rarely used APIs out of the engine. +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#endif #include // Do not let HIP's legacy macro corrupt libstdc++ attribute names. #ifdef __noinline__