From 2b47e072a6fae5515be4d6a4f0b08b8395025b4a Mon Sep 17 00:00:00 2001 From: Lauren Wrubleski Date: Fri, 9 Feb 2024 13:51:34 -0700 Subject: [PATCH 1/2] Enable compatibility with projects using HIP language --- CMakeLists.txt | 43 ++++++++++++++++++++++++++----------- benchmark/CMakeLists.txt | 4 ++-- cmake/SetupNVCC.cmake | 2 +- cmake/Summary.cmake | 13 +++++++---- cmake/VerifyCompiler.cmake | 8 +++---- library/CMakeLists.txt | 9 +++++--- test/CMakeLists.txt | 5 +++++ test/linkage/CMakeLists.txt | 5 +++++ test/package/CMakeLists.txt | 7 +++--- test/parity/CMakeLists.txt | 3 ++- 10 files changed, 69 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ead08b9a..7b62b7302 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,10 +57,29 @@ list(APPEND CMAKE_MODULE_PATH $ENV{ROCM_PATH}/llvm ) +# Detect if already using HIP language +get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES) +if(HIP IN_LIST ENABLED_LANGUAGES OR TRUE) + # do stuff + set(ROCRAND_LANG HIP) + if (DEFINED CACHE{CMAKE_CXX_COMPILER} AND (CMAKE_CXX_COMPILER MATCHES ".*hipcc$" OR CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+")) + # if the user's set the CXX compiler but not the HIP compiler, copy the value. + set(CMAKE_HIP_COMPILER "${CMAKE_CXX_COMPILER}" CACHE PATH "The path to the HIP compiler.") + endif() + # if the user's set the HIP compiler but not the CXX compiler, copy the value. + set(CMAKE_CXX_COMPILER "${CMAKE_HIP_COMPILER}" CACHE PATH "The path to the CXX compiler.") +else() + set(ROCRAND_LANG CXX) +endif() + # # rocRAND project # -project(rocRAND CXX) +project(rocRAND ${ROCRAND_LANG}) +# If using the HIP language, enable CXX language as well +if (ROCRAND_LANG STREQUAL HIP) + enable_language(CXX) +endif() # Set a default build type if none was specified if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "Setting build type to 'Release' as none was specified.") @@ -80,9 +99,9 @@ include(CMakeDependentOption) include(cmake/Dependencies.cmake) # Detect compiler support for target ID -if( CMAKE_CXX_COMPILER MATCHES ".*/hipcc$" ) - execute_process(COMMAND ${CMAKE_CXX_COMPILER} "--help" - OUTPUT_VARIABLE CXX_OUTPUT +if( CMAKE_${ROCRAND_LANG}_COMPILER MATCHES ".*/hipcc$" ) + execute_process(COMMAND ${CMAKE_${ROCRAND_LANG}_COMPILER} "--help" + OUTPUT_VARIABLE ${ROCRAND_LANG}_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE) string(REGEX MATCH ".mcode\-object\-version" TARGET_ID_SUPPORT ${CXX_OUTPUT}) @@ -118,17 +137,17 @@ endif() # Build option to disable -Werror option(DISABLE_WERROR "Disable building with Werror" ON) -# Build CXX flags -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) +# Build CXX/HIP flags +set(CMAKE_${ROCRAND_LANG}_STANDARD 11) +set(CMAKE_${ROCRAND_LANG}_STANDARD_REQUIRED ON) +set(CMAKE_${ROCRAND_LANG}_EXTENSIONS OFF) if(DISABLE_WERROR) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") + set(CMAKE_${ROCRAND_LANG}_FLAGS "${CMAKE_${ROCRAND_LANG}_FLAGS} -Wall -Wextra") else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") + set(CMAKE_${ROCRAND_LANG}_FLAGS "${CMAKE_${ROCRAND_LANG}_FLAGS} -Wall -Wextra -Werror") endif() if(CODE_COVERAGE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping") + set(CMAKE_${ROCRAND_LANG}_FLAGS "${CMAKE_${ROCRAND_LANG}_FLAGS} -fprofile-instr-generate -fcoverage-mapping") endif() # HIP on Windows: xhip is required with clang++ to get __half defined @@ -141,7 +160,7 @@ if(BUILD_LEGACY_BENCHMARK) message(DEPRECATION "Building the legacy benchmarks will be removed in a future release. Consider using the new benchmark suite.") endif() if(BUILD_ADDRESS_SANITIZER AND BUILD_SHARED_LIBS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -shared-libasan") + set(CMAKE_${ROCRAND_LANG}_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -shared-libasan") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -shared-libasan") add_link_options(-fuse-ld=lld) endif() diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index f7a44314b..90eade4fb 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -44,6 +44,7 @@ function(add_rocrand_benchmark benchmark_src legacy) ) target_link_libraries(${BENCHMARK_TARGET} ${CUDA_curand_LIBRARY}) else() + set_source_files_properties(${benchmark_src} PROPERTIES LANGUAGE ${ROCRAND_LANG}) target_link_libraries(${BENCHMARK_TARGET} hip::device) endif() endif() @@ -96,7 +97,7 @@ function(add_rocrand_benchmark benchmark_src legacy) # for now adding in all .dll as dependency chain is not cmake based on win32 file( GLOB third_party_dlls LIST_DIRECTORIES ON - CONFIGURE_DEPENDS + CONFIGURE_DEPENDS ${HIP_DIR}/bin/*.dll ${CMAKE_SOURCE_DIR}/rtest.* ) @@ -121,4 +122,3 @@ if(BUILD_LEGACY_BENCHMARK) add_rocrand_benchmark("benchmark_curand_kernel.cpp" ON) endif() endif() - diff --git a/cmake/SetupNVCC.cmake b/cmake/SetupNVCC.cmake index fa5b73a62..287a63253 100644 --- a/cmake/SetupNVCC.cmake +++ b/cmake/SetupNVCC.cmake @@ -123,5 +123,5 @@ set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${HIP_CPP_CONFIG_FLAGS} ${HIP_NVCC_FLA # Ignore warnings about #pragma unroll # and about deprecated CUDA function(s) used in hip/nvcc_detail/hip_runtime_api.h -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${HIP_CPP_CONFIG_FLAGS_STRIP} -Wno-unknown-pragmas -Wno-deprecated-declarations" +set(CMAKE_${ROCRAND_LANG}_FLAGS "${CMAKE_${ROCRAND_LANG}_FLAGS} ${HIP_CPP_CONFIG_FLAGS_STRIP} -Wno-unknown-pragmas -Wno-deprecated-declarations" CACHE STRING "compile flags" FORCE) diff --git a/cmake/Summary.cmake b/cmake/Summary.cmake index 9e7c2c8bc..24908a00c 100644 --- a/cmake/Summary.cmake +++ b/cmake/Summary.cmake @@ -1,13 +1,18 @@ function (print_configuration_summary) + if(ROCRAND_LANG STREQUAL "HIP") + set(lang_str "HIP") + else() + set(lang_str "C++") + endif() message(STATUS "") message(STATUS "******** Summary ********") message(STATUS "General:") message(STATUS " System : ${CMAKE_SYSTEM_NAME}") message(STATUS " HIP ROOT : ${HIP_ROOT_DIR}") - message(STATUS " C++ compiler : ${CMAKE_CXX_COMPILER}") - message(STATUS " C++ compiler version : ${CMAKE_CXX_COMPILER_VERSION}") - string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS_STRIP) - message(STATUS " CXX flags : ${CMAKE_CXX_FLAGS_STRIP}") + message(STATUS " ${lang_str} compiler : ${CMAKE_${HIP}_COMPILER}") + message(STATUS " ${lang_str} compiler version : ${CMAKE_${HIP}_COMPILER_VERSION}") + string(STRIP "${CMAKE_${HIP}_FLAGS}" CMAKE_${HIP}_FLAGS_STRIP) + message(STATUS " ${lang_str} flags : ${CMAKE_${HIP}_FLAGS_STRIP}") if(HIP_COMPILER STREQUAL "nvcc") string(REPLACE ";" " " HIP_NVCC_FLAGS_STRIP "${HIP_NVCC_FLAGS}") string(STRIP "${HIP_NVCC_FLAGS_STRIP}" HIP_NVCC_FLAGS_STRIP) diff --git a/cmake/VerifyCompiler.cmake b/cmake/VerifyCompiler.cmake index 0adadb8fc..d162b0de7 100644 --- a/cmake/VerifyCompiler.cmake +++ b/cmake/VerifyCompiler.cmake @@ -21,7 +21,7 @@ # SOFTWARE. list(APPEND CMAKE_PREFIX_PATH $ENV{ROCM_PATH} $ENV{ROCM_PATH}/hip) -if(CMAKE_CXX_COMPILER MATCHES ".*/nvcc$" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") +if(CMAKE_${ROCRAND_LANG}_COMPILER MATCHES ".*/nvcc$" OR "${CMAKE_${ROCRAND_LANG}_COMPILER_ID}" STREQUAL "GNU") find_package(hip QUIET CONFIG PATHS $ENV{ROCM_PATH}) if(NOT hip_FOUND) find_package(HIP REQUIRED) @@ -36,15 +36,15 @@ else() endif() if(HIP_COMPILER STREQUAL "nvcc") - if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + if("${CMAKE_${ROCRAND_LANG}_COMPILER_ID}" STREQUAL "GNU") include(SetupNVCC) else() message(WARNING "On CUDA platform 'g++' is recommended C++ compiler.") endif() elseif(HIP_COMPILER STREQUAL "clang") - if(NOT (CMAKE_CXX_COMPILER MATCHES ".*hipcc$" OR CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+")) + if(NOT (CMAKE_${ROCRAND_LANG}_COMPILER MATCHES ".*hipcc$" OR CMAKE_${ROCRAND_LANG}_COMPILER MATCHES ".*clang\\+\\+")) message(FATAL_ERROR "On ROCm platform 'hipcc' or HIP-aware Clang must be used as C++ compiler.") endif() else() message(FATAL_ERROR "HIP_COMPILER must be `clang` (AMD ROCm platform)") -endif() \ No newline at end of file +endif() diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 7a1a876b2..0a24e4311 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -76,6 +76,9 @@ if(NOT USE_HIP_CPU) ) set(CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER}) else() + set_source_files_properties(${rocRAND_SRCS} + PROPERTIES LANGUAGE ${ROCRAND_LANG} + ) target_link_libraries(rocrand PRIVATE hip::device) endif() else() @@ -191,7 +194,7 @@ if (WIN32) install (TARGETS rocrand DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") if (BUILD_TEST) add_custom_command( - TARGET rocrand + TARGET rocrand POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ @@ -200,13 +203,13 @@ if (WIN32) endif() if (BUILD_BENCHMARK) add_custom_command( - TARGET rocrand + TARGET rocrand POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${PROJECT_BINARY_DIR}/benchmark/$ ) - endif() + endif() endif() # Fortran wrappers for rocRAND diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 78d160985..cf3ad8c91 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -69,6 +69,11 @@ foreach(test_src ${rocRAND_TEST_SRCS}) CUDA_STANDARD 14 ) set(CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER}) + else() + set_source_files_properties(${test_name} + PROPERTIES + LANGUAGE ${ROCRAND_LANG} + ) endif() target_include_directories(${test_name} PUBLIC diff --git a/test/linkage/CMakeLists.txt b/test/linkage/CMakeLists.txt index 6485f80fb..d2bd36e6a 100644 --- a/test/linkage/CMakeLists.txt +++ b/test/linkage/CMakeLists.txt @@ -35,6 +35,11 @@ function(add_rocrand_link_test TEST_SOURCES) LANGUAGE CUDA CUDA_STANDARD 14 ) + else() + set_source_files_properties(${test_name} + PROPERTIES + LANGUAGE ${ROCRAND_LANG} + ) endif() target_include_directories(${TEST_TARGET} PUBLIC diff --git a/test/package/CMakeLists.txt b/test/package/CMakeLists.txt index 4a4fc62e4..4a099d20f 100644 --- a/test/package/CMakeLists.txt +++ b/test/package/CMakeLists.txt @@ -59,7 +59,7 @@ if(HIP_COMPILER STREQUAL "nvcc") enable_language(CUDA) set(LANG CUDA) else() - set(LANG CXX) + set(LANG ${ROCRAND_LANG}) endif() set(CMAKE_${LANG}_STANDARD 11) @@ -113,7 +113,7 @@ foreach(test_src ${rocrand_pkg_TEST_SRCS}) hip_cpu_rt::hip_cpu_rt ) elseif(HIP_COMPILER STREQUAL "nvcc") - target_link_libraries(${test_name} + target_link_libraries(${test_name} PRIVATE roc::rocrand ) @@ -130,6 +130,7 @@ foreach(test_src ${rocrand_pkg_TEST_SRCS}) roc::rocrand "-L${HIP_ROOT_DIR}/lib -Wl,-rpath,${HIP_ROOT_DIR}/lib" ) + set_source_files_properties(${test_src} PROPERTIES LANGUAGE ${LANG}) endif() add_relative_test(${test_name} ${test_name}) if (WIN32 AND NOT DEFINED DLLS_COPIED) @@ -145,5 +146,5 @@ foreach(test_src ${rocrand_pkg_TEST_SRCS}) foreach( file_i ${third_party_dlls}) add_custom_command( TARGET ${test_name} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${file_i} ${PROJECT_BINARY_DIR}/test/package ) endforeach( file_i ) - endif() + endif() endforeach() diff --git a/test/parity/CMakeLists.txt b/test/parity/CMakeLists.txt index 9f92f38cf..75448f48d 100644 --- a/test/parity/CMakeLists.txt +++ b/test/parity/CMakeLists.txt @@ -70,7 +70,7 @@ if(HIP_COMPILER STREQUAL "nvcc") enable_language(CUDA) set(LANG CUDA) else() - set(LANG CXX) + set(LANG ${ROCRAND_LANG}) endif() set(CMAKE_${LANG}_STANDARD 14) @@ -114,6 +114,7 @@ else() roc::rocrand "-L${HIP_ROOT_DIR}/lib -Wl,-rpath,${HIP_ROOT_DIR}/lib" ) + set_source_files_properties(parity_rocrand.cpp PROPERTIES LANGUAGE ${LANG}) endif() # Compile the files that invoke each API separately, to avoid symbol namespacing errors. From 6acb73fda40fa5dcee62ddde0fb0b6d6c6701c1f Mon Sep 17 00:00:00 2001 From: Lauren Wrubleski Date: Fri, 9 Feb 2024 13:54:06 -0700 Subject: [PATCH 2/2] Remove "OR TRUE" used for testing --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b62b7302..8ef8d2ff2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,7 @@ list(APPEND CMAKE_MODULE_PATH # Detect if already using HIP language get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES) -if(HIP IN_LIST ENABLED_LANGUAGES OR TRUE) +if(HIP IN_LIST ENABLED_LANGUAGES) # do stuff set(ROCRAND_LANG HIP) if (DEFINED CACHE{CMAKE_CXX_COMPILER} AND (CMAKE_CXX_COMPILER MATCHES ".*hipcc$" OR CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+"))