Skip to content

Commit 23233f7

Browse files
nrspruitKornevNikita
authored andcommitted
[UR][L0] Fix Barrier Event Cleanup (#17989)
- Fix barrier wait cleanup of events given an out event such that an additional refcnt release is done and with multi command lists and in order, the active barrier is properly cleared before reassignment. Signed-off-by: Neil R. Spruit <[email protected]>
1 parent 65ee2e1 commit 23233f7

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

unified-runtime/source/adapters/level_zero/event.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ ur_result_t urEnqueueEventsWaitWithBarrierExt(
318318
// need to keep track of any active barriers if we have in-order queue.
319319
if (UseMultipleCmdlistBarriers && !Queue->isInOrderQueue()) {
320320
auto UREvent = reinterpret_cast<ur_event_handle_t>(ResultEvent);
321+
// We must release the Active Barrier before we start the next one
322+
// otherwise we will leak an event that won't be released.
323+
UR_CALL(Queue->ActiveBarriers.clear());
321324
Queue->ActiveBarriers.add(UREvent);
322325
}
323326

@@ -900,12 +903,21 @@ ur_result_t
900903
urEventRelease(/** [in] handle of the event object */ ur_event_handle_t Event) {
901904
Event->RefCountExternal--;
902905
bool isEventsWaitCompleted =
903-
Event->CommandType == UR_COMMAND_EVENTS_WAIT && Event->Completed;
906+
(Event->CommandType == UR_COMMAND_EVENTS_WAIT ||
907+
Event->CommandType == UR_COMMAND_EVENTS_WAIT_WITH_BARRIER) &&
908+
Event->Completed;
904909
UR_CALL(urEventReleaseInternal(Event));
905910
// If this is a Completed Event Wait Out Event, then we need to cleanup the
906911
// event at user release and not at the time of completion.
912+
// If the event is labelled as completed and no additional references are
913+
// removed, then we still need to decrement the event, but not mark as
914+
// completed.
907915
if (isEventsWaitCompleted) {
908-
UR_CALL(CleanupCompletedEvent((Event), false, false));
916+
if (Event->CleanedUp) {
917+
UR_CALL(urEventReleaseInternal(Event));
918+
} else {
919+
UR_CALL(CleanupCompletedEvent((Event), false, false));
920+
}
909921
}
910922

911923
return UR_RESULT_SUCCESS;

unified-runtime/source/adapters/level_zero/queue.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,10 +2356,11 @@ ur_queue_handle_t_::insertActiveBarriers(ur_command_list_ptr_t &CmdList,
23562356
Event->WaitList = ActiveBarriersWaitList;
23572357
Event->OwnNativeHandle = true;
23582358

2359-
// If there are more active barriers, insert a barrier on the command-list. We
2360-
// do not need an event for finishing so we pass nullptr.
2359+
// If there are more active barriers, insert a barrier on the command-list.
2360+
// We need to signal the current active barrier event otherwise we will leak
2361+
// the Event object when we replace the active barrier.
23612362
ZE2UR_CALL(zeCommandListAppendBarrier,
2362-
(CmdList->first, nullptr, ActiveBarriersWaitList.Length,
2363+
(CmdList->first, Event->ZeEvent, ActiveBarriersWaitList.Length,
23632364
ActiveBarriersWaitList.ZeEventList));
23642365
return UR_RESULT_SUCCESS;
23652366
}

0 commit comments

Comments
 (0)