Skip to content

[Offload] Verify SyncCycle for events in AMDGPU #149524

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

RossBrunton
Copy link
Contributor

This check ensures that events after a synchronise (and thus after the
queue is reset) are always considered complete. A test has been added
as well.

This check ensures that events after a synchronise (and thus after the
queue is reset) are always considered complete. A test has been added
as well.
@llvmbot
Copy link
Member

llvmbot commented Jul 18, 2025

@llvm/pr-subscribers-offload

@llvm/pr-subscribers-backend-amdgpu

Author: Ross Brunton (RossBrunton)

Changes

This check ensures that events after a synchronise (and thus after the
queue is reset) are always considered complete. A test has been added
as well.


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

2 Files Affected:

  • (modified) offload/plugins-nextgen/amdgpu/src/rtl.cpp (+5)
  • (modified) offload/unittests/OffloadAPI/event/olWaitEvent.cpp (+17)
diff --git a/offload/plugins-nextgen/amdgpu/src/rtl.cpp b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
index d4400547f9568..f8db9bf0ae739 100644
--- a/offload/plugins-nextgen/amdgpu/src/rtl.cpp
+++ b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
@@ -1665,6 +1665,11 @@ Error AMDGPUStreamTy::waitEvent(const AMDGPUEventTy &Event) {
 Error AMDGPUStreamTy::synchronizeOn(AMDGPUEventTy &Event) {
   std::lock_guard<std::mutex> Lock(Mutex);
 
+  // If this event was for an older sync cycle, it has already been finalized
+  if (Event.RecordedSyncCycle < SyncCycle)
+    return Plugin::success();
+  assert(Event.RecordedSyncCycle == SyncCycle && "event is from the future?");
+
   // Wait until the requested slot has completed
   if (auto Err = Slots[Event.RecordedSlot].Signal->wait(
           StreamBusyWaitMicroseconds, &Device))
diff --git a/offload/unittests/OffloadAPI/event/olWaitEvent.cpp b/offload/unittests/OffloadAPI/event/olWaitEvent.cpp
index f80dabb4fc93f..1f2977eda64e2 100644
--- a/offload/unittests/OffloadAPI/event/olWaitEvent.cpp
+++ b/offload/unittests/OffloadAPI/event/olWaitEvent.cpp
@@ -30,3 +30,20 @@ TEST_P(olWaitEventTest, Success) {
 TEST_P(olWaitEventTest, InvalidNullEvent) {
   ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, olWaitEvent(nullptr));
 }
+
+TEST_P(olWaitEventTest, SuccessMultipleWait) {
+  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);
+
+  for (size_t I = 0; I < 10; I++)
+    ASSERT_SUCCESS(olWaitEvent(Event));
+
+  ASSERT_SUCCESS(olDestroyEvent(Event));
+}

@@ -1665,6 +1665,11 @@ Error AMDGPUStreamTy::waitEvent(const AMDGPUEventTy &Event) {
Error AMDGPUStreamTy::synchronizeOn(AMDGPUEventTy &Event) {
std::lock_guard<std::mutex> Lock(Mutex);

// If this event was for an older sync cycle, it has already been finalized
if (Event.RecordedSyncCycle < SyncCycle)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this is if the user consumes an event twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That, or if a later event gets consumed before an earlier one. If the latest event in the queue is synced on, the queue is reset and SyncCycle incremented.

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

Successfully merging this pull request may close these issues.

3 participants