Skip to content

[Libomptarget] Rework interface for enabling plugins #86875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions offload/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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: cuda, amdgpu, host 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 @@ -288,6 +307,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}")
if(LIBOMP_OMP_TOOLS_INCLUDE_DIR)
include_directories(${LIBOMP_OMP_TOOLS_INCLUDE_DIR})
Expand Down
20 changes: 20 additions & 0 deletions offload/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 offload/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 offload/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 offload/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ add_llvm_library(omptarget

ADDITIONAL_HEADER_DIRS
${LIBOMPTARGET_INCLUDE_DIR}
${LIBOMPTARGET_BINARY_INCLUDE_DIR}

LINK_COMPONENTS
Support
Expand All @@ -49,7 +50,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 @@ -65,20 +68,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 offload/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