[llvm] [OFFLOAD][L0] Implement Event APIs (PR #201306)
Kevin Sala Penades via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 3 23:07:55 PDT 2026
================
@@ -0,0 +1,124 @@
+//===--- Level Zero Target RTL Implementation -----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Level Zero Event and Event Pool abstractions.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0EVENT_H
+#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0EVENT_H
+
+#include <level_zero/ze_api.h>
+#include <memory>
+#include <mutex>
+
+#include "L0Defs.h"
+#include "L0Trace.h"
+
+namespace llvm::omp::target::plugin {
+
+class L0QueueTy;
+
+class L0EventTy {
+ ze_event_handle_t ZeEvent = nullptr;
+ L0QueueTy *Queue = nullptr;
+
+public:
+ L0EventTy(ze_event_handle_t ZeEvent) : ZeEvent(ZeEvent) {}
+ ze_event_handle_t getZeEvent() const { return ZeEvent; }
+ L0QueueTy *getQueue() const { return Queue; }
+
+ Error reset() {
+ Queue = nullptr;
+ CALL_ZE_RET_ERROR(zeEventHostReset, ZeEvent);
+ return Plugin::success();
+ }
+
+ void setQueue(L0QueueTy &Q) { Queue = &Q; }
+
+ Error synchronize() {
+ CALL_ZE_RET_ERROR(zeEventHostSynchronize, ZeEvent, L0DefaultTimeout);
+ return Plugin::success();
+ }
+
+ Expected<bool> isComplete() {
+ ze_result_t Result;
+
+ CALL_ZE(Result, zeEventQueryStatus, ZeEvent);
+ if (Result == ZE_RESULT_SUCCESS)
+ return true;
+ if (Result == ZE_RESULT_NOT_READY)
+ return false;
+ return Plugin::error(ErrorCode::UNKNOWN, "failed to query event status: %s",
+ getZeErrorName(Result));
+ }
+};
+
+/// Common event pool used in the plugin. This event pool assumes all events
+/// from the pool are host-visible and use the same event pool flag.
+class EventPoolTy {
+ /// Size of L0 event pool created on demand.
+ size_t PoolSize = 64;
+
+ /// Context of the events.
+ ze_context_handle_t Context = nullptr;
+
+ /// Additional event pool flags common to this pool.
+ uint32_t Flags = 0;
+
+ /// Protection.
+ std::unique_ptr<std::mutex> Mtx;
+
+ /// Created L0 event pools.
+ llvm::SmallVector<ze_event_pool_handle_t> Pools;
+
+ /// L0 events cache.
+ llvm::SmallVector<ze_event_handle_t> Events;
+
+ /// L0 event objects cache.
+ llvm::SmallVector<L0EventTy *> EventObjects;
+
+ // Internal method to get an event from the pool. The caller must hold the
+ // lock.
+ Expected<ze_event_handle_t> getEventLocked();
+
+public:
+ /// Initialize context, flags, and mutex.
+ Error init(ze_context_handle_t ContextIn, uint32_t FlagsIn) {
+ Context = ContextIn;
+ Flags = FlagsIn;
+ Mtx.reset(new std::mutex);
+ return Plugin::success();
+ }
+
+ /// Destroys L0 resources.
+ Error deinit() {
+ for (auto *EventObj : EventObjects)
+ delete EventObj;
+ for (auto E : Events)
+ CALL_ZE_RET_ERROR(zeEventDestroy, E);
+ for (auto P : Pools)
+ CALL_ZE_RET_ERROR(zeEventPoolDestroy, P);
+ return Plugin::success();
+ }
+
+ /// Get a free event from the pool.
+ Expected<ze_event_handle_t> getEvent() {
+ std::lock_guard<std::mutex> Lock(*Mtx);
+ return getEventLocked();
+ }
+ Expected<L0EventTy *> getEventObject();
+
+ /// Return an event to the pool.
+ Error releaseEvent(ze_event_handle_t Event);
+ Error releaseEventObject(L0EventTy *EventObj);
----------------
kevinsala wrote:
Can you document this function? What's the difference between releasing the object and the handle.
https://github.com/llvm/llvm-project/pull/201306
More information about the llvm-commits
mailing list