Skip to content

Commit ed4211c

Browse files
authored
Merge pull request #1706 from Bensuo/ewan/fix_leak
Fix L0 Event leak without return sync point
2 parents cb371ca + c0a422f commit ed4211c

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

source/adapters/level_zero/command_buffer.cpp

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static ur_result_t enqueueCommandBufferMemCopyHelper(
305305
ur_command_t CommandType, ur_exp_command_buffer_handle_t CommandBuffer,
306306
void *Dst, const void *Src, size_t Size, uint32_t NumSyncPointsInWaitList,
307307
const ur_exp_command_buffer_sync_point_t *SyncPointWaitList,
308-
ur_exp_command_buffer_sync_point_t *SyncPoint) {
308+
ur_exp_command_buffer_sync_point_t *RetSyncPoint) {
309309
if (CommandBuffer->IsInOrderCmdList) {
310310
ZE2UR_CALL(
311311
zeCommandListAppendMemoryCopy,
@@ -323,8 +323,12 @@ static ur_result_t enqueueCommandBufferMemCopyHelper(
323323
LaunchEvent->CommandType = CommandType;
324324

325325
// Get sync point and register the event with it.
326-
*SyncPoint = CommandBuffer->GetNextSyncPoint();
327-
CommandBuffer->RegisterSyncPoint(*SyncPoint, LaunchEvent);
326+
ur_exp_command_buffer_sync_point_t SyncPoint =
327+
CommandBuffer->GetNextSyncPoint();
328+
CommandBuffer->RegisterSyncPoint(SyncPoint, LaunchEvent);
329+
if (RetSyncPoint) {
330+
*RetSyncPoint = SyncPoint;
331+
}
328332

329333
ZE2UR_CALL(zeCommandListAppendMemoryCopy,
330334
(CommandBuffer->ZeCommandList, Dst, Src, Size,
@@ -346,7 +350,7 @@ static ur_result_t enqueueCommandBufferMemCopyRectHelper(
346350
size_t DstRowPitch, size_t SrcSlicePitch, size_t DstSlicePitch,
347351
uint32_t NumSyncPointsInWaitList,
348352
const ur_exp_command_buffer_sync_point_t *SyncPointWaitList,
349-
ur_exp_command_buffer_sync_point_t *SyncPoint) {
353+
ur_exp_command_buffer_sync_point_t *RetSyncPoint) {
350354

351355
uint32_t SrcOriginX = ur_cast<uint32_t>(SrcOrigin.x);
352356
uint32_t SrcOriginY = ur_cast<uint32_t>(SrcOrigin.y);
@@ -398,8 +402,12 @@ static ur_result_t enqueueCommandBufferMemCopyRectHelper(
398402
LaunchEvent->CommandType = CommandType;
399403

400404
// Get sync point and register the event with it.
401-
*SyncPoint = CommandBuffer->GetNextSyncPoint();
402-
CommandBuffer->RegisterSyncPoint(*SyncPoint, LaunchEvent);
405+
ur_exp_command_buffer_sync_point_t SyncPoint =
406+
CommandBuffer->GetNextSyncPoint();
407+
CommandBuffer->RegisterSyncPoint(SyncPoint, LaunchEvent);
408+
if (RetSyncPoint) {
409+
*RetSyncPoint = SyncPoint;
410+
}
403411

404412
ZE2UR_CALL(zeCommandListAppendMemoryCopyRegion,
405413
(CommandBuffer->ZeCommandList, Dst, &ZeDstRegion, DstPitch,
@@ -420,7 +428,7 @@ static ur_result_t enqueueCommandBufferFillHelper(
420428
void *Ptr, const void *Pattern, size_t PatternSize, size_t Size,
421429
uint32_t NumSyncPointsInWaitList,
422430
const ur_exp_command_buffer_sync_point_t *SyncPointWaitList,
423-
ur_exp_command_buffer_sync_point_t *SyncPoint) {
431+
ur_exp_command_buffer_sync_point_t *RetSyncPoint) {
424432
// Pattern size must be a power of two.
425433
UR_ASSERT((PatternSize > 0) && ((PatternSize & (PatternSize - 1)) == 0),
426434
UR_RESULT_ERROR_INVALID_VALUE);
@@ -451,8 +459,12 @@ static ur_result_t enqueueCommandBufferFillHelper(
451459
LaunchEvent->CommandType = CommandType;
452460

453461
// Get sync point and register the event with it.
454-
*SyncPoint = CommandBuffer->GetNextSyncPoint();
455-
CommandBuffer->RegisterSyncPoint(*SyncPoint, LaunchEvent);
462+
ur_exp_command_buffer_sync_point_t SyncPoint =
463+
CommandBuffer->GetNextSyncPoint();
464+
CommandBuffer->RegisterSyncPoint(SyncPoint, LaunchEvent);
465+
if (RetSyncPoint) {
466+
*RetSyncPoint = SyncPoint;
467+
}
456468

457469
ZE2UR_CALL(zeCommandListAppendMemoryFill,
458470
(CommandBuffer->ZeCommandList, Ptr, Pattern, PatternSize, Size,
@@ -615,7 +627,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
615627
const size_t *GlobalWorkSize, const size_t *LocalWorkSize,
616628
uint32_t NumSyncPointsInWaitList,
617629
const ur_exp_command_buffer_sync_point_t *SyncPointWaitList,
618-
ur_exp_command_buffer_sync_point_t *SyncPoint,
630+
ur_exp_command_buffer_sync_point_t *RetSyncPoint,
619631
ur_exp_command_buffer_command_handle_t *Command) {
620632
UR_ASSERT(CommandBuffer && Kernel && Kernel->Program,
621633
UR_RESULT_ERROR_INVALID_NULL_POINTER);
@@ -711,10 +723,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
711723
!CommandBuffer->IsProfilingEnabled));
712724
LaunchEvent->CommandType = UR_COMMAND_KERNEL_LAUNCH;
713725

714-
if (SyncPoint) {
715-
// Get sync point and register the event with it.
716-
*SyncPoint = CommandBuffer->GetNextSyncPoint();
717-
CommandBuffer->RegisterSyncPoint(*SyncPoint, LaunchEvent);
726+
// Get sync point and register the event with it.
727+
ur_exp_command_buffer_sync_point_t SyncPoint =
728+
CommandBuffer->GetNextSyncPoint();
729+
CommandBuffer->RegisterSyncPoint(SyncPoint, LaunchEvent);
730+
if (RetSyncPoint) {
731+
*RetSyncPoint = SyncPoint;
718732
}
719733

720734
ZE2UR_CALL(zeCommandListAppendLaunchKernel,
@@ -872,7 +886,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMPrefetchExp(
872886
ur_exp_command_buffer_handle_t CommandBuffer, const void *Mem, size_t Size,
873887
ur_usm_migration_flags_t Flags, uint32_t NumSyncPointsInWaitList,
874888
const ur_exp_command_buffer_sync_point_t *SyncPointWaitList,
875-
ur_exp_command_buffer_sync_point_t *SyncPoint) {
889+
ur_exp_command_buffer_sync_point_t *RetSyncPoint) {
876890
std::ignore = Flags;
877891

878892
if (CommandBuffer->IsInOrderCmdList) {
@@ -898,8 +912,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMPrefetchExp(
898912
LaunchEvent->CommandType = UR_COMMAND_USM_PREFETCH;
899913

900914
// Get sync point and register the event with it.
901-
*SyncPoint = CommandBuffer->GetNextSyncPoint();
902-
CommandBuffer->RegisterSyncPoint(*SyncPoint, LaunchEvent);
915+
ur_exp_command_buffer_sync_point_t SyncPoint =
916+
CommandBuffer->GetNextSyncPoint();
917+
CommandBuffer->RegisterSyncPoint(SyncPoint, LaunchEvent);
918+
if (RetSyncPoint) {
919+
*RetSyncPoint = SyncPoint;
920+
}
903921

904922
// Add the prefetch command to the command buffer.
905923
// Note that L0 does not handle migration flags.
@@ -919,7 +937,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMAdviseExp(
919937
ur_exp_command_buffer_handle_t CommandBuffer, const void *Mem, size_t Size,
920938
ur_usm_advice_flags_t Advice, uint32_t NumSyncPointsInWaitList,
921939
const ur_exp_command_buffer_sync_point_t *SyncPointWaitList,
922-
ur_exp_command_buffer_sync_point_t *SyncPoint) {
940+
ur_exp_command_buffer_sync_point_t *RetSyncPoint) {
923941
// A memory chunk can be advised with muliple memory advices
924942
// We therefore prefer if statements to switch cases to combine all potential
925943
// flags
@@ -969,8 +987,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMAdviseExp(
969987
LaunchEvent->CommandType = UR_COMMAND_USM_ADVISE;
970988

971989
// Get sync point and register the event with it.
972-
*SyncPoint = CommandBuffer->GetNextSyncPoint();
973-
CommandBuffer->RegisterSyncPoint(*SyncPoint, LaunchEvent);
990+
ur_exp_command_buffer_sync_point_t SyncPoint =
991+
CommandBuffer->GetNextSyncPoint();
992+
CommandBuffer->RegisterSyncPoint(SyncPoint, LaunchEvent);
993+
if (RetSyncPoint) {
994+
*RetSyncPoint = SyncPoint;
995+
}
974996

975997
ZE2UR_CALL(zeCommandListAppendMemAdvise,
976998
(CommandBuffer->ZeCommandList, CommandBuffer->Device->ZeDevice,

0 commit comments

Comments
 (0)