116 lines · plain
1#===-- cmake/modules/AddFlangRTOffload.cmake -------------------------------===#2#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-exception6#7#===------------------------------------------------------------------------===#8 9macro(enable_cuda_compilation name files)10 if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")11 if (FLANG_RT_ENABLE_SHARED)12 message(FATAL_ERROR13 "FLANG_RT_ENABLE_SHARED is not supported for CUDA offload build of Flang-RT"14 )15 endif()16 17 enable_language(CUDA)18 19 set_target_properties(${name}.static20 PROPERTIES21 CUDA_SEPARABLE_COMPILATION ON22 )23 24 # Treat all supported sources as CUDA files.25 set_source_files_properties(${files} PROPERTIES LANGUAGE CUDA)26 set(CUDA_COMPILE_OPTIONS)27 if ("${CMAKE_CUDA_COMPILER_ID}" MATCHES "Clang")28 # Allow varargs.29 set(CUDA_COMPILE_OPTIONS30 -Xclang -fcuda-allow-variadic-functions31 )32 endif()33 if ("${CMAKE_CUDA_COMPILER_ID}" MATCHES "NVIDIA")34 set(CUDA_COMPILE_OPTIONS35 --expt-relaxed-constexpr36 # Disable these warnings:37 # 'long double' is treated as 'double' in device code38 -Xcudafe --diag_suppress=2020839 -Xcudafe --display_error_number40 )41 endif()42 set_source_files_properties(${files} PROPERTIES COMPILE_OPTIONS43 "${CUDA_COMPILE_OPTIONS}")44 45 # Create a .a library consisting of CUDA PTX.46 # This is different from a regular static library. The CUDA_PTX_COMPILATION47 # property can only be applied to object libraries and create *.ptx files48 # instead of *.o files. The .a will consist of those *.ptx files only.49 add_flangrt_library(obj.${name}PTX OBJECT ${files})50 set_target_properties(obj.${name}PTX PROPERTIES51 CUDA_PTX_COMPILATION ON52 CUDA_SEPARABLE_COMPILATION ON53 )54 add_flangrt_library(${name}PTX STATIC "$<TARGET_OBJECTS:obj.${name}PTX>")55 56 # Apply configuration options57 if (FLANG_RT_CUDA_RUNTIME_PTX_WITHOUT_GLOBAL_VARS)58 target_compile_definitions(obj.${name}PTX59 PRIVATE FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS60 )61 endif()62 63 # When using libcudacxx headers files, we have to use them64 # for all files of Flang-RT.65 if (EXISTS "${FLANG_RT_LIBCUDACXX_PATH}/include")66 foreach (tgt IN ITEMS "${name}.static" "obj.${name}PTX")67 target_include_directories(${tgt} AFTER PRIVATE "${FLANG_RT_LIBCUDACXX_PATH}/include")68 target_compile_definitions(${tgt} PRIVATE RT_USE_LIBCUDACXX=1)69 endforeach ()70 endif ()71 endif()72endmacro()73 74macro(enable_omp_offload_compilation name files)75 if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "OpenMP")76 # OpenMP offload build only works with Clang compiler currently.77 78 if (FLANG_RT_ENABLE_SHARED)79 message(FATAL_ERROR80 "FLANG_RT_ENABLE_SHARED is not supported for OpenMP offload build of Flang-RT"81 )82 endif()83 84 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND85 "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")86 87 string(REPLACE ";" "," compile_for_architectures88 "${FLANG_RT_DEVICE_ARCHITECTURES}"89 )90 91 set(OMP_COMPILE_OPTIONS92 -fopenmp93 -fvisibility=hidden94 -fopenmp-cuda-mode95 --offload-arch=${compile_for_architectures}96 # Force LTO for the device part.97 -foffload-lto98 )99 set_source_files_properties(${files} PROPERTIES COMPILE_OPTIONS100 "${OMP_COMPILE_OPTIONS}"101 )102 target_link_options(${name}.static PUBLIC ${OMP_COMPILE_OPTIONS})103 104 # Enable "declare target" in the source code.105 set_source_files_properties(${files}106 PROPERTIES COMPILE_DEFINITIONS OMP_OFFLOAD_BUILD107 )108 else()109 message(FATAL_ERROR110 "Flang-rt build with OpenMP offload is not supported for these compilers:\n"111 "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}\n"112 "CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")113 endif()114 endif()115endmacro()116