Skip to content

Commit f7b6b96

Browse files
committed
[SYCL] Fix handling of discarded events in preview lib
The queue implementation assumed that handler.finalize() will return either a valid (non-discarded) event or nullptr (that's why parseEvent is nop in preview mode). However, that was not always true. It was possible for scheduler to only mark an event as discarded after handler.finalize() completed (during actual command execution). This resulted in discarded event being stored in LastEventPtr in the queue and calls to wait() on that discarded event (which is not allowed) in MT scenarios. Fix this, by modyfing addCG() to mark the event as discarded immediately and to return nullptr is the event is discarded.
1 parent 3b13547 commit f7b6b96

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

sycl/source/detail/scheduler/commands.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ ExecCGCommand::ExecCGCommand(
19611961
const std::vector<ur_exp_command_buffer_sync_point_t> &Dependencies)
19621962
: Command(CommandType::RUN_CG, std::move(Queue), CommandBuffer,
19631963
Dependencies),
1964-
MEventNeeded(EventNeeded), MCommandGroup(std::move(CommandGroup)) {
1964+
MDiscardEvent(!EventNeeded), MCommandGroup(std::move(CommandGroup)) {
19651965
if (MCommandGroup->getType() == detail::CGType::CodeplayHostTask) {
19661966
MEvent->setSubmittedQueue(
19671967
static_cast<detail::CGHostTask *>(MCommandGroup.get())->MQueue);
@@ -3132,22 +3132,17 @@ ur_result_t ExecCGCommand::enqueueImpQueue() {
31323132
auto RawEvents = getUrEvents(EventImpls);
31333133
flushCrossQueueDeps(EventImpls, MWorkerQueue);
31343134

3135-
// We can omit creating a UR event and create a "discarded" event if the
3136-
// command has been explicitly marked as not needing an event, e.g. if the
3137-
// user did not ask for one, and there are no requirements.
3138-
bool DiscardUrEvent = MQueue && !MEventNeeded &&
3139-
MQueue->supportsDiscardingPiEvents() &&
3140-
MCommandGroup->getRequirements().size() == 0;
3141-
31423135
ur_event_handle_t UREvent = nullptr;
3143-
ur_event_handle_t *Event = DiscardUrEvent ? nullptr : &UREvent;
3144-
detail::event_impl *EventImpl = DiscardUrEvent ? nullptr : MEvent.get();
3136+
ur_event_handle_t *Event = MDiscardEvent ? nullptr : &UREvent;
3137+
detail::event_impl *EventImpl = MDiscardEvent ? nullptr : MEvent.get();
31453138

31463139
auto SetEventHandleOrDiscard = [&]() {
3147-
if (Event)
3140+
if (MDiscardEvent) {
3141+
assert(MEvent->isDiscarded());
3142+
} else {
3143+
assert(!MEvent->isDiscarded());
31483144
MEvent->setHandle(*Event);
3149-
else
3150-
MEvent->setStateDiscarded();
3145+
}
31513146
};
31523147

31533148
switch (MCommandGroup->getType()) {

sycl/source/detail/scheduler/commands.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,10 +661,10 @@ class ExecCGCommand : public Command {
661661
// the cleanup process.
662662
EmptyCommand *MEmptyCmd = nullptr;
663663

664-
// MEventNeeded is true if the command needs to produce a valid event. The
664+
// MDiscardEvent is false if the command needs to produce a valid event. The
665665
// implementation may elect to not produce events (native or SYCL) if this
666-
// is false.
667-
bool MEventNeeded = true;
666+
// is true.
667+
bool MDiscardEvent = false;
668668

669669
bool producesPiEvent() const final;
670670

sycl/source/detail/scheduler/scheduler.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,22 @@ EventImplPtr Scheduler::addCG(
133133
}
134134
NewEvent = NewCmd->getEvent();
135135
NewEvent->setSubmissionTime();
136+
if (!EventNeeded) {
137+
NewEvent->setStateDiscarded();
138+
}
136139
}
137140

138141
enqueueCommandForCG(NewEvent, AuxiliaryCmds);
139142

140143
if (!AuxiliaryResources.empty())
141144
registerAuxiliaryResources(NewEvent, std::move(AuxiliaryResources));
142145

143-
return NewEvent;
146+
if (EventNeeded) {
147+
return NewEvent;
148+
} else {
149+
assert(NewEvent->isDiscarded());
150+
return nullptr;
151+
}
144152
}
145153

146154
void Scheduler::enqueueCommandForCG(EventImplPtr NewEvent,

sycl/source/handler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,12 @@ event handler::finalize() {
895895
#endif
896896
}
897897

898+
bool DiscardEvent = !impl->MEventNeeded && Queue &&
899+
Queue->supportsDiscardingPiEvents() &&
900+
CommandGroup->getRequirements().size() == 0;
901+
898902
detail::EventImplPtr Event = detail::Scheduler::getInstance().addCG(
899-
std::move(CommandGroup), Queue->shared_from_this(), impl->MEventNeeded);
903+
std::move(CommandGroup), Queue->shared_from_this(), !DiscardEvent);
900904

901905
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
902906
MLastEvent = Event;

0 commit comments

Comments
 (0)