@@ -16,23 +16,10 @@ if (LLVM_DIR)
16
16
# Builds that use pre-installed LLVM have LLVM_DIR set.
17
17
# A standalone or LLVM_ENABLE_RUNTIMES=openmp build takes this route
18
18
find_program (CLANG_TOOL clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
19
- find_program (PACKAGER_TOOL clang-offload-packager PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
20
- find_program (LINK_TOOL llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
21
- find_program (OPT_TOOL opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
22
- if ((NOT CLANG_TOOL ) OR (NOT LINK_TOOL ) OR (NOT OPT_TOOL ) OR (NOT PACKAGER_TOOL ))
23
- message (STATUS "Not building DeviceRTL. Missing clang: ${CLANG_TOOL} , llvm-link: ${LINK_TOOL} , opt: ${OPT_TOOL} , or clang-offload-packager: ${PACKAGER_TOOL} " )
24
- return ()
25
- else ()
26
- message (STATUS "Building DeviceRTL. Using clang: ${CLANG_TOOL} , llvm-link: ${LINK_TOOL} and opt: ${OPT_TOOL} " )
27
- endif ()
28
19
elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING AND NOT OPENMP_STANDALONE_BUILD )
29
20
# LLVM in-tree builds may use CMake target names to discover the tools.
30
21
# A LLVM_ENABLE_PROJECTS=openmp build takes this route
31
22
set (CLANG_TOOL $< TARGET_FILE:clang> )
32
- set (PACKAGER_TOOL $< TARGET_FILE:clang-offload-packager> )
33
- set (LINK_TOOL $< TARGET_FILE:llvm-link> )
34
- set (OPT_TOOL $< TARGET_FILE:opt> )
35
- message (STATUS "Building DeviceRTL. Using clang from in-tree build" )
36
23
else ()
37
24
message (STATUS "Not building DeviceRTL. No appropriate clang found" )
38
25
return ()
@@ -97,8 +84,6 @@ set(src_files
97
84
# propagation. That said, we will run the vectorizer again after the runtime
98
85
# has been linked into the user program.
99
86
set (clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512 -mllvm -vectorize-slp=false )
100
- set (link_opt_flags -O3 -openmp-opt-disable -attributor-enable=module -vectorize-slp=false )
101
- set (link_export_flag -passes=internalize -internalize-public-api-file=${source_directory}/exports )
102
87
103
88
# If the user built with the GPU C library enabled we will use that instead.
104
89
if (${LIBOMPTARGET_GPU_LIBC_SUPPORT} )
@@ -151,15 +136,14 @@ function(compileDeviceRTLLibrary target_name target_triple)
151
136
if (NOT _ocml_bc )
152
137
message (FATAL_ERROR "Could not find ocml.bc" )
153
138
endif ()
154
- list (APPEND bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ockl_bc} )
155
- list (APPEND bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ocml_bc} )
139
+ list (APPEND target_bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ockl_bc} )
140
+ list (APPEND target_bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ocml_bc} )
156
141
endif ()
157
142
158
- set (bc_files "" )
159
143
foreach (src ${src_files} )
160
144
get_filename_component (infile ${src} ABSOLUTE )
161
145
get_filename_component (outfile ${src} NAME )
162
- set (outfile "${outfile} -${target_name} .bc " )
146
+ set (outfile "${outfile} -${target_name} .o " )
163
147
set (depfile "${outfile} .d" )
164
148
165
149
# Passing an empty CPU to -march= suppressed target specific metadata.
@@ -186,99 +170,42 @@ function(compileDeviceRTLLibrary target_name target_triple)
186
170
endif ()
187
171
set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile} )
188
172
189
- list (APPEND bc_files ${outfile} )
173
+ list (APPEND obj_files ${CMAKE_CURRENT_BINARY_DIR} / ${outfile} )
190
174
endforeach ()
191
-
192
- set (bclib_name "libomptarget-${target_name} .bc" )
193
-
194
- # Link to a bitcode library.
195
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
196
- COMMAND ${LINK_TOOL}
197
- -o ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name} ${bc_files}
198
- DEPENDS ${bc_files}
199
- COMMENT "Linking LLVM bitcode ${bclib_name} "
200
- )
201
-
202
- if (TARGET llvm-link )
203
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
204
- DEPENDS llvm-link
205
- APPEND )
206
- endif ()
207
-
208
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
209
- COMMAND ${OPT_TOOL} ${link_export_flag} ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
210
- -o ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
211
- DEPENDS ${source_directory} /exports ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
212
- COMMENT "Internalizing LLVM bitcode ${bclib_name} "
213
- )
214
- if (TARGET opt )
215
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
216
- DEPENDS opt
217
- APPEND )
218
- endif ()
219
-
220
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
221
- COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
222
- -o ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
223
- DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
224
- COMMENT "Optimizing LLVM bitcode ${bclib_name} "
225
- )
226
- if (TARGET opt )
227
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
228
- DEPENDS opt
229
- APPEND )
230
- endif ()
231
-
232
- set (bclib_target_name "omptarget-${target_name} -bc" )
233
- add_custom_target (${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name} )
234
-
235
- # Copy library to destination.
236
- add_custom_command (TARGET ${bclib_target_name} POST_BUILD
237
- COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
238
- ${LIBOMPTARGET_LIBRARY_DIR} )
239
- add_dependencies (omptarget.devicertl.${target_name} ${bclib_target_name} )
240
-
241
- set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name} ${LIBOMPTARGET_LIBRARY_DIR} /${bclib_name} )
242
-
243
- # Install bitcode library under the lib destination folder.
244
- install (FILES ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name} DESTINATION "${OFFLOAD_INSTALL_LIBDIR} " )
245
-
246
- set (target_feature "" )
247
- if ("${target_triple} " STREQUAL "nvptx64-nvidia-cuda" )
248
- set (target_feature "feature=+ptx63" )
249
- endif ()
250
-
251
- # Package the bitcode in the bitcode and embed it in an ELF for the static library
252
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name}
253
- COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name}
254
- "--image=file=${CMAKE_CURRENT_BINARY_DIR} /${bclib_name} ,${target_feature} ,triple=${target_triple} ,arch=generic,kind=openmp"
255
- DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
256
- COMMENT "Packaging LLVM offloading binary ${bclib_name} .out"
257
- )
258
- if (TARGET clang-offload-packager )
259
- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name}
260
- DEPENDS clang-offload-packager
261
- APPEND )
262
- endif ()
263
-
264
- set (output_name "${CMAKE_CURRENT_BINARY_DIR} /devicertl-${target_name} .o" )
265
- add_custom_command (OUTPUT ${output_name}
266
- COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
267
- -Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
268
- -o ${output_name}
269
- ${source_directory} /Stub.cpp
270
- DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name} ${source_directory} /Stub.cpp
271
- COMMENT "Embedding LLVM offloading binary in devicertl-${target_name} .o"
272
- VERBATIM
175
+ # Trick to combine these into a bitcode file via the linker's LTO pass. This
176
+ # is used to provide the legacy `libomptarget-<name>.bc` files. Hack this
177
+ # through as an executable to get it to use the relocatable link.
178
+ add_executable (libomptarget-${target_name} )
179
+ target_sources (libomptarget-${target_name} PRIVATE ${obj_files} )
180
+ set_target_properties (libomptarget-${target_name} PROPERTIES
181
+ RUNTIME_OUTPUT_DIRECTORY ${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}
182
+ LINKER_LANGUAGE CXX
183
+ BUILD_RPATH ""
184
+ INSTALL_RPATH ""
185
+ RUNTIME_OUTPUT_NAME libomptarget-${target_name}.bc )
186
+ target_compile_options (libomptarget-${target_name} PRIVATE "--target=${target_triple} " )
187
+ target_link_options (libomptarget-${target_name} PRIVATE "--target=${target_triple} "
188
+ "-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm" )
189
+ install (TARGETS libomptarget-${target_name}
190
+ PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
191
+ DESTINATION ${OFFLOAD_INSTALL_LIBDIR} )
192
+
193
+ add_library (omptarget.${target_name}.all_objs OBJECT IMPORTED )
194
+ set_property (TARGET omptarget.${target_name}.all_objs APPEND PROPERTY IMPORTED_OBJECTS
195
+ ${LIBOMPTARGET_LLVM_LIBRARY_INTDIR} /libomptarget-${target_name}.bc )
196
+
197
+ # Archive all the object files generated above into a static library
198
+ add_library (omptarget.${target_name} STATIC )
199
+ set_target_properties (omptarget.${target_name} PROPERTIES
200
+ ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR} /${target_triple} "
201
+ ARCHIVE_OUTPUT_NAME ompdevice
202
+ LINKER_LANGUAGE CXX
273
203
)
274
- if (TARGET clang )
275
- add_custom_command (OUTPUT ${output_name}
276
- DEPENDS clang
277
- APPEND )
278
- endif ()
204
+ add_dependencies (omptarget.${target_name} libomptarget-${target_name} )
205
+ target_link_libraries (omptarget.${target_name} PRIVATE omptarget.${target_name}.all_objs )
279
206
280
- set_property ( DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${output_name} )
281
- set_property ( TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name} )
207
+ install ( TARGETS omptarget.${target_name}
208
+ ARCHIVE DESTINATION "lib ${LLVM_LIBDIR_SUFFIX} / ${target_triple} " )
282
209
283
210
if (CMAKE_EXPORT_COMPILE_COMMANDS )
284
211
set (ide_target_name omptarget-ide-${target_name} )
@@ -298,18 +225,10 @@ function(compileDeviceRTLLibrary target_name target_triple)
298
225
endif ()
299
226
endfunction ()
300
227
301
- add_custom_target (omptarget.devicertl.amdgpu )
302
- compileDeviceRTLLibrary (amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none )
303
-
304
- add_custom_target (omptarget.devicertl.nvptx )
305
- compileDeviceRTLLibrary (nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63 )
306
-
307
- # Archive all the object files generated above into a static library
308
- add_library (omptarget.devicertl STATIC )
309
- set_target_properties (omptarget.devicertl PROPERTIES
310
- ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR} "
311
- LINKER_LANGUAGE CXX
312
- )
313
- target_link_libraries (omptarget.devicertl PRIVATE omptarget.devicertl.all_objs )
228
+ if (NOT LLVM_TARGETS_TO_BUILD OR "AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD )
229
+ compileDeviceRTLLibrary (amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none )
230
+ endif ()
314
231
315
- install (TARGETS omptarget.devicertl ARCHIVE DESTINATION ${OFFLOAD_INSTALL_LIBDIR} )
232
+ if (NOT LLVM_TARGETS_TO_BUILD OR "NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD )
233
+ compileDeviceRTLLibrary (nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63 )
234
+ endif ()
0 commit comments