Skip to content

Commit e087156

Browse files
przemektmalonKenneth Benzie (Benie)
authored andcommitted
Fix linear interop memory and L0 V1 adapter leaks. (#18353)
This patch fixes the mapping of interop memory to a linear range represented by a `void*`. The SYCL Bindless Images spec is clarified with wording to indicate which free APIs should be used on mapped imported memory. Using `sycl::free` for freeing mapped imported linear memory regions does not work for the LevelZero adapter, as the backend needs to call `zeMemFree` directly - it cannot go through the regular `sycl::free` code as that will use UMF free functions by default. This is invalid for memory regions mapped using `map_external_linear_memory`. The `unmap_external_linear_memory` SYCL API has been added to the Bindless Images specification to remedy this problem. An `unmap_external_image_memory` has also been added. This patch also introduces an E2E test which verifies that a Vulkan buffer's memory can be correctly imported into SYCL and its data retrieved from and written to within a SYCL kernel by using purely USM pointers (i.e. without using bindless images). This is a step towards extending the test coverage of interop functionality currently in the Bindless Images extension, with the goal being at some point splitting the interop functionality into one or more separate extensions. Two other fixes have been made in the LevelZero UR adapter. A leaked `ze_image_handle_t` object has been fixed in the LevelZero V1 adapter. The previous implementation incorrectly reinterpreted bindless memory handles as `ur_mem_handle_t` types. The implementation of `urBindlessImagesImageFreeExp` is now unified between V1 and V2, removing the leak. As such the `level_zero/v2/image.cpp` file has been removed. The `urMemoryHandle` member of `ur_ze_external_memory_data` has been removed. Aside from being incorrectly reinterpreted from `ur_bindless_mem_handle_t`, the LevelZero adapter does not need to track the memory handle for external memory resources.
1 parent 3a58304 commit e087156

32 files changed

Lines changed: 472 additions & 58 deletions

include/ur_api.h

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/ur_api_funcs.def

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/ur_ddi.h

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/ur_print.h

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/ur_print.hpp

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/core/EXP-BINDLESS-IMAGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Enums
142142
* ${X}_FUNCTION_BINDLESS_IMAGES_IMPORT_EXTERNAL_MEMORY_EXP
143143
* ${X}_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_ARRAY_EXP
144144
* ${X}_FUNCTION_BINDLESS_IMAGES_RELEASE_EXTERNAL_MEMORY_EXP
145+
* ${X}_FUNCTION_BINDLESS_IMAGES_FREE_MAPPED_LINEAR_MEMORY_EXP
145146
* ${X}_FUNCTION_BINDLESS_IMAGES_IMPORT_EXTERNAL_SEMAPHORE_EXP
146147
* ${X}_FUNCTION_BINDLESS_IMAGES_RELEASE_EXTERNAL_SEMAPHORE_EXP
147148
* ${X}_FUNCTION_BINDLESS_IMAGES_WAIT_EXTERNAL_SEMAPHORE_EXP
@@ -198,6 +199,7 @@ Functions
198199
* ${x}BindlessImagesMapExternalArrayExp
199200
* ${x}BindlessImagesMapExternalLinearMemoryExp
200201
* ${x}BindlessImagesReleaseExternalMemoryExp
202+
* ${x}BindlessImagesFreeMappedLinearMemoryExp
201203
* ${x}BindlessImagesImportExternalSemaphoreExp
202204
* ${x}BindlessImagesReleaseExternalSemaphoreExp
203205
* ${x}BindlessImagesWaitExternalSemaphoreExp
@@ -286,6 +288,8 @@ Changelog
286288
| || - GetImageUnsampledHandleSupportExp |
287289
| || - GetImageSampledHandleSupportExp |
288290
+----------+-------------------------------------------------------------+
291+
| 23.0 | Added BindlessImagesFreeMappedLinearMemory function. |
292+
+----------+-------------------------------------------------------------+
289293

290294
Contributors
291295
--------------------------------------------------------------------------------

scripts/core/exp-bindless-images.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,29 @@ returns:
901901
- $X_RESULT_ERROR_INVALID_VALUE
902902
--- #--------------------------------------------------------------------------
903903
type: function
904+
desc: "Free a linear memory region mapped using MapExternalLinearMemoryExp"
905+
class: $xBindlessImages
906+
name: FreeMappedLinearMemoryExp
907+
ordinal: "0"
908+
analogue:
909+
- "**cuMemFree**"
910+
- "**zeMemFree**"
911+
params:
912+
- type: $x_context_handle_t
913+
name: hContext
914+
desc: "[in] handle of the context object"
915+
- type: $x_device_handle_t
916+
name: hDevice
917+
desc: "[in] handle of the device object"
918+
- type: void *
919+
name: pMem
920+
desc: "[in][release] pointer to mapped linear memory region to be freed"
921+
returns:
922+
- $X_RESULT_ERROR_INVALID_CONTEXT
923+
- $X_RESULT_ERROR_INVALID_NULL_POINTER:
924+
- "`pMem == NULL`"
925+
--- #--------------------------------------------------------------------------
926+
type: function
904927
desc: "Import an external semaphore"
905928
class: $xBindlessImages
906929
name: ImportExternalSemaphoreExp

scripts/core/registry.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,9 @@ etors:
658658
- name: BINDLESS_IMAGES_GET_IMAGE_MEMORY_HANDLE_TYPE_SUPPORT_EXP
659659
desc: Enumerator for $xBindlessImagesGetImageMemoryHandleTypeSupportExp
660660
value: '270'
661+
- name: BINDLESS_IMAGES_FREE_MAPPED_LINEAR_MEMORY_EXP
662+
desc: Enumerator for $xBindlessImagesFreeMappedLinearMemoryExp
663+
value: '271'
661664
---
662665
type: enum
663666
desc: Defines structure types

source/adapters/cuda/image.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,25 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesReleaseExternalMemoryExp(
16051605
return UR_RESULT_SUCCESS;
16061606
}
16071607

1608+
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesFreeMappedLinearMemoryExp(
1609+
ur_context_handle_t hContext, ur_device_handle_t hDevice, void *pMem) {
1610+
UR_ASSERT(std::find(hContext->getDevices().begin(),
1611+
hContext->getDevices().end(),
1612+
hDevice) != hContext->getDevices().end(),
1613+
UR_RESULT_ERROR_INVALID_CONTEXT);
1614+
UR_ASSERT(pMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
1615+
1616+
try {
1617+
ScopedContext Active(hDevice);
1618+
UR_CHECK_ERROR(cuMemFree(reinterpret_cast<CUdeviceptr>(pMem)));
1619+
} catch (ur_result_t Err) {
1620+
return Err;
1621+
} catch (...) {
1622+
return UR_RESULT_ERROR_UNKNOWN;
1623+
}
1624+
return UR_RESULT_SUCCESS;
1625+
}
1626+
16081627
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesImportExternalSemaphoreExp(
16091628
ur_context_handle_t hContext, ur_device_handle_t hDevice,
16101629
ur_exp_external_semaphore_type_t semHandleType,

source/adapters/cuda/ur_interface_loader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetBindlessImagesExpProcAddrTable(
344344
urBindlessImagesMapExternalLinearMemoryExp;
345345
pDdiTable->pfnReleaseExternalMemoryExp =
346346
urBindlessImagesReleaseExternalMemoryExp;
347+
pDdiTable->pfnFreeMappedLinearMemoryExp =
348+
urBindlessImagesFreeMappedLinearMemoryExp;
347349
pDdiTable->pfnImportExternalSemaphoreExp =
348350
urBindlessImagesImportExternalSemaphoreExp;
349351
pDdiTable->pfnReleaseExternalSemaphoreExp =

0 commit comments

Comments
 (0)