Skip to content

[UR][L0] Enable support for using L0 spec External Semaphores #17671

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 6 commits into from
Mar 27, 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
281 changes: 203 additions & 78 deletions unified-runtime/source/adapters/level_zero/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,59 +782,125 @@ ur_result_t urBindlessImagesImportExternalSemaphoreExp(
" {} function not supported!", __FUNCTION__);
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_intel_external_semaphore_exp_desc_t SemDesc = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_EXP_DESC, nullptr,
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD};
ze_intel_external_semaphore_exp_handle_t ExtSemaphoreHandle;
ze_intel_external_semaphore_desc_fd_exp_desc_t FDExpDesc = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_FD_EXP_DESC, nullptr, 0};
_ze_intel_external_semaphore_win32_exp_desc_t Win32ExpDesc = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WIN32_EXP_DESC, nullptr,
nullptr, nullptr};
void *pNext = const_cast<void *>(pExternalSemaphoreDesc->pNext);
while (pNext != nullptr) {
const ur_base_desc_t *BaseDesc = static_cast<const ur_base_desc_t *>(pNext);
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_FILE_DESCRIPTOR) {
auto FileDescriptor =
static_cast<const ur_exp_file_descriptor_t *>(pNext);
FDExpDesc.fd = FileDescriptor->fd;
SemDesc.pNext = &FDExpDesc;
switch (semHandleType) {
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_OPAQUE_FD:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_FD:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_FD;
break;
default:
return UR_RESULT_ERROR_INVALID_VALUE;
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
ze_external_semaphore_ext_desc_t SemDesc = {
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_EXT_DESC, nullptr,
ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_OPAQUE_FD};
ze_external_semaphore_ext_handle_t ExtSemaphoreHandle;
ze_external_semaphore_fd_ext_desc_t FDExpDesc = {
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_FD_EXT_DESC, nullptr, 0};
ze_external_semaphore_win32_ext_desc_t Win32ExpDesc = {
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WIN32_EXT_DESC, nullptr, nullptr,
nullptr};
void *pNext = const_cast<void *>(pExternalSemaphoreDesc->pNext);
while (pNext != nullptr) {
const ur_base_desc_t *BaseDesc =
static_cast<const ur_base_desc_t *>(pNext);
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_FILE_DESCRIPTOR) {
auto FileDescriptor =
static_cast<const ur_exp_file_descriptor_t *>(pNext);
FDExpDesc.fd = FileDescriptor->fd;
SemDesc.pNext = &FDExpDesc;
switch (semHandleType) {
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_OPAQUE_FD:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_OPAQUE_FD;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_FD:
SemDesc.flags =
ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_VK_TIMELINE_SEMAPHORE_FD;
break;
default:
return UR_RESULT_ERROR_INVALID_VALUE;
}
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_WIN32_HANDLE) {
SemDesc.pNext = &Win32ExpDesc;
auto Win32Handle = static_cast<const ur_exp_win32_handle_t *>(pNext);
switch (semHandleType) {
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_OPAQUE_WIN32;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT_DX12_FENCE:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_D3D12_FENCE;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_WIN32_NT:
SemDesc.flags =
ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_VK_TIMELINE_SEMAPHORE_WIN32;
break;
default:
return UR_RESULT_ERROR_INVALID_VALUE;
}
Win32ExpDesc.handle = Win32Handle->handle;
}
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_WIN32_HANDLE) {
SemDesc.pNext = &Win32ExpDesc;
auto Win32Handle = static_cast<const ur_exp_win32_handle_t *>(pNext);
switch (semHandleType) {
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_WIN32;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT_DX12_FENCE:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_D3D12_FENCE;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_WIN32_NT:
SemDesc.flags =
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_WIN32;
break;
default:
return UR_RESULT_ERROR_INVALID_VALUE;
pNext = const_cast<void *>(BaseDesc->pNext);
}
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt.zexImportExternalSemaphoreExp,
(hDevice->ZeDevice, &SemDesc, &ExtSemaphoreHandle));
*phExternalSemaphoreHandle =
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;

} else {
ze_intel_external_semaphore_exp_desc_t SemDesc = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_EXP_DESC, nullptr,
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD};
ze_intel_external_semaphore_exp_handle_t ExtSemaphoreHandle;
ze_intel_external_semaphore_desc_fd_exp_desc_t FDExpDesc = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_FD_EXP_DESC, nullptr, 0};
_ze_intel_external_semaphore_win32_exp_desc_t Win32ExpDesc = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WIN32_EXP_DESC, nullptr,
nullptr, nullptr};
void *pNext = const_cast<void *>(pExternalSemaphoreDesc->pNext);
while (pNext != nullptr) {
const ur_base_desc_t *BaseDesc =
static_cast<const ur_base_desc_t *>(pNext);
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_FILE_DESCRIPTOR) {
auto FileDescriptor =
static_cast<const ur_exp_file_descriptor_t *>(pNext);
FDExpDesc.fd = FileDescriptor->fd;
SemDesc.pNext = &FDExpDesc;
switch (semHandleType) {
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_OPAQUE_FD:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_FD:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_FD;
break;
default:
return UR_RESULT_ERROR_INVALID_VALUE;
}
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_WIN32_HANDLE) {
SemDesc.pNext = &Win32ExpDesc;
auto Win32Handle = static_cast<const ur_exp_win32_handle_t *>(pNext);
switch (semHandleType) {
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_WIN32;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT_DX12_FENCE:
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_D3D12_FENCE;
break;
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_WIN32_NT:
SemDesc.flags =
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_WIN32;
break;
default:
return UR_RESULT_ERROR_INVALID_VALUE;
}
Win32ExpDesc.handle = Win32Handle->handle;
}
Win32ExpDesc.handle = Win32Handle->handle;
pNext = const_cast<void *>(BaseDesc->pNext);
}
pNext = const_cast<void *>(BaseDesc->pNext);
}

ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt.zexImportExternalSemaphoreExp,
(hDevice->ZeDevice, &SemDesc, &ExtSemaphoreHandle));
*phExternalSemaphoreHandle =
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;
ze_device_handle_t translatedDevice;
ZE2UR_CALL(zelLoaderTranslateHandle, (ZEL_HANDLE_DEVICE, hDevice->ZeDevice,
(void **)&translatedDevice));
// If the L0 loader is not aware of the extension, the handles need to be
// translated
ZE2UR_CALL(
UrPlatform->ZeExternalSemaphoreExt.zexExpImportExternalSemaphoreExp,
(translatedDevice, &SemDesc, &ExtSemaphoreHandle));

*phExternalSemaphoreHandle =
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;
}

return UR_RESULT_SUCCESS;
}
Expand All @@ -849,9 +915,15 @@ ur_result_t urBindlessImagesReleaseExternalSemaphoreExp(
" {} function not supported!", __FUNCTION__);
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ZE2UR_CALL(
UrPlatform->ZeExternalSemaphoreExt.zexDeviceReleaseExternalSemaphoreExp,
((ze_intel_external_semaphore_exp_handle_t)hExternalSemaphore));
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
ZE2UR_CALL(
UrPlatform->ZeExternalSemaphoreExt.zexDeviceReleaseExternalSemaphoreExp,
((ze_external_semaphore_ext_handle_t)hExternalSemaphore));
} else {
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
.zexExpDeviceReleaseExternalSemaphoreExp,
((ze_intel_external_semaphore_exp_handle_t)hExternalSemaphore));
}

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -898,15 +970,44 @@ ur_result_t urBindlessImagesWaitExternalSemaphoreExp(
const auto &ZeCommandList = CommandList->first;
const auto &WaitList = (*Event)->WaitList;

ze_intel_external_semaphore_wait_params_exp_t WaitParams = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXP, nullptr, 0};
WaitParams.value = hasValue ? waitValue : 0;
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
.zexCommandListAppendWaitExternalSemaphoresExp,
(ZeCommandList, 1, &hExtSemaphore, &WaitParams, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
ze_external_semaphore_wait_params_ext_t WaitParams = {
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXT, nullptr, 0};
WaitParams.value = hasValue ? waitValue : 0;
ze_external_semaphore_ext_handle_t hExtSemaphore =
reinterpret_cast<ze_external_semaphore_ext_handle_t>(hSemaphore);
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
.zexCommandListAppendWaitExternalSemaphoresExp,
(ZeCommandList, 1, &hExtSemaphore, &WaitParams, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
} else {
ze_command_list_handle_t translatedCommandList;
ZE2UR_CALL(zelLoaderTranslateHandle,
(ZEL_HANDLE_COMMAND_LIST, ZeCommandList,
(void **)&translatedCommandList));
ze_event_handle_t translatedEvent = ZeEvent;
if (ZeEvent) {
ZE2UR_CALL(zelLoaderTranslateHandle,
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&translatedEvent));
}
std::vector<ze_event_handle_t> EventHandles(WaitList.Length + 1, nullptr);
if (WaitList.Length > 0) {
for (size_t i = 0; i < WaitList.Length; i++) {
ze_event_handle_t ZeEvent = WaitList.ZeEventList[i];
ZE2UR_CALL(zelLoaderTranslateHandle,
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&EventHandles[i + 1]));
}
}
ze_intel_external_semaphore_wait_params_exp_t WaitParams = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXP, nullptr, 0};
WaitParams.value = hasValue ? waitValue : 0;
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
.zexExpCommandListAppendWaitExternalSemaphoresExp,
(translatedCommandList, 1, &hExtSemaphore, &WaitParams,
translatedEvent, WaitList.Length, EventHandles.data()));
}

return UR_RESULT_SUCCESS;
}
Expand All @@ -915,13 +1016,6 @@ ur_result_t urBindlessImagesSignalExternalSemaphoreExp(
ur_queue_handle_t hQueue, ur_exp_external_semaphore_handle_t hSemaphore,
bool hasValue, uint64_t signalValue, uint32_t numEventsInWaitList,
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
std::ignore = hQueue;
std::ignore = hSemaphore;
std::ignore = hasValue;
std::ignore = signalValue;
std::ignore = numEventsInWaitList;
std::ignore = phEventWaitList;
std::ignore = phEvent;
auto UrPlatform = hQueue->Context->getPlatform();
if (UrPlatform->ZeExternalSemaphoreExt.Supported == false) {
logger::error(logger::LegacyMessage("[UR][L0] "),
Expand Down Expand Up @@ -960,16 +1054,47 @@ ur_result_t urBindlessImagesSignalExternalSemaphoreExp(
const auto &ZeCommandList = CommandList->first;
const auto &WaitList = (*Event)->WaitList;

ze_intel_external_semaphore_signal_params_exp_t SignalParams = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXP, nullptr, 0};
SignalParams.value = hasValue ? signalValue : 0;
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);

ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
.zexCommandListAppendSignalExternalSemaphoresExp,
(ZeCommandList, 1, &hExtSemaphore, &SignalParams, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
ze_external_semaphore_signal_params_ext_t SignalParams = {
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXT, nullptr, 0};
SignalParams.value = hasValue ? signalValue : 0;
ze_external_semaphore_ext_handle_t hExtSemaphore =
reinterpret_cast<ze_external_semaphore_ext_handle_t>(hSemaphore);

ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
.zexCommandListAppendSignalExternalSemaphoresExp,
(ZeCommandList, 1, &hExtSemaphore, &SignalParams, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
} else {
ze_intel_external_semaphore_signal_params_exp_t SignalParams = {
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXP, nullptr,
0};
SignalParams.value = hasValue ? signalValue : 0;
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);

ze_command_list_handle_t translatedCommandList;
ZE2UR_CALL(zelLoaderTranslateHandle,
(ZEL_HANDLE_COMMAND_LIST, ZeCommandList,
(void **)&translatedCommandList));
ze_event_handle_t translatedEvent = ZeEvent;
if (ZeEvent) {
ZE2UR_CALL(zelLoaderTranslateHandle,
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&translatedEvent));
}
std::vector<ze_event_handle_t> EventHandles(WaitList.Length + 1, nullptr);
if (WaitList.Length > 0) {
for (size_t i = 0; i < WaitList.Length; i++) {
ze_event_handle_t ZeEvent = WaitList.ZeEventList[i];
ZE2UR_CALL(zelLoaderTranslateHandle,
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&EventHandles[i + 1]));
}
}
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
.zexExpCommandListAppendSignalExternalSemaphoresExp,
(translatedCommandList, 1, &hExtSemaphore, &SignalParams,
translatedEvent, WaitList.Length, EventHandles.data()));
}

return UR_RESULT_SUCCESS;
}
Expand Down
Loading