[llvm] [Offload][omp] Use liboffload queues for AsyncInfo (PR #229713)
Alex Duran via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 03:06:58 PDT 2026
https://github.com/adurang created https://github.com/llvm/llvm-project/pull/229713
Adds a liboffload Context to devices, and a liboffload Queue to AsyncInfo objects. Implements AsyncInfo operations through liboffload.
Other operations still use the internal __tgt_async_info.
Remove synchronize and query_async from GenericPluginTy.
Assisted by Claude.
>From 1f1e4c63ffd1248e18ed5579b4a161c80576d3ec Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Wed, 7 Oct 2026 03:02:34 -0700
Subject: [PATCH] [Offload][omp] Use liboffload queues for AsyncInfo
Adds a liboffload Context to devices, and a liboffload Queue
to AsyncInfo objects. Implements AsyncInfo operations through
liboffload.
Other operations still use the internal __tgt_async_info.
Remove synchronize and query_async from GenericPluginTy.
Assisted by Claude.
---
offload/include/device.h | 4 ++
offload/include/omptarget.h | 14 ++++---
offload/liboffload/src/OffloadImpl.cpp | 4 ++
offload/libompaccsupport/AsyncInfo.cpp | 40 +++++++++++++++++--
offload/libompaccsupport/PluginManager.cpp | 10 ++---
offload/libompaccsupport/device.cpp | 39 +++++++++++++++++-
.../common/include/PluginInterface.h | 6 ---
.../common/src/PluginInterface.cpp | 24 -----------
offload/tools/kernelreplay/CMakeLists.txt | 1 +
9 files changed, 96 insertions(+), 46 deletions(-)
diff --git a/offload/include/device.h b/offload/include/device.h
index 8b53c7fabc4abd6..057f13d15108e91 100644
--- a/offload/include/device.h
+++ b/offload/include/device.h
@@ -91,6 +91,7 @@ struct DeviceTy {
GenericPluginTy *RTL;
int32_t RTLDeviceID;
ol_device_handle_t DeviceHandle;
+ ol_context_handle_t Context = nullptr;
DeviceTy(GenericPluginTy *RTL, int32_t DeviceID, int32_t RTLDeviceID,
ol_device_handle_t DeviceHandle);
@@ -103,6 +104,9 @@ struct DeviceTy {
/// Try to initialize the device and return any failure.
llvm::Error init();
+ /// Deinitialize the OpenMP device.
+ llvm::Error deinit();
+
/// Provide access to the mapping handler.
MappingInfoTy &getMappingInfo() { return MappingInfo; }
diff --git a/offload/include/omptarget.h b/offload/include/omptarget.h
index 6bd61036cc4b34a..a3960980cd20670 100644
--- a/offload/include/omptarget.h
+++ b/offload/include/omptarget.h
@@ -14,6 +14,8 @@
#ifndef _OMPTARGET_H_
#define _OMPTARGET_H_
+#include "OffloadAPI.h"
+
#include "Shared/APITypes.h"
#include "Shared/Environment.h"
#include "Shared/SourceInfo.h"
@@ -130,20 +132,22 @@ class AsyncInfoTy {
using PostProcFuncTy = std::function<int()>;
llvm::SmallVector<PostProcFuncTy> PostProcessingFunctions;
- __tgt_async_info AsyncInfo;
+ ol_queue_handle_t Queue = nullptr;
DeviceTy &Device;
public:
/// Synchronization method to be used.
SyncTy SyncType;
- AsyncInfoTy(DeviceTy &Device, SyncTy SyncType = SyncTy::BLOCKING)
- : Device(Device), SyncType(SyncType) {}
- ~AsyncInfoTy() { synchronize(); }
+ AsyncInfoTy(DeviceTy &Device, SyncTy SyncType = SyncTy::BLOCKING);
+ ~AsyncInfoTy();
/// Implicit conversion to the __tgt_async_info which is used in the
/// plugin interface.
- operator __tgt_async_info *() { return &AsyncInfo; }
+ operator __tgt_async_info *();
+
+ /// Get the underlying queue handle.
+ ol_queue_handle_t getQueue() const { return Queue; }
/// Synchronize all pending actions.
///
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index cff0b5330757931..4597a53cd5e8f91 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1540,6 +1540,10 @@ GenericPluginTy *__ol_tgt_GetPluginFromPlatform(ol_platform_handle_t Platform) {
int32_t __ol_tgt_GetPluginDeviceId(ol_device_handle_t Device) {
return Device->DeviceNum;
}
+
+__tgt_async_info *__ol_tgt_GetAsyncInfoFromQueue(ol_queue_handle_t Queue) {
+ return Queue ? Queue->AsyncInfo : nullptr;
+}
} // namespace tmp
} // namespace offload
diff --git a/offload/libompaccsupport/AsyncInfo.cpp b/offload/libompaccsupport/AsyncInfo.cpp
index 4a08cc67fcb231d..e4551c40d29e789 100644
--- a/offload/libompaccsupport/AsyncInfo.cpp
+++ b/offload/libompaccsupport/AsyncInfo.cpp
@@ -16,6 +16,32 @@
using namespace llvm::omp::target::debug;
+// Temporary helper from liboffload until all usage of AsyncInfo is migrated
+// to liboffload queues.
+namespace llvm::offload::tmp {
+__tgt_async_info *__ol_tgt_GetAsyncInfoFromQueue(ol_queue_handle_t Queue);
+} // namespace llvm::offload::tmp
+
+AsyncInfoTy::AsyncInfoTy(DeviceTy &Device, SyncTy SyncType)
+ : Device(Device), SyncType(SyncType) {
+ if (auto Res = olCreateQueue(Device.Context, Device.DeviceHandle, &Queue)) {
+ REPORT() << "Failed to create queue for device " << Device.DeviceID << ": "
+ << Res->Details;
+ Queue = nullptr;
+ }
+}
+
+AsyncInfoTy::~AsyncInfoTy() {
+ synchronize();
+ if (Queue)
+ if (auto Res = olDestroyQueue(Queue))
+ REPORT() << "Failed to destroy queue " << Queue << ": " << Res->Details;
+}
+
+AsyncInfoTy::operator __tgt_async_info *() {
+ return llvm::offload::tmp::__ol_tgt_GetAsyncInfoFromQueue(Queue);
+}
+
int AsyncInfoTy::synchronize() {
int Result = OFFLOAD_SUCCESS;
if (!isQueueEmpty()) {
@@ -23,9 +49,6 @@ int AsyncInfoTy::synchronize() {
case SyncTy::BLOCKING:
// If we have a queue we need to synchronize it now.
Result = Device.synchronize(*this);
- assert(AsyncInfo.Queue == nullptr &&
- "The device plugin should have nulled the queue to indicate there "
- "are no outstanding actions!");
break;
case SyncTy::NON_BLOCKING:
Result = Device.queryAsync(*this);
@@ -66,4 +89,13 @@ int32_t AsyncInfoTy::runPostProcessing() {
return OFFLOAD_SUCCESS;
}
-bool AsyncInfoTy::isQueueEmpty() const { return AsyncInfo.Queue == nullptr; }
+bool AsyncInfoTy::isQueueEmpty() const {
+ if (!Queue)
+ return true;
+ bool IsComplete;
+ if (auto Res = olQueryQueue(Queue, &IsComplete)) {
+ REPORT() << "Failed to query queue " << Queue << ": " << Res->Details;
+ return false;
+ }
+ return IsComplete;
+}
diff --git a/offload/libompaccsupport/PluginManager.cpp b/offload/libompaccsupport/PluginManager.cpp
index 9a3c9070ae6f04b..f39406558874947 100644
--- a/offload/libompaccsupport/PluginManager.cpp
+++ b/offload/libompaccsupport/PluginManager.cpp
@@ -71,11 +71,11 @@ void PluginManager::deinit() {
TIMESCOPE();
ODBG(ODT_Deinit) << "Unloading RTLs...";
- OMPT_IF_BUILT_AND_INITIALIZED({
- auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor();
- for (DeviceTy &Device : devices(ExclusiveDevicesAccessor))
- performOmptCallback(device_finalize, Device.DeviceID);
- });
+ auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor();
+ for (DeviceTy &Device : devices(ExclusiveDevicesAccessor))
+ if (auto Err = Device.deinit())
+ REPORT() << "Failed to deinitialize device " << Device.DeviceID << ": "
+ << toString(std::move(Err));
Plugins.clear();
if (auto Err = olShutDown())
diff --git a/offload/libompaccsupport/device.cpp b/offload/libompaccsupport/device.cpp
index af30aa757a15a21..5ad8ab99d97d1b9 100644
--- a/offload/libompaccsupport/device.cpp
+++ b/offload/libompaccsupport/device.cpp
@@ -84,6 +84,11 @@ DeviceTy::~DeviceTy() {
}
llvm::Error DeviceTy::init() {
+ if (auto Res = olCreateContext(1, &DeviceHandle, &Context))
+ return error::createOffloadError(
+ error::ErrorCode::BACKEND_FAILURE,
+ "failed to create context for device %d: %s", DeviceID, Res->Details);
+
OMPT_IF_BUILT_AND_INITIALIZED({
GenericDeviceTy &GenericDevice = RTL->getDevice(RTLDeviceID);
std::string ComputeUnitKind = GenericDevice.getComputeUnitKind();
@@ -124,6 +129,20 @@ llvm::Error DeviceTy::init() {
return llvm::Error::success();
}
+llvm::Error DeviceTy::deinit() {
+ OMPT_IF_BUILT_AND_INITIALIZED(
+ { performOmptCallback(device_finalize, DeviceID); });
+
+ if (!Context)
+ return llvm::Error::success();
+ if (auto Res = olDestroyContext(Context))
+ return error::createOffloadError(
+ error::ErrorCode::BACKEND_FAILURE,
+ "failed to destroy context for device %d: %s", DeviceID, Res->Details);
+ Context = nullptr;
+ return llvm::Error::success();
+}
+
// Extract the mapping of host function pointers to device function pointers
// from the entry table. Functions marked as 'indirect' in OpenMP will have
// offloading entries generated for them which map the host's function pointer
@@ -877,11 +896,27 @@ bool DeviceTy::isDataExchangable(const DeviceTy &DstDevice) {
}
int32_t DeviceTy::synchronize(AsyncInfoTy &AsyncInfo) {
- return RTL->synchronize(RTLDeviceID, AsyncInfo);
+ ol_queue_handle_t Queue = AsyncInfo.getQueue();
+ if (!Queue)
+ return OFFLOAD_SUCCESS;
+ if (auto Res = olSyncQueue(Queue)) {
+ REPORT() << "Failure to synchronize stream " << Queue << ": "
+ << Res->Details;
+ return OFFLOAD_FAIL;
+ }
+ return OFFLOAD_SUCCESS;
}
int32_t DeviceTy::queryAsync(AsyncInfoTy &AsyncInfo) {
- return RTL->query_async(RTLDeviceID, AsyncInfo);
+ ol_queue_handle_t Queue = AsyncInfo.getQueue();
+ if (!Queue)
+ return OFFLOAD_SUCCESS;
+ bool IsComplete;
+ if (auto Res = olQueryQueue(Queue, &IsComplete)) {
+ REPORT() << "Failure to query stream " << Queue << ": " << Res->Details;
+ return OFFLOAD_FAIL;
+ }
+ return OFFLOAD_SUCCESS;
}
int32_t DeviceTy::createEvent(void **Event) {
diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index f0c67599c0e1e9d..769005ebc2d6119 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -1714,12 +1714,6 @@ struct GenericPluginTy {
KernelLaunchArgsTy &LaunchArgs,
__tgt_async_info *AsyncInfoPtr);
- /// Synchronize an asyncrhonous queue with the plugin runtime.
- int32_t synchronize(int32_t DeviceId, __tgt_async_info *AsyncInfoPtr);
-
- /// Query the current state of an asynchronous queue.
- int32_t query_async(int32_t DeviceId, __tgt_async_info *AsyncInfoPtr);
-
/// Obtain information about the given device.
InfoTreeNode obtain_device_info(int32_t DeviceId);
diff --git a/offload/plugins-nextgen/common/src/PluginInterface.cpp b/offload/plugins-nextgen/common/src/PluginInterface.cpp
index 01891f5ddbae74e..26d6e0771ef9bcf 100644
--- a/offload/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp
@@ -1427,30 +1427,6 @@ int32_t GenericPluginTy::launch_kernel(int32_t DeviceId, void *TgtEntryPtr,
return OFFLOAD_SUCCESS;
}
-int32_t GenericPluginTy::synchronize(int32_t DeviceId,
- __tgt_async_info *AsyncInfoPtr) {
- auto Err = getDevice(DeviceId).synchronize(AsyncInfoPtr);
- if (Err) {
- REPORT() << "Failure to synchronize stream " << AsyncInfoPtr->Queue << ": "
- << toString(std::move(Err));
- return OFFLOAD_FAIL;
- }
-
- return OFFLOAD_SUCCESS;
-}
-
-int32_t GenericPluginTy::query_async(int32_t DeviceId,
- __tgt_async_info *AsyncInfoPtr) {
- auto Err = getDevice(DeviceId).queryAsync(AsyncInfoPtr);
- if (Err) {
- REPORT() << "Failure to query stream " << AsyncInfoPtr->Queue << ": "
- << toString(std::move(Err));
- return OFFLOAD_FAIL;
- }
-
- return OFFLOAD_SUCCESS;
-}
-
InfoTreeNode GenericPluginTy::obtain_device_info(int32_t DeviceId) {
auto InfoOrErr = getDevice(DeviceId).obtainInfo();
if (auto Err = InfoOrErr.takeError()) {
diff --git a/offload/tools/kernelreplay/CMakeLists.txt b/offload/tools/kernelreplay/CMakeLists.txt
index c789c8ff0185936..e4e359cdf5ff794 100644
--- a/offload/tools/kernelreplay/CMakeLists.txt
+++ b/offload/tools/kernelreplay/CMakeLists.txt
@@ -6,6 +6,7 @@ add_offload_tool(llvm-omp-kernel-replay llvm-omp-kernel-replay.cpp
target_include_directories(llvm-omp-kernel-replay PRIVATE
${LIBOMPTARGET_INCLUDE_DIR}
+ ${CMAKE_CURRENT_BINARY_DIR}/../../liboffload/API
)
target_link_libraries(llvm-omp-kernel-replay PRIVATE
omp
More information about the llvm-commits
mailing list