Skip to content

Commit fbb885e

Browse files
author
Andrew Lamzed-Short
authored
[SYCL] Assignment operators for 0-dim accessors (#8069)
This patch implements and provides tests for assignment operators for the non-read-only, 0-dimensional variants of the buffer accessor, local accessor, and host accessor classes. Implemented according to [Table 72 of the SYCL 2020 specification](https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#_interface_for_buffer_host_accessors) and the following PRs: KhronosGroup/SYCL-Docs#278, KhronosGroup/SYCL-Docs#334
1 parent 4f75718 commit fbb885e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

sycl/include/sycl/accessor.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,22 @@ class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
20122012
return *(getQualifiedPtr() + LinearIndex);
20132013
}
20142014

2015+
template <int Dims = Dimensions,
2016+
typename = detail::enable_if_t<AccessMode != access_mode::atomic &&
2017+
!IsAccessReadOnly && Dims == 0>>
2018+
const accessor &operator=(const value_type &Other) const {
2019+
*getQualifiedPtr() = Other;
2020+
return *this;
2021+
}
2022+
2023+
template <int Dims = Dimensions,
2024+
typename = detail::enable_if_t<AccessMode != access_mode::atomic &&
2025+
!IsAccessReadOnly && Dims == 0>>
2026+
const accessor &operator=(value_type &&Other) const {
2027+
*getQualifiedPtr() = std::move(Other);
2028+
return *this;
2029+
}
2030+
20152031
template <int Dims = Dimensions,
20162032
typename = detail::enable_if_t<(Dims > 0) && (IsAccessAnyWrite ||
20172033
IsAccessReadOnly)>>
@@ -2780,6 +2796,20 @@ class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor
27802796
return Property();
27812797
#endif
27822798
}
2799+
2800+
template <int Dims = Dimensions, typename = detail::enable_if_t<
2801+
!std::is_const_v<DataT> && Dims == 0>>
2802+
const local_accessor &operator=(const value_type &Other) const {
2803+
*local_acc::getQualifiedPtr() = Other;
2804+
return *this;
2805+
}
2806+
2807+
template <int Dims = Dimensions, typename = detail::enable_if_t<
2808+
!std::is_const_v<DataT> && Dims == 0>>
2809+
const local_accessor &operator=(value_type &&Other) const {
2810+
*local_acc::getQualifiedPtr() = std::move(Other);
2811+
return *this;
2812+
}
27832813
};
27842814

27852815
/// Image accessors.
@@ -2957,6 +2987,7 @@ class __SYCL_EBO host_accessor
29572987
access::placeholder::false_t>;
29582988

29592989
constexpr static int AdjustedDim = Dimensions == 0 ? 1 : Dimensions;
2990+
constexpr static bool IsAccessReadOnly = AccessMode == access::mode::read;
29602991

29612992
template <typename T, int Dims>
29622993
struct IsSameAsBuffer
@@ -3111,6 +3142,23 @@ class __SYCL_EBO host_accessor
31113142
const detail::code_location CodeLoc = detail::code_location::current())
31123143
: host_accessor(BufferRef, CommandGroupHandler, AccessRange, AccessOffset,
31133144
PropertyList, CodeLoc) {}
3145+
3146+
template <int Dims = Dimensions,
3147+
typename = detail::enable_if_t<AccessMode != access_mode::atomic &&
3148+
!IsAccessReadOnly && Dims == 0>>
3149+
const host_accessor &
3150+
operator=(const typename AccessorT::value_type &Other) const {
3151+
*AccessorT::getQualifiedPtr() = Other;
3152+
return *this;
3153+
}
3154+
3155+
template <int Dims = Dimensions,
3156+
typename = detail::enable_if_t<AccessMode != access_mode::atomic &&
3157+
!IsAccessReadOnly && Dims == 0>>
3158+
const host_accessor &operator=(typename AccessorT::value_type &&Other) const {
3159+
*AccessorT::getQualifiedPtr() = std::move(Other);
3160+
return *this;
3161+
}
31143162
};
31153163

31163164
template <typename DataT, int Dimensions, typename AllocatorT>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %t.out
3+
4+
#include <sycl/sycl.hpp>
5+
6+
int main() {
7+
int Expected = 64;
8+
int Data = 32;
9+
sycl::buffer<int, 1> DataBuffer(&Data, sycl::range<1>(1));
10+
sycl::host_accessor<int, 0> HostAcc{DataBuffer};
11+
12+
HostAcc = Expected;
13+
14+
assert(HostAcc == Expected);
15+
}

0 commit comments

Comments
 (0)