Skip to content

[SYCL] Fix integer vec conversions #11821

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

74 changes: 14 additions & 60 deletions sycl/include/sycl/detail/vector_convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,9 @@ using is_uint_to_uint =
std::bool_constant<is_sugeninteger_v<T> && is_sugeninteger_v<R>>;

template <typename T, typename R>
using is_sint_to_uint =
std::bool_constant<is_sigeninteger_v<T> && is_sugeninteger_v<R>>;

template <typename T, typename R>
using is_uint_to_sint =
std::bool_constant<is_sugeninteger_v<T> && is_sigeninteger_v<R>>;
using is_sint_to_from_uint = std::bool_constant<
(detail::is_sigeninteger_v<T> && detail::is_sugeninteger_v<R>) ||
(detail::is_sugeninteger_v<T> && detail::is_sigeninteger_v<R>)>;

template <typename T, typename R>
using is_sint_to_float =
Expand Down Expand Up @@ -146,18 +143,6 @@ To ConvertUToF(From Value) {
return static_cast<To>(Value);
}

template <typename From, typename To, int VecSize,
typename Enable = std::enable_if_t<VecSize == 1>>
To SatConvertSToU(From Value) {
return static_cast<To>(Value);
}

template <typename From, typename To, int VecSize,
typename Enable = std::enable_if_t<VecSize == 1>>
To SatConvertUToS(From Value) {
return static_cast<To>(Value);
}

template <typename From, typename To, int VecSize,
typename Enable = std::enable_if_t<VecSize == 1>,
sycl::rounding_mode RM>
Expand Down Expand Up @@ -319,44 +304,6 @@ __SYCL_INT_INT_CONVERT(U, ulong)
#undef __SYCL_VECTOR_INT_INT_CONVERT
#undef __SYCL_INT_INT_CONVERT

// signed to unsigned, unsigned to signed conversions
#define __SYCL_SCALAR_SINT_UINT_CONVERT(Op, DestType) \
template <typename From, typename To, int VecSize, typename Enable> \
enable_if_to_int_scalar_t<sycl::opencl::cl_##DestType, Enable, VecSize, To> \
SatConvert##Op(From value) { \
return __spirv_SatConvert##Op##_R##DestType(value); \
}

#define __SYCL_VECTOR_SINT_UINT_CONVERT(Op, N, DestType) \
template <typename From, typename To, int VecSize, typename Enable> \
enable_if_to_int_vector_t<sycl::opencl::cl_##DestType, Enable, N, VecSize, \
To> \
SatConvert##Op(From value) { \
return __spirv_SatConvert##Op##_R##DestType##N(value); \
}

#define __SYCL_SINT_UINT_CONVERT(Op, DestType) \
__SYCL_SCALAR_SINT_UINT_CONVERT(Op, DestType) \
__SYCL_VECTOR_SINT_UINT_CONVERT(Op, 2, DestType) \
__SYCL_VECTOR_SINT_UINT_CONVERT(Op, 3, DestType) \
__SYCL_VECTOR_SINT_UINT_CONVERT(Op, 4, DestType) \
__SYCL_VECTOR_SINT_UINT_CONVERT(Op, 8, DestType) \
__SYCL_VECTOR_SINT_UINT_CONVERT(Op, 16, DestType)

__SYCL_SINT_UINT_CONVERT(UToS, char)
__SYCL_SINT_UINT_CONVERT(UToS, short)
__SYCL_SINT_UINT_CONVERT(UToS, int)
__SYCL_SINT_UINT_CONVERT(UToS, long)

__SYCL_SINT_UINT_CONVERT(SToU, uchar)
__SYCL_SINT_UINT_CONVERT(SToU, ushort)
__SYCL_SINT_UINT_CONVERT(SToU, uint)
__SYCL_SINT_UINT_CONVERT(SToU, ulong)

#undef __SYCL_SCALAR_SINT_UINT_CONVERT
#undef __SYCL_VECTOR_SINT_UINT_CONVERT
#undef __SYCL_SINT_UINT_CONVERT

