1 set(LIBOMPTARGET_BUILD_DEVICERTL_BCLIB TRUE CACHE BOOL
2 "Can be set to false to disable building this library.")
4 if (NOT LIBOMPTARGET_BUILD_DEVICERTL_BCLIB)
5 message(STATUS "Not building DeviceRTL: Disabled by LIBOMPTARGET_BUILD_DEVICERTL_BCLIB")
9 # Check to ensure the host system is a supported host architecture.
10 if(NOT ${CMAKE_SIZEOF_VOID_P} EQUAL "8")
11 message(STATUS "Not building DeviceRTL: Runtime does not support 32-bit hosts")
16 # Builds that use pre-installed LLVM have LLVM_DIR set.
17 # A standalone or LLVM_ENABLE_RUNTIMES=openmp build takes this route
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}")
26 message(STATUS "Building DeviceRTL. Using clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} and opt: ${OPT_TOOL}")
28 elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING AND NOT OPENMP_STANDALONE_BUILD)
29 # LLVM in-tree builds may use CMake target names to discover the tools.
30 # A LLVM_ENABLE_PROJECTS=openmp build takes this route
31 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")
37 message(STATUS "Not building DeviceRTL. No appropriate clang found")
41 set(devicertl_base_directory ${CMAKE_CURRENT_SOURCE_DIR})
42 set(include_directory ${devicertl_base_directory}/include)
43 set(source_directory ${devicertl_base_directory}/src)
45 set(all_amdgpu_architectures "gfx700;gfx701;gfx801;gfx803;gfx900;gfx902;gfx906"
46 "gfx908;gfx90a;gfx90c;gfx940;gfx941;gfx942;gfx950;gfx1010"
47 "gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035"
48 "gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150"
49 "gfx1151;gfx1152;gfx1153")
50 set(all_nvptx_architectures "sm_35;sm_37;sm_50;sm_52;sm_53;sm_60;sm_61;sm_62"
51 "sm_70;sm_72;sm_75;sm_80;sm_86;sm_87;sm_89;sm_90")
52 set(all_gpu_architectures
53 "${all_amdgpu_architectures};${all_nvptx_architectures}")
55 set(LIBOMPTARGET_DEVICE_ARCHITECTURES "all" CACHE STRING
56 "List of device architectures to be used to compile the OpenMP DeviceRTL.")
58 if(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "all")
59 set(LIBOMPTARGET_DEVICE_ARCHITECTURES ${all_gpu_architectures})
60 elseif(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "auto" OR
61 LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "native")
62 if(NOT LIBOMPTARGET_NVPTX_ARCH AND NOT LIBOMPTARGET_AMDGPU_ARCH)
64 "Could not find 'amdgpu-arch' and 'nvptx-arch' tools required for 'auto'")
65 elseif(NOT LIBOMPTARGET_FOUND_NVIDIA_GPU AND NOT LIBOMPTARGET_FOUND_AMDGPU_GPU)
66 message(FATAL_ERROR "No AMD or NVIDIA GPU found on the system when using 'auto'")
68 set(LIBOMPTARGET_DEVICE_ARCHITECTURES
69 "${LIBOMPTARGET_NVPTX_DETECTED_ARCH_LIST};${LIBOMPTARGET_AMDGPU_DETECTED_ARCH_LIST}")
71 list(REMOVE_DUPLICATES LIBOMPTARGET_DEVICE_ARCHITECTURES)
74 ${include_directory}/Allocator.h
75 ${include_directory}/Configuration.h
76 ${include_directory}/Debug.h
77 ${include_directory}/Interface.h
78 ${include_directory}/LibC.h
79 ${include_directory}/Mapping.h
80 ${include_directory}/Profiling.h
81 ${include_directory}/State.h
82 ${include_directory}/Synchronization.h
83 ${include_directory}/DeviceTypes.h
84 ${include_directory}/DeviceUtils.h
85 ${include_directory}/Workshare.h
89 ${source_directory}/Allocator.cpp
90 ${source_directory}/Configuration.cpp
91 ${source_directory}/Debug.cpp
92 ${source_directory}/Kernel.cpp
93 ${source_directory}/LibC.cpp
94 ${source_directory}/Mapping.cpp
95 ${source_directory}/Misc.cpp
96 ${source_directory}/Parallelism.cpp
97 ${source_directory}/Profiling.cpp
98 ${source_directory}/Reduction.cpp
99 ${source_directory}/State.cpp
100 ${source_directory}/Synchronization.cpp
101 ${source_directory}/Tasking.cpp
102 ${source_directory}/DeviceUtils.cpp
103 ${source_directory}/Workshare.cpp
106 # We disable the slp vectorizer during the runtime optimization to avoid
107 # vectorized accesses to the shared state. Generally, those are "good" but
108 # the optimizer pipeline (esp. Attributor) does not fully support vectorized
109 # instructions yet and we end up missing out on way more important constant
110 # propagation. That said, we will run the vectorizer again after the runtime
111 # has been linked into the user program.
112 set(clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512 -mllvm -vectorize-slp=false )
113 set(link_opt_flags -O3 -openmp-opt-disable -attributor-enable=module -vectorize-slp=false )
114 set(link_export_flag -passes=internalize -internalize-public-api-file=${source_directory}/exports)
116 # If the user built with the GPU C library enabled we will use that instead.
117 if(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
118 list(APPEND clang_opt_flags -DOMPTARGET_HAS_LIBC)
121 # Prepend -I to each list element
122 set (LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL "${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
123 list(TRANSFORM LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL PREPEND "-I")
125 # Set flags for LLVM Bitcode compilation.
126 set(bc_flags -c -foffload-lto -std=c++17 -fvisibility=hidden
127 ${clang_opt_flags} --offload-device-only
128 -nocudalib -nogpulib -nogpuinc -nostdlibinc
129 -fopenmp -fopenmp-cuda-mode
130 -Wno-unknown-cuda-version
131 -DOMPTARGET_DEVICE_RUNTIME
132 -I${include_directory}
133 -I${devicertl_base_directory}/../include
134 ${LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL}
137 # first create an object target
138 add_library(omptarget.devicertl.all_objs OBJECT IMPORTED)
139 function(compileDeviceRTLLibrary target_cpu target_name target_triple)
140 set(target_bc_flags ${ARGN})
143 foreach(src ${src_files})
144 get_filename_component(infile ${src} ABSOLUTE)
145 get_filename_component(outfile ${src} NAME)
146 set(outfile "${outfile}-${target_cpu}.bc")
147 set(depfile "${outfile}.d")
149 add_custom_command(OUTPUT ${outfile}
150 COMMAND ${CLANG_TOOL}
152 --offload-arch=${target_cpu}
155 ${infile} -o ${outfile}
158 COMMENT "Building LLVM bitcode ${outfile}"
162 # Add a file-level dependency to ensure that clang is up-to-date.
163 # By default, add_custom_command only builds clang if the
164 # executable is missing.
165 add_custom_command(OUTPUT ${outfile}
170 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile})
172 list(APPEND bc_files ${outfile})
175 set(bclib_name "libomptarget-${target_name}-${target_cpu}.bc")
177 # Link to a bitcode library.
178 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
180 -o ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name} ${bc_files}
182 COMMENT "Linking LLVM bitcode ${bclib_name}"
186 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
191 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
192 COMMAND ${OPT_TOOL} ${link_export_flag} ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
193 -o ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
194 DEPENDS ${source_directory}/exports ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
195 COMMENT "Internalizing LLVM bitcode ${bclib_name}"
198 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
203 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
204 COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
205 -o ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
206 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
207 COMMENT "Optimizing LLVM bitcode ${bclib_name}"
210 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
215 set(bclib_target_name "omptarget-${target_name}-${target_cpu}-bc")
216 add_custom_target(${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name})
218 # Copy library to destination.
219 add_custom_command(TARGET ${bclib_target_name} POST_BUILD
220 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
221 ${LIBOMPTARGET_LIBRARY_DIR})
222 add_dependencies(omptarget.devicertl.${target_name} ${bclib_target_name})
224 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name} ${LIBOMPTARGET_LIBRARY_DIR}/${bclib_name})
226 # Install bitcode library under the lib destination folder.
227 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} DESTINATION "${OFFLOAD_INSTALL_LIBDIR}")
229 set(target_feature "")
230 if("${target_triple}" STREQUAL "nvptx64-nvidia-cuda")
231 set(target_feature "feature=+ptx63")
234 # Package the bitcode in the bitcode and embed it in an ELF for the static library
235 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
236 COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
237 "--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},${target_feature},triple=${target_triple},arch=${target_cpu},kind=openmp"
238 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
239 COMMENT "Packaging LLVM offloading binary ${bclib_name}.out"
241 if(TARGET clang-offload-packager)
242 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
243 DEPENDS clang-offload-packager
247 set(output_name "${CMAKE_CURRENT_BINARY_DIR}/devicertl-${target_name}-${target_cpu}.o")
248 add_custom_command(OUTPUT ${output_name}
249 COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
250 -Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
252 ${source_directory}/Stub.cpp
253 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} ${source_directory}/Stub.cpp
254 COMMENT "Embedding LLVM offloading binary in devicertl-${target_name}-${target_cpu}.o"
258 add_custom_command(OUTPUT ${output_name}
263 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${output_name})
264 set_property(TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name})
266 if (CMAKE_EXPORT_COMPILE_COMMANDS)
267 set(ide_target_name omptarget-ide-${target_name}-${target_cpu})
268 add_library(${ide_target_name} STATIC EXCLUDE_FROM_ALL ${src_files})
269 target_compile_options(${ide_target_name} PRIVATE
270 -fopenmp --offload-arch=${target_cpu} -fopenmp-cuda-mode
271 -mllvm -openmp-opt-disable
272 -foffload-lto -fvisibility=hidden --offload-device-only
273 -nocudalib -nogpulib -nogpuinc -nostdlibinc -Wno-unknown-cuda-version
275 target_compile_definitions(${ide_target_name} PRIVATE SHARED_SCRATCHPAD_SIZE=512)
276 target_include_directories(${ide_target_name} PRIVATE
278 ${devicertl_base_directory}/../include
279 ${LIBOMPTARGET_LLVM_INCLUDE_DIRS}
281 install(TARGETS ${ide_target_name} EXCLUDE_FROM_ALL)
285 # Generate a Bitcode library for all the gpu architectures the user requested.
286 add_custom_target(omptarget.devicertl.nvptx)
287 add_custom_target(omptarget.devicertl.amdgpu)
288 foreach(gpu_arch ${LIBOMPTARGET_DEVICE_ARCHITECTURES})
289 if("${gpu_arch}" IN_LIST all_amdgpu_architectures)
290 compileDeviceRTLLibrary(${gpu_arch} amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
291 elseif("${gpu_arch}" IN_LIST all_nvptx_architectures)
292 compileDeviceRTLLibrary(${gpu_arch} nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
294 message(FATAL_ERROR "Unknown GPU architecture '${gpu_arch}'")
298 # Archive all the object files generated above into a static library
299 add_library(omptarget.devicertl STATIC)
300 set_target_properties(omptarget.devicertl PROPERTIES
301 ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}"
304 target_link_libraries(omptarget.devicertl PRIVATE omptarget.devicertl.all_objs)
306 install(TARGETS omptarget.devicertl ARCHIVE DESTINATION ${OFFLOAD_INSTALL_LIBDIR})