Skip to content

Commit 29ce59f

Browse files
authored
Implement missing host-side math functions (#884)
* add tests for host library, bit 128 * Implement missing host math funcs * Compiling the bitode library to host code leads to linking errors some of the OpenCL natives being used in the library code are not available for the CPU, resulting in linking errors * OCML does not meant to be compiled in C++ mode for this, we create a drop-in opencl header defining the missing datatypes * Since OCML uses headers in the same directory as the source files, we must copy the necessary source files into a new directory so that our replacement headers get picked up instead of the originals * Implement a new header for mapping math funcs to host-side ocml * Discover an additional way to trigger 128 bit error * Discover a discrepancy in host and device func results * add missing rsqrt host funcs * fix typo
1 parent 712c4bb commit 29ce59f

19 files changed

Lines changed: 1444 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,9 @@ set(HIP_OFFLOAD_COMPILE_OPTIONS_BUILD_
512512
# combined compile and link or when the --hip-link is
513513
# present at the link step.
514514
list(APPEND HIP_OFFLOAD_LINK_OPTIONS_INSTALL_
515-
"-L${LIB_INSTALL_DIR}" "-lCHIP" "-no-hip-rt")
515+
"-L${LIB_INSTALL_DIR}" "-lCHIP" "-no-hip-rt -locml_host_math_funcs")
516516
list(APPEND HIP_OFFLOAD_LINK_OPTIONS_BUILD_
517-
"-L${CMAKE_BINARY_DIR}" "-lCHIP" "-no-hip-rt")
517+
"-L${CMAKE_BINARY_DIR}" "-lCHIP" "-no-hip-rt -locml_host_math_funcs")
518518

519519
if(OpenCL_LIBRARY)
520520
target_link_options(CHIP PUBLIC -Wl,-rpath,${OpenCL_DIR})
@@ -807,4 +807,7 @@ if(LevelZero_LIBRARY)
807807
endif()
808808

809809
# Include docker targets
810-
include(cmake/docker.cmake)
810+
include(cmake/docker.cmake)
811+
812+
add_subdirectory(host_math_funcs)
813+
target_link_libraries(CHIP PUBLIC ocml_host_math_funcs)

