Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable compatibility with projects using HIP language #443

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +61 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe just finish up #266 and suggest that users to set -DROCRAND_CMAKE_HIP_LANGUAGE=ON?

# 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.")
Expand All @@ -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$" )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hipcc cannot be used as the HIP language compiler.

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})
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.*
)
Expand All @@ -121,4 +122,3 @@ if(BUILD_LEGACY_BENCHMARK)
add_rocrand_benchmark("benchmark_curand_kernel.cpp" ON)
endif()
endif()

2 changes: 1 addition & 1 deletion cmake/SetupNVCC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 9 additions & 4 deletions cmake/Summary.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 4 additions & 4 deletions cmake/VerifyCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
endif()
9 changes: 6 additions & 3 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
$<TARGET_FILE:rocrand>
Expand All @@ -200,13 +203,13 @@ if (WIN32)
endif()
if (BUILD_BENCHMARK)
add_custom_command(
TARGET rocrand
TARGET rocrand
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:rocrand>
${PROJECT_BINARY_DIR}/benchmark/$<TARGET_FILE_NAME:rocrand>
)
endif()
endif()
endif()

# Fortran wrappers for rocRAND
Expand Down
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/linkage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions test/package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand All @@ -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)
Expand All @@ -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()
3 changes: 2 additions & 1 deletion test/parity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down