Skip to content

Commit dae689e

Browse files
authored
[UR][L0 v1/v2 adapter]Handle errors of img functions (#18261)
Handle errors in image functions to avoid memory leaks
1 parent 2710b53 commit dae689e

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

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

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,23 @@ ur_result_t createUrImgFromZeImage(ze_context_handle_t hContext,
272272
ze_device_handle_t hDevice,
273273
const ZeStruct<ze_image_desc_t> &ZeImageDesc,
274274
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));
275+
v2::raii::ze_image_handle_t ZeImage;
276+
try {
277+
ZE2UR_CALL_THROWS(zeImageCreate,
278+
(hContext, hDevice, &ZeImageDesc, ZeImage.ptr()));
279+
ZE2UR_CALL_THROWS(zeContextMakeImageResident,
280+
(hContext, hDevice, ZeImage.get()));
281+
} catch (...) {
282+
return exceptionToResult(std::current_exception());
283+
}
278284

279285
try {
280286
ur_bindless_mem_handle_t *urImg =
281-
new ur_bindless_mem_handle_t(ZeImage, ZeImageDesc);
287+
new ur_bindless_mem_handle_t(ZeImage.get(), ZeImageDesc);
288+
ZeImage.release();
282289
*pImg = reinterpret_cast<ur_exp_image_mem_native_handle_t>(urImg);
283-
} catch (const std::bad_alloc &) {
284-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
285290
} catch (...) {
286-
return UR_RESULT_ERROR_UNKNOWN;
291+
return exceptionToResult(std::current_exception());
287292
}
288293
return UR_RESULT_SUCCESS;
289294
}
@@ -314,7 +319,7 @@ ur_result_t bindlessImagesCreateImpl(ur_context_handle_t hContext,
314319
BindlessDesc.flags |= ZE_IMAGE_BINDLESS_EXP_FLAG_SAMPLED_IMAGE;
315320
}
316321

317-
ze_image_handle_t ZeImage;
322+
v2::raii::ze_image_handle_t ZeImage;
318323

319324
ze_memory_allocation_properties_t MemAllocProperties{
320325
ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES, nullptr,
@@ -330,9 +335,15 @@ ur_result_t bindlessImagesCreateImpl(ur_context_handle_t hContext,
330335
reinterpret_cast<ur_bindless_mem_handle_t *>(hImageMem);
331336
ze_image_handle_t zeImg1 = urImg->getZeImage();
332337

333-
ZE2UR_CALL(zeImageViewCreateExt,
334-
(zeCtx, hDevice->ZeDevice, &ZeImageDesc, zeImg1, &ZeImage));
335-
ZE2UR_CALL(zeContextMakeImageResident, (zeCtx, hDevice->ZeDevice, ZeImage));
338+
try {
339+
ZE2UR_CALL_THROWS(
340+
zeImageViewCreateExt,
341+
(zeCtx, hDevice->ZeDevice, &ZeImageDesc, zeImg1, ZeImage.ptr()));
342+
ZE2UR_CALL_THROWS(zeContextMakeImageResident,
343+
(zeCtx, hDevice->ZeDevice, ZeImage.get()));
344+
} catch (...) {
345+
return exceptionToResult(std::current_exception());
346+
}
336347
} else if (MemAllocProperties.type == ZE_MEMORY_TYPE_DEVICE ||
337348
MemAllocProperties.type == ZE_MEMORY_TYPE_HOST ||
338349
MemAllocProperties.type == ZE_MEMORY_TYPE_SHARED) {
@@ -343,10 +354,14 @@ ur_result_t bindlessImagesCreateImpl(ur_context_handle_t hContext,
343354
} else {
344355
BindlessDesc.pNext = &PitchedDesc;
345356
}
346-
347-
ZE2UR_CALL(zeImageCreate,
348-
(zeCtx, hDevice->ZeDevice, &ZeImageDesc, &ZeImage));
349-
ZE2UR_CALL(zeContextMakeImageResident, (zeCtx, hDevice->ZeDevice, ZeImage));
357+
try {
358+
ZE2UR_CALL_THROWS(zeImageCreate, (zeCtx, hDevice->ZeDevice, &ZeImageDesc,
359+
ZeImage.ptr()));
360+
ZE2UR_CALL_THROWS(zeContextMakeImageResident,
361+
(zeCtx, hDevice->ZeDevice, ZeImage.get()));
362+
} catch (...) {
363+
return exceptionToResult(std::current_exception());
364+
}
350365
} else {
351366
return UR_RESULT_ERROR_INVALID_VALUE;
352367
}
@@ -367,14 +382,16 @@ ur_result_t bindlessImagesCreateImpl(ur_context_handle_t hContext,
367382
uint64_t DeviceOffset{};
368383
ze_image_handle_t ZeImageTranslated;
369384
ZE2UR_CALL(zelLoaderTranslateHandle,
370-
(ZEL_HANDLE_IMAGE, ZeImage, (void **)&ZeImageTranslated));
385+
(ZEL_HANDLE_IMAGE, ZeImage.get(), (void **)&ZeImageTranslated));
371386
ZE2UR_CALL(zeImageGetDeviceOffsetExpFunctionPtr,
372387
(ZeImageTranslated, &DeviceOffset));
373388
*phImage = DeviceOffset;
374389

375390
std::shared_lock<ur_shared_mutex> Lock(hDevice->Mutex);
376-
hDevice->ZeOffsetToImageHandleMap[*phImage] = ZeImage;
391+
hDevice->ZeOffsetToImageHandleMap[*phImage] = ZeImage.get();
377392
Lock.release();
393+
ZeImage.release();
394+
378395
return UR_RESULT_SUCCESS;
379396
}
380397

0 commit comments

Comments
 (0)