[llvm] [offload] Add context parameter to olCreateQueue (PR #213057)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 30 08:57:46 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-offload

Author: Ɓukasz Plewa (lplewa)

<details>
<summary>Changes</summary>

Second patch in the context series.

Outstanding-queue bookkeeping (queues destroyed while still busy) moves from ol_device_impl_t to ol_context_impl_t, keyed by device. This keeps backend queue resources from being reused across native contexts and lets olDestroyContext drain everything it owns.

On the Level Zero side, LevelZeroPluginContextTy owns a ze_context and an L0QueueCacheTy pooling idle queues per device bound to that ze_context. L0ContextTy holds a DefaultUserCtx used by the libomptarget path when no user context is set on the async info. Queue creation, command-list creation and initAsyncInfoImpl all thread the plugin context through so queues are bound to the right ze_context.

Note: this patch temporarily passes null as the context in SYCL. @<!-- -->KseniyaTikhomirova is currently working on proper context management in libsycl - once that is implemented, this PR will be updated.

Assisted-by: Claude

---

Patch is 50.76 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/213057.diff


33 Files Affected:

- (modified) libsycl/src/detail/queue_impl.cpp (+2-1) 
- (modified) libsycl/unittests/mock/helpers.cpp (+3-1) 
- (modified) libsycl/unittests/mock/helpers.hpp (+2-1) 
- (modified) libsycl/unittests/mock/mock.cpp (+3-2) 
- (modified) libsycl/unittests/queue/queue.cpp (+1-1) 
- (modified) llvm/tools/llvm-gpu-loader/llvm-gpu-loader.cpp (+5-1) 
- (modified) llvm/tools/llvm-gpu-loader/llvm-gpu-loader.h (+10-1) 
- (modified) offload/liboffload/API/Queue.td (+12-3) 
- (modified) offload/liboffload/src/OffloadImpl.cpp (+84-64) 
- (modified) offload/plugins-nextgen/common/include/PluginInterface.h (+11-3) 
- (modified) offload/plugins-nextgen/common/src/PluginInterface.cpp (+7-4) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Context.h (+11-3) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Device.h (+13-10) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Plugin.h (+10-1) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Queue.h (+12-8) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Context.cpp (+12) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Device.cpp (+22-9) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Plugin.cpp (+15) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Queue.cpp (+18-14) 
- (modified) offload/unittests/Conformance/include/mathtest/DeviceContext.hpp (+3) 
- (modified) offload/unittests/Conformance/include/mathtest/OffloadForward.hpp (+3) 
- (modified) offload/unittests/Conformance/lib/DeviceContext.cpp (+7) 
- (modified) offload/unittests/OffloadAPI/common/Fixtures.hpp (+10-1) 
- (modified) offload/unittests/OffloadAPI/context/olCreateContext.cpp (+15-1) 
- (modified) offload/unittests/OffloadAPI/context/olGetContextInfo.cpp (+1-14) 
- (modified) offload/unittests/OffloadAPI/context/olGetContextInfoSize.cpp (+1-14) 
- (modified) offload/unittests/OffloadAPI/kernel/olLaunchKernel.cpp (+1-1) 
- (modified) offload/unittests/OffloadAPI/memory/olMemcpy.cpp (+1-1) 
- (modified) offload/unittests/OffloadAPI/queue/olCreateQueue.cpp (+19-3) 
- (modified) offload/unittests/OffloadAPI/queue/olGetQueueInfo.cpp (+8) 
- (modified) offload/unittests/OffloadAPI/queue/olGetQueueInfoSize.cpp (+6) 
- (modified) offload/unittests/OffloadAPI/queue/olLaunchHostFunction.cpp (+1-1) 
- (modified) offload/unittests/OffloadAPI/queue/olWaitEvents.cpp (+5-5) 


``````````diff
diff --git a/libsycl/src/detail/queue_impl.cpp b/libsycl/src/detail/queue_impl.cpp
index 6116e2d787e6f..c1602ac52d142 100644
--- a/libsycl/src/detail/queue_impl.cpp
+++ b/libsycl/src/detail/queue_impl.cpp
@@ -66,7 +66,8 @@ QueueImpl::QueueImpl(DeviceImpl &deviceImpl, const async_handler &asyncHandler,
     : MIsInorder(false), MAsyncHandler(asyncHandler), MPropList(propList),
       MDevice(deviceImpl),
       MContext(MDevice.getPlatformImpl().getDefaultContext()) {
-  callAndThrow(olCreateQueue, MDevice.getOLHandle(), &MOffloadQueue);
+  // TODO: temporary, waiting for proper context support in sycl.
+  callAndThrow(olCreateQueue, nullptr, MDevice.getOLHandle(), &MOffloadQueue);
 }
 
 QueueImpl::~QueueImpl() {
diff --git a/libsycl/unittests/mock/helpers.cpp b/libsycl/unittests/mock/helpers.cpp
index 6b4460cd66113..562754ae68b4c 100644
--- a/libsycl/unittests/mock/helpers.cpp
+++ b/libsycl/unittests/mock/helpers.cpp
@@ -222,8 +222,10 @@ void mock::MockLiboffload::initDefault() {
       });
 
   ON_CALL(*this, olCreateQueue)
-      .WillByDefault([this](ol_device_handle_t Device,
+      .WillByDefault([this](ol_context_handle_t Context,
+                            ol_device_handle_t Device,
                             ol_queue_handle_t *Queue) -> ol_result_t {
+        std::ignore = Context;
         if (!Device)
           return makeEmptyStrError(OL_ERRC_INVALID_NULL_HANDLE);
         if (!Queue)
diff --git a/libsycl/unittests/mock/helpers.hpp b/libsycl/unittests/mock/helpers.hpp
index 0690970f1f99b..50835284c73d9 100644
--- a/libsycl/unittests/mock/helpers.hpp
+++ b/libsycl/unittests/mock/helpers.hpp
@@ -90,7 +90,8 @@ class MockLiboffload {
               (ol_device_iterate_cb_t Callback, void *UserData));
   MOCK_METHOD(ol_result_t, olDestroyProgram, (ol_program_handle_t Program));
   MOCK_METHOD(ol_result_t, olCreateQueue,
-              (ol_device_handle_t Device, ol_queue_handle_t *Queue));
+              (ol_context_handle_t Context, ol_device_handle_t Device,
+               ol_queue_handle_t *Queue));
   MOCK_METHOD(ol_result_t, olDestroyQueue, (ol_queue_handle_t Queue));
   MOCK_METHOD(ol_result_t, olSyncQueue, (ol_queue_handle_t Queue));
   MOCK_METHOD(ol_result_t, olDestroyEvent, (ol_event_handle_t Event));
diff --git a/libsycl/unittests/mock/mock.cpp b/libsycl/unittests/mock/mock.cpp
index 8f5db4b5cb6ec..f050eb13317e8 100644
--- a/libsycl/unittests/mock/mock.cpp
+++ b/libsycl/unittests/mock/mock.cpp
@@ -50,8 +50,9 @@ ol_result_t olDestroyProgram(ol_program_handle_t Program) {
   return mock::getMockLiboffload().olDestroyProgram(Program);
 }
 
-ol_result_t olCreateQueue(ol_device_handle_t Device, ol_queue_handle_t *Queue) {
-  return mock::getMockLiboffload().olCreateQueue(Device, Queue);
+ol_result_t olCreateQueue(ol_context_handle_t Context,
+                          ol_device_handle_t Device, ol_queue_handle_t *Queue) {
+  return mock::getMockLiboffload().olCreateQueue(Context, Device, Queue);
 }
 
 ol_result_t olDestroyQueue(ol_queue_handle_t Queue) {
diff --git a/libsycl/unittests/queue/queue.cpp b/libsycl/unittests/queue/queue.cpp
index 1234e5eb94281..c505f78ecbdd4 100644
--- a/libsycl/unittests/queue/queue.cpp
+++ b/libsycl/unittests/queue/queue.cpp
@@ -20,7 +20,7 @@ using namespace ::testing;
 TEST(Queue, CommonQueriesAndLifetime) {
   mock::MockWrapper Mock;
 
-  EXPECT_CALL(Mock.get(), olCreateQueue(_, _)).Times(1);
+  EXPECT_CALL(Mock.get(), olCreateQueue(_, _, _)).Times(1);
   EXPECT_CALL(Mock.get(), olDestroyQueue(_)).Times(1);
   {
     queue Q;
diff --git a/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.cpp b/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.cpp
index 2cd2cfaeebb6b..82a8815c8a380 100644
--- a/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.cpp
+++ b/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.cpp
@@ -244,12 +244,15 @@ int main(int argc, const char **argv, const char **envp) {
   ol_device_handle_t Host = getHostDevice();
   assert(Host && "Host device should always be present");
 
+  ol_context_handle_t Context;
+  OFFLOAD_ERR(olCreateContext(1, &Device, &Context));
+
   ol_program_handle_t Program;
   OFFLOAD_ERR(olCreateProgram(Device, Image.getBufferStart(),
                               Image.getBufferSize(), &Program));
 
   ol_queue_handle_t Queue;
-  OFFLOAD_ERR(olCreateQueue(Device, &Queue));
+  OFFLOAD_ERR(olCreateQueue(Context, Device, &Queue));
 
   int DevArgc = static_cast<int>(NewArgv.size());
   void *DevArgv = copyArgumentVector(NewArgv.size(), NewArgv.begin(), Device);
@@ -284,6 +287,7 @@ int main(int argc, const char **argv, const char **envp) {
   OFFLOAD_ERR(olMemFree(DevArgv));
   OFFLOAD_ERR(olMemFree(DevEnvp));
   OFFLOAD_ERR(olDestroyQueue(Queue));
+  OFFLOAD_ERR(olDestroyContext(Context));
   OFFLOAD_ERR(olDestroyProgram(Program));
   OFFLOAD_ERR(olShutDown());
 
diff --git a/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.h b/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.h
index 1e9ae3448e7a0..c257f2ea391b2 100644
--- a/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.h
+++ b/llvm/tools/llvm-gpu-loader/llvm-gpu-loader.h
@@ -108,6 +108,7 @@ typedef struct ol_platform_impl_t *ol_platform_handle_t;
 typedef struct ol_program_impl_t *ol_program_handle_t;
 typedef struct ol_queue_impl_t *ol_queue_handle_t;
 typedef struct ol_symbol_impl_t *ol_symbol_handle_t;
+typedef struct ol_context_impl_t *ol_context_handle_t;
 typedef const struct ol_error_struct_t *ol_result_t;
 
 typedef bool (*ol_device_iterate_cb_t)(ol_device_handle_t Device,
@@ -138,7 +139,13 @@ ol_result_t (*olLaunchKernel)(
     const ol_kernel_launch_prop_t *Properties, size_t NumArgs, void **ArgPtrs,
     const size_t *ArgSizes);
 
-ol_result_t (*olCreateQueue)(ol_device_handle_t Device,
+ol_result_t (*olCreateContext)(size_t DevicesCount, ol_device_handle_t *Devices,
+                               ol_context_handle_t *Context);
+
+ol_result_t (*olDestroyContext)(ol_context_handle_t Context);
+
+ol_result_t (*olCreateQueue)(ol_context_handle_t Context,
+                             ol_device_handle_t Device,
                              ol_queue_handle_t *Queue);
 
 ol_result_t (*olDestroyQueue)(ol_queue_handle_t Queue);
@@ -195,6 +202,8 @@ llvm::Error loadLLVMOffload() {
   DYNAMIC_INIT(olDestroyProgram);
   DYNAMIC_INIT(olGetSymbol);
   DYNAMIC_INIT(olLaunchKernel);
+  DYNAMIC_INIT(olCreateContext);
+  DYNAMIC_INIT(olDestroyContext);
   DYNAMIC_INIT(olCreateQueue);
   DYNAMIC_INIT(olDestroyQueue);
   DYNAMIC_INIT(olSyncQueue);
diff --git a/offload/liboffload/API/Queue.td b/offload/liboffload/API/Queue.td
index 4008432375753..b03ad6d46a1e8 100644
--- a/offload/liboffload/API/Queue.td
+++ b/offload/liboffload/API/Queue.td
@@ -11,13 +11,21 @@
 //===----------------------------------------------------------------------===//
 
 def olCreateQueue : Function {
-    let desc = "Create a queue for the given device.";
-    let details = [];
+    let desc = "Create a queue for the given device within the given context.";
+    let details = [
+        "The queue is scoped to `Context` and `Device` must belong to it.",
+        "The queue can only operate on resources (memory allocations, programs) that were created in the same context."
+    ];
     let params = [
+        Param<"ol_context_handle_t", "Context", "handle of the context", PARAM_IN>,
         Param<"ol_device_handle_t", "Device", "handle of the device", PARAM_IN>,
         Param<"ol_queue_handle_t*", "Queue", "output pointer for the created queue", PARAM_OUT>
     ];
-    let returns = [];
+    let returns = [
+        Return<"OL_ERRC_INVALID_DEVICE", [
+            "Device does not belong to `Context`"
+        ]>
+    ];
 }
 
 def olDestroyQueue : Function {
@@ -61,6 +69,7 @@ def ol_queue_info_t : Enum {
   let is_typed = 1;
   let etors = [
     TaggedEtor<"DEVICE", "ol_device_handle_t", "The handle of the device associated with the queue.">,
+    TaggedEtor<"CONTEXT", "ol_context_handle_t", "The handle of the context associated with the queue.">,
     TaggedEtor<"EMPTY", "bool", "True if the queue is known to be empty. May be unconditionally false if the device does not support status queries.">,
   ];
 }
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 63fe726fbe544..d6a2b466cb92d 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -71,70 +71,17 @@ struct ol_device_impl_t {
       : DeviceNum(DeviceNum), Device(Device), Platform(Platform),
         Info(std::forward<InfoTreeNode>(DevInfo)) {}
 
-  ~ol_device_impl_t() {
-    assert(!OutstandingQueues.size() &&
-           "Device object dropped with outstanding queues");
-  }
-
   int DeviceNum;
   GenericDeviceTy *Device;
   ol_platform_impl_t &Platform;
   InfoTreeNode Info;
-
-  llvm::SmallVector<__tgt_async_info *> OutstandingQueues;
-  std::mutex OutstandingQueuesMutex;
-
-  /// If the device has any outstanding queues that are now complete, remove it
-  /// from the list and return it.
-  ///
-  /// Queues may be added to the outstanding queue list by olDestroyQueue if
-  /// they are destroyed but not completed.
-  __tgt_async_info *getOutstandingQueue() {
-    // Not locking the `size()` access is fine here - In the worst case we
-    // either miss a queue that exists or loop through an empty array after
-    // taking the lock. Both are sub-optimal but not that bad.
-    if (OutstandingQueues.size()) {
-      std::lock_guard<std::mutex> Lock(OutstandingQueuesMutex);
-
-      // As queues are pulled and popped from this list, longer running queues
-      // naturally bubble to the start of the array. Hence looping backwards.
-      for (auto Q = OutstandingQueues.rbegin(); Q != OutstandingQueues.rend();
-           Q++) {
-        if (!Device->hasPendingWork(*Q)) {
-          auto OutstandingQueue = *Q;
-          *Q = OutstandingQueues.back();
-          OutstandingQueues.pop_back();
-          return OutstandingQueue;
-        }
-      }
-    }
-    return nullptr;
-  }
-
-  /// Complete all pending work for this device and perform any needed cleanup.
-  ///
-  /// After calling this function, no liboffload functions should be called with
-  /// this device handle.
-  llvm::Error destroy() {
-    llvm::Error Result = Plugin::success();
-    for (auto Q : OutstandingQueues)
-      if (auto Err = Device->synchronize(Q, /*Release=*/true))
-        Result = llvm::joinErrors(std::move(Result), std::move(Err));
-    OutstandingQueues.clear();
-    return Result;
-  }
 };
 
 llvm::Error ol_platform_impl_t::destroy() {
-  llvm::Error Result = Plugin::success();
-  for (auto &D : Devices)
-    if (auto Err = D->destroy())
-      Result = llvm::joinErrors(std::move(Result), std::move(Err));
-
   if (auto Res = Plugin->deinit())
-    Result = llvm::joinErrors(std::move(Result), std::move(Res));
+    return Res;
 
-  return Result;
+  return llvm::Error::success();
 }
 
 llvm::Error ol_platform_impl_t::init() {
@@ -160,9 +107,12 @@ llvm::Error ol_platform_impl_t::init() {
 }
 
 struct ol_queue_impl_t {
-  ol_queue_impl_t(__tgt_async_info *AsyncInfo, ol_device_handle_t Device)
-      : AsyncInfo(AsyncInfo), Device(Device), Id(IdCounter++) {}
+  ol_queue_impl_t(__tgt_async_info *AsyncInfo, ol_context_handle_t Context,
+                  ol_device_handle_t Device)
+      : AsyncInfo(AsyncInfo), Context(Context), Device(Device),
+        Id(IdCounter++) {}
   __tgt_async_info *AsyncInfo;
+  ol_context_handle_t Context;
   ol_device_handle_t Device;
   // A unique identifier for the queue
   size_t Id;
@@ -218,6 +168,66 @@ struct ol_context_impl_t {
   ol_platform_impl_t *Platform;
   llvm::SmallVector<ol_device_handle_t> Devices;
   std::unique_ptr<plugin::PluginContextTy> PluginCtx;
+
+  bool contains(ol_device_handle_t Device) const {
+    return llvm::is_contained(Devices, Device);
+  }
+
+  /// Queues destroyed while still busy, keyed by owning device. Per-context
+  /// so their backend resources are not reused across native contexts.
+  llvm::DenseMap<ol_device_handle_t, llvm::SmallVector<__tgt_async_info *>>
+      OutstandingQueues;
+  std::mutex OutstandingQueuesMutex;
+
+  /// If the context has any outstanding queues for \p Device that are now
+  /// complete, remove it from the list and return it.
+  ///
+  /// Queues may be added to the outstanding queue list by olDestroyQueue if
+  /// they are destroyed but not completed.
+  __tgt_async_info *getOutstandingQueue(ol_device_handle_t Device) {
+    std::lock_guard<std::mutex> Lock(OutstandingQueuesMutex);
+    auto It = OutstandingQueues.find(Device);
+    if (It == OutstandingQueues.end() || It->second.empty())
+      return nullptr;
+    auto &Bucket = It->second;
+
+    // As queues are pulled and popped from this list, longer running queues
+    // naturally bubble to the start of the array. Hence looping backwards.
+    for (auto Q = Bucket.rbegin(); Q != Bucket.rend(); Q++) {
+      if (!Device->Device->hasPendingWork(*Q)) {
+        auto OutstandingQueue = *Q;
+        *Q = Bucket.back();
+        Bucket.pop_back();
+        return OutstandingQueue;
+      }
+    }
+    return nullptr;
+  }
+
+  /// Record \p AsyncInfo on the outstanding queue list for \p Device.
+  void addOutstandingQueue(ol_device_handle_t Device,
+                           __tgt_async_info *AsyncInfo) {
+    std::lock_guard<std::mutex> Lock(OutstandingQueuesMutex);
+    OutstandingQueues[Device].push_back(AsyncInfo);
+  }
+
+  /// Complete all pending work for this context. Called from olDestroyContext.
+  llvm::Error drainOutstandingQueues() {
+    llvm::Error Result = Plugin::success();
+    for (auto &Bucket : OutstandingQueues) {
+      auto *Device = Bucket.first;
+      for (auto *AI : Bucket.second)
+        if (auto Err = Device->Device->synchronize(AI, /*Release=*/true))
+          Result = llvm::joinErrors(std::move(Result), std::move(Err));
+    }
+    OutstandingQueues.clear();
+    return Result;
+  }
+
+  ~ol_context_impl_t() {
+    assert(OutstandingQueues.empty() &&
+           "Context dropped with outstanding queues");
+  }
 };
 
 namespace llvm {
@@ -629,6 +639,8 @@ Error olCreateContext_impl(size_t DevicesCount, ol_device_handle_t *Devices,
 }
 
 Error olDestroyContext_impl(ol_context_handle_t Context) {
+  if (auto Err = Context->drainOutstandingQueues())
+    return Err;
   return olDestroy(Context);
 }
 
@@ -845,10 +857,16 @@ Error olGetMemInfoSize_impl(const void *Ptr, ol_mem_info_t PropName,
   return olGetMemInfoImplDetail(Ptr, PropName, 0, nullptr, PropSizeRet);
 }
 
-Error olCreateQueue_impl(ol_device_handle_t Device, ol_queue_handle_t *Queue) {
-  auto CreatedQueue = std::make_unique<ol_queue_impl_t>(nullptr, Device);
+Error olCreateQueue_impl(ol_context_handle_t Context, ol_device_handle_t Device,
+                         ol_queue_handle_t *Queue) {
+  if (!Context->contains(Device))
+    return createOffloadError(ErrorCode::INVALID_DEVICE,
+                              "device does not belong to the given context");
+
+  auto CreatedQueue =
+      std::make_unique<ol_queue_impl_t>(nullptr, Context, Device);
 
-  auto OutstandingQueue = Device->getOutstandingQueue();
+  auto OutstandingQueue = Context->getOutstandingQueue(Device);
   if (OutstandingQueue) {
     // The queue is empty, but we still need to sync it to release any temporary
     // memory allocations or do other cleanup.
@@ -856,8 +874,8 @@ Error olCreateQueue_impl(ol_device_handle_t Device, ol_queue_handle_t *Queue) {
             Device->Device->synchronize(OutstandingQueue, /*Release=*/false))
       return Err;
     CreatedQueue->AsyncInfo = OutstandingQueue;
-  } else if (auto Err =
-                 Device->Device->initAsyncInfo(&(CreatedQueue->AsyncInfo))) {
+  } else if (auto Err = Device->Device->initAsyncInfo(
+                 &(CreatedQueue->AsyncInfo), Context->PluginCtx.get())) {
     return Err;
   }
 
@@ -867,6 +885,7 @@ Error olCreateQueue_impl(ol_device_handle_t Device, ol_queue_handle_t *Queue) {
 
 Error olDestroyQueue_impl(ol_queue_handle_t Queue) {
   auto *Device = Queue->Device;
+  auto *Context = Queue->Context;
   // This is safe; as soon as olDestroyQueue is called it is not possible to add
   // any more work to the queue, so if it's finished now it will remain finished
   // forever.
@@ -881,8 +900,7 @@ Error olDestroyQueue_impl(ol_queue_handle_t Queue) {
       return Err;
   } else {
     // The queue still has outstanding work. Store it so we can check it later.
-    std::lock_guard<std::mutex> Lock(Device->OutstandingQueuesMutex);
-    Device->OutstandingQueues.push_back(Queue->AsyncInfo);
+    Context->addOutstandingQueue(Device, Queue->AsyncInfo);
   }
 
   return olDestroy(Queue);
@@ -933,6 +951,8 @@ Error olGetQueueInfoImplDetail(ol_queue_handle_t Queue,
   switch (PropName) {
   case OL_QUEUE_INFO_DEVICE:
     return Info.write<ol_device_handle_t>(Queue->Device);
+  case OL_QUEUE_INFO_CONTEXT:
+    return Info.write<ol_context_handle_t>(Queue->Context);
   case OL_QUEUE_INFO_EMPTY: {
     auto Pending = Queue->Device->Device->hasPendingWork(Queue->AsyncInfo);
     if (auto Err = Pending.takeError())
diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index 67ebfbc943fdc..250f9d66a1afd 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -109,7 +109,8 @@ template <typename... ArgsTy>
 /// operations when calling AsyncInfoWrapperTy::finalize(). This latter function
 /// must be called before destroying the wrapper object.
 struct AsyncInfoWrapperTy {
-  AsyncInfoWrapperTy(GenericDeviceTy &Device, __tgt_async_info *AsyncInfoPtr);
+  AsyncInfoWrapperTy(GenericDeviceTy &Device, __tgt_async_info *AsyncInfoPtr,
+                     PluginContextTy *Context = nullptr);
 
   ~AsyncInfoWrapperTy() {
     assert(!AsyncInfoPtr && "AsyncInfoWrapperTy not finalized");
@@ -118,6 +119,10 @@ struct AsyncInfoWrapperTy {
   /// Get the raw __tgt_async_info pointer.
   operator __tgt_async_info *() const { return AsyncInfoPtr; }
 
+  /// Optional plugin-side context this async info is scoped to; null on the
+  /// libomptarget path.
+  PluginContextTy *getContext() const { return Context; }
+
   /// Indicate whether there is queue.
   bool hasQueue() const { return (AsyncInfoPtr->Queue != nullptr); }
 
@@ -177,6 +182,7 @@ struct AsyncInfoWrapperTy {
   GenericDeviceTy &Device;
   __tgt_async_info LocalAsyncInfo;
   __tgt_async_info *AsyncInfoPtr;
+  PluginContextTy *Context = nullptr;
 };
 
 enum class DeviceInfo {
@@ -1104,8 +1110,10 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
                      KernelExtraArgsTy *KernelExtraArgs,
                      __tgt_async_info *AsyncInfo);
 
-  /// Initialize a __tgt_async_info structure.
-  Error initAsyncInfo(__tgt_async_info **AsyncInfoPtr);
+  /// Initialize a __tgt_async_info structure. \p Context is optional and is
+  /// forwarded on the wrapper for plugin impls that need it.
+  Error initAsyncInfo(__tgt_async_info **AsyncInfoPtr,
+                      PluginContextTy *Context = nullptr);
   virtual Error initAsyncInfoImpl(AsyncInfoWrapperTy &AsyncInfoWrapper) = 0;
 
   /// Enqueue a host call to AsyncInfo
diff --git a/offload/plugins-nextgen/common/src/PluginInterface.cpp b/offload/plugins-nextgen/common/src/PluginInterface.cpp
index 7b821e77df179..4c0f639e475a3 100644
--- a/offload/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp
@@ -45,9 +45,11 @@ using namespace error;
 using namespace llvm::offload::debug;
 
 AsyncInfoWrapperTy::AsyncInfoWrapperTy(GenericDeviceTy &Device,
-                                       __tgt_async_info *AsyncInfoPtr)
+                                       __tgt_async_info *AsyncInfoPtr,
+                                       PluginContextTy *Context)
     : Device(Device),
-      AsyncInfoPtr(AsyncInfoPtr ? AsyncInfoPtr : &LocalAsyncInfo) {}
+      AsyncInfoPtr(AsyncInfoPtr ? AsyncInfoPtr : &LocalAsyncInfo),
+      Context(Context) {}
 
 Error AsyncInfoWrapperTy::synchronize() {
   assert(AsyncInfoPtr && "AsyncInfoWrapperTy already finalized");
@@ -1206,12 +1208,13 @@ Error GenericDeviceTy::launchKernel(void *EntryPtr, void **ArgPtrs,
   return Err;
 }
 
-Error GenericDeviceTy::initAsyncInfo(__tgt_async_info **AsyncInfoPtr) {
+Error GenericDeviceTy::initAsyncInfo(__tgt_async_info **AsyncInfoPtr,
+                  ...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/213057


More information about the llvm-commits mailing list