Skip to content

Commit 197c33a

Browse files
authored
[ESIMD] Add more user-friendly error msg when write beyond simd obj (#12059)
It is still not prohibited to create 'simd_view' that views beyond the bounds of viewed 'simd' object. And it is not yet prohibited to read using such 'simd_view' object, but the commit 17f8939 prohibited writing beyong simd bounds. The error message was difficult to read and did not give any guidance on the proposed fix. The old error message only said that __esimd_wrregion call could not be matched. The newly added static assert says that the viewed object in LHS does not fit the value in RHS. Example of problem situation: simd<float, 16> vec; // vec is only 16-elements auto vec_too_bit_view = vec.select<128, 1>();// 128-elems - too many vec_too_bit_view = simd<float, 128>{0}; //error: write 128-elem to 16-elem storage Signed-off-by: Klochkov, Vyacheslav N <[email protected]>
1 parent d616546 commit 197c33a

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

sycl/include/sycl/ext/intel/esimd/detail/intrin.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ __esimd_rdindirect(__ESIMD_DNS::vector_type_t<T, N> Input,
113113
// for (int i = 0; i < NumRows; ++i) {
114114
// for (int j = 0; j < Width; ++j) {
115115
// if (Mask[Index])
116-
// Result[i * VStride + j * Stride + EltOffset] =
117-
// NewVal[Index];
116+
// Result[i * VStride + j * Stride + EltOffset] = NewVal[Index];
118117
// ++Index;
119118
// }
120119
// }
@@ -143,9 +142,9 @@ template <class T> using __st = __raw_t<T>;
143142

144143
/// read from a basic region of a vector, return a vector
145144
template <typename BT, int BN, typename RTy>
146-
__ESIMD_DNS::vector_type_t<__st<typename RTy::element_type>, RTy::length>
147-
ESIMD_INLINE readRegion(
148-
const __ESIMD_DNS::vector_type_t<__st<BT>, BN> &Base, RTy Region) {
145+
__ESIMD_DNS::vector_type_t<__st<typename RTy::element_type>,
146+
RTy::length> ESIMD_INLINE
147+
readRegion(const __ESIMD_DNS::vector_type_t<__st<BT>, BN> &Base, RTy Region) {
149148
using ElemTy = __st<typename RTy::element_type>;
150149
auto Base1 = bitcast<ElemTy, __st<BT>, BN>(Base);
151150
constexpr int Bytes = BN * sizeof(BT);

sycl/include/sycl/ext/intel/esimd/detail/simd_obj_impl.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,13 @@ class [[__sycl_detail__::__uses_aspects__(
650650
constexpr int M = RTy::Size_x;
651651
constexpr int Stride = RTy::Stride_x;
652652
uint16_t Offset = Region.M_offset_x * sizeof(ElemTy);
653+
static_assert(M > 0, "Malformed RHS region.");
654+
static_assert(M <= BN, "Attempt to write beyond viewed area: The viewed "
655+
"object in LHS does not fit RHS.");
656+
// (M > BN) condition is added below to not duplicate the above assert
657+
// for big values of M. The assert below is for 'Stride'.
658+
static_assert((M > BN) || (M - 1) * Stride < BN,
659+
"Malformed RHS region - too big stride.");
653660

654661
// Merge and update.
655662
auto Merged = __esimd_wrregion<ElemTy, BN, M,
@@ -685,6 +692,11 @@ class [[__sycl_detail__::__uses_aspects__(
685692
constexpr int Stride = TR::Stride_x;
686693
uint16_t Offset = Region.first.M_offset_x * sizeof(ElemTy);
687694

695+
static_assert(M <= BN1, "Attempt to write beyond viewed area: The "
696+
"viewed object in LHS does not fit RHS.");
697+
static_assert(M > 0, "Malformed RHS region.");
698+
static_assert((M - 1) * Stride < BN,
699+
"Malformed RHS region - too big stride.");
688700
// Merge and update.
689701
Base1 = __esimd_wrregion<ElemTy, BN1, M,
690702
/*VS*/ 0, M, Stride>(Base1, Val, Offset);
@@ -702,6 +714,12 @@ class [[__sycl_detail__::__uses_aspects__(
702714
(Region.first.M_offset_y * PaTy::Size_x + Region.first.M_offset_x) *
703715
sizeof(ElemTy));
704716

717+
static_assert(M <= BN1, "Attempt to write beyond viewed area: The "
718+
"viewed object in LHS does not fit RHS.");
719+
static_assert(M > 0 && W > 0 && M % W == 0, "Malformed RHS region.");
720+
static_assert(W == 0 || ((M / W) - 1) * VS + (W - 1) * HS < BN1,
721+
"Malformed RHS region - too big vertical and/or "
722+
"horizontal stride.");
705723
// Merge and update.
706724
Base1 = __esimd_wrregion<ElemTy, BN1, M, VS, W, HS, ParentWidth>(
707725
Base1, Val, Offset);

sycl/test/esimd/wrregion.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ using namespace sycl::ext::intel::esimd;
99
// test wrregion size checks.
1010
SYCL_ESIMD_FUNCTION void test_wrregion_size_check() {
1111
simd<int, 16> v16 = 0;
12-
v16.template select<64, 1>(0) = slm_block_load<int, 64>(0);
13-
// expected-error@* {{no matching function for call to '__esimd_wrregion'}}
12+
simd<int, 64> v64;
13+
v16.template select<64, 1>(0) = v64;
14+
// expected-error@* {{static assertion failed due to requirement 'M <= BN'}}
1415
// expected-note@sycl/ext/intel/esimd/detail/simd_view_impl.hpp:* {{in instantiation of function template specialization}}
1516
// expected-note@sycl/ext/intel/esimd/detail/simd_view_impl.hpp:* {{in instantiation of member function}}
16-
// expected-note@* {{operator=' requested here}}
17+
// [email protected]:* {{in instantiation of member function}}
18+
// expected-note@sycl/ext/intel/esimd/detail/simd_obj_impl.hpp:* {{expression evaluates to '64 <= 16'}}
19+
20+
// expected-error@* {{no matching function for call to '__esimd_wrregion'}}
1721
// expected-note@sycl/ext/intel/esimd/detail/intrin.hpp:* {{candidate template ignored: requirement '64 <= 16' was not satisfied}}
22+
23+
simd<int, 2> v2;
24+
v16.template select<2, 64>() = v2;
25+
// expected-error@* {{static assertion failed due to requirement '(M > BN) || (M - 1) * Stride < BN'}}
26+
// expected-note@sycl/ext/intel/esimd/detail/simd_view_impl.hpp:* {{in instantiation of function template specialization}}
27+
// expected-note@sycl/ext/intel/esimd/detail/simd_view_impl.hpp:* {{in instantiation of member function}}
28+
// [email protected]:* {{in instantiation of member function}}
1829
}

0 commit comments

Comments
 (0)