From 1b14c69decc3a092c22fb1f6b2f93cadbf50855e Mon Sep 17 00:00:00 2001 From: "Agarwal, Udit" Date: Mon, 19 Feb 2024 22:26:41 -0800 Subject: [PATCH 1/4] Return nullptr when allocation size is zero --- sycl/include/sycl/usm/usm_allocator.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sycl/include/sycl/usm/usm_allocator.hpp b/sycl/include/sycl/usm/usm_allocator.hpp index 6ae1da5a3e350..31d9c5ec9a955 100644 --- a/sycl/include/sycl/usm/usm_allocator.hpp +++ b/sycl/include/sycl/usm/usm_allocator.hpp @@ -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( aligned_alloc(getAlignment(), NumberOfElements * sizeof(value_type), MDevice, MContext, AllocKind, MPropList, CodeLoc)); From 454156be5eb82bf54fc80949bab10722aac02f8c Mon Sep 17 00:00:00 2001 From: "Agarwal, Udit" Date: Tue, 20 Feb 2024 09:03:55 -0800 Subject: [PATCH 2/4] Added test case --- .../USM/usm_allocator_zero_allocation.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp diff --git a/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp b/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp new file mode 100644 index 0000000000000..fa54f25ccd32b --- /dev/null +++ b/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp @@ -0,0 +1,25 @@ +// RUN: %{build} -o %t.out +// RUN: %{run} %t.out + +#include + +using namespace sycl; +#define ALLOC_SIZE 0 + +int main() { + queue q; + auto dev = q.get_device(); + auto ctxt = q.get_context(); + + if (dev.get_info()) { + sycl::usm_allocator ua{ctxt, dev}; + int *p = ua.allocate(ALLOC_SIZE); + + assert(!p && "usm_allocator should return a null pointer when allocation " + "size is zero."); + + ua.deallocate(p, ALLOC_SIZE); + } + + return 0; +} From 2145b66d5fff13ac4218fde5afb146c22797144c Mon Sep 17 00:00:00 2001 From: "Agarwal, Udit" Date: Tue, 20 Feb 2024 12:53:44 -0800 Subject: [PATCH 3/4] Fix test case --- .../USM/usm_allocator_zero_allocation.cpp | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp b/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp index fa54f25ccd32b..3cda72a03f064 100644 --- a/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp +++ b/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp @@ -4,21 +4,27 @@ #include using namespace sycl; -#define ALLOC_SIZE 0 + +template void test(queue &q) { + sycl::usm_allocator 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."); + + ua.deallocate(p, 0); +} int main() { queue q; auto dev = q.get_device(); - auto ctxt = q.get_context(); if (dev.get_info()) { - sycl::usm_allocator ua{ctxt, dev}; - int *p = ua.allocate(ALLOC_SIZE); - - assert(!p && "usm_allocator should return a null pointer when allocation " - "size is zero."); - - ua.deallocate(p, ALLOC_SIZE); + test(q); + } + if (dev.get_info()) { + test(q); } return 0; From 7f1e0d3c67cc74e9ce21c61a160c796facb2d831 Mon Sep 17 00:00:00 2001 From: "Agarwal, Udit" Date: Tue, 20 Feb 2024 19:24:48 -0800 Subject: [PATCH 4/4] Address reviews --- sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp b/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp index 3cda72a03f064..b8aace8933c28 100644 --- a/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp +++ b/sycl/test-e2e/USM/usm_allocator_zero_allocation.cpp @@ -10,8 +10,7 @@ template void test(queue &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."); + "null pointer when allocation size is zero."); ua.deallocate(p, 0); } @@ -20,10 +19,10 @@ int main() { queue q; auto dev = q.get_device(); - if (dev.get_info()) { + if (dev.has(aspect::usm_host_allocations)) { test(q); } - if (dev.get_info()) { + if (dev.has(aspect::usm_shared_allocations)) { test(q); }