Skip to content

Commit d010d81

Browse files
[NFC][SYCL] Move vector/marray type traits into a standalone header (#15693)
Use `sycl::vec`/`sycl::marray` forward declaration to remove all the dependencies, so that the new header could later be used in various type traits without introducing circular dependency.
1 parent 06d3d10 commit d010d81

File tree

5 files changed

+111
-52
lines changed

5 files changed

+111
-52
lines changed

sycl/include/sycl/builtins_utils_vec.hpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#pragma once
1010

11+
#include <sycl/detail/type_traits/vec_marray_traits.hpp>
12+
1113
#include <sycl/builtins_utils_scalar.hpp>
1214

1315
#include <sycl/detail/type_traits.hpp>
@@ -41,19 +43,6 @@ template <typename ElementType, typename... Ts>
4143
struct is_valid_elem_type<ElementType *, Ts...>
4244
: std::bool_constant<check_type_in_v<ElementType, Ts...>> {};
4345

44-
// Utility trait for getting the number of elements in T.
45-
template <typename T>
46-
struct num_elements : std::integral_constant<size_t, 1> {};
47-
template <typename T, size_t N>
48-
struct num_elements<marray<T, N>> : std::integral_constant<size_t, N> {};
49-
template <typename T, int N>
50-
struct num_elements<vec<T, N>> : std::integral_constant<size_t, size_t(N)> {};
51-
template <typename VecT, typename OperationLeftT, typename OperationRightT,
52-
template <typename> class OperationCurrentT, int... Indexes>
53-
struct num_elements<SwizzleOp<VecT, OperationLeftT, OperationRightT,
54-
OperationCurrentT, Indexes...>>
55-
: std::integral_constant<size_t, sizeof...(Indexes)> {};
56-
5746
// Utilty trait for checking that the number of elements in T is in Ns.
5847
template <typename T, size_t... Ns>
5948
struct is_valid_size

sycl/include/sycl/detail/type_traits.hpp

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#pragma once
1010

11+
#include <sycl/detail/type_traits/vec_marray_traits.hpp>
12+
1113
#include <sycl/access/access.hpp> // for decorated, address_space
1214
#include <sycl/detail/generic_type_lists.hpp> // for vec, marray, integer_list
1315
#include <sycl/detail/type_list.hpp> // for is_contained, find_twi...
@@ -171,18 +173,6 @@ template <typename ElementType> struct get_elem_type_unqual<ElementType *> {
171173
using type = ElementType;
172174
};
173175

174-
template <typename T, typename = void>
175-
struct is_ext_vector : std::false_type {};
176-
177-
// FIXME: unguarded use of non-standard built-in
178-
template <typename T>
179-
struct is_ext_vector<
180-
T, std::void_t<decltype(__builtin_reduce_max(std::declval<T>()))>>
181-
: std::true_type {};
182-
183-
template <typename T>
184-
inline constexpr bool is_ext_vector_v = is_ext_vector<T>::value;
185-
186176
// FIXME: unguarded use of non-standard built-in
187177
template <typename T>
188178
struct get_elem_type_unqual<T, std::enable_if_t<is_ext_vector_v<T>>> {
@@ -255,11 +245,6 @@ template <typename T, int N, template <typename> class S>
255245
inline constexpr bool is_gen_based_on_type_sizeof_v =
256246
S<T>::value && (sizeof(vector_element_t<T>) == N);
257247

258-
template <typename> struct is_vec : std::false_type {};
259-
template <typename T, int N> struct is_vec<sycl::vec<T, N>> : std::true_type {};
260-
261-
template <typename T> constexpr bool is_vec_v = is_vec<T>::value;
262-
263248
template <typename> struct get_vec_size {
264249
static constexpr int size = 1;
265250
};
@@ -268,27 +253,6 @@ template <typename T, int N> struct get_vec_size<sycl::vec<T, N>> {
268253
static constexpr int size = N;
269254
};
270255

271-
// is_swizzle
272-
template <typename> struct is_swizzle : std::false_type {};
273-
template <typename VecT, typename OperationLeftT, typename OperationRightT,
274-
template <typename> class OperationCurrentT, int... Indexes>
275-
struct is_swizzle<SwizzleOp<VecT, OperationLeftT, OperationRightT,
276-
OperationCurrentT, Indexes...>> : std::true_type {};
277-
278-
template <typename T> constexpr bool is_swizzle_v = is_swizzle<T>::value;
279-
280-
// is_swizzle_or_vec_v
281-
282-
template <typename T>
283-
constexpr bool is_vec_or_swizzle_v = is_vec_v<T> || is_swizzle_v<T>;
284-
285-
// is_marray
286-
template <typename> struct is_marray : std::false_type {};
287-
template <typename T, size_t N>
288-
struct is_marray<sycl::marray<T, N>> : std::true_type {};
289-
290-
template <typename T> constexpr bool is_marray_v = is_marray<T>::value;
291-
292256
// is_integral
293257
template <typename T>
294258
struct is_integral : std::is_integral<get_elem_type_t<T>> {};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//==---------- Forward declarations and traits for vector/marray types -----==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <cstddef>
12+
#include <type_traits>
13+
14+
#include <sycl/detail/defines_elementary.hpp>
15+
16+
namespace sycl {
17+
inline namespace _V1 {
18+
template <typename DataT, int NumElements> class __SYCL_EBO vec;
19+
20+
template <typename DataT, std::size_t N> class marray;
21+
22+
namespace detail {
23+
template <typename VecT, typename OperationLeftT, typename OperationRightT,
24+
template <typename> class OperationCurrentT, int... Indexes>
25+
class SwizzleOp;
26+
27+
// --------- is_* traits ------------------ //
28+
template <typename> struct is_vec : std::false_type {};
29+
template <typename T, int N> struct is_vec<vec<T, N>> : std::true_type {};
30+
template <typename T> constexpr bool is_vec_v = is_vec<T>::value;
31+
32+
template <typename T, typename = void>
33+
struct is_ext_vector : std::false_type {};
34+
#if defined(__has_extension)
35+
#if __has_extension(attribute_ext_vector_type)
36+
template <typename T, int N>
37+
struct is_ext_vector<T __attribute__((ext_vector_type(N)))> : std::true_type {};
38+
#endif
39+
#endif
40+
template <typename T>
41+
inline constexpr bool is_ext_vector_v = is_ext_vector<T>::value;
42+
43+
template <typename> struct is_swizzle : std::false_type {};
44+
template <typename VecT, typename OperationLeftT, typename OperationRightT,
45+
template <typename> class OperationCurrentT, int... Indexes>
46+
struct is_swizzle<SwizzleOp<VecT, OperationLeftT, OperationRightT,
47+
OperationCurrentT, Indexes...>> : std::true_type {};
48+
template <typename T> constexpr bool is_swizzle_v = is_swizzle<T>::value;
49+
50+
template <typename T>
51+
constexpr bool is_vec_or_swizzle_v = is_vec_v<T> || is_swizzle_v<T>;
52+
53+
template <typename> struct is_marray : std::false_type {};
54+
template <typename T, std::size_t N>
55+
struct is_marray<marray<T, N>> : std::true_type {};
56+
template <typename T> constexpr bool is_marray_v = is_marray<T>::value;
57+
58+
// --------- num_elements trait ------------------ //
59+
template <typename T>
60+
struct num_elements : std::integral_constant<std::size_t, 1> {};
61+
template <typename T, std::size_t N>
62+
struct num_elements<marray<T, N>> : std::integral_constant<std::size_t, N> {};
63+
template <typename T, int N>
64+
struct num_elements<vec<T, N>>
65+
: std::integral_constant<std::size_t, std::size_t(N)> {};
66+
#if defined(__has_extension)
67+
#if __has_extension(attribute_ext_vector_type)
68+
template <typename T, int N>
69+
struct num_elements<T __attribute__((ext_vector_type(N)))>
70+
: std::integral_constant<std::size_t, N> {};
71+
#endif
72+
#endif
73+
template <typename VecT, typename OperationLeftT, typename OperationRightT,
74+
template <typename> class OperationCurrentT, int... Indexes>
75+
struct num_elements<SwizzleOp<VecT, OperationLeftT, OperationRightT,
76+
OperationCurrentT, Indexes...>>
77+
: std::integral_constant<std::size_t, sizeof...(Indexes)> {};
78+
79+
template <typename T>
80+
inline constexpr std::size_t num_elements_v = num_elements<T>::value;
81+
82+
// --------- element_type trait ------------------ //
83+
template <typename T, typename = void> struct element_type {
84+
using type = T;
85+
};
86+
template <typename T, int N> struct element_type<vec<T, N>> {
87+
using type = T;
88+
};
89+
template <typename T, std::size_t N> struct element_type<marray<T, N>> {
90+
using type = T;
91+
};
92+
#if defined(__has_extension)
93+
#if __has_extension(attribute_ext_vector_type)
94+
template <typename T, int N>
95+
struct element_type<T __attribute__((ext_vector_type(N)))> {
96+
using type = T;
97+
};
98+
#endif
99+
#endif
100+
template <typename T> using element_type_t = typename element_type<T>::type;
101+
102+
} // namespace detail
103+
} // namespace _V1
104+
} // namespace sycl

sycl/test/include_deps/sycl_accessor.hpp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// CHECK-NEXT: info/aspects.def
2525
// CHECK-NEXT: info/aspects_deprecated.def
2626
// CHECK-NEXT: detail/type_traits.hpp
27+
// CHECK-NEXT: detail/type_traits/vec_marray_traits.hpp
2728
// CHECK-NEXT: detail/generic_type_lists.hpp
2829
// CHECK-NEXT: detail/type_list.hpp
2930
// CHECK-NEXT: detail/boost/mp11/algorithm.hpp
@@ -112,5 +113,5 @@
112113
// CHECK-NEXT: detail/string_view.hpp
113114
// CHECK-NEXT: detail/util.hpp
114115
// CHECK-NEXT: device_selector.hpp
115-
// CHECK-NEXT: buffer_properties.def
116+
// CHECK-NEXT: properties/buffer_properties.def
116117
// CHECK-EMPTY:

sycl/test/include_deps/sycl_detail_core.hpp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// CHECK-NEXT: info/aspects.def
2626
// CHECK-NEXT: info/aspects_deprecated.def
2727
// CHECK-NEXT: detail/type_traits.hpp
28+
// CHECK-NEXT: detail/type_traits/vec_marray_traits.hpp
2829
// CHECK-NEXT: detail/generic_type_lists.hpp
2930
// CHECK-NEXT: detail/type_list.hpp
3031
// CHECK-NEXT: detail/boost/mp11/algorithm.hpp

0 commit comments

Comments
 (0)