Skip to content

Commit 0b07279

Browse files
committed
Retain queue when storing execution event in command buffer
Otherwise the event might be returned to the eventPool that has already been destroyed.
1 parent 34b79b3 commit 0b07279

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

unified-runtime/source/adapters/level_zero/v2/command_buffer.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ ur_exp_command_buffer_handle_t_::ur_exp_command_buffer_handle_t_(
7474
isInOrder(desc ? desc->isInOrder : false),
7575
commandListManager(
7676
context, device,
77-
std::forward<v2::raii::command_list_unique_handle>(commandList))
78-
{}
77+
std::forward<v2::raii::command_list_unique_handle>(commandList)) {}
7978

8079
ur_exp_command_buffer_sync_point_t
8180
ur_exp_command_buffer_handle_t_::getSyncPoint(ur_event_handle_t event) {
@@ -151,13 +150,25 @@ ur_event_handle_t ur_exp_command_buffer_handle_t_::getExecutionEventUnlocked() {
151150

152151
ur_result_t ur_exp_command_buffer_handle_t_::registerExecutionEventUnlocked(
153152
ur_event_handle_t nextExecutionEvent) {
153+
assert(nextExecutionEvent);
154+
assert(nextExecutionEvent->getQueue());
155+
156+
auto nextExecutionQueue = nextExecutionEvent->getURQueueHandle();
157+
auto currentExecutionQueue =
158+
currentExecution ? currentExecution->getURQueueHandle() : nullptr;
159+
154160
if (currentExecution) {
155161
UR_CALL(currentExecution->release());
156-
currentExecution = nullptr;
157162
}
158-
if (nextExecutionEvent) {
159-
currentExecution = nextExecutionEvent;
163+
164+
currentExecution = nextExecutionEvent;
165+
166+
if (currentExecutionQueue != nextExecutionQueue) {
167+
if (currentExecutionQueue)
168+
UR_CALL(currentExecutionQueue->queueRelease());
169+
UR_CALL(nextExecutionQueue->queueRetain());
160170
}
171+
161172
return UR_RESULT_SUCCESS;
162173
}
163174

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ ur_event_handle_t_::getEventEndTimestampAndHandle() {
192192

193193
ur_queue_t_ *ur_event_handle_t_::getQueue() const { return hQueue; }
194194

195+
ur_queue_handle_t ur_event_handle_t_::getURQueueHandle() const {
196+
auto urHandle = reinterpret_cast<uintptr_t>(getQueue()) -
197+
ur_queue_handle_t_::queue_offset;
198+
return reinterpret_cast<ur_queue_handle_t>(urHandle);
199+
}
200+
195201
ur_context_handle_t ur_event_handle_t_::getContext() const { return hContext; }
196202

197203
ur_command_t ur_event_handle_t_::getCommandType() const { return commandType; }
@@ -261,9 +267,7 @@ ur_result_t urEventGetInfo(ur_event_handle_t hEvent, ur_event_info_t propName,
261267
return returnValue(hEvent->RefCount.load());
262268
}
263269
case UR_EVENT_INFO_COMMAND_QUEUE: {
264-
auto urQueueHandle = reinterpret_cast<uintptr_t>(hEvent->getQueue()) -
265-
ur_queue_handle_t_::queue_offset;
266-
return returnValue(urQueueHandle);
270+
return returnValue(hEvent->getURQueueHandle());
267271
}
268272
case UR_EVENT_INFO_CONTEXT: {
269273
return returnValue(hEvent->getContext());

unified-runtime/source/adapters/level_zero/v2/event.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ struct ur_event_handle_t_ : ur_object {
9191
// Queue associated with this event. Can be nullptr (for native events)
9292
ur_queue_t_ *getQueue() const;
9393

94+
// UR handle associated with this event's queue.
95+
ur_queue_handle_t getURQueueHandle() const;
96+
9497
// Context associated with this event
9598
ur_context_handle_t getContext() const;
9699

0 commit comments

Comments
 (0)