Skip to content

Commit f078296

Browse files
committed
OpenCL adapter granular update error checks
1 parent a6572e6 commit f078296

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

source/adapters/opencl/command_buffer.cpp

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
9292

9393
try {
9494
auto URCommandBuffer = std::make_unique<ur_exp_command_buffer_handle_t_>(
95-
Queue, hContext, CLCommandBuffer, IsUpdatable);
95+
Queue, hContext, hDevice, CLCommandBuffer, IsUpdatable);
9696
*phCommandBuffer = URCommandBuffer.release();
9797
} catch (...) {
9898
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
@@ -540,18 +540,72 @@ void updateKernelArgs(std::vector<cl_mutable_dispatch_arg_khr> &CLArgs,
540540
}
541541
}
542542

543+
ur_result_t validateCommandDesc(
544+
ur_exp_command_buffer_command_handle_t Command,
545+
const ur_exp_command_buffer_update_kernel_launch_desc_t *UpdateDesc) {
546+
// Kernel handle updates are not yet supported.
547+
if (UpdateDesc->hNewKernel && UpdateDesc->hNewKernel != Command->Kernel) {
548+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
549+
}
550+
551+
// Error if work-dim has change but a new global size/offset hasn't been set
552+
if (UpdateDesc->newWorkDim != Command->WorkDim &&
553+
(!UpdateDesc->pNewGlobalWorkOffset || !UpdateDesc->pNewGlobalWorkSize)) {
554+
return UR_RESULT_ERROR_INVALID_OPERATION;
555+
}
556+
557+
// Verify that the device supports updating the aspects of the kernel that
558+
// the user is requesting.
559+
ur_device_handle_t URDevice = Command->hCommandBuffer->hDevice;
560+
cl_device_id CLDevice = cl_adapter::cast<cl_device_id>(URDevice);
561+
562+
ur_device_command_buffer_update_capability_flags_t UpdateCapabilities = 0;
563+
CL_RETURN_ON_FAILURE(
564+
getDeviceCommandBufferUpdateCapabilities(CLDevice, UpdateCapabilities));
565+
566+
size_t *NewGlobalWorkOffset = UpdateDesc->pNewGlobalWorkOffset;
567+
UR_ASSERT(
568+
!NewGlobalWorkOffset ||
569+
(UpdateCapabilities &
570+
UR_DEVICE_COMMAND_BUFFER_UPDATE_CAPABILITY_FLAG_GLOBAL_WORK_OFFSET),
571+
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
572+
573+
size_t *NewLocalWorkSize = UpdateDesc->pNewLocalWorkSize;
574+
UR_ASSERT(
575+
!NewLocalWorkSize ||
576+
(UpdateCapabilities &
577+
UR_DEVICE_COMMAND_BUFFER_UPDATE_CAPABILITY_FLAG_LOCAL_WORK_SIZE),
578+
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
579+
580+
size_t *NewGlobalWorkSize = UpdateDesc->pNewGlobalWorkSize;
581+
UR_ASSERT(
582+
!NewGlobalWorkSize ||
583+
(UpdateCapabilities &
584+
UR_DEVICE_COMMAND_BUFFER_UPDATE_CAPABILITY_FLAG_GLOBAL_WORK_SIZE),
585+
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
586+
UR_ASSERT(
587+
!(NewGlobalWorkSize && !NewLocalWorkSize) ||
588+
(UpdateCapabilities &
589+
UR_DEVICE_COMMAND_BUFFER_UPDATE_CAPABILITY_FLAG_LOCAL_WORK_SIZE),
590+
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
591+
592+
UR_ASSERT(
593+
(!UpdateDesc->numNewMemObjArgs && !UpdateDesc->numNewPointerArgs &&
594+
!UpdateDesc->numNewValueArgs) ||
595+
(UpdateCapabilities &
596+
UR_DEVICE_COMMAND_BUFFER_UPDATE_CAPABILITY_FLAG_KERNEL_ARGUMENTS),
597+
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
598+
599+
return UR_RESULT_SUCCESS;
600+
}
543601
} // end anonymous namespace
544602

545603
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
546604
ur_exp_command_buffer_command_handle_t hCommand,
547605
const ur_exp_command_buffer_update_kernel_launch_desc_t
548606
*pUpdateKernelLaunch) {
549607

550-
// Kernel handle updates are not yet supported.
551-
if (pUpdateKernelLaunch->hNewKernel &&
552-
pUpdateKernelLaunch->hNewKernel != hCommand->Kernel) {
553-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
554-
}
608+
UR_RETURN_ON_FAILURE(validateCommandDesc(hCommand, pUpdateKernelLaunch));
555609

556610
ur_exp_command_buffer_handle_t hCommandBuffer = hCommand->hCommandBuffer;
557611
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
@@ -565,12 +619,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
565619
if (!hCommandBuffer->IsFinalized || !hCommandBuffer->IsUpdatable)
566620
return UR_RESULT_ERROR_INVALID_OPERATION;
567621

568-
if (pUpdateKernelLaunch->newWorkDim != hCommand->WorkDim &&
569-
(!pUpdateKernelLaunch->pNewGlobalWorkOffset ||
570-
!pUpdateKernelLaunch->pNewGlobalWorkSize)) {
571-
return UR_RESULT_ERROR_INVALID_OPERATION;
572-
}
573-
574622
// Find the CL USM pointer arguments to the kernel to update
575623
std::vector<cl_mutable_dispatch_arg_khr> CLUSMArgs;
576624
updateKernelPointerArgs(CLUSMArgs, pUpdateKernelLaunch);

source/adapters/opencl/command_buffer.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ struct ur_exp_command_buffer_handle_t_ {
6464
ur_queue_handle_t hInternalQueue;
6565
/// Context the command-buffer is created for.
6666
ur_context_handle_t hContext;
67+
/// Device the command-buffer is created for.
68+
ur_device_handle_t hDevice;
6769
/// OpenCL command-buffer object.
6870
cl_command_buffer_khr CLCommandBuffer;
6971
/// Set to true if the kernel commands in the command-buffer can be updated,
@@ -83,9 +85,10 @@ struct ur_exp_command_buffer_handle_t_ {
8385

8486
ur_exp_command_buffer_handle_t_(ur_queue_handle_t hQueue,
8587
ur_context_handle_t hContext,
88+
ur_device_handle_t hDevice,
8689
cl_command_buffer_khr CLCommandBuffer,
8790
bool IsUpdatable)
88-
: hInternalQueue(hQueue), hContext(hContext),
91+
: hInternalQueue(hQueue), hContext(hContext), hDevice(hDevice),
8992
CLCommandBuffer(CLCommandBuffer), IsUpdatable(IsUpdatable),
9093
IsFinalized(false), RefCountInternal(0), RefCountExternal(0) {}
9194

test/conformance/exp_command_buffer/exp_command_buffer_adapter_native_cpu.match

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@
4646
{{OPT}}LocalMemoryUpdateTest.UpdateParametersPartialLocalSize/*
4747
{{OPT}}LocalMemoryMultiUpdateTest.UpdateParameters/*
4848
{{OPT}}LocalMemoryMultiUpdateTest.UpdateWithoutBlocking/*
49-
{{OPT}}urInvalidUpdateCommandBufferExpExecutionTest.*
49+
{{OPT}}InvalidUpdateCommandBufferExpExecutionTest.*

0 commit comments

Comments
 (0)