Skip to content

Commit c685944

Browse files
authored
Merge pull request #2589 from Bensuo/fabio/fix_potential_race_condition
Fix potential deadlock in the WaitEvent path of CmdBuffers
2 parents d3e9704 + b1b0c60 commit c685944

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

source/adapters/level_zero/command_buffer.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,15 +1535,17 @@ ur_result_t waitForDependencies(ur_exp_command_buffer_handle_t CommandBuffer,
15351535
* @param CommandList The command-list to append the QueryKernelTimestamps
15361536
* command to.
15371537
* @param SignalEvent The event that must be signaled after the profiling is
1538-
* finished. This event will contain the profiling information.
1538+
* finished.
15391539
* @param WaitEvent The event that must be waited on before starting the
15401540
* profiling.
1541+
* @param ProfilingEvent The event that will contain the profiling data.
15411542
* @return UR_RESULT_SUCCESS or an error code on failure.
15421543
*/
15431544
ur_result_t appendProfilingQueries(ur_exp_command_buffer_handle_t CommandBuffer,
15441545
ze_command_list_handle_t CommandList,
15451546
ur_event_handle_t SignalEvent,
1546-
ur_event_handle_t WaitEvent) {
1547+
ur_event_handle_t WaitEvent,
1548+
ur_event_handle_t ProfilingEvent) {
15471549
// Multiple submissions of a command buffer implies that we need to save
15481550
// the event timestamps before resubmiting the command buffer. We
15491551
// therefore copy these timestamps in a dedicated USM memory section
@@ -1556,12 +1558,17 @@ ur_result_t appendProfilingQueries(ur_exp_command_buffer_handle_t CommandBuffer,
15561558
Profiling->Timestamps =
15571559
new ze_kernel_timestamp_result_t[Profiling->NumEvents];
15581560

1561+
uint32_t NumWaitEvents = WaitEvent ? 1 : 0;
1562+
ze_event_handle_t *ZeWaitEventList =
1563+
WaitEvent ? &(WaitEvent->ZeEvent) : nullptr;
1564+
ze_event_handle_t ZeSignalEvent =
1565+
SignalEvent ? SignalEvent->ZeEvent : nullptr;
15591566
ZE2UR_CALL(zeCommandListAppendQueryKernelTimestamps,
15601567
(CommandList, CommandBuffer->ZeEventsList.size(),
15611568
CommandBuffer->ZeEventsList.data(), (void *)Profiling->Timestamps,
1562-
0, SignalEvent->ZeEvent, 1, &(WaitEvent->ZeEvent)));
1569+
0, ZeSignalEvent, NumWaitEvents, ZeWaitEventList));
15631570

1564-
SignalEvent->CommandData = static_cast<void *>(Profiling);
1571+
ProfilingEvent->CommandData = static_cast<void *>(Profiling);
15651572

15661573
return UR_RESULT_SUCCESS;
15671574
}
@@ -1615,8 +1622,8 @@ ur_result_t enqueueImmediateAppendPath(
16151622

16161623
if (DoProfiling) {
16171624
UR_CALL(appendProfilingQueries(CommandBuffer, CommandListHelper->first,
1618-
*Event,
1619-
CommandBuffer->ComputeFinishedEvent));
1625+
*Event, CommandBuffer->ComputeFinishedEvent,
1626+
*Event));
16201627
}
16211628

16221629
// When the current execution is finished, signal ExecutionFinishedEvent to
@@ -1694,25 +1701,28 @@ ur_result_t enqueueWaitEventPath(ur_exp_command_buffer_handle_t CommandBuffer,
16941701
(ZeCopyCommandQueue, 1, &CommandBuffer->ZeCopyCommandList, nullptr));
16951702
}
16961703

1704+
ZE2UR_CALL(zeCommandListAppendBarrier,
1705+
(SignalCommandList->first, nullptr, 1,
1706+
&(CommandBuffer->ExecutionFinishedEvent->ZeEvent)));
1707+
16971708
// Reset the wait-event for the UR command-buffer that is signaled when its
16981709
// submission dependencies have been satisfied.
16991710
ZE2UR_CALL(zeCommandListAppendEventReset,
17001711
(SignalCommandList->first, CommandBuffer->WaitEvent->ZeEvent));
1712+
17011713
// Reset the all-reset-event for the UR command-buffer that is signaled when
17021714
// all events of the main command-list have been reset.
17031715
ZE2UR_CALL(zeCommandListAppendEventReset,
17041716
(SignalCommandList->first, CommandBuffer->AllResetEvent->ZeEvent));
17051717

17061718
if (DoProfiling) {
17071719
UR_CALL(appendProfilingQueries(CommandBuffer, SignalCommandList->first,
1708-
*Event,
1709-
CommandBuffer->ExecutionFinishedEvent));
1710-
} else {
1711-
ZE2UR_CALL(zeCommandListAppendBarrier,
1712-
(SignalCommandList->first, (*Event)->ZeEvent, 1,
1713-
&(CommandBuffer->ExecutionFinishedEvent->ZeEvent)));
1720+
nullptr, nullptr, *Event));
17141721
}
17151722

1723+
ZE2UR_CALL(zeCommandListAppendBarrier,
1724+
(SignalCommandList->first, (*Event)->ZeEvent, 0, nullptr));
1725+
17161726
UR_CALL(Queue->executeCommandList(SignalCommandList, false /*IsBlocking*/,
17171727
false /*OKToBatchCommand*/));
17181728

0 commit comments

Comments
 (0)