Skip to content

[SYCL][UR][L0 v2] Move all enqueue-related logic to command_list_manager #18837

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 9, 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
195 changes: 63 additions & 132 deletions unified-runtime/source/adapters/level_zero/v2/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ ur_exp_command_buffer_handle_t_::ur_exp_command_buffer_handle_t_(
isInOrder(desc ? desc->isInOrder : false),
commandListManager(
context, device,
std::forward<v2::raii::command_list_unique_handle>(commandList),
isInOrder ? v2::EVENT_FLAGS_COUNTER : 0, nullptr,
PoolCacheType::Regular),
context(context), device(device) {}
std::forward<v2::raii::command_list_unique_handle>(commandList)),
context(context), device(device),
eventPool(context->getEventPoolCache(PoolCacheType::Regular)
.borrow(device->Id.value(),
isInOrder ? v2::EVENT_FLAGS_COUNTER : 0)) {}

ur_exp_command_buffer_sync_point_t
ur_exp_command_buffer_handle_t_::getSyncPoint(ur_event_handle_t event) {
Expand Down Expand Up @@ -155,7 +156,6 @@ ur_result_t ur_exp_command_buffer_handle_t_::registerExecutionEventUnlocked(
}
if (nextExecutionEvent) {
currentExecution = nextExecutionEvent;
UR_CALL(nextExecutionEvent->retain());
}
return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -202,6 +202,21 @@ ur_result_t ur_exp_command_buffer_handle_t_::applyUpdateCommands(

return UR_RESULT_SUCCESS;
}

ur_event_handle_t ur_exp_command_buffer_handle_t_::createEventIfRequested(
ur_exp_command_buffer_sync_point_t *retSyncPoint) {
if (retSyncPoint == nullptr) {
return nullptr;
}

auto event = eventPool->allocate();
event->setQueue(nullptr);

*retSyncPoint = getSyncPoint(event);

return event;
}

namespace ur::level_zero {

ur_result_t
Expand Down Expand Up @@ -292,18 +307,11 @@ ur_result_t urCommandBufferAppendKernelLaunchExp(
}
auto eventsWaitList = commandBuffer->getWaitListFromSyncPoints(
syncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (retSyncPoint != nullptr) {
event = &signalEvent;
}
UR_CALL(commandListLocked->appendKernelLaunch(
hKernel, workDim, pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize,
numSyncPointsInWaitList, eventsWaitList, event));

if (retSyncPoint != nullptr) {
*retSyncPoint = commandBuffer->getSyncPoint(signalEvent);
}
UR_CALL(commandListLocked->appendKernelLaunch(
hKernel, workDim, pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, 0,
nullptr, numSyncPointsInWaitList, eventsWaitList,
commandBuffer->createEventIfRequested(retSyncPoint)));

return UR_RESULT_SUCCESS;
} catch (...) {
Expand All @@ -324,17 +332,11 @@ ur_result_t urCommandBufferAppendUSMMemcpyExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendUSMMemcpy(
false, pDst, pSrc, size, numSyncPointsInWaitList, eventsWaitList, event));
false, pDst, pSrc, size, numSyncPointsInWaitList, eventsWaitList,
hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -357,18 +359,11 @@ ur_result_t urCommandBufferAppendMemBufferCopyExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendMemBufferCopy(
hSrcMem, hDstMem, srcOffset, dstOffset, size, numSyncPointsInWaitList,
eventsWaitList, event));
eventsWaitList, hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -391,18 +386,11 @@ ur_result_t urCommandBufferAppendMemBufferWriteExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}
UR_CALL(commandListLocked->appendMemBufferWrite(hBuffer, false, offset, size,
pSrc, numSyncPointsInWaitList,
eventsWaitList, event));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
UR_CALL(commandListLocked->appendMemBufferWrite(
hBuffer, false, offset, size, pSrc, numSyncPointsInWaitList,
eventsWaitList, hCommandBuffer->createEventIfRequested(pSyncPoint)));

return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -423,18 +411,11 @@ ur_result_t urCommandBufferAppendMemBufferReadExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}
UR_CALL(commandListLocked->appendMemBufferRead(hBuffer, false, offset, size,
pDst, numSyncPointsInWaitList,
eventsWaitList, event));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
UR_CALL(commandListLocked->appendMemBufferRead(
hBuffer, false, offset, size, pDst, numSyncPointsInWaitList,
eventsWaitList, hCommandBuffer->createEventIfRequested(pSyncPoint)));

