Skip to content

Commit dcbddc2

Browse files
authored
[Libomptarget] Unify and simplify plugin CMake (#86191)
Summary: This patch reworks the CMake handling for building plugins. All this does is pull a lot of shared and common logic into a single helper function. This also simplifies the OMPT libraries from being built separately instead of just added.
1 parent f3cfe01 commit dcbddc2

File tree

6 files changed

+87
-245
lines changed

6 files changed

+87
-245
lines changed

openmp/libomptarget/plugins-nextgen/CMakeLists.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,66 @@
1010
#
1111
##===----------------------------------------------------------------------===##
1212

13+
# Common interface to handle creating a plugin library.
14+
set(common_dir ${CMAKE_CURRENT_SOURCE_DIR}/common)
1315
add_subdirectory(common)
16+
function(add_target_library target_name lib_name)
17+
llvm_map_components_to_libnames(llvm_libs
18+
${LLVM_TARGETS_TO_BUILD}
19+
AggressiveInstCombine
20+
Analysis
21+
BinaryFormat
22+
BitReader
23+
BitWriter
24+
CodeGen
25+
Core
26+
Extensions
27+
InstCombine
28+
Instrumentation
29+
IPO
30+
IRReader
31+
Linker
32+
MC
33+
Object
34+
Passes
35+
Remarks
36+
ScalarOpts
37+
Support
38+
Target
39+
TargetParser
40+
TransformUtils
41+
Vectorize
42+
)
43+
44+
add_llvm_library(${target_name} SHARED
45+
NO_INSTALL_RPATH
46+
BUILDTREE_ONLY
47+
)
48+
49+
llvm_update_compile_flags(${target_name})
50+
target_link_libraries(${target_name} PUBLIC
51+
PluginCommon ${llvm_libs} ${OPENMP_PTHREAD_LIB})
52+
53+
target_compile_definitions(${target_name} PRIVATE TARGET_NAME=${lib_name})
54+
target_compile_definitions(${target_name} PRIVATE
55+
DEBUG_PREFIX="TARGET ${lib_name} RTL")
56+
57+
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
58+
# On FreeBSD, the 'environ' symbol is undefined at link time, but resolved by
59+
# the dynamic linker at runtime. Therefore, allow the symbol to be undefined
60+
# when creating a shared library.
61+
target_link_libraries(${target_name} PRIVATE "-Wl,--allow-shlib-undefined")
62+
else()
63+
target_link_libraries(${target_name} PRIVATE "-Wl,-z,defs")
64+
endif()
65+
66+
if(LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
67+
target_link_libraries(${target_name} PRIVATE
68+
"-Wl,--version-script=${common_dir}/../exports")
69+
endif()
70+
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET protected)
71+
endfunction()
72+
1473
add_subdirectory(amdgpu)
1574
add_subdirectory(cuda)
1675
add_subdirectory(host)

openmp/libomptarget/plugins-nextgen/amdgpu/CMakeLists.txt

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,76 +27,23 @@ if(NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(ppc64le)|(aarch64)$" AND CMAKE
2727
return()
2828
endif()
2929

30-
################################################################################
31-
# Define the suffix for the runtime messaging dumps.
32-
add_definitions(-DTARGET_NAME=AMDGPU)
33-
34-
# Define debug prefix. TODO: This should be automatized in the Debug.h but it
35-
# requires changing the original plugins.
36-
add_definitions(-DDEBUG_PREFIX="TARGET AMDGPU RTL")
30+
# Create the library and add the default arguments.
31+
add_target_library(omptarget.rtl.amdgpu AMDGPU)
3732

38-
set(LIBOMPTARGET_DLOPEN_LIBHSA OFF)
39-
option(LIBOMPTARGET_FORCE_DLOPEN_LIBHSA "Build with dlopened libhsa" ${LIBOMPTARGET_DLOPEN_LIBHSA})
40-
41-
if (${hsa-runtime64_FOUND} AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
42-
libomptarget_say("Building AMDGPU NextGen plugin linked against libhsa")
43-
set(LIBOMPTARGET_EXTRA_SOURCE)
44-
set(LIBOMPTARGET_DEP_LIBRARIES hsa-runtime64::hsa-runtime64)
45-
else()
46-
libomptarget_say("Building AMDGPU NextGen plugin for dlopened libhsa")
47-
include_directories(dynamic_hsa)
48-
set(LIBOMPTARGET_EXTRA_SOURCE dynamic_hsa/hsa.cpp)
49-
set(LIBOMPTARGET_DEP_LIBRARIES)
50-
endif()
33+
target_sources(omptarget.rtl.amdgpu PRIVATE src/rtl.cpp)
34+
target_include_directories(omptarget.rtl.amdgpu PRIVATE
35+
${CMAKE_CURRENT_SOURCE_DIR}/utils)
5136

52-
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
53-
# On FreeBSD, the 'environ' symbol is undefined at link time, but resolved by
54-
# the dynamic linker at runtime. Therefore, allow the symbol to be undefined
55-
# when creating a shared library.
56-
set(LDFLAGS_UNDEFINED "-Wl,--allow-shlib-undefined")
37+
option(LIBOMPTARGET_FORCE_DLOPEN_LIBHSA "Build with dlopened libhsa" OFF)
38+
if(hsa-runtime64_FOUND AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
39+
libomptarget_say("Building AMDGPU plugin linked against libhsa")
40+
target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64)
5741
else()
58-
set(LDFLAGS_UNDEFINED "-Wl,-z,defs")
42+
libomptarget_say("Building AMDGPU plugin for dlopened libhsa")
43+
target_include_directories(omptarget.rtl.amdgpu PRIVATE dynamic_hsa)
44+
target_sources(omptarget.rtl.amdgpu PRIVATE dynamic_hsa/hsa.cpp)
5945
endif()
6046

61-
add_llvm_library(omptarget.rtl.amdgpu SHARED
62-
src/rtl.cpp
63-
${LIBOMPTARGET_EXTRA_SOURCE}
64-
65-
ADDITIONAL_HEADER_DIRS
66-
${LIBOMPTARGET_INCLUDE_DIR}
67-
${CMAKE_CURRENT_SOURCE_DIR}/utils
68-
69-
LINK_COMPONENTS
70-
Support
71-
Object
72-
73-
LINK_LIBS
74-
PRIVATE
75-
PluginCommon
76-
${LIBOMPTARGET_DEP_LIBRARIES}
77-
${OPENMP_PTHREAD_LIB}
78-
${LDFLAGS_UNDEFINED}
79-
80-
NO_INSTALL_RPATH
81-
BUILDTREE_ONLY
82-
)
83-
84-
if ((OMPT_TARGET_DEFAULT) AND (LIBOMPTARGET_OMPT_SUPPORT))
85-
target_link_libraries(omptarget.rtl.amdgpu PRIVATE OMPT)
86-
endif()
87-
88-
if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
89-
target_link_libraries(omptarget.rtl.amdgpu PRIVATE
90-
"-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exports")
91-
endif()
92-
93-
target_include_directories(
94-
omptarget.rtl.amdgpu
95-
PRIVATE
96-
${LIBOMPTARGET_INCLUDE_DIR}
97-
${CMAKE_CURRENT_SOURCE_DIR}/utils
98-
)
99-
10047
# Configure testing for the AMDGPU plugin. We will build tests if we could a
10148
# functional AMD GPU on the system, or if manually specifies by the user.
10249
option(LIBOMPTARGET_FORCE_AMDGPU_TESTS "Build AMDGPU libomptarget tests" OFF)
@@ -114,5 +61,4 @@ endif()
11461
# Install plugin under the lib destination folder.
11562
install(TARGETS omptarget.rtl.amdgpu LIBRARY DESTINATION "${OPENMP_INSTALL_LIBDIR}")
11663
set_target_properties(omptarget.rtl.amdgpu PROPERTIES
117-
INSTALL_RPATH "$ORIGIN" BUILD_RPATH "$ORIGIN:${CMAKE_CURRENT_BINARY_DIR}/.."
118-
CXX_VISIBILITY_PRESET protected)
64+
INSTALL_RPATH "$ORIGIN" BUILD_RPATH "$ORIGIN:${CMAKE_CURRENT_BINARY_DIR}/..")

openmp/libomptarget/plugins-nextgen/common/CMakeLists.txt

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,6 @@ foreach(Target ${TargetsSupported})
2626
target_compile_definitions(PluginCommon PRIVATE "LIBOMPTARGET_JIT_${Target}")
2727
endforeach()
2828

29-
# This is required when using LLVM libraries.
30-
llvm_update_compile_flags(PluginCommon)
31-
32-
if (LLVM_LINK_LLVM_DYLIB)
33-
set(llvm_libs LLVM)
34-
else()
35-
llvm_map_components_to_libnames(llvm_libs
36-
${LLVM_TARGETS_TO_BUILD}
37-
AggressiveInstCombine
38-
Analysis
39-
BinaryFormat
40-
BitReader
41-
BitWriter
42-
CodeGen
43-
Core
44-
Extensions
45-
InstCombine
46-
Instrumentation
47-
IPO
48-
IRReader
49-
Linker
50-
MC
51-
Object
52-
Passes
53-
Remarks
54-
ScalarOpts
55-
Support
56-
Target
57-
TargetParser
58-
TransformUtils
59-
Vectorize
60-
)
61-
endif()
62-
63-
target_link_libraries(PluginCommon
64-
PUBLIC
65-
${llvm_libs}
66-
)
67-
6829
# Include the RPC server from the `libc` project if availible.
6930
if(TARGET llvmlibc_rpc_server AND ${LIBOMPTARGET_GPU_LIBC_SUPPORT})
7031
target_link_libraries(PluginCommon PRIVATE llvmlibc_rpc_server)
@@ -82,8 +43,10 @@ elseif(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
8243
endif()
8344
endif()
8445

85-
if ((OMPT_TARGET_DEFAULT) AND (LIBOMPTARGET_OMPT_SUPPORT))
86-
target_link_libraries(PluginCommon PUBLIC OMPT)
46+
# If we have OMPT enabled include it in the list of sources.
47+
if (OMPT_TARGET_DEFAULT AND LIBOMPTARGET_OMPT_SUPPORT)
48+
target_sources(PluginCommon PRIVATE OMPT/OmptCallback.cpp)
49+
target_include_directories(PluginCommon PRIVATE OMPT)
8750
endif()
8851

8952
# Define the TARGET_NAME and DEBUG_PREFIX.
@@ -95,16 +58,11 @@ target_compile_definitions(PluginCommon PRIVATE
9558
target_compile_options(PluginCommon PUBLIC ${offload_compile_flags})
9659
target_link_options(PluginCommon PUBLIC ${offload_link_flags})
9760

98-
target_include_directories(PluginCommon
99-
PRIVATE
100-
${LIBOMPTARGET_INCLUDE_DIR}
101-
PUBLIC
61+
target_include_directories(PluginCommon PUBLIC
10262
${CMAKE_CURRENT_SOURCE_DIR}/include
63+
${LIBOMPTARGET_INCLUDE_DIR}
10364
)
10465

10566
set_target_properties(PluginCommon PROPERTIES
10667
POSITION_INDEPENDENT_CODE ON
10768
CXX_VISIBILITY_PRESET protected)
108-
109-
add_subdirectory(OMPT)
110-

openmp/libomptarget/plugins-nextgen/common/OMPT/CMakeLists.txt

Lines changed: 0 additions & 70 deletions
This file was deleted.

openmp/libomptarget/plugins-nextgen/cuda/CMakeLists.txt

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,12 @@ endif()
2323

2424
libomptarget_say("Building CUDA NextGen offloading plugin.")
2525

26-
set(LIBOMPTARGET_DLOPEN_LIBCUDA OFF)
27-
option(LIBOMPTARGET_FORCE_DLOPEN_LIBCUDA "Build with dlopened libcuda" ${LIBOMPTARGET_DLOPEN_LIBCUDA})
28-
29-
add_llvm_library(omptarget.rtl.cuda SHARED
30-
src/rtl.cpp
31-
32-
LINK_COMPONENTS
33-
Support
34-
Object
35-
36-
LINK_LIBS PRIVATE
37-
PluginCommon
38-
${OPENMP_PTHREAD_LIB}
39-
40-
NO_INSTALL_RPATH
41-
BUILDTREE_ONLY
42-
)
43-
44-
if ((OMPT_TARGET_DEFAULT) AND (LIBOMPTARGET_OMPT_SUPPORT))
45-
target_link_libraries(omptarget.rtl.cuda PRIVATE OMPT)
46-
endif()
47-
48-
if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
49-
target_link_libraries(omptarget.rtl.cuda PRIVATE
50-
"-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exports,-z,defs")
51-
endif()
26+
# Create the library and add the default arguments.
27+
add_target_library(omptarget.rtl.cuda CUDA)
5228

29+
target_sources(omptarget.rtl.cuda PRIVATE src/rtl.cpp)
5330

31+
option(LIBOMPTARGET_FORCE_DLOPEN_LIBCUDA "Build with dlopened libcuda" OFF)
5432
if(LIBOMPTARGET_DEP_CUDA_FOUND AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBCUDA)
5533
libomptarget_say("Building CUDA plugin linked against libcuda")
5634
target_link_libraries(omptarget.rtl.cuda PRIVATE CUDA::cuda_driver)
@@ -60,13 +38,6 @@ else()
6038
target_sources(omptarget.rtl.cuda PRIVATE dynamic_cuda/cuda.cpp)
6139
endif()
6240

63-
# Define debug prefix. TODO: This should be automatized in the Debug.h but it
64-
# requires changing the original plugins.
65-
target_compile_definitions(omptarget.rtl.cuda PRIVATE TARGET_NAME="CUDA")
66-
target_compile_definitions(omptarget.rtl.cuda PRIVATE DEBUG_PREFIX="TARGET CUDA RTL")
67-
68-
target_include_directories(omptarget.rtl.cuda PRIVATE ${LIBOMPTARGET_INCLUDE_DIR})
69-
7041
# Configure testing for the CUDA plugin. We will build tests if we could a
7142
# functional NVIDIA GPU on the system, or if manually specifies by the user.
7243
option(LIBOMPTARGET_FORCE_NVIDIA_TESTS "Build NVIDIA libomptarget tests" OFF)
@@ -84,5 +55,4 @@ endif()
8455
# Install plugin under the lib destination folder.
8556
install(TARGETS omptarget.rtl.cuda LIBRARY DESTINATION "${OPENMP_INSTALL_LIBDIR}")
8657
set_target_properties(omptarget.rtl.cuda PROPERTIES
87-
INSTALL_RPATH "$ORIGIN" BUILD_RPATH "$ORIGIN:${CMAKE_CURRENT_BINARY_DIR}/.."
88-
CXX_VISIBILITY_PRESET protected)
58+
INSTALL_RPATH "$ORIGIN" BUILD_RPATH "$ORIGIN:${CMAKE_CURRENT_BINARY_DIR}/..")

0 commit comments

Comments
 (0)