Skip to content

Commit f685bb3

Browse files
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 50ef8c9 commit f685bb3

File tree

7 files changed

+26
-0
lines changed

7 files changed

+26
-0
lines changed

source/adapters/cuda/command_buffer.cpp

Lines changed: 3 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->IsFinalized,
405+
UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP);
404406
try {
405407
const unsigned long long flags = 0;
406408
#if CUDA_VERSION >= 12000
@@ -418,6 +420,7 @@ urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
418420
} catch (...) {
419421
return UR_RESULT_ERROR_UNKNOWN;
420422
}
423+
hCommandBuffer->IsFinalized = true;
421424
return UR_RESULT_SUCCESS;
422425
}
423426

source/adapters/cuda/command_buffer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ struct ur_exp_command_buffer_handle_t_ {
352352
ur_device_handle_t Device;
353353
// Whether commands in the command-buffer can be updated
354354
bool IsUpdatable;
355+
// Keep track of whether command buffer is finalized
356+
bool IsFinalized = false;
355357
// Cuda Graph handle
356358
CUgraph CudaGraph;
357359
// Cuda Graph Exec handle

source/adapters/hip/command_buffer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,16 @@ 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->IsFinalized,
310+
UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP);
309311
try {
310312
const unsigned long long flags = 0;
311313
UR_CHECK_ERROR(hipGraphInstantiateWithFlags(
312314
&hCommandBuffer->HIPGraphExec, hCommandBuffer->HIPGraph, flags));
313315
} catch (...) {
314316
return UR_RESULT_ERROR_UNKNOWN;
315317
}
318+
hCommandBuffer->IsFinalized = true;
316319
return UR_RESULT_SUCCESS;
317320
}
318321

source/adapters/hip/command_buffer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ struct ur_exp_command_buffer_handle_t_ {
172172
ur_device_handle_t Device;
173173
// Whether commands in the command-buffer can be updated
174174
bool IsUpdatable;
175+
// Keep track of whether command buffer is finalized
176+
bool IsFinalized = false;
175177
// HIP Graph handle
176178
hipGraph_t HIPGraph;
177179
// HIP Graph Exec handle

source/adapters/level_zero/command_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t CommandBuffer) {
595595
ur_result_t
596596
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t CommandBuffer) {
597597
UR_ASSERT(CommandBuffer, UR_RESULT_ERROR_INVALID_NULL_POINTER);
598+
UR_ASSERT(!CommandBuffer->IsFinalized,
599+
UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP);
598600
// It is not allowed to append to command list from multiple threads.
599601
std::scoped_lock<ur_shared_mutex> Guard(CommandBuffer->Mutex);
600602

source/adapters/opencl/command_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ 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,
128+
UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP);
127129
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
128130
cl_ext::clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHR = nullptr;
129131
UR_RETURN_ON_FAILURE(

test/conformance/exp_command_buffer/commands.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,15 @@ TEST_P(urCommandBufferAppendKernelLaunchExpTest, Basic) {
204204
ASSERT_EQ(result, ptrZ[i]);
205205
}
206206
}
207+
208+
TEST_P(urCommandBufferAppendKernelLaunchExpTest, FinalizeTwice) {
209+
ASSERT_SUCCESS(urCommandBufferAppendKernelLaunchExp(
210+
cmd_buf_handle, kernel, n_dimensions, &global_offset, &global_size,
211+
&local_size, 0, nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr,
212+
nullptr));
213+
214+
ASSERT_SUCCESS(urCommandBufferFinalizeExp(cmd_buf_handle));
215+
EXPECT_EQ_RESULT(urCommandBufferFinalizeExp(cmd_buf_handle),
216+
UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP);
217+
218+
}

0 commit comments

Comments
 (0)