Skip to content

Commit 7618dff

Browse files
authored
[SYCL] Lost data during implicit conversion in local and host accessors. (#9669)
* Fix local_accessor and host_accessor lost data during implicit conversion. * Add relevant test.
1 parent 4eaaaa9 commit 7618dff

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

sycl/include/sycl/accessor.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,8 @@ class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
13401340
/*IsPlaceH=*/true,
13411341
/*OffsetInBytes=*/0, /*IsSubBuffer=*/false, /*PropertyList=*/{}){};
13421342

1343+
template <typename, int, access_mode> friend class host_accessor;
1344+
13431345
#endif // __SYCL_DEVICE_ONLY__
13441346

13451347
private:
@@ -2948,6 +2950,9 @@ class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor
29482950
std::is_same_v<DataT_, std::remove_const_t<DataT>>>>
29492951
local_accessor(const local_accessor<DataT_, Dimensions> &other) {
29502952
local_acc::impl = other.impl;
2953+
#ifdef __SYCL_DEVICE_ONLY__
2954+
local_acc::MData = other.MData;
2955+
#endif
29512956
}
29522957

29532958
using value_type = DataT;
@@ -3417,9 +3422,11 @@ class __SYCL_EBO host_accessor
34173422
std::remove_const_t<DataT>>>>
34183423
host_accessor(const host_accessor<DataT_, Dimensions, AccessMode> &other)
34193424
#ifndef __SYCL_DEVICE_ONLY__
3420-
: host_accessor(other.impl)
3421-
#endif // __SYCL_DEVICE_ONLY__
3425+
: host_accessor(other.impl) {
3426+
AccessorT::MAccData = other.MAccData;
3427+
#else
34223428
{
3429+
#endif // __SYCL_DEVICE_ONLY__
34233430
}
34243431

34253432
// implicit conversion from read_write T accessor to read only T (const)
@@ -3430,9 +3437,11 @@ class __SYCL_EBO host_accessor
34303437
std::is_same_v<DataT_, std::remove_const_t<DataT>>>>
34313438
host_accessor(const host_accessor<DataT_, Dimensions, AccessMode_> &other)
34323439
#ifndef __SYCL_DEVICE_ONLY__
3433-
: host_accessor(other.impl)
3434-
#endif // __SYCL_DEVICE_ONLY__
3440+
: host_accessor(other.impl) {
3441+
AccessorT::MAccData = other.MAccData;
3442+
#else
34353443
{
3444+
#endif // __SYCL_DEVICE_ONLY__
34363445
}
34373446

34383447
// host_accessor needs to explicitly define the owner_before member functions

sycl/test-e2e/Basic/accessor/accessor.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ void implicit_conversion(const AccT &acc, const ResAccT &res_acc) {
7676
res_acc[0] = v;
7777
}
7878

79+
void implicit_conversion(const sycl::local_accessor<const int, 1> &acc,
80+
const ResAccT &res_acc) {
81+
auto v = acc[0];
82+
res_acc[0] = v;
83+
}
84+
85+
int implicit_conversion(
86+
const sycl::host_accessor<const int, 1, sycl::access_mode::read> &acc) {
87+
auto v = acc[0];
88+
return v;
89+
}
90+
7991
template <typename T> void TestAccSizeFuncs(const std::vector<T> &vec) {
8092
auto test = [=](auto &Res, const auto &Acc) {
8193
Res[0] = Acc.byte_size();
@@ -1340,7 +1352,8 @@ int main() {
13401352
const int data = 123;
13411353
int result = 0;
13421354

1343-
// accessor<const T, read_only> to accessor<T, read_only> implicit conversion.
1355+
// accessor<const T, read_only> to accessor<T, read_only> implicit
1356+
// conversion.
13441357
{
13451358
sycl::buffer<const int, 1> data_buf(&data, 1);
13461359
sycl::buffer<int, 1> res_buf(&result, 1);
@@ -1359,6 +1372,37 @@ int main() {
13591372
assert(result == 123 && "Expected value not seen.");
13601373
}
13611374

1375+
// local_accessor<T> to local_accessor<const T> implicit conversion.
1376+
{
1377+
int data = 123;
1378+
int result = 0;
1379+
{
1380+
sycl::buffer<int, 1> res_buf(&result, 1);
1381+
sycl::queue queue;
1382+
queue
1383+
.submit([&](sycl::handler &cgh) {
1384+
ResAccT res_acc = res_buf.get_access(cgh);
1385+
sycl::local_accessor<int, 1> locAcc(1, cgh);
1386+
cgh.parallel_for(sycl::nd_range<1>{1, 1}, [=](sycl::nd_item<1>) {
1387+
locAcc[0] = 123;
1388+
implicit_conversion(locAcc, res_acc);
1389+
});
1390+
})
1391+
.wait_and_throw();
1392+
}
1393+
assert(result == 123 && "Expected value not seen.");
1394+
}
1395+
1396+
// host_accessor<T, read_write> to host_accessor<const T, read> implicit
1397+
// conversion.
1398+
{
1399+
int data = -1;
1400+
sycl::buffer<int, 1> d(&data, sycl::range<1>(1));
1401+
sycl::host_accessor host_acc(d, sycl::read_write);
1402+
host_acc[0] = 399;
1403+
assert(implicit_conversion(host_acc) == 399);
1404+
}
1405+
13621406
// accessor swap
13631407
{
13641408
int data[2] = {2, 100};

0 commit comments

Comments
 (0)