Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to align CTS and Spec for Memory #2177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2508,8 +2508,12 @@ typedef enum ur_mem_type_t {
///////////////////////////////////////////////////////////////////////////////
/// @brief Memory Information type
typedef enum ur_mem_info_t {
UR_MEM_INFO_SIZE = 0, ///< [size_t] actual size of of memory object in bytes
UR_MEM_INFO_CONTEXT = 1, ///< [::ur_context_handle_t] context in which the memory object was created
UR_MEM_INFO_SIZE = 0, ///< [size_t] actual size of of memory object in bytes
UR_MEM_INFO_CONTEXT = 1, ///< [::ur_context_handle_t] context in which the memory object was created
UR_MEM_INFO_REFERENCE_COUNT = 2, ///< [uint32_t] Reference count of the memory 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.
/// @cond
UR_MEM_INFO_FORCE_UINT32 = 0x7fffffff
/// @endcond
Expand Down Expand Up @@ -2644,6 +2648,7 @@ typedef struct ur_image_desc_t {
/// - ::UR_RESULT_ERROR_INVALID_CONTEXT
/// - ::UR_RESULT_ERROR_INVALID_VALUE
/// - ::UR_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR
/// + `pImageDesc && UR_STRUCTURE_TYPE_IMAGE_DESC != pImageDesc->stype`
/// + `pImageDesc && UR_MEM_TYPE_IMAGE1D_ARRAY < pImageDesc->type`
/// + `pImageDesc && pImageDesc->numMipLevel != 0`
/// + `pImageDesc && pImageDesc->numSamples != 0`
Expand Down Expand Up @@ -2984,7 +2989,7 @@ urMemImageCreateWithNativeHandle(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hMemory`
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
/// + `::UR_MEM_INFO_CONTEXT < propName`
/// + `::UR_MEM_INFO_REFERENCE_COUNT < 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 @@ -5592,6 +5592,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_mem_info_t value) {
case UR_MEM_INFO_CONTEXT:
os << "UR_MEM_INFO_CONTEXT";
break;
case UR_MEM_INFO_REFERENCE_COUNT:
os << "UR_MEM_INFO_REFERENCE_COUNT";
break;
default:
os << "unknown enumerator";
break;
Expand Down Expand Up @@ -5633,6 +5636,18 @@ inline ur_result_t printTagged(std::ostream &os, const void *ptr, ur_mem_info_t

os << ")";
} break;
case UR_MEM_INFO_REFERENCE_COUNT: {
const uint32_t *tptr = (const uint32_t *)ptr;
if (sizeof(uint32_t) > size) {
os << "invalid size (is: " << size << ", expected: >=" << sizeof(uint32_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
8 changes: 7 additions & 1 deletion scripts/core/memory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ etors:
desc: "[size_t] actual size of of memory object in bytes"
- name: CONTEXT
desc: "[$x_context_handle_t] context in which the memory object was created"
- name: REFERENCE_COUNT
desc: |
[uint32_t] Reference count of the memory 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.
--- #--------------------------------------------------------------------------
type: enum
desc: "Image channel order info: number of channels and the channel layout"
Expand Down Expand Up @@ -241,6 +246,7 @@ returns:
- $X_RESULT_ERROR_INVALID_CONTEXT
- $X_RESULT_ERROR_INVALID_VALUE
- $X_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR:
- "`pImageDesc && UR_STRUCTURE_TYPE_IMAGE_DESC != pImageDesc->stype`"
- "`pImageDesc && UR_MEM_TYPE_IMAGE1D_ARRAY < pImageDesc->type`"
- "`pImageDesc && pImageDesc->numMipLevel != 0`"
- "`pImageDesc && pImageDesc->numSamples != 0`"
Expand Down Expand Up @@ -503,7 +509,7 @@ returns:
type: function
desc: "Create runtime image memory object from native memory handle."
class: $xMem
name: ImageCreateWithNativeHandle
name: Image`ativeHandle
decl: static
ordinal: "0"
details:
Expand Down
3 changes: 3 additions & 0 deletions source/adapters/cuda/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemGetInfo(ur_mem_handle_t hMemory,
case UR_MEM_INFO_CONTEXT: {
return ReturnValue(hMemory->getContext());
}
case UR_MEM_INFO_REFERENCE_COUNT: {
return ReturnValue(hMemory->getReferenceCount());
}

default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
Expand Down
3 changes: 3 additions & 0 deletions source/adapters/hip/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemGetInfo(ur_mem_handle_t hMemory,
case UR_MEM_INFO_CONTEXT: {
return ReturnValue(hMemory->getContext());
}
case UR_MEM_INFO_REFERENCE_COUNT: {
return ReturnValue(hMemory->getReferenceCount());
}

default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
Expand Down
7 changes: 4 additions & 3 deletions source/adapters/level_zero/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1886,12 +1886,13 @@ ur_result_t urMemGetInfo(
// Get size of the allocation
return ReturnValue(size_t{Buffer->Size});
}
case UR_MEM_INFO_REFERENCE_COUNT: {
return ReturnValue(Buffer->RefCount.load());
}
default: {
die("urMemGetInfo: Parameter is not implemented");
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}
}

return UR_RESULT_SUCCESS;
}

ur_result_t urMemImageGetInfo(
Expand Down
2 changes: 2 additions & 0 deletions source/adapters/opencl/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ cl_int mapURMemInfoToCL(ur_mem_info_t URPropName) {
return CL_MEM_SIZE;
case UR_MEM_INFO_CONTEXT:
return CL_MEM_CONTEXT;
case UR_MEM_INFO_REFERENCE_COUNT:
return CL_MEM_REFERENCE_COUNT;
default:
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions source/adapters/opencl/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ urGetMemProcAddrTable(ur_api_version_t Version, ur_mem_dditable_t *pDdiTable) {
pDdiTable->pfnGetInfo = urMemGetInfo;
pDdiTable->pfnGetNativeHandle = urMemGetNativeHandle;
pDdiTable->pfnImageCreate = urMemImageCreate;
pDdiTable->pfnImageCreateWithNativeHandle = urMemImageCreateWithNativeHandle;
pDdiTable->pfnImageGetInfo = urMemImageGetInfo;
pDdiTable->pfnRelease = urMemRelease;
pDdiTable->pfnRetain = urMemRetain;
Expand Down
6 changes: 5 additions & 1 deletion source/loader/layers/validation/ur_valddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,10 @@ __urdlllocal ur_result_t UR_APICALL urMemImageCreate(
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}

if (pImageDesc && UR_STRUCTURE_TYPE_IMAGE_DESC != pImageDesc->stype) {
return UR_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR;
}

if (pImageDesc && UR_MEM_TYPE_IMAGE1D_ARRAY < pImageDesc->type) {
return UR_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR;
}
Expand Down Expand Up @@ -1503,7 +1507,7 @@ __urdlllocal ur_result_t UR_APICALL urMemGetInfo(
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
}

if (UR_MEM_INFO_CONTEXT < propName) {
if (UR_MEM_INFO_REFERENCE_COUNT < propName) {
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}

Expand Down
3 changes: 2 additions & 1 deletion source/loader/ur_libapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,7 @@ ur_result_t UR_APICALL urContextSetExtendedDeleter(
/// - ::UR_RESULT_ERROR_INVALID_CONTEXT
/// - ::UR_RESULT_ERROR_INVALID_VALUE
/// - ::UR_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR
/// + `pImageDesc && UR_STRUCTURE_TYPE_IMAGE_DESC != pImageDesc->stype`
/// + `pImageDesc && UR_MEM_TYPE_IMAGE1D_ARRAY < pImageDesc->type`
/// + `pImageDesc && pImageDesc->numMipLevel != 0`
/// + `pImageDesc && pImageDesc->numSamples != 0`
Expand Down Expand Up @@ -1889,7 +1890,7 @@ ur_result_t UR_APICALL urMemImageCreateWithNativeHandle(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hMemory`
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
/// + `::UR_MEM_INFO_CONTEXT < propName`
/// + `::UR_MEM_INFO_REFERENCE_COUNT < propName`
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
/// + If `propName` is not supported by the adapter.
/// - ::UR_RESULT_ERROR_INVALID_SIZE
Expand Down
3 changes: 2 additions & 1 deletion source/ur_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,7 @@ ur_result_t UR_APICALL urContextSetExtendedDeleter(
/// - ::UR_RESULT_ERROR_INVALID_CONTEXT
/// - ::UR_RESULT_ERROR_INVALID_VALUE
/// - ::UR_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR
/// + `pImageDesc && UR_STRUCTURE_TYPE_IMAGE_DESC != pImageDesc->stype`
/// + `pImageDesc && UR_MEM_TYPE_IMAGE1D_ARRAY < pImageDesc->type`
/// + `pImageDesc && pImageDesc->numMipLevel != 0`
/// + `pImageDesc && pImageDesc->numSamples != 0`
Expand Down Expand Up @@ -1635,7 +1636,7 @@ ur_result_t UR_APICALL urMemImageCreateWithNativeHandle(
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hMemory`
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
/// + `::UR_MEM_INFO_CONTEXT < propName`
/// + `::UR_MEM_INFO_REFERENCE_COUNT < propName`
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
/// + If `propName` is not supported by the adapter.
/// - ::UR_RESULT_ERROR_INVALID_SIZE
Expand Down
2 changes: 2 additions & 0 deletions test/conformance/memory/memory_adapter_native_cpu.match
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,5 @@ urMemImageCreateTestWithImageFormatParam.Success/SYCL_NATIVE_CPU___SYCL_Native_C
urMemImageCreateTestWithImageFormatParam.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}}__UR_IMAGE_CHANNEL_ORDER_SRGBA__UR_IMAGE_CHANNEL_TYPE_FLOAT
urMemReleaseTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}}
urMemRetainTest.Success/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}}
urMemImageCreateWithNativeHandle.SuccessWithOwnedNativeHandle/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}}
urMemImageCreateWithNativeHandle.SuccessWithUnOwnedNativeHandle/SYCL_NATIVE_CPU___SYCL_Native_CPU__{{.*}}
2 changes: 0 additions & 2 deletions test/conformance/memory/memory_adapter_opencl.match
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
{{NONDETERMINISTIC}}
urMemImageCreateTest.InvalidImageDescStype/Intel_R__OpenCL___{{.*}}
47 changes: 40 additions & 7 deletions test/conformance/memory/urMemBufferCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ TEST_P(urMemBufferCreateWithHostPtrFlagsTest, SUCCESS) {
buffer.ptr()));
}

TEST_P(urMemBufferCreateWithHostPtrFlagsTest, InvalidHostPtr) {
uur::raii::Mem buffer = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_HOST_PTR,
urMemBufferCreate(context, getParam(), 4096, nullptr, buffer.ptr()));
}

TEST_P(urMemBufferCreateWithFlagsTest, InvalidNullPointerBuffer) {
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_POINTER,
Expand All @@ -79,3 +72,43 @@ TEST_P(urMemBufferCreateWithFlagsTest, InvalidBufferSizeZero) {
UR_RESULT_ERROR_INVALID_BUFFER_SIZE,
urMemBufferCreate(context, getParam(), 0, nullptr, buffer.ptr()));
}

struct urMemBufferCreateWithHostPtrFlagsInvalidTest
: public urMemBufferCreateTest {
uur::raii::Mem buffer;
ur_mem_flags_t flags =
UR_MEM_FLAG_USE_HOST_POINTER | UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER;
ur_buffer_properties_t properties;
};

UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(
urMemBufferCreateWithHostPtrFlagsInvalidTest);

TEST_P(urMemBufferCreateWithHostPtrFlagsInvalidTest,
InvalidHostPtrNullProperties) {
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_HOST_PTR,
urMemBufferCreate(context, flags, 4096, nullptr, buffer.ptr()));
}

TEST_P(urMemBufferCreateWithHostPtrFlagsInvalidTest,
InvalidHostPtrValidPropertiesNullHostPtr) {
properties.pHost = nullptr;

ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_HOST_PTR,
urMemBufferCreate(context, flags, 4096, &properties, buffer.ptr()));
}

TEST_P(urMemBufferCreateWithHostPtrFlagsInvalidTest,
InvalidHostPtrValidPropertiesValidHostPtr) {
flags = 0;

properties.pHost = nullptr;
int host = 42;
properties.pHost = &host;

ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_HOST_PTR,
urMemBufferCreate(context, flags, 4096, &properties, buffer.ptr()));
}
89 changes: 83 additions & 6 deletions test/conformance/memory/urMemBufferCreateWithNativeHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ TEST_P(urMemBufferCreateWithNativeHandleTest, Success) {
// We can however convert the native_handle back into a unified-runtime handle
// and perform some query on it to verify that it works.
ur_mem_handle_t mem = nullptr;
ur_mem_native_properties_t props = {
/*.stype =*/UR_STRUCTURE_TYPE_MEM_NATIVE_PROPERTIES,
/*.pNext =*/nullptr,
/*.isNativeHandleOwned =*/false,
};
UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(
urMemBufferCreateWithNativeHandle(hNativeMem, context, &props, &mem));
urMemBufferCreateWithNativeHandle(hNativeMem, context, nullptr, &mem));
ASSERT_NE(mem, nullptr);

size_t alloc_size = 0;
Expand All @@ -35,3 +30,85 @@ TEST_P(urMemBufferCreateWithNativeHandleTest, Success) {

ASSERT_SUCCESS(urMemRelease(mem));
}

TEST_P(urMemBufferCreateWithNativeHandleTest, SuccessWithOwnedNativeHandle) {
ur_native_handle_t native_handle = 0;
{
UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(
urMemGetNativeHandle(buffer, device, &native_handle));
}

ur_mem_handle_t mem = nullptr;
ur_mem_native_properties_t props = {
/*.stype =*/UR_STRUCTURE_TYPE_MEM_NATIVE_PROPERTIES,
/*.pNext =*/nullptr,
/*.isNativeHandleOwned =*/true,
};
UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(urMemBufferCreateWithNativeHandle(
native_handle, context, &props, &mem));
ASSERT_NE(nullptr, mem);

ur_context_handle_t mem_context = nullptr;
ASSERT_SUCCESS(urMemGetInfo(mem, UR_MEM_INFO_CONTEXT,
sizeof(ur_context_handle_t), &mem_context,
nullptr));
ASSERT_EQ(context, mem_context);
}

TEST_P(urMemBufferCreateWithNativeHandleTest, SuccessWithUnOwnedNativeHandle) {
ur_native_handle_t native_handle = 0;
{
UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(
urMemGetNativeHandle(buffer, device, &native_handle));
}
ur_mem_handle_t mem = nullptr;
ur_mem_native_properties_t props = {
/*.stype =*/UR_STRUCTURE_TYPE_MEM_NATIVE_PROPERTIES,
/*.pNext =*/nullptr,
/*.isNativeHandleOwned =*/false,
};
UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(urMemBufferCreateWithNativeHandle(
native_handle, context, &props, &mem));
ASSERT_NE(nullptr, mem);

ur_context_handle_t mem_context = nullptr;
ASSERT_SUCCESS(urMemGetInfo(mem, UR_MEM_INFO_CONTEXT,
sizeof(ur_context_handle_t), &mem_context,
nullptr));
ASSERT_EQ(context, mem_context);
}

TEST_P(urMemBufferCreateWithNativeHandleTest, InvalidNullHandle) {
ur_native_handle_t hNativeMem = 0;
{
UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(
urMemGetNativeHandle(buffer, device, &hNativeMem));
}

ur_mem_handle_t mem = nullptr;
ur_mem_native_properties_t props = {
/*.stype =*/UR_STRUCTURE_TYPE_MEM_NATIVE_PROPERTIES,
/*.pNext =*/nullptr,
/*.isNativeHandleOwned =*/false,
};
ASSERT_EQ(
urMemBufferCreateWithNativeHandle(hNativeMem, nullptr, &props, &mem),
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
}

TEST_P(urMemBufferCreateWithNativeHandleTest, InvalidNullPointer) {
ur_native_handle_t hNativeMem = 0;
{
UUR_ASSERT_SUCCESS_OR_UNSUPPORTED(
urMemGetNativeHandle(buffer, device, &hNativeMem));
}

ur_mem_native_properties_t props = {
/*.stype =*/UR_STRUCTURE_TYPE_MEM_NATIVE_PROPERTIES,
/*.pNext =*/nullptr,
/*.isNativeHandleOwned =*/false,
};
ASSERT_EQ(
urMemBufferCreateWithNativeHandle(hNativeMem, context, &props, nullptr),
UR_RESULT_ERROR_INVALID_NULL_POINTER);
}
Loading
Loading