Skip to content

[SYCL] Change vec::operator! to match SYCL spec #11750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions sycl/include/sycl/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,22 @@ template <typename Type, int NumElements> class vec {
return Ret;
}

template <typename T>
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
using OpNotRet = detail::rel_t<T>;
#else
using OpNotRet = T;
#endif // __INTEL_PREVIEW_BREAKING_CHANGES

// operator!
template <typename T = DataT, int N = NumElements>
EnableIfNotUsingArray<vec<T, N>> operator!() const {
return vec<T, N>{(typename vec<DataT, NumElements>::DataType) !m_Data};
EnableIfNotUsingArray<vec<OpNotRet<T>, N>> operator!() const {
return vec<T, N>{(typename vec<DataT, NumElements>::DataType) !m_Data}
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
.template as<vec<OpNotRet<T>, N>>();
#else
;
#endif // __INTEL_PREVIEW_BREAKING_CHANGES
}

// std::byte neither supports ! unary op or casting, so special handling is
Expand All @@ -1191,32 +1203,44 @@ template <typename Type, int NumElements> class vec {
template <typename T = DataT, int N = NumElements>
typename std::enable_if_t<std::is_same_v<std::byte, T> &&
(IsUsingArrayOnDevice || IsUsingArrayOnHost),
vec<T, N>>
vec<OpNotRet<T>, N>>
operator!() const {
vec Ret{};
for (size_t I = 0; I < NumElements; ++I) {
Ret.setValue(I, std::byte{!vec_data<DataT>::get(getValue(I))});
}
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
return Ret.template as<vec<OpNotRet<T>, N>>();
#else
return Ret;
#endif // __INTEL_PREVIEW_BREAKING_CHANGES
}

template <typename T = DataT, int N = NumElements>
typename std::enable_if_t<!std::is_same_v<std::byte, T> &&
(IsUsingArrayOnDevice || IsUsingArrayOnHost),
vec<T, N>>
vec<OpNotRet<T>, N>>
operator!() const {
vec Ret{};
for (size_t I = 0; I < NumElements; ++I)
Ret.setValue(I, !vec_data<DataT>::get(getValue(I)));
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
return Ret.template as<vec<OpNotRet<T>, N>>();
#else
return Ret;
#endif // __INTEL_PREVIEW_BREAKING_CHANGES
}
#else
template <typename T = DataT, int N = NumElements>
EnableIfUsingArray<vec<T, N>> operator!() const {
EnableIfUsingArray<vec<OpNotRet<T>, N>> operator!() const {
vec Ret{};
for (size_t I = 0; I < NumElements; ++I)
Ret.setValue(I, !vec_data<DataT>::get(getValue(I)));
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
return Ret.template as<vec<OpNotRet<T>, N>>();
#else
return Ret;
#endif // __INTEL_PREVIEW_BREAKING_CHANGES
}
#endif

Expand Down
31 changes: 30 additions & 1 deletion sycl/test/basic_tests/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ std::string vec2string(const sycl::vec<vecType, numOfElems> &vec) {
return str;
}

// vec::operator! might return a different type as described in Table 143 of the
// SYCL 2020 specification. This function checks that the result type matches
// the expected type.
template <typename T, typename Expected> inline void checkVecNotReturnType() {
constexpr int N = 4;
using Vector = sycl::vec<T, N>;
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
using ExpectedVector = sycl::vec<Expected, N>;
#else
using ExpectedVector = sycl::vec<T, N>;
#endif
using OpNotResult = decltype(std::declval<Vector>().operator!());
static_assert(std::is_same_v<OpNotResult, ExpectedVector>,
"Incorrect vec::operator! return type");
}

// the math built-in testing ensures that the vec binary ops get tested,
// but the unary ops are only tested by the CTS tests. Here we do some
// basic testing of the unary ops, ensuring they compile correctly.
Expand All @@ -145,8 +161,21 @@ template <typename T> void checkVecUnaryOps(T &v) {
std::cout << vec2string(g) << std::endl;
}

T f = !v;
auto f = !v;
std::cout << vec2string(f) << std::endl;

// Check operator! return type
checkVecNotReturnType<int8_t, int8_t>();
checkVecNotReturnType<uint8_t, int8_t>();
checkVecNotReturnType<int16_t, int16_t>();
checkVecNotReturnType<uint16_t, int16_t>();
checkVecNotReturnType<sycl::half, int16_t>();
checkVecNotReturnType<int32_t, int32_t>();
checkVecNotReturnType<uint32_t, int32_t>();
checkVecNotReturnType<float, int32_t>();
checkVecNotReturnType<int64_t, int64_t>();
checkVecNotReturnType<uint64_t, int64_t>();
checkVecNotReturnType<double, int64_t>();
}

void checkVariousVecUnaryOps() {
Expand Down