Skip to content

Commit

Permalink
Merge pull request #2327 from hdelan/double-finalize
Browse files Browse the repository at this point in the history
[CommandBuffer] Make double finalizing fail
  • Loading branch information
martygrant authored Dec 13, 2024
2 parents b97f2ea + b631132 commit f6337ce
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8562,6 +8562,7 @@ urCommandBufferReleaseExp(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hCommandBuffer`
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
UR_APIEXPORT ur_result_t UR_APICALL
Expand Down
2 changes: 2 additions & 0 deletions scripts/core/exp-command-buffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ params:
desc: "[in] Handle of the command-buffer object."
returns:
- $X_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
- $X_RESULT_ERROR_INVALID_OPERATION
- "If `hCommandBuffer` has already been finalized"
- $X_RESULT_ERROR_OUT_OF_HOST_MEMORY
- $X_RESULT_ERROR_OUT_OF_RESOURCES
--- #--------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions source/adapters/cuda/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {

UR_APIEXPORT ur_result_t UR_APICALL
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
UR_ASSERT(hCommandBuffer->CudaGraphExec == nullptr,
UR_RESULT_ERROR_INVALID_OPERATION);
try {
const unsigned long long flags = 0;
#if CUDA_VERSION >= 12000
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/cuda/command_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ struct ur_exp_command_buffer_handle_t_ {
// Cuda Graph handle
CUgraph CudaGraph;
// Cuda Graph Exec handle
CUgraphExec CudaGraphExec;
CUgraphExec CudaGraphExec = nullptr;
// Atomic variable counting the number of reference to this command_buffer
// using std::atomic prevents data race when incrementing/decrementing.
std::atomic_uint32_t RefCountInternal;
Expand Down
2 changes: 2 additions & 0 deletions source/adapters/hip/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {

UR_APIEXPORT ur_result_t UR_APICALL
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
UR_ASSERT(hCommandBuffer->HIPGraphExec == nullptr,
UR_RESULT_ERROR_INVALID_OPERATION);
try {
const unsigned long long flags = 0;
UR_CHECK_ERROR(hipGraphInstantiateWithFlags(
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/hip/command_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ struct ur_exp_command_buffer_handle_t_ {
// HIP Graph handle
hipGraph_t HIPGraph;
// HIP Graph Exec handle
hipGraphExec_t HIPGraphExec;
hipGraphExec_t HIPGraphExec = nullptr;
// Atomic variable counting the number of reference to this command_buffer
// using std::atomic prevents data race when incrementing/decrementing.
std::atomic_uint32_t RefCountInternal;
Expand Down
1 change: 1 addition & 0 deletions source/adapters/level_zero/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ finalizeWaitEventPath(ur_exp_command_buffer_handle_t CommandBuffer) {
ur_result_t
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t CommandBuffer) {
UR_ASSERT(CommandBuffer, UR_RESULT_ERROR_INVALID_NULL_POINTER);
UR_ASSERT(!CommandBuffer->IsFinalized, UR_RESULT_ERROR_INVALID_OPERATION);

// It is not allowed to append to command list from multiple threads.
std::scoped_lock<ur_shared_mutex> Guard(CommandBuffer->Mutex);
Expand Down
1 change: 1 addition & 0 deletions source/adapters/opencl/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {

UR_APIEXPORT ur_result_t UR_APICALL
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
UR_ASSERT(!hCommandBuffer->IsFinalized, UR_RESULT_ERROR_INVALID_OPERATION);
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHR = nullptr;
UR_RETURN_ON_FAILURE(
Expand Down
1 change: 1 addition & 0 deletions source/loader/ur_libapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7584,6 +7584,7 @@ ur_result_t UR_APICALL urCommandBufferReleaseExp(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hCommandBuffer`
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
ur_result_t UR_APICALL urCommandBufferFinalizeExp(
Expand Down
1 change: 1 addition & 0 deletions source/ur_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6440,6 +6440,7 @@ ur_result_t UR_APICALL urCommandBufferReleaseExp(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hCommandBuffer`
/// - ::UR_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "If `hCommandBuffer` has already been finalized"
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
ur_result_t UR_APICALL urCommandBufferFinalizeExp(
Expand Down
11 changes: 11 additions & 0 deletions test/conformance/exp_command_buffer/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,14 @@ TEST_P(urCommandBufferAppendKernelLaunchExpTest, Basic) {
ASSERT_EQ(result, ptrZ[i]);
}
}

TEST_P(urCommandBufferAppendKernelLaunchExpTest, FinalizeTwice) {
ASSERT_SUCCESS(urCommandBufferAppendKernelLaunchExp(
cmd_buf_handle, kernel, n_dimensions, &global_offset, &global_size,
&local_size, 0, nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr,
nullptr));

ASSERT_SUCCESS(urCommandBufferFinalizeExp(cmd_buf_handle));
EXPECT_EQ_RESULT(urCommandBufferFinalizeExp(cmd_buf_handle),
UR_RESULT_ERROR_INVALID_OPERATION);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ urCommandBufferCommandsTest.urCommandBufferAppendMemBufferFillExp/*
urCommandBufferCommandsTest.urCommandBufferAppendUSMPrefetchExp/*
urCommandBufferCommandsTest.urCommandBufferAppendUSMAdviseExp/*
urCommandBufferAppendKernelLaunchExpTest.Basic/*
urCommandBufferAppendKernelLaunchExpTest.FinalizeTwice/*
urCommandBufferFillCommandsTest.Buffer/*
urCommandBufferFillCommandsTest.USM/*
KernelCommandEventSyncTest.Basic/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{OPT}}urCommandBufferRetainCommandExpTest.Success/*
{{OPT}}urCommandBufferRetainCommandExpTest.InvalidNullHandle/*
{{OPT}}urCommandBufferAppendKernelLaunchExpTest.Basic/*
{{OPT}}urCommandBufferAppendKernelLaunchExpTest.FinalizeTwice/*
{{OPT}}BufferFillCommandTest.UpdateParameters/*
{{OPT}}BufferFillCommandTest.UpdateGlobalSize/*
{{OPT}}BufferFillCommandTest.SeparateUpdateCalls/*
Expand Down

0 comments on commit f6337ce

Please sign in to comment.