From 8124950f6ca7d5f5157e48bf3e881115c23bb224 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 15:59:20 -0400 Subject: CUDA: Generalize CMAKE_{CUDA => }_HOST_COMPILER variable docs --- Help/envvar/CUDAHOSTCXX.rst | 6 +++-- Help/manual/cmake-variables.7.rst | 1 + Help/variable/CMAKE_CUDA_HOST_COMPILER.rst | 34 +++---------------------- Help/variable/CMAKE_LANG_HOST_COMPILER.rst | 41 ++++++++++++++++++++++++++++++ Modules/CheckLanguage.cmake | 10 ++++---- 5 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 Help/variable/CMAKE_LANG_HOST_COMPILER.rst diff --git a/Help/envvar/CUDAHOSTCXX.rst b/Help/envvar/CUDAHOSTCXX.rst index 74f5d48..4e78afa 100644 --- a/Help/envvar/CUDAHOSTCXX.rst +++ b/Help/envvar/CUDAHOSTCXX.rst @@ -8,8 +8,10 @@ CUDAHOSTCXX Preferred executable for compiling host code when compiling ``CUDA`` language files. Will only be used by CMake on the first configuration to determine ``CUDA`` host compiler, after which the value for ``CUDAHOSTCXX`` is -stored in the cache as :variable:`CMAKE_CUDA_HOST_COMPILER`. This environment -variable is preferred over :variable:`CMAKE_CUDA_HOST_COMPILER`. +stored in the cache as +:variable:`CMAKE_CUDA_HOST_COMPILER _HOST_COMPILER>`. +This environment variable is preferred over +:variable:`CMAKE_CUDA_HOST_COMPILER _HOST_COMPILER>`. This environment variable is primarily meant for use with projects that enable ``CUDA`` as a first-class language. diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index 536046a..d9df773 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -628,6 +628,7 @@ Variables for Languages /variable/CMAKE_LANG_FLAGS_RELEASE_INIT /variable/CMAKE_LANG_FLAGS_RELWITHDEBINFO /variable/CMAKE_LANG_FLAGS_RELWITHDEBINFO_INIT + /variable/CMAKE_LANG_HOST_COMPILER /variable/CMAKE_LANG_IGNORE_EXTENSIONS /variable/CMAKE_LANG_IMPLICIT_INCLUDE_DIRECTORIES /variable/CMAKE_LANG_IMPLICIT_LINK_DIRECTORIES diff --git a/Help/variable/CMAKE_CUDA_HOST_COMPILER.rst b/Help/variable/CMAKE_CUDA_HOST_COMPILER.rst index 6e2d756..498e2e5 100644 --- a/Help/variable/CMAKE_CUDA_HOST_COMPILER.rst +++ b/Help/variable/CMAKE_CUDA_HOST_COMPILER.rst @@ -3,34 +3,6 @@ CMAKE_CUDA_HOST_COMPILER .. versionadded:: 3.10 -When :variable:`CMAKE_CUDA_COMPILER_ID _COMPILER_ID>` is -``NVIDIA``, ``CMAKE_CUDA_HOST_COMPILER`` selects the compiler executable to use -when compiling host code for ``CUDA`` language files. -This maps to the ``nvcc -ccbin`` option. - -The ``CMAKE_CUDA_HOST_COMPILER`` variable may be set explicitly before CUDA is -first enabled by a :command:`project` or :command:`enable_language` command. -This can be done via ``-DCMAKE_CUDA_HOST_COMPILER=...`` on the command line -or in a :ref:`toolchain file `. Or, one may set -the :envvar:`CUDAHOSTCXX` environment variable to provide a default value. - -Once the CUDA language is enabled, the ``CMAKE_CUDA_HOST_COMPILER`` variable -is read-only and changes to it are undefined behavior. - -.. note:: - - Since ``CMAKE_CUDA_HOST_COMPILER`` is meaningful only when the - :variable:`CMAKE_CUDA_COMPILER_ID _COMPILER_ID>` is ``NVIDIA``, - it does not make sense to set ``CMAKE_CUDA_HOST_COMPILER`` without also - setting ``CMAKE_CUDA_COMPILER`` to NVCC. - -.. note:: - - Projects should not try to set ``CMAKE_CUDA_HOST_COMPILER`` to match - :variable:`CMAKE_CXX_COMPILER _COMPILER>` themselves. - It is the end-user's responsibility, not the project's, to ensure that - the C++ and CUDA compilers target the same ABI. - -.. note:: - - Ignored when using :ref:`Visual Studio Generators`. +This is the original CUDA-specific name for the more general +:variable:`CMAKE__HOST_COMPILER` variable. See the latter +for details. diff --git a/Help/variable/CMAKE_LANG_HOST_COMPILER.rst b/Help/variable/CMAKE_LANG_HOST_COMPILER.rst new file mode 100644 index 0000000..2759784 --- /dev/null +++ b/Help/variable/CMAKE_LANG_HOST_COMPILER.rst @@ -0,0 +1,41 @@ +CMAKE__HOST_COMPILER +-------------------------- + +.. versionadded:: 3.10 + ``CMAKE_CUDA_HOST_COMPILER`` + +This variable is available when ```` is ``CUDA``. + +When :variable:`CMAKE__COMPILER_ID` is +``NVIDIA``, ``CMAKE__HOST_COMPILER`` selects the compiler executable +to use when compiling host code for ``CUDA`` 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` +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 +provide a default value. + +Once the CUDA language is enabled, the ``CMAKE__HOST_COMPILER`` +variable is read-only and changes to it are undefined behavior. + +.. note:: + + Since ``CMAKE__HOST_COMPILER`` is meaningful only when the + :variable:`CMAKE__COMPILER_ID` is ``NVIDIA``, + it does not make sense to set ``CMAKE__HOST_COMPILER`` without also + setting ``CMAKE__COMPILER`` to NVCC. + +.. note:: + + Projects should not try to set ``CMAKE__HOST_COMPILER`` to match + :variable:`CMAKE_CXX_COMPILER _COMPILER>` themselves. + It is the end-user's responsibility, not the project's, to ensure that + NVCC targets the same ABI as the C++ compiler. + +.. note:: + + Ignored when using :ref:`Visual Studio Generators`. diff --git a/Modules/CheckLanguage.cmake b/Modules/CheckLanguage.cmake index 04e721c..1ad362f 100644 --- a/Modules/CheckLanguage.cmake +++ b/Modules/CheckLanguage.cmake @@ -25,7 +25,7 @@ or :command:`project` commands: If this variable is already set, either explicitly or cached by a previous call, the check is skipped. - :variable:`CMAKE_CUDA_HOST_COMPILER` + :variable:`CMAKE__HOST_COMPILER` This variable is set when ```` is ``CUDA``. If the check detects an explicit host compiler that is required for @@ -34,12 +34,12 @@ or :command:`project` commands: this variable will be cleared. If this variable is already set, its value is preserved only if - :variable:`CMAKE_CUDA_COMPILER _COMPILER>` is also set. + :variable:`CMAKE__COMPILER` is also set. Otherwise, the check runs and overwrites - :variable:`CMAKE_CUDA_HOST_COMPILER` with a new result. - Note that :variable:`CMAKE_CUDA_HOST_COMPILER` documents it should + :variable:`CMAKE__HOST_COMPILER` with a new result. + Note that :variable:`CMAKE__HOST_COMPILER` documents it should not be set without also setting - :variable:`CMAKE_CUDA_COMPILER _COMPILER>` to a NVCC compiler. + :variable:`CMAKE__COMPILER` to a NVCC compiler. For example: -- cgit v0.12 From 2a606636706fb9000b7c199b3d1592c40ed15ed1 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 11:37:03 -0400 Subject: HIP: Simplify CMAKE_GENERATOR references for determining compiler --- Modules/CMakeDetermineHIPCompiler.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/CMakeDetermineHIPCompiler.cmake b/Modules/CMakeDetermineHIPCompiler.cmake index 21322f1..28a4f1d 100644 --- a/Modules/CMakeDetermineHIPCompiler.cmake +++ b/Modules/CMakeDetermineHIPCompiler.cmake @@ -5,8 +5,8 @@ include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake) include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake) include(${CMAKE_ROOT}/Modules/CMakeParseLibraryArchitecture.cmake) -if( NOT ( ("${CMAKE_GENERATOR}" MATCHES "Make") OR - ("${CMAKE_GENERATOR}" MATCHES "Ninja") ) ) +if(NOT ((CMAKE_GENERATOR MATCHES "Make") OR + (CMAKE_GENERATOR MATCHES "Ninja"))) message(FATAL_ERROR "HIP language not currently supported by \"${CMAKE_GENERATOR}\" generator") endif() -- cgit v0.12 From 09d759dc7f2e47eef1d04d77aec6f1ed45274937 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 14:19:32 -0400 Subject: HIP: Simplify exclusion of AMD device runtime with NVIDIA GPUs --- Modules/Compiler/NVIDIA-HIP.cmake | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Modules/Compiler/NVIDIA-HIP.cmake b/Modules/Compiler/NVIDIA-HIP.cmake index e144ff8..c888bc7 100644 --- a/Modules/Compiler/NVIDIA-HIP.cmake +++ b/Modules/Compiler/NVIDIA-HIP.cmake @@ -2,10 +2,8 @@ include(Compiler/NVIDIA) __compiler_nvidia_cxx_standards(HIP) __compiler_nvidia_cuda_flags(HIP) -# The ROCm hip-lang package does not work for nvcc, -# so provide a minimal one ourselves. -add_library(hip-lang::device INTERFACE IMPORTED) -set(_CMAKE_HIP_DEVICE_RUNTIME_TARGET hip-lang::device) +# The ROCm hip-lang cmake package's device runtime library is not needed for NVIDIA GPUs. +set(_CMAKE_HIP_DEVICE_RUNTIME_TARGET "") set(CMAKE_HIP_STANDARD_INCLUDE_DIRECTORIES "${CMAKE_HIP_COMPILER_ROCM_ROOT}/include") -- cgit v0.12 From 479450512253e904c077c5e77fc04bc916466930 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 11:40:17 -0400 Subject: HIP: Do not require hip-lang package for NVIDIA platform ROCm may not install the `hip-lang` cmake package on NVIDIA platforms. Skip verifying its presence. We do not use it for NVIDIA platforms anyway. Fixes: #25271 --- Modules/CMakeDetermineHIPCompiler.cmake | 94 +++++++++++++++++---------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/Modules/CMakeDetermineHIPCompiler.cmake b/Modules/CMakeDetermineHIPCompiler.cmake index 28a4f1d..bc85dbb 100644 --- a/Modules/CMakeDetermineHIPCompiler.cmake +++ b/Modules/CMakeDetermineHIPCompiler.cmake @@ -164,57 +164,61 @@ if(NOT CMAKE_HIP_COMPILER_ROCM_ROOT) message(FATAL_ERROR "Failed to find ROCm root directory.") endif() -# Normally implicit link information is not detected until ABI detection, -# but we need to populate CMAKE_HIP_LIBRARY_ARCHITECTURE to find hip-lang. -cmake_parse_implicit_link_info("${CMAKE_HIP_COMPILER_PRODUCED_OUTPUT}" - _CMAKE_HIP_COMPILER_ID_IMPLICIT_LIBS - _CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS - _CMAKE_HIP_COMPILER_ID_IMPLICIT_FWKS - _CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG - "" LANGUAGE HIP) -message(CONFIGURE_LOG - "Parsed HIP implicit link information from compiler id output:\n${_CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG}\n\n") -cmake_parse_library_architecture(HIP "${_CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS}" "" CMAKE_HIP_LIBRARY_ARCHITECTURE) -if(CMAKE_HIP_LIBRARY_ARCHITECTURE) +if(CMAKE_HIP_PLATFORM STREQUAL "amd") + # For this platform 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. + cmake_parse_implicit_link_info("${CMAKE_HIP_COMPILER_PRODUCED_OUTPUT}" + _CMAKE_HIP_COMPILER_ID_IMPLICIT_LIBS + _CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS + _CMAKE_HIP_COMPILER_ID_IMPLICIT_FWKS + _CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG + "" LANGUAGE HIP) message(CONFIGURE_LOG - "Parsed HIP library architecture from compiler id output: ${CMAKE_HIP_LIBRARY_ARCHITECTURE}\n") -endif() -unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_LIBS) -unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS) -unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_FWKS) -unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG) - -if(NOT CMAKE_HIP_COMPILER_ROCM_LIB) - set(_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS - "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib" - "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib64" - ) + "Parsed HIP implicit link information from compiler id output:\n${_CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG}\n\n") + cmake_parse_library_architecture(HIP "${_CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS}" "" CMAKE_HIP_LIBRARY_ARCHITECTURE) if(CMAKE_HIP_LIBRARY_ARCHITECTURE) - list(APPEND _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/${CMAKE_HIP_LIBRARY_ARCHITECTURE}") + message(CONFIGURE_LOG + "Parsed HIP library architecture from compiler id output: ${CMAKE_HIP_LIBRARY_ARCHITECTURE}\n") endif() - foreach(dir IN LISTS _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS) - if(EXISTS "${dir}/cmake/hip-lang/hip-lang-config.cmake") - set(CMAKE_HIP_COMPILER_ROCM_LIB "${dir}") - break() - endif() - endforeach() + unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_LIBS) + unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS) + unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_FWKS) + unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG) + if(NOT CMAKE_HIP_COMPILER_ROCM_LIB) - list(TRANSFORM _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS APPEND "/cmake/hip-lang/hip-lang-config.cmake") - string(REPLACE ";" "\n " _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS "${_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS}") - message(FATAL_ERROR - "The ROCm root directory:\n" - " ${CMAKE_HIP_COMPILER_ROCM_ROOT}\n" - "does not contain the HIP runtime CMake package, expected at one of:\n" - " ${_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS}\n" + set(_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS + "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib" + "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib64" ) + if(CMAKE_HIP_LIBRARY_ARCHITECTURE) + list(APPEND _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/${CMAKE_HIP_LIBRARY_ARCHITECTURE}") + endif() + foreach(dir IN LISTS _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS) + if(EXISTS "${dir}/cmake/hip-lang/hip-lang-config.cmake") + set(CMAKE_HIP_COMPILER_ROCM_LIB "${dir}") + break() + endif() + endforeach() + if(NOT CMAKE_HIP_COMPILER_ROCM_LIB) + list(TRANSFORM _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS APPEND "/cmake/hip-lang/hip-lang-config.cmake") + string(REPLACE ";" "\n " _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS "${_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS}") + message(FATAL_ERROR + "The ROCm root directory:\n" + " ${CMAKE_HIP_COMPILER_ROCM_ROOT}\n" + "does not contain the HIP runtime CMake package, expected at one of:\n" + " ${_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS}\n" + ) + endif() + unset(_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS) + endif() + if(CMAKE_HIP_COMPILER_ROCM_LIB MATCHES "/lib64$" AND NOT DEFINED CMAKE_SIZEOF_VOID_P) + # We have not yet determined the target ABI but we need 'find_package' to + # search lib64 directories to find hip-lang CMake package dependencies. + # This will be replaced by ABI detection later. + set(CMAKE_HIP_SIZEOF_DATA_PTR 8) endif() - unset(_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS) -endif() -if(CMAKE_HIP_COMPILER_ROCM_LIB MATCHES "/lib64$" AND NOT DEFINED CMAKE_SIZEOF_VOID_P) - # We have not yet determined the target ABI but we need 'find_package' to - # search lib64 directories to find hip-lang CMake package dependencies. - # This will be replaced by ABI detection later. - set(CMAKE_HIP_SIZEOF_DATA_PTR 8) endif() if (NOT _CMAKE_TOOLCHAIN_LOCATION) -- cgit v0.12 From e43918b4caf8de881cb24a0a7494e086202cff34 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 15:12:14 -0400 Subject: HIP: Fix linking mixed-lang binary with CXX compiler and Makefile generators Following commit 6377a43814 (CUDA: Support response files with nvcc, 2022-06-01, v3.25.0-rc1~636^2), while determining the compiler ABI, do not use response files, so that we can extract implicit link flags. Fixes: #25272 --- Source/cmCoreTryCompile.cxx | 14 ++++++++++---- Tests/HIP/MixedLanguage/CMakeLists.txt | 4 ++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 0674d5a..ee40bd5 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -732,10 +732,16 @@ cm::optional cmCoreTryCompile::TryCompileCode( // The link and compile lines for ABI detection step need to not use // response files so we can extract implicit includes given to // the underlying host compiler - if (testLangs.find("CUDA") != testLangs.end()) { - fprintf(fout, "set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES OFF)\n"); - fprintf(fout, "set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES OFF)\n"); - fprintf(fout, "set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_OBJECTS OFF)\n"); + static std::array const noRSP{ { "CUDA", "HIP" } }; + for (std::string const& lang : noRSP) { + if (testLangs.find(lang) != testLangs.end()) { + fprintf(fout, "set(CMAKE_%s_USE_RESPONSE_FILE_FOR_INCLUDES OFF)\n", + lang.c_str()); + fprintf(fout, "set(CMAKE_%s_USE_RESPONSE_FILE_FOR_LIBRARIES OFF)\n", + lang.c_str()); + fprintf(fout, "set(CMAKE_%s_USE_RESPONSE_FILE_FOR_OBJECTS OFF)\n", + lang.c_str()); + } } } fprintf(fout, "set(CMAKE_VERBOSE_MAKEFILE 1)\n"); diff --git a/Tests/HIP/MixedLanguage/CMakeLists.txt b/Tests/HIP/MixedLanguage/CMakeLists.txt index 4f6dd3b..af58ade 100644 --- a/Tests/HIP/MixedLanguage/CMakeLists.txt +++ b/Tests/HIP/MixedLanguage/CMakeLists.txt @@ -17,3 +17,7 @@ set_target_properties(MixedStaticLib PROPERTIES POSITION_INDEPENDENT_CODE ON) add_executable(HIPMixedLanguage main.cxx) target_link_libraries(HIPMixedLanguage PRIVATE MixedStaticLib MixedSharedLib) + +add_executable(HIPMixedLanguageCXX main.cxx) +target_link_libraries(HIPMixedLanguageCXX PRIVATE MixedStaticLib MixedSharedLib) +set_property(TARGET HIPMixedLanguageCXX PROPERTY LINKER_LANGUAGE CXX) -- cgit v0.12 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 From 8c8b3f1bfaf055910c2764fa710da583a247338a Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 16:16:53 -0400 Subject: HIP: Fix support for -DCMAKE_HIP_ARCHITECTURES=native with NVCC --- Modules/CMakeDetermineCompilerABI.cmake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Modules/CMakeDetermineCompilerABI.cmake b/Modules/CMakeDetermineCompilerABI.cmake index 13bfeec..7d8fa19 100644 --- a/Modules/CMakeDetermineCompilerABI.cmake +++ b/Modules/CMakeDetermineCompilerABI.cmake @@ -26,13 +26,13 @@ function(CMAKE_DETERMINE_COMPILER_ABI lang src) if(DEFINED CMAKE_${lang}_VERBOSE_COMPILE_FLAG) set(COMPILE_DEFINITIONS "${CMAKE_${lang}_VERBOSE_COMPILE_FLAG}") endif() - if(lang STREQUAL "CUDA") - if(CMAKE_CUDA_ARCHITECTURES STREQUAL "native") + if(lang MATCHES "^(CUDA|HIP)$") + if(CMAKE_${lang}_ARCHITECTURES STREQUAL "native") # We are about to detect the native architectures, so we do # not yet know them. Use all architectures during detection. - set(CMAKE_CUDA_ARCHITECTURES "all") + set(CMAKE_${lang}_ARCHITECTURES "all") endif() - set(CMAKE_CUDA_RUNTIME_LIBRARY "Static") + set(CMAKE_${lang}_RUNTIME_LIBRARY "Static") endif() if(NOT "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xMSVC") # Avoid adding our own platform standard libraries for compilers -- cgit v0.12 From fd982eec10840eabcccc1920c7c32eb0a13d65a9 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Sep 2023 16:22:04 -0400 Subject: HIP: Add tests for special NVIDIA values of CMAKE_HIP_ARCHITECTURES --- Tests/HIP/ArchSpecial/CMakeLists.txt | 66 ++++++++++++++++++++++++++++++++++++ Tests/HIP/ArchSpecial/main.hip | 3 ++ Tests/HIP/CMakeLists.txt | 3 ++ 3 files changed, 72 insertions(+) create mode 100644 Tests/HIP/ArchSpecial/CMakeLists.txt create mode 100644 Tests/HIP/ArchSpecial/main.hip diff --git a/Tests/HIP/ArchSpecial/CMakeLists.txt b/Tests/HIP/ArchSpecial/CMakeLists.txt new file mode 100644 index 0000000..2bc6bd2 --- /dev/null +++ b/Tests/HIP/ArchSpecial/CMakeLists.txt @@ -0,0 +1,66 @@ +cmake_minimum_required(VERSION 3.27) +project(ArchSpecial HIP) + +if(CMAKE_HIP_COMPILER_ID STREQUAL "NVIDIA" AND + CMAKE_HIP_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0) + set(compile_options -Wno-deprecated-gpu-targets) +endif() + +function(verify_output flag) + string(REPLACE "-" "_" architectures "${flag}") + string(TOUPPER "${architectures}" architectures) + set(architectures "${CMAKE_HIP_ARCHITECTURES_${architectures}}") + list(TRANSFORM architectures REPLACE "-real" "") + + if(CMAKE_HIP_COMPILER_ID STREQUAL "NVIDIA") + set(match_regex "-arch compute_([0-9]+)") + endif() + + string(REGEX MATCHALL "${match_regex}" target_cpus "${output}") + + foreach(cpu ${target_cpus}) + string(REGEX MATCH "${match_regex}" dont_care "${cpu}") + list(APPEND command_archs "${CMAKE_MATCH_1}") + endforeach() + + list(SORT command_archs) + list(REMOVE_DUPLICATES command_archs) + if(NOT "${command_archs}" STREQUAL "${architectures}") + message(FATAL_ERROR "Architectures used for \"${flag}\" don't match the reference (\"${command_archs}\" != \"${architectures}\").") + endif() +endfunction() + +set(try_compile_flags -v ${compile_options}) + +set(CMAKE_HIP_ARCHITECTURES all) +try_compile(all_archs_compiles + ${CMAKE_CURRENT_BINARY_DIR}/try_compile/all_archs_compiles + ${CMAKE_CURRENT_SOURCE_DIR}/main.hip + COMPILE_DEFINITIONS ${try_compile_flags} + OUTPUT_VARIABLE output + ) +verify_output(all) + +set(CMAKE_HIP_ARCHITECTURES all-major) +try_compile(all_major_archs_compiles + ${CMAKE_CURRENT_BINARY_DIR}/try_compile/all_major_archs_compiles + ${CMAKE_CURRENT_SOURCE_DIR}/main.hip + COMPILE_DEFINITIONS ${try_compile_flags} + OUTPUT_VARIABLE output + ) +verify_output(all-major) + +set(CMAKE_HIP_ARCHITECTURES native) +try_compile(native_archs_compiles + ${CMAKE_CURRENT_BINARY_DIR}/try_compile/native_archs_compiles + ${CMAKE_CURRENT_SOURCE_DIR}/main.hip + COMPILE_DEFINITIONS ${try_compile_flags} + OUTPUT_VARIABLE output + ) +verify_output(native) + +if(all_archs_compiles AND all_major_archs_compiles AND native_archs_compiles) + set(CMAKE_HIP_ARCHITECTURES all) + add_executable(HIPArchSpecial main.hip) + target_compile_options(HIPArchSpecial PRIVATE ${compile_options}) +endif() diff --git a/Tests/HIP/ArchSpecial/main.hip b/Tests/HIP/ArchSpecial/main.hip new file mode 100644 index 0000000..5047a34 --- /dev/null +++ b/Tests/HIP/ArchSpecial/main.hip @@ -0,0 +1,3 @@ +int main() +{ +} diff --git a/Tests/HIP/CMakeLists.txt b/Tests/HIP/CMakeLists.txt index 26d7459..f1e2e51 100644 --- a/Tests/HIP/CMakeLists.txt +++ b/Tests/HIP/CMakeLists.txt @@ -5,6 +5,9 @@ macro (add_hip_test_macro name) endmacro () add_hip_test_macro(HIP.ArchitectureOff HIPOnlyArchitectureOff) +if(CMake_TEST_HIP STREQUAL "nvidia") + add_hip_test_macro(HIP.ArchSpecial HIPArchSpecial) +endif() add_hip_test_macro(HIP.CompileFlags HIPOnlyCompileFlags) add_hip_test_macro(HIP.EnableStandard HIPEnableStandard) add_hip_test_macro(HIP.InferHipLang1 HIPInferHipLang1) -- cgit v0.12