// float to signed, float to unsigned conversion
#define __SYCL_SCALAR_FLOAT_INT_CONVERT(Op, DestType, RoundingMode, \
RoundingModeCondition) \
Expand Down Expand Up @@ -591,11 +538,18 @@ NativeToT convertImpl(NativeFromT Value) {
else if constexpr (is_float_to_uint<FromT, ToT>::value)
return ConvertFToU<NativeFromT, NativeToT, VecSize, ElemTy, RoundingMode>(
Value);
else if constexpr (is_sint_to_uint<FromT, ToT>::value)
return SatConvertSToU<NativeFromT, NativeToT, VecSize, ElemTy>(Value);
else {
static_assert(is_uint_to_sint<FromT, ToT>::value);
return SatConvertUToS<NativeFromT, NativeToT, VecSize, ElemTy>(Value);
static_assert(is_sint_to_from_uint<FromT, ToT>::value,
"Unexpected conversion type");
static_assert(VecSize == 1, "Conversion between signed and unsigned data "
"types is only available for scalars");
// vec::convert is underspecified and therefore it is not entirely clear
// what to do here. 'static_cast' implementation matches SYCL CTS and it
// matches our old implementation. Unfortunately, OpSetConvertUToS and
// OpSatConvertSToU behave differently and we can't use them here until the
// behavior of conversions is well-defined by the SYCL 2020 specificiation.
// See https://github.com/KhronosGroup/SYCL-Docs/issues/492
return static_cast<NativeToT>(Value);
}
}

