Skip to content

Commit 1beca12

Browse files
jhuber6ronlieb
authored andcommitted
[PATCH] [Libomptarget] Unify and simplify plugin CMake llvm#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. Change-Id: I62210d506c1e51f4412c69162a91da2db8939600
1 parent dabcf58 commit 1beca12

File tree

9 files changed

+120
-269
lines changed

9 files changed

+120
-269
lines changed

openmp/libomptarget/hostexec/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ foreach(archname ${HOSTRPC_ARCHS})
181181
elseif(${archname} STREQUAL "nvptx")
182182
set(openmp_device_args
183183
-I../../runtime/src # to pickup omp.h, we may need a dependency
184+
-I${CMAKE_CURRENT_SOURCE_DIR}/../DeviceRTL/include # pickup Platform.h
184185
-fopenmp -fopenmp-cuda-mode -mllvm -openmp-opt-disable
185186
-std=c++17 -fvisibility=hidden
186187
-fopenmp-targets=${triple} -Xopenmp-target=${triple} -march=${mcpu}

openmp/libomptarget/hostexec/services/amdgcn_hostexec.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818
#include <atomic>
1919
#include <cstring>
2020
#include <functional>
21-
#include <hsa.h>
22-
#include <hsa_ext_amd.h>
2321
#include <iostream>
2422
#include <list>
2523
#include <mutex>
2624
#include <thread>
2725

26+
#if __has_include("hsa/hsa.h")
27+
#include "hsa/hsa.h"
28+
#include "hsa/hsa_ext_amd.h"
29+
#else
30+
#include "hsa.h"
31+
#include "hsa_ext_amd.h"
32+
#endif
33+
2834
/// Defines how many GPUs are maximally supported on a system
2935
#define AMD_MAX_HSA_AGENTS 16
3036

openmp/libomptarget/hostexec/services/urilocator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929

3030
#ifndef URILOCATOR_H
3131
#define URILOCATOR_H
32+
33+
#if __has_include("hsa/hsa.h")
34+
#include "hsa/hsa_ven_amd_loader.h"
35+
#else
3236
#include "hsa_ven_amd_loader.h"
37+
#endif
3338
#include <string>
3439
#include <vector>
3540

openmp/libomptarget/plugins-nextgen/CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,64 @@
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+
add_llvm_library(${target_name} SHARED
18+
LINK_COMPONENTS
19+
${LLVM_TARGETS_TO_BUILD}
20+
AggressiveInstCombine
21+
Analysis
22+
BinaryFormat
23+
BitReader
24+
BitWriter
25+
CodeGen
26+
Core
27+
Extensions
28+
InstCombine
29+
Instrumentation
30+
IPO
31+
IRReader
32+
Linker
33+
MC
34+
Object
35+
Passes
36+
Remarks
37+
ScalarOpts
38+
Support
39+
Target
40+
TargetParser
41+
TransformUtils
42+
Vectorize
43+
NO_INSTALL_RPATH
44+
BUILDTREE_ONLY
45+
)
46+
47+
llvm_update_compile_flags(${target_name})
48+
target_link_libraries(${target_name} PRIVATE
49+
PluginCommon ${llvm_libs} ${OPENMP_PTHREAD_LIB})
50+
51+
target_compile_definitions(${target_name} PRIVATE TARGET_NAME=${lib_name})
52+
target_compile_definitions(${target_name} PRIVATE
53+
DEBUG_PREFIX="TARGET ${lib_name} RTL")
54+
55+
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
56+
# On FreeBSD, the 'environ' symbol is undefined at link time, but resolved by
57+
# the dynamic linker at runtime. Therefore, allow the symbol to be undefined
58+
# when creating a shared library.
59+
target_link_libraries(${target_name} PRIVATE "-Wl,--allow-shlib-undefined")
60+
else()
61+
target_link_libraries(${target_name} PRIVATE "-Wl,-z,defs")
62+
endif()
63+
64+
if(LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
65+
target_link_libraries(${target_name} PRIVATE
66+
"-Wl,--version-script=${common_dir}/../exports")
67+
endif()
68+
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET protected)
69+
endfunction()
70+
1471
add_subdirectory(amdgpu)
1572
add_subdirectory(cuda)
1673
add_subdirectory(host)

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

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,98 +22,53 @@ endif()
2222
# If we are bootstrapping hsa via external project we need to use find_library
2323
# as it will not be installed.
2424
if(DEFINED LIBOMPTARGET_EXTERNAL_PROJECT_HSA_PATH)
25-
find_library(HSA_RUNTIME hsa-runtime64 HINTS ${CMAKE_BINARY_DIR}/../../runtimes/rocr-runtime-prefix/src/rocr-runtime-build ${CMAKE_INSTALL_PREFIX})
25+
find_library(HSA_RUNTIME hsa-runtime64
26+
HINTS
27+
${CMAKE_BINARY_DIR}/../../runtimes/rocr-runtime-prefix/src/rocr-runtime-build
28+
${CMAKE_INSTALL_PREFIX}
29+
PATHS
30+
${LIBOMPTARGET_EXTERNAL_PROJECT_HSA_PATH}
31+
)
2632
else()
2733
# As of rocm-3.7, hsa is installed with cmake packages and kmt is found via hsa
2834
find_package(hsa-runtime64 QUIET 1.2.0 HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
2935
endif()
3036

31-
# Check for COMGr package , ASan requires COMGr with minimum version 2.4
32-
if(SANITIZER_AMDGPU)
33-
add_definitions(-DSANITIZER_AMDGPU=1)
34-
find_package(amd_comgr QUIET 2.4.0 HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
35-
endif()
36-
3737
if(NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(ppc64le)|(aarch64)$" AND CMAKE_SYSTEM_NAME MATCHES "Linux"))
3838
libomptarget_say("Not building AMDGPU NextGen plugin: only support AMDGPU in Linux x86_64, ppc64le, or aarch64 hosts")
3939
return()
4040
endif()
4141

42-
################################################################################
43-
# Define the suffix for the runtime messaging dumps.
44-
add_definitions(-DTARGET_NAME=AMDGPU)
42+
# Create the library and add the default arguments.
43+
add_target_library(omptarget.rtl.amdgpu AMDGPU)
4544

46-
# Define debug prefix. TODO: This should be automatized in the Debug.h but it
47-
# requires changing the original plugins.
48-
add_definitions(-DDEBUG_PREFIX="TARGET AMDGPU RTL")
45+
target_sources(omptarget.rtl.amdgpu PRIVATE src/rtl.cpp)
46+
target_include_directories(omptarget.rtl.amdgpu PRIVATE
47+
${CMAKE_CURRENT_SOURCE_DIR}/utils)
48+
target_link_libraries(omptarget.rtl.amdgpu PRIVATE
49+
-Wl,--whole-archive amdgcn_hostexec_services -Wl,--no-whole-archive)
4950

50-
set(LIBOMPTARGET_DLOPEN_LIBHSA OFF)
51-
option(LIBOMPTARGET_FORCE_DLOPEN_LIBHSA "Build with dlopened libhsa" ${LIBOMPTARGET_DLOPEN_LIBHSA})
52-
53-
if(DEFINED HSA_RUNTIME AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
54-
libomptarget_say("Building AMDGPU NextGen plugin linked against libhsa")
55-
set(LIBOMPTARGET_EXTRA_SOURCE)
56-
set(LIBOMPTARGET_DEP_LIBRARIES ${HSA_RUNTIME})
57-
elseif(${hsa-runtime64_FOUND} AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
58-
libomptarget_say("Building AMDGPU NextGen plugin linked against libhsa")
59-
set(LIBOMPTARGET_EXTRA_SOURCE)
60-
set(LIBOMPTARGET_DEP_LIBRARIES hsa-runtime64::hsa-runtime64)
61-
else()
62-
libomptarget_say("Building AMDGPU NextGen plugin for dlopened libhsa")
63-
include_directories(dynamic_hsa)
64-
set(LIBOMPTARGET_EXTRA_SOURCE dynamic_hsa/hsa.cpp)
65-
set(LIBOMPTARGET_DEP_LIBRARIES)
51+
# Check for COMGr package , ASan requires COMGr with minimum version 2.4
52+
if(SANITIZER_AMDGPU)
53+
target_compile_definitions(omptarget.rtl.amdgpu PRIVATE SANITIZER_AMDGPU=1)
54+
find_package(amd_comgr QUIET 2.4.0 HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
6655
endif()
6756

68-
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
69-
# On FreeBSD, the 'environ' symbol is undefined at link time, but resolved by
70-
# the dynamic linker at runtime. Therefore, allow the symbol to be undefined
71-
# when creating a shared library.
72-
set(LDFLAGS_UNDEFINED "-Wl,--allow-shlib-undefined")
57+
option(LIBOMPTARGET_FORCE_DLOPEN_LIBHSA "Build with dlopened libhsa" OFF)
58+
if(DEFINED HSA_RUNTIME AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
59+
libomptarget_say("Building AMDGPU plugin linked against libhsa")
60+
target_link_libraries(omptarget.rtl.amdgpu PRIVATE ${HSA_RUNTIME})
61+
get_filename_component(library_path ${HSA_RUNTIME} DIRECTORY)
62+
target_include_directories(omptarget.rtl.amdgpu PRIVATE ${library_path}/../include)
63+
elseif(hsa-runtime64_FOUND AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
64+
libomptarget_say("Building AMDGPU plugin linked against libhsa")
65+
target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64)
7366
else()
74-
set(LDFLAGS_UNDEFINED "-Wl,-z,defs")
67+
libomptarget_say("Building AMDGPU plugin for dlopened libhsa")
68+
target_include_directories(omptarget.rtl.amdgpu PRIVATE dynamic_hsa)
69+
target_sources(omptarget.rtl.amdgpu PRIVATE dynamic_hsa/hsa.cpp)
7570
endif()
7671

77-
add_llvm_library(omptarget.rtl.amdgpu SHARED
78-
src/rtl.cpp
79-
${LIBOMPTARGET_EXTRA_SOURCE}
80-
81-
ADDITIONAL_HEADER_DIRS
82-
${LIBOMPTARGET_INCLUDE_DIR}
83-
${CMAKE_CURRENT_SOURCE_DIR}/utils
84-
85-
LINK_COMPONENTS
86-
Support
87-
Object
88-
89-
LINK_LIBS
90-
PRIVATE
91-
OMPT
92-
PluginCommon
93-
${LIBOMPTARGET_DEP_LIBRARIES}
94-
${OPENMP_PTHREAD_LIB}
95-
-Wl,--whole-archive amdgcn_hostexec_services -Wl,--no-whole-archive
96-
${LDFLAGS_UNDEFINED}
97-
98-
NO_INSTALL_RPATH
99-
BUILDTREE_ONLY
100-
)
101-
add_dependencies(omptarget.rtl.amdgpu amdgcn_hostexec_services)
102-
103-
if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
104-
target_link_libraries(
105-
omptarget.rtl.amdgpu
106-
PRIVATE
107-
"-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exports")
108-
endif()
109-
110-
target_include_directories(
111-
omptarget.rtl.amdgpu
112-
PRIVATE
113-
${LIBOMPTARGET_INCLUDE_DIR}
114-
${CMAKE_CURRENT_SOURCE_DIR}/utils
115-
)
116-
11772
# Configure testing for the AMDGPU plugin. We will build tests if we could a
11873
# functional AMD GPU on the system, or if manually specifies by the user.
11974
option(LIBOMPTARGET_FORCE_AMDGPU_TESTS "Build AMDGPU libomptarget tests" OFF)
@@ -130,7 +85,6 @@ endif()
13085

13186
# Install plugin under the lib destination folder.
13287
install(TARGETS omptarget.rtl.amdgpu LIBRARY DESTINATION "${OPENMP_INSTALL_LIBDIR}")
133-
13488
if(NOT DEFINED CMAKE_INSTALL_RPATH)
13589
set_target_properties(omptarget.rtl.amdgpu PROPERTIES
13690
INSTALL_RPATH "$ORIGIN")

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

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,14 @@ add_library(PluginCommon OBJECT
1919
src/RPC.cpp
2020
src/Utils/ELF.cpp
2121
)
22+
add_dependencies(PluginCommon intrinsics_gen)
2223

2324
# Only enable JIT for those targets that LLVM can support.
2425
string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" TargetsSupported)
2526
foreach(Target ${TargetsSupported})
2627
target_compile_definitions(PluginCommon PRIVATE "LIBOMPTARGET_JIT_${Target}")
2728
endforeach()
2829

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-
6830
# Include the RPC server from the `libc` project if availible.
6931
if(TARGET llvmlibc_rpc_server AND ${LIBOMPTARGET_GPU_LIBC_SUPPORT})
7032
target_link_libraries(PluginCommon PRIVATE llvmlibc_rpc_server)
@@ -82,8 +44,10 @@ elseif(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
8244
endif()
8345
endif()
8446

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

8953
# Define the TARGET_NAME and DEBUG_PREFIX.
@@ -95,16 +59,11 @@ target_compile_definitions(PluginCommon PRIVATE
9559
target_compile_options(PluginCommon PUBLIC ${offload_compile_flags})
9660
target_link_options(PluginCommon PUBLIC ${offload_link_flags})
9761

98-
target_include_directories(PluginCommon
99-
PRIVATE
100-
${LIBOMPTARGET_INCLUDE_DIR}
101-
PUBLIC
62+
target_include_directories(PluginCommon PUBLIC
10263
${CMAKE_CURRENT_SOURCE_DIR}/include
64+
${LIBOMPTARGET_INCLUDE_DIR}
10365
)
10466

10567
set_target_properties(PluginCommon PROPERTIES
10668
POSITION_INDEPENDENT_CODE ON
10769
CXX_VISIBILITY_PRESET protected)
108-
109-
add_subdirectory(OMPT)
110-

0 commit comments

Comments
 (0)