Skip to content

Commit

Permalink
[EXP][CMDBUF] Make command handle behaviour consistent
Browse files Browse the repository at this point in the history
- Attempting to obtain a command handle when the command buffer is not updatable is now an error across all adapters
- command-buffer spec and adapter code update to reflect this
- invalid_update CTS test updated to reflect new behaviour
- Add ability to query command-buffer descriptor properties
  • Loading branch information
Bensuo committed Oct 7, 2024
1 parent df6da35 commit bee945e
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 28 deletions.
10 changes: 8 additions & 2 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8196,6 +8196,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 @@ -8445,6 +8449,7 @@ urCommandBufferFinalizeExp(
/// + `pSyncPointWaitList != NULL && numSyncPointsInWaitList == 0`
/// - ::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 All @@ -8466,7 +8471,8 @@ urCommandBufferAppendKernelLaunchExp(
const ur_exp_command_buffer_sync_point_t *pSyncPointWaitList, ///< [in][optional] A list of sync points that this command depends on. May
///< be ignored if command-buffer is in-order.
ur_exp_command_buffer_sync_point_t *pSyncPoint, ///< [out][optional] Sync point associated with this command.
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 @@ -9006,7 +9012,7 @@ urCommandBufferUpdateKernelLaunchExp(
/// - ::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 @@ -9810,6 +9810,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 @@ -9838,6 +9841,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 @@ -301,6 +301,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 @@ -106,6 +106,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 @@ -366,7 +372,8 @@ params:
desc: "[out][optional] Sync point associated with this command."
- 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 @@ -382,6 +389,8 @@ returns:
- "`pSyncPointWaitList != NULL && numSyncPointsInWaitList == 0`"
- $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 @@ -359,6 +359,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_sync_point_t *pSyncPoint,
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 @@ -1064,6 +1067,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 @@ -326,6 +326,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_sync_point_t *pSyncPoint,
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 @@ -1041,6 +1044,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 @@ -740,6 +740,9 @@ ur_result_t urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_sync_point_t *RetSyncPoint,
ur_exp_command_buffer_command_handle_t *Command) {
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 @@ -770,7 +773,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 @@ -1605,14 +1608,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 @@ -1641,6 +1644,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 @@ -8368,8 +8368,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
///< be ignored if command-buffer is in-order.
ur_exp_command_buffer_sync_point_t *
pSyncPoint, ///< [out][optional] Sync point associated with this command.
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 @@ -148,6 +148,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
ur_exp_command_buffer_sync_point_t *pSyncPoint,
ur_exp_command_buffer_command_handle_t *phCommandHandle) {

// 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 @@ -580,6 +584,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 @@ -7154,8 +7154,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
///< be ignored if command-buffer is in-order.
ur_exp_command_buffer_sync_point_t *
pSyncPoint, ///< [out][optional] Sync point associated with this command.
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 @@ -8077,8 +8077,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
///< be ignored if command-buffer is in-order.
ur_exp_command_buffer_sync_point_t *
pSyncPoint, ///< [out][optional] Sync point associated with this command.
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 @@ -9005,7 +9006,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 @@ -7124,8 +7124,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
///< be ignored if command-buffer is in-order.
ur_exp_command_buffer_sync_point_t *
pSyncPoint, ///< [out][optional] Sync point associated with this command.
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 @@ -7535,6 +7535,7 @@ ur_result_t UR_APICALL urCommandBufferFinalizeExp(
/// + `pSyncPointWaitList != NULL && numSyncPointsInWaitList == 0`
/// - ::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 @@ -7564,8 +7565,9 @@ ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
///< be ignored if command-buffer is in-order.
ur_exp_command_buffer_sync_point_t *
pSyncPoint, ///< [out][optional] Sync point associated with this command.
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 @@ -8358,7 +8360,7 @@ ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
/// - ::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 @@ -6391,6 +6391,7 @@ ur_result_t UR_APICALL urCommandBufferFinalizeExp(
/// + `pSyncPointWaitList != NULL && numSyncPointsInWaitList == 0`
/// - ::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 @@ -6420,8 +6421,9 @@ ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
///< be ignored if command-buffer is in-order.
ur_exp_command_buffer_sync_point_t *
pSyncPoint, ///< [out][optional] Sync point associated with this command.
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 @@ -7063,7 +7065,7 @@ ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
/// - ::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 bee945e

Please sign in to comment.