Skip to content

[SYCL][NativeCPU] Fix alignment of global and local memory. #19076

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

Merged
merged 1 commit into from
Jun 19, 2025
Merged
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
14 changes: 14 additions & 0 deletions unified-runtime/source/adapters/native_cpu/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ inline void *aligned_malloc(size_t alignment, size_t size) {
return ptr;
}

// In many cases we require aligned memory without being told what the alignment
// requirement is. This helper function returns maximally aligned memory based
// on the size.
inline void *aligned_malloc(size_t size) {
constexpr size_t max_alignment = 16 * sizeof(double);
size_t alignment = max_alignment;
while (alignment > size) {
alignment >>= 1;
}
// aligned_malloc requires size to be a multiple of alignment; round up.
size = (size + alignment - 1) & ~(alignment - 1);
return aligned_malloc(alignment, size);
}

inline void aligned_free(void *ptr) {
#ifdef _MSC_VER
_aligned_free(ptr);
Expand Down
13 changes: 4 additions & 9 deletions unified-runtime/source/adapters/native_cpu/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct ur_kernel_handle_t_ : RefCounted {

~ur_kernel_handle_t_() {
removeArgReferences();
free(_localMemPool);
native_cpu::aligned_free(_localMemPool);
}

ur_kernel_handle_t_(ur_program_handle_t hProgram, const char *name,
Expand All @@ -59,7 +59,6 @@ struct ur_kernel_handle_t_ : RefCounted {
args_index_t Indices;
std::vector<size_t> ParamSizes;
std::vector<bool> OwnsMem;
static constexpr size_t MaxAlign = 16 * sizeof(double);

arguments() = default;

Expand Down Expand Up @@ -109,11 +108,7 @@ struct ur_kernel_handle_t_ : RefCounted {
}
}
if (NeedAlloc) {
size_t Align = MaxAlign;
while (Align > Size) {
Align >>= 1;
}
Indices[Index] = native_cpu::aligned_malloc(Align, Size);
Indices[Index] = native_cpu::aligned_malloc(Size);
ParamSizes[Index] = Size;
OwnsMem[Index] = true;
}
Expand Down Expand Up @@ -158,8 +153,8 @@ struct ur_kernel_handle_t_ : RefCounted {
if (reqSize == 0 || reqSize == _localMemPoolSize) {
return;
}
// realloc handles nullptr case
_localMemPool = (char *)realloc(_localMemPool, reqSize);
native_cpu::aligned_free(_localMemPool);
_localMemPool = static_cast<char *>(native_cpu::aligned_malloc(reqSize));
_localMemPoolSize = reqSize;
}

Expand Down
9 changes: 4 additions & 5 deletions unified-runtime/source/adapters/native_cpu/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@

struct ur_mem_handle_t_ : ur_object {
ur_mem_handle_t_(size_t Size, bool _IsImage)
: _mem{static_cast<char *>(malloc(Size))}, _ownsMem{true},
IsImage{_IsImage} {}
: _mem{static_cast<char *>(native_cpu::aligned_malloc(Size))},
_ownsMem{true}, IsImage{_IsImage} {}

ur_mem_handle_t_(void *HostPtr, size_t Size, bool _IsImage)
: _mem{static_cast<char *>(malloc(Size))}, _ownsMem{true},
IsImage{_IsImage} {
: ur_mem_handle_t_(Size, _IsImage) {
memcpy(_mem, HostPtr, Size);
}

Expand All @@ -34,7 +33,7 @@ struct ur_mem_handle_t_ : ur_object {

~ur_mem_handle_t_() {
if (_ownsMem) {
free(_mem);
native_cpu::aligned_free(_mem);
}
}

Expand Down