388 lines · plain
1# See for https://openmp.llvm.org/SupportAndFAQ.html for instructions on how2# to build offload with CMake.3 4cmake_minimum_required(VERSION 3.20.0)5set(LLVM_SUBPROJECT_TITLE "liboffload")6 7# Permit redefining OPENMP_STANDALONE_BUILD when doing a runtimes build.8if (OPENMP_STANDALONE_BUILD OR "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")9 set(OPENMP_STANDALONE_BUILD TRUE)10 project(offload C CXX ASM)11else()12 set(OPENMP_STANDALONE_BUILD FALSE)13endif()14 15# Check that the library can actually be built.16if(APPLE OR WIN32 OR WASM)17 message(WARNING "libomptarget cannot be built on Windows and MacOS X!")18 return()19elseif(NOT "cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)20 message(WARNING "Host compiler must support C++17 to build libomptarget!")21 return()22elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)23 message(WARNING "libomptarget on 32-bit systems is not supported!")24 return()25endif()26 27set(OFFLOAD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})28 29if(OPENMP_STANDALONE_BUILD)30 set(OFFLOAD_LIBDIR_SUFFIX "" CACHE STRING31 "Suffix of lib installation directory, e.g. 64 => lib64")32 set(OFFLOAD_INSTALL_LIBDIR "lib${OFFLOAD_LIBDIR_SUFFIX}" CACHE STRING33 "Path where built offload libraries should be installed.")34else()35 # When building in tree we install the runtime according to the LLVM settings.36 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)37 set(OFFLOAD_INSTALL_LIBDIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE STRING38 "Path where built offload libraries should be installed.")39 else()40 set(OFFLOAD_INSTALL_LIBDIR "lib${LLVM_LIBDIR_SUFFIX}" CACHE STRING41 "Path where built offload libraries should be installed.")42 endif()43endif()44 45set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)46 47option(OFFLOAD_INCLUDE_TESTS "Generate and build offload tests." ${LLVM_INCLUDE_TESTS})48 49# Add path for custom modules50list(INSERT CMAKE_MODULE_PATH 051 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"52 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"53 "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"54 "${LLVM_COMMON_CMAKE_UTILS}"55 "${LLVM_COMMON_CMAKE_UTILS}/Modules"56 )57 58if (OPENMP_STANDALONE_BUILD)59 # CMAKE_BUILD_TYPE was not set, default to Release.60 if (NOT CMAKE_BUILD_TYPE)61 set(CMAKE_BUILD_TYPE Release)62 endif()63 64 # Group common settings.65 set(OPENMP_ENABLE_WERROR FALSE CACHE BOOL66 "Enable -Werror flags to turn warnings into errors for supporting compilers.")67 set(OPENMP_LIBDIR_SUFFIX "" CACHE STRING68 "Suffix of lib installation directory, e.g. 64 => lib64")69 # Do not use OPENMP_LIBDIR_SUFFIX directly, use OPENMP_INSTALL_LIBDIR.70 set(OPENMP_INSTALL_LIBDIR "lib${OPENMP_LIBDIR_SUFFIX}")71 72 # Used by llvm_add_tool() and tests.73 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR})74 75 # Group test settings.76 set(OPENMP_TEST_C_COMPILER ${CMAKE_C_COMPILER} CACHE STRING77 "C compiler to use for testing OpenMP runtime libraries.")78 set(OPENMP_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE STRING79 "C++ compiler to use for testing OpenMP runtime libraries.")80 set(OPENMP_TEST_Fortran_COMPILER ${CMAKE_Fortran_COMPILER} CACHE STRING81 "FORTRAN compiler to use for testing OpenMP runtime libraries.")82 set(OPENMP_LLVM_TOOLS_DIR "" CACHE PATH "Path to LLVM tools for testing.")83 84 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")85 set(CMAKE_CXX_STANDARD_REQUIRED NO)86 set(CMAKE_CXX_EXTENSIONS NO)87else()88 set(OPENMP_ENABLE_WERROR ${LLVM_ENABLE_WERROR})89 # If building in tree, we honor the same install suffix LLVM uses.90 set(OPENMP_INSTALL_LIBDIR "lib${LLVM_LIBDIR_SUFFIX}")91 92 if (NOT MSVC)93 set(OPENMP_TEST_C_COMPILER ${LLVM_TOOLS_BINARY_DIR}/clang)94 set(OPENMP_TEST_CXX_COMPILER ${LLVM_TOOLS_BINARY_DIR}/clang++)95 else()96 set(OPENMP_TEST_C_COMPILER ${LLVM_TOOLS_BINARY_DIR}/clang.exe)97 set(OPENMP_TEST_CXX_COMPILER ${LLVM_TOOLS_BINARY_DIR}/clang++.exe)98 endif()99 100 # Check for flang101 if (NOT MSVC)102 set(OPENMP_TEST_Fortran_COMPILER ${LLVM_TOOLS_BINARY_DIR}/flang)103 else()104 set(OPENMP_TEST_Fortran_COMPILER ${LLVM_TOOLS_BINARY_DIR}/flang.exe)105 endif()106 107 # Set fortran test compiler if flang is found108 if (EXISTS "${OPENMP_TEST_Fortran_COMPILER}")109 message("Using local flang build at ${OPENMP_TEST_Fortran_COMPILER}")110 else()111 unset(OPENMP_TEST_Fortran_COMPILER)112 endif()113 114 # If not standalone, set CMAKE_CXX_STANDARD but don't set the global cache value,115 # only set it locally for OpenMP.116 set(CMAKE_CXX_STANDARD 17)117 set(CMAKE_CXX_STANDARD_REQUIRED NO)118 set(CMAKE_CXX_EXTENSIONS NO)119endif()120 121# Set the path of all resulting libraries to a unified location so that it can122# be used for testing.123set(LIBOMPTARGET_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR})124set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBOMPTARGET_LIBRARY_DIR})125set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBOMPTARGET_LIBRARY_DIR})126set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBOMPTARGET_LIBRARY_DIR})127 128if(NOT LLVM_LIBRARY_OUTPUT_INTDIR)129 set(LIBOMPTARGET_INTDIR ${LIBOMPTARGET_LIBRARY_DIR})130else()131 set(LIBOMPTARGET_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})132endif()133 134# Get dependencies for the different components of the project.135include(LibomptargetGetDependencies)136 137# Set up testing infrastructure.138include(OpenMPTesting)139 140include(CheckCXXCompilerFlag)141check_cxx_compiler_flag(-Werror=global-constructors OFFLOAD_HAVE_WERROR_CTOR)142 143# LLVM source tree is required at build time for libomptarget144if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)145 message(FATAL_ERROR "Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS")146endif()147 148if(DEFINED LIBOMPTARGET_BUILD_CUDA_PLUGIN OR149 DEFINED LIBOMPTARGET_BUILD_AMDGPU_PLUGIN)150 message(WARNING "Option removed, use 'LIBOMPTARGET_PLUGINS_TO_BUILD' instead")151endif()152 153set(LIBOMPTARGET_ALL_PLUGIN_TARGETS amdgpu cuda host)154set(LIBOMPTARGET_PLUGINS_TO_BUILD "all" CACHE STRING155 "Semicolon-separated list of plugins to use: cuda, amdgpu, host or \"all\".")156 157if(LIBOMPTARGET_PLUGINS_TO_BUILD STREQUAL "all")158 set(LIBOMPTARGET_PLUGINS_TO_BUILD ${LIBOMPTARGET_ALL_PLUGIN_TARGETS})159endif()160 161if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux" AND162 "host" IN_LIST LIBOMPTARGET_PLUGINS_TO_BUILD)163 message(STATUS "Not building host plugin: only Linux systems are supported")164 list(REMOVE_ITEM LIBOMPTARGET_PLUGINS_TO_BUILD "host")165endif()166if(NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(ppc64le)|(aarch64)$"167 AND CMAKE_SYSTEM_NAME MATCHES "Linux"))168 if("amdgpu" IN_LIST LIBOMPTARGET_PLUGINS_TO_BUILD)169 message(STATUS "Not building AMDGPU plugin: only support AMDGPU in "170 "Linux x86_64, ppc64le, or aarch64 hosts")171 list(REMOVE_ITEM LIBOMPTARGET_PLUGINS_TO_BUILD "amdgpu")172 endif()173 if("cuda" IN_LIST LIBOMPTARGET_PLUGINS_TO_BUILD)174 message(STATUS "Not building CUDA plugin: only support CUDA in "175 "Linux x86_64, ppc64le, or aarch64 hosts")176 list(REMOVE_ITEM LIBOMPTARGET_PLUGINS_TO_BUILD "cuda")177 endif()178endif()179message(STATUS "Building the offload library with support for "180 "the \"${LIBOMPTARGET_PLUGINS_TO_BUILD}\" plugins")181 182set(LIBOMPTARGET_DLOPEN_PLUGINS "${LIBOMPTARGET_PLUGINS_TO_BUILD}" CACHE STRING183 "Semicolon-separated list of plugins to use 'dlopen' for runtime linking")184 185set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS "")186foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)187 set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS188 "${LIBOMPTARGET_ENUM_PLUGIN_TARGETS}PLUGIN_TARGET(${plugin})\n")189endforeach()190string(STRIP ${LIBOMPTARGET_ENUM_PLUGIN_TARGETS} LIBOMPTARGET_ENUM_PLUGIN_TARGETS)191configure_file(192 ${CMAKE_CURRENT_SOURCE_DIR}/include/Shared/Targets.def.in193 ${CMAKE_CURRENT_BINARY_DIR}/include/Shared/Targets.def194)195 196include_directories(${LIBOMPTARGET_LLVM_INCLUDE_DIRS})197 198# This is a list of all the targets that are supported/tested right now.199set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} aarch64-unknown-linux-gnu")200set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} aarch64-unknown-linux-gnu-LTO")201set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} amdgcn-amd-amdhsa")202set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64le-ibm-linux-gnu")203set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64le-ibm-linux-gnu-LTO")204set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64-ibm-linux-gnu")205set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64-ibm-linux-gnu-LTO")206set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} x86_64-unknown-linux-gnu")207set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} x86_64-unknown-linux-gnu-LTO")208set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} nvptx64-nvidia-cuda")209set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} nvptx64-nvidia-cuda-LTO")210set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} nvptx64-nvidia-cuda-JIT-LTO")211set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} s390x-ibm-linux-gnu")212set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} s390x-ibm-linux-gnu-LTO")213set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} riscv64-unknown-linux-gnu")214set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} riscv64-unknown-linux-gnu-LTO")215set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} loongarch64-unknown-linux-gnu")216set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} loongarch64-unknown-linux-gnu-LTO")217 218# Once the plugins for the different targets are validated, they will be added to219# the list of supported targets in the current system.220set (LIBOMPTARGET_SYSTEM_TARGETS "")221set (LIBOMPTARGET_TESTED_PLUGINS "")222 223# Check whether using debug mode. In debug mode, allow dumping progress224# messages at runtime by default. Otherwise, it can be enabled225# independently using the LIBOMPTARGET_ENABLE_DEBUG option.226string( TOLOWER "${CMAKE_BUILD_TYPE}" LIBOMPTARGET_CMAKE_BUILD_TYPE)227if(LIBOMPTARGET_CMAKE_BUILD_TYPE MATCHES debug)228 option(LIBOMPTARGET_ENABLE_DEBUG "Allow debug output with the environment variable LIBOMPTARGET_DEBUG=1" ON)229else()230 option(LIBOMPTARGET_ENABLE_DEBUG "Allow debug output with the environment variable LIBOMPTARGET_DEBUG=1" OFF)231endif()232if(LIBOMPTARGET_ENABLE_DEBUG)233 add_definitions(-DOMPTARGET_DEBUG)234endif()235 236# No exceptions and no RTTI, except if requested.237set(offload_compile_flags -fno-exceptions)238if(NOT LLVM_ENABLE_RTTI)239 set(offload_compile_flags ${offload_compile_flags} -fno-rtti)240endif()241if(OFFLOAD_HAVE_WERROR_CTOR)242 list(APPEND offload_compile_flags -Werror=global-constructors)243endif()244 245# TODO: Consider enabling LTO by default if supported.246# https://cmake.org/cmake/help/latest/module/CheckIPOSupported.html can be used247# to test for working LTO. However, before CMake 3.24 this will test the248# default linker and ignore options such as LLVM_ENABLE_LLD. As a result, CMake249# would test whether LTO works with the default linker but build with another one.250# In a typical scenario, libomptarget is compiled with the in-tree Clang, but251# linked with ld.gold, which requires the LLVMgold plugin, when it actually252# would work with the lld linker (or also fail because the system lld is too old253# to understand opaque pointers). Using gcc as the compiler would pass the test, but fail254# when linking with lld since does not understand gcc's LTO format.255set(LIBOMPTARGET_USE_LTO FALSE CACHE BOOL "Use LTO for the offload runtimes if available")256if (LIBOMPTARGET_USE_LTO)257 # CMake sets CMAKE_CXX_COMPILE_OPTIONS_IPO depending on the compiler and is258 # also what CheckIPOSupported uses to test support.259 list(APPEND offload_compile_flags ${CMAKE_CXX_COMPILE_OPTIONS_IPO})260 list(APPEND offload_link_flags ${CMAKE_CXX_COMPILE_OPTIONS_IPO})261endif()262 263if(OPENMP_STANDALONE_BUILD)264 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")265 execute_process(266 OUTPUT_STRIP_TRAILING_WHITESPACE267 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir268 RESULT_VARIABLE COMMAND_RETURN_CODE269 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR270 )271 endif()272 273 set(LIBOMP_HAVE_OMPT_SUPPORT FALSE)274 set(LIBOMP_OMPT_SUPPORT FALSE)275 276 find_path (277 LIBOMP_OMP_TOOLS_INCLUDE_DIR278 NAMES279 omp-tools.h280 HINTS281 ${COMPILER_RESOURCE_DIR}/include282 ${CMAKE_INSTALL_PREFIX}/include283 )284 285 if(LIBOMP_OMP_TOOLS_INCLUDE_DIR)286 set(LIBOMP_HAVE_OMPT_SUPPORT TRUE)287 set(LIBOMP_OMPT_SUPPORT TRUE)288 endif()289 290 # LLVM_LIBRARY_DIRS set by find_package(LLVM) in LibomptargetGetDependencies291 find_library (292 LIBOMP_STANDALONE293 NAMES294 omp295 HINTS296 ${CMAKE_INSTALL_PREFIX}/lib297 ${LLVM_LIBRARY_DIRS}298 REQUIRED299 )300 301 find_path (302 LIBOMP_INCLUDE_DIR303 NAMES304 omp.h305 HINTS306 ${COMPILER_RESOURCE_DIR}/include307 ${CMAKE_INSTALL_PREFIX}/include308 )309 310 get_filename_component(LIBOMP_LIBRARY_DIR ${LIBOMP_STANDALONE} DIRECTORY)311 312 set(OPENMP_TEST_FLAGS "" CACHE STRING313 "Extra compiler flags to send to the test compiler.")314 set(OPENMP_TEST_OPENMP_FLAGS ${OPENMP_TEST_COMPILER_OPENMP_FLAGS} CACHE STRING315 "OpenMP compiler flag to use for testing OpenMP runtime libraries.")316 set(LIBOMPTARGET_OPENMP_HEADER_FOLDER "${LIBOMP_INCLUDE_DIR}" CACHE STRING317 "Path to folder containing omp.h")318 set(LIBOMPTARGET_OPENMP_HOST_RTL_FOLDER "${LIBOMP_LIBRARY_DIR}" CACHE STRING319 "Path to folder containing libomp.so, and libLLVMSupport.so with profiling enabled")320endif()321 322macro(pythonize_bool var)323if (${var})324 set(${var} True)325else()326 set(${var} False)327endif()328endmacro()329 330# OMPT support for libomptarget331# Follow host OMPT support and check if host support has been requested.332# LIBOMP_HAVE_OMPT_SUPPORT indicates whether host OMPT support has been implemented.333# LIBOMP_OMPT_SUPPORT indicates whether host OMPT support has been requested (default is ON).334# LIBOMPTARGET_OMPT_SUPPORT indicates whether target OMPT support has been requested (default is ON).335set(OMPT_TARGET_DEFAULT FALSE)336if ((LIBOMP_HAVE_OMPT_SUPPORT) AND (LIBOMP_OMPT_SUPPORT) AND (NOT WIN32))337 set (OMPT_TARGET_DEFAULT TRUE)338endif()339set(LIBOMPTARGET_OMPT_SUPPORT ${OMPT_TARGET_DEFAULT} CACHE BOOL "OMPT-target-support?")340if ((OMPT_TARGET_DEFAULT) AND (LIBOMPTARGET_OMPT_SUPPORT))341 add_definitions(-DOMPT_SUPPORT=1)342 message(STATUS "OMPT target enabled")343else()344 set(LIBOMPTARGET_OMPT_SUPPORT FALSE)345 message(STATUS "OMPT target disabled")346endif()347 348pythonize_bool(LIBOMPTARGET_OMPT_SUPPORT)349 350if(${LLVM_LIBC_GPU_BUILD})351 set(LIBOMPTARGET_HAS_LIBC TRUE)352else()353 set(LIBOMPTARGET_HAS_LIBC FALSE)354endif()355set(LIBOMPTARGET_GPU_LIBC_SUPPORT ${LIBOMPTARGET_HAS_LIBC} CACHE BOOL356 "Libomptarget support for the GPU libc")357pythonize_bool(LIBOMPTARGET_GPU_LIBC_SUPPORT)358 359set(LIBOMPTARGET_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)360set(LIBOMPTARGET_BINARY_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)361message(STATUS "OpenMP tools dir in libomptarget: ${LIBOMP_OMP_TOOLS_INCLUDE_DIR}")362if(LIBOMP_OMP_TOOLS_INCLUDE_DIR)363 include_directories(${LIBOMP_OMP_TOOLS_INCLUDE_DIR})364endif()365 366set(LIBOMPTARGET_LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE STRING367 "Path to folder containing llvm library libomptarget.so")368set(LIBOMPTARGET_LLVM_LIBRARY_INTDIR "${LIBOMPTARGET_INTDIR}" CACHE STRING369 "Path to folder where intermediate libraries will be output")370 371add_subdirectory(tools/offload-tblgen)372 373# Build offloading plugins and device RTLs if they are available.374add_subdirectory(plugins-nextgen)375add_subdirectory(tools)376add_subdirectory(docs)377 378# Build target agnostic offloading library.379add_subdirectory(libomptarget)380 381add_subdirectory(liboffload)382 383# Add tests.384if(OFFLOAD_INCLUDE_TESTS)385 add_subdirectory(test)386 add_subdirectory(unittests)387endif()388