From 8f21274bf9efac184d2a34c9a0968ed6cc8448c1 Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Tue, 9 Apr 2024 15:30:34 +0200 Subject: [PATCH 1/5] Added missing interface for CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR property --- layers/11_semaemu/emulate.cpp | 58 +++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index 8791398..f718457 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -48,6 +48,8 @@ typedef struct _cl_semaphore_khr ptrdiff_t numProperties = 0; cl_semaphore_type_khr type = ~0; + std::vector devices; + if( properties ) { const cl_semaphore_properties_khr* check = properties; @@ -78,10 +80,11 @@ typedef struct _cl_semaphore_khr else { found_CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR = true; - ++check; - while(*check++ != CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR) + check++; + while(*check != CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR) { - // TODO: validate device handles. + devices.push_back(((cl_device_id*)check)[0]); + check++; } } break; @@ -91,6 +94,38 @@ typedef struct _cl_semaphore_khr } } numProperties = check - properties + 1; + + // validate device handles. + if (!devices.empty()) { + std::vector types; + for (int i = devices.size() - 1; i >= 0; i--) { + size_t num_types_size = 0; + clGetDeviceInfo(devices[i], + CL_DEVICE_SEMAPHORE_TYPES_KHR, + 0, nullptr, &num_types_size); + + if (!num_types_size) + continue; + + types.resize(num_types_size / sizeof(cl_semaphore_type_khr)); + clGetDeviceInfo( + devices[i], CL_DEVICE_SEMAPHORE_TYPES_KHR, num_types_size, + types.data(), nullptr); + + bool capable = false; + for (auto sema_type : types) { + if (type == sema_type) { + capable = true; + break; + } + } + + if (!capable) { + errorCode = CL_INVALID_DEVICE; + break; + } + } + } } switch( type ) { @@ -111,6 +146,12 @@ typedef struct _cl_semaphore_khr semaphore->Properties.begin(), properties, properties + numProperties ); + + if (!devices.empty()) + semaphore->Devices.assign( + devices.begin(), + devices.end() ); + } return semaphore; } @@ -124,6 +165,7 @@ typedef struct _cl_semaphore_khr const cl_context Context; const cl_semaphore_type_khr Type; std::vector Properties; + std::vector Devices; std::atomic RefCount; cl_event Event; @@ -349,6 +391,16 @@ cl_int CL_API_CALL clGetSemaphoreInfoKHR_EMU( ptr ); } break; + case CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR: + { + auto ptr = (cl_device_id*)param_value; + return writeVectorToMemory( + param_value_size, + semaphore->Devices, + param_value_size_ret, + ptr ); + } + break; default: return CL_INVALID_VALUE; } From 7f8e6141d47f41d704a65b9f56c5beaffd851627 Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Mon, 6 May 2024 13:54:30 +0200 Subject: [PATCH 2/5] approach to correct CI checks errors around semaphore emulation layer to handle CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR flag --- layers/11_semaemu/emulate.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index f718457..cec6865 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -100,17 +100,17 @@ typedef struct _cl_semaphore_khr std::vector types; for (int i = devices.size() - 1; i >= 0; i--) { size_t num_types_size = 0; - clGetDeviceInfo(devices[i], - CL_DEVICE_SEMAPHORE_TYPES_KHR, - 0, nullptr, &num_types_size); + clGetDeviceInfo_override(devices[i], + CL_DEVICE_SEMAPHORE_TYPES_KHR, 0, + nullptr, &num_types_size, &errorCode); if (!num_types_size) continue; types.resize(num_types_size / sizeof(cl_semaphore_type_khr)); - clGetDeviceInfo( + clGetDeviceInfo_override( devices[i], CL_DEVICE_SEMAPHORE_TYPES_KHR, num_types_size, - types.data(), nullptr); + types.data(), nullptr, &errorCode); bool capable = false; for (auto sema_type : types) { From b57878e5cc8df8dc29b830a0ebe2610b963e1d16 Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Mon, 6 May 2024 15:11:55 +0200 Subject: [PATCH 3/5] Corrections related to negative CTS tests of clCreateSemaphoreWithPropertiesKHR --- layers/11_semaemu/emulate.cpp | 36 ++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index cec6865..6320b0b 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -50,7 +50,13 @@ typedef struct _cl_semaphore_khr std::vector devices; - if( properties ) + if ( properties == nullptr ) + errorCode = CL_INVALID_VALUE; + + if ( context == nullptr ) + errorCode = CL_INVALID_CONTEXT; + + if( errorCode == CL_SUCCESS && properties ) { const cl_semaphore_properties_khr* check = properties; bool found_CL_SEMAPHORE_TYPE_KHR = false; @@ -63,7 +69,7 @@ typedef struct _cl_semaphore_khr case CL_SEMAPHORE_TYPE_KHR: if( found_CL_SEMAPHORE_TYPE_KHR ) { - errorCode = CL_INVALID_VALUE; + errorCode = CL_INVALID_PROPERTY; } else { @@ -75,7 +81,7 @@ typedef struct _cl_semaphore_khr case CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR: if( found_CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR ) { - errorCode = CL_INVALID_VALUE; + errorCode = CL_INVALID_PROPERTY; } else { @@ -89,14 +95,17 @@ typedef struct _cl_semaphore_khr } break; default: - errorCode = CL_INVALID_VALUE; + errorCode = CL_INVALID_PROPERTY; break; } } numProperties = check - properties + 1; + if ( errorCode == CL_SUCCESS && type == ~0) + errorCode = CL_INVALID_VALUE; + // validate device handles. - if (!devices.empty()) { + if (errorCode == CL_SUCCESS && !devices.empty()) { std::vector types; for (int i = devices.size() - 1; i >= 0; i--) { size_t num_types_size = 0; @@ -127,16 +136,17 @@ typedef struct _cl_semaphore_khr } } } - switch( type ) - { - case CL_SEMAPHORE_TYPE_BINARY_KHR: + + if (errorCode == CL_SUCCESS) { + switch (type) { + case CL_SEMAPHORE_TYPE_BINARY_KHR: break; - default: - errorCode = CL_INVALID_VALUE; + default: + errorCode = CL_INVALID_PROPERTY; + } } - if( errcode_ret ) - { - errcode_ret[0] = errorCode; + if (errcode_ret) { + errcode_ret[0] = errorCode; } if( errorCode == CL_SUCCESS ) { From e107dc6a25b2f41a865f751a93e8242552e499ac Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Tue, 21 May 2024 10:53:37 +0200 Subject: [PATCH 4/5] corrections related to review #113 --- layers/11_semaemu/emulate.cpp | 67 +++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index 6320b0b..c07d899 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -35,6 +35,28 @@ SLayerContext& getLayerContext(void) return c; } +static bool isDeviceWithinContext(const cl_context context, + const cl_device_id device) { + cl_uint numDevices = 0; + cl_int error = g_pNextDispatch->clGetContextInfo( + context, CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint), &numDevices, NULL); + if (error != CL_SUCCESS || numDevices == 0) + return false; + + std::vector devices(numDevices, 0); + error = g_pNextDispatch->clGetContextInfo(context, CL_CONTEXT_DEVICES, + numDevices * sizeof(cl_device_id), + devices.data(), NULL); + if (error != CL_SUCCESS) + return false; + + for (auto dev : devices) + if (dev == device) + return true; + + return false; +} + typedef struct _cl_semaphore_khr { static _cl_semaphore_khr* create( @@ -105,34 +127,23 @@ typedef struct _cl_semaphore_khr errorCode = CL_INVALID_VALUE; // validate device handles. - if (errorCode == CL_SUCCESS && !devices.empty()) { - std::vector types; - for (int i = devices.size() - 1; i >= 0; i--) { - size_t num_types_size = 0; - clGetDeviceInfo_override(devices[i], - CL_DEVICE_SEMAPHORE_TYPES_KHR, 0, - nullptr, &num_types_size, &errorCode); - - if (!num_types_size) - continue; - - types.resize(num_types_size / sizeof(cl_semaphore_type_khr)); - clGetDeviceInfo_override( - devices[i], CL_DEVICE_SEMAPHORE_TYPES_KHR, num_types_size, - types.data(), nullptr, &errorCode); - - bool capable = false; - for (auto sema_type : types) { - if (type == sema_type) { - capable = true; + if (!devices.empty()) { + // for now - if CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR is specified + // as part of sema_props, but it does not identify exactly one + // valid device + if (devices.size() > 1) { + errorCode = CL_INVALID_DEVICE; + } else { + // if a device identified by CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR + // is not one of the devices within context + std::vector types; + for (auto device : devices) { + if (device == nullptr || + !isDeviceWithinContext(context, device)) { + errorCode = CL_INVALID_DEVICE; break; } } - - if (!capable) { - errorCode = CL_INVALID_DEVICE; - break; - } } } } @@ -157,11 +168,7 @@ typedef struct _cl_semaphore_khr properties, properties + numProperties ); - if (!devices.empty()) - semaphore->Devices.assign( - devices.begin(), - devices.end() ); - + semaphore->Devices=devices; } return semaphore; } From 214ccf11c58452343198d32ec7b6fb08d28ffdfb Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Mon, 3 Jun 2024 09:10:46 +0200 Subject: [PATCH 5/5] Take into account additional property field to distinguish between CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR and array terminator --- layers/11_semaemu/emulate.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index c07d899..3e40b1a 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -114,6 +114,7 @@ typedef struct _cl_semaphore_khr devices.push_back(((cl_device_id*)check)[0]); check++; } + check++; } break; default: