From f56cf104447c6f56a642318614125918b5a5fa55 Mon Sep 17 00:00:00 2001 From: omarahmed1111 Date: Fri, 11 Oct 2024 16:42:15 +0100 Subject: [PATCH] Add isNativeHandleOwned flag for handles --- source/adapters/opencl/context.cpp | 11 ++---- source/adapters/opencl/context.hpp | 35 ++++++++++++++----- source/adapters/opencl/device.cpp | 8 ++--- source/adapters/opencl/device.hpp | 7 +++- source/adapters/opencl/event.cpp | 9 ++--- source/adapters/opencl/event.hpp | 5 ++- source/adapters/opencl/kernel.cpp | 8 ++--- source/adapters/opencl/kernel.hpp | 28 ++++++++++----- source/adapters/opencl/memory.cpp | 13 +++---- source/adapters/opencl/memory.hpp | 5 ++- source/adapters/opencl/program.cpp | 8 ++--- source/adapters/opencl/program.hpp | 5 ++- source/adapters/opencl/queue.cpp | 8 ++--- source/adapters/opencl/queue.hpp | 24 ++++++++++--- source/adapters/opencl/sampler.cpp | 9 ++--- source/adapters/opencl/sampler.hpp | 5 ++- .../context/context_adapter_opencl.match | 1 - 17 files changed, 108 insertions(+), 81 deletions(-) delete mode 100644 test/conformance/context/context_adapter_opencl.match diff --git a/source/adapters/opencl/context.cpp b/source/adapters/opencl/context.cpp index ae77169817..27bb3cf7ba 100644 --- a/source/adapters/opencl/context.cpp +++ b/source/adapters/opencl/context.cpp @@ -94,15 +94,13 @@ urContextRelease(ur_context_handle_t hContext) { if (hContext->decrementReferenceCount() == 0) { delete hContext; - } else { - CL_RETURN_ON_FAILURE(clReleaseContext(hContext->get())); } + return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urContextRetain(ur_context_handle_t hContext) { - CL_RETURN_ON_FAILURE(clRetainContext(hContext->get())); hContext->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -123,11 +121,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle( cl_context NativeHandle = reinterpret_cast(hNativeContext); UR_RETURN_ON_FAILURE(ur_context_handle_t_::makeWithNative( NativeHandle, numDevices, phDevices, *phContext)); - - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainContext(NativeHandle)); - } - + (*phContext)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/context.hpp b/source/adapters/opencl/context.hpp index 66ab0e2518..4b60ca5afe 100644 --- a/source/adapters/opencl/context.hpp +++ b/source/adapters/opencl/context.hpp @@ -20,6 +20,7 @@ struct ur_context_handle_t_ { std::vector Devices; uint32_t DeviceCount; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_context_handle_t_(native_type Ctx, uint32_t DevCount, const ur_device_handle_t *phDevices) @@ -49,18 +50,32 @@ struct ur_context_handle_t_ { CL_RETURN_ON_FAILURE(clGetContextInfo(Ctx, CL_CONTEXT_DEVICES, sizeof(CLDevices), CLDevices.data(), nullptr)); - if (DevCount != CLDeviceCount) { - return UR_RESULT_ERROR_INVALID_CONTEXT; - } - for (uint32_t i = 0; i < DevCount; i++) { - if (phDevices[i]->get() != CLDevices[i]) { + std::vector URDevices; + if (DevCount) { + if (DevCount != CLDeviceCount) { return UR_RESULT_ERROR_INVALID_CONTEXT; } + for (uint32_t i = 0; i < DevCount; i++) { + if (phDevices[i]->get() != CLDevices[i]) { + return UR_RESULT_ERROR_INVALID_CONTEXT; + } + URDevices.push_back(phDevices[i]); + } + } else { + DevCount = CLDeviceCount; + for (uint32_t i = 0; i < CLDeviceCount; i++) { + ur_device_handle_t UrDevice = nullptr; + ur_native_handle_t hNativeHandle = + reinterpret_cast(CLDevices[i]); + UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle( + hNativeHandle, nullptr, nullptr, &UrDevice)); + URDevices.push_back(UrDevice); + } } - auto URContext = - std::make_unique(Ctx, DevCount, phDevices); + + auto URContext = std::make_unique(Ctx, DevCount, + URDevices.data()); Context = URContext.release(); - CL_RETURN_ON_FAILURE(clRetainContext(Ctx)); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; } catch (...) { @@ -74,7 +89,9 @@ struct ur_context_handle_t_ { for (uint32_t i = 0; i < DeviceCount; i++) { urDeviceRelease(Devices[i]); } - clReleaseContext(Context); + if (IsNativeHandleOwned) { + clReleaseContext(Context); + } } native_type get() { return Context; } diff --git a/source/adapters/opencl/device.cpp b/source/adapters/opencl/device.cpp index 263ac4b293..893c17572b 100644 --- a/source/adapters/opencl/device.cpp +++ b/source/adapters/opencl/device.cpp @@ -1105,7 +1105,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( // Root devices ref count are unchanged through out the program lifetime. UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - CL_RETURN_ON_FAILURE(clRetainDevice(hDevice->get())); hDevice->incrementReferenceCount(); } @@ -1118,8 +1117,6 @@ urDeviceRelease(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { if (hDevice->decrementReferenceCount() == 0) { delete hDevice; - } else { - CL_RETURN_ON_FAILURE(clReleaseDevice(hDevice->get())); } } return UR_RESULT_SUCCESS; @@ -1134,7 +1131,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetNativeHandle( UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle( ur_native_handle_t hNativeDevice, ur_adapter_handle_t, - const ur_device_native_properties_t *, ur_device_handle_t *phDevice) { + const ur_device_native_properties_t *pProperties, + ur_device_handle_t *phDevice) { cl_device_id NativeHandle = reinterpret_cast(hNativeDevice); uint32_t NumPlatforms = 0; @@ -1154,6 +1152,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle( for (auto &Device : Devices) { if (Device->get() == NativeHandle) { *phDevice = Device; + (*phDevice)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } } diff --git a/source/adapters/opencl/device.hpp b/source/adapters/opencl/device.hpp index 7420f58665..8527f9b518 100644 --- a/source/adapters/opencl/device.hpp +++ b/source/adapters/opencl/device.hpp @@ -19,6 +19,7 @@ struct ur_device_handle_t_ { cl_device_type Type = 0; ur_device_handle_t ParentDevice = nullptr; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat, ur_device_handle_t Parent) @@ -32,7 +33,11 @@ struct ur_device_handle_t_ { } } - ~ur_device_handle_t_() {} + ~ur_device_handle_t_() { + if (ParentDevice && IsNativeHandleOwned) { + clReleaseDevice(Device); + } + } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/event.cpp b/source/adapters/opencl/event.cpp index a03ecbb7eb..b432959a75 100644 --- a/source/adapters/opencl/event.cpp +++ b/source/adapters/opencl/event.cpp @@ -119,16 +119,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle( try { auto UREvent = std::make_unique(NativeHandle, hContext, nullptr); + UREvent->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; *phEvent = UREvent.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; } catch (...) { return UR_RESULT_ERROR_UNKNOWN; } - - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainEvent(NativeHandle)); - } return UR_RESULT_SUCCESS; } @@ -140,14 +138,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle( UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) { if (hEvent->decrementReferenceCount() == 0) { delete hEvent; - } else { - CL_RETURN_ON_FAILURE(clReleaseEvent(hEvent->get())); } return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) { - CL_RETURN_ON_FAILURE(clRetainEvent(hEvent->get())); hEvent->incrementReferenceCount(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/event.hpp b/source/adapters/opencl/event.hpp index f7f17e7e1e..18d7c83f6d 100644 --- a/source/adapters/opencl/event.hpp +++ b/source/adapters/opencl/event.hpp @@ -19,6 +19,7 @@ struct ur_event_handle_t_ { ur_context_handle_t Context; ur_queue_handle_t Queue; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx, ur_queue_handle_t Queue) @@ -35,7 +36,9 @@ struct ur_event_handle_t_ { if (Queue) { urQueueRelease(Queue); } - clReleaseEvent(Event); + if (IsNativeHandleOwned) { + clReleaseEvent(Event); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/kernel.cpp b/source/adapters/opencl/kernel.cpp index 28be59b26e..92ccc0a995 100644 --- a/source/adapters/opencl/kernel.cpp +++ b/source/adapters/opencl/kernel.cpp @@ -262,7 +262,6 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice, } UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) { - CL_RETURN_ON_FAILURE(clRetainKernel(hKernel->get())); hKernel->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -271,8 +270,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelRelease(ur_kernel_handle_t hKernel) { if (hKernel->decrementReferenceCount() == 0) { delete hKernel; - } else { - CL_RETURN_ON_FAILURE(clReleaseKernel(hKernel->get())); } return UR_RESULT_SUCCESS; } @@ -403,9 +400,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelCreateWithNativeHandle( UR_RETURN_ON_FAILURE(ur_kernel_handle_t_::makeWithNative( NativeHandle, hProgram, hContext, *phKernel)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainKernel(NativeHandle)); - } + (*phKernel)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/kernel.hpp b/source/adapters/opencl/kernel.hpp index 44651ebfc7..b103af03c2 100644 --- a/source/adapters/opencl/kernel.hpp +++ b/source/adapters/opencl/kernel.hpp @@ -21,19 +21,26 @@ struct ur_kernel_handle_t_ { ur_program_handle_t Program; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_kernel_handle_t_(native_type Kernel, ur_program_handle_t Program, ur_context_handle_t Context) : Kernel(Kernel), Program(Program), Context(Context) { RefCount = 1; - urProgramRetain(Program); + if (Program) { + urProgramRetain(Program); + } urContextRetain(Context); } ~ur_kernel_handle_t_() { - clReleaseKernel(Kernel); - urProgramRelease(Program); + if (Program) { + urProgramRelease(Program); + } urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseKernel(Kernel); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } @@ -46,9 +53,6 @@ struct ur_kernel_handle_t_ { ur_program_handle_t Program, ur_context_handle_t Context, ur_kernel_handle_t &Kernel) { - if (!Program || !Context) { - return UR_RESULT_ERROR_INVALID_NULL_HANDLE; - } try { cl_context CLContext; CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_CONTEXT, @@ -62,9 +66,17 @@ struct ur_kernel_handle_t_ { if (Context->get() != CLContext) { return UR_RESULT_ERROR_INVALID_CONTEXT; } - if (Program->get() != CLProgram) { - return UR_RESULT_ERROR_INVALID_PROGRAM; + if (Program) { + if (Program->get() != CLProgram) { + return UR_RESULT_ERROR_INVALID_PROGRAM; + } + } else { + ur_native_handle_t hNativeHandle = + reinterpret_cast(CLProgram); + UR_RETURN_ON_FAILURE(urProgramCreateWithNativeHandle( + hNativeHandle, Context, nullptr, &Program)); } + auto URKernel = std::make_unique(NativeKernel, Program, Context); Kernel = URKernel.release(); diff --git a/source/adapters/opencl/memory.cpp b/source/adapters/opencl/memory.cpp index 064feba00d..4d41a9463f 100644 --- a/source/adapters/opencl/memory.cpp +++ b/source/adapters/opencl/memory.cpp @@ -368,9 +368,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle( cl_mem NativeHandle = reinterpret_cast(hNativeMem); UR_RETURN_ON_FAILURE( ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainMemObject((*phMem)->get())); - } + (*phMem)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } @@ -382,9 +381,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle( cl_mem NativeHandle = reinterpret_cast(hNativeMem); UR_RETURN_ON_FAILURE( ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainMemObject(NativeHandle)); - } + (*phMem)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } @@ -441,7 +439,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, } UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { - CL_RETURN_ON_FAILURE(clRetainMemObject(hMem->get())); hMem->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -449,8 +446,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) { if (hMem->decrementReferenceCount() == 0) { delete hMem; - } else { - CL_RETURN_ON_FAILURE(clReleaseMemObject(hMem->get())); } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/memory.hpp b/source/adapters/opencl/memory.hpp index df8794c897..f54d9b1347 100644 --- a/source/adapters/opencl/memory.hpp +++ b/source/adapters/opencl/memory.hpp @@ -19,6 +19,7 @@ struct ur_mem_handle_t_ { native_type Memory; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_mem_handle_t_(native_type Mem, ur_context_handle_t Ctx) : Memory(Mem), Context(Ctx) { @@ -27,8 +28,10 @@ struct ur_mem_handle_t_ { } ~ur_mem_handle_t_() { - clReleaseMemObject(Memory); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseMemObject(Memory); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index eb1f109712..2703c319c9 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -356,7 +356,6 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UR_APIEXPORT ur_result_t UR_APICALL urProgramRetain(ur_program_handle_t hProgram) { - CL_RETURN_ON_FAILURE(clRetainProgram(hProgram->get())); hProgram->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -365,8 +364,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramRelease(ur_program_handle_t hProgram) { if (hProgram->decrementReferenceCount() == 0) { delete hProgram; - } else { - CL_RETURN_ON_FAILURE(clReleaseProgram(hProgram->get())); } return UR_RESULT_SUCCESS; } @@ -386,9 +383,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle( UR_RETURN_ON_FAILURE( ur_program_handle_t_::makeWithNative(NativeHandle, hContext, *phProgram)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainProgram(NativeHandle)); - } + (*phProgram)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/program.hpp b/source/adapters/opencl/program.hpp index 1c6bae2e8d..f2c065d895 100644 --- a/source/adapters/opencl/program.hpp +++ b/source/adapters/opencl/program.hpp @@ -19,6 +19,7 @@ struct ur_program_handle_t_ { native_type Program; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx) : Program(Prog), Context(Ctx) { @@ -27,8 +28,10 @@ struct ur_program_handle_t_ { } ~ur_program_handle_t_() { - clReleaseProgram(Program); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseProgram(Program); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/queue.cpp b/source/adapters/opencl/queue.cpp index 2cbeb6f738..68f59d4563 100644 --- a/source/adapters/opencl/queue.cpp +++ b/source/adapters/opencl/queue.cpp @@ -191,9 +191,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( UR_RETURN_ON_FAILURE(ur_queue_handle_t_::makeWithNative( NativeHandle, hContext, hDevice, *phQueue)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainCommandQueue(NativeHandle)); - } + (*phQueue)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } @@ -211,7 +210,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush(ur_queue_handle_t hQueue) { } UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { - CL_RETURN_ON_FAILURE(clRetainCommandQueue(hQueue->get())); hQueue->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -219,8 +217,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { UR_APIEXPORT ur_result_t UR_APICALL urQueueRelease(ur_queue_handle_t hQueue) { if (hQueue->decrementReferenceCount() == 0) { delete hQueue; - } else { - CL_RETURN_ON_FAILURE(clReleaseCommandQueue(hQueue->get())); } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/queue.hpp b/source/adapters/opencl/queue.hpp index 6ea50402a9..b3c05346cd 100644 --- a/source/adapters/opencl/queue.hpp +++ b/source/adapters/opencl/queue.hpp @@ -21,12 +21,15 @@ struct ur_queue_handle_t_ { ur_context_handle_t Context; ur_device_handle_t Device; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_queue_handle_t_(native_type Queue, ur_context_handle_t Ctx, ur_device_handle_t Dev) : Queue(Queue), Context(Ctx), Device(Dev) { RefCount = 1; - urDeviceRetain(Device); + if (Device) { + urDeviceRetain(Device); + } urContextRetain(Context); } @@ -45,8 +48,15 @@ struct ur_queue_handle_t_ { if (Context->get() != CLContext) { return UR_RESULT_ERROR_INVALID_CONTEXT; } - if (Device->get() != CLDevice) { - return UR_RESULT_ERROR_INVALID_DEVICE; + if (Device) { + if (Device->get() != CLDevice) { + return UR_RESULT_ERROR_INVALID_DEVICE; + } + } else { + ur_native_handle_t hNativeHandle = + reinterpret_cast(CLDevice); + UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle( + hNativeHandle, nullptr, nullptr, &Device)); } auto URQueue = std::make_unique(NativeQueue, Context, Device); @@ -60,9 +70,13 @@ struct ur_queue_handle_t_ { } ~ur_queue_handle_t_() { - clReleaseCommandQueue(Queue); - urDeviceRelease(Device); + if (Device) { + urDeviceRelease(Device); + } urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseCommandQueue(Queue); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/sampler.cpp b/source/adapters/opencl/sampler.cpp index 9dee3065f3..7a8ebeba2f 100644 --- a/source/adapters/opencl/sampler.cpp +++ b/source/adapters/opencl/sampler.cpp @@ -220,7 +220,6 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urSamplerRetain(ur_sampler_handle_t hSampler) { - CL_RETURN_ON_FAILURE(clRetainSampler(hSampler->get())); hSampler->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -229,8 +228,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urSamplerRelease(ur_sampler_handle_t hSampler) { if (hSampler->decrementReferenceCount() == 0) { delete hSampler; - } else { - CL_RETURN_ON_FAILURE(clRetainSampler(hSampler->get())); } return UR_RESULT_SUCCESS; } @@ -249,6 +246,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urSamplerCreateWithNativeHandle( try { auto URSampler = std::make_unique(NativeHandle, hContext); + URSampler->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; *phSampler = URSampler.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; @@ -256,9 +255,5 @@ UR_APIEXPORT ur_result_t UR_APICALL urSamplerCreateWithNativeHandle( return UR_RESULT_ERROR_UNKNOWN; } - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainSampler(NativeHandle)); - } - return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/sampler.hpp b/source/adapters/opencl/sampler.hpp index 238ee1cecc..49f8ec43eb 100644 --- a/source/adapters/opencl/sampler.hpp +++ b/source/adapters/opencl/sampler.hpp @@ -18,6 +18,7 @@ struct ur_sampler_handle_t_ { native_type Sampler; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = false; ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx) : Sampler(Sampler), Context(Ctx) { @@ -26,8 +27,10 @@ struct ur_sampler_handle_t_ { } ~ur_sampler_handle_t_() { - clReleaseSampler(Sampler); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseSampler(Sampler); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/test/conformance/context/context_adapter_opencl.match b/test/conformance/context/context_adapter_opencl.match deleted file mode 100644 index 869f11cd42..0000000000 --- a/test/conformance/context/context_adapter_opencl.match +++ /dev/null @@ -1 +0,0 @@ -urContextCreateWithNativeHandleTest.SuccessWithUnOwnedNativeHandle/Intel_R__OpenCL___{{.*}}_