Skip to content

Commit

Permalink
Pre land trunk commit: [offload] Fix link issues when LLVM_LINK_LLVM_…
Browse files Browse the repository at this point in the history
…DYLIB on llvm#106583

Change-Id: Ib96cbf7e1e1b0908cf6d11ae9251f46ce3a9a850
  • Loading branch information
ronlieb committed Aug 31, 2024
1 parent c74c21f commit 497128f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
10 changes: 8 additions & 2 deletions offload/plugins-nextgen/amdgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ target_include_directories(omptarget.rtl.amdgpu PRIVATE
target_link_libraries(omptarget.rtl.amdgpu PRIVATE
-Wl,--whole-archive amdgcn_hostexec_services -Wl,--no-whole-archive)

if (LLVM_LINK_LLVM_DYLIB)
set(llvm_libs LLVM)
else()
set(llvm_libs LLVMFrontendOffloading)
endif()

option(LIBOMPTARGET_FORCE_DLOPEN_LIBHSA "Build with dlopened libhsa" OFF)
if(DEFINED HSA_RUNTIME AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
message(STATUS "Building AMDGPU plugin linked against libhsa")
Expand All @@ -38,12 +44,12 @@ if(DEFINED HSA_RUNTIME AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
target_include_directories(omptarget.rtl.amdgpu PRIVATE ${library_path}/../include)
elseif(hsa-runtime64_FOUND AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
message(STATUS "Building AMDGPU plugin linked against libhsa")
target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64 LLVMFrontendOffloading)
target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64 ${llvm_libs})
else()
message(STATUS "Building AMDGPU plugin for dlopened libhsa")
target_include_directories(omptarget.rtl.amdgpu PRIVATE dynamic_hsa)
target_sources(omptarget.rtl.amdgpu PRIVATE dynamic_hsa/hsa.cpp)
target_link_libraries(omptarget.rtl.amdgpu PRIVATE LLVMFrontendOffloading)
target_link_libraries(omptarget.rtl.amdgpu PRIVATE ${llvm_libs})
endif()

# Configure testing for the AMDGPU plugin. We will build tests if we could a
Expand Down
6 changes: 5 additions & 1 deletion offload/plugins-nextgen/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ target_compile_definitions(PluginCommon PRIVATE

target_compile_options(PluginCommon PUBLIC ${offload_compile_flags})
target_link_options(PluginCommon PUBLIC ${offload_link_flags})
target_link_libraries(PluginCommon PRIVATE LLVMProfileData)
if (LLVM_LINK_LLVM_DYLIB)
target_link_libraries(PluginCommon PRIVATE LLVM)
else()
target_link_libraries(PluginCommon PRIVATE LLVMProfileData)
endif()

target_include_directories(PluginCommon PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
Expand Down
62 changes: 62 additions & 0 deletions offload/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,65 @@ set_target_properties(omptarget PROPERTIES
POSITION_INDEPENDENT_CODE ON
BUILD_RPATH "$ORIGIN:${CMAKE_CURRENT_BINARY_DIR}/..")
install(TARGETS omptarget LIBRARY COMPONENT omptarget DESTINATION "${OFFLOAD_INSTALL_LIBDIR}")

#===============================================================================
# Ensure that omptarget does not contain a mixture of static and dynamically
# linked LLVM libs.
#===============================================================================
if (LLVM_LINK_LLVM_DYLIB)
if(LLVM_AVAILABLE_LIBS)
set(llvm_libs ${LLVM_AVAILABLE_LIBS})
else()
# Inside LLVM itself available libs are in a global property.
get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
endif()

#-----------------------------------------------------------------------------
# Helper function to recursively get the llvm targets that 'tgt' links against
#-----------------------------------------------------------------------------
function(get_llvm_link_targets var tgt visited)
if(${tgt} IN_LIST visited)
return()
endif()
list(APPEND visited ${tgt})

get_target_property(link_libs ${tgt} LINK_LIBRARIES)
if(NOT link_libs)
set(link_libs "")
endif()
get_target_property(i_link_libs ${tgt} INTERFACE_LINK_LIBRARIES)
if(i_link_libs)
list(APPEND link_libs ${i_link_libs})
endif()
if(NOT link_libs)
return()
endif()
list(REMOVE_DUPLICATES link_libs)

foreach(lib ${link_libs})
if(${lib} IN_LIST llvm_libs)
list(APPEND rv ${lib})
endif()
if(TARGET ${lib})
get_llvm_link_targets(indirect ${lib} visited)
list(APPEND rv ${indirect})
list(REMOVE_DUPLICATES rv)
endif()
endforeach()

set(${var} ${rv} PARENT_SCOPE)
endfunction()

#-----------------------------------------------------------------------------
# Check for extraneous libs
#-----------------------------------------------------------------------------
get_llvm_link_targets(llvm_link_targets omptarget "")
list(REMOVE_ITEM llvm_link_targets "LLVM")
if(llvm_link_targets)
list(JOIN llvm_link_targets " " pp_list)
message(
FATAL_ERROR
"'omptarget' should only link against 'LLVM' when 'LLVM_LINK_LLVM_DYLIB' "
"is on. Extraneous LLVM Libraries: ${pp_list}")
endif()
endif()

0 comments on commit 497128f

Please sign in to comment.