Skip to content

Commit de52645

Browse files
[SYCL] Adjust for all Dims offset in accessor's device __init
The optimization done for 1-dim accessor is suitable for all dimensions.
1 parent b2e3338 commit de52645

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

llvm/lib/SYCLLowerIR/ESIMD/ESIMDVerifier.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ static const char *LegalSYCLFunctions[] = {
5252
"^cl::sycl::ext::oneapi::experimental::this_sub_group"};
5353

5454
static const char *LegalSYCLFunctionsInStatelessMode[] = {
55-
"^cl::sycl::multi_ptr<.+>::get", "^cl::sycl::multi_ptr<.+>::multi_ptr",
55+
"^cl::sycl::multi_ptr<.+>::get",
56+
"^cl::sycl::multi_ptr<.+>::multi_ptr",
5657
"^cl::sycl::accessor<.+>::get_pointer.+",
5758
"^cl::sycl::accessor<.+>::getPointerAdjusted",
58-
"^cl::sycl::accessor<.+>::getQualifiedPtr"};
59+
"^cl::sycl::accessor<.+>::getQualifiedPtr",
60+
"^cl::sycl::accessor<.+>::getTotalOffset"};
5961

6062
namespace {
6163

sycl/include/sycl/accessor.hpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -832,17 +832,14 @@ class __SYCL_SPECIAL_CLASS accessor :
832832

833833
template <int Dims = Dimensions> size_t getLinearIndex(id<Dims> Id) const {
834834

835-
#ifdef __SYCL_DEVICE_ONLY__
836-
// Pointer is already adjusted for 1D case.
837-
if (Dimensions == 1)
838-
return Id[0];
839-
#endif // __SYCL_DEVICE_ONLY__
840-
841835
size_t Result = 0;
842836
// Unroll the following loop for both host and device code
843837
__SYCL_UNROLL(3)
844838
for (int I = 0; I < Dims; ++I) {
845839
Result = Result * getMemoryRange()[I] + Id[I];
840+
// We've already adjusted for the accessor's offset in the __init, so
841+
// don't include it here in case of device.
842+
#ifndef __SYCL_DEVICE_ONLY__
846843
#if __cplusplus >= 201703L
847844
if constexpr (!(PropertyListT::template has_property<
848845
sycl::ext::oneapi::property::no_offset>())) {
@@ -851,6 +848,7 @@ class __SYCL_SPECIAL_CLASS accessor :
851848
#else
852849
Result += getOffset()[I];
853850
#endif
851+
#endif // __SYCL_DEVICE_ONLY__
854852
}
855853
return Result;
856854
}
@@ -919,17 +917,10 @@ class __SYCL_SPECIAL_CLASS accessor :
919917
getAccessRange()[I] = AccessRange[I];
920918
getMemoryRange()[I] = MemRange[I];
921919
}
922-
// In case of 1D buffer, adjust pointer during initialization rather
923-
// then each time in operator[]. Will have to re-adjust in get_pointer
924-
if (1 == AdjustedDim)
925-
#if __cplusplus >= 201703L
926-
if constexpr (!(PropertyListT::template has_property<
927-
sycl::ext::oneapi::property::no_offset>())) {
928-
MData += Offset[0];
929-
}
930-
#else
931-
MData += Offset[0];
932-
#endif
920+
921+
// Adjust for offsets as that part is invariant for all invocations of
922+
// operator[]. Will have to re-adjust in get_pointer.
923+
MData += getTotalOffset();
933924
}
934925

935926
// __init variant used by the device compiler for ESIMD kernels.
@@ -1797,17 +1788,37 @@ class __SYCL_SPECIAL_CLASS accessor :
17971788
bool operator!=(const accessor &Rhs) const { return !(*this == Rhs); }
17981789

17991790
private:
1791+
#ifdef __SYCL_DEVICE_ONLY__
1792+
size_t getTotalOffset() const {
1793+
size_t TotalOffset = 0;
1794+
__SYCL_UNROLL(3)
1795+
for (int I = 0; I < Dimensions; ++I) {
1796+
TotalOffset = TotalOffset * impl.MemRange[I];
1797+
#if __cplusplus >= 201703L
1798+
if constexpr (!(PropertyListT::template has_property<
1799+
sycl::ext::oneapi::property::no_offset>())) {
1800+
TotalOffset += impl.Offset[I];
1801+
}
1802+
#else
1803+
TotalOffset += impl.Offset[I];
1804+
#endif
1805+
}
1806+
1807+
return TotalOffset;
1808+
}
1809+
#endif
1810+
18001811
// supporting function for get_pointer()
1801-
// when dim==1, MData will have been preadjusted for faster access with []
1812+
// MData has been preadjusted with offset for faster access with []
18021813
// but for get_pointer() we must return the original pointer.
18031814
// On device, getQualifiedPtr() returns MData, so we need to backjust it.
18041815
// On host, getQualifiedPtr() does not return MData, no need to adjust.
18051816
PtrType getPointerAdjusted() const {
18061817
#ifdef __SYCL_DEVICE_ONLY__
1807-
if (1 == AdjustedDim)
1808-
return getQualifiedPtr() - impl.Offset[0];
1809-
#endif
1818+
return getQualifiedPtr() - getTotalOffset();
1819+
#else
18101820
return getQualifiedPtr();
1821+
#endif
18111822
}
18121823

18131824
void preScreenAccessor(const size_t elemInBuffer,

0 commit comments

Comments
 (0)