From 9989d97bceeabd1e83949ff3c86ba34af94907d3 Mon Sep 17 00:00:00 2001 From: Ross Brunton Date: Mon, 21 Oct 2024 16:35:45 +0100 Subject: [PATCH] Handle INVALID_VALUE in OCL urPlatformGet The OpenCL function clGetPlatformIDs may return CL_INVALID_VALUE, which needs to be converted into appropriate UR returns as follows: * If a non-null platforms list is provided, but the number of elements in it is 0, then return UR_RESULT_ERROR_INVALID_SIZE. * If neither platform or sizes outputs are provided, then return UR_RESULT_ERROR_INVALID_VALUE. This required a spec change. This fixes a bug Intel are tracking internally as URT-831. --- include/ur_api.h | 2 ++ scripts/core/platform.yml | 2 ++ source/adapters/opencl/platform.cpp | 5 +++++ source/loader/layers/validation/ur_valddi.cpp | 4 ++++ source/loader/ur_libapi.cpp | 2 ++ source/ur_api.cpp | 2 ++ test/conformance/platform/urPlatformGet.cpp | 7 +++++++ 7 files changed, 24 insertions(+) diff --git a/include/ur_api.h b/include/ur_api.h index 8731d78c00..8591b20daa 100644 --- a/include/ur_api.h +++ b/include/ur_api.h @@ -1054,6 +1054,8 @@ typedef enum ur_adapter_backend_t { /// + `NULL == phAdapters` /// - ::UR_RESULT_ERROR_INVALID_SIZE /// + `NumEntries == 0 && phPlatforms != NULL` +/// - ::UR_RESULT_ERROR_INVALID_VALUE +/// + `pNumPlatforms == NULL && phPlatforms == NULL` UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet( ur_adapter_handle_t *phAdapters, ///< [in][range(0, NumAdapters)] array of adapters to query for platforms. diff --git a/scripts/core/platform.yml b/scripts/core/platform.yml index a1aa0dc7ca..997f4918ee 100644 --- a/scripts/core/platform.yml +++ b/scripts/core/platform.yml @@ -48,6 +48,8 @@ params: returns: - $X_RESULT_ERROR_INVALID_SIZE: - "`NumEntries == 0 && phPlatforms != NULL`" + - $X_RESULT_ERROR_INVALID_VALUE: + - "`pNumPlatforms == NULL && phPlatforms == NULL`" --- #-------------------------------------------------------------------------- type: enum desc: "Supported platform info" diff --git a/source/adapters/opencl/platform.cpp b/source/adapters/opencl/platform.cpp index 9fa5025196..218a5e7f00 100644 --- a/source/adapters/opencl/platform.cpp +++ b/source/adapters/opencl/platform.cpp @@ -96,6 +96,11 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries, } } + /* INVALID_VALUE is returned when the size is invalid, special case it here */ + if (Result == CL_INVALID_VALUE && phPlatforms != nullptr && NumEntries == 0) { + return UR_RESULT_ERROR_INVALID_SIZE; + } + return mapCLErrorToUR(Result); } diff --git a/source/loader/layers/validation/ur_valddi.cpp b/source/loader/layers/validation/ur_valddi.cpp index ddf40de35f..5430eb3c28 100644 --- a/source/loader/layers/validation/ur_valddi.cpp +++ b/source/loader/layers/validation/ur_valddi.cpp @@ -234,6 +234,10 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGet( if (NumEntries == 0 && phPlatforms != NULL) { return UR_RESULT_ERROR_INVALID_SIZE; } + + if (pNumPlatforms == NULL && phPlatforms == NULL) { + return UR_RESULT_ERROR_INVALID_VALUE; + } } ur_result_t result = diff --git a/source/loader/ur_libapi.cpp b/source/loader/ur_libapi.cpp index a77c5916b1..3562732d54 100644 --- a/source/loader/ur_libapi.cpp +++ b/source/loader/ur_libapi.cpp @@ -509,6 +509,8 @@ ur_result_t UR_APICALL urAdapterGetInfo( /// + `NULL == phAdapters` /// - ::UR_RESULT_ERROR_INVALID_SIZE /// + `NumEntries == 0 && phPlatforms != NULL` +/// - ::UR_RESULT_ERROR_INVALID_VALUE +/// + `pNumPlatforms == NULL && phPlatforms == NULL` ur_result_t UR_APICALL urPlatformGet( ur_adapter_handle_t * phAdapters, ///< [in][range(0, NumAdapters)] array of adapters to query for platforms. diff --git a/source/ur_api.cpp b/source/ur_api.cpp index e375d496f8..30a53270bb 100644 --- a/source/ur_api.cpp +++ b/source/ur_api.cpp @@ -460,6 +460,8 @@ ur_result_t UR_APICALL urAdapterGetInfo( /// + `NULL == phAdapters` /// - ::UR_RESULT_ERROR_INVALID_SIZE /// + `NumEntries == 0 && phPlatforms != NULL` +/// - ::UR_RESULT_ERROR_INVALID_VALUE +/// + `pNumPlatforms == NULL && phPlatforms == NULL` ur_result_t UR_APICALL urPlatformGet( ur_adapter_handle_t * phAdapters, ///< [in][range(0, NumAdapters)] array of adapters to query for platforms. diff --git a/test/conformance/platform/urPlatformGet.cpp b/test/conformance/platform/urPlatformGet.cpp index f3ac6318e9..20f12c16df 100644 --- a/test/conformance/platform/urPlatformGet.cpp +++ b/test/conformance/platform/urPlatformGet.cpp @@ -41,3 +41,10 @@ TEST_F(urPlatformGetTest, InvalidNullPointer) { static_cast(adapters.size()), 0, nullptr, &count)); } + +TEST_F(urPlatformGetTest, NullArgs) { + ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_VALUE, + urPlatformGet(adapters.data(), + static_cast(adapters.size()), 0, + nullptr, nullptr)); +}