Skip to content

Commit 03db009

Browse files
authored
[SYCL] Change read only accessor return const reference (#2142)
This should improve performance in some cases. Also fixed memcpy-in-vec-as.cpp as the patch reveals problems in the test.
1 parent c7f397a commit 03db009

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

sycl/include/CL/sycl/accessor.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ class accessor_common {
260260
AccessMode == access::mode::read_write;
261261

262262
using RefType = detail::const_if_const_AS<AS, DataT> &;
263+
using ConstRefType = const DataT &;
263264
using PtrType = detail::const_if_const_AS<AS, DataT> *;
264265

265266
using AccType =
@@ -299,7 +300,7 @@ class accessor_common {
299300

300301
template <int CurDims = SubDims,
301302
typename = detail::enable_if_t<CurDims == 1 && IsAccessReadOnly>>
302-
DataT operator[](size_t Index) const {
303+
ConstRefType operator[](size_t Index) const {
303304
MIDs[Dims - SubDims] = Index;
304305
return MAccessor[MIDs];
305306
}
@@ -767,6 +768,7 @@ class accessor :
767768
using ConcreteASPtrType = typename detail::PtrValueType<DataT, AS>::type *;
768769

769770
using RefType = detail::const_if_const_AS<AS, DataT> &;
771+
using ConstRefType = const DataT &;
770772
using PtrType = detail::const_if_const_AS<AS, DataT> *;
771773

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

1202-
template <int Dims = Dimensions,
1203-
typename = detail::enable_if_t<(Dims > 0) && IsAccessReadOnly>>
1204-
DataT operator[](id<Dimensions> Index) const {
1204+
template <int Dims = Dimensions>
1205+
typename detail::enable_if_t<(Dims > 0) && IsAccessReadOnly, ConstRefType>
1206+
operator[](id<Dimensions> Index) const {
12051207
const size_t LinearIndex = getLinearIndex(Index);
12061208
return getQualifiedPtr()[LinearIndex];
12071209
}

sycl/test/basic_tests/accessor/addrspace_exposure.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ int main() {
3333
Cgh.single_task<class test>([=]() {
3434
static_assert(std::is_same<decltype(GlobalRWAcc[0]), int &>::value,
3535
"Incorrect type from global read-write accessor");
36-
static_assert(std::is_same<decltype(GlobalRAcc[0]), int>::value,
36+
static_assert(std::is_same<decltype(GlobalRAcc[0]), const int &>::value,
3737
"Incorrect type from global read accessor");
38-
static_assert(std::is_same<decltype(ConstantAcc[0]), int>::value,
38+
static_assert(std::is_same<decltype(ConstantAcc[0]), const int &>::value,
3939
"Incorrect type from constant accessor");
4040
static_assert(std::is_same<decltype(LocalAcc[0]), int &>::value,
4141
"Incorrect type from local accessor");

sycl/test/regression/memcpy-in-vec-as.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ int main() {
1313
cl::sycl::range<1>(1));
1414
cl::sycl::queue Queue;
1515
Queue.submit([&](cl::sycl::handler &cgh) {
16-
auto In = InBuf.get_access<cl::sycl::access::mode::write>(cgh);
17-
auto Out = OutBuf.get_access<cl::sycl::access::mode::read>(cgh);
16+
auto In = InBuf.get_access<cl::sycl::access::mode::read>(cgh);
17+
auto Out = OutBuf.get_access<cl::sycl::access::mode::write>(cgh);
1818
cgh.single_task<class as_op>(
1919
[=]() { Out[0] = In[0].as<res_vec_type>(); });
2020
});
2121
}
22-
std::cout << res.s0() << " " << res.s1() << " " << res.s2() << " " << res.s3()
23-
<< std::endl;
22+
23+
if (res.s0() != 513 || res.s1() != 1027 || res.s2() != 1541 || res.s3() != 2055) {
24+
std::cerr << "Incorrect result" << std::endl;
25+
return 1;
26+
}
27+
2428
return 0;
2529
}

0 commit comments

Comments
 (0)