================
@@ -61,16 +61,73 @@ static inline StreamTy *toInternalStream(Stream_t Stream) {
   return reinterpret_cast<StreamTy *>(Stream);
 }
 
-/// Convert a Stream_t to an ol_queue_handle_t.
-static inline Error_t getQueueFromStream(Stream_t Stream,
-                                         ol_queue_handle_t *Queue) {
-  if (!Stream)
-    return ErrorInvalidValue;
+static inline ol_result_t
+syncAndDestroyEvents(llvm::SmallVectorImpl<ol_event_handle_t> &Events) {
+  ol_result_t FirstError = OL_SUCCESS;
+  for (ol_event_handle_t Event : Events) {
+    if (!Event)
+      continue;
+
+    ol_result_t SyncResult = olSyncEvent(Event);
+    if (FirstError == OL_SUCCESS && SyncResult != OL_SUCCESS)
+      FirstError = SyncResult;
 
-  // TODO: add proper DEBUG/assert guarded checks
-  StreamTy *InternalStream = toInternalStream(Stream);
-  *Queue = InternalStream->Queue;
-  return Success;
+    ol_result_t DestroyResult = olDestroyEvent(Event);
+    if (FirstError == OL_SUCCESS && DestroyResult != OL_SUCCESS)
+      FirstError = DestroyResult;
+  }
+  Events.clear();
+  return FirstError;
+}
+
+/// Wait for blocking streams before executing if we are legacy default stream.
+static inline ol_result_t waitOnBlockingStreams(StateTy &State, ThreadStateTy 
&ThreadState) {
+  ol_device_handle_t Device = ThreadState.getDefaultDevice();
+  SmallPtrSet<StreamTy *, 8> BlockingStreams = 
State.getBlockingStreams(Device);
+  if (!State.hasLegacyDefaultStream(Device) || BlockingStreams.empty())
+    return OL_SUCCESS;
+
+  StreamTy *DefaultStream = ThreadState.getDefaultStream();
+  SmallVector<ol_event_handle_t, 8> Events;
+  for (StreamTy *BlockingStream : BlockingStreams) {
+    ol_event_handle_t Event = nullptr;
+    ol_result_t Result =
+        olCreateEvent(BlockingStream->Queue, OL_EVENT_FLAGS_NONE, &Event);
+    if (Result != OL_SUCCESS) {
+      if (Event)
+        Events.push_back(Event);
+      syncAndDestroyEvents(Events);
+      return Result;
+    }
+    Events.push_back(Event);
+  }
+
+  return DefaultStream->waitOnAndTrackDependencyEvents(Events);
+}
+
+/// Wait for the legacy default stream to complete before launching a kernel on
+/// a blocking stream.
+static inline ol_result_t waitOnLegacyDefaultStream(StateTy &State, 
ThreadStateTy &ThreadState, StreamTy *SourceStream,
+                                                    ol_device_handle_t Device) 
{
+  if (!State.hasLegacyDefaultStream(Device))
+    return OL_SUCCESS;
+
+  StreamTy *DefaultStream = ThreadState.getDefaultStream();
+  assert(DefaultStream->Kind == QueueKind::LegacyDefault &&
+         "Default stream is not a legacy default stream");
+
+  ol_event_handle_t Event = nullptr;
+  ol_result_t Result =
+      olCreateEvent(DefaultStream->Queue, OL_EVENT_FLAGS_NONE, &Event);
+  if (Result != OL_SUCCESS) {
+    if (Event) {
----------------
kevinsala wrote:

Is this possible? If there was an error creating an event, `olCreateEvent` 
should be responsible for fully handling its destruction internally. The 
handling here should be removed.

https://github.com/llvm/llvm-project/pull/218049
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to