Skip to content

[SYCL] Fix handling of discarded events in preview lib #19005

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 4 commits into from
Jun 17, 2025
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
3 changes: 3 additions & 0 deletions sycl/include/sycl/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ class __SYCL_EXPORT handler {
/// object destruction.
///
/// \return a SYCL event object representing the command group
///
/// Note: in preview mode, handler.finalize() is expected to return
/// nullptr if the event is not needed (discarded).
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
detail::EventImplPtr finalize();
#else
Expand Down
6 changes: 5 additions & 1 deletion sycl/source/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,11 @@ class queue_impl : public std::enable_shared_from_this<queue_impl> {
}

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
#define parseEvent(arg) (arg)
inline const detail::EventImplPtr &
parseEvent(const detail::EventImplPtr &Event) {
assert(!Event || !Event->isDiscarded());
return Event;
}
#else
inline detail::EventImplPtr parseEvent(const event &Event) {
const detail::EventImplPtr &EventImpl = getSyclObjImpl(Event);
Expand Down
19 changes: 7 additions & 12 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3132,22 +3132,17 @@ ur_result_t ExecCGCommand::enqueueImpQueue() {
auto RawEvents = getUrEvents(EventImpls);
flushCrossQueueDeps(EventImpls, MWorkerQueue);

// We can omit creating a UR event and create a "discarded" event if the
// command has been explicitly marked as not needing an event, e.g. if the
// user did not ask for one, and there are no requirements.
bool DiscardUrEvent = MQueue && !MEventNeeded &&
MQueue->supportsDiscardingPiEvents() &&
MCommandGroup->getRequirements().size() == 0;

ur_event_handle_t UREvent = nullptr;
ur_event_handle_t *Event = DiscardUrEvent ? nullptr : &UREvent;
detail::event_impl *EventImpl = DiscardUrEvent ? nullptr : MEvent.get();
ur_event_handle_t *Event = !MEventNeeded ? nullptr : &UREvent;
detail::event_impl *EventImpl = !MEventNeeded ? nullptr : MEvent.get();

auto SetEventHandleOrDiscard = [&]() {
if (Event)
if (!MEventNeeded) {
assert(MEvent->isDiscarded());
} else {
assert(!MEvent->isDiscarded());
MEvent->setHandle(*Event);
else
MEvent->setStateDiscarded();
}
};

switch (MCommandGroup->getType()) {
Expand Down
8 changes: 8 additions & 0 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ EventImplPtr Scheduler::addCG(
}
NewEvent = NewCmd->getEvent();
NewEvent->setSubmissionTime();

// This is the last moment we can mark the event as discarded.
// Doing this during command execution would lead to incorrect
// event handling (as event would change it's state from non-discarded
// to discarded).
if (!EventNeeded) {
NewEvent->setStateDiscarded();
}
}

enqueueCommandForCG(NewEvent, AuxiliaryCmds);
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/detail/scheduler/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ class Scheduler {
/// directly to the queue.
/// \param Dependencies Optional list of dependency
/// sync points when enqueuing to a command buffer.
/// \return an event object to wait on for command group completion.
/// \return an event object to wait on for command group completion. It can
/// be a discarded event.
EventImplPtr addCG(
std::unique_ptr<detail::CG> CommandGroup, const QueueImplPtr &Queue,
bool EventNeeded, ur_exp_command_buffer_handle_t CommandBuffer = nullptr,
Expand Down
8 changes: 6 additions & 2 deletions sycl/source/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,11 +895,15 @@ event handler::finalize() {
#endif
}

bool DiscardEvent = !impl->MEventNeeded && Queue &&
Queue->supportsDiscardingPiEvents() &&
CommandGroup->getRequirements().size() == 0;

detail::EventImplPtr Event = detail::Scheduler::getInstance().addCG(
std::move(CommandGroup), Queue->shared_from_this(), impl->MEventNeeded);
std::move(CommandGroup), Queue->shared_from_this(), !DiscardEvent);

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
MLastEvent = Event;
MLastEvent = DiscardEvent ? nullptr : Event;
#else
MLastEvent = detail::createSyclObjFromImpl<event>(Event);
#endif
Expand Down