Skip to content

Commit f040b1f

Browse files
authored
Merge pull request #1778 from nrspruit/fix_multi_driver_exp_handle
[L0] Fix Handle used in calls to L0 Driver zex apis given multi drivers
2 parents 08acafb + b3f36b9 commit f040b1f

File tree

9 files changed

+45
-13
lines changed

9 files changed

+45
-13
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
4949
for (uint32_t I = 0; I < ZeDriverCount; ++I) {
5050
auto platform = std::make_unique<ur_platform_handle_t_>(ZeDrivers[I]);
5151
UR_CALL(platform->initialize());
52+
ZE2UR_CALL(zelLoaderTranslateHandle,
53+
(ZEL_HANDLE_DRIVER, platform->ZeDriver,
54+
(void **)&platform->ZeDriverHandleExpTranslated));
5255

5356
// Save a copy in the cache for future uses.
5457
platforms.push_back(std::move(platform));

source/adapters/level_zero/adapter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "logger/ur_logger.hpp"
1313
#include <atomic>
14+
#include <loader/ze_loader.h>
1415
#include <mutex>
1516
#include <optional>
1617
#include <ur/ur.hpp>

source/adapters/level_zero/command_buffer.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ bool PreferCopyEngineForFill = [] {
5656
ur_exp_command_buffer_handle_t_::ur_exp_command_buffer_handle_t_(
5757
ur_context_handle_t Context, ur_device_handle_t Device,
5858
ze_command_list_handle_t CommandList,
59+
ze_command_list_handle_t CommandListTranslated,
5960
ze_command_list_handle_t CommandListResetEvents,
6061
ze_command_list_handle_t CopyCommandList,
6162
ZeStruct<ze_command_list_desc_t> ZeDesc,
6263
ZeStruct<ze_command_list_desc_t> ZeCopyDesc,
6364
const ur_exp_command_buffer_desc_t *Desc, const bool IsInOrderCmdList)
6465
: Context(Context), Device(Device), ZeComputeCommandList(CommandList),
66+
ZeComputeCommandListTranslated(CommandListTranslated),
6567
ZeCommandListResetEvents(CommandListResetEvents),
6668
ZeCommandListDesc(ZeDesc), ZeCopyCommandList(CopyCommandList),
6769
ZeCopyCommandListDesc(ZeCopyDesc), ZeFencesMap(), ZeActiveFence(nullptr),
@@ -605,11 +607,16 @@ urCommandBufferCreateExp(ur_context_handle_t Context, ur_device_handle_t Device,
605607
&ZeCopyCommandList));
606608
}
607609

