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")
21 if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
22 libomptarget_say("Not building DeviceRTL: Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS")
27 # Builds that use pre-installed LLVM have LLVM_DIR set.
28 find_program(CLANG_TOOL clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
29 find_program(LINK_TOOL llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR}
31 find_program(OPT_TOOL opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
32 if ((NOT CLANG_TOOL) OR (NOT LINK_TOOL) OR (NOT OPT_TOOL))
33 libomptarget_say("Not building DeviceRTL. Missing clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} or opt: ${OPT_TOOL}")
36 libomptarget_say("Building DeviceRTL. Using clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} and opt: ${OPT_TOOL}")
38 elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING AND NOT OPENMP_STANDALONE_BUILD)
39 # LLVM in-tree builds may use CMake target names to discover the tools.
40 set(CLANG_TOOL $<TARGET_FILE:clang>)
41 set(LINK_TOOL $<TARGET_FILE:llvm-link>)
42 set(OPT_TOOL $<TARGET_FILE:opt>)
43 libomptarget_say("Building DeviceRTL. Using clang from in-tree build")
45 libomptarget_say("Not building DeviceRTL. No appropriate clang found")
49 # TODO: This part needs to be refined when libomptarget is going to support
51 # TODO: This part can also be removed if we can change the clang driver to make
52 # it support device only compilation.
53 if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
54 set(aux_triple x86_64-unknown-linux-gnu)
55 elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "ppc64le")
56 set(aux_triple powerpc64le-unknown-linux-gnu)
57 elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
58 set(aux_triple aarch64-unknown-linux-gnu)
60 libomptarget_say("Not building DeviceRTL: unknown host arch: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
64 set(devicertl_base_directory ${CMAKE_CURRENT_SOURCE_DIR})
65 set(include_directory ${devicertl_base_directory}/include)
66 set(source_directory ${devicertl_base_directory}/src)
68 set(all_capabilities 35 37 50 52 53 60 61 62 70 72 75 80 86)
70 set(LIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES ${all_capabilities} CACHE STRING
71 "List of CUDA Compute Capabilities to be used to compile the NVPTX DeviceRTL.")
72 string(TOLOWER ${LIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES} LIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES)
74 if (LIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES STREQUAL "all")
75 set(nvptx_sm_list ${all_capabilities})
76 elseif(LIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES STREQUAL "auto")
77 if (NOT LIBOMPTARGET_DEP_CUDA_FOUND)
78 libomptarget_error_say("[NVPTX] Cannot auto detect compute capability as CUDA not found.")
80 set(nvptx_sm_list ${LIBOMPTARGET_DEP_CUDA_ARCH})
82 string(REPLACE "," ";" nvptx_sm_list "${LIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES}")
85 # If user set LIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES to empty, we disable the
87 if (NOT nvptx_sm_list)
88 libomptarget_say("Not building CUDA offloading DeviceRTL: empty compute capability list")
93 foreach(sm ${nvptx_sm_list})
94 if (NOT ${sm} IN_LIST all_capabilities)
95 libomptarget_warning_say("[NVPTX] Compute capability ${sm} is not supported. Make sure clang can work with it.")
99 set(amdgpu_mcpus gfx700 gfx701 gfx801 gfx803 gfx900 gfx902 gfx906 gfx908 gfx90a gfx1010 gfx1030 gfx1031)
100 if (DEFINED LIBOMPTARGET_AMDGCN_GFXLIST)
101 set(amdgpu_mcpus ${LIBOMPTARGET_AMDGCN_GFXLIST})
105 # Activate RTL message dumps if requested by the user.
106 set(LIBOMPTARGET_DEVICE_DEBUG FALSE CACHE BOOL
107 "Activate DeviceRTL debug messages.")
111 ${source_directory}/Configuration.cpp
112 ${source_directory}/Debug.cpp
113 ${source_directory}/Kernel.cpp
114 ${source_directory}/Mapping.cpp
115 ${source_directory}/Misc.cpp
116 ${source_directory}/Parallelism.cpp
117 ${source_directory}/Reduction.cpp
118 ${source_directory}/State.cpp
119 ${source_directory}/Synchronization.cpp
120 ${source_directory}/Tasking.cpp
121 ${source_directory}/Utils.cpp
122 ${source_directory}/Workshare.cpp
125 set(clang_opt_flags -O1 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=2048)
126 set(link_opt_flags -O1 -openmp-opt-disable)
128 # Prepend -I to each list element
129 set (LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL "${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
130 list(TRANSFORM LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL PREPEND "-I")
132 # Set flags for LLVM Bitcode compilation.
133 set(bc_flags -S -x c++ -std=c++17
135 -Xclang -emit-llvm-bc
136 -Xclang -aux-triple -Xclang ${aux_triple}
137 -fopenmp -fopenmp-cuda-mode -Xclang -fopenmp-is-device
138 -I${include_directory}
139 -I${devicertl_base_directory}/../include
140 ${LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL}
143 if(${LIBOMPTARGET_DEVICE_DEBUG})
144 list(APPEND bc_flags -DOMPTARGET_DEBUG=-1)
146 list(APPEND bc_flags -DOMPTARGET_DEBUG=0)
149 function(compileDeviceRTLLibrary target_cpu target_name)
150 set(target_bc_flags ${ARGN})
153 foreach(src ${src_files})
154 get_filename_component(infile ${src} ABSOLUTE)
155 get_filename_component(outfile ${src} NAME)
156 set(outfile "${outfile}-${target_cpu}.bc")
158 add_custom_command(OUTPUT ${outfile}
159 COMMAND ${CLANG_TOOL}
161 -Xclang -target-cpu -Xclang ${target_cpu}
163 ${infile} -o ${outfile}
165 IMPLICIT_DEPENDS CXX ${infile}
166 COMMENT "Building LLVM bitcode ${outfile}"
169 if("${CLANG_TOOL}" STREQUAL "$<TARGET_FILE:clang>")
170 # Add a file-level dependency to ensure that clang is up-to-date.
171 # By default, add_custom_command only builds clang if the
172 # executable is missing.
173 add_custom_command(OUTPUT ${outfile}
178 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile})
180 list(APPEND bc_files ${outfile})
183 set(bclib_name "libomptarget-new-${target_name}-${target_cpu}.bc")
185 # Link to a bitcode library.
186 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
188 -o ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name} ${bc_files}
190 COMMENT "Linking LLVM bitcode ${bclib_name}"
193 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
194 COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
195 -o ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
196 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
197 COMMENT "Optimizing LLVM bitcode ${bclib_name}"
200 # Add a file-level dependency to ensure that llvm-link and opt are up-to-date.
201 # By default, add_custom_command only builds the tool if the executable is missing
202 if("${LINK_TOOL}" STREQUAL "$<TARGET_FILE:llvm-link>")
203 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
207 if("${OPT_TOOL}" STREQUAL "$<TARGET_FILE:opt>")
208 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
213 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name})
215 set(bclib_target_name "omptarget-new-${target_name}-${target_cpu}-bc")
217 add_custom_target(${bclib_target_name} ALL 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})
224 # Install bitcode library under the lib destination folder.
225 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} DESTINATION "${OPENMP_INSTALL_LIBDIR}")
228 # Generate a Bitcode library for all the compute capabilities the user requested
229 foreach(sm ${nvptx_sm_list})
230 compileDeviceRTLLibrary(sm_${sm} nvptx -target nvptx64 -Xclang -target-feature -Xclang +ptx61 "-D__CUDA_ARCH__=${sm}0")
233 foreach(mcpu ${amdgpu_mcpus})
234 compileDeviceRTLLibrary(${mcpu} amdgpu -target amdgcn-amd-amdhsa -D__AMDGCN__ -fvisibility=default -nogpulib)