[clangd] Fix warnings
[llvm-project.git] / offload / DeviceRTL / CMakeLists.txt
blobb52305b9516fb1adbe5e92d65de63505716cdd41
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")
6   return()
7 endif()
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")
12   return()
13 endif()
15 if (LLVM_DIR)
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}")
24     return()
25   else()
26     message(STATUS "Building DeviceRTL. Using clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} and opt: ${OPT_TOOL}")
27   endif()
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")
36 else()
37   message(STATUS "Not building DeviceRTL. No appropriate clang found")
38   return()
39 endif()
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)
63     message(FATAL_ERROR
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'")
67   endif()
68   set(LIBOMPTARGET_DEVICE_ARCHITECTURES
69       "${LIBOMPTARGET_NVPTX_DETECTED_ARCH_LIST};${LIBOMPTARGET_AMDGPU_DETECTED_ARCH_LIST}")
70 endif()
71 list(REMOVE_DUPLICATES LIBOMPTARGET_DEVICE_ARCHITECTURES)
73 set(include_files
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
88 set(src_files
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)
119 endif()
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              -I${devicertl_base_directory}/../../libc
135              ${LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL}
138 # first create an object target
139 add_library(omptarget.devicertl.all_objs OBJECT IMPORTED)
140 function(compileDeviceRTLLibrary target_cpu target_name target_triple)
141   set(target_bc_flags ${ARGN})
143   set(bc_files "")
144   foreach(src ${src_files})
145     get_filename_component(infile ${src} ABSOLUTE)
146     get_filename_component(outfile ${src} NAME)
147     set(outfile "${outfile}-${target_cpu}.bc")
148     set(depfile "${outfile}.d")
150     add_custom_command(OUTPUT ${outfile}
151       COMMAND ${CLANG_TOOL}
152       ${bc_flags}
153       --offload-arch=${target_cpu}
154       ${target_bc_flags}
155       -MD -MF ${depfile}
156       ${infile} -o ${outfile}
157       DEPENDS ${infile}
158       DEPFILE ${depfile}
159       COMMENT "Building LLVM bitcode ${outfile}"
160       VERBATIM
161     )
162     if(TARGET clang)
163       # Add a file-level dependency to ensure that clang is up-to-date.
164       # By default, add_custom_command only builds clang if the
165       # executable is missing.
166       add_custom_command(OUTPUT ${outfile}
167         DEPENDS clang
168         APPEND
169       )
170     endif()
171     set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile})
173     list(APPEND bc_files ${outfile})
174   endforeach()
176   set(bclib_name "libomptarget-${target_name}-${target_cpu}.bc")
178   # Link to a bitcode library.
179   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
180       COMMAND ${LINK_TOOL}
181         -o ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name} ${bc_files}
182       DEPENDS ${bc_files}
183       COMMENT "Linking LLVM bitcode ${bclib_name}"
184   )
186   if(TARGET llvm-link)
187     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
188       DEPENDS llvm-link
189       APPEND)
190   endif()
192   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
193       COMMAND ${OPT_TOOL} ${link_export_flag} ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
194                       -o ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
195       DEPENDS ${source_directory}/exports ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
196       COMMENT "Internalizing LLVM bitcode ${bclib_name}"
197   )
198   if(TARGET opt)
199     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
200       DEPENDS opt
201       APPEND)
202   endif()
204   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
205       COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
206                       -o ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
207       DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
208       COMMENT "Optimizing LLVM bitcode ${bclib_name}"
209   )
210   if(TARGET opt)
211     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
212       DEPENDS opt
213       APPEND)
214   endif()
216   set(bclib_target_name "omptarget-${target_name}-${target_cpu}-bc")
217   add_custom_target(${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name})
219   # Copy library to destination.
220   add_custom_command(TARGET ${bclib_target_name} POST_BUILD
221                     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
222                     ${LIBOMPTARGET_LIBRARY_DIR})
223   add_dependencies(omptarget.devicertl.${target_name} ${bclib_target_name})
225   set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name} ${LIBOMPTARGET_LIBRARY_DIR}/${bclib_name})
227   # Install bitcode library under the lib destination folder.
228   install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} DESTINATION "${OFFLOAD_INSTALL_LIBDIR}")
230   set(target_feature "")
231   if("${target_triple}" STREQUAL "nvptx64-nvidia-cuda")
232     set(target_feature "feature=+ptx63")
233   endif()
235   # Package the bitcode in the bitcode and embed it in an ELF for the static library
236   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
237       COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
238         "--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},${target_feature},triple=${target_triple},arch=${target_cpu},kind=openmp"
239       DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
240       COMMENT "Packaging LLVM offloading binary ${bclib_name}.out"
241   )
242   if(TARGET clang-offload-packager)
243     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
244       DEPENDS clang-offload-packager
245       APPEND)
246   endif()
248   set(output_name "${CMAKE_CURRENT_BINARY_DIR}/devicertl-${target_name}-${target_cpu}.o")
249   add_custom_command(OUTPUT ${output_name}
250     COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
251             -Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
252             -o ${output_name}
253             ${source_directory}/Stub.cpp
254     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} ${source_directory}/Stub.cpp
255     COMMENT "Embedding LLVM offloading binary in devicertl-${target_name}-${target_cpu}.o"
256     VERBATIM
257   )
258   if(TARGET clang)
259     add_custom_command(OUTPUT ${output_name}
260       DEPENDS clang
261       APPEND)
262   endif()
264   set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${output_name})
265   set_property(TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name})
267   if (CMAKE_EXPORT_COMPILE_COMMANDS)
268     set(ide_target_name omptarget-ide-${target_name}-${target_cpu})
269     add_library(${ide_target_name} STATIC EXCLUDE_FROM_ALL ${src_files})
270     target_compile_options(${ide_target_name} PRIVATE
271       -fopenmp --offload-arch=${target_cpu} -fopenmp-cuda-mode
272       -mllvm -openmp-opt-disable
273       -foffload-lto -fvisibility=hidden --offload-device-only
274       -nocudalib -nogpulib -nogpuinc -nostdlibinc -Wno-unknown-cuda-version
275     )
276     target_compile_definitions(${ide_target_name} PRIVATE SHARED_SCRATCHPAD_SIZE=512)
277     target_include_directories(${ide_target_name} PRIVATE
278       ${include_directory}
279       ${devicertl_base_directory}/../../libc
280       ${devicertl_base_directory}/../include
281       ${LIBOMPTARGET_LLVM_INCLUDE_DIRS}
282     )
283     install(TARGETS ${ide_target_name} EXCLUDE_FROM_ALL)
284   endif()
285 endfunction()
287 # Generate a Bitcode library for all the gpu architectures the user requested.
288 add_custom_target(omptarget.devicertl.nvptx)
289 add_custom_target(omptarget.devicertl.amdgpu)
290 foreach(gpu_arch ${LIBOMPTARGET_DEVICE_ARCHITECTURES})
291   if("${gpu_arch}" IN_LIST all_amdgpu_architectures)
292     compileDeviceRTLLibrary(${gpu_arch} amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
293   elseif("${gpu_arch}" IN_LIST all_nvptx_architectures)
294     compileDeviceRTLLibrary(${gpu_arch} nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
295   else()
296     message(FATAL_ERROR "Unknown GPU architecture '${gpu_arch}'")
297   endif()
298 endforeach()
300 # Archive all the object files generated above into a static library
301 add_library(omptarget.devicertl STATIC)
302 set_target_properties(omptarget.devicertl PROPERTIES
303   ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}"
304   LINKER_LANGUAGE CXX
306 target_link_libraries(omptarget.devicertl PRIVATE omptarget.devicertl.all_objs)
308 install(TARGETS omptarget.devicertl ARCHIVE DESTINATION ${OFFLOAD_INSTALL_LIBDIR})