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

[SYCL] Return nullptr when allocation size is zero in usm allocator #12765

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions sycl/include/sycl/usm/usm_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class usm_allocator {
T *allocate(size_t NumberOfElements, const detail::code_location CodeLoc =
detail::code_location::current()) {

if (!NumberOfElements)
return nullptr;

auto Result = reinterpret_cast<T *>(
aligned_alloc(getAlignment(), NumberOfElements * sizeof(value_type),
MDevice, MContext, AllocKind, MPropList, CodeLoc));
Expand Down
31 changes: 31 additions & 0 deletions sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out

#include <sycl/sycl.hpp>

using namespace sycl;

template <usm::alloc alloc_kind> void test(queue &q) {
sycl::usm_allocator<int, alloc_kind> ua(q);
int *p = ua.allocate(0);

assert(!p && "Our implementation of usm_allocator is expected to return a "
"null pointer when allocation "
"size is zero.");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine, but another option is to simply remove the assert. From an API perspective, the important thing is that you can call allocate with a zero size and that you can then call deallocate on the returned pointer, all without any exception being raised. It's not important whether or not the returned pointer is null.

uditagarwal97 marked this conversation as resolved.
Show resolved Hide resolved

ua.deallocate(p, 0);
}

int main() {
queue q;
auto dev = q.get_device();

if (dev.get_info<info::device::usm_host_allocations>()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we only test "host" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how adding more allocation types will increase the test coverage of this change. Nevertheless, I've expanded the test to also check for zero-sized shared allocations. usm::alloc::device is not supported by usm_allocator so, I've skipped that.

uditagarwal97 marked this conversation as resolved.
Show resolved Hide resolved
test<usm::alloc::host>(q);
}
if (dev.get_info<info::device::usm_shared_allocations>()) {
uditagarwal97 marked this conversation as resolved.
Show resolved Hide resolved
test<usm::alloc::shared>(q);
}

return 0;
}
Loading