Skip to content

Commit 0523c95

Browse files
nrspruitKornevNikita
authored andcommitted
[UR][L0] Enable support for using L0 spec External Semaphores (intel#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 8eec32b commit 0523c95

File tree

3 files changed

+289
-93
lines changed

3 files changed

+289
-93
lines changed

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

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

846-
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt.zexImportExternalSemaphoreExp,
847-
(hDevice->ZeDevice, &SemDesc, &ExtSemaphoreHandle));
848-
*phExternalSemaphoreHandle =
849-
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;
904+
ze_device_handle_t translatedDevice;
905+
ZE2UR_CALL(zelLoaderTranslateHandle, (ZEL_HANDLE_DEVICE, hDevice->ZeDevice,
906+
(void **)&translatedDevice));
907+
// If the L0 loader is not aware of the extension, the handles need to be
908+
// translated
909+
ZE2UR_CALL(
910+
UrPlatform->ZeExternalSemaphoreExt.zexExpImportExternalSemaphoreExp,
911+
(translatedDevice, &SemDesc, &ExtSemaphoreHandle));
912+
913+
*phExternalSemaphoreHandle =
914+
(ur_exp_external_semaphore_handle_t)ExtSemaphoreHandle;
915+
}
850916

851917
return UR_RESULT_SUCCESS;
852918
}
@@ -861,9 +927,15 @@ ur_result_t urBindlessImagesReleaseExternalSemaphoreExp(
861927
" {} function not supported!", __FUNCTION__);
862928
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
863929
}
864-
ZE2UR_CALL(
865-
UrPlatform->ZeExternalSemaphoreExt.zexDeviceReleaseExternalSemaphoreExp,
866-
((ze_intel_external_semaphore_exp_handle_t)hExternalSemaphore));
930+
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
931+
ZE2UR_CALL(
932+
UrPlatform->ZeExternalSemaphoreExt.zexDeviceReleaseExternalSemaphoreExp,
933+
((ze_external_semaphore_ext_handle_t)hExternalSemaphore));
934+
} else {
935+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
936+
.zexExpDeviceReleaseExternalSemaphoreExp,
937+
((ze_intel_external_semaphore_exp_handle_t)hExternalSemaphore));
938+
}
867939

868940
return UR_RESULT_SUCCESS;
869941
}
@@ -910,15 +982,44 @@ ur_result_t urBindlessImagesWaitExternalSemaphoreExp(
910982
const auto &ZeCommandList = CommandList->first;
911983
const auto &WaitList = (*Event)->WaitList;
912984

913-
ze_intel_external_semaphore_wait_params_exp_t WaitParams = {
914-
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXP, nullptr, 0};
915-
WaitParams.value = hasValue ? waitValue : 0;
916-
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
917-
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
918-
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
919-
.zexCommandListAppendWaitExternalSemaphoresExp,
920-
(ZeCommandList, 1, &hExtSemaphore, &WaitParams, ZeEvent,
921-
WaitList.Length, WaitList.ZeEventList));
985+
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
986+
ze_external_semaphore_wait_params_ext_t WaitParams = {
987+
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXT, nullptr, 0};
988+
WaitParams.value = hasValue ? waitValue : 0;
989+
ze_external_semaphore_ext_handle_t hExtSemaphore =
990+
reinterpret_cast<ze_external_semaphore_ext_handle_t>(hSemaphore);
991+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
992+
.zexCommandListAppendWaitExternalSemaphoresExp,
993+
(ZeCommandList, 1, &hExtSemaphore, &WaitParams, ZeEvent,
994+
WaitList.Length, WaitList.ZeEventList));
995+
} else {
996+
ze_command_list_handle_t translatedCommandList;
997+
ZE2UR_CALL(zelLoaderTranslateHandle,
998+
(ZEL_HANDLE_COMMAND_LIST, ZeCommandList,
999+
(void **)&translatedCommandList));
1000+
ze_event_handle_t translatedEvent = ZeEvent;
1001+
if (ZeEvent) {
1002+
ZE2UR_CALL(zelLoaderTranslateHandle,
1003+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&translatedEvent));
1004+
}
1005+
std::vector<ze_event_handle_t> EventHandles(WaitList.Length + 1, nullptr);
1006+
if (WaitList.Length > 0) {
1007+
for (size_t i = 0; i < WaitList.Length; i++) {
1008+
ze_event_handle_t ZeEvent = WaitList.ZeEventList[i];
1009+
ZE2UR_CALL(zelLoaderTranslateHandle,
1010+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&EventHandles[i + 1]));
1011+
}
1012+
}
1013+
ze_intel_external_semaphore_wait_params_exp_t WaitParams = {
1014+
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WAIT_PARAMS_EXP, nullptr, 0};
1015+
WaitParams.value = hasValue ? waitValue : 0;
1016+
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
1017+
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
1018+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
1019+
.zexExpCommandListAppendWaitExternalSemaphoresExp,
1020+
(translatedCommandList, 1, &hExtSemaphore, &WaitParams,
1021+
translatedEvent, WaitList.Length, EventHandles.data()));
1022+
}
9221023

