Skip to content

Commit

Permalink
Merge pull request #2131 from Bensuo/ben/command-handle-fix
Browse files Browse the repository at this point in the history
[EXP][CMDBUF] Make command handle behaviour consistent
  • Loading branch information
kbenzie authored Oct 21, 2024
2 parents 3a8bf2c + 504d3b6 commit c742ca4
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 29 deletions.
10 changes: 8 additions & 2 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8233,6 +8233,10 @@ typedef enum ur_exp_command_buffer_info_t {
///< The reference count returned should be considered immediately stale.
///< It is unsuitable for general use in applications. This feature is
///< provided for identifying memory leaks.
UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR = 1, ///< [::ur_exp_command_buffer_desc_t] Returns a ::ur_exp_command_buffer_desc_t
///< with the properties of the command-buffer. Returned values may differ
///< from those passed on construction if the property was ignored by the
///< adapter.
/// @cond
UR_EXP_COMMAND_BUFFER_INFO_FORCE_UINT32 = 0x7fffffff
/// @endcond
Expand Down Expand Up @@ -8489,6 +8493,7 @@ urCommandBufferFinalizeExp(
/// + If the device associated with `hCommandBuffer` does not support UR_DEVICE_INFO_COMMAND_BUFFER_EVENT_SUPPORT_EXP and either `phEvent` or `phEventWaitList` are not NULL.
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "phCommand is not NULL and hCommandBuffer is not updatable."
UR_APIEXPORT ur_result_t UR_APICALL
urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_handle_t hCommandBuffer, ///< [in] Handle of the command-buffer object.
Expand Down Expand Up @@ -8517,7 +8522,8 @@ urCommandBufferAppendKernelLaunchExp(
ur_event_handle_t *phEvent, ///< [out][optional] return an event object that will be signaled by the
///< completion of this command in the next execution of the
///< command-buffer.
ur_exp_command_buffer_command_handle_t *phCommand ///< [out][optional] Handle to this command.
ur_exp_command_buffer_command_handle_t *phCommand ///< [out][optional] Handle to this command. Only available if the
///< command-buffer is updatable.
);

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -9289,7 +9295,7 @@ urCommandBufferUpdateWaitEventsExp(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hCommandBuffer`
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
/// + `::UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT < propName`
/// + `::UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR < propName`
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
/// + If `propName` is not supported by the adapter.
/// - ::UR_RESULT_ERROR_INVALID_SIZE
Expand Down
15 changes: 15 additions & 0 deletions include/ur_print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9847,6 +9847,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_exp_command_buffer_inf
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
os << "UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT";
break;
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR:
os << "UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR";
break;
default:
os << "unknown enumerator";
break;
Expand Down Expand Up @@ -9875,6 +9878,18 @@ inline ur_result_t printTagged(std::ostream &os, const void *ptr, ur_exp_command

os << ")";
} break;
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
const ur_exp_command_buffer_desc_t *tptr = (const ur_exp_command_buffer_desc_t *)ptr;
if (sizeof(ur_exp_command_buffer_desc_t) > size) {
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_exp_command_buffer_desc_t) << ")";
return UR_RESULT_ERROR_INVALID_SIZE;
}
os << (const void *)(tptr) << " (";

os << *tptr;

os << ")";
} break;
default:
os << "unknown enumerator";
return UR_RESULT_ERROR_INVALID_ENUMERATION;
Expand Down
1 change: 1 addition & 0 deletions scripts/core/EXP-COMMAND-BUFFER.rst
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ Enums
* ${X}_FUNCTION_COMMAND_BUFFER_UPDATE_KERNEL_LAUNCH_EXP
* ${x}_exp_command_buffer_info_t
* ${X}_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT
* ${X}_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR
* ${x}_exp_command_buffer_command_info_t
* ${X}_EXP_COMMAND_BUFFER_COMMAND_INFO_REFERENCE_COUNT

Expand Down
11 changes: 10 additions & 1 deletion scripts/core/exp-command-buffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ etors:
[uint32_t] Reference count of the command-buffer object.
The reference count returned should be considered immediately stale.
It is unsuitable for general use in applications. This feature is provided for identifying memory leaks.
- name: DESCRIPTOR
desc: |
[$x_exp_command_buffer_desc_t] Returns a $x_exp_command_buffer_desc_t
with the properties of the command-buffer. Returned values may differ
from those passed on construction if the property was ignored by the
adapter.
--- #--------------------------------------------------------------------------
type: enum
desc: "Command-buffer command query information type"
Expand Down Expand Up @@ -380,7 +386,8 @@ params:
desc: "[out][optional] return an event object that will be signaled by the completion of this command in the next execution of the command-buffer."
- type: "$x_exp_command_buffer_command_handle_t*"
name: phCommand
desc: "[out][optional] Handle to this command."
desc: "[out][optional] Handle to this command. Only available if the
command-buffer is updatable."
returns:
- $X_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
- $X_RESULT_ERROR_INVALID_KERNEL
Expand All @@ -403,6 +410,8 @@ returns:
- "If the device associated with `hCommandBuffer` does not support UR_DEVICE_INFO_COMMAND_BUFFER_EVENT_SUPPORT_EXP and either `phEvent` or `phEventWaitList` are not NULL."
- $X_RESULT_ERROR_OUT_OF_HOST_MEMORY
- $X_RESULT_ERROR_OUT_OF_RESOURCES
- $X_RESULT_ERROR_INVALID_OPERATION
- "phCommand is not NULL and hCommandBuffer is not updatable."
--- #--------------------------------------------------------------------------
type: function
desc: "Append a USM memcpy command to a command-buffer object."
Expand Down
13 changes: 13 additions & 0 deletions source/adapters/cuda/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_sync_point_t *pSyncPoint, ur_event_handle_t *phEvent,
ur_exp_command_buffer_command_handle_t *phCommand) {
// Preconditions
// Command handles can only be obtained from updatable command-buffers
UR_ASSERT(!(phCommand && !hCommandBuffer->IsUpdatable),
UR_RESULT_ERROR_INVALID_OPERATION);
UR_ASSERT(hCommandBuffer->Context == hKernel->getContext(),
UR_RESULT_ERROR_INVALID_KERNEL);
UR_ASSERT(workDim > 0, UR_RESULT_ERROR_INVALID_WORK_DIMENSION);
Expand Down Expand Up @@ -1479,6 +1482,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp(
switch (propName) {
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
return ReturnValue(hCommandBuffer->getExternalReferenceCount());
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
ur_exp_command_buffer_desc_t Descriptor{};
Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC;
Descriptor.pNext = nullptr;
Descriptor.isUpdatable = hCommandBuffer->IsUpdatable;
Descriptor.isInOrder = false;
Descriptor.enableProfiling = false;

return ReturnValue(Descriptor);
}
default:
assert(!"Command-buffer info request not implemented");
}
Expand Down
12 changes: 12 additions & 0 deletions source/adapters/hip/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
std::ignore = phEventWaitList;
std::ignore = phEvent;
// Preconditions
// Command handles can only be obtained from updatable command-buffers
UR_ASSERT(!(phCommand && !hCommandBuffer->IsUpdatable),
UR_RESULT_ERROR_INVALID_OPERATION);
UR_ASSERT(hCommandBuffer->Context == hKernel->getContext(),
UR_RESULT_ERROR_INVALID_KERNEL);
UR_ASSERT(workDim > 0, UR_RESULT_ERROR_INVALID_WORK_DIMENSION);
Expand Down Expand Up @@ -1128,6 +1131,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp(
switch (propName) {
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
return ReturnValue(hCommandBuffer->getExternalReferenceCount());
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
ur_exp_command_buffer_desc_t Descriptor{};
Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC;
Descriptor.pNext = nullptr;
Descriptor.isUpdatable = hCommandBuffer->IsUpdatable;
Descriptor.isInOrder = false, Descriptor.enableProfiling = false;

return ReturnValue(Descriptor);
}
default:
assert(!"Command-buffer info request not implemented");
}
Expand Down
19 changes: 16 additions & 3 deletions source/adapters/level_zero/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,9 @@ ur_result_t urCommandBufferAppendKernelLaunchExp(
std::ignore = Event;

UR_ASSERT(Kernel->Program, UR_RESULT_ERROR_INVALID_NULL_POINTER);
// Command handles can only be obtained from updatable command-buffers
UR_ASSERT(!(Command && !CommandBuffer->IsUpdatable),
UR_RESULT_ERROR_INVALID_OPERATION);

// Lock automatically releases when this goes out of scope.
std::scoped_lock<ur_shared_mutex, ur_shared_mutex, ur_shared_mutex> Lock(
Expand Down Expand Up @@ -775,7 +778,7 @@ ur_result_t urCommandBufferAppendKernelLaunchExp(
// reference count on the kernel, using the kernel saved in CommandData.
UR_CALL(ur::level_zero::urKernelRetain(Kernel));

if (Command && CommandBuffer->IsUpdatable) {
if (Command) {
UR_CALL(createCommandHandle(CommandBuffer, Kernel, WorkDim, LocalWorkSize,
*Command));
}
Expand Down Expand Up @@ -1676,14 +1679,14 @@ ur_result_t updateKernelCommand(
ur_result_t urCommandBufferUpdateKernelLaunchExp(
ur_exp_command_buffer_command_handle_t Command,
const ur_exp_command_buffer_update_kernel_launch_desc_t *CommandDesc) {
UR_ASSERT(Command->CommandBuffer->IsUpdatable,
UR_RESULT_ERROR_INVALID_OPERATION);
UR_ASSERT(Command->Kernel, UR_RESULT_ERROR_INVALID_NULL_HANDLE);

// Lock command, kernel and command buffer for update.
std::scoped_lock<ur_shared_mutex, ur_shared_mutex, ur_shared_mutex> Guard(
Command->Mutex, Command->CommandBuffer->Mutex, Command->Kernel->Mutex);

UR_ASSERT(Command->CommandBuffer->IsUpdatable,
UR_RESULT_ERROR_INVALID_OPERATION);
UR_ASSERT(Command->CommandBuffer->IsFinalized,
UR_RESULT_ERROR_INVALID_OPERATION);

Expand Down Expand Up @@ -1728,6 +1731,16 @@ urCommandBufferGetInfoExp(ur_exp_command_buffer_handle_t hCommandBuffer,
switch (propName) {
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
return ReturnValue(uint32_t{hCommandBuffer->RefCount.load()});
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
ur_exp_command_buffer_desc_t Descriptor{};
Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC;
Descriptor.pNext = nullptr;
Descriptor.isUpdatable = hCommandBuffer->IsUpdatable;
Descriptor.isInOrder = hCommandBuffer->IsInOrderCmdList;
Descriptor.enableProfiling = hCommandBuffer->IsProfilingEnabled;

return ReturnValue(Descriptor);
}
default:
assert(!"Command-buffer info request not implemented");
}
Expand Down
5 changes: 3 additions & 2 deletions source/adapters/mock/ur_mockddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8399,8 +8399,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
phEvent, ///< [out][optional] return an event object that will be signaled by the
///< completion of this command in the next execution of the
///< command-buffer.
ur_exp_command_buffer_command_handle_t
*phCommand ///< [out][optional] Handle to this command.
ur_exp_command_buffer_command_handle_t *
phCommand ///< [out][optional] Handle to this command. Only available if the
///< command-buffer is updatable.
) try {
ur_result_t result = UR_RESULT_SUCCESS;

Expand Down
14 changes: 14 additions & 0 deletions source/adapters/opencl/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
(void)phEventWaitList;
(void)phEvent;

// Command handles can only be obtained from updatable command-buffers
UR_ASSERT(!(phCommandHandle && !hCommandBuffer->IsUpdatable),
UR_RESULT_ERROR_INVALID_OPERATION);

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHR = nullptr;
UR_RETURN_ON_FAILURE(
Expand Down Expand Up @@ -646,6 +650,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp(
switch (propName) {
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
return ReturnValue(hCommandBuffer->getExternalReferenceCount());
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
ur_exp_command_buffer_desc_t Descriptor{};
Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC;
Descriptor.pNext = nullptr;
Descriptor.isUpdatable = hCommandBuffer->IsUpdatable;
Descriptor.isInOrder = false;
Descriptor.enableProfiling = false;

return ReturnValue(Descriptor);
}
default:
assert(!"Command-buffer info request not implemented");
}
Expand Down
5 changes: 3 additions & 2 deletions source/loader/layers/tracing/ur_trcddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7185,8 +7185,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
phEvent, ///< [out][optional] return an event object that will be signaled by the
///< completion of this command in the next execution of the
///< command-buffer.
ur_exp_command_buffer_command_handle_t
*phCommand ///< [out][optional] Handle to this command.
ur_exp_command_buffer_command_handle_t *
phCommand ///< [out][optional] Handle to this command. Only available if the
///< command-buffer is updatable.
) {
auto pfnAppendKernelLaunchExp =
getContext()->urDdiTable.CommandBufferExp.pfnAppendKernelLaunchExp;
Expand Down
7 changes: 4 additions & 3 deletions source/loader/layers/validation/ur_valddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8108,8 +8108,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
phEvent, ///< [out][optional] return an event object that will be signaled by the
///< completion of this command in the next execution of the
///< command-buffer.
ur_exp_command_buffer_command_handle_t
*phCommand ///< [out][optional] Handle to this command.
ur_exp_command_buffer_command_handle_t *
phCommand ///< [out][optional] Handle to this command. Only available if the
///< command-buffer is updatable.
) {
auto pfnAppendKernelLaunchExp =
getContext()->urDdiTable.CommandBufferExp.pfnAppendKernelLaunchExp;
Expand Down Expand Up @@ -9437,7 +9438,7 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferGetInfoExp(
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
}

if (UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT < propName) {
if (UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR < propName) {
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}

Expand Down
5 changes: 3 additions & 2 deletions source/loader/ur_ldrddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7155,8 +7155,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
phEvent, ///< [out][optional] return an event object that will be signaled by the
///< completion of this command in the next execution of the
///< command-buffer.
ur_exp_command_buffer_command_handle_t
*phCommand ///< [out][optional] Handle to this command.
ur_exp_command_buffer_command_handle_t *
phCommand ///< [out][optional] Handle to this command. Only available if the
///< command-buffer is updatable.
) {
ur_result_t result = UR_RESULT_SUCCESS;

Expand Down
8 changes: 5 additions & 3 deletions source/loader/ur_libapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7573,6 +7573,7 @@ ur_result_t UR_APICALL urCommandBufferFinalizeExp(
/// + If the device associated with `hCommandBuffer` does not support UR_DEVICE_INFO_COMMAND_BUFFER_EVENT_SUPPORT_EXP and either `phEvent` or `phEventWaitList` are not NULL.
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "phCommand is not NULL and hCommandBuffer is not updatable."
ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_handle_t
hCommandBuffer, ///< [in] Handle of the command-buffer object.
Expand Down Expand Up @@ -7611,8 +7612,9 @@ ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
phEvent, ///< [out][optional] return an event object that will be signaled by the
///< completion of this command in the next execution of the
///< command-buffer.
ur_exp_command_buffer_command_handle_t
*phCommand ///< [out][optional] Handle to this command.
ur_exp_command_buffer_command_handle_t *
phCommand ///< [out][optional] Handle to this command. Only available if the
///< command-buffer is updatable.
) try {
auto pfnAppendKernelLaunchExp =
ur_lib::getContext()
Expand Down Expand Up @@ -8706,7 +8708,7 @@ ur_result_t UR_APICALL urCommandBufferUpdateWaitEventsExp(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hCommandBuffer`
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
/// + `::UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT < propName`
/// + `::UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR < propName`
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
/// + If `propName` is not supported by the adapter.
/// - ::UR_RESULT_ERROR_INVALID_SIZE
Expand Down
8 changes: 5 additions & 3 deletions source/ur_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6429,6 +6429,7 @@ ur_result_t UR_APICALL urCommandBufferFinalizeExp(
/// + If the device associated with `hCommandBuffer` does not support UR_DEVICE_INFO_COMMAND_BUFFER_EVENT_SUPPORT_EXP and either `phEvent` or `phEventWaitList` are not NULL.
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
/// - ::UR_RESULT_ERROR_INVALID_OPERATION - "phCommand is not NULL and hCommandBuffer is not updatable."
ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_handle_t
hCommandBuffer, ///< [in] Handle of the command-buffer object.
Expand Down Expand Up @@ -6467,8 +6468,9 @@ ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
phEvent, ///< [out][optional] return an event object that will be signaled by the
///< completion of this command in the next execution of the
///< command-buffer.
ur_exp_command_buffer_command_handle_t
*phCommand ///< [out][optional] Handle to this command.
ur_exp_command_buffer_command_handle_t *
phCommand ///< [out][optional] Handle to this command. Only available if the
///< command-buffer is updatable.
) {
ur_result_t result = UR_RESULT_SUCCESS;
return result;
Expand Down Expand Up @@ -7382,7 +7384,7 @@ ur_result_t UR_APICALL urCommandBufferUpdateWaitEventsExp(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hCommandBuffer`
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
/// + `::UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT < propName`
/// + `::UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR < propName`
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
/// + If `propName` is not supported by the adapter.
/// - ::UR_RESULT_ERROR_INVALID_SIZE
Expand Down
Loading

0 comments on commit c742ca4

Please sign in to comment.