[llvm] Use liboffload to initialize libomptarget plugins (PR #220260)
Alex Duran via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 1 07:26:25 PDT 2026
https://github.com/adurang updated https://github.com/llvm/llvm-project/pull/220260
>From a8aa0075869b2cdd5c4669cae456f2f0ef81363f Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Tue, 1 Sep 2026 07:18:37 -0700
Subject: [PATCH 1/2] Use liboffload to initialize libomptarget plugins
---
offload/include/PluginManager.h | 2 +-
offload/liboffload/API/Common.td | 5 ++-
offload/liboffload/exports | 6 +++
offload/liboffload/src/OffloadImpl.cpp | 27 +++++++++---
offload/libomptarget/CMakeLists.txt | 13 +++---
offload/libomptarget/PluginManager.cpp | 43 +++++++++----------
offload/libomptarget/exports | 5 +++
offload/plugins-nextgen/common/CMakeLists.txt | 4 ++
8 files changed, 68 insertions(+), 37 deletions(-)
diff --git a/offload/include/PluginManager.h b/offload/include/PluginManager.h
index 6c6fdebe76dff..4de9c85174a78 100644
--- a/offload/include/PluginManager.h
+++ b/offload/include/PluginManager.h
@@ -155,7 +155,7 @@ struct PluginManager {
llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;
// List of all plugins, in use or not.
- llvm::SmallVector<std::unique_ptr<GenericPluginTy>> Plugins;
+ llvm::SmallVector<GenericPluginTy *> Plugins;
// Mapping of plugins to the OpenMP device identifier.
llvm::DenseMap<std::pair<const GenericPluginTy *, int32_t>, int32_t>
diff --git a/offload/liboffload/API/Common.td b/offload/liboffload/API/Common.td
index f09ee57961d67..1f1401f936cfc 100644
--- a/offload/liboffload/API/Common.td
+++ b/offload/liboffload/API/Common.td
@@ -154,13 +154,14 @@ def ol_init_args_t : Struct {
let members = [
StructMember<"size_t", "Size", "Size of this struct, used for ABI compatibility. Must be set to sizeof(ol_init_args_t) by the caller.">,
StructMember<"uint32_t", "NumPlatforms", "Number of entries in the Platforms array.">,
- StructMember<"const ol_platform_backend_t*", "Platforms", "Pointer to an array of platform backends to initialize.">
+ StructMember<"const ol_platform_backend_t*", "Platforms", "Pointer to an array of platform backends to initialize.">,
+ StructMember<"bool", "InitDevices", "Initialize devices of the platform.">
];
}
def OL_INIT_ARGS_INIT : Macro {
let desc = "Default initializer for ol_init_args_t. Sets Size to the correct value and all other fields to zero/NULL.";
- let value = "{ sizeof(ol_init_args_t), 0, NULL }";
+ let value = "{ sizeof(ol_init_args_t), 0, NULL, true }";
}
def olInit : Function {
diff --git a/offload/liboffload/exports b/offload/liboffload/exports
index 168341aa7d938..4288d67d86877 100644
--- a/offload/liboffload/exports
+++ b/offload/liboffload/exports
@@ -1,6 +1,12 @@
VERS1.0 {
global:
ol*;
+ extern "C++" {
+ llvm::omp::target::plugin::*;
+ llvm::omp::target::RPCServerTy::*;
+ llvm::omp::target::ompt::*;
+ error::*;
+ };
local:
*;
};
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 38ecb27ed905d..310d7f45a658a 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -29,9 +29,9 @@
#ifdef OMPT_SUPPORT
namespace llvm::omp::target {
namespace ompt {
-bool Initialized = false;
-ompt_get_callback_t lookupCallbackByCode = nullptr;
-ompt_function_lookup_t lookupCallbackByName = nullptr;
+LLVM_ATTRIBUTE_WEAK bool Initialized = false;
+LLVM_ATTRIBUTE_WEAK ompt_get_callback_t lookupCallbackByCode = nullptr;
+LLVM_ATTRIBUTE_WEAK ompt_function_lookup_t lookupCallbackByName = nullptr;
} // namespace ompt
} // namespace llvm::omp::target
#endif
@@ -54,7 +54,7 @@ struct ol_platform_impl_t {
llvm::Error destroy();
/// Initialize the associated plugin and devices.
- llvm::Error init();
+ llvm::Error init(bool InitDevices);
/// Direct access to the plugin, may be uninitialized if accessed here.
std::unique_ptr<GenericPluginTy> Plugin;
@@ -137,12 +137,14 @@ llvm::Error ol_platform_impl_t::destroy() {
return Result;
}
-llvm::Error ol_platform_impl_t::init() {
+llvm::Error ol_platform_impl_t::init(bool InitDevices) {
if (!Plugin)
return llvm::Error::success();
if (llvm::Error Err = Plugin->init())
return Err;
+ if (!InitDevices)
+ return llvm::Error::success();
for (auto Id = 0, End = Plugin->getNumDevices(); Id != End; Id++) {
if (llvm::Error Err = Plugin->initDevice(Id))
@@ -268,6 +270,19 @@ bool isTracingEnabled() {
bool isValidationEnabled() { return OffloadContext::get().ValidationEnabled; }
bool isOffloadInitialized() { return OffloadContextVal != nullptr; }
+// Temporary helpers to be able to transitions from the liboffload API to the plugin API.
+// Only to be used to assist in the libomptarget transition. These will be removed once
+// the transition is complete.
+extern "C" size_t olGetInitializedPluginCount() {
+ if (!isOffloadInitialized())
+ return 0;
+ return OffloadContext::get().Platforms.size();
+}
+extern "C" GenericPluginTy *olGetInitializedPlugin(size_t Index) {
+ return OffloadContext::get().Platforms[Index]->Plugin.get();
+}
+// End of temporary helpers
+
template <typename HandleT> Error olDestroy(HandleT Handle) {
delete Handle;
return Error::success();
@@ -314,7 +329,7 @@ Error initPlugins(OffloadContext &Context, const ol_init_args_t *InitArgs) {
// that the platform is initialized at a consistent point to maintain the
// expected teardown order in the vendor libraries.
for (auto &Platform : Context.Platforms) {
- if (Error Err = Platform->init())
+ if (Error Err = Platform->init(InitArgs ? InitArgs->InitDevices : true))
return Err;
}
diff --git a/offload/libomptarget/CMakeLists.txt b/offload/libomptarget/CMakeLists.txt
index 8e6314b2a6eae..a0b2cb431c69e 100644
--- a/offload/libomptarget/CMakeLists.txt
+++ b/offload/libomptarget/CMakeLists.txt
@@ -24,11 +24,16 @@ add_library(omptarget SHARED
if(LLVM_LINK_LLVM_DYLIB)
set(llvm_libs LLVM)
else()
- llvm_map_components_to_libnames(llvm_libs FrontendOpenMP Support Object)
+ llvm_map_components_to_libnames(llvm_libs FrontendOpenMP Support Object Offload)
endif()
target_link_libraries(omptarget PRIVATE omp ${llvm_libs})
+target_link_libraries(omptarget PRIVATE LLVMOffload)
target_include_directories(omptarget PRIVATE
- ${LIBOMPTARGET_INCLUDE_DIR} ${LIBOMPTARGET_BINARY_INCLUDE_DIR}
+ ${LIBOMPTARGET_INCLUDE_DIR}
+ ${LIBOMPTARGET_BINARY_INCLUDE_DIR}
+ ${CMAKE_CURRENT_BINARY_DIR}/../liboffload/API
+ ${CMAKE_CURRENT_SOURCE_DIR}/../plugins-nextgen/common/include
+ ${CMAKE_CURRENT_BINARY_DIR}/../plugins-nextgen/common/include
)
if(LLVM_HAVE_LINK_VERSION_SCRIPT)
@@ -41,10 +46,6 @@ target_compile_definitions(omptarget PRIVATE
DEBUG_PREFIX="omptarget"
)
-foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
- target_link_libraries(omptarget PRIVATE omptarget.rtl.${plugin})
-endforeach()
-
target_compile_options(omptarget PRIVATE ${offload_compile_flags})
target_link_options(omptarget PRIVATE ${offload_link_flags})
diff --git a/offload/libomptarget/PluginManager.cpp b/offload/libomptarget/PluginManager.cpp
index 41b653a60adfd..81e61cf375aa5 100644
--- a/offload/libomptarget/PluginManager.cpp
+++ b/offload/libomptarget/PluginManager.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "PluginManager.h"
+#include "OffloadAPI.h"
#include "OffloadPolicy.h"
#include "Shared/Debug.h"
#include "Shared/Profile.h"
@@ -24,11 +25,12 @@ using namespace llvm;
using namespace llvm::sys;
using namespace llvm::omp::target::debug;
-PluginManager *PM = nullptr;
+// Temporary helpers from liboffload to allow to use both liboffoad and
+// the plugin interface at the same time.
+extern "C" size_t olGetInitializedPluginCount();
+extern "C" GenericPluginTy *olGetInitializedPlugin(size_t Index);
-// Every plugin exports this method to create an instance of the plugin type.
-#define PLUGIN_TARGET(Name) extern "C" GenericPluginTy *createPlugin_##Name();
-#include "Shared/Targets.def"
+PluginManager *PM = nullptr;
void PluginManager::init() {
TIMESCOPE();
@@ -38,14 +40,17 @@ void PluginManager::init() {
}
ODBG(ODT_Init) << "Loading RTLs";
-
- // Attempt to create an instance of each supported plugin.
-#define PLUGIN_TARGET(Name) \
- do { \
- Plugins.emplace_back( \
- std::unique_ptr<GenericPluginTy>(createPlugin_##Name())); \
- } while (false);
-#include "Shared/Targets.def"
+ ol_init_args_t InitArgs = OL_INIT_ARGS_INIT;
+ InitArgs.InitDevices = false;
+ if (ol_result_t Res = olInit(&InitArgs))
+ REPORT() << "Failed to initialize liboffload: "
+ << (Res->Details ? Res->Details : "(no details)");
+
+ for (size_t I = 0, End = olGetInitializedPluginCount(); I != End; ++I) {
+ GenericPluginTy *Plugin = olGetInitializedPlugin(I);
+ ODBG(ODT_Init) << "Adding plugin " << Plugin->getName() << " from liboffload";
+ Plugins.push_back(Plugin);
+ }
ODBG(ODT_Init) << "RTLs loaded!";
}
@@ -54,16 +59,10 @@ void PluginManager::deinit() {
TIMESCOPE();
ODBG(ODT_Deinit) << "Unloading RTLs...";
- for (auto &Plugin : Plugins) {
- if (!Plugin->is_initialized())
- continue;
-
- if (auto Err = Plugin->deinit()) {
- std::string InfoMsg = toString(std::move(Err));
- ODBG(ODT_Deinit) << "Failed to deinit plugin: " << InfoMsg;
- }
- Plugin.release();
- }
+ Plugins.clear();
+ if (ol_result_t Res = olShutDown())
+ REPORT() << "Failed to shut down liboffload: "
+ << (Res->Details ? Res->Details : "(no details)");
ODBG(ODT_Deinit) << "RTLs unloaded!";
}
diff --git a/offload/libomptarget/exports b/offload/libomptarget/exports
index 1831c43cc5f29..fb5f88e9dcb2b 100644
--- a/offload/libomptarget/exports
+++ b/offload/libomptarget/exports
@@ -83,6 +83,11 @@ VERS1.0 {
__llvmPushCallConfiguration;
__llvmPopCallConfiguration;
llvmLaunchKernel;
+ extern "C++" {
+ llvm::omp::target::ompt::Initialized;
+ llvm::omp::target::ompt::lookupCallbackByCode;
+ llvm::omp::target::ompt::lookupCallbackByName;
+ };
local:
*;
};
diff --git a/offload/plugins-nextgen/common/CMakeLists.txt b/offload/plugins-nextgen/common/CMakeLists.txt
index 6ad20796c3ca4..9e20294187638 100644
--- a/offload/plugins-nextgen/common/CMakeLists.txt
+++ b/offload/plugins-nextgen/common/CMakeLists.txt
@@ -45,6 +45,10 @@ target_compile_definitions(PluginCommon PRIVATE
target_compile_options(PluginCommon PUBLIC ${offload_compile_flags})
target_link_options(PluginCommon PUBLIC ${offload_link_flags})
+set_target_properties(PluginCommon PROPERTIES
+ CXX_VISIBILITY_PRESET default
+ C_VISIBILITY_PRESET default)
+
target_include_directories(PluginCommon PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/include
>From cab056a44e849d71b07be9edc967ba3a959639c7 Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Tue, 1 Sep 2026 07:24:38 -0700
Subject: [PATCH 2/2] format
---
offload/liboffload/src/OffloadImpl.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 310d7f45a658a..1c76fe8efc433 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -270,9 +270,9 @@ bool isTracingEnabled() {
bool isValidationEnabled() { return OffloadContext::get().ValidationEnabled; }
bool isOffloadInitialized() { return OffloadContextVal != nullptr; }
-// Temporary helpers to be able to transitions from the liboffload API to the plugin API.
-// Only to be used to assist in the libomptarget transition. These will be removed once
-// the transition is complete.
+// Temporary helpers to be able to transitions from the liboffload API to the
+// plugin API. Only to be used to assist in the libomptarget transition. These
+// will be removed once the transition is complete.
extern "C" size_t olGetInitializedPluginCount() {
if (!isOffloadInitialized())
return 0;
More information about the llvm-commits
mailing list