1616#else
1717#include " context.hpp"
1818#endif
19+ #include " helpers/memory_helpers.hpp"
1920#include " image_common.hpp"
2021#include " logger/ur_logger.hpp"
2122#include " sampler.hpp"
@@ -267,9 +268,14 @@ ur_result_t ze2urImageFormat(const ze_image_format_t &ZeImageFormat,
267268}
268269
269270// / Construct UR bindless image struct from ZE image handle and desc.
270- ur_result_t createUrImgFromZeImage (ze_image_handle_t ZeImage,
271+ ur_result_t createUrImgFromZeImage (ze_context_handle_t hContext,
272+ ze_device_handle_t hDevice,
271273 const ZeStruct<ze_image_desc_t > &ZeImageDesc,
272274 ur_exp_image_mem_native_handle_t *pImg) {
275+ ze_image_handle_t ZeImage;
276+ ZE2UR_CALL (zeImageCreate, (hContext, hDevice, &ZeImageDesc, &ZeImage));
277+ ZE2UR_CALL (zeContextMakeImageResident, (hContext, hDevice, ZeImage));
278+
273279 try {
274280 ur_bindless_mem_handle_t *urImg =
275281 new ur_bindless_mem_handle_t (ZeImage, ZeImageDesc);
@@ -951,12 +957,8 @@ ur_result_t urBindlessImagesImageAllocateExp(
951957 ZeImageBindlessDesc.flags = ZE_IMAGE_BINDLESS_EXP_FLAG_BINDLESS;
952958 ZeImageDesc.pNext = &ZeImageBindlessDesc;
953959
954- ze_image_handle_t ZeImage;
955- ZE2UR_CALL (zeImageCreate, (hContext->getZeHandle (), hDevice->ZeDevice ,
956- &ZeImageDesc, &ZeImage));
957- ZE2UR_CALL (zeContextMakeImageResident,
958- (hContext->getZeHandle (), hDevice->ZeDevice , ZeImage));
959- UR_CALL (createUrImgFromZeImage (ZeImage, ZeImageDesc, phImageMem));
960+ UR_CALL (createUrImgFromZeImage (hContext->getZeHandle (), hDevice->ZeDevice ,
961+ ZeImageDesc, phImageMem));
960962 return UR_RESULT_SUCCESS;
961963}
962964
@@ -990,9 +992,9 @@ ur_result_t urBindlessImagesUnsampledImageHandleDestroyExp(
990992 auto item = hDevice->ZeOffsetToImageHandleMap .find (hImage);
991993
992994 if (item != hDevice->ZeOffsetToImageHandleMap .end ()) {
993- ZE2UR_CALL (zeImageDestroy, (item->second ));
994995 hDevice->ZeOffsetToImageHandleMap .erase (item);
995996 Lock.release ();
997+ ZE2UR_CALL (zeImageDestroy, (item->second ));
996998 } else {
997999 Lock.release ();
9981000 return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
@@ -1165,12 +1167,9 @@ ur_result_t urBindlessImagesMapExternalArrayExp(
11651167 ZeImageBindlessDesc.flags = ZE_IMAGE_BINDLESS_EXP_FLAG_BINDLESS;
11661168 ZeImageDesc.pNext = &ZeImageBindlessDesc;
11671169
1168- ze_image_handle_t ZeImage;
1169- ZE2UR_CALL (zeImageCreate, (hContext->getZeHandle (), hDevice->ZeDevice ,
1170- &ZeImageDesc, &ZeImage));
1171- ZE2UR_CALL (zeContextMakeImageResident,
1172- (hContext->getZeHandle (), hDevice->ZeDevice , ZeImage));
1173- UR_CALL (createUrImgFromZeImage (ZeImage, ZeImageDesc, phImageMem));
1170+ UR_CALL (createUrImgFromZeImage (hContext->getZeHandle (), hDevice->ZeDevice ,
1171+ ZeImageDesc, phImageMem));
1172+
11741173 externalMemoryData->urMemoryHandle =
11751174 reinterpret_cast <ur_mem_handle_t >(*phImageMem);
11761175 return UR_RESULT_SUCCESS;
@@ -1186,7 +1185,29 @@ ur_result_t urBindlessImagesReleaseExternalMemoryExp(
11861185 struct ur_ze_external_memory_data *externalMemoryData =
11871186 reinterpret_cast <ur_ze_external_memory_data *>(hExternalMem);
11881187
1189- UR_CALL (ur::level_zero::urMemRelease (externalMemoryData->urMemoryHandle ));
1188+ // externalMemoryData->urMemoryHandle can be ur_bindless_mem_handle_t or
1189+ // buffer allocated by zeMemAllocDevice()
1190+
1191+ ze_memory_allocation_properties_t allocProperties{
1192+ ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES, nullptr ,
1193+ ZE_MEMORY_TYPE_UNKNOWN, 0 , 0 };
1194+ ZE2UR_CALL (zeMemGetAllocProperties,
1195+ (hContext->getZeHandle (), externalMemoryData->urMemoryHandle ,
1196+ &allocProperties, nullptr ));
1197+
1198+ switch (allocProperties.type ) {
1199+ case ZE_MEMORY_TYPE_DEVICE:
1200+ zeMemFree (hContext->getZeHandle (),
1201+ externalMemoryData->urMemoryHandle ); // Free device memory
1202+ break ;
1203+ case ZE_MEMORY_TYPE_UNKNOWN:
1204+ default :
1205+ auto *bindlessMem = reinterpret_cast <ur_bindless_mem_handle_t *>(
1206+ externalMemoryData->urMemoryHandle );
1207+ zeImageDestroy (bindlessMem->getZeImage ());
1208+ delete bindlessMem;
1209+ break ;
1210+ }
11901211
11911212 switch (externalMemoryData->type ) {
11921213 case UR_ZE_EXTERNAL_OPAQUE_FD:
0 commit comments