forked from ROCm/ROCm-Device-Libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCL.cmake
191 lines (166 loc) · 6.83 KB
/
OCL.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
##===--------------------------------------------------------------------------
## ROCm Device Libraries
##
## This file is distributed under the University of Illinois Open Source
## License. See LICENSE.TXT for details.
##===--------------------------------------------------------------------------
# Required because we need to generate response files on windows for long
# command-lines, but the only way to do this as part of the dependency graph is
# configure_file and we are included from multiple places. To get around this
# we `file(WRITE)` a file with an @variable reference and `configure_file` it.
cmake_policy(SET CMP0053 OLD)
if (WIN32)
set(EXE_SUFFIX ".exe")
else()
set(EXE_SUFFIX)
endif()
set(CLANG "${LLVM_TOOLS_BINARY_DIR}/clang${EXE_SUFFIX}")
set(LLVM_LINK "${LLVM_TOOLS_BINARY_DIR}/llvm-link${EXE_SUFFIX}")
set(LLVM_OBJDUMP "${LLVM_TOOLS_BINARY_DIR}/llvm-objdump${EXE_SUFFIX}")
set(LLVM_OPT "${LLVM_TOOLS_BINARY_DIR}/opt${EXE_SUFFIX}")
# -Wno-error=atomic-alignment was added to workaround build problems due to
# potential mis-aligned atomic ops detected by clang
set(CLANG_OCL_FLAGS -fcolor-diagnostics -Werror -Wno-error=atomic-alignment -x cl -Xclang
-cl-std=CL2.0 -target "${AMDGPU_TARGET_TRIPLE}" -fvisibility=protected -fomit-frame-pointer
-Xclang -finclude-default-header "${CLANG_OPTIONS_APPEND}")
set (BC_EXT .bc)
set (LIB_SUFFIX ".lib${BC_EXT}")
set (STRIP_SUFFIX ".strip${BC_EXT}")
set (FINAL_SUFFIX ".amdgcn${BC_EXT}")
# Set `inc_options` to contain Clang command-line for include directories for
# current source directory.
macro(set_inc_options)
get_property(inc_dirs
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
PROPERTY INCLUDE_DIRECTORIES)
set(inc_options)
foreach(inc_dir ${inc_dirs})
list(APPEND inc_options "-I${inc_dir}")
endforeach()
endmacro()
# called with NAME: library name
# SOURCES: .cl and .ll source files
# INTERNAL_LINK_LIBS: Extra .lls to be linked and internalized into final library
macro(opencl_bc_lib)
set(parse_options)
set(one_value_args NAME)
set(multi_value_args SOURCES INTERNAL_LINK_LIBS)
cmake_parse_arguments(OPENCL_BC_LIB "${parse_options}" "${one_value_args}"
"${multi_value_args}" ${ARGN})
set(name ${OPENCL_BC_LIB_NAME})
set(sources ${OPENCL_BC_LIB_SOURCES})
set(internal_link_libs ${OPENCL_BC_LIB_INTERNAL_LINK_LIBS})
get_target_property(irif_lib_output irif_lib OUTPUT_NAME)
set(OUT_NAME "${CMAKE_CURRENT_BINARY_DIR}/${name}")
set(LIB_TGT ${name}_lib)
set(clean_files)
list(APPEND AMDGCN_LIB_LIST ${LIB_TGT})
set(AMDGCN_LIB_LIST ${AMDGCN_LIB_LIST} PARENT_SCOPE)
list(APPEND AMDGCN_DEP_LIST ${LIB_TGT})
set(AMDGCN_DEP_LIST ${AMDGCN_DEP_LIST} PARENT_SCOPE)
set_inc_options()
set(deps)
foreach(file ${OPENCL_BC_LIB_SOURCES})
get_filename_component(fname_we "${file}" NAME_WE)
get_filename_component(fext "${file}" EXT)
if (fext STREQUAL ".cl")
set(output "${CMAKE_CURRENT_BINARY_DIR}/${fname_we}${BC_EXT}")
add_custom_command(OUTPUT "${output}"
COMMAND "${CLANG}" ${inc_options} ${CLANG_OCL_FLAGS}
-emit-llvm -Xclang -mlink-builtin-bitcode -Xclang "${irif_lib_output}"
-c "${file}" -o "${output}"
DEPENDS "${file}" "${irif_lib_output}" "${CLANG}")
list(APPEND deps "${output}")
list(APPEND clean_files "${output}")
endif()
if (fext STREQUAL ".ll")
list(APPEND deps "${file}")
endif()
endforeach()
# The llvm-link command-lines can get long enough to trigger strange behavior
# on Windows. LLVM tools support "response files" which can work around this:
# http://llvm.org/docs/CommandLine.html#response-files
set(RESPONSE_COMMAND_LINE)
foreach(dep ${deps})
set(RESPONSE_COMMAND_LINE "${RESPONSE_COMMAND_LINE} ${dep}")
endforeach()
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/response.in" "@RESPONSE_COMMAND_LINE@")
configure_file("${CMAKE_CURRENT_BINARY_DIR}/response.in"
"${OUT_NAME}_response" @ONLY)
add_custom_command(OUTPUT "${OUT_NAME}${FINAL_SUFFIX}"
# Link regular library dependencies
COMMAND "${LLVM_LINK}"
-o "${OUT_NAME}.link0${LIB_SUFFIX}" "@${OUT_NAME}_response"
# Extra link step with internalize
COMMAND "${LLVM_LINK}" -internalize -only-needed "${OUT_NAME}.link0${LIB_SUFFIX}"
-o "${OUT_NAME}${LIB_SUFFIX}" ${internal_link_libs}
COMMAND "${LLVM_OPT}" -strip
-o "${OUT_NAME}${STRIP_SUFFIX}" "${OUT_NAME}${LIB_SUFFIX}"
COMMAND "${PREPARE_BUILTINS}"
-o "${OUT_NAME}${FINAL_SUFFIX}" "${OUT_NAME}${STRIP_SUFFIX}"
DEPENDS "${deps}" "${OUT_NAME}_response" "${PREPARE_BUILTINS}" ${internal_link_libs})
add_custom_target("${LIB_TGT}" ALL
DEPENDS "${OUT_NAME}${FINAL_SUFFIX}"
SOURCES ${OPENCL_BC_LIB_SOURCES})
set_target_properties(${LIB_TGT} PROPERTIES
OUTPUT_NAME "${OUT_NAME}${FINAL_SUFFIX}"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
ARCHIVE_OUTPUT_NAME "${name}"
PREFIX "" SUFFIX ${FINAL_SUFFIX})
list(APPEND clean_files
"${OUT_NAME}${LIB_SUFFIX}" "${OUT_NAME}${STRIP_SUFFIX}")
if(NOT ROCM_DEVICELIB_STANDALONE_BUILD)
add_dependencies("${LIB_TGT}" llvm-link clang opt llvm-objdump)
endif()
if (TARGET prepare-builtins)
add_dependencies("${LIB_TGT}" prepare-builtins)
endif()
add_dependencies("${LIB_TGT}" irif_lib)
set_directory_properties(PROPERTIES
ADDITIONAL_MAKE_CLEAN_FILES "${clean_files}")
install(FILES "${OUT_NAME}${FINAL_SUFFIX}"
DESTINATION lib
COMPONENT device-libs)
endmacro()
function(clang_opencl_code name dir)
set(TEST_TGT "${name}_code")
set(OUT_NAME "${CMAKE_CURRENT_BINARY_DIR}/${name}")
set(mlink_flags)
foreach (lib ${ARGN})
get_target_property(lib_path "${lib}_lib" OUTPUT_NAME)
list(APPEND mlink_flags
-Xclang -mlink-bitcode-file
-Xclang "${lib_path}")
endforeach()
set_inc_options()
add_custom_command(OUTPUT "${OUT_NAME}.co"
COMMAND "${CLANG}" ${inc_options} ${CLANG_OCL_FLAGS}
-mcpu=fiji ${mlink_flags} -o "${OUT_NAME}.co" -c "${dir}/${name}.cl"
DEPENDS "${dir}/${name}.cl")
add_custom_target("${TEST_TGT}" ALL
DEPENDS "${OUT_NAME}.co"
SOURCES "${dir}/${name}.cl")
set_target_properties(${TEST_TGT} PROPERTIES
OUTPUT_NAME "${OUT_NAME}.co")
foreach (lib ${ARGN})
add_dependencies(${TEST_TGT} ${lib}_lib)
endforeach()
endfunction()
set(OCLC_DEFAULT_LIBS
oclc_correctly_rounded_sqrt_off
oclc_daz_opt_off
oclc_finite_only_off
oclc_isa_version_803
oclc_unsafe_math_off)
macro(clang_opencl_test name dir)
clang_opencl_code(${name} ${dir} hip opencl ocml ockl ${OCLC_DEFAULT_LIBS})
add_test(
NAME ${name}:llvm-objdump
COMMAND ${LLVM_OBJDUMP} -disassemble -mcpu=fiji "${name}.co"
)
endmacro()
macro(clang_opencl_test_file dir fname)
get_filename_component(name ${fname} NAME_WE)
get_filename_component(fdir ${fname} DIRECTORY)
clang_opencl_test(${name} ${dir}/${fdir})
endmacro()