From b3e92775ab5defa49783b5ff46627c3d3662ab2b Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 16:00:22 -0400 Subject: HIP: Add CMAKE_HIP_HOST_COMPILER when compiler is NVCC Also add `HIPHOSTCXX` environment variable. --- Help/envvar/HIPHOSTCXX.rst | 19 +++++++++++++++++++ Help/manual/cmake-env-variables.7.rst | 1 + Help/variable/CMAKE_LANG_HOST_COMPILER.rst | 13 ++++++++----- Modules/CMakeDetermineHIPCompiler.cmake | 10 ++++++++++ Modules/CheckLanguage.cmake | 4 ++-- Tests/CMakeOnly/CheckLanguage/CMakeLists.txt | 2 +- 6 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 Help/envvar/HIPHOSTCXX.rst diff --git a/Help/envvar/HIPHOSTCXX.rst b/Help/envvar/HIPHOSTCXX.rst new file mode 100644 index 0000000..871fbfb --- /dev/null +++ b/Help/envvar/HIPHOSTCXX.rst @@ -0,0 +1,19 @@ +HIPHOSTCXX +----------- + +.. versionadded:: 3.28 + +.. include:: ENV_VAR.txt + +Preferred executable for compiling host code when compiling ``HIP`` +language files with the NVIDIA CUDA Compiler. Will only be used by CMake +on the first configuration to determine ``HIP`` host compiler, after which +the value for ``HIPHOSTCXX`` is stored in the cache as +:variable:`CMAKE_HIP_HOST_COMPILER _HOST_COMPILER>`. + +This environment variable is primarily meant for use with projects that +enable ``HIP`` as a first-class language. + +.. note:: + + Ignored when using :ref:`Visual Studio Generators`. diff --git a/Help/manual/cmake-env-variables.7.rst b/Help/manual/cmake-env-variables.7.rst index 356e73d..55f07b7 100644 --- a/Help/manual/cmake-env-variables.7.rst +++ b/Help/manual/cmake-env-variables.7.rst @@ -84,6 +84,7 @@ Environment Variables for Languages /envvar/FFLAGS /envvar/HIPCXX /envvar/HIPFLAGS + /envvar/HIPHOSTCXX /envvar/ISPC /envvar/ISPCFLAGS /envvar/OBJC diff --git a/Help/variable/CMAKE_LANG_HOST_COMPILER.rst b/Help/variable/CMAKE_LANG_HOST_COMPILER.rst index 2759784..cf3ba62 100644 --- a/Help/variable/CMAKE_LANG_HOST_COMPILER.rst +++ b/Help/variable/CMAKE_LANG_HOST_COMPILER.rst @@ -4,22 +4,25 @@ CMAKE__HOST_COMPILER .. versionadded:: 3.10 ``CMAKE_CUDA_HOST_COMPILER`` -This variable is available when ```` is ``CUDA``. +.. versionadded:: 3.28 + ``CMAKE_HIP_HOST_COMPILER`` + +This variable is available when ```` is ``CUDA`` or ``HIP``. When :variable:`CMAKE__COMPILER_ID` is ``NVIDIA``, ``CMAKE__HOST_COMPILER`` selects the compiler executable -to use when compiling host code for ``CUDA`` language files. +to use when compiling host code for ``CUDA`` or ``HIP`` language files. This maps to the ``nvcc -ccbin`` option. The ``CMAKE__HOST_COMPILER`` variable may be set explicitly before CUDA -is first enabled by a :command:`project` or :command:`enable_language` +or HIP is first enabled by a :command:`project` or :command:`enable_language` command. This can be done via ``-DCMAKE__HOST_COMPILER=...`` on the command line or in a :ref:`toolchain file `. Or, one may set -the :envvar:`CUDAHOSTCXX` environment variable to +the :envvar:`CUDAHOSTCXX` or :envvar:`HIPHOSTCXX` environment variable to provide a default value. -Once the CUDA language is enabled, the ``CMAKE__HOST_COMPILER`` +Once the CUDA or HIP language is enabled, the ``CMAKE__HOST_COMPILER`` variable is read-only and changes to it are undefined behavior. .. note:: diff --git a/Modules/CMakeDetermineHIPCompiler.cmake b/Modules/CMakeDetermineHIPCompiler.cmake index bc85dbb..e667099 100644 --- a/Modules/CMakeDetermineHIPCompiler.cmake +++ b/Modules/CMakeDetermineHIPCompiler.cmake @@ -107,6 +107,13 @@ if(NOT CMAKE_HIP_COMPILER_ID_RUN) # If the user set CMAKE_HIP_ARCHITECTURES, validate its value. include(Internal/CMakeCUDAArchitecturesValidate) cmake_cuda_architectures_validate(HIP) + + if(NOT CMAKE_HIP_HOST_COMPILER AND NOT $ENV{HIPHOSTCXX} STREQUAL "") + get_filename_component(CMAKE_HIP_HOST_COMPILER $ENV{HIPHOSTCXX} PROGRAM) + if(NOT EXISTS "${CMAKE_HIP_HOST_COMPILER}") + message(FATAL_ERROR "Could not find compiler set in environment variable HIPHOSTCXX:\n$ENV{HIPHOSTCXX}.\n${CMAKE_HIP_HOST_COMPILER}") + endif() + endif() endif() if(CMAKE_HIP_COMPILER_ID STREQUAL "Clang") @@ -114,6 +121,9 @@ if(NOT CMAKE_HIP_COMPILER_ID_RUN) 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") + if(CMAKE_HIP_HOST_COMPILER) + string(APPEND CMAKE_HIP_COMPILER_ID_TEST_FLAGS_FIRST " -ccbin=\"${CMAKE_HIP_HOST_COMPILER}\"") + endif() endif() # We perform compiler identification for a second time to extract implicit linking info. diff --git a/Modules/CheckLanguage.cmake b/Modules/CheckLanguage.cmake index 1ad362f..94948b9 100644 --- a/Modules/CheckLanguage.cmake +++ b/Modules/CheckLanguage.cmake @@ -26,7 +26,7 @@ or :command:`project` commands: a previous call, the check is skipped. :variable:`CMAKE__HOST_COMPILER` - This variable is set when ```` is ``CUDA``. + This variable is set when ```` is ``CUDA`` or ``HIP``. If the check detects an explicit host compiler that is required for compilation, this variable will be set to that compiler. @@ -65,7 +65,7 @@ macro(check_language lang) file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}) set(extra_compiler_variables) - if(${lang} STREQUAL CUDA AND NOT CMAKE_GENERATOR MATCHES "Visual Studio") + if("${lang}" MATCHES "^(CUDA|HIP)$" AND NOT CMAKE_GENERATOR MATCHES "Visual Studio") set(extra_compiler_variables "set(CMAKE_CUDA_HOST_COMPILER \\\"\${CMAKE_CUDA_HOST_COMPILER}\\\")") endif() diff --git a/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt b/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt index 0c76158..f005843 100644 --- a/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt +++ b/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt @@ -13,7 +13,7 @@ endif() unset(expect_Fortran) set(expect_NoSuchLanguage 0) -set(LANGUAGES C CXX Fortran CUDA NoSuchLanguage) +set(LANGUAGES C CXX Fortran CUDA HIP NoSuchLanguage) if(APPLE) list(APPEND LANGUAGES OBJC OBJCXX) endif() -- cgit v0.12