Skip to content

Commit eaff40c

Browse files
[NFCI][SYCL] Eliminate sycl/builtins_utils_*.hpp (#16931)
Split the helpers they used to provide between the builtins implementation and `sycl/detail/type_traits/vec_marray_traits.hpp`.
1 parent d160d75 commit eaff40c

File tree

6 files changed

+96
-155
lines changed

6 files changed

+96
-155
lines changed

sycl/include/sycl/builtins_utils_scalar.hpp

Lines changed: 0 additions & 73 deletions
This file was deleted.

sycl/include/sycl/builtins_utils_vec.hpp

Lines changed: 0 additions & 79 deletions
This file was deleted.

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,73 @@
6363

6464
#pragma once
6565

66-
#include <sycl/builtins_utils_vec.hpp>
66+
#include <sycl/detail/type_traits.hpp>
67+
#include <sycl/detail/type_traits/vec_marray_traits.hpp>
68+
#include <sycl/detail/vector_convert.hpp>
69+
#include <sycl/marray.hpp> // for marray
70+
#include <sycl/vector.hpp> // for vec
6771

6872
namespace sycl {
6973
inline namespace _V1 {
7074
namespace detail {
75+
#ifdef __FAST_MATH__
76+
template <typename T>
77+
struct use_fast_math
78+
: std::is_same<std::remove_cv_t<get_elem_type_t<T>>, float> {};
79+
#else
80+
template <typename> struct use_fast_math : std::false_type {};
81+
#endif
82+
template <typename T> constexpr bool use_fast_math_v = use_fast_math<T>::value;
83+
84+
// Utility trait for getting the decoration of a multi_ptr.
85+
template <typename T> struct get_multi_ptr_decoration;
86+
template <typename ElementType, access::address_space Space,
87+
access::decorated DecorateAddress>
88+
struct get_multi_ptr_decoration<
89+
multi_ptr<ElementType, Space, DecorateAddress>> {
90+
static constexpr access::decorated value = DecorateAddress;
91+
};
92+
93+
template <typename T>
94+
constexpr access::decorated get_multi_ptr_decoration_v =
95+
get_multi_ptr_decoration<T>::value;
96+
97+
// Utility trait for checking if a multi_ptr has a "writable" address space,
98+
// i.e. global, local, private or generic.
99+
template <typename T> struct has_writeable_addr_space : std::false_type {};
100+
template <typename ElementType, access::address_space Space,
101+
access::decorated DecorateAddress>
102+
struct has_writeable_addr_space<multi_ptr<ElementType, Space, DecorateAddress>>
103+
: std::bool_constant<Space == access::address_space::global_space ||
104+
Space == access::address_space::local_space ||
105+
Space == access::address_space::private_space ||
106+
Space == access::address_space::generic_space> {};
107+
108+
template <typename T>
109+
constexpr bool has_writeable_addr_space_v = has_writeable_addr_space<T>::value;
110+
111+
// Utility trait for changing the element type of a type T. If T is a scalar,
112+
// the new type replaces T completely.
113+
template <typename NewElemT, typename T, typename = void>
114+
struct change_elements {
115+
using type = NewElemT;
116+
};
117+
template <typename NewElemT, typename T>
118+
struct change_elements<NewElemT, T, std::enable_if_t<is_marray_v<T>>> {
119+
using type =
120+
marray<typename change_elements<NewElemT, typename T::value_type>::type,
121+
T::size()>;
122+
};
123+
template <typename NewElemT, typename T>
124+
struct change_elements<NewElemT, T, std::enable_if_t<is_vec_or_swizzle_v<T>>> {
125+
using type =
126+
vec<typename change_elements<NewElemT, typename T::element_type>::type,
127+
T::size()>;
128+
};
129+
130+
template <typename NewElemT, typename T>
131+
using change_elements_t = typename change_elements<NewElemT, T>::type;
132+
71133
template <typename... Ts>
72134
inline constexpr bool builtin_same_shape_v =
73135
((... && is_scalar_arithmetic_v<Ts>) || (... && is_marray_v<Ts>) ||
@@ -80,6 +142,23 @@ inline constexpr bool builtin_same_or_swizzle_v =
80142
// Use builtin_same_shape_v to filter out types unrelated to builtins.
81143
builtin_same_shape_v<Ts...> && all_same_v<simplify_if_swizzle_t<Ts>...>;
82144

145+
// Utility functions for converting to/from vec/marray.
146+
template <class T, size_t N> vec<T, 2> to_vec2(marray<T, N> X, size_t Start) {
147+
return {X[Start], X[Start + 1]};
148+
}
149+
template <class T, size_t N> vec<T, N> to_vec(marray<T, N> X) {
150+
vec<T, N> Vec;
151+
for (size_t I = 0; I < N; I++)
152+
Vec[I] = X[I];
153+
return Vec;
154+
}
155+
template <class T, int N> marray<T, N> to_marray(vec<T, N> X) {
156+
marray<T, N> Marray;
157+
for (size_t I = 0; I < N; I++)
158+
Marray[I] = X[I];
159+
return Marray;
160+
}
161+
83162
namespace builtins {
84163
#ifdef __SYCL_DEVICE_ONLY__
85164
template <typename T> auto convert_arg(T &&x) {

sycl/include/sycl/detail/type_traits/vec_marray_traits.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ template <typename VecT, typename OperationLeftT, typename OperationRightT,
2424
template <typename> class OperationCurrentT, int... Indexes>
2525
class SwizzleOp;
2626

27+
// Utility for converting a swizzle to a vector or preserve the type if it isn't
28+
// a swizzle.
29+
template <typename T> struct simplify_if_swizzle {
30+
using type = T;
31+
};
32+
33+
template <typename VecT, typename OperationLeftT, typename OperationRightT,
34+
template <typename> class OperationCurrentT, int... Indexes>
35+
struct simplify_if_swizzle<SwizzleOp<VecT, OperationLeftT, OperationRightT,
36+
OperationCurrentT, Indexes...>> {
37+
using type = vec<typename VecT::element_type, sizeof...(Indexes)>;
38+
};
39+
40+
template <typename T>
41+
using simplify_if_swizzle_t = typename simplify_if_swizzle<T>::type;
42+
2743
// --------- is_* traits ------------------ //
2844
template <typename> struct is_vec : std::false_type {};
2945
template <typename T, int N> struct is_vec<vec<T, N>> : std::true_type {};

sycl/include/sycl/ext/oneapi/experimental/bfloat16_math.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#pragma once
1010

1111
#include <sycl/builtins.hpp> // for ceil, cos, exp, exp10, exp2
12-
#include <sycl/builtins_utils_vec.hpp> // For simplify_if_swizzle, is_swizzle
1312
#include <sycl/detail/memcpy.hpp> // sycl::detail::memcpy
1413
#include <sycl/detail/vector_convert.hpp>
1514
#include <sycl/ext/oneapi/bfloat16.hpp> // for bfloat16, bfloat16ToBits

sycl/include/sycl/ext/oneapi/experimental/builtins.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include <sycl/aliases.hpp> // for half
1212
#include <sycl/builtins.hpp> // for to_vec2
13-
#include <sycl/builtins_utils_vec.hpp> // for to_vec, to_marray...
1413
#include <sycl/detail/defines_elementary.hpp> // for __SYCL_ALWAYS_INLINE
1514
#include <sycl/detail/generic_type_traits.hpp> // for is_svgenfloath, is_sv...
1615
#include <sycl/detail/memcpy.hpp> // detail::memcpy

0 commit comments

Comments
 (0)