Skip to content

[SYCL] Fix returned alignment of allocation functions #6205

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 2 commits into from
May 26, 2022
Merged
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
32 changes: 20 additions & 12 deletions sycl/include/CL/sycl/usm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ T *malloc_device(
const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(
malloc_device(Count * sizeof(T), Dev, Ctxt, PropList, CodeLoc));
aligned_alloc_device(std::max(alignof(T), alignof(std::max_align_t)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmlueck , after reading your comment in the llvm-test-suite PR I think this is overly pessimistic and should be just alignof(T) without any std::max. Would you agree?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are correct. In fact, the spec says this:

No alignment parameter; Templated on allocation type T
Pointer is suitably aligned for an object of type T.

Thanks for noticing.

Count * sizeof(T), Dev, Ctxt, PropList, CodeLoc));
}

template <typename T>
Expand All @@ -174,8 +175,9 @@ T *aligned_alloc_device(
size_t Alignment, size_t Count, const device &Dev, const context &Ctxt,
const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(aligned_alloc_device(Alignment, Count * sizeof(T),
Dev, Ctxt, PropList, CodeLoc));
return static_cast<T *>(aligned_alloc_device(std::max(Alignment, alignof(T)),
Count * sizeof(T), Dev, Ctxt,
PropList, CodeLoc));
}

template <typename T>
Expand All @@ -192,7 +194,8 @@ T *malloc_host(
size_t Count, const context &Ctxt, const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(
malloc_host(Count * sizeof(T), Ctxt, PropList, CodeLoc));
aligned_alloc_host(std::max(alignof(T), alignof(std::max_align_t)),
Count * sizeof(T), Ctxt, PropList, CodeLoc));
}

template <typename T>
Expand All @@ -208,7 +211,8 @@ T *malloc_shared(
const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(
malloc_shared(Count * sizeof(T), Dev, Ctxt, PropList, CodeLoc));
aligned_alloc_shared(std::max(alignof(T), alignof(std::max_align_t)),
Count * sizeof(T), Dev, Ctxt, PropList, CodeLoc));
}

template <typename T>
Expand All @@ -224,8 +228,9 @@ T *aligned_alloc_host(
size_t Alignment, size_t Count, const context &Ctxt,
const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(aligned_alloc_host(Alignment, Count * sizeof(T), Ctxt,
PropList, CodeLoc));
return static_cast<T *>(aligned_alloc_host(std ::max(Alignment, alignof(T)),
Count * sizeof(T), Ctxt, PropList,
CodeLoc));
}

template <typename T>
Expand All @@ -242,8 +247,9 @@ T *aligned_alloc_shared(
size_t Alignment, size_t Count, const device &Dev, const context &Ctxt,
const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(aligned_alloc_shared(Alignment, Count * sizeof(T),
Dev, Ctxt, PropList, CodeLoc));
return static_cast<T *>(aligned_alloc_shared(std::max(Alignment, alignof(T)),
Count * sizeof(T), Dev, Ctxt,
PropList, CodeLoc));
}

template <typename T>
Expand All @@ -261,7 +267,8 @@ T *malloc(
const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(
malloc(Count * sizeof(T), Dev, Ctxt, Kind, PropList, CodeLoc));
aligned_alloc(std::max(alignof(T), alignof(std::max_align_t)),
Count * sizeof(T), Dev, Ctxt, Kind, PropList, CodeLoc));
}

template <typename T>
Expand All @@ -278,8 +285,9 @@ T *aligned_alloc(
size_t Alignment, size_t Count, const device &Dev, const context &Ctxt,
usm::alloc Kind, const property_list &PropList = {},
const detail::code_location CodeLoc = detail::code_location::current()) {
return static_cast<T *>(aligned_alloc(Alignment, Count * sizeof(T), Dev, Ctxt,
Kind, PropList, CodeLoc));
return static_cast<T *>(aligned_alloc(std::max(Alignment, alignof(T)),
Count * sizeof(T), Dev, Ctxt, Kind,
PropList, CodeLoc));
}

template <typename T>
Expand Down