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

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
@@ -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

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
@@ -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

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
@@ -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

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
@@ -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

Lines changed: 1 addition & 0 deletions
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(

0 commit comments

Comments
 (0)