Skip to content

Commit 3e67d81

Browse files
Encapsulate vendor math lib logic in helper
1 parent af1a72a commit 3e67d81

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

offload/unittests/CMakeLists.txt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ endif ()
1818
set(OFFLOAD_UNITTESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1919

2020
function(add_offload_test_device_code test_filename test_name)
21+
cmake_parse_arguments(
22+
"ARGS" "WITH_DEVICE_MATH_LIBS" "" "" ${ARGN})
23+
2124
set(SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${test_filename})
2225
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
2326

@@ -37,13 +40,25 @@ function(add_offload_test_device_code test_filename test_name)
3740
endif()
3841

3942
if(nvptx_arch AND CUDAToolkit_FOUND)
43+
set(nvptx_compile_flags ${ARGS_UNPARSED_ARGUMENTS})
44+
45+
if(ARGS_WITH_DEVICE_MATH_LIBS)
46+
file(GLOB libdevice_paths "${CUDAToolkit_LIBRARY_ROOT}/nvvm/libdevice/libdevice.*.bc")
47+
if(libdevice_paths)
48+
list(GET libdevice_paths 0 libdevice_path)
49+
list(APPEND nvptx_compile_flags "-Xclang" "-mlink-builtin-bitcode")
50+
list(APPEND nvptx_compile_flags "-Xclang" "${libdevice_path}")
51+
list(APPEND nvptx_compile_flags "-DCUDA_MATH_FOUND=1")
52+
endif()
53+
endif()
54+
4055
set(output_file "${CMAKE_CURRENT_BINARY_DIR}/${test_name}.nvptx64.bin")
4156
add_custom_command(
4257
OUTPUT ${output_file}
4358
COMMAND ${CMAKE_CXX_COMPILER}
4459
-I${OFFLOAD_UNITTESTS_DIR}
4560
--target=nvptx64-nvidia-cuda -march=${nvptx_arch}
46-
-nogpulib --cuda-path=${cuda_path} -flto ${ARGN}
61+
-nogpulib --cuda-path=${cuda_path} -flto ${nvptx_compile_flags}
4762
${SRC_PATH} -o ${output_file}
4863
DEPENDS ${SRC_PATH}
4964
)
@@ -62,13 +77,25 @@ function(add_offload_test_device_code test_filename test_name)
6277
endif()
6378

6479
if(amdgpu_arch)
80+
set(amdgpu_compile_flags ${ARGS_UNPARSED_ARGUMENTS})
81+
82+
if(ARGS_WITH_DEVICE_MATH_LIBS)
83+
find_package(AMDDeviceLibs QUIET HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
84+
if(AMDDeviceLibs_FOUND)
85+
get_target_property(ocml_path ocml IMPORTED_LOCATION)
86+
list(APPEND amdgpu_compile_flags "-Xclang" "-mlink-builtin-bitcode")
87+
list(APPEND amdgpu_compile_flags "-Xclang" "${ocml_path}")
88+
list(APPEND amdgpu_compile_flags "-DHIP_MATH_FOUND=1")
89+
endif()
90+
endif()
91+
6592
set(output_file "${CMAKE_CURRENT_BINARY_DIR}/${test_name}.amdgpu.bin")
6693
add_custom_command(
6794
OUTPUT ${output_file}
6895
COMMAND ${CMAKE_CXX_COMPILER}
6996
-I${OFFLOAD_UNITTESTS_DIR}
7097
--target=amdgcn-amd-amdhsa -mcpu=${amdgpu_arch}
71-
-nogpulib -flto ${ARGN} ${SRC_PATH} -o ${output_file}
98+
-nogpulib -flto ${amdgpu_compile_flags} ${SRC_PATH} -o ${output_file}
7299
DEPENDS ${SRC_PATH}
73100
)
74101
add_custom_target(${test_name}.amdgpu DEPENDS ${output_file})

offload/unittests/Conformance/device_code/CMakeLists.txt

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
1-
set(cuda_math_flags "")
2-
find_package(CUDAToolkit QUIET)
3-
if(CUDAToolkit_FOUND)
4-
file(GLOB libdevice_paths "${CUDAToolkit_LIBRARY_ROOT}/nvvm/libdevice/libdevice.*.bc")
5-
list(GET libdevice_paths 0 libdevice_path)
6-
7-
if (EXISTS ${libdevice_path})
8-
list(APPEND cuda_math_flags "-Xclang" "-mlink-builtin-bitcode" "-Xclang" "${libdevice_path}")
9-
list(APPEND cuda_math_flags "-DCUDA_MATH_FOUND=1")
10-
endif()
11-
endif()
12-
13-
set(hip_math_flags "")
14-
find_package(AMDDeviceLibs QUIET HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
15-
if(AMDDeviceLibs_FOUND)
16-
get_target_property(ocml_path ocml IMPORTED_LOCATION)
17-
list(APPEND hip_math_flags "-Xclang" "-mlink-builtin-bitcode" "-Xclang" "${ocml_path}")
18-
list(APPEND hip_math_flags "-DHIP_MATH_FOUND=1")
19-
endif()
20-
21-
add_offload_test_device_code(CUDAMath.cpp cuda-math -O3 -stdlib -fno-builtin ${cuda_math_flags})
22-
add_offload_test_device_code(HIPMath.cpp hip-math -O3 -stdlib -fno-builtin ${hip_math_flags})
1+
add_offload_test_device_code(CUDAMath.cpp cuda-math WITH_DEVICE_MATH_LIBS -O3 -stdlib -fno-builtin)
2+
add_offload_test_device_code(HIPMath.cpp hip-math WITH_DEVICE_MATH_LIBS -O3 -stdlib -fno-builtin)
233
add_offload_test_device_code(LLVMLibm.cpp llvm-libm -O3 -stdlib -fno-builtin)
244

255
add_custom_target(conformance_device_binaries DEPENDS

0 commit comments

Comments
 (0)