diff --git a/openmp/libomptarget/DeviceRTL/CMakeLists.txt b/openmp/libomptarget/DeviceRTL/CMakeLists.txt --- a/openmp/libomptarget/DeviceRTL/CMakeLists.txt +++ b/openmp/libomptarget/DeviceRTL/CMakeLists.txt @@ -38,24 +38,48 @@ if (NOT LIBOMPTARGET_NVPTX_CUDA_COMPILER STREQUAL "") set(cuda_compiler ${LIBOMPTARGET_NVPTX_CUDA_COMPILER}) +elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING) + # Compile the deviceRTL with the clang that is built in the project. + set(cuda_compiler "$") elseif(${CMAKE_C_COMPILER_ID} STREQUAL "Clang") + # Compile the device runtime with the compiler that OpenMP is built with. + # This is the case with LLVM_ENABLE_RUNTIMES=openmp. + # FIXME: This is unreliable; the compiler can be on older version of clang + # that does not support compiling CUDA, or only an older version of it. The + # risk is especially high on sytems where clang is the default compiler + # (MacOS, BSDs). LLVM_ENABLE_RUNTIMES=openmp should itself set + # LIBOMPTARGET_NVPTX_CUDA_COMPILER instead. set(cuda_compiler ${CMAKE_C_COMPILER}) else() - libomptarget_say("Not building NVPTX deviceRTL: clang not found") + libomptarget_say("Not building deviceRTL: clang not found") return() endif() # Get compiler directory to try to locate a suitable linker. get_filename_component(compiler_dir ${cuda_compiler} DIRECTORY) -set(llvm_link "${compiler_dir}/llvm-link") -set(opt "${compiler_dir}/opt") if (NOT LIBOMPTARGET_NVPTX_BC_LINKER STREQUAL "") set(bc_linker ${LIBOMPTARGET_NVPTX_BC_LINKER}) -elseif (EXISTS ${llvm_link}) +elseif (EXISTS "${compiler_dir}/llvm-link" AND NOT IS_DIRECTORY "${compiler_dir}/llvm-link") + # Try to use the linker consistent with the CUDA compiler unless explicitly + # set to a different linker. set(bc_linker ${llvm_link}) +elseif (NOT OPENMP_STANDALONE_BUILD AND NOT CMAKE_CROSSCOMPILING) + # Use the linker also built in the same project. + set(bc_linker "$") else() - libomptarget_say("Not building NVPTX deviceRTL: llvm-link not found") + libomptarget_say("Not building deviceRTL: llvm-link not found") + return() +endif() + +if (EXISTS "${compiler_dir}/opt" AND NOT IS_DIRECTORY "${compiler_dir}/opt") + # Try to use the opt consistent with the CUDA compiler. + set(opt "${compiler_dir}/opt") +elseif (NOT OPENMP_STANDALONE_BUILD AND NOT CMAKE_CROSSCOMPILING) + # Use opt that is also built in the same project. + set(opt "$") +else() + libomptarget_say("Not building deviceRTL: opt not found") return() endif() @@ -118,7 +142,11 @@ set(LIBOMPTARGET_DEVICE_DEBUG FALSE CACHE BOOL "Activate NVPTX device RTL debug messages.") -libomptarget_say("Building CUDA LLVM bitcode offloading device RTL.") +if ("${cuda_compiler}" STREQUAL "$") + libomptarget_say("Building LLVM bitcode offloading device RTL using in-tree clang.") +else () + libomptarget_say("Building LLVM bitcode offloading device RTL using ${cuda_compiler}") +endif () set(src_files ${source_directory}/Configuration.cpp @@ -182,6 +210,15 @@ COMMENT "Building LLVM bitcode ${outfile}" VERBATIM ) + if("${cuda_compiler}" STREQUAL "$") + # Add a file-level dependency to ensure that clang is up-to-date. + # By default, add_custom_command only builds clang if the + # executable is missing. + add_custom_command(OUTPUT ${outfile} + DEPENDS clang + APPEND + ) + endif() set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile}) list(APPEND bc_files ${outfile}) @@ -196,6 +233,15 @@ DEPENDS ${bc_files} COMMENT "Linking LLVM bitcode ${bclib_name}" ) + if("${bc_linker}" STREQUAL "$") + # Add a file-level dependency to ensure that llvm-link is up-to-date. + # By default, add_custom_command only builds llvm-link if the + # executable is missing. + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} + DEPENDS llvm-link + APPEND + ) + endif() add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}_opt COMMAND ${opt} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} @@ -203,6 +249,15 @@ DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} COMMENT "Optimizing LLVM bitcode ${bclib_name}" ) + if("${opt}" STREQUAL "$") + # Add a file-level dependency to ensure that opt is up-to-date. + # By default, add_custom_command only builds opt if the + # executable is missing. + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}_opt + DEPENDS opt + APPEND + ) + endif() set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name}) set(bclib_target_name "omptarget-new-nvptx-sm_${sm}-bc")