Skip to content

Commit 69b0e1b

Browse files
author
Hugh Delaney
committed
Make double finalizing fail
A command buffer should fail if finalize is called multiple times on the same command buffer. This matches openCL behaviour.
1 parent 098deca commit 69b0e1b

File tree

13 files changed

+26
-2
lines changed

13 files changed

+26
-2
lines changed

include/ur_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8517,6 +8517,7 @@ urCommandBufferReleaseExp(
85178517
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
85188518
/// + `NULL == hCommandBuffer`
85198519
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
8520+
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
85208521
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
85218522
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
85228523
UR_APIEXPORT ur_result_t UR_APICALL

scripts/core/exp-command-buffer.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ params:
330330
desc: "[in] Handle of the command-buffer object."
331331
returns:
332332
- $X_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
333+
- $X_RESULT_ERROR_INVALID_OPERATION
334+
- "If `hCommandBuffer` has already been finalized"
333335
- $X_RESULT_ERROR_OUT_OF_HOST_MEMORY
334336
- $X_RESULT_ERROR_OUT_OF_RESOURCES
335337
--- #--------------------------------------------------------------------------

source/adapters/cuda/command_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
401401

402402
UR_APIEXPORT ur_result_t UR_APICALL
403403
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
404+
UR_ASSERT(hCommandBuffer->CudaGraphExec == nullptr,
405+
UR_RESULT_ERROR_INVALID_OPERATION);
404406
try {
405407
const unsigned long long flags = 0;
406408
#if CUDA_VERSION >= 12000

source/adapters/cuda/command_buffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ struct ur_exp_command_buffer_handle_t_ {
355355
// Cuda Graph handle
356356
CUgraph CudaGraph;
357357
// Cuda Graph Exec handle
358-
CUgraphExec CudaGraphExec;
358+
CUgraphExec CudaGraphExec = nullptr;
359359
// Atomic variable counting the number of reference to this command_buffer
360360
// using std::atomic prevents data race when incrementing/decrementing.
361361
std::atomic_uint32_t RefCountInternal;

source/adapters/hip/command_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
306306

307307
UR_APIEXPORT ur_result_t UR_APICALL
308308
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
309+
UR_ASSERT(hCommandBuffer->HIPGraphExec == nullptr,
310+
UR_RESULT_ERROR_INVALID_OPERATION);
309311
try {
310312
const unsigned long long flags = 0;
311313
UR_CHECK_ERROR(hipGraphInstantiateWithFlags(

source/adapters/hip/command_buffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ struct ur_exp_command_buffer_handle_t_ {
175175
// HIP Graph handle
176176
hipGraph_t HIPGraph;
177177
// HIP Graph Exec handle
178-
hipGraphExec_t HIPGraphExec;
178+
hipGraphExec_t HIPGraphExec = nullptr;
179179
// Atomic variable counting the number of reference to this command_buffer
180180
// using std::atomic prevents data race when incrementing/decrementing.
181181
std::atomic_uint32_t RefCountInternal;

source/adapters/level_zero/command_buffer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ finalizeWaitEventPath(ur_exp_command_buffer_handle_t CommandBuffer) {
865865
ur_result_t
866866
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t CommandBuffer) {
867867
UR_ASSERT(CommandBuffer, UR_RESULT_ERROR_INVALID_NULL_POINTER);
868+
UR_ASSERT(!CommandBuffer->IsFinalized, UR_RESULT_ERROR_INVALID_OPERATION);
868869

869870
// It is not allowed to append to command list from multiple threads.
870871
std::scoped_lock<ur_shared_mutex> Guard(CommandBuffer->Mutex);

source/adapters/opencl/command_buffer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
124124

125125
UR_APIEXPORT ur_result_t UR_APICALL
126126
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
127+
UR_ASSERT(!hCommandBuffer->IsFinalized, UR_RESULT_ERROR_INVALID_OPERATION);
127128
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
128129
cl_ext::clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHR = nullptr;
129130
UR_RETURN_ON_FAILURE(

source/loader/ur_libapi.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7547,6 +7547,7 @@ ur_result_t UR_APICALL urCommandBufferReleaseExp(
75477547
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
75487548
/// + `NULL == hCommandBuffer`
75497549
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
7550+
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
75507551
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
75517552
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
75527553
ur_result_t UR_APICALL urCommandBufferFinalizeExp(

source/ur_api.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6410,6 +6410,7 @@ ur_result_t UR_APICALL urCommandBufferReleaseExp(
64106410
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
64116411
/// + `NULL == hCommandBuffer`
64126412
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
6413+
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
64136414
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
64146415
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
64156416
ur_result_t UR_APICALL urCommandBufferFinalizeExp(

0 commit comments

Comments
 (0)