Skip to content

Commit 586effa

Browse files
committed
[Libomptarget] Rework interface for enabling plugins
Summary: Previously we would build all of the plugins by default and then only load some using the `LIBOMPTARGET_PLUGINS_TO_LOAD` variable. This patch renamed this to `LIBOMPTARGET_PLUGINS_TO_BUILD` and changes whether or not it will include the plugin in CMake. Additionally this patch creates a new `Targets.def` file that allows us to enumerate all of the enabled plugins. This is somewhat different from the old method, and it's done this way for future use that will need to be shared. This follows the same method that LLVM uses for its targets, however it does require adding an extra include path. Depends on llvm#86868
1 parent bb5f330 commit 586effa

File tree

6 files changed

+64
-32
lines changed

6 files changed

+64
-32
lines changed

openmp/libomptarget/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
4141
message(FATAL_ERROR "Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS")
4242
endif()
4343

44+
set(LIBOMPTARGET_ALL_PLUGIN_TARGETS amdgpu cuda host)
45+
set(LIBOMPTARGET_PLUGINS_TO_BUILD "all" CACHE STRING
46+
"Semicolon-separated list of plugins to use, or \"all\".")
47+
48+
if(LIBOMPTARGET_PLUGINS_TO_BUILD STREQUAL "all")
49+
set(LIBOMPTARGET_PLUGINS_TO_BUILD ${LIBOMPTARGET_ALL_PLUGIN_TARGETS})
50+
endif()
51+
52+
set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS "")
53+
foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
54+
set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS
55+
"${LIBOMPTARGET_ENUM_PLUGIN_TARGETS}PLUGIN_TARGET(${plugin})\n")
56+
endforeach()
57+
string(STRIP ${LIBOMPTARGET_ENUM_PLUGIN_TARGETS} LIBOMPTARGET_ENUM_PLUGIN_TARGETS)
58+
configure_file(
59+
${CMAKE_CURRENT_SOURCE_DIR}/include/Shared/Targets.def.in
60+
${CMAKE_CURRENT_BINARY_DIR}/include/Shared/Targets.def
61+
)
62+
4463
include_directories(${LIBOMPTARGET_LLVM_INCLUDE_DIRS})
4564

