Skip to content

Commit fc03fda

Browse files
authored
[SYCL] Add buffer dimensions restriction (#1147)
According to the specification, a buffer can have dimensions of only 1, 2, and 3. In the old version, "static_assert" was used to limit it, which did not throw an exception when creating a pointer to a buffer with an incorrect dimension. In this patch it is impossible to create not only an object of the buffer class with incorrect dimensions, but also a link to it. Also, constructor calls in the accessor class that tried to instantiate a buffer with a zero dimension were fixed. Signed-off-by: amochalo <[email protected]>
1 parent a904808 commit fc03fda

File tree

4 files changed

+44
-36
lines changed

4 files changed

+44
-36
lines changed

sycl/include/CL/sycl/accessor.hpp

+33-30
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,7 @@ class accessor :
764764
: impl(id<AdjustedDim>(), range<1>{1}, BufferRef.get_range()) {
765765
#else
766766
: AccessorBaseHost(
767-
/*Offset=*/{0, 0, 0},
768-
detail::convertToArrayOfN<3, 1>(range<1>{1}),
767+
/*Offset=*/{0, 0, 0}, detail::convertToArrayOfN<3, 1>(range<1>{1}),
769768
detail::convertToArrayOfN<3, 1>(BufferRef.get_range()), AccessMode,
770769
detail::getSyclObjImpl(BufferRef).get(), AdjustedDim, sizeof(DataT),
771770
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
@@ -774,18 +773,19 @@ class accessor :
774773
#endif
775774
}
776775

777-
template <int Dims = Dimensions, typename AllocatorT>
778-
accessor(buffer<DataT, 1, AllocatorT> &BufferRef,
779-
detail::enable_if_t<Dims == 0 &&
780-
(!IsPlaceH && (IsGlobalBuf || IsConstantBuf)),
781-
handler> &CommandGroupHandler)
776+
template <int Dims = Dimensions, typename AllocatorT,
777+
typename = typename detail::enable_if_t<
778+
(Dims == 0) &&
779+
(!IsPlaceH && (IsGlobalBuf || IsConstantBuf))>
780+
>
781+
accessor(buffer<DataT,1,AllocatorT> &BufferRef,
782+
handler &CommandGroupHandler)
782783
#ifdef __SYCL_DEVICE_ONLY__
783784
: impl(id<AdjustedDim>(), range<1>{1}, BufferRef.get_range()) {
784785
}
785786
#else
786787
: AccessorBaseHost(
787-
/*Offset=*/{0, 0, 0},
788-
detail::convertToArrayOfN<3, 1>(range<1>{1}),
788+
/*Offset=*/{0, 0, 0}, detail::convertToArrayOfN<3, 1>(range<1>{1}),
789789
detail::convertToArrayOfN<3, 1>(BufferRef.get_range()), AccessMode,
790790
detail::getSyclObjImpl(BufferRef).get(), Dimensions, sizeof(DataT),
791791
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
@@ -794,11 +794,11 @@ class accessor :
794794
#endif
795795

796796
template <int Dims = Dimensions, typename AllocatorT,
797-
typename detail::enable_if_t<
798-
(Dims > 0) && ((!IsPlaceH && IsHostBuf) ||
799-
(IsPlaceH && (IsGlobalBuf || IsConstantBuf)))>
800-
* = nullptr>
801-
accessor(buffer<DataT, Dimensions, AllocatorT> &BufferRef)
797+
typename = detail::enable_if_t<(Dims > 0) && (Dims == Dimensions) &&
798+
((!IsPlaceH && IsHostBuf) ||
799+
(IsPlaceH &&
800+
(IsGlobalBuf || IsConstantBuf)))>>
801+
accessor(buffer<DataT, Dims, AllocatorT> &BufferRef)
802802
#ifdef __SYCL_DEVICE_ONLY__
803803
: impl(id<Dimensions>(), BufferRef.get_range(), BufferRef.get_range()) {
804804
}
@@ -815,9 +815,10 @@ class accessor :
815815
#endif
816816

817817
template <int Dims = Dimensions, typename AllocatorT,
818-
typename = detail::enable_if_t<
819-
(Dims > 0) && (!IsPlaceH && (IsGlobalBuf || IsConstantBuf))>>
820-
accessor(buffer<DataT, Dimensions, AllocatorT> &BufferRef,
818+
typename = detail::enable_if_t<(Dims > 0) && (Dims == Dimensions) &&
819+
(!IsPlaceH &&
820+
(IsGlobalBuf || IsConstantBuf))>>
821+
accessor(buffer<DataT, Dims, AllocatorT> &BufferRef,
821822
handler &CommandGroupHandler)
822823
#ifdef __SYCL_DEVICE_ONLY__
823824
: impl(id<AdjustedDim>(), BufferRef.get_range(), BufferRef.get_range()) {
@@ -834,10 +835,11 @@ class accessor :
834835
#endif
835836

836837
template <int Dims = Dimensions, typename AllocatorT,
837-
typename = detail::enable_if_t<
838-
(Dims > 0) && ((!IsPlaceH && IsHostBuf) ||
839-
(IsPlaceH && (IsGlobalBuf || IsConstantBuf)))>>
840-
accessor(buffer<DataT, Dimensions, AllocatorT> &BufferRef,
838+
typename = detail::enable_if_t<(Dims > 0) && (Dims == Dimensions) &&
839+
((!IsPlaceH && IsHostBuf) ||
840+
(IsPlaceH &&
841+
(IsGlobalBuf || IsConstantBuf)))>>
842+
accessor(buffer<DataT, Dims, AllocatorT> &BufferRef,
841843
range<Dimensions> AccessRange, id<Dimensions> AccessOffset = {})
842844
#ifdef __SYCL_DEVICE_ONLY__
843845
: impl(AccessOffset, AccessRange, BufferRef.get_range()) {
@@ -855,9 +857,10 @@ class accessor :
855857
#endif
856858

857859
template <int Dims = Dimensions, typename AllocatorT,
858-
typename = detail::enable_if_t<
859-
(Dims > 0) && (!IsPlaceH && (IsGlobalBuf || IsConstantBuf))>>
860-
accessor(buffer<DataT, Dimensions, AllocatorT> &BufferRef,
860+
typename = detail::enable_if_t<(Dims > 0) && (Dims == Dimensions) &&
861+
(!IsPlaceH &&
862+
(IsGlobalBuf || IsConstantBuf))>>
863+
accessor(buffer<DataT, Dims, AllocatorT> &BufferRef,
861864
handler &CommandGroupHandler, range<Dimensions> AccessRange,
862865
id<Dimensions> AccessOffset = {})
863866
#ifdef __SYCL_DEVICE_ONLY__
@@ -933,17 +936,17 @@ class accessor :
933936
}
934937

935938
template <int Dims = Dimensions>
936-
operator typename std::enable_if<Dims == 0 &&
937-
AccessMode == access::mode::atomic,
938-
atomic<DataT, AS>>::type() const {
939+
operator typename detail::enable_if_t<
940+
Dims == 0 && AccessMode == access::mode::atomic, atomic<DataT, AS>>()
941+
const {
939942
const size_t LinearIndex = getLinearIndex(id<AdjustedDim>());
940943
return atomic<DataT, AS>(
941944
multi_ptr<DataT, AS>(getQualifiedPtr() + LinearIndex));
942945
}
943946

944947
template <int Dims = Dimensions>
945-
typename std::enable_if<(Dims > 0) && AccessMode == access::mode::atomic,
946-
atomic<DataT, AS>>::type
948+
typename detail::enable_if_t<(Dims > 0) && AccessMode == access::mode::atomic,
949+
atomic<DataT, AS>>
947950
operator[](id<Dimensions> Index) const {
948951
const size_t LinearIndex = getLinearIndex(Index);
949952
return atomic<DataT, AS>(
@@ -952,7 +955,7 @@ class accessor :
952955

953956
template <int Dims = Dimensions>
954957
typename detail::enable_if_t<Dims == 1 && AccessMode == access::mode::atomic,
955-
atomic<DataT, AS>>::type
958+
atomic<DataT, AS>>
956959
operator[](size_t Index) const {
957960
const size_t LinearIndex = getLinearIndex(id<AdjustedDim>(Index));
958961
return atomic<DataT, AS>(

sycl/include/CL/sycl/buffer.hpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class queue;
2121
template <int dimensions> class range;
2222

2323
template <typename T, int dimensions = 1,
24-
typename AllocatorT = cl::sycl::buffer_allocator>
24+
typename AllocatorT = cl::sycl::buffer_allocator,
25+
typename = typename std::enable_if<(dimensions > 0) &&
26+
(dimensions <= 3)>::type>
2527
class buffer {
2628
public:
2729
using value_type = T;
@@ -302,9 +304,10 @@ class buffer {
302304
shared_ptr_class<detail::buffer_impl> impl;
303305
template <class Obj>
304306
friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
305-
template <typename A, int dims, typename C> friend class buffer;
306-
template <typename DataT, int dims, access::mode mode,
307-
access::target target, access::placeholder isPlaceholder>
307+
template <typename A, int dims, typename C, typename Enable>
308+
friend class buffer;
309+
template <typename DataT, int dims, access::mode mode, access::target target,
310+
access::placeholder isPlaceholder>
308311
friend class accessor;
309312
range<dimensions> Range;
310313
// Offset field specifies the origin of the sub buffer inside the parent

sycl/include/CL/sycl/detail/buffer_impl.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ namespace sycl {
3030
template <typename DataT, int Dimensions, access::mode AccessMode,
3131
access::target AccessTarget, access::placeholder IsPlaceholder>
3232
class accessor;
33-
template <typename T, int Dimensions, typename AllocatorT> class buffer;
33+
template <typename T, int Dimensions, typename AllocatorT, typename Enable>
34+
class buffer;
3435
class handler;
3536

3637
using buffer_allocator = detail::sycl_memory_object_allocator;

sycl/include/CL/sycl/handler.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ namespace sycl {
5858
// Forward declaration
5959

6060
class handler;
61-
template <typename T, int Dimensions, typename AllocatorT> class buffer;
61+
template <typename T, int Dimensions, typename AllocatorT, typename Enable>
62+
class buffer;
6263
namespace detail {
6364

6465
/// This class is the default KernelName template parameter type for kernel

0 commit comments

Comments
 (0)