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 @@ -31,7 +31,8 @@ find_program(PACKAGER_TOOL clang-offload-packager PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH) find_program(LINK_TOOL llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH) find_program(OPT_TOOL opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH) - if ((NOT CLANG_TOOL) OR (NOT LINK_TOOL) OR (NOT OPT_TOOL)) + find_program(EXTRACT_TOOL llvm-extract PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH) + if ((NOT CLANG_TOOL) OR (NOT LINK_TOOL) OR (NOT OPT_TOOL) OR (NOT EXTRACT_TOOL)) libomptarget_say("Not building DeviceRTL. Missing clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} or opt: ${OPT_TOOL}") return() else() @@ -44,6 +45,7 @@ set(PACKAGER_TOOL $) set(LINK_TOOL $) set(OPT_TOOL $) + set(EXTRACT_TOOL $) libomptarget_say("Building DeviceRTL. Using clang from in-tree build") else() libomptarget_say("Not building DeviceRTL. No appropriate clang found") @@ -118,6 +120,7 @@ set(clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512) set(link_opt_flags -O3 -openmp-opt-disable) set(link_export_flag -passes=internalize -internalize-public-api-file=${source_directory}/exports) +set(link_extract_flag --func='__keep_alive' --delete) # Prepend -I to each list element set (LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL "${LIBOMPTARGET_LLVM_INCLUDE_DIRS}") @@ -199,11 +202,18 @@ COMMENT "Optimizing LLVM bitcode ${bclib_name}" ) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/extracted_${bclib_name} + COMMAND ${EXTRACT_TOOL} ${link_extract_flag} ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} + -o ${CMAKE_CURRENT_BINARY_DIR}/extracted_${bclib_name} + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} + COMMENT "Extracting LLVM bitcode ${bclib_name}" + ) + # Package the bitcode in the bitcode and embed it in an ELF for the static library add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} - "--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},triple=${target_triple},arch=${target_cpu},kind=openmp" - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} + "--image=file=${CMAKE_CURRENT_BINARY_DIR}/extracted_${bclib_name},triple=${target_triple},arch=${target_cpu},kind=openmp" + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/extracted_${bclib_name} COMMENT "Packaging LLVM offloading binary ${bclib_name}.out" ) @@ -232,6 +242,11 @@ DEPENDS opt APPEND) endif() + if("${EXTRACT_TOOL}" STREQUAL "$") + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/extracted_${bclib_name} + DEPENDS opt + APPEND) + endif() if("${PACKAGER_TOOL}" STREQUAL "$") add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} DEPENDS clang-offload-packager diff --git a/openmp/libomptarget/DeviceRTL/include/Types.h b/openmp/libomptarget/DeviceRTL/include/Types.h --- a/openmp/libomptarget/DeviceRTL/include/Types.h +++ b/openmp/libomptarget/DeviceRTL/include/Types.h @@ -213,13 +213,6 @@ #define CONSTANT(NAME) \ [[clang::address_space(4)]] NAME [[clang::loader_uninitialized]] -// Attribute to keep alive certain definition for the bitcode library. -#ifdef LIBOMPTARGET_BC_TARGET -#define KEEP_ALIVE __attribute__((used, retain)) -#else -#define KEEP_ALIVE -#endif - ///} #endif diff --git a/openmp/libomptarget/DeviceRTL/src/Mapping.cpp b/openmp/libomptarget/DeviceRTL/src/Mapping.cpp --- a/openmp/libomptarget/DeviceRTL/src/Mapping.cpp +++ b/openmp/libomptarget/DeviceRTL/src/Mapping.cpp @@ -276,7 +276,7 @@ // TODO: This is a workaround for initialization coming from kernels outside of // the TU. We will need to solve this more correctly in the future. -int __attribute__((weak)) KEEP_ALIVE SHARED(IsSPMDMode); +int __attribute__((weak)) SHARED(IsSPMDMode); void mapping::init(bool IsSPMD) { if (mapping::isInitialThreadInLevel0(IsSPMD)) diff --git a/openmp/libomptarget/DeviceRTL/src/Utils.cpp b/openmp/libomptarget/DeviceRTL/src/Utils.cpp --- a/openmp/libomptarget/DeviceRTL/src/Utils.cpp +++ b/openmp/libomptarget/DeviceRTL/src/Utils.cpp @@ -19,16 +19,17 @@ using namespace _OMP; -namespace _OMP { +extern "C" __attribute__((weak)) int IsSPMDMode; + /// Helper to keep code alive without introducing a performance penalty. -__attribute__((weak, optnone, cold)) KEEP_ALIVE void keepAlive() { +extern "C" __attribute__((weak, optnone, cold, used, retain)) void +__keep_alive() { __kmpc_get_hardware_thread_id_in_block(); __kmpc_get_hardware_num_threads_in_block(); __kmpc_get_warp_size(); - __kmpc_barrier_simple_spmd(nullptr, 0); - __kmpc_barrier_simple_generic(nullptr, 0); + __kmpc_barrier_simple_spmd(nullptr, IsSPMDMode); + __kmpc_barrier_simple_generic(nullptr, IsSPMDMode); } -} // namespace _OMP namespace impl { diff --git a/openmp/libomptarget/DeviceRTL/src/exports b/openmp/libomptarget/DeviceRTL/src/exports --- a/openmp/libomptarget/DeviceRTL/src/exports +++ b/openmp/libomptarget/DeviceRTL/src/exports @@ -2,6 +2,9 @@ *llvm_* __kmpc_* +__keep_alive +IsSPMDMode + memcmp printf __assert_fail