diff --git a/source/adapters/opencl/context.cpp b/source/adapters/opencl/context.cpp index 59b3fefd0a..1382ec3c47 100644 --- a/source/adapters/opencl/context.cpp +++ b/source/adapters/opencl/context.cpp @@ -97,17 +97,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle( - ur_native_handle_t hNativeContext, uint32_t numDevices, const ur_device_handle_t *phDevices, - const ur_context_native_properties_t *pProperties, ur_context_handle_t *phContext) { + ur_native_handle_t hNativeContext, uint32_t numDevices, + const ur_device_handle_t *phDevices, + const ur_context_native_properties_t *pProperties, + ur_context_handle_t *phContext) { cl_context NativeHandle = reinterpret_cast(hNativeContext); - auto URContext = std::make_unique( - NativeHandle, numDevices, phDevices); UR_RETURN_ON_FAILURE(ur_context_handle_t_::makeWithNative( NativeHandle, numDevices, phDevices, *phContext)); + if (!pProperties || !pProperties->isNativeHandleOwned) { - return clRetainContext(NativeHandle); + CL_RETURN_ON_FAILURE(clRetainContext(NativeHandle)); } + return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/context.hpp b/source/adapters/opencl/context.hpp index 8c40e3bcf5..555636b1b8 100644 --- a/source/adapters/opencl/context.hpp +++ b/source/adapters/opencl/context.hpp @@ -40,29 +40,28 @@ struct ur_context_handle_t_ { static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount, const ur_device_handle_t *phDevices, ur_context_handle_t &Context) { + if (!phDevices) { + return UR_RESULT_ERROR_INVALID_NULL_POINTER; + } try { - auto URContext = - std::make_unique(Ctx, DevCount, phDevices); - CL_RETURN_ON_FAILURE(clRetainContext(Ctx)); - native_type &NativeContext = URContext->Context; - uint32_t &DeviceCount = URContext->DeviceCount; - if (!DeviceCount) { - CL_RETURN_ON_FAILURE( - clGetContextInfo(NativeContext, CL_CONTEXT_NUM_DEVICES, - sizeof(DeviceCount), &DeviceCount, nullptr)); - std::vector CLDevices(DeviceCount); - CL_RETURN_ON_FAILURE(clGetContextInfo(NativeContext, CL_CONTEXT_DEVICES, - sizeof(CLDevices), - CLDevices.data(), nullptr)); - URContext->Devices.resize(DeviceCount); - for (uint32_t i = 0; i < DeviceCount; i++) { - ur_native_handle_t NativeDevice = - reinterpret_cast(CLDevices[i]); - UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle( - NativeDevice, nullptr, nullptr, &(URContext->Devices[i]))); - UR_RETURN_ON_FAILURE(urDeviceRetain(URContext->Devices[i])); + uint32_t CLDeviceCount; + CL_RETURN_ON_FAILURE(clGetContextInfo(Ctx, CL_CONTEXT_NUM_DEVICES, + sizeof(CLDeviceCount), + &CLDeviceCount, nullptr)); + std::vector CLDevices(CLDeviceCount); + 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]) { + return UR_RESULT_ERROR_INVALID_CONTEXT; } } + auto URContext = + std::make_unique(Ctx, DevCount, phDevices); Context = URContext.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; @@ -81,4 +80,6 @@ struct ur_context_handle_t_ { } native_type get() { return Context; } + + const std::vector &getDevices() { return Devices; } }; diff --git a/source/adapters/opencl/device.cpp b/source/adapters/opencl/device.cpp index f63da30da2..fb776926bb 100644 --- a/source/adapters/opencl/device.cpp +++ b/source/adapters/opencl/device.cpp @@ -966,8 +966,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( CLSubDevices[i], hDevice->Platform, hDevice); phSubDevices[i] = URSubDevice.release(); } catch (std::bad_alloc &) { + // Delete all the successfully created subdevices before the failed one. + for (uint32_t j = 0; j < i; j++) { + delete phSubDevices[j]; + } return UR_RESULT_ERROR_OUT_OF_RESOURCES; } catch (...) { + // Delete all the successfully created subdevices before the failed one. + for (uint32_t j = 0; j < i; j++) { + delete phSubDevices[j]; + } return UR_RESULT_ERROR_UNKNOWN; } } diff --git a/source/adapters/opencl/event.hpp b/source/adapters/opencl/event.hpp index 70b577eb7e..f7f17e7e1e 100644 --- a/source/adapters/opencl/event.hpp +++ b/source/adapters/opencl/event.hpp @@ -24,18 +24,14 @@ struct ur_event_handle_t_ { ur_queue_handle_t Queue) : Event(Event), Context(Ctx), Queue(Queue) { RefCount = 1; - if (Context) { - urContextRetain(Context); - } + urContextRetain(Context); if (Queue) { urQueueRetain(Queue); } } ~ur_event_handle_t_() { - if (Context) { - urContextRelease(Context); - } + urContextRelease(Context); if (Queue) { urQueueRelease(Queue); } diff --git a/source/adapters/opencl/kernel.cpp b/source/adapters/opencl/kernel.cpp index 6e2e13e5f1..899af56fe7 100644 --- a/source/adapters/opencl/kernel.cpp +++ b/source/adapters/opencl/kernel.cpp @@ -407,7 +407,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelCreateWithNativeHandle( NativeHandle, hProgram, hContext, *phKernel)); if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainKernel((*phKernel)->get())); + CL_RETURN_ON_FAILURE(clRetainKernel(NativeHandle)); } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/kernel.hpp b/source/adapters/opencl/kernel.hpp index f4ebb3e61b..44651ebfc7 100644 --- a/source/adapters/opencl/kernel.hpp +++ b/source/adapters/opencl/kernel.hpp @@ -11,6 +11,7 @@ #include "common.hpp" #include "context.hpp" +#include "program.hpp" #include @@ -25,22 +26,14 @@ struct ur_kernel_handle_t_ { ur_context_handle_t Context) : Kernel(Kernel), Program(Program), Context(Context) { RefCount = 1; - if (Program) { - urProgramRetain(Program); - } - if (Context) { - urContextRetain(Context); - } + urProgramRetain(Program); + urContextRetain(Context); } ~ur_kernel_handle_t_() { clReleaseKernel(Kernel); - if (Program) { - urProgramRelease(Program); - } - if (Context) { - urContextRelease(Context); - } + urProgramRelease(Program); + urContextRelease(Context); } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } @@ -53,33 +46,27 @@ 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 { - auto URKernel = - std::make_unique(NativeKernel, Program, Context); - if (!Program) { - cl_program CLProgram; - CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_PROGRAM, - sizeof(CLProgram), &CLProgram, - nullptr)); - ur_native_handle_t NativeProgram = - reinterpret_cast(CLProgram); - UR_RETURN_ON_FAILURE(urProgramCreateWithNativeHandle( - NativeProgram, nullptr, nullptr, &(URKernel->Program))); - UR_RETURN_ON_FAILURE(urProgramRetain(URKernel->Program)); - } cl_context CLContext; CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_CONTEXT, sizeof(CLContext), &CLContext, nullptr)); - if (!Context) { - ur_native_handle_t NativeContext = - reinterpret_cast(CLContext); - UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle( - NativeContext, 0, nullptr, nullptr, &(URKernel->Context))); - UR_RETURN_ON_FAILURE(urContextRetain(URKernel->Context)); - } else if (Context->get() != CLContext) { + cl_program CLProgram; + CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_PROGRAM, + sizeof(CLProgram), &CLProgram, + nullptr)); + + if (Context->get() != CLContext) { return UR_RESULT_ERROR_INVALID_CONTEXT; } + if (Program->get() != CLProgram) { + return UR_RESULT_ERROR_INVALID_PROGRAM; + } + auto URKernel = + std::make_unique(NativeKernel, Program, Context); Kernel = URKernel.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; diff --git a/source/adapters/opencl/memory.cpp b/source/adapters/opencl/memory.cpp index 8862bbf5e5..db4af8f409 100644 --- a/source/adapters/opencl/memory.cpp +++ b/source/adapters/opencl/memory.cpp @@ -390,7 +390,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle( UR_RETURN_ON_FAILURE( ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem)); if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainMemObject((*phMem)->get())); + CL_RETURN_ON_FAILURE(clRetainMemObject(NativeHandle)); } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/memory.hpp b/source/adapters/opencl/memory.hpp index a91001d1a6..df8794c897 100644 --- a/source/adapters/opencl/memory.hpp +++ b/source/adapters/opencl/memory.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "context.hpp" #include @@ -22,16 +23,12 @@ struct ur_mem_handle_t_ { ur_mem_handle_t_(native_type Mem, ur_context_handle_t Ctx) : Memory(Mem), Context(Ctx) { RefCount = 1; - if (Context) { - urContextRetain(Context); - } + urContextRetain(Context); } ~ur_mem_handle_t_() { clReleaseMemObject(Memory); - if (Context) { - urContextRelease(Context); - } + urContextRelease(Context); } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } @@ -43,18 +40,18 @@ struct ur_mem_handle_t_ { static ur_result_t makeWithNative(native_type NativeMem, ur_context_handle_t Ctx, ur_mem_handle_t &Mem) { + if (!Ctx) { + return UR_RESULT_ERROR_INVALID_NULL_HANDLE; + } try { - auto URMem = std::make_unique(NativeMem, Ctx); - if (!Ctx) { - cl_context CLContext; - CL_RETURN_ON_FAILURE(clGetMemObjectInfo( - NativeMem, CL_MEM_CONTEXT, sizeof(CLContext), &CLContext, nullptr)); - ur_native_handle_t NativeContext = - reinterpret_cast(CLContext); - UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle( - NativeContext, 0, nullptr, nullptr, &(URMem->Context))); - UR_RETURN_ON_FAILURE(urContextRetain(URMem->Context)); + cl_context CLContext; + CL_RETURN_ON_FAILURE(clGetMemObjectInfo( + NativeMem, CL_MEM_CONTEXT, sizeof(CLContext), &CLContext, nullptr)); + + if (Ctx->get() != CLContext) { + return UR_RESULT_ERROR_INVALID_CONTEXT; } + auto URMem = std::make_unique(NativeMem, Ctx); Mem = URMem.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; diff --git a/source/adapters/opencl/platform.hpp b/source/adapters/opencl/platform.hpp index 09cc7742f5..6a9c49eb37 100644 --- a/source/adapters/opencl/platform.hpp +++ b/source/adapters/opencl/platform.hpp @@ -71,7 +71,6 @@ struct ur_platform_handle_t_ { } ur_result_t getPlatformVersion(oclv::OpenCLVersion &Version) { - size_t PlatVerSize = 0; CL_RETURN_ON_FAILURE(clGetPlatformInfo(Platform, CL_PLATFORM_VERSION, 0, nullptr, &PlatVerSize)); diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index e4b7d28f10..cf9a35c0bb 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -33,7 +33,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( if (PlatVer >= oclv::V2_1) { /* Make sure all devices support CL 2.1 or newer as well. */ - for (ur_device_handle_t URDev : hContext->Devices) { + for (ur_device_handle_t URDev : hContext->getDevices()) { oclv::OpenCLVersion DevVer; CL_RETURN_ON_FAILURE_AND_SET_NULL(URDev->getDeviceVersion(DevVer), @@ -70,7 +70,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( /* If none of the devices conform with CL 2.1 or newer make sure they all * support the cl_khr_il_program extension. */ - for (ur_device_handle_t URDev : hContext->Devices) { + for (ur_device_handle_t URDev : hContext->getDevices()) { bool Supported = false; CL_RETURN_ON_FAILURE_AND_SET_NULL( URDev->checkDeviceExtensions({"cl_khr_il_program"}, Supported), @@ -178,7 +178,6 @@ static cl_int mapURProgramInfoToCL(ur_program_info_t URPropName) { UR_APIEXPORT ur_result_t UR_APICALL urProgramGetInfo(ur_program_handle_t hProgram, ur_program_info_t propName, size_t propSize, void *pPropValue, size_t *pPropSizeRet) { - UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); const cl_program_info CLPropName = mapURProgramInfoToCL(propName); @@ -368,7 +367,7 @@ 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((*phProgram)->get())); + CL_RETURN_ON_FAILURE(clRetainProgram(NativeHandle)); } return UR_RESULT_SUCCESS; } @@ -386,8 +385,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants( return UR_RESULT_ERROR_INVALID_CONTEXT; } - std::vector &DevicesInCtx = Ctx->Devices; - ur_platform_handle_t CurPlatform = Ctx->Devices[0]->Platform; oclv::OpenCLVersion PlatVer; @@ -397,7 +394,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants( if (PlatVer < oclv::V2_2) { UseExtensionLookup = true; } else { - for (ur_device_handle_t Dev : DevicesInCtx) { + for (ur_device_handle_t Dev : Ctx->getDevices()) { oclv::OpenCLVersion DevVer; UR_RETURN_ON_FAILURE(Dev->getDeviceVersion(DevVer)); diff --git a/source/adapters/opencl/program.hpp b/source/adapters/opencl/program.hpp index 85ddfde6da..1c6bae2e8d 100644 --- a/source/adapters/opencl/program.hpp +++ b/source/adapters/opencl/program.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "context.hpp" #include @@ -22,16 +23,12 @@ struct ur_program_handle_t_ { ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx) : Program(Prog), Context(Ctx) { RefCount = 1; - if (Context) { - urContextRetain(Context); - } + urContextRetain(Context); } ~ur_program_handle_t_() { clReleaseProgram(Program); - if (Context) { - urContextRelease(Context); - } + urContextRelease(Context); } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } @@ -43,20 +40,19 @@ struct ur_program_handle_t_ { static ur_result_t makeWithNative(native_type NativeProg, ur_context_handle_t Context, ur_program_handle_t &Program) { + if (!Context) { + return UR_RESULT_ERROR_INVALID_NULL_HANDLE; + } try { + cl_context CLContext; + CL_RETURN_ON_FAILURE(clGetProgramInfo(NativeProg, CL_PROGRAM_CONTEXT, + sizeof(CLContext), &CLContext, + nullptr)); + if (Context->get() != CLContext) { + return UR_RESULT_ERROR_INVALID_CONTEXT; + } auto URProgram = std::make_unique(NativeProg, Context); - if (!Context) { - cl_context CLContext; - CL_RETURN_ON_FAILURE(clGetProgramInfo(NativeProg, CL_PROGRAM_CONTEXT, - sizeof(CLContext), &CLContext, - nullptr)); - ur_native_handle_t NativeContext = - reinterpret_cast(CLContext); - UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle( - NativeContext, 0, nullptr, nullptr, &(URProgram->Context))); - UR_RETURN_ON_FAILURE(urContextRetain(URProgram->Context)); - } Program = URProgram.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; diff --git a/source/adapters/opencl/queue.cpp b/source/adapters/opencl/queue.cpp index 605ef2fcf2..9328e32092 100644 --- a/source/adapters/opencl/queue.cpp +++ b/source/adapters/opencl/queue.cpp @@ -182,8 +182,7 @@ urQueueGetNativeHandle(ur_queue_handle_t hQueue, ur_queue_native_desc_t *, UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( ur_native_handle_t hNativeQueue, ur_context_handle_t hContext, - ur_device_handle_t hDevice, - [[maybe_unused]] const ur_queue_native_properties_t *pProperties, + ur_device_handle_t hDevice, const ur_queue_native_properties_t *pProperties, ur_queue_handle_t *phQueue) { cl_command_queue NativeHandle = @@ -192,7 +191,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( UR_RETURN_ON_FAILURE(ur_queue_handle_t_::makeWithNative( NativeHandle, hContext, hDevice, *phQueue)); - CL_RETURN_ON_FAILURE(clRetainCommandQueue(NativeHandle)); + if (!pProperties || !pProperties->isNativeHandleOwned) { + CL_RETURN_ON_FAILURE(clRetainCommandQueue(NativeHandle)); + } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/queue.hpp b/source/adapters/opencl/queue.hpp index 63d6a6ea50..e44af5f4d9 100644 --- a/source/adapters/opencl/queue.hpp +++ b/source/adapters/opencl/queue.hpp @@ -10,6 +10,8 @@ #pragma once #include "common.hpp" +#include "context.hpp" +#include "device.hpp" #include @@ -24,43 +26,33 @@ struct ur_queue_handle_t_ { ur_device_handle_t Dev) : Queue(Queue), Context(Ctx), Device(Dev) { RefCount = 1; - if (Device) { - urDeviceRetain(Device); - } - if (Context) { - urContextRetain(Context); - } + urDeviceRetain(Device); + urContextRetain(Context); } static ur_result_t makeWithNative(native_type NativeQueue, ur_context_handle_t Context, ur_device_handle_t Device, ur_queue_handle_t &Queue) { + if (!Context || !Device) { + return UR_RESULT_ERROR_INVALID_NULL_HANDLE; + } try { - auto URQueue = - std::make_unique(NativeQueue, Context, Device); - if (!Context) { - cl_context CLContext; - CL_RETURN_ON_FAILURE( - clGetCommandQueueInfo(NativeQueue, CL_QUEUE_CONTEXT, - sizeof(CLContext), &CLContext, nullptr)); - ur_native_handle_t NativeContext = - reinterpret_cast(CLContext); - UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle( - NativeContext, 0, nullptr, nullptr, &(URQueue->Context))); - UR_RETURN_ON_FAILURE(urContextRetain(URQueue->Context)); + cl_context CLContext; + CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(NativeQueue, CL_QUEUE_CONTEXT, + sizeof(CLContext), &CLContext, + nullptr)); + cl_device_id CLDevice; + CL_RETURN_ON_FAILURE(clGetCommandQueueInfo( + NativeQueue, CL_QUEUE_DEVICE, sizeof(CLDevice), &CLDevice, nullptr)); + if (Context->get() != CLContext) { + return UR_RESULT_ERROR_INVALID_CONTEXT; } - if (!Device) { - cl_device_id CLDevice; - CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(NativeQueue, CL_QUEUE_DEVICE, - sizeof(CLDevice), &CLDevice, - nullptr)); - ur_native_handle_t NativeDevice = - reinterpret_cast(CLDevice); - UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle( - NativeDevice, nullptr, nullptr, &(URQueue->Device))); - UR_RETURN_ON_FAILURE(urDeviceRetain(URQueue->Device)); + if (Device->get() != CLDevice) { + return UR_RESULT_ERROR_INVALID_DEVICE; } + auto URQueue = + std::make_unique(NativeQueue, Context, Device); Queue = URQueue.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; @@ -72,12 +64,8 @@ struct ur_queue_handle_t_ { ~ur_queue_handle_t_() { clReleaseCommandQueue(Queue); - if (Device) { - urDeviceRelease(Device); - } - if (Context) { - urContextRelease(Context); - } + urDeviceRelease(Device); + urContextRelease(Context); } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/sampler.hpp b/source/adapters/opencl/sampler.hpp index c3f01e124b..238ee1cecc 100644 --- a/source/adapters/opencl/sampler.hpp +++ b/source/adapters/opencl/sampler.hpp @@ -22,16 +22,12 @@ struct ur_sampler_handle_t_ { ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx) : Sampler(Sampler), Context(Ctx) { RefCount = 1; - if (Context) { - urContextRetain(Context); - } + urContextRetain(Context); } ~ur_sampler_handle_t_() { clReleaseSampler(Sampler); - if (Context) { - urContextRelease(Context); - } + urContextRelease(Context); } uint32_t incrementReferenceCount() noexcept { return ++RefCount; }