Skip to content

[Offload] Adding missing Offload unit tests for event entry points #137315

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

callumfare
Copy link
Contributor

A couple of liboffload entry points were missed out from the tests, and unsurprisingly a crash in one of them made it in. Add the tests and fix the unchecked error in olDestroyEvent.

@llvmbot
Copy link
Member

llvmbot commented Apr 25, 2025

@llvm/pr-subscribers-offload

Author: Callum Fare (callumfare)

Changes

A couple of liboffload entry points were missed out from the tests, and unsurprisingly a crash in one of them made it in. Add the tests and fix the unchecked error in olDestroyEvent.


Full diff: https://github.com/llvm/llvm-project/pull/137315.diff

4 Files Affected:

  • (modified) offload/liboffload/src/OffloadImpl.cpp (+4-1)
  • (modified) offload/unittests/OffloadAPI/CMakeLists.txt (+2)
  • (added) offload/unittests/OffloadAPI/event/olDestroyEvent.cpp (+31)
  • (added) offload/unittests/OffloadAPI/event/olWaitEvent.cpp (+31)
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index d956d274b5eb1..fdd22c441aef6 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -69,7 +69,6 @@ struct ol_queue_impl_t {
 struct ol_event_impl_t {
   ol_event_impl_t(void *EventInfo, ol_queue_handle_t Queue)
       : EventInfo(EventInfo), Queue(Queue) {}
-  ~ol_event_impl_t() { (void)Queue->Device->Device->destroyEvent(EventInfo); }
   void *EventInfo;
   ol_queue_handle_t Queue;
 };
@@ -378,6 +377,10 @@ ol_impl_result_t olWaitEvent_impl(ol_event_handle_t Event) {
 }
 
 ol_impl_result_t olDestroyEvent_impl(ol_event_handle_t Event) {
+  auto Res = Event->Queue->Device->Device->destroyEvent(Event->EventInfo);
+  if (Res)
+    return {OL_ERRC_INVALID_EVENT, "The event could not be destroyed"};
+
   return olDestroy(Event);
 }
 
diff --git a/offload/unittests/OffloadAPI/CMakeLists.txt b/offload/unittests/OffloadAPI/CMakeLists.txt
index c4d628a5a87f8..73202076ab114 100644
--- a/offload/unittests/OffloadAPI/CMakeLists.txt
+++ b/offload/unittests/OffloadAPI/CMakeLists.txt
@@ -21,6 +21,8 @@ add_libompt_unittest("offload.unittests"
     ${CMAKE_CURRENT_SOURCE_DIR}/program/olDestroyProgram.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/kernel/olGetKernel.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/kernel/olLaunchKernel.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/event/olDestroyEvent.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/event/olWaitEvent.cpp
     )
 add_dependencies("offload.unittests" ${PLUGINS_TEST_COMMON} LibomptUnitTestsDeviceBins)
 target_compile_definitions("offload.unittests" PRIVATE DEVICE_CODE_PATH="${OFFLOAD_TEST_DEVICE_CODE_PATH}")
diff --git a/offload/unittests/OffloadAPI/event/olDestroyEvent.cpp b/offload/unittests/OffloadAPI/event/olDestroyEvent.cpp
new file mode 100644
index 0000000000000..1a6b7aebb0781
--- /dev/null
+++ b/offload/unittests/OffloadAPI/event/olDestroyEvent.cpp
@@ -0,0 +1,31 @@
+//===------- Offload API tests - olDestroyEvent ---------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "../common/Fixtures.hpp"
+#include <OffloadAPI.h>
+#include <gtest/gtest.h>
+
+using olDestroyEventTest = OffloadQueueTest;
+
+TEST_F(olDestroyEventTest, Success) {
+  uint32_t Src = 42;
+  void *DstPtr;
+
+  ol_event_handle_t Event = nullptr;
+  ASSERT_SUCCESS(
+      olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, sizeof(uint32_t), &DstPtr));
+  ASSERT_SUCCESS(
+      olMemcpy(Queue, DstPtr, Device, &Src, Host, sizeof(Src), &Event));
+  ASSERT_NE(Event, nullptr);
+  ASSERT_SUCCESS(olWaitQueue(Queue));
+  ASSERT_SUCCESS(olDestroyEvent(Event));
+}
+
+TEST_F(olDestroyEventTest, InvalidNullEvent) {
+  ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, olDestroyEvent(nullptr));
+}
diff --git a/offload/unittests/OffloadAPI/event/olWaitEvent.cpp b/offload/unittests/OffloadAPI/event/olWaitEvent.cpp
new file mode 100644
index 0000000000000..632d99288c71b
--- /dev/null
+++ b/offload/unittests/OffloadAPI/event/olWaitEvent.cpp
@@ -0,0 +1,31 @@
+//===------- Offload API tests - olWaitEvent -====-------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "../common/Fixtures.hpp"
+#include <OffloadAPI.h>
+#include <gtest/gtest.h>
+
+using olWaitEventTest = OffloadQueueTest;
+
+TEST_F(olWaitEventTest, Success) {
+  uint32_t Src = 42;
+  void *DstPtr;
+
+  ol_event_handle_t Event = nullptr;
+  ASSERT_SUCCESS(
+      olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, sizeof(uint32_t), &DstPtr));
+  ASSERT_SUCCESS(
+      olMemcpy(Queue, DstPtr, Device, &Src, Host, sizeof(Src), &Event));
+  ASSERT_NE(Event, nullptr);
+  ASSERT_SUCCESS(olWaitEvent(Event));
+  ASSERT_SUCCESS(olDestroyEvent(Event));
+}
+
+TEST_F(olWaitEventTest, InvalidNullEvent) {
+  ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, olWaitEvent(nullptr));
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants