Skip to content

Commit 449c008

Browse files
authored
[UR][L0] Enable support for using L0 spec External Semaphores (#17671)
- Implements the spec version of the L0 Extension External Semaphores - Implements handling the translation of handles if the driver only supports the EXP functions. --------- Signed-off-by: Neil R. Spruit <[email protected]>
1 parent 8fec832 commit 449c008

File tree

3 files changed

+289
-93
lines changed

3 files changed

+289
-93
lines changed

unified-runtime/source/adapters/level_zero/image.cpp

+203-78
Original file line numberDiff line numberDiff line change
@@ -782,59 +782,125 @@ ur_result_t urBindlessImagesImportExternalSemaphoreExp(
782782
" {} function not supported!", __FUNCTION__);
783783
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
784784
}
785-
ze_intel_external_semaphore_exp_desc_t SemDesc = {
786-
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_EXP_DESC, nullptr,
787-
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD};
788-
ze_intel_external_semaphore_exp_handle_t ExtSemaphoreHandle;
789-
ze_intel_external_semaphore_desc_fd_exp_desc_t FDExpDesc = {
790-
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_FD_EXP_DESC, nullptr, 0};
791-
_ze_intel_external_semaphore_win32_exp_desc_t Win32ExpDesc = {
792-
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WIN32_EXP_DESC, nullptr,
793-
nullptr, nullptr};
794-
void *pNext = const_cast<void *>(pExternalSemaphoreDesc->pNext);
795-
while (pNext != nullptr) {
796-
const ur_base_desc_t *BaseDesc = static_cast<const ur_base_desc_t *>(pNext);
797-
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_FILE_DESCRIPTOR) {
798-
auto FileDescriptor =
799-
static_cast<const ur_exp_file_descriptor_t *>(pNext);
800-
FDExpDesc.fd = FileDescriptor->fd;
801-
SemDesc.pNext = &FDExpDesc;
802-
switch (semHandleType) {
803-
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_OPAQUE_FD:
804-
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD;
805-
break;
806-
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_FD:
807-
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_FD;
808-
break;
809-
default:
810-
return UR_RESULT_ERROR_INVALID_VALUE;
785+
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
786+
ze_external_semaphore_ext_desc_t SemDesc = {
787+
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_EXT_DESC, nullptr,
788+
ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_OPAQUE_FD};
789+
ze_external_semaphore_ext_handle_t ExtSemaphoreHandle;
790+
ze_external_semaphore_fd_ext_desc_t FDExpDesc = {
791+
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_FD_EXT_DESC, nullptr, 0};
792+
ze_external_semaphore_win32_ext_desc_t Win32ExpDesc = {
793+
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WIN32_EXT_DESC, nullptr, nullptr,
794+
nullptr};
795+
void *pNext = const_cast<void *>(pExternalSemaphoreDesc->pNext);
796+
while (pNext != nullptr) {
797+
const ur_base_desc_t *BaseDesc =
798+
static_cast<const ur_base_desc_t *>(pNext);
799+
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_FILE_DESCRIPTOR) {
800+
auto FileDescriptor =
801+
static_cast<const ur_exp_file_descriptor_t *>(pNext);
802+
FDExpDesc.fd = FileDescriptor->fd;
803+
SemDesc.pNext = &FDExpDesc;
804+
switch (semHandleType) {
805+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_OPAQUE_FD:
806+
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_OPAQUE_FD;
807+
break;
808+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_FD:
809+
SemDesc.flags =
810+
ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_VK_TIMELINE_SEMAPHORE_FD;
811+
break;
812+
default:
813+
return UR_RESULT_ERROR_INVALID_VALUE;
814+
}
815+
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_WIN32_HANDLE) {
816+
SemDesc.pNext = &Win32ExpDesc;
817+
auto Win32Handle = static_cast<const ur_exp_win32_handle_t *>(pNext);
818+
switch (semHandleType) {
819+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT:
820+
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_OPAQUE_WIN32;
821+
break;
822+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT_DX12_FENCE:
823+
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_D3D12_FENCE;
824+
break;
825+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_WIN32_NT:
826+
SemDesc.flags =
827+
ZE_EXTERNAL_SEMAPHORE_EXT_FLAG_VK_TIMELINE_SEMAPHORE_WIN32;
828+
break;
829+
default:
830+
return UR_RESULT_ERROR_INVALID_VALUE;
831+
}
832+
Win32ExpDesc.handle = Win32Handle->handle;
811833
}
812-
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_WIN32_HANDLE) {
813-
SemDesc.pNext = &Win32ExpDesc;
814-
auto Win32Handle = static_cast<const ur_exp_win32_handle_t *>(pNext);
815-
switch (semHandleType) {
816-
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT:
817-
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_WIN32;
818-
break;
819-
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT_DX12_FENCE:
820-
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_D3D12_FENCE;
821-
break;
822-
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_WIN32_NT:
823-
SemDesc.flags =
824-
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_WIN32;
825-
break;
826-
default:
827-
return UR_RESULT_ERROR_INVALID_VALUE;
834+
pNext = const_cast<void *>(BaseDesc->pNext);
835+
}
836+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt.zexImportExternalSemaphoreExp,
837+
(hDevice->ZeDevice, &SemDesc, &ExtSemaphoreHandle));
838+
*phExternalSemaphoreHandle =
839+
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;
840+
841+
} else {
842+
ze_intel_external_semaphore_exp_desc_t SemDesc = {
843+
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_EXP_DESC, nullptr,
844+
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD};
845+
ze_intel_external_semaphore_exp_handle_t ExtSemaphoreHandle;
846+
ze_intel_external_semaphore_desc_fd_exp_desc_t FDExpDesc = {
847+
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_FD_EXP_DESC, nullptr, 0};
848+
_ze_intel_external_semaphore_win32_exp_desc_t Win32ExpDesc = {
849+
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WIN32_EXP_DESC, nullptr,
850+
nullptr, nullptr};
851+
void *pNext = const_cast<void *>(pExternalSemaphoreDesc->pNext);
852+
while (pNext != nullptr) {
853+
const ur_base_desc_t *BaseDesc =
854+
static_cast<const ur_base_desc_t *>(pNext);
855+
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_FILE_DESCRIPTOR) {
856+
auto FileDescriptor =
857+
static_cast<const ur_exp_file_descriptor_t *>(pNext);
858+
FDExpDesc.fd = FileDescriptor->fd;
859+
SemDesc.pNext = &FDExpDesc;
860+
switch (semHandleType) {
861+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_OPAQUE_FD:
862+
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD;
863+
break;
864+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_FD:
865+
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_FD;
866+
break;
867+
default:
868+
return UR_RESULT_ERROR_INVALID_VALUE;
869+
}
870+
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_WIN32_HANDLE) {
871+
SemDesc.pNext = &Win32ExpDesc;
872+
auto Win32Handle = static_cast<const ur_exp_win32_handle_t *>(pNext);
873+
switch (semHandleType) {
874+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT:
875+
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_WIN32;
876+
break;
877+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_WIN32_NT_DX12_FENCE:
878+
SemDesc.flags = ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_D3D12_FENCE;
879+
break;
880+
case UR_EXP_EXTERNAL_SEMAPHORE_TYPE_TIMELINE_WIN32_NT:
881+
SemDesc.flags =
882+
ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_WIN32;
883+
break;
884+
default:
885+
return UR_RESULT_ERROR_INVALID_VALUE;
886+
}
887+
Win32ExpDesc.handle = Win32Handle->handle;
828888
}
829-
Win32ExpDesc.handle = Win32Handle->handle;
889+
pNext = const_cast<void *>(BaseDesc->pNext);
830890
}
831-
pNext = const_cast<void *>(BaseDesc->pNext);
832-
}
833891

834-
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt.zexImportExternalSemaphoreExp,
835-
(hDevice->ZeDevice, &SemDesc, &ExtSemaphoreHandle));
836-
*phExternalSemaphoreHandle =
837-
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;
892+
ze_device_handle_t translatedDevice;
893+
ZE2UR_CALL(zelLoaderTranslateHandle, (ZEL_HANDLE_DEVICE, hDevice->ZeDevice,
894+
(void **)&translatedDevice));
895+
// If the L0 loader is not aware of the extension, the handles need to be
896+
// translated
897+
ZE2UR_CALL(
898+
UrPlatform->ZeExternalSemaphoreExt.zexExpImportExternalSemaphoreExp,
899+
(translatedDevice, &SemDesc, &ExtSemaphoreHandle));
900+
901+
*phExternalSemaphoreHandle =
902+
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;
903+
}
838904

839905
return UR_RESULT_SUCCESS;
840906
}
@@ -849,9 +915,15 @@ ur_result_t urBindlessImagesReleaseExternalSemaphoreExp(
849915
" {} function not supported!", __FUNCTION__);
850916
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
851917
}
852-
ZE2UR_CALL(
853-
UrPlatform->ZeExternalSemaphoreExt.zexDeviceReleaseExternalSemaphoreExp,
854-
((ze_intel_external_semaphore_exp_handle_t)hExternalSemaphore));
918+
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
919+
ZE2UR_CALL(
920+
UrPlatform->ZeExternalSemaphoreExt.zexDeviceReleaseExternalSemaphoreExp,
921+
((ze_external_semaphore_ext_handle_t)hExternalSemaphore));
922+
} else {
923+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
924+
.zexExpDeviceReleaseExternalSemaphoreExp,
925+
((ze_intel_external_semaphore_exp_handle_t)hExternalSemaphore));
926+
}
855927

856928
return UR_RESULT_SUCCESS;
857929
}
@@ -898,15 +970,44 @@ ur_result_t urBindlessImagesWaitExternalSemaphoreExp(
898970
const auto &ZeCommandList = CommandList->first;
899971
const auto &WaitList = (*Event)->WaitList;
900972

901-
ze_intel_external_semaphore_wait_params_exp_t WaitParams = {
902-
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXP, nullptr, 0};
903-
WaitParams.value = hasValue ? waitValue : 0;
904-
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
905-
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
906-
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
907-
.zexCommandListAppendWaitExternalSemaphoresExp,
908-
(ZeCommandList, 1, &hExtSemaphore, &WaitParams, ZeEvent,
909-
WaitList.Length, WaitList.ZeEventList));
973+
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
974+
ze_external_semaphore_wait_params_ext_t WaitParams = {
975+
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXT, nullptr, 0};
976+
WaitParams.value = hasValue ? waitValue : 0;
977+
ze_external_semaphore_ext_handle_t hExtSemaphore =
978+
reinterpret_cast<ze_external_semaphore_ext_handle_t>(hSemaphore);
979+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
980+
.zexCommandListAppendWaitExternalSemaphoresExp,
981+
(ZeCommandList, 1, &hExtSemaphore, &WaitParams, ZeEvent,
982+
WaitList.Length, WaitList.ZeEventList));
983+
} else {
984+
ze_command_list_handle_t translatedCommandList;
985+
ZE2UR_CALL(zelLoaderTranslateHandle,
986+
(ZEL_HANDLE_COMMAND_LIST, ZeCommandList,
987+
(void **)&translatedCommandList));
988+
ze_event_handle_t translatedEvent = ZeEvent;
989+
if (ZeEvent) {
990+
ZE2UR_CALL(zelLoaderTranslateHandle,
991+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&translatedEvent));
992+
}
993+
std::vector<ze_event_handle_t> EventHandles(WaitList.Length + 1, nullptr);
994+
if (WaitList.Length > 0) {
995+
for (size_t i = 0; i < WaitList.Length; i++) {
996+
ze_event_handle_t ZeEvent = WaitList.ZeEventList[i];
997+
ZE2UR_CALL(zelLoaderTranslateHandle,
998+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&EventHandles[i + 1]));
999+
}
1000+
}
1001+
ze_intel_external_semaphore_wait_params_exp_t WaitParams = {
1002+
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXP, nullptr, 0};
1003+
WaitParams.value = hasValue ? waitValue : 0;
1004+
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
1005+
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
1006+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
1007+
.zexExpCommandListAppendWaitExternalSemaphoresExp,
1008+
(translatedCommandList, 1, &hExtSemaphore, &WaitParams,
1009+
translatedEvent, WaitList.Length, EventHandles.data()));
1010+
}
9101011

