Skip to content

Commit 7c9426f

Browse files
committed
[SYCL] implement no_offset property for accessor_property_list class
1 parent 51b450c commit 7c9426f

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

sycl/include/CL/sycl/accessor.hpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,16 @@ 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<sycl::ext::oneapi::property::no_offset>())) {
846+
Result += getOffset()[I];
847+
}
848+
#else
849+
Result += getOffset()[I];
850+
#endif
851+
}
844852
return Result;
845853
}
846854

@@ -897,14 +905,26 @@ class __SYCL_SPECIAL_CLASS accessor :
897905
MData = Ptr;
898906
#pragma unroll
899907
for (int I = 0; I < AdjustedDim; ++I) {
900-
getOffset()[I] = Offset[I];
908+
#if __cplusplus >= 201703L
909+
if constexpr (!(PropertyListT::template has_property<sycl::ext::oneapi::property::no_offset>())) {
910+
getOffset()[I] = Offset[I];
911+
}
912+
#else
913+
getOffset()[I] = Offset[I];
914+
#endif
901915
getAccessRange()[I] = AccessRange[I];
902916
getMemoryRange()[I] = MemRange[I];
903917
}
904918
// In case of 1D buffer, adjust pointer during initialization rather
905919
// then each time in operator[] or get_pointer functions.
906920
if (1 == AdjustedDim)
921+
#if __cplusplus >= 201703L
922+
if constexpr (!(PropertyListT::template has_property<sycl::ext::oneapi::property::no_offset>())) {
923+
MData += Offset[0];
924+
}
925+
#else
907926
MData += Offset[0];
927+
#endif
908928
}
909929

910930
// __init variant used by the device compiler for ESIMD kernels.
@@ -1530,6 +1550,10 @@ class __SYCL_SPECIAL_CLASS accessor :
15301550

15311551
template <int Dims = Dimensions, typename = detail::enable_if_t<(Dims > 0)>>
15321552
id<Dimensions> get_offset() const {
1553+
#if __cplusplus >= 201703L
1554+
static_assert (!(PropertyListT::template has_property<sycl::ext::oneapi::property::no_offset>()),
1555+
"Accessor has no_offset property, get_offset() can not be used");
1556+
#endif
15331557
return detail::convertToArrayOfN<Dimensions, 0>(getOffset());
15341558
}
15351559

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 -fsycl-early-optimizations -fsycl-dead-args-optimization -D__SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__ -Xclang -verify -Xclang -verify-ignore-unexpected=note -S -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)