[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / openmp / libomptarget / DeviceRTL / CMakeLists.txt
blobdf8e4a5126fd44354844e0fcbeeb5093ec2ea64c
1 ##===----------------------------------------------------------------------===##
3 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 # See https://llvm.org/LICENSE.txt for license information.
5 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 ##===----------------------------------------------------------------------===##
9 # Build the DeviceRTL for all toolchains that are available
11 ##===----------------------------------------------------------------------===##
13 set(LIBOMPTARGET_BUILD_DEVICERTL_BCLIB TRUE CACHE BOOL
14   "Can be set to false to disable building this library.")
16 if (NOT LIBOMPTARGET_BUILD_DEVICERTL_BCLIB)
17   libomptarget_say("Not building DeviceRTL: Disabled by LIBOMPTARGET_BUILD_DEVICERTL_BCLIB")
18   return()
19 endif()
21 # Check to ensure the host system is a supported host architecture.
22 if(NOT ${CMAKE_SIZEOF_VOID_P} EQUAL "8")
23   libomptarget_say("Not building DeviceRTL: Runtime does not support 32-bit hosts")
24   return()
25 endif()
27 if (LLVM_DIR)
28   # Builds that use pre-installed LLVM have LLVM_DIR set.
29   # A standalone or LLVM_ENABLE_RUNTIMES=openmp build takes this route
30   find_program(CLANG_TOOL clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
31   find_program(PACKAGER_TOOL clang-offload-packager PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
32   find_program(LINK_TOOL llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
33   find_program(OPT_TOOL opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
34   if ((NOT CLANG_TOOL) OR (NOT LINK_TOOL) OR (NOT OPT_TOOL) OR (NOT PACKAGER_TOOL))
35     libomptarget_say("Not building DeviceRTL. Missing clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL}, opt: ${OPT_TOOL}, or clang-offload-packager: ${PACKAGER_TOOL}")
36     return()
37   else()
38     libomptarget_say("Building DeviceRTL. Using clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} and opt: ${OPT_TOOL}")
39   endif()
40 elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING AND NOT OPENMP_STANDALONE_BUILD)
41   # LLVM in-tree builds may use CMake target names to discover the tools.
42   # A LLVM_ENABLE_PROJECTS=openmp build takes this route
43   set(CLANG_TOOL $<TARGET_FILE:clang>)
44   set(PACKAGER_TOOL $<TARGET_FILE:clang-offload-packager>)
45   set(LINK_TOOL $<TARGET_FILE:llvm-link>)
46   set(OPT_TOOL $<TARGET_FILE:opt>)
47   libomptarget_say("Building DeviceRTL. Using clang from in-tree build")
48 else()
49   libomptarget_say("Not building DeviceRTL. No appropriate clang found")
50   return()
51 endif()
53 set(devicertl_base_directory ${CMAKE_CURRENT_SOURCE_DIR})
54 set(include_directory ${devicertl_base_directory}/include)
55 set(source_directory ${devicertl_base_directory}/src)
57 set(all_amdgpu_architectures "gfx700;gfx701;gfx801;gfx803;gfx900;gfx902;gfx906"
58                              "gfx908;gfx90a;gfx90c;gfx940;gfx941;gfx942;gfx1010"
59                              "gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035"
60                              "gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150"
61                              "gfx1151")
62 set(all_nvptx_architectures "sm_35;sm_37;sm_50;sm_52;sm_53;sm_60;sm_61;sm_62"
63                             "sm_70;sm_72;sm_75;sm_80;sm_86;sm_87;sm_89;sm_90")
64 set(all_gpu_architectures
65     "${all_amdgpu_architectures};${all_nvptx_architectures}")
67 set(LIBOMPTARGET_DEVICE_ARCHITECTURES "all" CACHE STRING
68     "List of device architectures to be used to compile the OpenMP DeviceRTL.")
70 if(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "all")
71   set(LIBOMPTARGET_DEVICE_ARCHITECTURES ${all_gpu_architectures})
72 elseif(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "auto" OR
73        LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "native")
74   if(NOT LIBOMPTARGET_NVPTX_ARCH AND NOT LIBOMPTARGET_AMDGPU_ARCH)
75     libomptarget_error_say(
76       "Could not find 'amdgpu-arch' and 'nvptx-arch' tools required for 'auto'")
77   elseif(NOT LIBOMPTARGET_FOUND_NVIDIA_GPU AND NOT LIBOMPTARGET_FOUND_AMDGPU_GPU)
78     libomptarget_error_say("No AMD or NVIDIA GPU found on the system when using 'auto'")
79   endif()
80   set(LIBOMPTARGET_DEVICE_ARCHITECTURES
81       "${LIBOMPTARGET_NVPTX_DETECTED_ARCH_LIST};${LIBOMPTARGET_AMDGPU_DETECTED_ARCH_LIST}")
82 endif()
83 list(REMOVE_DUPLICATES LIBOMPTARGET_DEVICE_ARCHITECTURES)
85 set(include_files
86   ${include_directory}/Allocator.h
87   ${include_directory}/Configuration.h
88   ${include_directory}/Debug.h
89   ${include_directory}/Interface.h
90   ${include_directory}/LibC.h
91   ${include_directory}/Mapping.h
92   ${include_directory}/State.h
93   ${include_directory}/Synchronization.h
94   ${include_directory}/Types.h
95   ${include_directory}/Utils.h
98 set(src_files
99   ${source_directory}/Allocator.cpp
100   ${source_directory}/Configuration.cpp
101   ${source_directory}/Debug.cpp
102   ${source_directory}/Kernel.cpp
103   ${source_directory}/LibC.cpp
104   ${source_directory}/Mapping.cpp
105   ${source_directory}/Misc.cpp
106   ${source_directory}/Parallelism.cpp
107   ${source_directory}/Reduction.cpp
108   ${source_directory}/State.cpp
109   ${source_directory}/Synchronization.cpp
110   ${source_directory}/Tasking.cpp
111   ${source_directory}/Utils.cpp
112   ${source_directory}/Workshare.cpp
115 # We disable the slp vectorizer during the runtime optimization to avoid
116 # vectorized accesses to the shared state. Generally, those are "good" but
117 # the optimizer pipeline (esp. Attributor) does not fully support vectorized
118 # instructions yet and we end up missing out on way more important constant
119 # propagation. That said, we will run the vectorizer again after the runtime 
120 # has been linked into the user program.
121 set(clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512 -mllvm -vectorize-slp=false )
122 set(link_opt_flags  -O3        -openmp-opt-disable -attributor-enable=module -vectorize-slp=false )
123 set(link_export_flag -passes=internalize -internalize-public-api-file=${source_directory}/exports)
125 # Prepend -I to each list element
126 set (LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL "${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
127 list(TRANSFORM LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL PREPEND "-I")
129 # Set flags for LLVM Bitcode compilation.
130 set(bc_flags -c -foffload-lto -std=c++17 -fvisibility=hidden
131               ${clang_opt_flags} --offload-device-only
132              -nocudalib -nogpulib -nostdinc
133              -fopenmp -fopenmp-cuda-mode
134              -Wno-unknown-cuda-version
135              -DOMPTARGET_DEVICE_RUNTIME
136              -I${include_directory}
137              -I${devicertl_base_directory}/../include
138              ${LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL}
141 # first create an object target
142 add_library(omptarget.devicertl.all_objs OBJECT IMPORTED)
143 function(compileDeviceRTLLibrary target_cpu target_name target_triple)
144   set(target_bc_flags ${ARGN})
146   set(bc_files "")
147   foreach(src ${src_files})
148     get_filename_component(infile ${src} ABSOLUTE)
149     get_filename_component(outfile ${src} NAME)
150     set(outfile "${outfile}-${target_cpu}.bc")
151     set(depfile "${outfile}.d")
153     add_custom_command(OUTPUT ${outfile}
154       COMMAND ${CLANG_TOOL}
155       ${bc_flags}
156       --offload-arch=${target_cpu}
157       ${target_bc_flags}
158       -MD -MF ${depfile}
159       ${infile} -o ${outfile}
160       DEPENDS ${infile}
161       DEPFILE ${depfile}
162       COMMENT "Building LLVM bitcode ${outfile}"
163       VERBATIM
164     )
165     if(TARGET clang)
166       # Add a file-level dependency to ensure that clang is up-to-date.
167       # By default, add_custom_command only builds clang if the
168       # executable is missing.
169       add_custom_command(OUTPUT ${outfile}
170         DEPENDS clang
171         APPEND
172       )
173     endif()
174     set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile})
176     list(APPEND bc_files ${outfile})
177   endforeach()
179   set(bclib_name "libomptarget-${target_name}-${target_cpu}.bc")
181   # Link to a bitcode library.
182   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
183       COMMAND ${LINK_TOOL}
184         -o ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name} ${bc_files}
185       DEPENDS ${bc_files}
186       COMMENT "Linking LLVM bitcode ${bclib_name}"
187   )
189   if(TARGET llvm-link)
190     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
191       DEPENDS llvm-link
192       APPEND)
193   endif()
195   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
196       COMMAND ${OPT_TOOL} ${link_export_flag} ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
197                       -o ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
198       DEPENDS ${source_directory}/exports ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
199       COMMENT "Internalizing LLVM bitcode ${bclib_name}"
200   )
201   if(TARGET opt)
202     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
203       DEPENDS opt
204       APPEND)
205   endif()
207   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
208       COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
209                       -o ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
210       DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
211       COMMENT "Optimizing LLVM bitcode ${bclib_name}"
212   )
213   if(TARGET opt)
214     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
215       DEPENDS opt
216       APPEND)
217   endif()
219   set(bclib_target_name "omptarget-${target_name}-${target_cpu}-bc")
220   add_custom_target(${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name})
222   # Copy library to destination.
223   add_custom_command(TARGET ${bclib_target_name} POST_BUILD
224                     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
225                     ${LIBOMPTARGET_LIBRARY_DIR})
226   add_dependencies(omptarget.devicertl.${target_name} ${bclib_target_name})
228   set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name} ${LIBOMPTARGET_LIBRARY_DIR}/${bclib_name})
230   # Install bitcode library under the lib destination folder.
231   install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} DESTINATION "${OPENMP_INSTALL_LIBDIR}")
233   set(target_feature "")
234   if("${target_triple}" STREQUAL "nvptx64-nvidia-cuda")
235     set(target_feature "feature=+ptx63")
236   endif()
238   # Package the bitcode in the bitcode and embed it in an ELF for the static library
239   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
240       COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
241         "--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},${target_feature},triple=${target_triple},arch=${target_cpu},kind=openmp"
242       DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
243       COMMENT "Packaging LLVM offloading binary ${bclib_name}.out"
244   )
245   if(TARGET clang-offload-packager)
246     add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
247       DEPENDS clang-offload-packager
248       APPEND)
249   endif()
251   set(output_name "${CMAKE_CURRENT_BINARY_DIR}/devicertl-${target_name}-${target_cpu}.o")
252   add_custom_command(OUTPUT ${output_name}
253     COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
254             -Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
255             -o ${output_name}
256             ${source_directory}/Stub.cpp
257     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} ${source_directory}/Stub.cpp
258     COMMENT "Embedding LLVM offloading binary in devicertl-${target_name}-${target_cpu}.o"
259     VERBATIM
260   )
261   if(TARGET clang)
262     add_custom_command(OUTPUT ${output_name}
263       DEPENDS clang
264       APPEND)
265   endif()
267   set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${output_name})
268   set_property(TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name})
270   if (CMAKE_EXPORT_COMPILE_COMMANDS)
271     set(ide_target_name omptarget-ide-${target_name}-${target_cpu})
272     add_library(${ide_target_name} STATIC EXCLUDE_FROM_ALL ${src_files})
273     target_compile_options(${ide_target_name} PRIVATE
274       -fopenmp --offload-arch=${target_cpu} -fopenmp-cuda-mode
275       -mllvm -openmp-opt-disable
276       -foffload-lto -fvisibility=hidden --offload-device-only
277       -nocudalib -nogpulib -nostdinc -Wno-unknown-cuda-version
278     )
279     target_compile_definitions(${ide_target_name} PRIVATE SHARED_SCRATCHPAD_SIZE=512)
280     target_include_directories(${ide_target_name} PRIVATE
281       ${include_directory}
282       ${devicertl_base_directory}/../include
283       ${LIBOMPTARGET_LLVM_INCLUDE_DIRS}
284     )
285     install(TARGETS ${ide_target_name} EXCLUDE_FROM_ALL)
286   endif()
287 endfunction()
289 # Generate a Bitcode library for all the gpu architectures the user requested.
290 add_custom_target(omptarget.devicertl.nvptx)
291 add_custom_target(omptarget.devicertl.amdgpu)
292 foreach(gpu_arch ${LIBOMPTARGET_DEVICE_ARCHITECTURES})
293   if("${gpu_arch}" IN_LIST all_amdgpu_architectures)
294     compileDeviceRTLLibrary(${gpu_arch} amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
295   elseif("${gpu_arch}" IN_LIST all_nvptx_architectures)
296     compileDeviceRTLLibrary(${gpu_arch} nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx61)
297   else()
298     libomptarget_error_say("Unknown GPU architecture '${gpu_arch}'")
299   endif()
300 endforeach()
302 # Archive all the object files generated above into a static library
303 add_library(omptarget.devicertl STATIC)
304 set_target_properties(omptarget.devicertl PROPERTIES LINKER_LANGUAGE CXX)
305 target_link_libraries(omptarget.devicertl PRIVATE omptarget.devicertl.all_objs)
307 install(TARGETS omptarget.devicertl ARCHIVE DESTINATION ${OPENMP_INSTALL_LIBDIR})