diff --git a/scripts/core/PROG.rst b/scripts/core/PROG.rst index 6ab208166d..1fe4ce19b8 100644 --- a/scripts/core/PROG.rst +++ b/scripts/core/PROG.rst @@ -269,17 +269,6 @@ ${x}QueueRetain. An application must call ${x}QueueRelease when a queue object is no longer needed. When a queue object's reference count becomes zero, it is deleted by the runtime. -Native Driver Access ----------------------------------- - -The runtime API provides accessors for native handles. -For example, given a ${x}_program_handle_t, we can -call ${x}ProgramGetNativeHandle to retrieve a ${x}_native_handle_t. -We can then leverage a platform extension to convert the -native handle to a driver handle. For example, OpenCL platform -may expose an extension ${x}ProgramCreateWithNativeHandle to retrieve -a cl_program. - Memory ====== @@ -343,10 +332,9 @@ Native Handle Ownership ----------------------- By default a ${X} object constructed from a native handle doesn't own the -native handle, it is guaranteed not to modify the native handle's reference -count or otherwise cause its resources to be released. A ${X} object that -doesn't own its associated native handle **must** be destroyed before the -native handle is. +native handle, it is guaranteed not to retain a reference to the native handle, +or cause its resources to be released. A ${X} object that doesn't own its +associated native handle **must** be destroyed before the native handle is. Ownership of the native handle can be tranferred to the ${X} object by passing ``isNativeHandleOwned = true`` in the native properties struct when calling the diff --git a/source/adapters/cuda/queue.cpp b/source/adapters/cuda/queue.cpp index 548940f853..b9779f37be 100644 --- a/source/adapters/cuda/queue.cpp +++ b/source/adapters/cuda/queue.cpp @@ -284,17 +284,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( std::vector ComputeCuStreams(1, CuStream); std::vector TransferCuStreams(0); + auto isNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; + // Create queue and set num_compute_streams to 1, as computeCuStreams has // valid stream - *phQueue = - new ur_queue_handle_t_{std::move(ComputeCuStreams), - std::move(TransferCuStreams), - hContext, - hDevice, - CuFlags, - Flags, - /*priority*/ 0, - /*backend_owns*/ pProperties->isNativeHandleOwned}; + *phQueue = new ur_queue_handle_t_{std::move(ComputeCuStreams), + std::move(TransferCuStreams), + hContext, + hDevice, + CuFlags, + Flags, + /*priority*/ 0, + /*backend_owns*/ isNativeHandleOwned}; (*phQueue)->NumComputeStreams = 1; return UR_RESULT_SUCCESS; diff --git a/source/adapters/hip/memory.cpp b/source/adapters/hip/memory.cpp index 93d8450862..700fdfeb4a 100644 --- a/source/adapters/hip/memory.cpp +++ b/source/adapters/hip/memory.cpp @@ -301,6 +301,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemGetNativeHandle(ur_mem_handle_t hMem, ur_device_handle_t Device, ur_native_handle_t *phNativeMem) { UR_ASSERT(Device != nullptr, UR_RESULT_ERROR_INVALID_NULL_HANDLE); + if (hMem->isImage()) { + return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; + } #if defined(__HIP_PLATFORM_NVIDIA__) if (sizeof(BufferMem::native_type) > sizeof(ur_native_handle_t)) { // Check that all the upper bits that cannot be represented by diff --git a/source/adapters/hip/queue.cpp b/source/adapters/hip/queue.cpp index 5ab28d45ba..94177e097d 100644 --- a/source/adapters/hip/queue.cpp +++ b/source/adapters/hip/queue.cpp @@ -318,17 +318,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( std::vector ComputeHIPStreams(1, HIPStream); std::vector TransferHIPStreams(0); + auto isNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; + // Create queue and set num_compute_streams to 1, as computeHIPStreams has // valid stream - *phQueue = - new ur_queue_handle_t_{std::move(ComputeHIPStreams), - std::move(TransferHIPStreams), - hContext, - hDevice, - HIPFlags, - Flags, - /*priority*/ 0, - /*backend_owns*/ pProperties->isNativeHandleOwned}; + *phQueue = new ur_queue_handle_t_{std::move(ComputeHIPStreams), + std::move(TransferHIPStreams), + hContext, + hDevice, + HIPFlags, + Flags, + /*priority*/ 0, + /*backend_owns*/ isNativeHandleOwned}; (*phQueue)->NumComputeStreams = 1; return UR_RESULT_SUCCESS; diff --git a/source/adapters/level_zero/kernel.cpp b/source/adapters/level_zero/kernel.cpp index b98b7e701b..3d5beba793 100644 --- a/source/adapters/level_zero/kernel.cpp +++ b/source/adapters/level_zero/kernel.cpp @@ -1079,9 +1079,9 @@ ur_result_t urKernelCreateWithNativeHandle( ze_kernel_handle_t ZeKernel = ur_cast(NativeKernel); ur_kernel_handle_t_ *Kernel = nullptr; try { - Kernel = new ur_kernel_handle_t_(ZeKernel, Properties->isNativeHandleOwned, - Context); - if (Properties->isNativeHandleOwned) { + auto OwnNativeHandle = Properties ? Properties->isNativeHandleOwned : false; + Kernel = new ur_kernel_handle_t_(ZeKernel, OwnNativeHandle, Context); + if (OwnNativeHandle) { // If ownership is passed to the adapter we need to pass the kernel // to this vector which is then used during ZeKernelRelease. Kernel->ZeKernels.push_back(ZeKernel); diff --git a/source/adapters/level_zero/program.cpp b/source/adapters/level_zero/program.cpp index d7adc5eb37..5cba6a72f6 100644 --- a/source/adapters/level_zero/program.cpp +++ b/source/adapters/level_zero/program.cpp @@ -931,7 +931,8 @@ ur_result_t urProgramGetNativeHandle( } } if (!Module) - return UR_RESULT_ERROR_INVALID_OPERATION; + // L0 only supprts returning native handle from built programs. + return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; *ZeModule = Module; return UR_RESULT_SUCCESS; diff --git a/source/adapters/opencl/ur_interface_loader.cpp b/source/adapters/opencl/ur_interface_loader.cpp index cba90ee152..c0f7d7998e 100644 --- a/source/adapters/opencl/ur_interface_loader.cpp +++ b/source/adapters/opencl/ur_interface_loader.cpp @@ -159,6 +159,7 @@ urGetMemProcAddrTable(ur_api_version_t Version, ur_mem_dditable_t *pDdiTable) { pDdiTable->pfnGetNativeHandle = urMemGetNativeHandle; pDdiTable->pfnImageCreate = urMemImageCreate; pDdiTable->pfnImageGetInfo = urMemImageGetInfo; + pDdiTable->pfnImageCreateWithNativeHandle = urMemImageCreateWithNativeHandle; pDdiTable->pfnRelease = urMemRelease; pDdiTable->pfnRetain = urMemRetain; return UR_RESULT_SUCCESS; diff --git a/test/conformance/event/event_adapter_native_cpu.match b/test/conformance/event/event_adapter_native_cpu.match index 95b8228e1f..a8c6b213cf 100644 --- a/test/conformance/event/event_adapter_native_cpu.match +++ b/test/conformance/event/event_adapter_native_cpu.match @@ -25,6 +25,7 @@ urEventReleaseTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urEventGetNativeHandleTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urEventGetNativeHandleTest.InvalidNullPointerNativeEvent/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urEventCreateWithNativeHandleTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} +urEventCreateWithNativeHandleTest.SuccessWithProperties/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urEventSetCallbackTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urEventSetCallbackTest.ValidateParameters/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urEventSetCallbackTest.AllStates/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} diff --git a/test/conformance/kernel/kernel_adapter_native_cpu.match b/test/conformance/kernel/kernel_adapter_native_cpu.match index 368f4ad358..ffdabbafc9 100644 --- a/test/conformance/kernel/kernel_adapter_native_cpu.match +++ b/test/conformance/kernel/kernel_adapter_native_cpu.match @@ -5,6 +5,7 @@ urKernelCreateTest.InvalidNullPointerName/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{. urKernelCreateTest.InvalidNullPointerKernel/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urKernelCreateTest.InvalidKernelName/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urKernelCreateWithNativeHandleTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} +urKernelCreateWithNativeHandleTest.SuccessWithProperties/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urKernelCreateWithNativeHandleTest.InvalidNullHandleContext/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urKernelCreateWithNativeHandleTest.InvalidNullPointerNativeKernel/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} urKernelGetGroupInfoTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}}__UR_KERNEL_GROUP_INFO_GLOBAL_WORK_SIZE diff --git a/test/conformance/memory/memory_adapter_level_zero_v2.match b/test/conformance/memory/memory_adapter_level_zero_v2.match index 3ff57fc1e5..4bdd65c849 100644 --- a/test/conformance/memory/memory_adapter_level_zero_v2.match +++ b/test/conformance/memory/memory_adapter_level_zero_v2.match @@ -232,6 +232,8 @@ {{OPT}}urMemImageCreateTestWithImageFormatParam.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_IMAGE_CHANNEL_ORDER_SRGBA__UR_IMAGE_CHANNEL_TYPE_UNSIGNED_INT32 {{OPT}}urMemImageCreateTestWithImageFormatParam.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_IMAGE_CHANNEL_ORDER_SRGBA__UR_IMAGE_CHANNEL_TYPE_HALF_FLOAT {{OPT}}urMemImageCreateTestWithImageFormatParam.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_IMAGE_CHANNEL_ORDER_SRGBA__UR_IMAGE_CHANNEL_TYPE_FLOAT +{{OPT}}urMemImageCreateWithNativeHandleTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}} +{{OPT}}urMemImageCreateWithNativeHandleTest.SuccessWithProperties/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}} {{OPT}}urMemImageGetInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_IMAGE_INFO_FORMAT {{OPT}}urMemImageGetInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_IMAGE_INFO_ELEMENT_SIZE {{OPT}}urMemImageGetInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_IMAGE_INFO_ROW_PITCH diff --git a/test/conformance/memory/urMemBufferCreateWithNativeHandle.cpp b/test/conformance/memory/urMemBufferCreateWithNativeHandle.cpp index 1a44a98481..c3b2fb6fbe 100644 --- a/test/conformance/memory/urMemBufferCreateWithNativeHandle.cpp +++ b/test/conformance/memory/urMemBufferCreateWithNativeHandle.cpp @@ -53,30 +53,9 @@ TEST_P(urMemBufferCreateWithNativeHandleTest, SuccessWithProperties) { ASSERT_SUCCESS(urMemRelease(mem)); } -TEST_P(urMemBufferCreateWithNativeHandleTest, SuccessWithUnOwnedNativeHandle) { - ur_native_handle_t native_handle = 0; - ASSERT_SUCCESS(urMemGetNativeHandle(buffer, device, &native_handle)); - - ur_mem_handle_t mem = nullptr; - ur_mem_native_properties_t props = { - /*.stype =*/UR_STRUCTURE_TYPE_MEM_NATIVE_PROPERTIES, - /*.pNext =*/nullptr, - /*.isNativeHandleOwned =*/false, - }; - ASSERT_SUCCESS(urMemBufferCreateWithNativeHandle(native_handle, context, - &props, &mem)); - ASSERT_NE(nullptr, mem); - - ur_context_handle_t mem_context = nullptr; - ASSERT_SUCCESS(urMemGetInfo(mem, UR_MEM_INFO_CONTEXT, - sizeof(ur_context_handle_t), &mem_context, - nullptr)); - ASSERT_EQ(context, mem_context); -} - TEST_P(urMemBufferCreateWithNativeHandleTest, InvalidNullHandle) { ur_native_handle_t hNativeMem = 0; - ASSERT_SUCCESS(urMemGetNativeHandle(buffer, device, &hNativeMem)); + UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(urMemGetNativeHandle(buffer, device, &hNativeMem)); ur_mem_handle_t mem = nullptr; ur_mem_native_properties_t props = { @@ -91,7 +70,7 @@ TEST_P(urMemBufferCreateWithNativeHandleTest, InvalidNullHandle) { TEST_P(urMemBufferCreateWithNativeHandleTest, InvalidNullPointer) { ur_native_handle_t hNativeMem = 0; - ASSERT_SUCCESS(urMemGetNativeHandle(buffer, device, &hNativeMem)); + UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(urMemGetNativeHandle(buffer, device, &hNativeMem)); ur_mem_native_properties_t props = { /*.stype =*/UR_STRUCTURE_TYPE_MEM_NATIVE_PROPERTIES, diff --git a/test/conformance/program/program_adapter_native_cpu.match b/test/conformance/program/program_adapter_native_cpu.match index 47163ce042..bb2e37980d 100644 --- a/test/conformance/program/program_adapter_native_cpu.match +++ b/test/conformance/program/program_adapter_native_cpu.match @@ -23,6 +23,7 @@ {{OPT}}urProgramCreateWithILTest.InvalidNullPointerProgram/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} {{OPT}}urProgramCreateWithILTest.BuildInvalidProgram/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} {{OPT}}urProgramCreateWithNativeHandleTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} +{{OPT}}urProgramCreateWithNativeHandleTest.SuccessWithProperties/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} {{OPT}}urProgramCreateWithNativeHandleTest.InvalidNullHandleContext/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} {{OPT}}urProgramCreateWithNativeHandleTest.InvalidNullPointerProgram/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}} {{OPT}}urProgramGetBuildInfoTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}}__UR_PROGRAM_BUILD_INFO_STATUS