Skip to content

Commit f6337ce

Browse files
authored
Merge pull request #2327 from hdelan/double-finalize
[CommandBuffer] Make double finalizing fail
2 parents b97f2ea + b631132 commit f6337ce

13 files changed

+26
-2
lines changed

include/ur_api.h

+1
Original file line numberDiff line numberDiff line change
@@ -8562,6 +8562,7 @@ urCommandBufferReleaseExp(
85628562
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
85638563
/// + `NULL == hCommandBuffer`
85648564
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
8565+
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
85658566
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
85668567
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
85678568
UR_APIEXPORT ur_result_t UR_APICALL

scripts/core/exp-command-buffer.yml

+2
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

+2
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
404404

405405
UR_APIEXPORT ur_result_t UR_APICALL
406406
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
407+
UR_ASSERT(hCommandBuffer->CudaGraphExec == nullptr,
408+
UR_RESULT_ERROR_INVALID_OPERATION);
407409
try {
408410
const unsigned long long flags = 0;
409411
#if CUDA_VERSION >= 12000

source/adapters/cuda/command_buffer.hpp

+1-1
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

+2
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
304304

305305
UR_APIEXPORT ur_result_t UR_APICALL
306306
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
307+
UR_ASSERT(hCommandBuffer->HIPGraphExec == nullptr,
308+
UR_RESULT_ERROR_INVALID_OPERATION);
307309
try {
308310
const unsigned long long flags = 0;
309311
UR_CHECK_ERROR(hipGraphInstantiateWithFlags(

source/adapters/hip/command_buffer.hpp

+1-1
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

+1
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

+1
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

+1
Original file line numberDiff line numberDiff line change
@@ -7584,6 +7584,7 @@ ur_result_t UR_APICALL urCommandBufferReleaseExp(
75847584
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
75857585
/// + `NULL == hCommandBuffer`
75867586
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
7587+
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
75877588
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
75887589
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
75897590
ur_result_t UR_APICALL urCommandBufferFinalizeExp(

source/ur_api.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -6440,6 +6440,7 @@ ur_result_t UR_APICALL urCommandBufferReleaseExp(
64406440
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
64416441
/// + `NULL == hCommandBuffer`
64426442
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
6443+
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
64436444
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
64446445
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
64456446
ur_result_t UR_APICALL urCommandBufferFinalizeExp(

test/conformance/exp_command_buffer/commands.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,14 @@ 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_OPERATION);
217+
}

test/conformance/exp_command_buffer/exp_command_buffer_adapter_level_zero_v2.match

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ urCommandBufferCommandsTest.urCommandBufferAppendMemBufferFillExp/*
1414
urCommandBufferCommandsTest.urCommandBufferAppendUSMPrefetchExp/*
1515
urCommandBufferCommandsTest.urCommandBufferAppendUSMAdviseExp/*
1616
urCommandBufferAppendKernelLaunchExpTest.Basic/*
17+
urCommandBufferAppendKernelLaunchExpTest.FinalizeTwice/*
1718
urCommandBufferFillCommandsTest.Buffer/*
1819
urCommandBufferFillCommandsTest.USM/*
1920
KernelCommandEventSyncTest.Basic/*

test/conformance/exp_command_buffer/exp_command_buffer_adapter_native_cpu.match

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{{OPT}}urCommandBufferRetainCommandExpTest.Success/*
66
{{OPT}}urCommandBufferRetainCommandExpTest.InvalidNullHandle/*
77
{{OPT}}urCommandBufferAppendKernelLaunchExpTest.Basic/*
8+
{{OPT}}urCommandBufferAppendKernelLaunchExpTest.FinalizeTwice/*
89
{{OPT}}BufferFillCommandTest.UpdateParameters/*
910
{{OPT}}BufferFillCommandTest.UpdateGlobalSize/*
1011
{{OPT}}BufferFillCommandTest.SeparateUpdateCalls/*

0 commit comments

Comments
 (0)