From 1d81e4c625fea0156ead5c4c819f82915ee5b241 Mon Sep 17 00:00:00 2001 From: "Larsen, Steffen" Date: Wed, 27 Jul 2022 05:05:50 -0700 Subject: [PATCH 1/3] [SYCL][ABI-break] Promote guarded SYCL 2020 features and fix buffer reinterpret Promotes all SYCL 2020 features currently guarded by the SYCL2020_CONFORMANT_APIS macro. Additionally, `buffer::reinterpret` is changed to correctly rebind the allocator. To accomplish this, `std::allocator_traits` is specialized for `aligned_allocator`. Signed-off-by: Larsen, Steffen --- sycl/CMakeLists.txt | 2 +- sycl/doc/PreprocessorMacros.md | 19 +----- sycl/include/sycl/backend.hpp | 42 +------------ sycl/include/sycl/buffer.hpp | 36 +++++------ .../include/sycl/detail/aligned_allocator.hpp | 61 ++++++++++++++++++- .../sycl/detail/backend_traits_opencl.hpp | 19 ------ .../sycl/detail/sycl_mem_obj_allocator.hpp | 4 +- .../sycl/ext/oneapi/backend/level_zero.hpp | 4 +- sycl/test/abi/layout_buffer.cpp | 2 +- sycl/test/abi/user_mangling.cpp | 2 +- .../check_vector_of_opencl_event.cpp | 2 +- .../scheduler/NoHostUnifiedMemory.cpp | 4 +- 12 files changed, 87 insertions(+), 110 deletions(-) diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt index 6408a74388b14..2226f67df4e70 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -28,7 +28,7 @@ include(SYCLUtils) set(SYCL_MAJOR_VERSION 5) set(SYCL_MINOR_VERSION 7) set(SYCL_PATCH_VERSION 0) -set(SYCL_DEV_ABI_VERSION 4) +set(SYCL_DEV_ABI_VERSION 5) if (SYCL_ADD_DEV_VERSION_POSTFIX) set(SYCL_VERSION_POSTFIX "-${SYCL_DEV_ABI_VERSION}") endif() diff --git a/sycl/doc/PreprocessorMacros.md b/sycl/doc/PreprocessorMacros.md index ee77e20181f14..899897bd265af 100644 --- a/sycl/doc/PreprocessorMacros.md +++ b/sycl/doc/PreprocessorMacros.md @@ -55,24 +55,7 @@ This file describes macros that have effect on SYCL compiler and run-time. - **SYCL2020_CONFORMANT_APIS** This macro is used to comply with the SYCL 2020 specification, as some of the current implementations may be widespread and not conform to it. - Description of what it changes: - 1) According to spec, `backend_return_t` for opencl event - should be `std::vector` instead of `cl_event`. Defining this macro - will change the behavior of `sycl::get_native()` function and using types for - next structs: `interop`, `BackendInput`, - `BackendReturn` to be in line with the spec. - 2) According to spec, `backend_return_t` for opencl buffer - should be `std::vector` instead of `cl_mem`. Defining this macro - will change the behavior of `interop_handle::get_native_mem()` and `sycl::get_native()` functions - and using type for `BackendReturn` to be in line with the spec. - 3) According to spec, `sycl::buffer_allocator` should be a template class taking a single - type parameter denoting the data type of the associated buffer. Likewise, `sycl::buffer` - with that take an allocator as a constructor argument should use - `sycl::buffer_allocator>` by default, where `T` is the data type of - that buffer. Defining this macro will change the definition of `sycl::buffer_allocator` to - be templated and `sycl::buffer` will be using `sycl::buffer_allocator>` - by default, where `T` is the data type of that buffer, if it is not explicitly given an - allocator. + Defining this macro currently has no effect on the API. ## Version macros diff --git a/sycl/include/sycl/backend.hpp b/sycl/include/sycl/backend.hpp index 611c794c9a8f6..79c3807b1bc21 100644 --- a/sycl/include/sycl/backend.hpp +++ b/sycl/include/sycl/backend.hpp @@ -86,7 +86,6 @@ struct BufferInterop { } }; -#ifdef SYCL2020_CONFORMANT_APIS template struct BufferInterop { using ReturnType = @@ -101,7 +100,6 @@ struct BufferInterop { return ReturnValue; } }; -#endif template @@ -143,30 +141,12 @@ auto get_native(const kernel_bundle &Obj) } template * = nullptr> -#ifndef SYCL2020_CONFORMANT_APIS -__SYCL_DEPRECATED( - "get_native, which return type " - "cl_mem is deprecated. According to SYCL 2020 spec, please define " - "SYCL2020_CONFORMANT_APIS and use vector instead.") -#endif -auto get_native(const buffer &Obj) - -> backend_return_t> { - return detail::get_native_buffer(Obj); -} - -template * = nullptr> + typename AllocatorT> auto get_native(const buffer &Obj) -> backend_return_t> { return detail::get_native_buffer(Obj); } -// define SYCL2020_CONFORMANT_APIS to correspond SYCL 2020 spec and return -// vector from get_native instead of just cl_event -#ifdef SYCL2020_CONFORMANT_APIS template <> inline backend_return_t get_native(const event &Obj) { @@ -184,24 +164,6 @@ get_native(const event &Obj) { } return ReturnValue; } -#else -// Specialization for cl_event with deprecation message -template <> -__SYCL_DEPRECATED( - "get_native, which return type is " - "cl_event is deprecated. According to SYCL 2020 spec, please define " - "SYCL2020_CONFORMANT_APIS and use vector instead.") -inline backend_return_t get_native< - backend::opencl, event>(const event &Obj) { - // TODO use SYCL 2020 exception when implemented - if (Obj.get_backend() != backend::opencl) { - throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch", - PI_ERROR_INVALID_OPERATION); - } - return reinterpret_cast< - typename detail::interop::type>(Obj.getNative()); -} -#endif // Native handle of an accessor should be accessed through interop_handler template > + typename AllocatorT = buffer_allocator>> typename std::enable_if::MakeBuffer == true && Backend != backend::ext_oneapi_level_zero, diff --git a/sycl/include/sycl/buffer.hpp b/sycl/include/sycl/buffer.hpp index b640834d41d58..eeba24ccd9868 100644 --- a/sycl/include/sycl/buffer.hpp +++ b/sycl/include/sycl/buffer.hpp @@ -23,27 +23,11 @@ class handler; class queue; template class range; -// Guard SYCL 2020 buffer_allocator with template arguments behind the -// SYCL2020_CONFORMANT_APIS macro. -#ifdef SYCL2020_CONFORMANT_APIS template using buffer_allocator = detail::sycl_memory_object_allocator; -#else -using buffer_allocator = detail::sycl_memory_object_allocator; -#endif namespace detail { -// Generalized implementation of the default allocator used by buffers. -// TODO: When the SYCL 1.2.1 version of buffer_allocator is removed, this should -// be removed. -#ifdef SYCL2020_CONFORMANT_APIS -template -using default_buffer_allocator = buffer_allocator>; -#else -template using default_buffer_allocator = buffer_allocator; -#endif - template buffer make_buffer_helper(pi_native_handle Handle, const context &Ctx, event Evt = {}, @@ -59,7 +43,7 @@ auto get_native_buffer(const buffer &Obj) buffer>; template > + typename AllocatorT = buffer_allocator>> struct BufferInterop; } // namespace detail @@ -72,7 +56,7 @@ struct BufferInterop; /// /// \ingroup sycl_api template , + typename AllocatorT = buffer_allocator>, typename __Enabled = typename detail::enable_if_t<(dimensions > 0) && (dimensions <= 3)>> class buffer { @@ -492,7 +476,9 @@ class buffer { bool is_sub_buffer() const { return IsSubBuffer; } template - buffer + buffer::template rebind_alloc< + ReinterpretT>> reinterpret(range reinterpretRange) const { if (sizeof(ReinterpretT) * reinterpretRange.size() != byte_size()) throw sycl::invalid_object_error( @@ -501,16 +487,22 @@ class buffer { "represented by the type and range of this SYCL buffer", PI_ERROR_INVALID_VALUE); - return buffer( + return buffer::template rebind_alloc>( impl, reinterpretRange, OffsetInBytes, IsSubBuffer); } template typename std::enable_if< (sizeof(ReinterpretT) == sizeof(T)) && (dimensions == ReinterpretDim), - buffer>::type + buffer::template rebind_alloc< + ReinterpretT>>>::type reinterpret() const { - return buffer( + return buffer::template rebind_alloc>( impl, get_range(), OffsetInBytes, IsSubBuffer); } diff --git a/sycl/include/sycl/detail/aligned_allocator.hpp b/sycl/include/sycl/detail/aligned_allocator.hpp index 6c16d68e8d215..4e47ce1546316 100644 --- a/sycl/include/sycl/detail/aligned_allocator.hpp +++ b/sycl/include/sycl/detail/aligned_allocator.hpp @@ -10,12 +10,12 @@ #include #include -#include #include #include #include #include +#include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { @@ -81,3 +81,62 @@ template class aligned_allocator { } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) + +namespace std { +template +struct allocator_traits> { + using allocator_type = typename cl::sycl::detail::aligned_allocator; + using value_type = typename allocator_type::value_type; + using pointer = typename allocator_type::pointer; + using const_pointer = typename allocator_type::const_pointer; + using void_pointer = + typename std::pointer_traits::template rebind; + using const_void_pointer = + typename std::pointer_traits::template rebind; + using difference_type = + typename std::pointer_traits::difference_type; + using size_type = typename std::make_unsigned::type; + using propagate_on_container_copy_assignment = std::false_type; + using propagate_on_container_move_assignment = std::false_type; + using propagate_on_container_swap = std::false_type; + using is_always_equal = typename std::is_empty::type; + + template + using rebind_alloc = typename cl::sycl::detail::aligned_allocator< + T>::template rebind::other; + template using rebind_traits = allocator_traits>; + + static pointer allocate(allocator_type &Allocator, size_type NumElems) { + return Allocator.allocate(NumElems); + } + + static pointer allocate(allocator_type &Allocator, size_type NumElems, + const_void_pointer) { + // TODO: Utilize the locality hint argument. + return Allocator.allocate(NumElems); + } + + static void deallocate(allocator_type &Allocator, pointer Ptr, + size_type NumElems) { + Allocator.deallocate(Ptr, NumElems); + } + + template + static void construct(allocator_type &Allocator, U *Ptr, ArgsT &&...Args) { + return Allocator.construct(Ptr, Args...); + } + + template static void destroy(allocator_type &Allocator, U *Ptr) { + Allocator.destroy(Ptr); + } + + static size_type max_size(const allocator_type &) noexcept { + return std::numeric_limits::max() / sizeof(value_type); + } + + static allocator_type + select_on_container_copy_construction(const allocator_type &Allocator) { + return Allocator; + } +}; +} // namespace std diff --git a/sycl/include/sycl/detail/backend_traits_opencl.hpp b/sycl/include/sycl/detail/backend_traits_opencl.hpp index dd5ec51e10060..304f29a45aae6 100644 --- a/sycl/include/sycl/detail/backend_traits_opencl.hpp +++ b/sycl/include/sycl/detail/backend_traits_opencl.hpp @@ -84,17 +84,10 @@ struct BackendInput> { using type = cl_mem; }; -#ifdef SYCL2020_CONFORMANT_APIS template struct BackendReturn> { using type = std::vector; }; -#else -template -struct BackendReturn> { - using type = cl_mem; -}; -#endif template <> struct BackendInput { using type = cl_context; @@ -112,7 +105,6 @@ template <> struct BackendReturn { using type = cl_device_id; }; -#ifdef SYCL2020_CONFORMANT_APIS template <> struct interop { using type = std::vector; using value_type = cl_event; @@ -125,17 +117,6 @@ template <> struct BackendReturn { using type = std::vector; using value_type = cl_event; }; -#else -template <> struct interop { - using type = cl_event; -}; -template <> struct BackendInput { - using type = cl_event; -}; -template <> struct BackendReturn { - using type = cl_event; -}; -#endif template <> struct BackendInput { using type = cl_command_queue; diff --git a/sycl/include/sycl/detail/sycl_mem_obj_allocator.hpp b/sycl/include/sycl/detail/sycl_mem_obj_allocator.hpp index 617e679857cbf..e94bd5b342c58 100644 --- a/sycl/include/sycl/detail/sycl_mem_obj_allocator.hpp +++ b/sycl/include/sycl/detail/sycl_mem_obj_allocator.hpp @@ -8,12 +8,12 @@ #pragma once +#include + __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { -template class aligned_allocator; - template using sycl_memory_object_allocator = aligned_allocator; diff --git a/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp b/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp index 8b3c5e8792b00..1d47823b13291 100644 --- a/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp +++ b/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp @@ -193,7 +193,7 @@ inline kernel make_kernel( // Specialization of sycl::make_buffer with event for Level-Zero backend. template > + typename AllocatorT = buffer_allocator>> typename std::enable_if>::type make_buffer( @@ -208,7 +208,7 @@ make_buffer( // Specialization of sycl::make_buffer for Level-Zero backend. template > + typename AllocatorT = buffer_allocator>> typename std::enable_if>::type make_buffer( diff --git a/sycl/test/abi/layout_buffer.cpp b/sycl/test/abi/layout_buffer.cpp index c68638060968b..c7c076a82d025 100644 --- a/sycl/test/abi/layout_buffer.cpp +++ b/sycl/test/abi/layout_buffer.cpp @@ -79,7 +79,7 @@ void foo(sycl::buffer) {} // CHECK-NEXT: | [sizeof=184, dsize=184, align=8, // CHECK-NEXT: | nvsize=184, nvalign=8] -// CHECK: 0 | class sycl::buffer, void> +// CHECK: 0 | class sycl::buffer, void> // CHECK-NEXT: 0 | class std::shared_ptr impl // CHECK-NEXT: 0 | class std::__shared_ptr (base) // CHECK-NEXT: 0 | class std::__shared_ptr_access (base) (empty) diff --git a/sycl/test/abi/user_mangling.cpp b/sycl/test/abi/user_mangling.cpp index af66df4569f40..d5d6453b86c27 100644 --- a/sycl/test/abi/user_mangling.cpp +++ b/sycl/test/abi/user_mangling.cpp @@ -49,7 +49,7 @@ void acc(sycl::accessor) {} -// CHK-HOST: define dso_local void @_Z3bufN2cl4sycl6bufferIiLi1ENS0_6detail17aligned_allocatorIcEEvEE({{.*}}) +// CHK-HOST: define dso_local void @_Z3bufN2cl4sycl6bufferIiLi1ENS0_6detail17aligned_allocatorIiEEvEE({{.*}}) void buf(sycl::buffer) {} // CHK-HOST: define dso_local void @_Z3ctxN2cl4sycl7contextE({{.*}}) diff --git a/sycl/test/regression/check_vector_of_opencl_event.cpp b/sycl/test/regression/check_vector_of_opencl_event.cpp index 8624a28a6af67..f6cbaf6ad17f6 100644 --- a/sycl/test/regression/check_vector_of_opencl_event.cpp +++ b/sycl/test/regression/check_vector_of_opencl_event.cpp @@ -1,4 +1,4 @@ -// RUN: %clangxx -fsycl -DSYCL2020_CONFORMANT_APIS %s -o %t.out +// RUN: %clangxx -fsycl %s -o %t.out // RUN: %RUN_ON_HOST %t.out // //===----------------------------------------------------------------------===// diff --git a/sycl/unittests/scheduler/NoHostUnifiedMemory.cpp b/sycl/unittests/scheduler/NoHostUnifiedMemory.cpp index 23693a8957f18..13c97a39a74f0 100644 --- a/sycl/unittests/scheduler/NoHostUnifiedMemory.cpp +++ b/sycl/unittests/scheduler/NoHostUnifiedMemory.cpp @@ -221,8 +221,8 @@ TEST_F(SchedulerTest, NoHostUnifiedMemory) { InteropPiContext = detail::getSyclObjImpl(InteropContext)->getHandleRef(); auto BufI = std::make_shared( detail::pi::cast(MockInteropBuffer), Q.get_context(), - make_unique_ptr, char>>(), + make_unique_ptr< + detail::SYCLMemObjAllocatorHolder, char>>(), /* OwnNativeHandle */ true, event()); detail::Requirement Req = getMockRequirement(); From cbea9b2d0681241150d9a541080d1b6e0b67d3f1 Mon Sep 17 00:00:00 2001 From: "Larsen, Steffen" Date: Mon, 8 Aug 2022 09:44:31 -0700 Subject: [PATCH 2/3] Fix formatting Signed-off-by: Larsen, Steffen --- sycl/include/sycl/detail/aligned_allocator.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/include/sycl/detail/aligned_allocator.hpp b/sycl/include/sycl/detail/aligned_allocator.hpp index 4e47ce1546316..6c73fdde592bc 100644 --- a/sycl/include/sycl/detail/aligned_allocator.hpp +++ b/sycl/include/sycl/detail/aligned_allocator.hpp @@ -14,8 +14,8 @@ #include #include #include -#include #include +#include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { From 66d0908442071edda68e171079db9649f102bcaa Mon Sep 17 00:00:00 2001 From: "Larsen, Steffen" Date: Mon, 8 Aug 2022 10:28:42 -0700 Subject: [PATCH 3/3] Replace cl::sycl with sycl Signed-off-by: Larsen, Steffen --- sycl/include/sycl/detail/aligned_allocator.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sycl/include/sycl/detail/aligned_allocator.hpp b/sycl/include/sycl/detail/aligned_allocator.hpp index 6c73fdde592bc..1aacb95bdd7d2 100644 --- a/sycl/include/sycl/detail/aligned_allocator.hpp +++ b/sycl/include/sycl/detail/aligned_allocator.hpp @@ -84,8 +84,8 @@ template class aligned_allocator { namespace std { template -struct allocator_traits> { - using allocator_type = typename cl::sycl::detail::aligned_allocator; +struct allocator_traits> { + using allocator_type = typename sycl::detail::aligned_allocator; using value_type = typename allocator_type::value_type; using pointer = typename allocator_type::pointer; using const_pointer = typename allocator_type::const_pointer; @@ -102,8 +102,8 @@ struct allocator_traits> { using is_always_equal = typename std::is_empty::type; template - using rebind_alloc = typename cl::sycl::detail::aligned_allocator< - T>::template rebind::other; + using rebind_alloc = + typename sycl::detail::aligned_allocator::template rebind::other; template using rebind_traits = allocator_traits>; static pointer allocate(allocator_type &Allocator, size_type NumElems) {