610+
ze_command_list_handle_t ZeComputeCommandListTranslated = nullptr;
611+
ZE2UR_CALL(zelLoaderTranslateHandle,
612+
(ZEL_HANDLE_COMMAND_LIST, ZeComputeCommandList,
613+
(void **)&ZeComputeCommandListTranslated));
614+
608615
try {
609616
*CommandBuffer = new ur_exp_command_buffer_handle_t_(
610-
Context, Device, ZeComputeCommandList, ZeCommandListResetEvents,
611-
ZeCopyCommandList, ZeCommandListDesc, ZeCopyCommandListDesc,
612-
CommandBufferDesc, IsInOrder);
617+
Context, Device, ZeComputeCommandList, ZeComputeCommandListTranslated,
618+
ZeCommandListResetEvents, ZeCopyCommandList, ZeCommandListDesc,
619+
ZeCopyCommandListDesc, CommandBufferDesc, IsInOrder);
613620
} catch (const std::bad_alloc &) {
614621
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
615622
} catch (...) {
@@ -791,8 +798,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
791798
UR_ASSERT(Plt->ZeMutableCmdListExt.Supported,
792799
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
793800
ZE2UR_CALL(Plt->ZeMutableCmdListExt.zexCommandListGetNextCommandIdExp,
794-
(CommandBuffer->ZeComputeCommandList, &ZeMutableCommandDesc,
795-
&CommandId));
801+
(CommandBuffer->ZeComputeCommandListTranslated,
802+
&ZeMutableCommandDesc, &CommandId));
796803
DEBUG_LOG(CommandId);
797804
}
798805
try {
@@ -1619,8 +1626,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
16191626
auto Plt = CommandBuffer->Context->getPlatform();
16201627
UR_ASSERT(Plt->ZeMutableCmdListExt.Supported,
16211628
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
1622-
ZE2UR_CALL(Plt->ZeMutableCmdListExt.zexCommandListUpdateMutableCommandsExp,
1623-
(CommandBuffer->ZeComputeCommandList, &MutableCommandDesc));
1629+
ZE2UR_CALL(
1630+
Plt->ZeMutableCmdListExt.zexCommandListUpdateMutableCommandsExp,
1631+
(CommandBuffer->ZeComputeCommandListTranslated, &MutableCommandDesc));
16241632
ZE2UR_CALL(zeCommandListClose, (CommandBuffer->ZeComputeCommandList));
16251633

16261634
return UR_RESULT_SUCCESS;

source/adapters/level_zero/command_buffer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
2828
ur_exp_command_buffer_handle_t_(
2929
ur_context_handle_t Context, ur_device_handle_t Device,
3030
ze_command_list_handle_t CommandList,
31+
ze_command_list_handle_t CommandListTranslated,
3132
ze_command_list_handle_t CommandListResetEvents,
3233
ze_command_list_handle_t CopyCommandList,
3334
ZeStruct<ze_command_list_desc_t> ZeDesc,
@@ -55,6 +56,9 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
5556
ur_device_handle_t Device;
5657
// Level Zero command list handle
5758
ze_command_list_handle_t ZeComputeCommandList;
59+
// Given a multi driver scenario, the driver handle must be translated to the
60+
// internal driver handle to allow calls to driver experimental apis.
61+
ze_command_list_handle_t ZeComputeCommandListTranslated;
5862
// Level Zero command list handle
5963
ze_command_list_handle_t ZeCommandListResetEvents;
6064
// Level Zero command list descriptor

source/adapters/level_zero/image.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,11 @@ ur_result_t bindlessImagesCreateImpl(ur_context_handle_t hContext,
488488
return UR_RESULT_ERROR_INVALID_OPERATION;
489489

490490
uint64_t DeviceOffset{};
491-
ZE2UR_CALL(zeImageGetDeviceOffsetExpFunctionPtr, (ZeImage, &DeviceOffset));
491+
ze_image_handle_t ZeImageTranslated;
492+
ZE2UR_CALL(zelLoaderTranslateHandle,
493+
(ZEL_HANDLE_IMAGE, ZeImage, (void **)&ZeImageTranslated));
494+
ZE2UR_CALL(zeImageGetDeviceOffsetExpFunctionPtr,
495+
(ZeImageTranslated, &DeviceOffset));
492496
*phImage = reinterpret_cast<ur_exp_image_handle_t>(DeviceOffset);
493497

494498
return UR_RESULT_SUCCESS;
@@ -652,8 +656,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMPitchedAllocExp(
652656

653657
size_t Width = widthInBytes / elementSizeBytes;
654658
size_t RowPitch;
659+
ze_device_handle_t ZeDeviceTranslated;
660+
ZE2UR_CALL(zelLoaderTranslateHandle, (ZEL_HANDLE_DEVICE, hDevice->ZeDevice,
661+
(void **)&ZeDeviceTranslated));
655662
ZE2UR_CALL(zeMemGetPitchFor2dImageFunctionPtr,
656-
(hContext->ZeContext, hDevice->ZeDevice, Width, height,
663+
(hContext->ZeContext, ZeDeviceTranslated, Width, height,
657664
elementSizeBytes, &RowPitch));
658665
*pResultPitch = RowPitch;
659666

source/adapters/level_zero/memory.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
16681668
// If not shared of any type, we can import the ptr
16691669
if (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_UNKNOWN) {
16701670
// Promote the host ptr to USM host memory
1671-
ze_driver_handle_t driverHandle = Context->getPlatform()->ZeDriver;
1671+
ze_driver_handle_t driverHandle =
1672+
Context->getPlatform()->ZeDriverHandleExpTranslated;
16721673
ZeUSMImport.doZeUSMImport(driverHandle, Host, Size);
16731674
HostPtrImported = true;
16741675
}
@@ -2252,7 +2253,8 @@ ur_result_t _ur_buffer::free() {
22522253
UR_CALL(ZeMemFreeHelper(UrContext, ZeHandle));
22532254
break;
22542255
case allocation_t::unimport:
2255-
ZeUSMImport.doZeUSMRelease(UrContext->getPlatform()->ZeDriver, ZeHandle);
2256+
ZeUSMImport.doZeUSMRelease(
2257+
UrContext->getPlatform()->ZeDriverHandleExpTranslated, ZeHandle);
22562258
break;
22572259
default:
22582260
die("_ur_buffer::free(): Unhandled release action");

source/adapters/level_zero/platform.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ struct ur_platform_handle_t_ : public _ur_platform {
2424
// a pretty good fit to keep here.
2525
ze_driver_handle_t ZeDriver;
2626

27+
// Given a multi driver scenario, the driver handle must be translated to the
28+
// internal driver handle to allow calls to driver experimental apis.
29+
ze_driver_handle_t ZeDriverHandleExpTranslated;
30+
2731
// Cache versions info from zeDriverGetProperties.
2832
std::string ZeDriverVersion;
2933
std::string ZeDriverApiVersion;

source/adapters/level_zero/ur_level_zero.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <unordered_map>
1919
#include <vector>
2020

21+
#include <loader/ze_loader.h>
2122
#include <ur/ur.hpp>
2223
#include <ur_api.h>
2324
#include <ze_api.h>

source/adapters/level_zero/usm.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMImportExp(ur_context_handle_t Context,
10261026
// If not shared of any type, we can import the ptr
10271027
if (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_UNKNOWN) {
10281028
// Promote the host ptr to USM host memory
1029-
ze_driver_handle_t driverHandle = Context->getPlatform()->ZeDriver;
1029+
ze_driver_handle_t driverHandle =
1030+
Context->getPlatform()->ZeDriverHandleExpTranslated;
10301031
ZeUSMImport.doZeUSMImport(driverHandle, HostPtr, Size);
10311032
}
10321033
}
@@ -1039,6 +1040,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMReleaseExp(ur_context_handle_t Context,
10391040

10401041
// Release the imported memory.
10411042
if (ZeUSMImport.Supported && HostPtr != nullptr)
1042-
ZeUSMImport.doZeUSMRelease(Context->getPlatform()->ZeDriver, HostPtr);
1043+
ZeUSMImport.doZeUSMRelease(
1044+
Context->getPlatform()->ZeDriverHandleExpTranslated, HostPtr);
10431045
return UR_RESULT_SUCCESS;
10441046
}

0 commit comments

Comments
 (0)