9111012
return UR_RESULT_SUCCESS;
9121013
}
@@ -915,13 +1016,6 @@ ur_result_t urBindlessImagesSignalExternalSemaphoreExp(
9151016
ur_queue_handle_t hQueue, ur_exp_external_semaphore_handle_t hSemaphore,
9161017
bool hasValue, uint64_t signalValue, uint32_t numEventsInWaitList,
9171018
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
918-
std::ignore = hQueue;
919-
std::ignore = hSemaphore;
920-
std::ignore = hasValue;
921-
std::ignore = signalValue;
922-
std::ignore = numEventsInWaitList;
923-
std::ignore = phEventWaitList;
924-
std::ignore = phEvent;
9251019
auto UrPlatform = hQueue->Context->getPlatform();
9261020
if (UrPlatform->ZeExternalSemaphoreExt.Supported == false) {
9271021
logger::error(logger::LegacyMessage("[UR][L0] "),
@@ -960,16 +1054,47 @@ ur_result_t urBindlessImagesSignalExternalSemaphoreExp(
9601054
const auto &ZeCommandList = CommandList->first;
9611055
const auto &WaitList = (*Event)->WaitList;
9621056

963-
ze_intel_external_semaphore_signal_params_exp_t SignalParams = {
964-
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXP, nullptr, 0};
965-
SignalParams.value = hasValue ? signalValue : 0;
966-
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
967-
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
968-
969-
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
970-
.zexCommandListAppendSignalExternalSemaphoresExp,
971-
(ZeCommandList, 1, &hExtSemaphore, &SignalParams, ZeEvent,
972-
WaitList.Length, WaitList.ZeEventList));
1057+
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
1058+
ze_external_semaphore_signal_params_ext_t SignalParams = {
1059+
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXT, nullptr, 0};
1060+
SignalParams.value = hasValue ? signalValue : 0;
1061+
ze_external_semaphore_ext_handle_t hExtSemaphore =
1062+
reinterpret_cast<ze_external_semaphore_ext_handle_t>(hSemaphore);
1063+
1064+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
1065+
.zexCommandListAppendSignalExternalSemaphoresExp,
1066+
(ZeCommandList, 1, &hExtSemaphore, &SignalParams, ZeEvent,
1067+
WaitList.Length, WaitList.ZeEventList));
1068+
} else {
1069+
ze_intel_external_semaphore_signal_params_exp_t SignalParams = {
1070+
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXP, nullptr,
1071+
0};
1072+
SignalParams.value = hasValue ? signalValue : 0;
1073+
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
1074+
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
1075+
1076+
ze_command_list_handle_t translatedCommandList;
1077+
ZE2UR_CALL(zelLoaderTranslateHandle,
1078+
(ZEL_HANDLE_COMMAND_LIST, ZeCommandList,
1079+
(void **)&translatedCommandList));
1080+
ze_event_handle_t translatedEvent = ZeEvent;
1081+
if (ZeEvent) {
1082+
ZE2UR_CALL(zelLoaderTranslateHandle,
1083+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&translatedEvent));
1084+
}
1085+
std::vector<ze_event_handle_t> EventHandles(WaitList.Length + 1, nullptr);
1086+
if (WaitList.Length > 0) {
1087+
for (size_t i = 0; i < WaitList.Length; i++) {
1088+
ze_event_handle_t ZeEvent = WaitList.ZeEventList[i];
1089+
ZE2UR_CALL(zelLoaderTranslateHandle,
1090+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&EventHandles[i + 1]));
1091+
}
1092+
}
1093+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
1094+
.zexExpCommandListAppendSignalExternalSemaphoresExp,
1095+
(translatedCommandList, 1, &hExtSemaphore, &SignalParams,
1096+
translatedEvent, WaitList.Length, EventHandles.data()));
1097+
}
9731098

9741099
return UR_RESULT_SUCCESS;
9751100
}

0 commit comments

Comments
 (0)