1 ##===----------------------------------------------------------------------===##
3 # The LLVM Compiler Infrastructure
5 # This file is dual licensed under the MIT and the University of Illinois Open
6 # Source Licenses. See LICENSE.txt for details.
8 ##===----------------------------------------------------------------------===##
10 # Build a plugin for an AMDGPU machine if available.
12 ##===----------------------------------------------------------------------===##
14 ################################################################################
15 set(LIBOMPTARGET_BUILD_AMDGPU_PLUGIN TRUE CACHE BOOL
16 "Whether to build AMDGPU plugin")
17 if (NOT LIBOMPTARGET_BUILD_AMDGPU_PLUGIN)
18 libomptarget_say("Not building AMDGPU NextGen offloading plugin: LIBOMPTARGET_BUILD_AMDGPU_PLUGIN is false")
22 # as of rocm-3.7, hsa is installed with cmake packages and kmt is found via hsa
23 find_package(hsa-runtime64 QUIET 1.2.0 HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
25 if(NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(ppc64le)|(aarch64)$" AND CMAKE_SYSTEM_NAME MATCHES "Linux"))
26 libomptarget_say("Not building AMDGPU NextGen plugin: only support AMDGPU in Linux x86_64, ppc64le, or aarch64 hosts")
30 ################################################################################
31 # Define the suffix for the runtime messaging dumps.
32 add_definitions(-DTARGET_NAME=AMDGPU)
34 # Define debug prefix. TODO: This should be automatized in the Debug.h but it
35 # requires changing the original plugins.
36 add_definitions(-DDEBUG_PREFIX="TARGET AMDGPU RTL")
38 if(CMAKE_SYSTEM_PROCESSOR MATCHES "(ppc64le)|(aarch64)$")
39 add_definitions(-DLITTLEENDIAN_CPU=1)
42 if(CMAKE_BUILD_TYPE MATCHES Debug)
43 add_definitions(-DDEBUG)
46 set(LIBOMPTARGET_DLOPEN_LIBHSA OFF)
47 option(LIBOMPTARGET_FORCE_DLOPEN_LIBHSA "Build with dlopened libhsa" ${LIBOMPTARGET_DLOPEN_LIBHSA})
49 if (${hsa-runtime64_FOUND} AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
50 libomptarget_say("Building AMDGPU NextGen plugin linked against libhsa")
51 set(LIBOMPTARGET_EXTRA_SOURCE)
52 set(LIBOMPTARGET_DEP_LIBRARIES hsa-runtime64::hsa-runtime64)
54 libomptarget_say("Building AMDGPU NextGen plugin for dlopened libhsa")
55 include_directories(dynamic_hsa)
56 set(LIBOMPTARGET_EXTRA_SOURCE dynamic_hsa/hsa.cpp)
57 set(LIBOMPTARGET_DEP_LIBRARIES)
60 if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
61 # On FreeBSD, the 'environ' symbol is undefined at link time, but resolved by
62 # the dynamic linker at runtime. Therefore, allow the symbol to be undefined
63 # when creating a shared library.
64 set(LDFLAGS_UNDEFINED "-Wl,--allow-shlib-undefined")
66 set(LDFLAGS_UNDEFINED "-Wl,-z,defs")
69 add_llvm_library(omptarget.rtl.amdgpu SHARED
71 ${LIBOMPTARGET_EXTRA_SOURCE}
73 ADDITIONAL_HEADER_DIRS
74 ${LIBOMPTARGET_INCLUDE_DIR}
75 ${CMAKE_CURRENT_SOURCE_DIR}/utils
86 ${LIBOMPTARGET_DEP_LIBRARIES}
93 if ((OMPT_TARGET_DEFAULT) AND (LIBOMPTARGET_OMPT_SUPPORT))
94 target_link_libraries(omptarget.rtl.amdgpu PRIVATE OMPT)
97 if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
98 target_link_libraries(omptarget.rtl.amdgpu PRIVATE
99 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exports")
102 target_include_directories(
105 ${LIBOMPTARGET_INCLUDE_DIR}
106 ${CMAKE_CURRENT_SOURCE_DIR}/utils
109 # Configure testing for the AMDGPU plugin. We will build tests if we could a
110 # functional AMD GPU on the system, or if manually specifies by the user.
111 option(LIBOMPTARGET_FORCE_AMDGPU_TESTS "Build AMDGPU libomptarget tests" OFF)
112 if (LIBOMPTARGET_FOUND_AMDGPU_GPU OR LIBOMPTARGET_FORCE_AMDGPU_TESTS)
113 # Report to the parent scope that we are building a plugin for amdgpu
114 set(LIBOMPTARGET_SYSTEM_TARGETS
115 "${LIBOMPTARGET_SYSTEM_TARGETS} amdgcn-amd-amdhsa" PARENT_SCOPE)
116 list(APPEND LIBOMPTARGET_TESTED_PLUGINS "omptarget.rtl.amdgpu")
117 set(LIBOMPTARGET_TESTED_PLUGINS "${LIBOMPTARGET_TESTED_PLUGINS}" PARENT_SCOPE)
119 libomptarget_say("Not generating AMDGPU tests, no supported devices detected."
120 " Use 'LIBOMPTARGET_FORCE_AMDGPU_TESTS' to override.")
124 # Install plugin under the lib destination folder.
125 install(TARGETS omptarget.rtl.amdgpu LIBRARY DESTINATION "${OPENMP_INSTALL_LIBDIR}")
126 set_target_properties(omptarget.rtl.amdgpu PROPERTIES
127 INSTALL_RPATH "$ORIGIN" BUILD_RPATH "$ORIGIN:${CMAKE_CURRENT_BINARY_DIR}/.."
128 CXX_VISIBILITY_PRESET protected)