Skip to content

Commit b71b5ac

Browse files
[SYCL] Don't use sycl::vec::vector_t in built-in functions (#16530)
* `vector_t` is expected to be removed in KhronosGroup/SYCL-Docs#676 * we aren't required to use it here * `operator vector_t`/`vec(vector_t)` are implemented as a simple `bit_cast` anyway, can use it explicitly as well.
1 parent 2d475de commit b71b5ac

File tree

3 files changed

+9
-37
lines changed

3 files changed

+9
-37
lines changed

sycl/include/sycl/detail/builtins/builtins.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ template <typename T> auto convert_arg(T &&x) {
9393
using result_type = std::conditional_t<N == 1, converted_elem_type,
9494
converted_elem_type
9595
__attribute__((ext_vector_type(N)))>;
96-
// TODO: We should have this bit_cast impl inside vec::convert.
97-
return bit_cast<result_type>(static_cast<typename no_cv_ref::vector_t>(x));
96+
return bit_cast<result_type>(x);
9897
} else if constexpr (is_swizzle_v<no_cv_ref>) {
9998
return convert_arg(simplify_if_swizzle_t<no_cv_ref>{x});
10099
} else {
@@ -104,14 +103,6 @@ template <typename T> auto convert_arg(T &&x) {
104103
return convertToOpenCLType(std::forward<T>(x));
105104
}
106105
}
107-
108-
template <typename RetTy, typename T> auto convert_result(T &&x) {
109-
if constexpr (is_vec_v<RetTy>) {
110-
return bit_cast<typename RetTy::vector_t>(x);
111-
} else {
112-
return std::forward<T>(x);
113-
}
114-
}
115106
#endif
116107
} // namespace builtins
117108

sycl/include/sycl/detail/builtins/integer_functions.inc

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ BUILTIN_CREATE_ENABLER(builtin_enable_suint32, default_ret_type,
3838
NUM_ARGS, NAME, builtin_enable_integer_t, [](auto... xs) { \
3939
using ret_ty = \
4040
detail::builtin_enable_integer_t<NUM_ARGS##_TEMPLATE_TYPE>; \
41-
return detail::builtins::convert_result<ret_ty>( \
42-
__spirv_ocl_##NAME(xs...)); \
41+
return bit_cast<ret_ty>(__spirv_ocl_##NAME(xs...)); \
4342
})
4443
#else
4544
#define BUILTIN_GENINT(NUM_ARGS, NAME) \
@@ -54,11 +53,10 @@ BUILTIN_CREATE_ENABLER(builtin_enable_suint32, default_ret_type,
5453
NUM_ARGS, NAME, builtin_enable_integer_t, [](auto... xs) { \
5554
using ret_ty = \
5655
detail::builtin_enable_integer_t<NUM_ARGS##_TEMPLATE_TYPE>; \
57-
using detail::builtins::convert_result; \
5856
if constexpr (std::is_signed_v<detail::get_elem_type_t<T0>>) \
59-
return convert_result<ret_ty>(__spirv_ocl_s_##NAME(xs...)); \
57+
return bit_cast<ret_ty>(__spirv_ocl_s_##NAME(xs...)); \
6058
else \
61-
return convert_result<ret_ty>(__spirv_ocl_u_##NAME(xs...)); \
59+
return bit_cast<ret_ty>(__spirv_ocl_u_##NAME(xs...)); \
6260
})
6361
#else
6462
#define BUILTIN_GENINT_SU(NUM_ARGS, NAME) BUILTIN_GENINT(NUM_ARGS, NAME)
@@ -67,15 +65,14 @@ BUILTIN_CREATE_ENABLER(builtin_enable_suint32, default_ret_type,
6765
#if __SYCL_DEVICE_ONLY__
6866
DEVICE_IMPL_TEMPLATE(ONE_ARG, abs, builtin_enable_integer_t, [](auto x) {
6967
using ret_ty = detail::builtin_enable_integer_t<T0>;
70-
using detail::builtins::convert_result;
7168
if constexpr (std::is_signed_v<detail::get_elem_type_t<T0>>)
7269
// SPIR-V builtin returns unsigned type, SYCL's return type is signed
7370
// with the following restriction:
7471
// > The behavior is undefined if the result cannot be represented by
7572
// > the return type
76-
return convert_result<ret_ty>(bit_cast<T0>(__spirv_ocl_s_abs(x)));
73+
return bit_cast<ret_ty>(__spirv_ocl_s_abs(x));
7774
else
78-
return convert_result<ret_ty>(__spirv_ocl_u_abs(x));
75+
return bit_cast<ret_ty>(__spirv_ocl_u_abs(x));
7976
})
8077
#else
8178
BUILTIN_GENINT_SU(ONE_ARG, abs)
@@ -87,25 +84,10 @@ BUILTIN_GENINT_SU(TWO_ARGS, add_sat)
8784
DEVICE_IMPL_TEMPLATE(
8885
TWO_ARGS, abs_diff, builtin_enable_integer_t, [](auto... xs) {
8986
using ret_ty = detail::builtin_enable_integer_t<T0>;
90-
using detail::builtins::convert_result;
9187
if constexpr (std::is_signed_v<detail::get_elem_type_t<T0>>) {
92-
// SPIRV built-in returns [vector of] unsigned type(s).
93-
auto ret = __spirv_ocl_s_abs_diff(xs...);
94-
if constexpr (detail::is_vec_v<T0>) {
95-
// SYCL 2020 revision 8's abs_diff returns T0 (or corresponding vec in
96-
// case of a swizzle). The only way to produce signed ext_vector_type
97-
// from unsigned is with C-style case. Also note that element type of
98-
// sycl::vec and ext_vector_type might be different, e.g.
99-
// sycl::vec<char, N>::vector_t is
100-
// signed char __attribute__((ext_vector_type(N))).
101-
//
102-
// TODO: Shouldn't be different from "abs" above.
103-
return convert_result<ret_ty>((typename T0::vector_t)(ret));
104-
} else {
105-
return convert_result<ret_ty>(ret);
106-
}
88+
return bit_cast<ret_ty>(__spirv_ocl_s_abs_diff(xs...));
10789
} else {
108-
return convert_result<ret_ty>(__spirv_ocl_u_abs_diff(xs...));
90+
return bit_cast<ret_ty>(__spirv_ocl_u_abs_diff(xs...));
10991
}
11092
})
11193
#else

sycl/include/sycl/detail/builtins/relational_functions.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ DEVICE_IMPL_TEMPLATE(
133133
THREE_ARGS, bitselect, builtin_enable_bitselect_t, [](auto... xs) {
134134
using ret_ty =
135135
detail::builtin_enable_bitselect_t<THREE_ARGS_TEMPLATE_TYPE>;
136-
using detail::builtins::convert_result;
137-
return convert_result<ret_ty>(__spirv_ocl_bitselect(xs...));
136+
return bit_cast<ret_ty>(__spirv_ocl_bitselect(xs...));
138137
})
139138
#else
140139
HOST_IMPL_TEMPLATE(THREE_ARGS, bitselect, builtin_enable_bitselect_t, rel,

0 commit comments

Comments
 (0)