4665
# This is a list of all the targets that are supported/tested right now.
@@ -126,6 +145,7 @@ set(LIBOMPTARGET_GPU_LIBC_SUPPORT ${LLVM_LIBC_GPU_BUILD} CACHE BOOL
126145
pythonize_bool(LIBOMPTARGET_GPU_LIBC_SUPPORT)
127146

128147
set(LIBOMPTARGET_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
148+
set(LIBOMPTARGET_BINARY_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
129149
message(STATUS "OpenMP tools dir in libomptarget: ${LIBOMP_OMP_TOOLS_INCLUDE_DIR}")
130150
include_directories(${LIBOMP_OMP_TOOLS_INCLUDE_DIR})
131151

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Shared/Targets.def - Target plugin enumerator -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Enumerates over all of the supported target plugins that are available to
10+
// the offloading library.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef PLUGIN_TARGET
15+
# error Please define the macro PLUGIN_TARGET(TargetName)
16+
#endif
17+
18+
@LIBOMPTARGET_ENUM_PLUGIN_TARGETS@
19+
20+
#undef PLUGIN_TARGET

openmp/libomptarget/plugins-nextgen/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ function(add_target_library target_name lib_name)
6969
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET protected)
7070
endfunction()
7171

72-
add_subdirectory(amdgpu)
73-
add_subdirectory(cuda)
74-
add_subdirectory(host)
72+
foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
73+
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${plugin})
74+
message(FATAL_ERROR "Unknown plugin target '${plugin}'")
75+
endif()
76+
add_subdirectory(${plugin})
77+
endforeach()
7578

7679
# Make sure the parent scope can see the plugins that will be created.
7780
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ target_link_options(PluginCommon PUBLIC ${offload_link_flags})
6262
target_include_directories(PluginCommon PUBLIC
6363
${CMAKE_CURRENT_SOURCE_DIR}/include
6464
${LIBOMPTARGET_LLVM_INCLUDE_DIRS}
65+
${LIBOMPTARGET_BINARY_INCLUDE_DIR}
6566
${LIBOMPTARGET_INCLUDE_DIR}
6667
)
6768

openmp/libomptarget/src/CMakeLists.txt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_llvm_library(omptarget
3131

3232
ADDITIONAL_HEADER_DIRS
3333
${LIBOMPTARGET_INCLUDE_DIR}
34+
${LIBOMPTARGET_BINARY_INCLUDE_DIR}
3435

3536
LINK_COMPONENTS
3637
Support
@@ -43,7 +44,9 @@ add_llvm_library(omptarget
4344
NO_INSTALL_RPATH
4445
BUILDTREE_ONLY
4546
)
46-
target_include_directories(omptarget PRIVATE ${LIBOMPTARGET_INCLUDE_DIR})
47+
target_include_directories(omptarget PRIVATE
48+
${LIBOMPTARGET_INCLUDE_DIR} ${LIBOMPTARGET_BINARY_INCLUDE_DIR}
49+
)
4750

4851
if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
4952
target_link_libraries(omptarget PRIVATE
@@ -59,20 +62,6 @@ target_compile_definitions(omptarget PRIVATE
5962
target_compile_options(omptarget PUBLIC ${offload_compile_flags})
6063
target_link_options(omptarget PUBLIC ${offload_link_flags})
6164

62-
macro(check_plugin_target target)
63-
if (TARGET omptarget.rtl.${target})
64-
list(APPEND LIBOMPTARGET_PLUGINS_TO_LOAD ${target})
65-
endif()
66-
endmacro()
67-
68-
set(LIBOMPTARGET_PLUGINS_TO_LOAD "" CACHE STRING
69-
"Comma separated list of plugin names to look for at runtime")
70-
if (NOT LIBOMPTARGET_PLUGINS_TO_LOAD)
71-
check_plugin_target(cuda)
72-
check_plugin_target(amdgpu)
73-
check_plugin_target(host)
74-
endif()
75-
7665
list(TRANSFORM LIBOMPTARGET_PLUGINS_TO_LOAD PREPEND "\"libomptarget.rtl.")
7766
list(TRANSFORM LIBOMPTARGET_PLUGINS_TO_LOAD APPEND "\"")
7867
list(JOIN LIBOMPTARGET_PLUGINS_TO_LOAD "," ENABLED_OFFLOAD_PLUGINS)

openmp/libomptarget/src/PluginManager.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ using namespace llvm::sys;
2323

2424
PluginManager *PM = nullptr;
2525

26-
// List of all plugins that can support offloading.
27-
static const char *RTLNames[] = {ENABLED_OFFLOAD_PLUGINS};
28-
2926
Expected<std::unique_ptr<PluginAdaptorTy>>
3027
PluginAdaptorTy::create(const std::string &Name) {
3128
DP("Attempting to load library '%s'...\n", Name.c_str());
@@ -95,17 +92,19 @@ void PluginManager::init() {
9592

9693
// Attempt to open all the plugins and, if they exist, check if the interface
9794
// is correct and if they are supporting any devices.
98-
for (const char *Name : RTLNames) {
99-
auto PluginAdaptorOrErr =
100-
PluginAdaptorTy::create(std::string(Name) + ".so");
101-
if (!PluginAdaptorOrErr) {
102-
[[maybe_unused]] std::string InfoMsg =
103-
toString(PluginAdaptorOrErr.takeError());
104-
DP("%s", InfoMsg.c_str());
105-
} else {
106-
PluginAdaptors.push_back(std::move(*PluginAdaptorOrErr));
107-
}
108-
}
95+
#define PLUGIN_TARGET(Name) \
96+
do { \
97+
auto PluginAdaptorOrErr = \
98+
PluginAdaptorTy::create("libomptarget.rtl." #Name ".so"); \
99+
if (!PluginAdaptorOrErr) { \
100+
[[maybe_unused]] std::string InfoMsg = \
101+
toString(PluginAdaptorOrErr.takeError()); \
102+
DP("%s", InfoMsg.c_str()); \
103+
} else { \
104+
PluginAdaptors.push_back(std::move(*PluginAdaptorOrErr)); \
105+
} \
106+
} while (false);
107+
#include "Shared/Targets.def"
109108

110109
DP("RTLs loaded!\n");
111110
}

0 commit comments

Comments
 (0)