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 AMDGCN Device RTL bitcode library using clang -ffreestanding
11 ##===----------------------------------------------------------------------===##
13 set(LIBOMPTARGET_BUILD_AMDGCN_BCLIB TRUE CACHE BOOL
14 "Can be set to false to disable building this library.")
16 if (NOT LIBOMPTARGET_BUILD_AMDGCN_BCLIB)
17 libomptarget_say("Not building AMDGCN device RTL: Disabled by LIBOMPTARGET_BUILD_AMDGCN_BCLIB")
21 if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
22 libomptarget_say("Not building AMDGCN device RTL: Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS")
27 # Copied from nvptx CMakeLists
28 if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
29 set(aux_triple x86_64-unknown-linux-gnu)
30 elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "ppc64le")
31 set(aux_triple powerpc64le-unknown-linux-gnu)
32 elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
33 set(aux_triple aarch64-unknown-linux-gnu)
35 libomptarget_say("Not building AMDGCN device RTL: unknown host arch: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
40 # Builds that use pre-installed LLVM have LLVM_DIR set.
41 find_program(CLANG_TOOL clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
42 find_program(LINK_TOOL llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
43 find_program(OPT_TOOL opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
44 if ((NOT CLANG_TOOL) OR (NOT LINK_TOOL) OR (NOT OPT_TOOL))
45 libomptarget_say("Not building AMDGCN device RTL. Missing clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} or opt: ${OPT_TOOL}")
48 libomptarget_say("Building AMDGCN device RTL. Using clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} and opt: ${OPT_TOOL}")
50 elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING AND NOT OPENMP_STANDALONE_BUILD)
51 # LLVM in-tree builds may use CMake target names to discover the tools.
52 set(CLANG_TOOL $<TARGET_FILE:clang>)
53 set(LINK_TOOL $<TARGET_FILE:llvm-link>)
54 set(OPT_TOOL $<TARGET_FILE:opt>)
55 libomptarget_say("Building AMDGCN device RTL. Using clang from in-tree build")
57 libomptarget_say("Not building AMDGCN device RTL. No appropriate clang found")
61 project(omptarget-amdgcn)
63 add_custom_target(omptarget-amdgcn ALL)
66 set(optimization_level 2)
68 # Activate RTL message dumps if requested by the user.
69 if(LIBOMPTARGET_NVPTX_DEBUG)
70 set(CUDA_DEBUG -DOMPTARGET_NVPTX_DEBUG=-1 -g)
73 get_filename_component(devicertl_base_directory
74 ${CMAKE_CURRENT_SOURCE_DIR}
78 ${CMAKE_CURRENT_SOURCE_DIR}/src/amdgcn_smid.hip
79 ${CMAKE_CURRENT_SOURCE_DIR}/src/amdgcn_locks.hip
80 ${CMAKE_CURRENT_SOURCE_DIR}/src/target_impl.hip
81 ${devicertl_base_directory}/common/src/cancel.cu
82 ${devicertl_base_directory}/common/src/critical.cu
83 ${devicertl_base_directory}/common/src/data_sharing.cu
84 ${devicertl_base_directory}/common/src/libcall.cu
85 ${devicertl_base_directory}/common/src/loop.cu
86 ${devicertl_base_directory}/common/src/omp_data.cu
87 ${devicertl_base_directory}/common/src/omptarget.cu
88 ${devicertl_base_directory}/common/src/parallel.cu
89 ${devicertl_base_directory}/common/src/reduction.cu
90 ${devicertl_base_directory}/common/src/support.cu
91 ${devicertl_base_directory}/common/src/shuffle.cpp
92 ${devicertl_base_directory}/common/src/sync.cu
93 ${devicertl_base_directory}/common/src/task.cu)
96 ${CMAKE_CURRENT_SOURCE_DIR}/src/amdgcn_interface.h
97 ${CMAKE_CURRENT_SOURCE_DIR}/src/target_impl.h
98 ${devicertl_base_directory}/common/debug.h
99 ${devicertl_base_directory}/common/omptarget.h
100 ${devicertl_base_directory}/common/omptargeti.h
101 ${devicertl_base_directory}/common/state-queue.h
102 ${devicertl_base_directory}/common/state-queuei.h
103 ${devicertl_base_directory}/common/support.h)
105 # for both in-tree and out-of-tree build
106 if (NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
107 set(OUTPUTDIR ${CMAKE_CURRENT_BINARY_DIR})
109 set(OUTPUTDIR ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
112 # create gfx bitcode libraries
113 set(mcpus gfx700 gfx701 gfx801 gfx803 gfx900 gfx902 gfx906 gfx908 gfx90a gfx1010 gfx1030 gfx1031)
114 if (DEFINED LIBOMPTARGET_AMDGCN_GFXLIST)
115 set(mcpus ${LIBOMPTARGET_AMDGCN_GFXLIST})
118 # Prepend -I to each list element
119 set (LIBOMPTARGET_LLVM_INCLUDE_DIRS_AMDGCN "${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
120 list(TRANSFORM LIBOMPTARGET_LLVM_INCLUDE_DIRS_AMDGCN PREPEND "-I")
122 macro(add_cuda_bc_library)
123 set(cu_cmd ${CLANG_TOOL}
126 -mllvm -openmp-opt-disable
129 -target amdgcn-amd-amdhsa
131 -Xclang -aux-triple -Xclang ${aux_triple}
132 -fopenmp -fopenmp-cuda-mode -Xclang -fopenmp-is-device
134 -Xclang -target-cpu -Xclang ${mcpu}
138 -O${optimization_level}
140 -I${CMAKE_CURRENT_SOURCE_DIR}/src
141 -I${devicertl_base_directory}/common/include
142 -I${devicertl_base_directory}
143 -I${devicertl_base_directory}/../include
144 ${LIBOMPTARGET_LLVM_INCLUDE_DIRS_AMDGCN})
148 foreach(file ${ARGN})
149 get_filename_component(fname ${file} NAME_WE)
150 set(bc1_filename ${fname}.${mcpu}.bc)
153 OUTPUT ${bc1_filename}
154 COMMAND ${cu_cmd} ${file} -o ${bc1_filename}
155 DEPENDS ${file} ${h_files})
157 list(APPEND bc1_files ${bc1_filename})
161 OUTPUT linkout.cuda.${mcpu}.bc
162 COMMAND ${LINK_TOOL} ${bc1_files} -o linkout.cuda.${mcpu}.bc
163 DEPENDS ${bc1_files})
165 list(APPEND bc_files linkout.cuda.${mcpu}.bc)
168 set(libname "omptarget-amdgcn")
170 set(toolchain_deps "")
172 list(APPEND toolchain_deps llvm-link)
175 list(APPEND toolchain_deps opt)
178 foreach(mcpu ${mcpus})
180 add_cuda_bc_library(${cuda_sources})
182 set(bc_libname lib${libname}-${mcpu}.bc)
185 COMMAND ${LINK_TOOL} ${bc_files} | ${OPT_TOOL} --always-inline -o ${OUTPUTDIR}/${bc_libname}
186 DEPENDS ${bc_files} ${toolchain_deps})
188 add_custom_target(lib${libname}-${mcpu} ALL DEPENDS ${bc_libname})
190 install(FILES ${OUTPUTDIR}/${bc_libname}
191 DESTINATION "${OPENMP_INSTALL_LIBDIR}"