HIP: Simplify detection of HIP runtime CMake package

It only makes sense to use the CMake package from the same ROCm
installation that the compiler uses.  Ask the HIP compiler to report the
location of the ROCm installation.  Verify up front that it contains the
expected CMake package file.
This commit is contained in:
Brad King
2021-09-14 17:14:22 +00:00
parent a71f0fc9c7
commit cb93f72624
6 changed files with 29 additions and 125 deletions
+20
View File
@@ -76,6 +76,18 @@ if(NOT CMAKE_HIP_COMPILER_ID_RUN)
_cmake_find_compiler_sysroot(HIP)
endif()
if(NOT CMAKE_HIP_COMPILER_ROCM_ROOT AND CMAKE_HIP_COMPILER_ID STREQUAL "Clang")
execute_process(COMMAND "${CMAKE_HIP_COMPILER}" -v -print-targets
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _CMAKE_HIP_COMPILER_RESULT
OUTPUT_VARIABLE _CMAKE_HIP_COMPILER_STDOUT
ERROR_VARIABLE _CMAKE_HIP_COMPILER_STDERR
)
if(_CMAKE_HIP_COMPILER_RESULT EQUAL 0 AND _CMAKE_HIP_COMPILER_STDERR MATCHES "Found HIP installation: *([^,]*)[,\n]")
set(CMAKE_HIP_COMPILER_ROCM_ROOT "${CMAKE_MATCH_1}")
endif()
endif()
if(NOT CMAKE_HIP_COMPILER_ROCM_ROOT)
execute_process(
COMMAND hipconfig --rocmpath
@@ -89,6 +101,14 @@ endif()
if(NOT CMAKE_HIP_COMPILER_ROCM_ROOT)
message(FATAL_ERROR "Failed to find ROCm root directory.")
endif()
if(NOT EXISTS "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/cmake/hip-lang/hip-lang-config.cmake")
message(FATAL_ERROR
"The ROCm root directory:\n"
" ${CMAKE_HIP_COMPILER_ROCM_ROOT}\n"
"does not contain the HIP runtime CMake package, expected at:\n"
" ${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/cmake/hip-lang/hip-lang-config.cmake\n"
)
endif()
if (NOT _CMAKE_TOOLCHAIN_LOCATION)
get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_HIP_COMPILER}" PATH)
+1
View File
@@ -16,6 +16,7 @@ set(CMAKE_HIP_COMPILER_FRONTEND_VARIANT "@CMAKE_HIP_COMPILER_FRONTEND_VARIANT@")
set(CMAKE_HIP_SIMULATE_VERSION "@CMAKE_HIP_SIMULATE_VERSION@")
@SET_MSVC_HIP_ARCHITECTURE_ID@
@_SET_CMAKE_HIP_COMPILER_SYSROOT@
set(CMAKE_HIP_COMPILER_ROCM_ROOT "@CMAKE_HIP_COMPILER_ROCM_ROOT@")
set(CMAKE_HIP_COMPILER_ENV_VAR "HIPCXX")
+7 -4
View File
@@ -132,8 +132,11 @@ endif()
set(CMAKE_HIP_INFORMATION_LOADED 1)
# Load the file and find the relevant HIP runtime.
# This file will only exist after all compiler detection has finished
include(${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPRuntime.cmake OPTIONAL)
if(COMMAND _CMAKE_FIND_HIP_RUNTIME)
_CMAKE_FIND_HIP_RUNTIME()
if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
set(hip-lang_DIR "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/cmake/hip-lang")
find_package(hip-lang CONFIG QUIET NO_DEFAULT_PATH)
endif()
if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
list(APPEND CMAKE_HIP_RUNTIME_LIBRARIES_STATIC ${_CMAKE_HIP_DEVICE_RUNTIME_TARGET})
list(APPEND CMAKE_HIP_RUNTIME_LIBRARIES_SHARED ${_CMAKE_HIP_DEVICE_RUNTIME_TARGET})
endif()
-99
View File
@@ -1,99 +0,0 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
function(_CMAKE_FIND_HIP_RUNTIME )
# Determined when hipcc is the HIP compiler
set(CMAKE_HIP_COMPILER_ROCM_ROOT "@CMAKE_HIP_COMPILER_ROCM_ROOT@")
# Forward facing value that can be provided by the user
set(CMAKE_HIP_COMPILER_TOOLKIT_ROOT @CMAKE_HIP_COMPILER_TOOLKIT_ROOT@)
if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
set(message_on_found TRUE)
endif()
set(explicit_search_only FALSE)
set(rocm_root_dirs )
if(DEFINED CMAKE_HIP_COMPILER_TOOLKIT_ROOT)
set(rocm_root_dirs "${CMAKE_HIP_COMPILER_TOOLKIT_ROOT}")
set(explicit_search_only TRUE)
set(error_message_location "the variable CMAKE_HIP_COMPILER_TOOLKIT_ROOT [\"${CMAKE_HIP_COMPILER_TOOLKIT_ROOT}\"]")
elseif(DEFINED ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT})
set(rocm_root_dirs "$ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT}")
set(explicit_search_only TRUE)
set(error_message_location "CMAKE_HIP_COMPILER_TOOLKIT_ROOT")
set(error_message_location "the environment variable CMAKE_HIP_COMPILER_TOOLKIT_ROOT [\"$ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT}\"]")
elseif(DEFINED CMAKE_HIP_COMPILER_ROCM_ROOT)
set(rocm_root_dirs "${CMAKE_HIP_COMPILER_ROCM_ROOT}")
set(explicit_search_only TRUE)
set(error_message_location "the associated hipconfig --rocmpath [\"${CMAKE_HIP_COMPILER_ROCM_ROOT}\"]")
endif()
# Guess on where rocm is installed
if(NOT rocm_root_dirs AND (UNIX AND NOT APPLE))
set(platform_base "/opt/rocm-")
# Finad all default rocm installations
file(GLOB possible_paths "${platform_base}*")
set(versions)
foreach(p ${possible_paths})
# Extract version number from end of string
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+$" p_version ${p})
if(IS_DIRECTORY ${p} AND p_version)
list(APPEND versions ${p_version})
endif()
endforeach()
# Sort numerically in descending order, so we try the newest versions first.
list(SORT versions COMPARE NATURAL ORDER DESCENDING)
# With a descending list of versions, populate possible paths to search.
set(rocm_root_dirs "/opt/rocm")
foreach(v IN LISTS versions)
list(APPEND rocm_root_dirs "${platform_base}${v}")
endforeach()
endif()
set(search_rel_path "/lib/cmake/hip-lang/")
list(TRANSFORM rocm_root_dirs APPEND "${search_rel_path}")
find_package(hip-lang
CONFIG
PATHS ${rocm_root_dirs}
QUIET
NO_DEFAULT_PATH
)
if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET AND NOT explicit_search_only)
find_package(hip-lang CONFIG QUIET)
endif()
if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
set(CMAKE_HIP_RUNTIME_LIBRARIES_STATIC
${CMAKE_HIP_RUNTIME_LIBRARIES_STATIC}
${_CMAKE_HIP_DEVICE_RUNTIME_TARGET} PARENT_SCOPE)
set(CMAKE_HIP_RUNTIME_LIBRARIES_SHARED
${CMAKE_HIP_RUNTIME_LIBRARIES_SHARED}
${_CMAKE_HIP_DEVICE_RUNTIME_TARGET} PARENT_SCOPE)
endif()
if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET AND message_on_found)
message(STATUS "Found HIP runtime: ${hip-lang_DIR}")
elseif(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
if(explicit_search_only)
set(error_message "Failed to find the HIP runtime, Could not find hip-lang-config.cmake at the following location(s):\n")
foreach(p IN LISTS rocm_root_dirs)
string(APPEND error_message "\t${p}\n")
endforeach()
string(APPEND "which are computed from the location specified by ${error_message_location}. \
Please specify CMAKE_HIP_COMPILER_TOOLKIT_ROOT to the location of")
message(FATAL_ERROR "${error_message}")
else()
message(FATAL_ERROR
"Failed to find the HIP runtime, Could not find hip-lang-config.cmake.\
Try setting CMAKE_HIP_COMPILER_TOOLKIT_ROOT")
endif()
endif()
endfunction()
-21
View File
@@ -81,20 +81,6 @@ unset(__CMAKE_HIP_FLAGS)
include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
CMAKE_DETERMINE_COMPILE_FEATURES(HIP)
# Setup the following:
# Configure the new template file CMakeHipRuntime.cmake to
# - ${CMAKE_PLATFORM_INFO_DIR}/
# This file will do the actual find_package query. We than have
# CMakeHIPInformation.cmake include `CMakeHipRuntime`
# So it is included once system information has been finished
#
configure_file(
${CMAKE_ROOT}/Modules/CMakeHIPRuntime.cmake.in
${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPRuntime.cmake
@ONLY
)
# Re-configure to save learned information.
configure_file(
${CMAKE_ROOT}/Modules/CMakeHIPCompiler.cmake.in
@@ -113,10 +99,3 @@ endif()
set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
unset(__CMAKE_HIP_COMPILER_OUTPUT)
# Load the file and find the relevant HIP runtime.
# This file will only exist after all compiler detection has finished
include(${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPRuntime.cmake)
if(COMMAND _CMAKE_FIND_HIP_RUNTIME)
_CMAKE_FIND_HIP_RUNTIME()
endif()
+1 -1
View File
@@ -15,6 +15,6 @@ set(CMAKE_HIP_RUNTIME_LIBRARY_DEFAULT "SHARED")
set(CMAKE_HIP_RUNTIME_LIBRARY_LINK_OPTIONS_STATIC "")
set(CMAKE_HIP_RUNTIME_LIBRARY_LINK_OPTIONS_SHARED "")
# Populated by CMakeHIPRuntime.cmake
# Populated by CMakeHIPInformation.cmake
set(CMAKE_HIP_RUNTIME_LIBRARIES_STATIC "")
set(CMAKE_HIP_RUNTIME_LIBRARIES_SHARED "")