host_math_funcs/CMakeLists.txt

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
message(STATUS "Building host math functions")
2+
set(ROCm-Device-Libs_SRC_DIR ${CMAKE_SOURCE_DIR}/bitcode/ROCm-Device-Libs)
3+
set(OCML_SRC_DIR ${ROCm-Device-Libs_SRC_DIR}/ocml/src)
4+
set(OCML_INCLUDE_DIR ${ROCm-Device-Libs_SRC_DIR}/ocml/include)
5+
set(OCML_LIB_DIR ${ROCm-Device-Libs_SRC_DIR}/ocml/lib)
6+
7+
# Add include directories for OCML
8+
include_directories(
9+
${OCML_INCLUDE_DIR}
10+
${ROCm-Device-Libs_SRC_DIR}/irif/inc
11+
${ROCm-Device-Libs_SRC_DIR}/oclc/inc
12+
${ROCm-Device-Libs_SRC_DIR}/ocml/inc
13+
${CMAKE_CURRENT_SOURCE_DIR}
14+
)
15+
16+
17+
# Create host_math_funcs directory in build
18+
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/host_math_funcs)
19+
20+
# Copy ncdfF.cl to the build directory
21+
22+
# cospi OpenCL in chipStar (extern)
23+
# cospif OpenCL in chipStar (calling cospi)
24+
file(COPY ${OCML_SRC_DIR}/trigredF.h DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
25+
file(COPY ${OCML_SRC_DIR}/sincospiredF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
26+
file(COPY ${OCML_SRC_DIR}/tanpiredF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
27+
file(COPY ${OCML_SRC_DIR}/trigpiredF.h DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
28+
file(COPY ${OCML_SRC_DIR}/trigpiredF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
29+
file(COPY ${OCML_SRC_DIR}/cospiF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
30+
31+
file(COPY ${OCML_SRC_DIR}/trigpiredD.h DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
32+
file(COPY ${OCML_SRC_DIR}/sincospiredD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
33+
file(COPY ${OCML_SRC_DIR}/tanpiredD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
34+
file(COPY ${OCML_SRC_DIR}/trigpiredD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
35+
file(COPY ${OCML_SRC_DIR}/cospiD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
36+
37+
file(COPY ${OCML_SRC_DIR}/erfcD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
38+
file(COPY ${OCML_SRC_DIR}/erfcF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
39+
40+
file(COPY ${OCML_SRC_DIR}/expD_base.h DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
41+
file(COPY ${OCML_SRC_DIR}/expF_base.h DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
42+
file(COPY ${OCML_SRC_DIR}/logD_base.h DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
43+
file(COPY ${OCML_SRC_DIR}/logF_base.h DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
44+
file(COPY ${OCML_SRC_DIR}/logD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
45+
file(COPY ${OCML_SRC_DIR}/logF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
46+
47+
48+
file(COPY ${OCML_SRC_DIR}/expD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
49+
file(COPY ${OCML_SRC_DIR}/expF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
50+
51+
# erfcinv OCML in chipStar (__ocml_erfcinv_f64)
52+
file(COPY ${OCML_SRC_DIR}/erfcinvD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
53+
54+
# # erfcinvf OCML in chipStar (__ocml_erfcinv_f32)
55+
file(COPY ${OCML_SRC_DIR}/erfcinvF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
56+
57+
# # erfcx OCML in chipStar (__ocml_erfcx_f64)
58+
file(COPY ${OCML_SRC_DIR}/erfcxD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
59+
60+
# # erfcxf OCML in chipStar (__ocml_erfcx_f32)
61+
file(COPY ${OCML_SRC_DIR}/erfcxF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
62+
63+
# # erfinv OCML in chipStar (__ocml_erfinv_f64)
64+
file(COPY ${OCML_SRC_DIR}/erfinvD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
65+
66+
# # erfinvf OCML in chipStar (__ocml_erfinv_f32)
67+
file(COPY ${OCML_SRC_DIR}/erfinvF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
68+
69+
# # normcdf OCML in chipStar (__ocml_ncdf_f64)
70+
file(COPY ${OCML_SRC_DIR}/ncdfD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
71+
72+
# # normcdff OCML in chipStar (__ocml_ncdf_f32)
73+
file(COPY ${OCML_SRC_DIR}/ncdfF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
74+
75+
# # normcdfinv OCML in chipStar (__ocml_ncdfinv_f64)
76+
file(COPY ${OCML_SRC_DIR}/ncdfinvD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
77+
78+
# # normcdfinvf OCML in chipStar (__ocml_ncdfinv_f32)
79+
file(COPY ${OCML_SRC_DIR}/ncdfinvF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
80+
81+
# # rcbrt OCML in chipStar (__ocml_rcbrt_f64)
82+
file(COPY ${OCML_SRC_DIR}/rcbrtD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
83+
84+
# # rcbrtf OCML in chipStar (__ocml_rcbrt_f32)
85+
file(COPY ${OCML_SRC_DIR}/rcbrtF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
86+
87+
# # sincospi OCML in chipStar (__ocml_sincospi_f64)
88+
file(COPY ${OCML_SRC_DIR}/sincospiD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
89+
90+
# # sincospif OCML in chipStar (__ocml_sincospi_f32)
91+
file(COPY ${OCML_SRC_DIR}/sincospiF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
92+
93+
# # sinpi OpenCL in chipStar (extern)
94+
file(COPY ${OCML_SRC_DIR}/sinpiD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
95+
96+
# # sinpif OpenCL in chipStar (call sinpi)
97+
file(COPY ${OCML_SRC_DIR}/sinpiF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
98+
99+
# rsqrt OCML in chipStar (__ocml_rsqrt_f64)
100+
file(COPY ${OCML_SRC_DIR}/rsqrtD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
101+
102+
# rsqrtf OCML in chipStar (__ocml_rsqrt_f32)
103+
file(COPY ${OCML_SRC_DIR}/rsqrtF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
104+
105+
# llmax calling max() in chipStar
106+
# llmin calling min() in chipStar
107+
# ullmax calling max() in chipStar
108+
# ullmin calling min() in chipStar
109+
# umax calling max() in chipStar
110+
# umin calling min() in chipStar
111+
112+
# signbit OCML in chipStar (__ocml_signbit_f64)
113+
file(COPY ${OCML_SRC_DIR}/signbitD.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
114+
file(COPY ${OCML_SRC_DIR}/signbitF.cl DESTINATION ${CMAKE_BINARY_DIR}/host_math_funcs)
115+
116+
117+
# Gather all .cl sources from the OCML source directory
118+
file(GLOB OCML_CL_SOURCES ${CMAKE_BINARY_DIR}/host_math_funcs/*.cl)
119+
120+
# Print the list of gathered .cl sources for debugging
121+
message(STATUS "OCML CL Sources: ${OCML_CL_SOURCES}")
122+
123+
# Set the language for the target
124+
set_source_files_properties(${OCML_CL_SOURCES} PROPERTIES LANGUAGE C)
125+
126+
# Add the library target
127+
add_library(ocml_host_math_funcs STATIC ${OCML_CL_SOURCES})
128+
set_target_properties(ocml_host_math_funcs PROPERTIES
129+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
130+
)
131+
# Set the linker language explicitly
132+
set_target_properties(ocml_host_math_funcs PROPERTIES LINKER_LANGUAGE C)
133+
134+
# Add ocml_host_math_funcs to the export set
135+
install(TARGETS ocml_host_math_funcs EXPORT hip-targets)
136+
install(TARGETS ocml_host_math_funcs EXPORT CHIPTargets)

0 commit comments

Comments
 (0)