diff --git a/Help/release/dev/hip-chipStar.rst b/Help/release/dev/hip-chipStar.rst new file mode 100644 index 0000000000..9012d85f03 --- /dev/null +++ b/Help/release/dev/hip-chipStar.rst @@ -0,0 +1,5 @@ +hip-chipStar +------------ + +* ``HIP`` language code may now be compiled for SPIR-V targets (e.g., via chipStar). + See the :variable:`CMAKE_HIP_PLATFORM` variable. diff --git a/Help/variable/CMAKE_HIP_ARCHITECTURES.rst b/Help/variable/CMAKE_HIP_ARCHITECTURES.rst index 3f179831ef..96876b8cc0 100644 --- a/Help/variable/CMAKE_HIP_ARCHITECTURES.rst +++ b/Help/variable/CMAKE_HIP_ARCHITECTURES.rst @@ -12,5 +12,9 @@ This is initialized based on the value of :variable:`CMAKE_HIP_PLATFORM`: Uses architectures reported by ``rocm_agent_enumerator``, if available, and otherwise to a default chosen by the compiler. +``spirv`` + Defaults to ``OFF``. The HIP package (e.g., chipStar) handles targeting + via its own configuration. + This variable is used to initialize the :prop_tgt:`HIP_ARCHITECTURES` property on all targets. See the target property for additional information. diff --git a/Help/variable/CMAKE_HIP_PLATFORM.rst b/Help/variable/CMAKE_HIP_PLATFORM.rst index 5e3a2b7d5f..d5b59820e6 100644 --- a/Help/variable/CMAKE_HIP_PLATFORM.rst +++ b/Help/variable/CMAKE_HIP_PLATFORM.rst @@ -13,6 +13,11 @@ The value must be one of: ``nvidia`` NVIDIA GPUs +.. versionadded:: 4.3 + +``spirv`` + SPIR-V target (e.g., chipStar) + If not specified, a default is computed via ``hipconfig --platform``. :variable:`CMAKE_HIP_ARCHITECTURES` entries are interpreted with diff --git a/Modules/CMakeDetermineHIPCompiler.cmake b/Modules/CMakeDetermineHIPCompiler.cmake index 13ec392f4b..16b5e159eb 100644 --- a/Modules/CMakeDetermineHIPCompiler.cmake +++ b/Modules/CMakeDetermineHIPCompiler.cmake @@ -17,15 +17,17 @@ if(NOT CMAKE_HIP_PLATFORM) ) if(_CMAKE_HIPCONFIG_RESULT EQUAL 0 AND _CMAKE_HIPCONFIG_PLATFORM MATCHES "^(nvidia|nvcc)$") set(CMAKE_HIP_PLATFORM "nvidia" CACHE STRING "HIP platform" FORCE) + elseif(_CMAKE_HIPCONFIG_RESULT EQUAL 0 AND _CMAKE_HIPCONFIG_PLATFORM MATCHES "^(spirv)$") + set(CMAKE_HIP_PLATFORM "spirv" CACHE STRING "HIP platform" FORCE) else() set(CMAKE_HIP_PLATFORM "amd" CACHE STRING "HIP platform" FORCE) endif() endif() -if(NOT CMAKE_HIP_PLATFORM MATCHES "^(amd|nvidia)$") +if(NOT CMAKE_HIP_PLATFORM MATCHES "^(amd|nvidia|spirv)$") message(FATAL_ERROR "The CMAKE_HIP_PLATFORM has unsupported value:\n" " '${CMAKE_HIP_PLATFORM}'\n" - "It must be 'amd' or 'nvidia'." + "It must be 'amd', 'nvidia', or 'spirv'." ) endif() @@ -54,10 +56,10 @@ if(NOT CMAKE_HIP_COMPILER) if(NOT CMAKE_HIP_COMPILER_INIT) if(CMAKE_HIP_PLATFORM STREQUAL "nvidia") set(CMAKE_HIP_COMPILER_LIST nvcc) - elseif(CMAKE_HIP_PLATFORM STREQUAL "amd") + elseif(CMAKE_HIP_PLATFORM STREQUAL "amd" OR CMAKE_HIP_PLATFORM STREQUAL "spirv") set(CMAKE_HIP_COMPILER_LIST clang++) - # Look for the Clang coming with ROCm to support HIP. + # Look for the Clang coming with ROCm or chipStar to support HIP. execute_process(COMMAND hipconfig --hipclangpath OUTPUT_VARIABLE _CMAKE_HIPCONFIG_CLANGPATH RESULT_VARIABLE _CMAKE_HIPCONFIG_RESULT @@ -140,7 +142,13 @@ if(NOT CMAKE_HIP_COMPILER_ID_RUN) endif() if(CMAKE_HIP_COMPILER_ID STREQUAL "Clang") - list(APPEND CMAKE_HIP_COMPILER_ID_TEST_FLAGS_FIRST "-v") + # For spirv platform (chipStar), set special flags for compiler identification + include(Internal/CMakeChipStarHIP OPTIONAL) + if(CMAKE_HIP_PLATFORM STREQUAL "spirv") + _cmake_chipstar_set_compiler_id_flags() + else() + list(APPEND CMAKE_HIP_COMPILER_ID_TEST_FLAGS_FIRST "-v") + endif() elseif(CMAKE_HIP_COMPILER_ID STREQUAL "NVIDIA") # Tell nvcc to treat .hip files as CUDA sources. list(APPEND CMAKE_HIP_COMPILER_ID_TEST_FLAGS_FIRST "-x cu -v") @@ -194,11 +202,18 @@ if(NOT CMAKE_HIP_COMPILER_ROCM_ROOT) endif() endif() if(NOT CMAKE_HIP_COMPILER_ROCM_ROOT) - message(FATAL_ERROR "Failed to find ROCm root directory.") + # For spirv platform (chipStar), fall back to HIP_PATH environment variable + if(CMAKE_HIP_PLATFORM STREQUAL "spirv" AND DEFINED ENV{HIP_PATH} AND IS_DIRECTORY "$ENV{HIP_PATH}") + set(CMAKE_HIP_COMPILER_ROCM_ROOT "$ENV{HIP_PATH}") + file(TO_CMAKE_PATH "${CMAKE_HIP_COMPILER_ROCM_ROOT}" CMAKE_HIP_COMPILER_ROCM_ROOT) + else() + message(FATAL_ERROR "Failed to find HIP root directory.") + endif() endif() -if(CMAKE_HIP_PLATFORM STREQUAL "amd") - # For this platform we need the hip-lang cmake package. + +if(CMAKE_HIP_PLATFORM STREQUAL "amd" OR CMAKE_HIP_PLATFORM STREQUAL "spirv") + # For amd and spirv platforms we need the hip-lang cmake package. # Normally implicit link information is not detected until ABI detection, # but we need to populate CMAKE_HIP_LIBRARY_ARCHITECTURE to find hip-lang. @@ -309,6 +324,9 @@ if(CMAKE_HIP_COMPILER_ID STREQUAL "NVIDIA") endif() endif() unset(CMAKE_HIP_ARCHITECTURES_DEFAULT) +elseif(NOT DEFINED CMAKE_HIP_ARCHITECTURES AND CMAKE_HIP_PLATFORM STREQUAL "spirv") + # chipStar handles targeting via hip package + set(CMAKE_HIP_ARCHITECTURES "OFF" CACHE STRING "HIP architectures") elseif(NOT DEFINED CMAKE_HIP_ARCHITECTURES) # Use 'rocm_agent_enumerator' to get the current GPU architecture. set(_CMAKE_HIP_ARCHITECTURES) diff --git a/Modules/CMakeHIPInformation.cmake b/Modules/CMakeHIPInformation.cmake index 9162129819..75fb9fb495 100644 --- a/Modules/CMakeHIPInformation.cmake +++ b/Modules/CMakeHIPInformation.cmake @@ -69,6 +69,13 @@ _cmake_common_language_platform_flags(HIP) # CMAKE_HIP_COMPILE_OBJECT # CMAKE_HIP_LINK_EXECUTABLE +# For spirv platform (chipStar), set compile and link commands +if(CMAKE_HIP_PLATFORM STREQUAL "spirv") + include(Internal/CMakeChipStarHIP OPTIONAL) + _cmake_chipstar_set_compiler_flags() + _cmake_chipstar_set_link_commands() +endif() + # create a shared library if(NOT CMAKE_HIP_CREATE_SHARED_LIBRARY) set(CMAKE_HIP_CREATE_SHARED_LIBRARY diff --git a/Modules/CMakeTestHIPCompiler.cmake b/Modules/CMakeTestHIPCompiler.cmake index 60b212173c..ed1df10656 100644 --- a/Modules/CMakeTestHIPCompiler.cmake +++ b/Modules/CMakeTestHIPCompiler.cmake @@ -11,7 +11,7 @@ endif() set(__CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS}") -if(CMAKE_HIP_COMPILER_ID STREQUAL "Clang") +if(CMAKE_HIP_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_HIP_PLATFORM STREQUAL "spirv") string(APPEND CMAKE_HIP_FLAGS " --cuda-host-only") endif() diff --git a/Modules/Internal/CMakeChipStarHIP.cmake b/Modules/Internal/CMakeChipStarHIP.cmake new file mode 100644 index 0000000000..2403351526 --- /dev/null +++ b/Modules/Internal/CMakeChipStarHIP.cmake @@ -0,0 +1,54 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file LICENSE.rst or https://cmake.org/licensing for details. + +# Helper functions for chipStar (spirv platform) HIP support + +# Set chipStar compile flags in CMAKE_HIP_COMPILE_OBJECT +function(_cmake_chipstar_set_compiler_flags) + if(NOT CMAKE_HIP_PLATFORM STREQUAL "spirv" OR NOT CMAKE_HIP_COMPILER_ID STREQUAL "Clang") + return() + endif() + set(_hip_path "${CMAKE_HIP_COMPILER_ROCM_ROOT}") + set(_chipstar_flags "--offload=spirv64 --hip-path=${_hip_path} -nogpulib -nohipwrapperinc -include ${_hip_path}/include/hip/spirv_fixups.h") + set(CMAKE_HIP_COMPILE_OBJECT + " ${_chipstar_flags} -o -x hip -c " PARENT_SCOPE) +endfunction() + +# Set link commands for chipStar to include libCHIP.so +# This makes CMake treat it as if the compiler driver links it implicitly +function(_cmake_chipstar_set_link_commands) + if(NOT CMAKE_HIP_PLATFORM STREQUAL "spirv") + return() + endif() + set(_hip_path "${CMAKE_HIP_COMPILER_ROCM_ROOT}") + set(_chipstar_lib "${_hip_path}/lib/libCHIP.so") + # Set link command templates with libCHIP.so appended (quoted for paths with spaces) + set(CMAKE_HIP_LINK_EXECUTABLE + " -o \"${_chipstar_lib}\"" PARENT_SCOPE) + set(CMAKE_HIP_CREATE_SHARED_LIBRARY + " -o \"${_chipstar_lib}\"" PARENT_SCOPE) + set(CMAKE_HIP_CREATE_SHARED_MODULE + " -o \"${_chipstar_lib}\"" PARENT_SCOPE) +endfunction() + + + +# Set compiler ID test flags for chipStar (called before CMAKE_HIP_COMPILER_ROCM_ROOT is set) +function(_cmake_chipstar_set_compiler_id_flags) + if(NOT CMAKE_HIP_PLATFORM STREQUAL "spirv") + return() + endif() + # At this point CMAKE_HIP_COMPILER_ROCM_ROOT isn't set yet, use HIP_PATH or hipconfig + set(_hip_path "$ENV{HIP_PATH}") + if(NOT IS_DIRECTORY "${_hip_path}") + execute_process(COMMAND hipconfig --rocmpath + OUTPUT_VARIABLE _hip_path OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() + if(IS_DIRECTORY "${_hip_path}") + file(TO_CMAKE_PATH "${_hip_path}" _hip_path) + # All flags in one string so they're used together. -no-hip-rt prevents linking amdhip64. + list(APPEND CMAKE_HIP_COMPILER_ID_TEST_FLAGS_FIRST + "-v --offload=spirv64 --hip-path=${_hip_path} -nogpulib -nohipwrapperinc -no-hip-rt -L${_hip_path}/lib -lCHIP -include ${_hip_path}/include/hip/spirv_fixups.h") + set(CMAKE_HIP_COMPILER_ID_TEST_FLAGS_FIRST "${CMAKE_HIP_COMPILER_ID_TEST_FLAGS_FIRST}" PARENT_SCOPE) + endif() +endfunction() diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 74eca71b74..32f756543a 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -2764,6 +2764,11 @@ void cmGeneratorTarget::AddHIPArchitectureFlags(cmBuildStep compileOrLink, return; } + // For spirv platform, chipStar handles targeting via the hip package + if (this->Makefile->GetSafeDefinition("CMAKE_HIP_PLATFORM") == "spirv") { + return; + } + cmList options(arch); for (std::string& option : options) {