Skip to content

Commit f009896

Browse files
authored
[UR] Check for memory alignment before calling clEnqueueMemFillINTEL_fn in OpenCL (#18423)
Fixes oneapi-src/unified-runtime#2440 [clEnqueueMemFillINTEL returns CL_INVALID_VALUE if dst_ptr is NULL, or if dst_ptr is not aligned to pattern_size bytes. ](https://registry.khronos.org/OpenCL/extensions/intel/cl_intel_unified_shared_memory.html) This PR adds a memory alignment check before calling `clEnqueueMemFillINTEL` to ensure we can safely call it, otherwise take the host side copy path. Re-enables the `USM/fill_any_size.cpp` and also moves `checkUSMImplAlignment` from Hip into a common header so it can be used in both Hip and OpenCL.
1 parent 781b312 commit f009896

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed

sycl/test-e2e/USM/fill_any_size.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// RUN: %{build} -o %t1.out
22
// RUN: %{run} %t1.out
33
// clang-format off
4-
// UNSUPPORTED: opencl
5-
// UNSUPPORTED-TRACKER: https://github.com/oneapi-src/unified-runtime/issues/2440
64
// clang-format on
75

86
/**

unified-runtime/source/adapters/hip/usm.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t,
144144
return Err;
145145
}
146146

147-
assert(checkUSMImplAlignment(Alignment, ResultPtr));
147+
assert(isPointerAlignedTo(Alignment, *ResultPtr));
148148
return UR_RESULT_SUCCESS;
149149
}
150150

@@ -160,7 +160,7 @@ ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t,
160160
return Err;
161161
}
162162

163-
assert(checkUSMImplAlignment(Alignment, ResultPtr));
163+
assert(isPointerAlignedTo(Alignment, *ResultPtr));
164164
return UR_RESULT_SUCCESS;
165165
}
166166

@@ -174,7 +174,7 @@ ur_result_t USMHostAllocImpl(void **ResultPtr,
174174
return Err;
175175
}
176176

177-
assert(checkUSMImplAlignment(Alignment, ResultPtr));
177+
assert(isPointerAlignedTo(Alignment, *ResultPtr));
178178
return UR_RESULT_SUCCESS;
179179
}
180180

@@ -485,11 +485,6 @@ bool checkUSMAlignment(uint32_t &alignment, const ur_usm_desc_t *pUSMDesc) {
485485
(alignment == 0 || ((alignment & (alignment - 1)) == 0)));
486486
}
487487

488-
bool checkUSMImplAlignment(uint32_t Alignment, void **ResultPtr) {
489-
return Alignment == 0 ||
490-
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0;
491-
}
492-
493488
UR_APIEXPORT ur_result_t UR_APICALL urUSMPoolCreateExp(ur_context_handle_t,
494489
ur_device_handle_t,
495490
ur_usm_pool_desc_t *,

unified-runtime/source/adapters/hip/usm.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,3 @@ ur_result_t USMHostAllocImpl(void **ResultPtr, ur_context_handle_t Context,
120120
uint32_t Alignment);
121121

122122
bool checkUSMAlignment(uint32_t &alignment, const ur_usm_desc_t *pUSMDesc);
123-
124-
bool checkUSMImplAlignment(uint32_t Alignment, void **ResultPtr);

unified-runtime/source/adapters/opencl/usm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMFill(
269269
// Have to look up the context from the kernel
270270
cl_context CLContext = hQueue->Context->CLContext;
271271

272-
if (patternSize <= 128 && isPowerOf2(patternSize)) {
272+
if (patternSize <= 128 && isPowerOf2(patternSize) &&
273+
isPointerAlignedTo(patternSize, ptr)) {
273274
clEnqueueMemFillINTEL_fn EnqueueMemFill = nullptr;
274275
UR_RETURN_ON_FAILURE(
275276
cl_ext::getExtFuncFromContext<clEnqueueMemFillINTEL_fn>(

unified-runtime/source/common/ur_util.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,9 @@ static inline std::string groupDigits(Numeric numeric) {
548548

549549
template <typename T> Spinlock<Rc<T>> AtomicSingleton<T>::instance;
550550

551+
inline bool isPointerAlignedTo(uint32_t Alignment, void *Ptr) {
552+
return Alignment == 0 ||
553+
reinterpret_cast<std::uintptr_t>(Ptr) % Alignment == 0;
554+
}
555+
551556
#endif /* UR_UTIL_H */

0 commit comments

Comments
 (0)