Skip to content

[SYCL] Change read only accessor return const reference #2142

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

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 6 additions & 4 deletions sycl/include/CL/sycl/accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class accessor_common {
AccessMode == access::mode::read_write;

using RefType = detail::const_if_const_AS<AS, DataT> &;
using ConstRefType = const DataT &;
using PtrType = detail::const_if_const_AS<AS, DataT> *;

using AccType =
Expand Down Expand Up @@ -299,7 +300,7 @@ class accessor_common {

template <int CurDims = SubDims,
typename = detail::enable_if_t<CurDims == 1 && IsAccessReadOnly>>
DataT operator[](size_t Index) const {
ConstRefType operator[](size_t Index) const {
MIDs[Dims - SubDims] = Index;
return MAccessor[MIDs];
}
Expand Down Expand Up @@ -767,6 +768,7 @@ class accessor :
using ConcreteASPtrType = typename detail::PtrValueType<DataT, AS>::type *;

using RefType = detail::const_if_const_AS<AS, DataT> &;
using ConstRefType = const DataT &;
using PtrType = detail::const_if_const_AS<AS, DataT> *;

template <int Dims = Dimensions> size_t getLinearIndex(id<Dims> Id) const {
Expand Down Expand Up @@ -1199,9 +1201,9 @@ class accessor :
return *(getQualifiedPtr() + LinearIndex);
}

template <int Dims = Dimensions,
typename = detail::enable_if_t<(Dims > 0) && IsAccessReadOnly>>
DataT operator[](id<Dimensions> Index) const {
template <int Dims = Dimensions>
typename detail::enable_if_t<(Dims > 0) && IsAccessReadOnly, ConstRefType>
operator[](id<Dimensions> Index) const {
const size_t LinearIndex = getLinearIndex(Index);
return getQualifiedPtr()[LinearIndex];
}
Expand Down
4 changes: 2 additions & 2 deletions sycl/test/basic_tests/accessor/addrspace_exposure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ int main() {
Cgh.single_task<class test>([=]() {
static_assert(std::is_same<decltype(GlobalRWAcc[0]), int &>::value,
"Incorrect type from global read-write accessor");
static_assert(std::is_same<decltype(GlobalRAcc[0]), int>::value,
static_assert(std::is_same<decltype(GlobalRAcc[0]), const int &>::value,
"Incorrect type from global read accessor");
static_assert(std::is_same<decltype(ConstantAcc[0]), int>::value,
static_assert(std::is_same<decltype(ConstantAcc[0]), const int &>::value,
"Incorrect type from constant accessor");
static_assert(std::is_same<decltype(LocalAcc[0]), int &>::value,
"Incorrect type from local accessor");
Expand Down
12 changes: 8 additions & 4 deletions sycl/test/regression/memcpy-in-vec-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ int main() {
cl::sycl::range<1>(1));
cl::sycl::queue Queue;
Queue.submit([&](cl::sycl::handler &cgh) {
auto In = InBuf.get_access<cl::sycl::access::mode::write>(cgh);
auto Out = OutBuf.get_access<cl::sycl::access::mode::read>(cgh);
auto In = InBuf.get_access<cl::sycl::access::mode::read>(cgh);
auto Out = OutBuf.get_access<cl::sycl::access::mode::write>(cgh);
cgh.single_task<class as_op>(
[=]() { Out[0] = In[0].as<res_vec_type>(); });
});
}
std::cout << res.s0() << " " << res.s1() << " " << res.s2() << " " << res.s3()
<< std::endl;

if (res.s0() != 513 || res.s1() != 1027 || res.s2() != 1541 || res.s3() != 2055) {
std::cerr << "Incorrect result" << std::endl;
return 1;
}

return 0;
}