From 089e02cdf70bdf52cd3a7df8caa4ce58a8a1d869 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Tue, 14 Oct 2025 10:07:32 -0700 Subject: [PATCH] add system SVM testing via clSVMAllocWithProperties (#2516) This PR adds system SVM testing both using driver APIs (`clSVMAllocWithPropertiesKHR`) and the system allocator directly (e.g. `malloc`). This is done by finding all of the SVM capabilities that are "system allocated" and duplicating them with a special "use system allocator" pseudo-capability. When the "use system allocator" pseudo-capability is not present, the system SVM type is treated the same as all other unified SVM types and is tested using driver APIs. When the "use system allocator" pseudo-capability is present, the system SVM type is allocated using the system allocator directly, though this also adds some limitations, for example the properties of the allocation may not be queried using `clGetSVMPointerInfoKHR`. See discussion in: https://github.com/KhronosGroup/OpenCL-Docs/issues/1446 --- ...t_unified_svm_api_suggested_type_index.cpp | 7 +++ .../SVM/test_unified_svm_apis.cpp | 8 ++-- .../SVM/test_unified_svm_capabilities.cpp | 2 +- .../SVM/test_unified_svm_execinfo.cpp | 18 ++------ .../SVM/test_unified_svm_free.cpp | 6 +-- .../SVM/test_unified_svm_mem_cpy.cpp | 9 ++-- test_conformance/SVM/unified_svm_fixture.h | 43 ++++++++++++++++++- 7 files changed, 64 insertions(+), 29 deletions(-) diff --git a/test_conformance/SVM/test_unified_svm_api_suggested_type_index.cpp b/test_conformance/SVM/test_unified_svm_api_suggested_type_index.cpp index a427d9af..47489a39 100644 --- a/test_conformance/SVM/test_unified_svm_api_suggested_type_index.cpp +++ b/test_conformance/SVM/test_unified_svm_api_suggested_type_index.cpp @@ -37,6 +37,13 @@ struct UnifiedSVMAPISuggestedTypeIndex : UnifiedSVMBase cl_svm_capabilities_khr allSupportedDeviceUSVMCaps = 0; for (const auto caps : deviceUSVMCaps) { + if (caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) + { + // The system allocator pseudo-capability is not a real + // capability, so skip it. + continue; + } + err = checkSuggestedTypeIndex(caps, size); test_error(err, "suggested type index failed"); diff --git a/test_conformance/SVM/test_unified_svm_apis.cpp b/test_conformance/SVM/test_unified_svm_apis.cpp index 30152c17..da185bd9 100644 --- a/test_conformance/SVM/test_unified_svm_apis.cpp +++ b/test_conformance/SVM/test_unified_svm_apis.cpp @@ -57,7 +57,7 @@ struct UnifiedSVMAPIs : UnifiedSVMBase // that support CL_SVM_CAPABILITY_DEVICE_UNASSOCIATED_KHR, as long as // this is not a system allocated type. if (caps & CL_SVM_CAPABILITY_DEVICE_UNASSOCIATED_KHR - && !(caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR)) + && !(caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR)) { std::vector props; props.push_back(CL_SVM_ALLOC_ASSOCIATED_DEVICE_HANDLE_KHR); @@ -83,7 +83,7 @@ struct UnifiedSVMAPIs : UnifiedSVMBase // this is not a system allocated type. // !!! Check: Is this a valid test? if (caps & CL_SVM_CAPABILITY_DEVICE_UNASSOCIATED_KHR - && !(caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR)) + && !(caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR)) { std::vector props; props.push_back(CL_SVM_ALLOC_ASSOCIATED_DEVICE_HANDLE_KHR); @@ -108,7 +108,7 @@ struct UnifiedSVMAPIs : UnifiedSVMBase // CL_SVM_CAPABILITY_DEVICE_UNASSOCIATED_KHR, as long as this is not a // system allocated type. if (caps & CL_SVM_CAPABILITY_DEVICE_UNASSOCIATED_KHR - && !(caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR)) + && !(caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR)) { std::vector props; props.push_back(0); @@ -237,7 +237,7 @@ struct UnifiedSVMAPIs : UnifiedSVMBase cl_int err; // We cannot test queries for system allocated memory. - if (caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + if (caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) { return CL_SUCCESS; } diff --git a/test_conformance/SVM/test_unified_svm_capabilities.cpp b/test_conformance/SVM/test_unified_svm_capabilities.cpp index 9f1640ba..31a358d4 100644 --- a/test_conformance/SVM/test_unified_svm_capabilities.cpp +++ b/test_conformance/SVM/test_unified_svm_capabilities.cpp @@ -70,7 +70,7 @@ struct UnifiedSVMCapabilities : UnifiedSVMBase cl_int test_CL_SVM_CAPABILITY_DEVICE_UNASSOCIATED_KHR(cl_uint typeIndex) { const auto caps = deviceUSVMCaps[typeIndex]; - if (caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + if (caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) { return CL_SUCCESS; } diff --git a/test_conformance/SVM/test_unified_svm_execinfo.cpp b/test_conformance/SVM/test_unified_svm_execinfo.cpp index c5eaa39a..f3e32475 100644 --- a/test_conformance/SVM/test_unified_svm_execinfo.cpp +++ b/test_conformance/SVM/test_unified_svm_execinfo.cpp @@ -189,10 +189,7 @@ struct UnifiedSVMExecInfo : UnifiedSVMBase cl_int setup() override { cl_int err = UnifiedSVMBase::setup(); - if (CL_SUCCESS != err) - { - return err; - } + test_error(err, "UnifiedSVMBase setup failed"); return createIndirectAccessKernel(); } @@ -209,19 +206,12 @@ struct UnifiedSVMExecInfo : UnifiedSVMBase err = mem->allocate(alloc_count); test_error(err, "SVM allocation failed"); - log_info(" testing clSetKernelArgSVMPointer() SVM type %u \n", - ti); + log_info(" testing clSetKernelExecInfo() SVM type %u \n", ti); err = test_svm_exec_info_read(mem.get()); - if (CL_SUCCESS != err) - { - return err; - } + test_error(err, "test_svm_exec_info_read failed"); err = test_svm_exec_info_write(mem.get()); - if (CL_SUCCESS != err) - { - return err; - } + test_error(err, "test_svm_exec_info_write failed"); err = mem->free(); test_error(err, "SVM free failed"); diff --git a/test_conformance/SVM/test_unified_svm_free.cpp b/test_conformance/SVM/test_unified_svm_free.cpp index 063128a1..1fbf2087 100644 --- a/test_conformance/SVM/test_unified_svm_free.cpp +++ b/test_conformance/SVM/test_unified_svm_free.cpp @@ -49,7 +49,7 @@ void CL_CALLBACK callback_svm_free(cl_command_queue queue, { data->svm_pointers[i] = svm_pointers[i]; - if (data->svm_caps[i] & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + if (data->svm_caps[i] & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) { align_free(data); } @@ -169,13 +169,13 @@ struct UnifiedSVMFree : UnifiedSVMBase test_error(err, "test_SVMFree"); } - // We need to filter out the SVM types that support system allocation + // We need to filter out the SVM types that are system allocated // as we cannot test clEnqueueSVMFree without a callback for them std::vector test_indexes; for (size_t i = 0; i < deviceUSVMCaps.size(); i++) { auto caps = deviceUSVMCaps[i]; - if (0 == (caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR)) + if (0 == (caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR)) { test_indexes.push_back(i); } diff --git a/test_conformance/SVM/test_unified_svm_mem_cpy.cpp b/test_conformance/SVM/test_unified_svm_mem_cpy.cpp index c0c9cb26..6883d745 100644 --- a/test_conformance/SVM/test_unified_svm_mem_cpy.cpp +++ b/test_conformance/SVM/test_unified_svm_mem_cpy.cpp @@ -135,7 +135,7 @@ struct UnifiedSVMOPs : UnifiedSVMBase // We check if the memory can be read by the host. if (caps & CL_SVM_CAPABILITY_HOST_READ_KHR - || caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + || caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) { err = test_SVMMemcpy(mem.get(), hostMem.get()); test_error(err, "test_SVMMemcpy"); @@ -143,7 +143,7 @@ struct UnifiedSVMOPs : UnifiedSVMBase // We check if the memory can be written by the host. if (caps & CL_SVM_CAPABILITY_HOST_WRITE_KHR - || caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + || caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) { err = test_SVMMemcpy(hostMem.get(), mem.get()); test_error(err, "test_SVMMemcpy"); @@ -162,7 +162,7 @@ struct UnifiedSVMOPs : UnifiedSVMBase cl_int err; cl_uint max_ti = static_cast(deviceUSVMCaps.size()); - // Test all possible comabinations between supported types + // Test all possible combinations between supported types for (cl_uint src_ti = 0; src_ti < max_ti; src_ti++) { for (cl_uint dst_ti = 0; dst_ti < max_ti; dst_ti++) @@ -208,7 +208,7 @@ struct UnifiedSVMOPs : UnifiedSVMBase { return std::unique_ptr>( new USVMWrapper(nullptr, nullptr, nullptr, CL_UINT_MAX, - CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR + PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR | CL_SVM_CAPABILITY_HOST_READ_KHR | CL_SVM_CAPABILITY_HOST_WRITE_KHR, 0, nullptr, nullptr, nullptr, nullptr)); @@ -216,7 +216,6 @@ struct UnifiedSVMOPs : UnifiedSVMBase bool caps_compatibility_check(cl_uint srcTypeIndex, cl_uint dstTypeIndex) { - const auto srcCaps = deviceUSVMCaps[srcTypeIndex]; const auto dstCaps = deviceUSVMCaps[dstTypeIndex]; diff --git a/test_conformance/SVM/unified_svm_fixture.h b/test_conformance/SVM/unified_svm_fixture.h index a5b72330..5a62ecf1 100644 --- a/test_conformance/SVM/unified_svm_fixture.h +++ b/test_conformance/SVM/unified_svm_fixture.h @@ -19,6 +19,9 @@ #include #include +constexpr cl_bitfield PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR = + ((cl_bitfield)1 << 63); + static inline void parseSVMAllocProperties( std::vector props, cl_device_id& device, cl_svm_alloc_access_flags_khr& accessFlags, size_t& alignment) @@ -82,7 +85,7 @@ public: free(); } - if (caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + if (caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) { if (count == 0) { @@ -135,7 +138,7 @@ public: { if (data) { - if (caps & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + if (caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) { align_free(data); } @@ -317,6 +320,42 @@ struct UnifiedSVMBase err, "clGetDeviceInfo failed for CL_DEVICE_SVM_CAPABILITIES_KHR data"); + test_assert_error(platformUSVMCaps.size() == deviceUSVMCaps.size(), + "Platform SVM capability size does not match device " + "SVM capability size!"); + + const size_t check = platformUSVMCaps.size(); + + // Look through the platform and device SVM capabilities. + // If our pseudo capability to indicate that the system allocator should + // be used is reported as a real capability, then we can't continue + // testing. + for (size_t ti = 0; ti < check; ti++) + { + auto caps = deviceUSVMCaps[ti]; + if (caps & PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR) + { + log_info("Unable to continue, device reports system allocator " + "pseudo-capability as an SVM capability.\n"); + return CL_INVALID_OPERATION; + } + } + + // For any capabilities that support the system allocator, add a + // scenario testing with the system allocator vs. allocating via OpenCL. + for (size_t ti = 0; ti < check; ti++) + { + if (deviceUSVMCaps[ti] & CL_SVM_CAPABILITY_SYSTEM_ALLOCATED_KHR) + { + deviceUSVMCaps.push_back( + deviceUSVMCaps[ti] + | PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR); + platformUSVMCaps.push_back( + platformUSVMCaps[ti] + | PSEUDO_CAPABILITY_USE_SYSTEM_ALLOCATOR); + } + } + clSVMAllocWithPropertiesKHR = (clSVMAllocWithPropertiesKHR_fn) clGetExtensionFunctionAddressForPlatform( platform, "clSVMAllocWithPropertiesKHR");