Skip to content

Commit 36aa5f4

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 72b0c11 commit 36aa5f4

File tree

6 files changed

+64
-32
lines changed

6 files changed

+64
-32
lines changed

offload/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
151151
message(FATAL_ERROR "Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS")
152152
endif()
153153

154+
set(LIBOMPTARGET_ALL_PLUGIN_TARGETS amdgpu cuda host)
155+
set(LIBOMPTARGET_PLUGINS_TO_BUILD "all" CACHE STRING
156+
"Semicolon-separated list of plugins to use, or \"all\".")
157+
158+
if(LIBOMPTARGET_PLUGINS_TO_BUILD STREQUAL "all")
159+
set(LIBOMPTARGET_PLUGINS_TO_BUILD ${LIBOMPTARGET_ALL_PLUGIN_TARGETS})
160+
endif()
161+
162+
set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS "")
163+
foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
164+
set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS
165+
"${LIBOMPTARGET_ENUM_PLUGIN_TARGETS}PLUGIN_TARGET(${plugin})\n")
166+
endforeach()
167+
string(STRIP ${LIBOMPTARGET_ENUM_PLUGIN_TARGETS} LIBOMPTARGET_ENUM_PLUGIN_TARGETS)
168+
configure_file(
169+
${CMAKE_CURRENT_SOURCE_DIR}/include/Shared/Targets.def.in
170+
${CMAKE_CURRENT_BINARY_DIR}/include/Shared/Targets.def
171+
)
172+
154173
include_directories(${LIBOMPTARGET_LLVM_INCLUDE_DIRS})
155174

156175
# This is a list of all the targets that are supported/tested right now.
@@ -288,6 +307,7 @@ set(LIBOMPTARGET_GPU_LIBC_SUPPORT ${LLVM_LIBC_GPU_BUILD} CACHE BOOL
288307
pythonize_bool(LIBOMPTARGET_GPU_LIBC_SUPPORT)
289308

290309
set(LIBOMPTARGET_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
310+
set(LIBOMPTARGET_BINARY_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
291311
message(STATUS "OpenMP tools dir in libomptarget: ${LIBOMP_OMP_TOOLS_INCLUDE_DIR}")
292312
if(LIBOMP_OMP_TOOLS_INCLUDE_DIR)
293313
include_directories(${LIBOMP_OMP_TOOLS_INCLUDE_DIR})

offload/include/Shared/Targets.def.in

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

offload/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)

offload/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

offload/src/CMakeLists.txt

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

3838
ADDITIONAL_HEADER_DIRS
3939
${LIBOMPTARGET_INCLUDE_DIR}
40+
${LIBOMPTARGET_BINARY_INCLUDE_DIR}
4041

4142
LINK_COMPONENTS
4243
Support
@@ -49,7 +50,9 @@ add_llvm_library(omptarget
4950
NO_INSTALL_RPATH
5051
BUILDTREE_ONLY
5152
)
52-
target_include_directories(omptarget PRIVATE ${LIBOMPTARGET_INCLUDE_DIR})
53+
target_include_directories(omptarget PRIVATE
54+
${LIBOMPTARGET_INCLUDE_DIR} ${LIBOMPTARGET_BINARY_INCLUDE_DIR}
55+
)
5356

5457
if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
5558
target_link_libraries(omptarget PRIVATE
@@ -65,20 +68,6 @@ target_compile_definitions(omptarget PRIVATE
6568
target_compile_options(omptarget PUBLIC ${offload_compile_flags})
6669
target_link_options(omptarget PUBLIC ${offload_link_flags})
6770

68-
macro(check_plugin_target target)
69-
if (TARGET omptarget.rtl.${target})
70-
list(APPEND LIBOMPTARGET_PLUGINS_TO_LOAD ${target})
71-
endif()
72-
endmacro()
73-
74-
set(LIBOMPTARGET_PLUGINS_TO_LOAD "" CACHE STRING
75-
"Comma separated list of plugin names to look for at runtime")
76-
if (NOT LIBOMPTARGET_PLUGINS_TO_LOAD)
77-
check_plugin_target(cuda)
78-
check_plugin_target(amdgpu)
79-
check_plugin_target(host)
80-
endif()
81-
8271
list(TRANSFORM LIBOMPTARGET_PLUGINS_TO_LOAD PREPEND "\"libomptarget.rtl.")
8372
list(TRANSFORM LIBOMPTARGET_PLUGINS_TO_LOAD APPEND "\"")
8473
list(JOIN LIBOMPTARGET_PLUGINS_TO_LOAD "," ENABLED_OFFLOAD_PLUGINS)

offload/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)