Skip to content

Commit

Permalink
[Libomptarget] Rework interface for enabling plugins
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jhuber6 committed Mar 28, 2024
1 parent fe0b672 commit b7059c7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 32 deletions.
20 changes: 20 additions & 0 deletions openmp/libomptarget/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
message(FATAL_ERROR "Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS")
endif()

set(LIBOMPTARGET_ALL_PLUGIN_TARGETS amdgpu cuda host)
set(LIBOMPTARGET_PLUGINS_TO_BUILD "all" CACHE STRING
"Semicolon-separated list of plugins to use, or \"all\".")

if(LIBOMPTARGET_PLUGINS_TO_BUILD STREQUAL "all")
set(LIBOMPTARGET_PLUGINS_TO_BUILD ${LIBOMPTARGET_ALL_PLUGIN_TARGETS})
endif()

set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS "")
foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
set(LIBOMPTARGET_ENUM_PLUGIN_TARGETS
"${LIBOMPTARGET_ENUM_PLUGIN_TARGETS}PLUGIN_TARGET(${plugin})\n")
endforeach()
string(STRIP ${LIBOMPTARGET_ENUM_PLUGIN_TARGETS} LIBOMPTARGET_ENUM_PLUGIN_TARGETS)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/Shared/Targets.def.in
${CMAKE_CURRENT_BINARY_DIR}/include/Shared/Targets.def
)

include_directories(${LIBOMPTARGET_LLVM_INCLUDE_DIRS})

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

set(LIBOMPTARGET_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(LIBOMPTARGET_BINARY_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
message(STATUS "OpenMP tools dir in libomptarget: ${LIBOMP_OMP_TOOLS_INCLUDE_DIR}")
include_directories(${LIBOMP_OMP_TOOLS_INCLUDE_DIR})

Expand Down
20 changes: 20 additions & 0 deletions openmp/libomptarget/include/Shared/Targets.def.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Shared/Targets.def - Target plugin enumerator -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Enumerates over all of the supported target plugins that are available to
// the offloading library.
//
//===----------------------------------------------------------------------===//

#ifndef PLUGIN_TARGET
# error Please define the macro PLUGIN_TARGET(TargetName)
#endif

@LIBOMPTARGET_ENUM_PLUGIN_TARGETS@

#undef PLUGIN_TARGET
9 changes: 6 additions & 3 deletions openmp/libomptarget/plugins-nextgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ function(add_target_library target_name lib_name)
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET protected)
endfunction()

add_subdirectory(amdgpu)
add_subdirectory(cuda)
add_subdirectory(host)
foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${plugin})
message(FATAL_ERROR "Unknown plugin target '${plugin}'")
endif()
add_subdirectory(${plugin})
endforeach()

# Make sure the parent scope can see the plugins that will be created.
set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
Expand Down
1 change: 1 addition & 0 deletions openmp/libomptarget/plugins-nextgen/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ target_link_options(PluginCommon PUBLIC ${offload_link_flags})
target_include_directories(PluginCommon PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${LIBOMPTARGET_LLVM_INCLUDE_DIRS}
${LIBOMPTARGET_BINARY_INCLUDE_DIR}
${LIBOMPTARGET_INCLUDE_DIR}
)

Expand Down
19 changes: 4 additions & 15 deletions openmp/libomptarget/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_llvm_library(omptarget

ADDITIONAL_HEADER_DIRS
${LIBOMPTARGET_INCLUDE_DIR}
${LIBOMPTARGET_BINARY_INCLUDE_DIR}

LINK_COMPONENTS
Support
Expand All @@ -43,7 +44,9 @@ add_llvm_library(omptarget
NO_INSTALL_RPATH
BUILDTREE_ONLY
)
target_include_directories(omptarget PRIVATE ${LIBOMPTARGET_INCLUDE_DIR})
target_include_directories(omptarget PRIVATE
${LIBOMPTARGET_INCLUDE_DIR} ${LIBOMPTARGET_BINARY_INCLUDE_DIR}
)

if (LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
target_link_libraries(omptarget PRIVATE
Expand All @@ -59,20 +62,6 @@ target_compile_definitions(omptarget PRIVATE
target_compile_options(omptarget PUBLIC ${offload_compile_flags})
target_link_options(omptarget PUBLIC ${offload_link_flags})

macro(check_plugin_target target)
if (TARGET omptarget.rtl.${target})
list(APPEND LIBOMPTARGET_PLUGINS_TO_LOAD ${target})
endif()
endmacro()

set(LIBOMPTARGET_PLUGINS_TO_LOAD "" CACHE STRING
"Comma separated list of plugin names to look for at runtime")
if (NOT LIBOMPTARGET_PLUGINS_TO_LOAD)
check_plugin_target(cuda)
check_plugin_target(amdgpu)
check_plugin_target(host)
endif()

list(TRANSFORM LIBOMPTARGET_PLUGINS_TO_LOAD PREPEND "\"libomptarget.rtl.")
list(TRANSFORM LIBOMPTARGET_PLUGINS_TO_LOAD APPEND "\"")
list(JOIN LIBOMPTARGET_PLUGINS_TO_LOAD "," ENABLED_OFFLOAD_PLUGINS)
Expand Down
27 changes: 13 additions & 14 deletions openmp/libomptarget/src/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ using namespace llvm::sys;

PluginManager *PM = nullptr;

// List of all plugins that can support offloading.
static const char *RTLNames[] = {ENABLED_OFFLOAD_PLUGINS};

Expected<std::unique_ptr<PluginAdaptorTy>>
PluginAdaptorTy::create(const std::string &Name) {
DP("Attempting to load library '%s'...\n", Name.c_str());
Expand Down Expand Up @@ -95,17 +92,19 @@ void PluginManager::init() {

// Attempt to open all the plugins and, if they exist, check if the interface
// is correct and if they are supporting any devices.
for (const char *Name : RTLNames) {
auto PluginAdaptorOrErr =
PluginAdaptorTy::create(std::string(Name) + ".so");
if (!PluginAdaptorOrErr) {
[[maybe_unused]] std::string InfoMsg =
toString(PluginAdaptorOrErr.takeError());
DP("%s", InfoMsg.c_str());
} else {
PluginAdaptors.push_back(std::move(*PluginAdaptorOrErr));
}
}
#define PLUGIN_TARGET(Name) \
do { \
auto PluginAdaptorOrErr = \
PluginAdaptorTy::create("libomptarget.rtl." #Name ".so"); \
if (!PluginAdaptorOrErr) { \
[[maybe_unused]] std::string InfoMsg = \
toString(PluginAdaptorOrErr.takeError()); \
DP("%s", InfoMsg.c_str()); \
} else { \
PluginAdaptors.push_back(std::move(*PluginAdaptorOrErr)); \
} \
} while (false);
#include "Shared/Targets.def"

DP("RTLs loaded!\n");
}
Expand Down

0 comments on commit b7059c7

Please sign in to comment.