return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -459,19 +440,12 @@ ur_result_t urCommandBufferAppendMemBufferCopyRectExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendMemBufferCopyRect(
hSrcMem, hDstMem, srcOrigin, dstOrigin, region, srcRowPitch,
srcSlicePitch, dstRowPitch, dstSlicePitch, numSyncPointsInWaitList,
eventsWaitList, event));
eventsWaitList, hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -496,19 +470,13 @@ ur_result_t urCommandBufferAppendMemBufferWriteRectExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendMemBufferWriteRect(
hBuffer, false, bufferOffset, hostOffset, region, bufferRowPitch,
bufferSlicePitch, hostRowPitch, hostSlicePitch, pSrc,
numSyncPointsInWaitList, eventsWaitList, event));
numSyncPointsInWaitList, eventsWaitList,
hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -533,19 +501,13 @@ ur_result_t urCommandBufferAppendMemBufferReadRectExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendMemBufferReadRect(
hBuffer, false, bufferOffset, hostOffset, region, bufferRowPitch,
bufferSlicePitch, hostRowPitch, hostSlicePitch, pDst,
numSyncPointsInWaitList, eventsWaitList, event));
numSyncPointsInWaitList, eventsWaitList,
hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -565,17 +527,10 @@ ur_result_t urCommandBufferAppendUSMFillExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}
UR_CALL(commandListLocked->appendUSMFill(pMemory, patternSize, pPattern, size,
numSyncPointsInWaitList,
eventsWaitList, event));
if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}

UR_CALL(commandListLocked->appendUSMFill(
pMemory, patternSize, pPattern, size, numSyncPointsInWaitList,
eventsWaitList, hCommandBuffer->createEventIfRequested(pSyncPoint)));
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -596,17 +551,11 @@ ur_result_t urCommandBufferAppendMemBufferFillExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendMemBufferFill(
hBuffer, pPattern, patternSize, offset, size, numSyncPointsInWaitList,
eventsWaitList, event));
if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
eventsWaitList, hCommandBuffer->createEventIfRequested(pSyncPoint)));

return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -628,17 +577,11 @@ ur_result_t urCommandBufferAppendUSMPrefetchExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendUSMPrefetch(
pMemory, size, flags, numSyncPointsInWaitList, eventsWaitList, event));
pMemory, size, flags, numSyncPointsInWaitList, eventsWaitList,
hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand All @@ -658,17 +601,11 @@ ur_result_t urCommandBufferAppendUSMAdviseExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}

UR_CALL(commandListLocked->appendUSMAdvise(
pMemory, size, advice, numSyncPointsInWaitList, eventsWaitList, event));
pMemory, size, advice, numSyncPointsInWaitList, eventsWaitList,
hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
} catch (...) {
return exceptionToResult(std::current_exception());
Expand Down Expand Up @@ -714,23 +651,17 @@ ur_result_t urCommandBufferAppendNativeCommandExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
auto eventsWaitList = hCommandBuffer->getWaitListFromSyncPoints(
pSyncPointWaitList, numSyncPointsInWaitList);
ur_event_handle_t *event = nullptr;
ur_event_handle_t signalEvent = nullptr;
if (pSyncPoint != nullptr) {
event = &signalEvent;
}
UR_CALL(commandListLocked->appendBarrier(numSyncPointsInWaitList,
eventsWaitList, nullptr));

UR_CALL(commandListLocked->appendEventsWaitWithBarrier(
numSyncPointsInWaitList, eventsWaitList, nullptr));

// Call user-defined function immediately
pfnNativeCommand(pData);

// Barrier on all commands after user defined commands.
UR_CALL(commandListLocked->appendBarrier(0, nullptr, event));
UR_CALL(commandListLocked->appendEventsWaitWithBarrier(
0, nullptr, hCommandBuffer->createEventIfRequested(pSyncPoint)));

if (pSyncPoint != nullptr) {
*pSyncPoint = hCommandBuffer->getSyncPoint(signalEvent);
}
return UR_RESULT_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ struct ur_exp_command_buffer_handle_t_ : public ur_object {
const ur_exp_command_buffer_sync_point_t *pSyncPointWaitList,
uint32_t numSyncPointsInWaitList);

ur_event_handle_t
createEventIfRequested(ur_exp_command_buffer_sync_point_t *retSyncPoint);

private:
// Stores all sync points that are created by the command buffer.
std::vector<ur_event_handle_t> syncPoints;
Expand All @@ -77,4 +80,6 @@ struct ur_exp_command_buffer_handle_t_ : public ur_object {
bool isFinalized = false;

ur_event_handle_t currentExecution = nullptr;

v2::raii::cache_borrowed_event_pool eventPool;
};
Loading