Expand Down
40 changes: 27 additions & 13 deletions sycl/include/sycl/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,9 @@ template <typename Type, int NumElements> class vec {
if constexpr (!std::is_same_v<DataT, convertT>) {
// Dummy conversion for cases like vec<signed char> -> vec<char>
vec<convertT, NumElements> Result;
for (size_t I = 0; I < NumElements; ++I) {
Result.setValue(I, vec_data<convertT>::get(static_cast<convertT>(
vec_data<DataT>::get(getValue(I)))));
}
for (size_t I = 0; I < NumElements; ++I)
Result.setValue(I, static_cast<convertT>(getValue(I)));

return Result;
} else {
// No conversion necessary
Expand All @@ -779,15 +778,33 @@ template <typename Type, int NumElements> class vec {
using OpenCLT = detail::ConvertToOpenCLType_t<T>;
using OpenCLR = detail::ConvertToOpenCLType_t<R>;
vec<convertT, NumElements> Result;

#if defined(__INTEL_PREVIEW_BREAKING_CHANGES) && defined(__SYCL_DEVICE_ONLY__)
using OpenCLVecT = OpenCLT __attribute__((ext_vector_type(NumElements)));
using OpenCLVecR = OpenCLR __attribute__((ext_vector_type(NumElements)));
if constexpr (NativeVec && vec<convertT, NumElements>::NativeVec &&
std::is_convertible_v<decltype(m_Data), OpenCLVecT> &&
std::is_convertible_v<decltype(Result.m_Data), OpenCLR>) {
// If both vectors are representable as native vectors and these native
// vectors can be converted to valid OpenCL representations, then we can
// use a single vector-wide operation to do a conversion:
// Whole vector conversion can only be done, if:
constexpr bool canUseNativeVectorConvert =
#ifdef __NVPTX__
// - we are not on CUDA, see intel/llvm#11840
false &&
#endif
// - both vectors are represented using native vector types;
NativeVec && vec<convertT, NumElements>::NativeVec &&
// - vec storage has an equivalent OpenCL native vector it is implicitly
// convertible to. There are some corner cases where it is not the
// case with char, long and long long types.
std::is_convertible_v<decltype(m_Data), OpenCLVecT> &&
std::is_convertible_v<decltype(Result.m_Data), OpenCLVecR> &&
// - it is not a signed to unsigned (or vice versa) conversion
// see comments within 'convertImpl' for more details;
!detail::is_sint_to_from_uint<T, R>::value &&
// - destination type is not bool. bool is stored as integer under the
// hood and therefore conversion to bool looks like conversion between
// two integer types. Since bit pattern for true and false is not
// defined, there is no guarantee that integer conversion yields
// right results here;
!std::is_same_v<convertT, bool>;
if constexpr (canUseNativeVectorConvert) {
Result.m_Data = detail::convertImpl<T, R, roundingMode, NumElements,
OpenCLVecT, OpenCLVecR>(m_Data);
} else
Expand All @@ -803,9 +820,6 @@ template <typename Type, int NumElements> class vec {
}
}

if constexpr (std::is_same_v<convertT, bool>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frasercrmck, FYI, this PR re-implements your fix

Result.ConvertToDataT();
}
return Result;
}

Expand Down
158 changes: 158 additions & 0 deletions sycl/test-e2e/Basic/vector/int-convert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Basic acceptance test which checks vec::convert implementation on both
// host and device. Coverage is limited to vec<T, 4> only, rest of vector sizes
// are covered by SYCL-CTS.
//
// Macro is passed to silence warnings about sycl::byte
//
// XFAIL: cuda
// FIXME: un-xfail the test once intel/llvm#11840 is resolved
//
// RUN: %{build} -o %t.out -DSYCL2020_DISABLE_DEPRECATION_WARNINGS
// RUN: %{run} %t.out
//
// RUN: %if preview-breaking-changes-supported %{ %{build} -fpreview-breaking-changes -DSYCL2020_DISABLE_DEPRECATION_WARNINGS %s -o %t2.out %}
// RUN: %if preview-breaking-changes-supported %{ %{run} %t2.out %}

#include <sycl/sycl.hpp>

#include <cstdint>
#include <iostream>
#include <string>
#include <type_traits>

template <typename T> std::string to_string() { return "unknown type"; }
template <> std::string to_string<std::byte>() { return "std::byte"; }
template <> std::string to_string<char>() { return "char"; }
template <> std::string to_string<signed char>() { return "signed char"; }
template <> std::string to_string<short>() { return "short"; }
template <> std::string to_string<int>() { return "int"; }
template <> std::string to_string<long>() { return "long"; }
template <> std::string to_string<long long>() { return "long long"; }
template <> std::string to_string<unsigned char>() { return "unsigned char"; }
template <> std::string to_string<unsigned short>() { return "unsigned short"; }
template <> std::string to_string<unsigned int>() { return "unsigned int"; }
template <> std::string to_string<unsigned long>() { return "unsigned long"; }
template <> std::string to_string<unsigned long long>() {
return "unsigned long long";
}
template <> std::string to_string<bool>() { return "bool"; }

template <typename T>
bool check_vectors_equal(sycl::vec<T, 4> a, sycl::vec<T, 4> b,
const std::string &fail_message) {
bool result =
a.x() == b.x() && a.y() == b.y() && a.z() == b.z() && a.w() == b.w();
if (!result) {
std::cout << fail_message << std::endl;
std::cout << "\t{" << static_cast<int>(a.x()) << ", "
<< static_cast<int>(a.y()) << ", " << static_cast<int>(a.z())
<< ", " << static_cast<int>(a.w()) << "} vs {"
<< static_cast<int>(b.x()) << ", " << static_cast<int>(b.y())
<< ", " << static_cast<int>(b.z()) << ", "
<< static_cast<int>(b.w()) << "}" << std::endl;
}

return result;
}

template <typename From, typename To> bool check_convert() {
sycl::vec<From, 4> input;
if constexpr (std::is_signed_v<From>) {
input = sycl::vec<From, 4>{static_cast<From>(37), static_cast<From>(0),
static_cast<From>(-11), static_cast<From>(13)};
} else {
input = sycl::vec<From, 4>{static_cast<From>(37), static_cast<From>(0),
static_cast<From>(11), static_cast<From>(13)};
}

sycl::vec<To, 4> hostResult = input.template convert<To>();

sycl::buffer<sycl::vec<To, 4>> buf(sycl::range{1});
sycl::queue q;
q.submit([&](sycl::handler &cgh) {
sycl::accessor acc(buf, cgh);
cgh.single_task([=]() { acc[0] = input.template convert<To>(); });
}).wait();

auto acc = buf.get_host_access();
auto deviceResult = acc[0];

std::string test =
"(vec<" + to_string<From>() + ", 4>::convert<" + to_string<To>() + ">)";

// Host and device results must match.
bool host_and_device_match = check_vectors_equal(
hostResult, deviceResult, "host and device results do not match " + test);
// And they should match with a reference, which is for integer conversions
// can be computed with a simple static_cast.
// Strictly speaking, integer conversions are underspecified in the SYCL 2020
// spec, but `static_cast` implementation matches SYCL-CTS, so we will leave
// it here for now as well.
// See https://github.com/KhronosGroup/SYCL-Docs/issues/492
sycl::vec<To, 4> reference{
static_cast<To>(input.x()), static_cast<To>(input.y()),
static_cast<To>(input.z()), static_cast<To>(input.w())};
bool device_matches_reference = check_vectors_equal(
deviceResult, reference, "device results don't match reference " + test);
bool host_matches_reference = check_vectors_equal(
hostResult, reference, "host resutls don't match reference " + test);

return host_and_device_match && device_matches_reference &&
host_matches_reference;
}

template <class T>
constexpr auto has_unsigned_v =
std::is_integral_v<T> && !std::is_same_v<T, bool> &&
!std::is_same_v<T, sycl::byte> && !std::is_same_v<T, std::byte>;

template <typename From, typename To> bool check_signed_unsigned_convert_to() {
bool pass = true;
pass &= check_convert<From, To>();
if constexpr (has_unsigned_v<To>)
pass &= check_convert<From, std::make_unsigned_t<To>>();
if constexpr (has_unsigned_v<From>)
pass &= check_convert<std::make_unsigned_t<From>, To>();
if constexpr (has_unsigned_v<To> && has_unsigned_v<From>)
pass &=
check_convert<std::make_unsigned_t<From>, std::make_unsigned_t<To>>();
return pass;
}

template <typename From> bool check_convert_from() {
bool pass = true;
pass &= check_signed_unsigned_convert_to<From, sycl::byte>();
pass &= check_signed_unsigned_convert_to<From, std::byte>();
pass &= check_signed_unsigned_convert_to<From, std::int8_t>();
pass &= check_signed_unsigned_convert_to<From, std::int16_t>();
pass &= check_signed_unsigned_convert_to<From, std::int32_t>();
pass &= check_signed_unsigned_convert_to<From, std::int64_t>();
pass &= check_signed_unsigned_convert_to<From, bool>();
pass &= check_signed_unsigned_convert_to<From, char>();
pass &= check_signed_unsigned_convert_to<From, signed char>();
pass &= check_signed_unsigned_convert_to<From, short>();
pass &= check_signed_unsigned_convert_to<From, int>();
pass &= check_signed_unsigned_convert_to<From, long>();
pass &= check_signed_unsigned_convert_to<From, long long>();

return pass;
}

int main() {
bool pass = true;
pass &= check_convert_from<sycl::byte>();
pass &= check_convert_from<std::byte>();
pass &= check_convert_from<std::int8_t>();
pass &= check_convert_from<std::int16_t>();
pass &= check_convert_from<std::int32_t>();
pass &= check_convert_from<std::int64_t>();
pass &= check_convert_from<char>();
pass &= check_convert_from<signed char>();
pass &= check_convert_from<short>();
pass &= check_convert_from<int>();
pass &= check_convert_from<long>();
pass &= check_convert_from<long long>();
pass &= check_convert_from<bool>();

return static_cast<int>(!pass);
}