diff --git a/include/ur_api.h b/include/ur_api.h index 2f56b5dd16..90d62e7e8f 100644 --- a/include/ur_api.h +++ b/include/ur_api.h @@ -1188,6 +1188,8 @@ typedef struct ur_platform_native_properties_t { /// - ::UR_RESULT_ERROR_UNINITIALIZED /// - ::UR_RESULT_ERROR_DEVICE_LOST /// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hAdapter` /// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER /// + `NULL == phPlatform` /// - ::UR_RESULT_ERROR_UNSUPPORTED_FEATURE @@ -1195,6 +1197,7 @@ typedef struct ur_platform_native_properties_t { UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t hNativePlatform, ///< [in][nocheck] the native handle of the platform. + ur_adapter_handle_t hAdapter, ///< [in] handle of the adapter associated with the native backend. const ur_platform_native_properties_t *pProperties, ///< [in][optional] pointer to native platform properties struct. ur_platform_handle_t *phPlatform ///< [out] pointer to the handle of the platform object created. ); @@ -9552,6 +9555,7 @@ typedef struct ur_platform_get_native_handle_params_t { /// allowing the callback the ability to modify the parameter's value typedef struct ur_platform_create_with_native_handle_params_t { ur_native_handle_t *phNativePlatform; + ur_adapter_handle_t *phAdapter; const ur_platform_native_properties_t **ppProperties; ur_platform_handle_t **pphPlatform; } ur_platform_create_with_native_handle_params_t; diff --git a/include/ur_ddi.h b/include/ur_ddi.h index 4aaa6d9fe3..deff91472c 100644 --- a/include/ur_ddi.h +++ b/include/ur_ddi.h @@ -49,6 +49,7 @@ typedef ur_result_t(UR_APICALL *ur_pfnPlatformGetNativeHandle_t)( /// @brief Function-pointer for urPlatformCreateWithNativeHandle typedef ur_result_t(UR_APICALL *ur_pfnPlatformCreateWithNativeHandle_t)( ur_native_handle_t, + ur_adapter_handle_t, const ur_platform_native_properties_t *, ur_platform_handle_t *); diff --git a/include/ur_print.hpp b/include/ur_print.hpp index 0cc35e84c5..7bd6cc75ab 100644 --- a/include/ur_print.hpp +++ b/include/ur_print.hpp @@ -10214,6 +10214,12 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct ur::details::printPtr(os, *(params->phNativePlatform)); + os << ", "; + os << ".hAdapter = "; + + ur::details::printPtr(os, + *(params->phAdapter)); + os << ", "; os << ".pProperties = "; diff --git a/scripts/core/platform.yml b/scripts/core/platform.yml index c35b71622d..f3069005ab 100644 --- a/scripts/core/platform.yml +++ b/scripts/core/platform.yml @@ -211,6 +211,9 @@ params: - type: $x_native_handle_t name: hNativePlatform desc: "[in][nocheck] the native handle of the platform." + - type: $x_adapter_handle_t + name: hAdapter + desc: "[in] handle of the adapter associated with the native backend." - type: const $x_platform_native_properties_t* name: pProperties desc: "[in][optional] pointer to native platform properties struct." diff --git a/scripts/templates/helper.py b/scripts/templates/helper.py index dc6becfec5..0c90f4da8e 100644 --- a/scripts/templates/helper.py +++ b/scripts/templates/helper.py @@ -653,7 +653,7 @@ def get_adapter_handles(specs): objs = [] for s in specs: for obj in s['objects']: - if obj_traits.is_handle(obj) and not obj_traits.is_loader_only(obj): + if obj_traits.is_handle(obj) and not (obj_traits.is_loader_only(obj) or 'native' in obj['name']): objs.append(obj) return objs diff --git a/scripts/templates/ldrddi.cpp.mako b/scripts/templates/ldrddi.cpp.mako index bbc7c7c7d0..4da75d1f91 100644 --- a/scripts/templates/ldrddi.cpp.mako +++ b/scripts/templates/ldrddi.cpp.mako @@ -132,7 +132,7 @@ namespace ur_loader %else: <%param_replacements={}%> %for i, item in enumerate(th.get_loader_prologue(n, tags, obj, meta)): - %if not '_native_object_' in item['obj'] or th.make_func_name(n, tags, obj) == 'urPlatformCreateWithNativeHandle': + %if not '_native_object_' in item['obj']: // extract platform's function pointer table auto dditable = reinterpret_cast<${item['obj']}*>( ${item['pointer']}${item['name']} )->dditable; auto ${th.make_pfn_name(n, tags, obj)} = dditable->${n}.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)}; @@ -151,7 +151,7 @@ namespace ur_loader for( size_t i = ${item['range'][0]}; i < ${item['range'][1]}; ++i ) ${item['name']}Local[ i ] = reinterpret_cast<${item['obj']}*>( ${item['name']}[ i ] )->handle; %else: - %if not '_native_object_' in item['obj'] or th.make_func_name(n, tags, obj) == 'urPlatformCreateWithNativeHandle': + %if not '_native_object_' in item['obj']: // convert loader handle to platform handle %if item['optional']: ${item['name']} = ( ${item['name']} ) ? reinterpret_cast<${item['obj']}*>( ${item['name']} )->handle : nullptr; @@ -279,7 +279,7 @@ namespace ur_loader %if item['release']: // release loader handle ${item['factory']}.release( ${item['name']} ); - %elif not '_native_object_' in item['obj'] or th.make_func_name(n, tags, obj) == 'urPlatformCreateWithNativeHandle': + %elif not '_native_object_' in item['obj']: try { %if 'typename' in item: diff --git a/source/adapters/cuda/platform.cpp b/source/adapters/cuda/platform.cpp index 27b94f756f..d89a5b7a19 100644 --- a/source/adapters/cuda/platform.cpp +++ b/source/adapters/cuda/platform.cpp @@ -141,12 +141,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( - ur_native_handle_t hNativePlatform, - const ur_platform_native_properties_t *pProperties, - ur_platform_handle_t *phPlatform) { - std::ignore = hNativePlatform; - std::ignore = pProperties; - std::ignore = phPlatform; + ur_native_handle_t, ur_adapter_handle_t, + const ur_platform_native_properties_t *, ur_platform_handle_t *) { + // There is no CUDA equivalent to ur_platform_handle_t return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; } diff --git a/source/adapters/hip/platform.cpp b/source/adapters/hip/platform.cpp index ee66a70081..8671d70a57 100644 --- a/source/adapters/hip/platform.cpp +++ b/source/adapters/hip/platform.cpp @@ -135,12 +135,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( - ur_native_handle_t hNativePlatform, - const ur_platform_native_properties_t *pProperties, - ur_platform_handle_t *phPlatform) { - std::ignore = hNativePlatform; - std::ignore = pProperties; - std::ignore = phPlatform; + ur_native_handle_t, ur_adapter_handle_t, + const ur_platform_native_properties_t *, ur_platform_handle_t *) { + // There is no HIP equivalent to ur_platform_handle_t return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; } diff --git a/source/adapters/level_zero/platform.cpp b/source/adapters/level_zero/platform.cpp index d508d85c82..ca162bbad2 100644 --- a/source/adapters/level_zero/platform.cpp +++ b/source/adapters/level_zero/platform.cpp @@ -123,6 +123,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle( UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t NativePlatform, ///< [in] the native handle of the platform. + ur_adapter_handle_t, const ur_platform_native_properties_t *Properties, ///< [in][optional] pointer to native platform properties ///< struct. diff --git a/source/adapters/native_cpu/platform.cpp b/source/adapters/native_cpu/platform.cpp index 31f35d03b2..8d650764c1 100644 --- a/source/adapters/native_cpu/platform.cpp +++ b/source/adapters/native_cpu/platform.cpp @@ -96,13 +96,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetBackendOption( } UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( - ur_native_handle_t hNativePlatform, - const ur_platform_native_properties_t *pProperties, - ur_platform_handle_t *phPlatform) { - std::ignore = hNativePlatform; - std::ignore = pProperties; - std::ignore = phPlatform; - + ur_native_handle_t, ur_adapter_handle_t, + const ur_platform_native_properties_t *, ur_platform_handle_t *) { DIE_NO_IMPLEMENTATION; } diff --git a/source/adapters/null/ur_nullddi.cpp b/source/adapters/null/ur_nullddi.cpp index a713a385a7..19a9c9003b 100644 --- a/source/adapters/null/ur_nullddi.cpp +++ b/source/adapters/null/ur_nullddi.cpp @@ -263,6 +263,8 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetNativeHandle( __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t hNativePlatform, ///< [in][nocheck] the native handle of the platform. + ur_adapter_handle_t + hAdapter, ///< [in] handle of the adapter associated with the native backend. const ur_platform_native_properties_t * pProperties, ///< [in][optional] pointer to native platform properties struct. ur_platform_handle_t * @@ -274,8 +276,8 @@ __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( auto pfnCreateWithNativeHandle = d_context.urDdiTable.Platform.pfnCreateWithNativeHandle; if (nullptr != pfnCreateWithNativeHandle) { - result = - pfnCreateWithNativeHandle(hNativePlatform, pProperties, phPlatform); + result = pfnCreateWithNativeHandle(hNativePlatform, hAdapter, + pProperties, phPlatform); } else { // generic implementation *phPlatform = reinterpret_cast(d_context.get()); diff --git a/source/adapters/opencl/platform.cpp b/source/adapters/opencl/platform.cpp index 7188a3e8f0..9fa5025196 100644 --- a/source/adapters/opencl/platform.cpp +++ b/source/adapters/opencl/platform.cpp @@ -106,8 +106,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( - ur_native_handle_t hNativePlatform, const ur_platform_native_properties_t *, - ur_platform_handle_t *phPlatform) { + ur_native_handle_t hNativePlatform, ur_adapter_handle_t, + const ur_platform_native_properties_t *, ur_platform_handle_t *phPlatform) { *phPlatform = reinterpret_cast(hNativePlatform); return UR_RESULT_SUCCESS; } diff --git a/source/loader/layers/tracing/ur_trcddi.cpp b/source/loader/layers/tracing/ur_trcddi.cpp index da61c34992..e5cc8f81e2 100644 --- a/source/loader/layers/tracing/ur_trcddi.cpp +++ b/source/loader/layers/tracing/ur_trcddi.cpp @@ -352,6 +352,8 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetNativeHandle( __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t hNativePlatform, ///< [in][nocheck] the native handle of the platform. + ur_adapter_handle_t + hAdapter, ///< [in] handle of the adapter associated with the native backend. const ur_platform_native_properties_t * pProperties, ///< [in][optional] pointer to native platform properties struct. ur_platform_handle_t * @@ -365,15 +367,15 @@ __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( } ur_platform_create_with_native_handle_params_t params = { - &hNativePlatform, &pProperties, &phPlatform}; + &hNativePlatform, &hAdapter, &pProperties, &phPlatform}; uint64_t instance = context.notify_begin(UR_FUNCTION_PLATFORM_CREATE_WITH_NATIVE_HANDLE, "urPlatformCreateWithNativeHandle", ¶ms); context.logger.info("---> urPlatformCreateWithNativeHandle"); - ur_result_t result = - pfnCreateWithNativeHandle(hNativePlatform, pProperties, phPlatform); + ur_result_t result = pfnCreateWithNativeHandle(hNativePlatform, hAdapter, + pProperties, phPlatform); context.notify_end(UR_FUNCTION_PLATFORM_CREATE_WITH_NATIVE_HANDLE, "urPlatformCreateWithNativeHandle", ¶ms, &result, diff --git a/source/loader/layers/validation/ur_valddi.cpp b/source/loader/layers/validation/ur_valddi.cpp index 9998ae91bb..5ac97dfc1d 100644 --- a/source/loader/layers/validation/ur_valddi.cpp +++ b/source/loader/layers/validation/ur_valddi.cpp @@ -347,6 +347,8 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetNativeHandle( __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t hNativePlatform, ///< [in][nocheck] the native handle of the platform. + ur_adapter_handle_t + hAdapter, ///< [in] handle of the adapter associated with the native backend. const ur_platform_native_properties_t * pProperties, ///< [in][optional] pointer to native platform properties struct. ur_platform_handle_t * @@ -360,13 +362,22 @@ __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( } if (context.enableParameterValidation) { + if (NULL == hAdapter) { + return UR_RESULT_ERROR_INVALID_NULL_HANDLE; + } + if (NULL == phPlatform) { return UR_RESULT_ERROR_INVALID_NULL_POINTER; } } - ur_result_t result = - pfnCreateWithNativeHandle(hNativePlatform, pProperties, phPlatform); + if (context.enableLifetimeValidation && + !refCountContext.isReferenceValid(hAdapter)) { + refCountContext.logInvalidReference(hAdapter); + } + + ur_result_t result = pfnCreateWithNativeHandle(hNativePlatform, hAdapter, + pProperties, phPlatform); return result; } diff --git a/source/loader/ur_ldrddi.cpp b/source/loader/ur_ldrddi.cpp index fb392dd607..ed7e4299b1 100644 --- a/source/loader/ur_ldrddi.cpp +++ b/source/loader/ur_ldrddi.cpp @@ -22,7 +22,6 @@ ur_event_factory_t ur_event_factory; ur_program_factory_t ur_program_factory; ur_kernel_factory_t ur_kernel_factory; ur_queue_factory_t ur_queue_factory; -ur_native_factory_t ur_native_factory; ur_sampler_factory_t ur_sampler_factory; ur_mem_factory_t ur_mem_factory; ur_physical_mem_factory_t ur_physical_mem_factory; @@ -364,6 +363,8 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetNativeHandle( __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t hNativePlatform, ///< [in][nocheck] the native handle of the platform. + ur_adapter_handle_t + hAdapter, ///< [in] handle of the adapter associated with the native backend. const ur_platform_native_properties_t * pProperties, ///< [in][optional] pointer to native platform properties struct. ur_platform_handle_t * @@ -372,8 +373,7 @@ __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_result_t result = UR_RESULT_SUCCESS; // extract platform's function pointer table - auto dditable = - reinterpret_cast(hNativePlatform)->dditable; + auto dditable = reinterpret_cast(hAdapter)->dditable; auto pfnCreateWithNativeHandle = dditable->ur.Platform.pfnCreateWithNativeHandle; if (nullptr == pfnCreateWithNativeHandle) { @@ -381,12 +381,11 @@ __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( } // convert loader handle to platform handle - hNativePlatform = - reinterpret_cast(hNativePlatform)->handle; + hAdapter = reinterpret_cast(hAdapter)->handle; // forward to device-platform - result = - pfnCreateWithNativeHandle(hNativePlatform, pProperties, phPlatform); + result = pfnCreateWithNativeHandle(hNativePlatform, hAdapter, pProperties, + phPlatform); if (UR_RESULT_SUCCESS != result) { return result; diff --git a/source/loader/ur_ldrddi.hpp b/source/loader/ur_ldrddi.hpp index d98b99a655..af5b5d49b5 100644 --- a/source/loader/ur_ldrddi.hpp +++ b/source/loader/ur_ldrddi.hpp @@ -49,10 +49,6 @@ using ur_queue_object_t = object_t; using ur_queue_factory_t = singleton_factory_t; -using ur_native_object_t = object_t; -using ur_native_factory_t = - singleton_factory_t; - using ur_sampler_object_t = object_t; using ur_sampler_factory_t = singleton_factory_t; diff --git a/source/loader/ur_libapi.cpp b/source/loader/ur_libapi.cpp index 668fbd07ad..7e63d4a3cf 100644 --- a/source/loader/ur_libapi.cpp +++ b/source/loader/ur_libapi.cpp @@ -653,6 +653,8 @@ ur_result_t UR_APICALL urPlatformGetNativeHandle( /// - ::UR_RESULT_ERROR_UNINITIALIZED /// - ::UR_RESULT_ERROR_DEVICE_LOST /// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hAdapter` /// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER /// + `NULL == phPlatform` /// - ::UR_RESULT_ERROR_UNSUPPORTED_FEATURE @@ -660,6 +662,8 @@ ur_result_t UR_APICALL urPlatformGetNativeHandle( ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t hNativePlatform, ///< [in][nocheck] the native handle of the platform. + ur_adapter_handle_t + hAdapter, ///< [in] handle of the adapter associated with the native backend. const ur_platform_native_properties_t * pProperties, ///< [in][optional] pointer to native platform properties struct. ur_platform_handle_t * @@ -671,7 +675,8 @@ ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( return UR_RESULT_ERROR_UNINITIALIZED; } - return pfnCreateWithNativeHandle(hNativePlatform, pProperties, phPlatform); + return pfnCreateWithNativeHandle(hNativePlatform, hAdapter, pProperties, + phPlatform); } catch (...) { return exceptionToResult(std::current_exception()); } diff --git a/source/ur_api.cpp b/source/ur_api.cpp index f7b4bb017f..e4da9739e0 100644 --- a/source/ur_api.cpp +++ b/source/ur_api.cpp @@ -569,6 +569,8 @@ ur_result_t UR_APICALL urPlatformGetNativeHandle( /// - ::UR_RESULT_ERROR_UNINITIALIZED /// - ::UR_RESULT_ERROR_DEVICE_LOST /// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hAdapter` /// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER /// + `NULL == phPlatform` /// - ::UR_RESULT_ERROR_UNSUPPORTED_FEATURE @@ -576,6 +578,8 @@ ur_result_t UR_APICALL urPlatformGetNativeHandle( ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_native_handle_t hNativePlatform, ///< [in][nocheck] the native handle of the platform. + ur_adapter_handle_t + hAdapter, ///< [in] handle of the adapter associated with the native backend. const ur_platform_native_properties_t * pProperties, ///< [in][optional] pointer to native platform properties struct. ur_platform_handle_t * diff --git a/test/conformance/platform/urPlatformCreateWithNativeHandle.cpp b/test/conformance/platform/urPlatformCreateWithNativeHandle.cpp index 435ac23a00..4f6684c432 100644 --- a/test/conformance/platform/urPlatformCreateWithNativeHandle.cpp +++ b/test/conformance/platform/urPlatformCreateWithNativeHandle.cpp @@ -20,8 +20,8 @@ TEST_F(urPlatformCreateWithNativeHandleTest, Success) { // We can however convert the native_handle back into a unified-runtime // handle and perform some query on it to verify that it works. ur_platform_handle_t plat = nullptr; - UUR_ASSERT_SUCCESS_OR_UNSUPPORTED( - urPlatformCreateWithNativeHandle(native_handle, nullptr, &plat)); + UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(urPlatformCreateWithNativeHandle( + native_handle, adapters[0], nullptr, &plat)); ASSERT_NE(plat, nullptr); std::string input_platform_name = uur::GetPlatformName(platform); @@ -45,8 +45,8 @@ TEST_F(urPlatformCreateWithNativeHandleTest, SuccessWithOwnedNativeHandle) { ur_platform_native_properties_t props = { UR_STRUCTURE_TYPE_PLATFORM_NATIVE_PROPERTIES, nullptr, true}; ur_platform_handle_t plat = nullptr; - UUR_ASSERT_SUCCESS_OR_UNSUPPORTED( - urPlatformCreateWithNativeHandle(native_handle, &props, &plat)); + UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(urPlatformCreateWithNativeHandle( + native_handle, adapters[0], &props, &plat)); ASSERT_NE(plat, nullptr); std::string input_platform_name = uur::GetPlatformName(platform); @@ -70,8 +70,8 @@ TEST_F(urPlatformCreateWithNativeHandleTest, SuccessWithUnOwnedNativeHandle) { ur_platform_native_properties_t props = { UR_STRUCTURE_TYPE_PLATFORM_NATIVE_PROPERTIES, nullptr, false}; ur_platform_handle_t plat = nullptr; - UUR_ASSERT_SUCCESS_OR_UNSUPPORTED( - urPlatformCreateWithNativeHandle(native_handle, &props, &plat)); + UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(urPlatformCreateWithNativeHandle( + native_handle, adapters[0], &props, &plat)); ASSERT_NE(plat, nullptr); std::string input_platform_name = uur::GetPlatformName(platform); @@ -84,8 +84,8 @@ TEST_F(urPlatformCreateWithNativeHandleTest, InvalidNullPointerPlatform) { for (auto platform : platforms) { ur_native_handle_t native_handle = nullptr; ASSERT_SUCCESS(urPlatformGetNativeHandle(platform, &native_handle)); - ASSERT_EQ_RESULT( - UR_RESULT_ERROR_INVALID_NULL_POINTER, - urPlatformCreateWithNativeHandle(native_handle, nullptr, nullptr)); + ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_NULL_POINTER, + urPlatformCreateWithNativeHandle( + native_handle, adapters[0], nullptr, nullptr)); } }