Skip to content

[SYCL] Retain PI events until they have signaled #3350

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

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ createEventAndAssociateQueue(pi_queue Queue, pi_event *Event,
// In piEventRelease, the reference counter of the Queue is decremented
// to release it.
piQueueRetainNoLock(Queue);

// SYCL RT does not track completion of the events, so it could
// release a PI event as soon as that's not being waited in the app.
// But we have to ensure that the event is not destroyed before
// it is really signalled, so retain it explicitly here and
// release in cleanupAfterEvent.
//
PI_CALL(piEventRetain(*Event));

return PI_SUCCESS;
}

Expand Down Expand Up @@ -3840,6 +3849,15 @@ static pi_result cleanupAfterEvent(pi_event Event) {
PI_CALL(piKernelRelease(pi_cast<pi_kernel>(Event->CommandData)));
Event->CommandData = nullptr;
}

if (!Event->CleanedUp) {
Event->CleanedUp = true;
// Release this event since we explicitly retained it on creation.
// NOTE: that this needs to be done only once for an event so
// this is guarded with the CleanedUp flag.
//
PI_CALL(piEventRelease(Event));
}
}

// Make a list of all the dependent events that must have signalled
Expand All @@ -3863,6 +3881,7 @@ static pi_result cleanupAfterEvent(pi_event Event) {
EventsToBeReleased);
PI_CALL(piEventRelease(DepEvent));
}

return PI_SUCCESS;
}

Expand Down Expand Up @@ -3924,6 +3943,9 @@ pi_result piEventRetain(pi_event Event) {

pi_result piEventRelease(pi_event Event) {
PI_ASSERT(Event, PI_INVALID_EVENT);
if (!Event->RefCount) {
die("piEventRelease: called on a destroyed event");
}

if (--(Event->RefCount) == 0) {
cleanupAfterEvent(Event);
Expand Down
6 changes: 6 additions & 0 deletions sycl/plugins/level_zero/pi_level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,12 @@ struct _pi_event : _pi_object {
// enqueued, and must then be released when this event has signalled.
// This list must be destroyed once the event has signalled.
_pi_ze_event_list_t WaitList;

// Tracks if the needed cleanupAfterEvent was already performed for
// a completed event. This allows to control that some cleanup
// actions are performed only once.
//
bool CleanedUp = {false};
};

struct _pi_program : _pi_object {
Expand Down