9231024
return UR_RESULT_SUCCESS;
9241025
}
@@ -927,13 +1028,6 @@ ur_result_t urBindlessImagesSignalExternalSemaphoreExp(
9271028
ur_queue_handle_t hQueue, ur_exp_external_semaphore_handle_t hSemaphore,
9281029
bool hasValue, uint64_t signalValue, uint32_t numEventsInWaitList,
9291030
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
930-
std::ignore = hQueue;
931-
std::ignore = hSemaphore;
932-
std::ignore = hasValue;
933-
std::ignore = signalValue;
934-
std::ignore = numEventsInWaitList;
935-
std::ignore = phEventWaitList;
936-
std::ignore = phEvent;
9371031
auto UrPlatform = hQueue->Context->getPlatform();
9381032
if (UrPlatform->ZeExternalSemaphoreExt.Supported == false) {
9391033
logger::error(logger::LegacyMessage("[UR][L0] "),
@@ -972,16 +1066,47 @@ ur_result_t urBindlessImagesSignalExternalSemaphoreExp(
9721066
const auto &ZeCommandList = CommandList->first;
9731067
const auto &WaitList = (*Event)->WaitList;
9741068

975-
ze_intel_external_semaphore_signal_params_exp_t SignalParams = {
976-
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXP, nullptr, 0};
977-
SignalParams.value = hasValue ? signalValue : 0;
978-
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
979-
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
980-
981-
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
982-
.zexCommandListAppendSignalExternalSemaphoresExp,
983-
(ZeCommandList, 1, &hExtSemaphore, &SignalParams, ZeEvent,
984-
WaitList.Length, WaitList.ZeEventList));
1069+
if (UrPlatform->ZeExternalSemaphoreExt.LoaderExtension) {
1070+
ze_external_semaphore_signal_params_ext_t SignalParams = {
1071+
ZE_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXT, nullptr, 0};
1072+
SignalParams.value = hasValue ? signalValue : 0;
1073+
ze_external_semaphore_ext_handle_t hExtSemaphore =
1074+
reinterpret_cast<ze_external_semaphore_ext_handle_t>(hSemaphore);
1075+
1076+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
1077+
.zexCommandListAppendSignalExternalSemaphoresExp,
1078+
(ZeCommandList, 1, &hExtSemaphore, &SignalParams, ZeEvent,
1079+
WaitList.Length, WaitList.ZeEventList));
1080+
} else {
1081+
ze_intel_external_semaphore_signal_params_exp_t SignalParams = {
1082+
ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS_EXP, nullptr,
1083+
0};
1084+
SignalParams.value = hasValue ? signalValue : 0;
1085+
const ze_intel_external_semaphore_exp_handle_t hExtSemaphore =
1086+
reinterpret_cast<ze_intel_external_semaphore_exp_handle_t>(hSemaphore);
1087+
1088+
ze_command_list_handle_t translatedCommandList;
1089+
ZE2UR_CALL(zelLoaderTranslateHandle,
1090+
(ZEL_HANDLE_COMMAND_LIST, ZeCommandList,
1091+
(void **)&translatedCommandList));
1092+
ze_event_handle_t translatedEvent = ZeEvent;
1093+
if (ZeEvent) {
1094+
ZE2UR_CALL(zelLoaderTranslateHandle,
1095+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&translatedEvent));
1096+
}
1097+
std::vector<ze_event_handle_t> EventHandles(WaitList.Length + 1, nullptr);
1098+
if (WaitList.Length > 0) {
1099+
for (size_t i = 0; i < WaitList.Length; i++) {
1100+
ze_event_handle_t ZeEvent = WaitList.ZeEventList[i];
1101+
ZE2UR_CALL(zelLoaderTranslateHandle,
1102+
(ZEL_HANDLE_EVENT, ZeEvent, (void **)&EventHandles[i + 1]));
1103+
}
1104+
}
1105+
ZE2UR_CALL(UrPlatform->ZeExternalSemaphoreExt
1106+
.zexExpCommandListAppendSignalExternalSemaphoresExp,
1107+
(translatedCommandList, 1, &hExtSemaphore, &SignalParams,
1108+
translatedEvent, WaitList.Length, EventHandles.data()));
1109+
}
9851110

9861111
return UR_RESULT_SUCCESS;
9871112
}

0 commit comments

Comments
 (0)