Skip to content

Commit 308e5ad

Browse files
authored
[SYCL] implement no_offset property for accessor_property_list class (#4920)
1 parent cda4ed5 commit 308e5ad

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

sycl/include/CL/sycl/accessor.hpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,17 @@ class __SYCL_SPECIAL_CLASS accessor :
839839
size_t Result = 0;
840840
// Unroll the following loop for both host and device code
841841
__SYCL_UNROLL(3)
842-
for (int I = 0; I < Dims; ++I)
843-
Result = Result * getMemoryRange()[I] + getOffset()[I] + Id[I];
842+
for (int I = 0; I < Dims; ++I) {
843+
Result = Result * getMemoryRange()[I] + Id[I];
844+
#if __cplusplus >= 201703L
845+
if constexpr (!(PropertyListT::template has_property<
846+
sycl::ext::oneapi::property::no_offset>())) {
847+
Result += getOffset()[I];
848+
}
849+
#else
850+
Result += getOffset()[I];
851+
#endif
852+
}
844853
return Result;
845854
}
846855

@@ -897,14 +906,28 @@ class __SYCL_SPECIAL_CLASS accessor :
897906
MData = Ptr;
898907
#pragma unroll
899908
for (int I = 0; I < AdjustedDim; ++I) {
909+
#if __cplusplus >= 201703L
910+
if constexpr (!(PropertyListT::template has_property<
911+
sycl::ext::oneapi::property::no_offset>())) {
912+
getOffset()[I] = Offset[I];
913+
}
914+
#else
900915
getOffset()[I] = Offset[I];
916+
#endif
901917
getAccessRange()[I] = AccessRange[I];
902918
getMemoryRange()[I] = MemRange[I];
903919
}
904920
// In case of 1D buffer, adjust pointer during initialization rather
905921
// then each time in operator[] or get_pointer functions.
906922
if (1 == AdjustedDim)
923+
#if __cplusplus >= 201703L
924+
if constexpr (!(PropertyListT::template has_property<
925+
sycl::ext::oneapi::property::no_offset>())) {
926+
MData += Offset[0];
927+
}
928+
#else
907929
MData += Offset[0];
930+
#endif
908931
}
909932

910933
// __init variant used by the device compiler for ESIMD kernels.
@@ -1530,6 +1553,12 @@ class __SYCL_SPECIAL_CLASS accessor :
15301553

15311554
template <int Dims = Dimensions, typename = detail::enable_if_t<(Dims > 0)>>
15321555
id<Dimensions> get_offset() const {
1556+
#if __cplusplus >= 201703L
1557+
static_assert(
1558+
!(PropertyListT::template has_property<
1559+
sycl::ext::oneapi::property::no_offset>()),
1560+
"Accessor has no_offset property, get_offset() can not be used");
1561+
#endif
15331562
return detail::convertToArrayOfN<Dimensions, 0>(getOffset());
15341563
}
15351564

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %clangxx -fsycl-device-only -fsycl-early-optimizations -fsycl-dead-args-optimization -D__SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__ -S -emit-llvm -o - %s | FileCheck %s
2+
3+
#include <CL/sycl.hpp>
4+
5+
inline constexpr int size = 100;
6+
7+
int main() {
8+
{
9+
sycl::buffer<int> a{sycl::range{size}};
10+
sycl::buffer<int> b{sycl::range{size}};
11+
12+
sycl::queue q;
13+
14+
q.submit([&](sycl::handler &cgh) {
15+
sycl::ext::oneapi::accessor_property_list PL{sycl::ext::oneapi::no_offset, sycl::no_init};
16+
sycl::accessor acc_a(a, cgh, sycl::write_only, PL);
17+
sycl::accessor acc_b{b, cgh, sycl::read_only};
18+
// CHECK: define weak_odr dso_local spir_kernel void @_ZTSZZ4mainENKUlRN2cl4sycl7handlerEE_clES2_EUlT_E_(i32 addrspace(1)* %_arg_, i32 addrspace(1)* readonly %_arg_4, %"class.cl::sycl::id"* byval(%"class.cl::sycl::id") align 8 %_arg_8)
19+
cgh.parallel_for(size, [=](auto i) {
20+
acc_a[i] = acc_b[i];
21+
});
22+
});
23+
24+
q.wait();
25+
}
26+
27+
{
28+
sycl::buffer<int> a{sycl::range{size}};
29+
sycl::buffer<int> b{sycl::range{size}};
30+
31+
sycl::queue q;
32+
33+
q.submit([&](sycl::handler &cgh) {
34+
sycl::accessor acc_a(a, cgh, sycl::write_only);
35+
sycl::accessor acc_b{b, cgh, sycl::read_only};
36+
// CHECK: define weak_odr dso_local spir_kernel void @_ZTSZZ4mainENKUlRN2cl4sycl7handlerEE0_clES2_EUlT_E_(i32 addrspace(1)* %_arg_, %"class.cl::sycl::id"* byval(%"class.cl::sycl::id") align 8 %_arg_3, i32 addrspace(1)* readonly %_arg_4, %"class.cl::sycl::id"* byval(%"class.cl::sycl::id") align 8 %_arg_8)
37+
cgh.parallel_for(size, [=](auto i) {
38+
acc_a[i] = acc_b[i];
39+
});
40+
});
41+
42+
q.wait();
43+
}
44+
45+
return 0;
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clangxx -fsycl-device-only -Xclang -verify -Xclang -verify-ignore-unexpected=note -emit-llvm -o - %s
2+
3+
#include <CL/sycl.hpp>
4+
5+
inline constexpr int size = 100;
6+
7+
int main() {
8+
9+
sycl::buffer<int> a{sycl::range{size}};
10+
sycl::queue q;
11+
12+
q.submit([&](sycl::handler &cgh) {
13+
sycl::ext::oneapi::accessor_property_list PL{sycl::ext::oneapi::no_offset, sycl::no_init};
14+
sycl::accessor acc_a(a, cgh, sycl::write_only, PL);
15+
// expected-error@CL/sycl/accessor.hpp:* {{static_assert failed due to requirement '!(accessor_property_list<sycl::ext::oneapi::property::no_offset::instance<true>, sycl::property::no_init>::has_property())' "Accessor has no_offset property, get_offset() can not be used"}}
16+
auto b = acc_a.get_offset();
17+
});
18+
19+
q.wait();
20+
return 0;
21+
}

0 commit comments

Comments
 (0)