@@ -16,23 +16,10 @@ if (LLVM_DIR)
1616 # Builds that use pre-installed LLVM have LLVM_DIR set.
1717 # A standalone or LLVM_ENABLE_RUNTIMES=openmp build takes this route
1818 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 ()
2819elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING AND NOT OPENMP_STANDALONE_BUILD)
2920 # LLVM in-tree builds may use CMake target names to discover the tools.
3021 # A LLVM_ENABLE_PROJECTS=openmp build takes this route
3122 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" )
3623else ()
3724 message (STATUS "Not building DeviceRTL. No appropriate clang found" )
3825 return ()
@@ -82,8 +69,6 @@ set(src_files
8269# propagation. That said, we will run the vectorizer again after the runtime
8370# has been linked into the user program.
8471set (clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512 -mllvm -vectorize-slp=false )
85- set (link_opt_flags -O3 -openmp-opt-disable -attributor-enable=module -vectorize-slp=false )
86- set (link_export_flag -passes=internalize -internalize-public -api-file=${source_directory} /exports)
8772
8873# If the user built with the GPU C library enabled we will use that instead.
8974if (${LIBOMPTARGET_GPU_LIBC_SUPPORT} )
@@ -107,15 +92,15 @@ set(bc_flags -c -flto -std=c++17 -fvisibility=hidden
10792)
10893
10994# first create an object target
110- add_library (omptarget.devicertl.all_objs OBJECT IMPORTED )
11195function (compileDeviceRTLLibrary target_name target_triple)
11296 set (target_bc_flags ${ARGN} )
11397
11498 set (bc_files "" )
99+ add_library (omp.${target_name} .all_objs OBJECT IMPORTED )
115100 foreach (src ${src_files} )
116101 get_filename_component (infile ${src} ABSOLUTE )
117102 get_filename_component (outfile ${src} NAME )
118- set (outfile "${outfile} -${target_name} .bc " )
103+ set (outfile "${outfile} -${target_name} .o " )
119104 set (depfile "${outfile} .d" )
120105
121106 # Passing an empty CPU to -march= suppressed target specific metadata.
@@ -142,99 +127,36 @@ function(compileDeviceRTLLibrary target_name target_triple)
142127 endif ()
143128 set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile} )
144129
145- list (APPEND bc_files ${outfile} )
130+ list (APPEND obj_files ${CMAKE_CURRENT_BINARY_DIR} / ${outfile} )
146131 endforeach ()
147-
148- set (bclib_name "libomptarget-${target_name} .bc" )
149-
150- # Link to a bitcode library.
151- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
152- COMMAND ${LINK_TOOL}
153- -o ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name} ${bc_files}
154- DEPENDS ${bc_files}
155- COMMENT "Linking LLVM bitcode ${bclib_name} "
156- )
157-
158- if (TARGET llvm-link)
159- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
160- DEPENDS llvm-link
161- APPEND )
162- endif ()
163-
164- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
165- COMMAND ${OPT_TOOL} ${link_export_flag} ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
166- -o ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
167- DEPENDS ${source_directory} /exports ${CMAKE_CURRENT_BINARY_DIR} /linked_${bclib_name}
168- COMMENT "Internalizing LLVM bitcode ${bclib_name} "
132+ set_property (TARGET omp.${target_name} .all_objs
133+ APPEND PROPERTY IMPORTED_OBJECTS ${obj_files} )
134+
135+ # Archive all the object files generated above into a static library
136+ add_library (omp.${target_name} STATIC )
137+ set_target_properties (omp.${target_name} PROPERTIES
138+ ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR} /${target_triple} "
139+ ARCHIVE_OUTPUT_NAME omp
140+ LINKER_LANGUAGE CXX
169141 )
170- if (TARGET opt)
171- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
172- DEPENDS opt
173- APPEND )
174- endif ()
175-
176- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
177- COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
178- -o ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
179- DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /internalized_${bclib_name}
180- COMMENT "Optimizing LLVM bitcode ${bclib_name} "
181- )
182- if (TARGET opt)
183- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
184- DEPENDS opt
185- APPEND )
186- endif ()
187-
188- set (bclib_target_name "omptarget-${target_name} -bc" )
189- add_custom_target (${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name} )
190-
191- # Copy library to destination.
192- add_custom_command (TARGET ${bclib_target_name} POST_BUILD
193- COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
194- ${LIBOMPTARGET_LIBRARY_DIR} )
195- add_dependencies (omptarget.devicertl.${target_name} ${bclib_target_name} )
196-
197- set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name} ${LIBOMPTARGET_LIBRARY_DIR} /${bclib_name} )
198-
199- # Install bitcode library under the lib destination folder.
200- install (FILES ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name} DESTINATION "${OFFLOAD_INSTALL_LIBDIR} " )
201-
202- set (target_feature "" )
203- if ("${target_triple} " STREQUAL "nvptx64-nvidia-cuda" )
204- set (target_feature "feature=+ptx63" )
205- endif ()
206-
207- # Package the bitcode in the bitcode and embed it in an ELF for the static library
208- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name}
209- COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name}
210- "--image=file=${CMAKE_CURRENT_BINARY_DIR} /${bclib_name} ,${target_feature} ,triple=${target_triple} ,arch=generic,kind=openmp"
211- DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /${bclib_name}
212- COMMENT "Packaging LLVM offloading binary ${bclib_name} .out"
213- )
214- if (TARGET clang-offload-packager)
215- add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name}
216- DEPENDS clang-offload-packager
217- APPEND )
218- endif ()
219-
220- set (output_name "${CMAKE_CURRENT_BINARY_DIR} /devicertl-${target_name} .o" )
221- add_custom_command (OUTPUT ${output_name}
222- COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
223- -Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name}
224- -o ${output_name}
225- ${source_directory} /Stub.cpp
226- DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /packaged_${bclib_name} ${source_directory} /Stub.cpp
227- COMMENT "Embedding LLVM offloading binary in devicertl-${target_name} .o"
228- VERBATIM
229- )
230- if (TARGET clang)
231- add_custom_command (OUTPUT ${output_name}
232- DEPENDS clang
233- APPEND )
234- endif ()
235-
236- set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${output_name} )
237- set_property (TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name} )
142+ target_link_libraries (omp.${target_name} PRIVATE omp.${target_name} .all_objs)
143+
144+ install (TARGETS omp.${target_name}
145+ ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX} /${target_triple} " )
146+
147+ # Trick to combine these into a bitcode file via the linker's LTO pass. This
148+ # is used to provide the legacy `libomptarget-<name>.bc` files.
149+ add_executable (libomptarget-${target_name} ${obj_files} )
150+ set_target_properties (libomptarget-${target_name} PROPERTIES
151+ RUNTIME_OUTPUT_DIRECTORY ${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}
152+ LINKER_LANGUAGE CXX
153+ RUNTIME_OUTPUT_NAME libomptarget-${target_name} .bc)
154+ target_compile_options (libomptarget-${target_name} PRIVATE "--target=${target_triple} " )
155+ target_link_options (libomptarget-${target_name} PRIVATE "--target=${target_triple} "
156+ "-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm" )
157+ install (TARGETS libomptarget-${target_name}
158+ PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
159+ DESTINATION ${OFFLOAD_INSTALL_LIBDIR} )
238160
239161 if (CMAKE_EXPORT_COMPILE_COMMANDS)
240162 set (ide_target_name omptarget-ide-${target_name} )
@@ -254,18 +176,6 @@ function(compileDeviceRTLLibrary target_name target_triple)
254176 endif ()
255177endfunction ()
256178
257- add_custom_target (omptarget.devicertl.amdgpu)
258179compileDeviceRTLLibrary(amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version =none)
259180
260- add_custom_target (omptarget.devicertl.nvptx)
261181compileDeviceRTLLibrary(nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
262-
263- # Archive all the object files generated above into a static library
264- add_library (omptarget.devicertl STATIC )
265- set_target_properties (omptarget.devicertl PROPERTIES
266- ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR} "
267- LINKER_LANGUAGE CXX
268- )
269- target_link_libraries (omptarget.devicertl PRIVATE omptarget.devicertl.all_objs)
270-
271- install (TARGETS omptarget.devicertl ARCHIVE DESTINATION ${OFFLOAD_INSTALL_LIBDIR} )
0 commit comments