Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
43 changes: 23 additions & 20 deletions source/adapters/cuda/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,
return USMFreeImpl(hContext, pMem);
}

ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t,
ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t hContext,
ur_device_handle_t Device,
ur_usm_device_mem_flags_t, size_t Size,
uint32_t Alignment) {
Expand All @@ -141,12 +141,13 @@ ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t,
return Err;
}

#ifdef NDEBUG
std::ignore = Alignment;
#else
assert((Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0));
#endif
const bool validAlignment =
(Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0);
if (!validAlignment) {
urUSMFree(hContext, *ResultPtr);
return UR_RESULT_ERROR_INVALID_VALUE;
}
return UR_RESULT_SUCCESS;
}

Expand Down Expand Up @@ -175,16 +176,17 @@ ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t Context,
return Err;
}

#ifdef NDEBUG
std::ignore = Alignment;
#else
assert((Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0));
#endif
const bool validAlignment =
(Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0);
if (!validAlignment) {
urUSMFree(Context, *ResultPtr);
return UR_RESULT_ERROR_INVALID_VALUE;
}
return UR_RESULT_SUCCESS;
}

ur_result_t USMHostAllocImpl(void **ResultPtr, ur_context_handle_t,
ur_result_t USMHostAllocImpl(void **ResultPtr, ur_context_handle_t hContext,
ur_usm_host_mem_flags_t, size_t Size,
uint32_t Alignment) {
try {
Expand All @@ -193,12 +195,13 @@ ur_result_t USMHostAllocImpl(void **ResultPtr, ur_context_handle_t,
return Err;
}

#ifdef NDEBUG
std::ignore = Alignment;
#else
assert((Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0));
#endif
const bool validAlignment =
(Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0);
if (!validAlignment) {
urUSMFree(hContext, *ResultPtr);
return UR_RESULT_ERROR_INVALID_VALUE;
}
return UR_RESULT_SUCCESS;
}

Expand Down
31 changes: 20 additions & 11 deletions source/adapters/hip/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,
}
}

ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t,
ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t hContext,
ur_device_handle_t Device,
ur_usm_device_mem_flags_t, size_t Size,
[[maybe_unused]] uint32_t Alignment) {
Expand All @@ -114,11 +114,14 @@ ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t,
return Err;
}

assert(checkUSMImplAlignment(Alignment, ResultPtr));
if (!checkUSMImplAlignment(Alignment, ResultPtr)) {
urUSMFree(hContext, *ResultPtr);
return UR_RESULT_ERROR_INVALID_VALUE;
}
return UR_RESULT_SUCCESS;
}

ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t,
ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t hContext,
ur_device_handle_t Device,
ur_usm_host_mem_flags_t,
ur_usm_device_mem_flags_t, size_t Size,
Expand All @@ -130,12 +133,15 @@ ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t,
return Err;
}

assert(checkUSMImplAlignment(Alignment, ResultPtr));
if (!checkUSMImplAlignment(Alignment, ResultPtr)) {
urUSMFree(hContext, *ResultPtr);
return UR_RESULT_ERROR_INVALID_VALUE;
}
return UR_RESULT_SUCCESS;
}

ur_result_t USMHostAllocImpl(void **ResultPtr,
[[maybe_unused]] ur_context_handle_t Context,
[[maybe_unused]] ur_context_handle_t hContext,
ur_usm_host_mem_flags_t, size_t Size,
[[maybe_unused]] uint32_t Alignment) {
try {
Expand All @@ -144,7 +150,10 @@ ur_result_t USMHostAllocImpl(void **ResultPtr,
return Err;
}

assert(checkUSMImplAlignment(Alignment, ResultPtr));
if (!checkUSMImplAlignment(Alignment, ResultPtr)) {
urUSMFree(hContext, *ResultPtr);
return UR_RESULT_ERROR_INVALID_VALUE;
}
return UR_RESULT_SUCCESS;
}

Expand Down Expand Up @@ -372,11 +381,11 @@ bool ur_usm_pool_handle_t_::hasUMFPool(umf_memory_pool_t *umf_pool) {
}

UR_APIEXPORT ur_result_t UR_APICALL urUSMPoolCreate(
ur_context_handle_t Context, ///< [in] handle of the context object
ur_usm_pool_desc_t
*PoolDesc, ///< [in] pointer to USM pool descriptor. Can be chained with
///< ::ur_usm_pool_limits_desc_t
ur_usm_pool_handle_t *Pool ///< [out] pointer to USM memory pool
ur_context_handle_t Context, ///< [in] handle of the context object
ur_usm_pool_desc_t *PoolDesc, ///< [in] pointer to USM pool descriptor.
///< Can be chained with
///< ::ur_usm_pool_limits_desc_t
ur_usm_pool_handle_t *Pool ///< [out] pointer to USM memory pool
) {
// Without pool tracking we can't free pool allocations.
#ifdef UMF_ENABLE_POOL_TRACKING
Expand Down
24 changes: 15 additions & 9 deletions source/adapters/opencl/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,

*ppMem = Ptr;

assert((Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ppMem) % Alignment == 0) &&
"Allocation not aligned correctly!");
if (!(Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ppMem) % Alignment == 0)) {
urUSMFree(hContext, Ptr);
return UR_RESULT_ERROR_INVALID_VALUE;
}

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -158,9 +160,11 @@ urUSMDeviceAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,

*ppMem = Ptr;

assert((Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ppMem) % Alignment == 0) &&
"Allocation not aligned correctly!");
if (!(Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ppMem) % Alignment == 0)) {
urUSMFree(hContext, Ptr);
return UR_RESULT_ERROR_INVALID_VALUE;
}

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -201,9 +205,11 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,

*ppMem = Ptr;

assert((Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ppMem) % Alignment == 0) &&
"Allocation not aligned correctly!");
if (!(Alignment == 0 ||
reinterpret_cast<std::uintptr_t>(*ppMem) % Alignment == 0)) {
urUSMFree(hContext, Ptr);
return UR_RESULT_ERROR_INVALID_VALUE;
}
return UR_RESULT_SUCCESS;
}

Expand Down