Unified cl_khr_external_semaphore tests using the same export and import scheme (#2591)

Due to discussion from #2542 and following work from closed PR #2568

This change refactors the external semaphore tests by unifying
`external_semaphores_import_export_fd` with
`external_semaphores_cross_context` tests, removing duplicated logic and
avoiding OS-specific conditions. The updated test now covers all
import/export handle types consistently across single- and multi-context
scenarios.
This commit is contained in:
Marcin Hajder
2025-12-09 19:10:56 +01:00
committed by GitHub
parent afb6f6519c
commit 67fbbe4ee2
3 changed files with 76 additions and 160 deletions

View File

@@ -3,7 +3,6 @@ set(MODULE_NAME CL_KHR_EXTERNAL_SEMAPHORE)
set(${MODULE_NAME}_SOURCES set(${MODULE_NAME}_SOURCES
main.cpp main.cpp
test_external_semaphore.cpp test_external_semaphore.cpp
test_external_semaphore_sync_fd.cpp
) )
set (CLConform_VULKAN_LIBRARIES_DIR "${VULKAN_LIB_DIR}") set (CLConform_VULKAN_LIBRARIES_DIR "${VULKAN_LIB_DIR}")

View File

@@ -197,8 +197,11 @@ REGISTER_TEST_VERSION(external_semaphores_queries, Version(1, 2))
return TEST_PASS; return TEST_PASS;
} }
REGISTER_TEST_VERSION(external_semaphores_cross_context, Version(1, 2)) cl_int doTestImportExport(cl_device_id device, cl_context contexts[2],
cl_command_queue queues[2])
{ {
cl_int err = CL_SUCCESS;
REQUIRE_EXTENSION("cl_khr_external_semaphore"); REQUIRE_EXTENSION("cl_khr_external_semaphore");
GET_PFN(device, clEnqueueSignalSemaphoresKHR); GET_PFN(device, clEnqueueSignalSemaphoresKHR);
@@ -210,7 +213,6 @@ REGISTER_TEST_VERSION(external_semaphores_cross_context, Version(1, 2))
std::vector<cl_external_semaphore_handle_type_khr> import_handle_types; std::vector<cl_external_semaphore_handle_type_khr> import_handle_types;
std::vector<cl_external_semaphore_handle_type_khr> export_handle_types; std::vector<cl_external_semaphore_handle_type_khr> export_handle_types;
cl_int err = CL_SUCCESS;
err = get_device_semaphore_handle_types( err = get_device_semaphore_handle_types(
device, CL_DEVICE_SEMAPHORE_IMPORT_HANDLE_TYPES_KHR, device, CL_DEVICE_SEMAPHORE_IMPORT_HANDLE_TYPES_KHR,
import_handle_types); import_handle_types);
@@ -237,17 +239,9 @@ REGISTER_TEST_VERSION(external_semaphores_cross_context, Version(1, 2))
export_handle_types.begin(), export_handle_types.end(), export_handle_types.begin(), export_handle_types.end(),
std::back_inserter(import_export_handle_types)); std::back_inserter(import_export_handle_types));
cl_context context2 = cl_context& context2 = contexts[1];
clCreateContext(NULL, 1, &device, notify_callback, NULL, &err); cl_command_queue& queue1 = queues[0];
test_error(err, "Failed to create context2"); cl_command_queue& queue2 = queues[1];
clCommandQueueWrapper queue1 =
clCreateCommandQueue(context, device, 0, &err);
test_error(err, "Could not create command queue");
clCommandQueueWrapper queue2 =
clCreateCommandQueue(context2, device, 0, &err);
test_error(err, "Could not create command queue");
if (import_export_handle_types.empty()) if (import_export_handle_types.empty())
{ {
@@ -270,7 +264,7 @@ REGISTER_TEST_VERSION(external_semaphores_cross_context, Version(1, 2))
// Signal semaphore on context1 // Signal semaphore on context1
cl_semaphore_khr exportable_semaphore = cl_semaphore_khr exportable_semaphore =
clCreateSemaphoreWithPropertiesKHR(context, export_props, &err); clCreateSemaphoreWithPropertiesKHR(contexts[0], export_props, &err);
test_error(err, "Failed to create exportable semaphore"); test_error(err, "Failed to create exportable semaphore");
err = clEnqueueSignalSemaphoresKHR(queue1, 1, &exportable_semaphore, err = clEnqueueSignalSemaphoresKHR(queue1, 1, &exportable_semaphore,
@@ -313,12 +307,77 @@ REGISTER_TEST_VERSION(external_semaphores_cross_context, Version(1, 2))
test_error(err, "Failed to release semaphore"); test_error(err, "Failed to release semaphore");
} }
err = clReleaseContext(context2);
test_error(err, "Failed to release context2");
return TEST_PASS; return TEST_PASS;
} }
REGISTER_TEST_VERSION(external_semaphores_cross_context, Version(1, 2))
{
cl_int err = CL_SUCCESS;
clContextWrapper context_sec =
clCreateContext(NULL, 1, &device, notify_callback, NULL, &err);
test_error(err, "Failed to create context2");
cl_context contexts[2] = { context, context_sec };
clCommandQueueWrapper queue0 =
clCreateCommandQueue(context, device, 0, &err);
test_error(err, "Could not create command queue");
clCommandQueueWrapper queue1 =
clCreateCommandQueue(contexts[1], device, 0, &err);
test_error(err, "Could not create command queue");
cl_command_queue queues[2] = { queue0, queue1 };
return doTestImportExport(device, contexts, queues);
}
REGISTER_TEST_VERSION(external_semaphores_import_export, Version(1, 2))
{
cl_int err = CL_SUCCESS;
cl_int total_status = TEST_PASS;
// test external semaphores with out-of-order queue
{
cl_command_queue_properties device_props = 0;
err = clGetDeviceInfo(device, CL_DEVICE_QUEUE_PROPERTIES,
sizeof(device_props), &device_props, NULL);
test_error(err,
"clGetDeviceInfo for CL_DEVICE_QUEUE_PROPERTIES failed");
if ((device_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE) != 0)
{
// Create ooo queue
clCommandQueueWrapper test_queue = clCreateCommandQueue(
context, device, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err);
test_error(err, "Could not create command queue");
cl_command_queue queues[2] = { test_queue, test_queue };
cl_context contexts[2] = { context, context };
cl_int status = doTestImportExport(device, contexts, queues);
if (status != TEST_PASS && status != TEST_SKIPPED_ITSELF)
{
total_status = TEST_FAIL;
}
}
}
// test external semaphore sync fd with in-order harness queue
{
cl_command_queue queues[2] = { queue, queue };
cl_context contexts[2] = { context, context };
cl_int status = doTestImportExport(device, contexts, queues);
if (status != TEST_PASS && status != TEST_SKIPPED_ITSELF)
{
total_status = TEST_FAIL;
}
}
return total_status;
}
// Confirm that a signal followed by a wait will complete successfully // Confirm that a signal followed by a wait will complete successfully
REGISTER_TEST_VERSION(external_semaphores_simple_1, Version(1, 2)) REGISTER_TEST_VERSION(external_semaphores_simple_1, Version(1, 2))
{ {

View File

@@ -1,142 +0,0 @@
//
// Copyright (c) 2024 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "harness/typeWrappers.h"
#include "harness/extensionHelpers.h"
#include "harness/errorHelpers.h"
cl_int doTest(cl_device_id device, cl_context context, cl_command_queue queue)
{
cl_int err = CL_SUCCESS;
// Obtain pointers to semaphore's API
GET_PFN(device, clCreateSemaphoreWithPropertiesKHR);
GET_PFN(device, clEnqueueSignalSemaphoresKHR);
GET_PFN(device, clEnqueueWaitSemaphoresKHR);
GET_PFN(device, clGetSemaphoreHandleForTypeKHR);
GET_PFN(device, clReleaseSemaphoreKHR);
// Create semaphore
cl_semaphore_properties_khr sema_1_props[] = {
static_cast<cl_semaphore_properties_khr>(CL_SEMAPHORE_TYPE_KHR),
static_cast<cl_semaphore_properties_khr>(CL_SEMAPHORE_TYPE_BINARY_KHR),
static_cast<cl_semaphore_properties_khr>(
CL_SEMAPHORE_EXPORT_HANDLE_TYPES_KHR),
static_cast<cl_semaphore_properties_khr>(
CL_SEMAPHORE_HANDLE_SYNC_FD_KHR),
static_cast<cl_semaphore_properties_khr>(
CL_SEMAPHORE_EXPORT_HANDLE_TYPES_LIST_END_KHR),
0
};
cl_semaphore_khr sema_1 =
clCreateSemaphoreWithPropertiesKHR(context, sema_1_props, &err);
test_error(err, "Could not create semaphore");
// Signal semaphore
clEventWrapper signal_event;
err = clEnqueueSignalSemaphoresKHR(queue, 1, &sema_1, nullptr, 0, nullptr,
&signal_event);
test_error(err, "Could not signal semaphore");
// Extract sync fd
int handle = -1;
size_t handle_size;
err = clGetSemaphoreHandleForTypeKHR(sema_1, device,
CL_SEMAPHORE_HANDLE_SYNC_FD_KHR,
sizeof(handle), &handle, &handle_size);
test_error(err, "Could not extract semaphore handle");
test_assert_error(sizeof(handle) == handle_size, "Invalid handle size");
test_assert_error(handle >= 0, "Invalid handle");
// Create semaphore from sync fd
cl_semaphore_properties_khr sema_2_props[] = {
static_cast<cl_semaphore_properties_khr>(CL_SEMAPHORE_TYPE_KHR),
static_cast<cl_semaphore_properties_khr>(CL_SEMAPHORE_TYPE_BINARY_KHR),
CL_SEMAPHORE_HANDLE_SYNC_FD_KHR,
static_cast<cl_semaphore_properties_khr>(handle), 0
};
cl_semaphore_khr sema_2 =
clCreateSemaphoreWithPropertiesKHR(context, sema_2_props, &err);
test_error(err, "Could not create semaphore");
// Wait semaphore
clEventWrapper wait_event;
err = clEnqueueWaitSemaphoresKHR(queue, 1, &sema_2, nullptr, 0, nullptr,
&wait_event);
test_error(err, "Could not wait semaphore");
// Finish
err = clFinish(queue);
test_error(err, "Could not finish queue");
// Check all events are completed
test_assert_event_complete(signal_event);
test_assert_event_complete(wait_event);
// Release semaphore
err = clReleaseSemaphoreKHR(sema_1);
test_error(err, "Could not release semaphore");
err = clReleaseSemaphoreKHR(sema_2);
test_error(err, "Could not release semaphore");
return TEST_PASS;
}
// Test it is possible to export a semaphore to a sync fd and import the same
// sync fd to a new semaphore
REGISTER_TEST_VERSION(external_semaphores_import_export_fd, Version(1, 2))
{
REQUIRE_EXTENSION("cl_khr_external_semaphore");
REQUIRE_EXTENSION("cl_khr_external_semaphore_sync_fd");
cl_int err = CL_SUCCESS;
cl_int total_status = TEST_PASS;
// test external semaphore sync fd with out-of-order queue
{
cl_command_queue_properties device_props = 0;
err = clGetDeviceInfo(device, CL_DEVICE_QUEUE_PROPERTIES,
sizeof(device_props), &device_props, NULL);
test_error(err,
"clGetDeviceInfo for CL_DEVICE_QUEUE_PROPERTIES failed");
if ((device_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE) != 0)
{
// Create ooo queue
clCommandQueueWrapper test_queue = clCreateCommandQueue(
context, device, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err);
test_error(err, "Could not create command queue");
cl_int status = doTest(device, context, test_queue);
if (status != TEST_PASS && status != TEST_SKIPPED_ITSELF)
{
total_status = TEST_FAIL;
}
}
}
// test external semaphore sync fd with in-order harness queue
{
cl_int status = doTest(device, context, queue);
if (status != TEST_PASS && status != TEST_SKIPPED_ITSELF)
{
total_status = TEST_FAIL;
}
}
return total_status;
}