DPC++ Runtime
Runtime libraries for oneAPI DPC++
type_traits.hpp
Go to the documentation of this file.
1 //==----------------- type_traits.hpp - SYCL type traits -------------------==//
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 <sycl/access/access.hpp>
15 
16 #include <array>
17 #include <tuple>
18 #include <type_traits>
19 
20 namespace sycl {
22 template <int Dimensions> class group;
23 namespace ext {
24 namespace oneapi {
25 struct sub_group;
26 
27 namespace experimental {
28 template <typename Group, std::size_t Extent> class group_with_scratchpad;
29 
30 namespace detail {
31 template <typename T> struct is_group_helper : std::false_type {};
32 
33 template <typename Group, std::size_t Extent>
34 struct is_group_helper<group_with_scratchpad<Group, Extent>> : std::true_type {
35 };
36 } // namespace detail
37 } // namespace experimental
38 } // namespace oneapi
39 } // namespace ext
40 
41 namespace detail {
42 
43 template <typename T> struct is_group : std::false_type {};
44 
45 template <int Dimensions>
46 struct is_group<group<Dimensions>> : std::true_type {};
47 
48 template <typename T> struct is_sub_group : std::false_type {};
49 
50 template <> struct is_sub_group<ext::oneapi::sub_group> : std::true_type {};
51 
52 template <typename T>
54  : std::integral_constant<bool,
55  is_group<T>::value || is_sub_group<T>::value> {};
56 
57 namespace half_impl {
58 class half;
59 }
60 } // namespace detail
62 
63 // Forward declaration
64 template <typename ElementType, access::address_space Space,
65  access::decorated DecorateAddress>
66 class multi_ptr;
67 
68 template <class T>
71 
72 namespace ext::oneapi::experimental {
73 template <class T>
76 } // namespace ext::oneapi::experimental
77 
78 namespace detail {
79 // Type for Intel device UUID extension.
80 // For details about this extension, see
81 // sycl/doc/extensions/supported/sycl_ext_intel_device_info.md
82 using uuid_type = std::array<unsigned char, 16>;
83 
84 template <typename T, typename R> struct copy_cv_qualifiers;
85 
86 template <typename T, typename R>
88 
89 template <int V> using int_constant = std::integral_constant<int, V>;
90 
91 // vector_size
92 // scalars are interpreted as a vector of 1 length.
93 template <typename T> struct vector_size_impl : int_constant<1> {};
94 template <typename T, int N>
95 struct vector_size_impl<vec<T, N>> : int_constant<N> {};
96 template <typename T>
97 struct vector_size : vector_size_impl<remove_cv_t<remove_reference_t<T>>> {};
98 
99 // 4.10.2.6 Memory layout and alignment
100 template <typename T, int N>
102  : conditional_t<N == 3, int_constant<sizeof(T) * 4>,
103  int_constant<sizeof(T) * N>> {};
104 
105 template <typename T, int N>
107  : vector_alignment_impl<remove_cv_t<remove_reference_t<T>>, N> {};
108 
109 // vector_element
110 template <typename T> struct vector_element_impl;
111 template <typename T>
113 template <typename T> struct vector_element_impl {
114  using type = T;
115 };
116 template <typename T, int N> struct vector_element_impl<vec<T, N>> {
117  using type = T;
118 };
119 template <typename T> struct vector_element {
121 };
122 template <class T> using vector_element_t = typename vector_element<T>::type;
123 
124 // change_base_type_t
125 template <typename T, typename B> struct change_base_type {
126  using type = B;
127 };
128 
129 template <typename T, int N, typename B> struct change_base_type<vec<T, N>, B> {
130  using type = vec<B, N>;
131 };
132 
133 template <typename T, typename B>
135 
136 // Applies the same the cv-qualifiers from T type to R type
137 template <typename T, typename R> struct copy_cv_qualifiers_impl {
138  using type = R;
139 };
140 
141 template <typename T, typename R> struct copy_cv_qualifiers_impl<const T, R> {
142  using type = const R;
143 };
144 
145 template <typename T, typename R>
146 struct copy_cv_qualifiers_impl<volatile T, R> {
147  using type = volatile R;
148 };
149 
150 template <typename T, typename R>
151 struct copy_cv_qualifiers_impl<const volatile T, R> {
152  using type = const volatile R;
153 };
154 
155 template <typename T, typename R> struct copy_cv_qualifiers {
157 };
158 
159 // make_signed with support SYCL vec class
160 template <typename T, typename Enable = void> struct make_signed_impl;
161 
162 template <typename T>
164 
165 template <typename T>
167  T, std::enable_if_t<is_contained<T, gtl::scalar_integer_list>::value, T>> {
168  using type = typename std::make_signed<T>::type;
169 };
170 
171 template <typename T>
173  T, std::enable_if_t<is_contained<T, gtl::vector_integer_list>::value, T>> {
176 };
177 
178 // TODO Delete this specialization after solving the problems in the test
179 // infrastructure.
180 template <typename T>
182  T, std::enable_if_t<!is_contained<T, gtl::integer_list>::value, T>> {
183  using type = T;
184 };
185 
186 template <typename T> struct make_signed {
189 };
190 
191 template <typename T> using make_signed_t = typename make_signed<T>::type;
192 
193 // make_unsigned with support SYCL vec class
194 template <typename T, typename Enable = void> struct make_unsigned_impl;
195 
196 template <typename T>
198 
199 template <typename T>
201  T, std::enable_if_t<is_contained<T, gtl::scalar_integer_list>::value, T>> {
202  using type = typename std::make_unsigned<T>::type;
203 };
204 
205 template <typename T>
207  T, std::enable_if_t<is_contained<T, gtl::vector_integer_list>::value, T>> {
210 };
211 
212 // TODO Delete this specialization after solving the problems in the test
213 // infrastructure.
214 template <typename T>
216  T, std::enable_if_t<!is_contained<T, gtl::integer_list>::value, T>> {
217  using type = T;
218 };
219 
220 template <typename T> struct make_unsigned {
223 };
224 
225 template <typename T> using make_unsigned_t = typename make_unsigned<T>::type;
226 
227 // Checks that sizeof base type of T equal N and T satisfies S<T>::value
228 template <typename T, int N, template <typename> class S>
231 
232 template <typename> struct is_vec : std::false_type {};
233 template <typename T, std::size_t N>
234 struct is_vec<sycl::vec<T, N>> : std::true_type {};
235 
236 // is_integral
237 template <typename T>
238 struct is_integral : std::is_integral<vector_element_t<T>> {};
239 
240 // is_floating_point
241 template <typename T>
242 struct is_floating_point_impl : std::is_floating_point<T> {};
243 
244 template <> struct is_floating_point_impl<half> : std::true_type {};
245 
246 template <typename T>
248  : is_floating_point_impl<remove_cv_t<vector_element_t<T>>> {};
249 
250 // is_arithmetic
251 template <typename T>
253  : bool_constant<is_integral<T>::value || is_floating_point<T>::value> {};
254 
255 template <typename T>
257  : bool_constant<!is_vec<T>::value && is_arithmetic<T>::value> {};
258 
259 template <typename T>
261  : bool_constant<is_vec<T>::value && is_arithmetic<T>::value> {};
262 
263 // is_bool
264 template <typename T>
266  : bool_constant<std::is_same<remove_cv_t<T>, bool>::value> {};
267 
268 template <typename T>
270  : bool_constant<is_vec<T>::value &&
271  is_scalar_bool<vector_element_t<T>>::value> {};
272 
273 template <typename T>
274 struct is_bool : bool_constant<is_scalar_bool<vector_element_t<T>>::value> {};
275 
276 // is_pointer
277 template <typename T> struct is_pointer_impl : std::false_type {};
278 
279 template <typename T> struct is_pointer_impl<T *> : std::true_type {};
280 
281 template <typename T, access::address_space Space,
282  access::decorated DecorateAddress>
283 struct is_pointer_impl<multi_ptr<T, Space, DecorateAddress>> : std::true_type {
284 };
285 
286 template <typename T> struct is_pointer : is_pointer_impl<remove_cv_t<T>> {};
287 
288 // remove_pointer_t
289 template <typename T> struct remove_pointer_impl {
290  using type = T;
291 };
292 
293 template <typename T> struct remove_pointer_impl<T *> {
294  using type = T;
295 };
296 
297 template <typename T, access::address_space Space,
298  access::decorated DecorateAddress>
299 struct remove_pointer_impl<multi_ptr<T, Space, DecorateAddress>> {
300  using type = T;
301 };
302 
303 template <typename T>
304 struct remove_pointer : remove_pointer_impl<remove_cv_t<T>> {};
305 
306 template <typename T> using remove_pointer_t = typename remove_pointer<T>::type;
307 
308 // is_address_space_compliant
309 template <typename T, typename SpaceList>
310 struct is_address_space_compliant_impl : std::false_type {};
311 
312 template <typename T, typename SpaceList>
313 struct is_address_space_compliant_impl<T *, SpaceList> : std::true_type {};
314 
315 template <typename T, typename SpaceList, access::address_space Space,
316  access::decorated DecorateAddress>
317 struct is_address_space_compliant_impl<multi_ptr<T, Space, DecorateAddress>,
318  SpaceList>
319  : bool_constant<is_one_of_spaces<Space, SpaceList>::value> {};
320 
321 template <typename T, typename SpaceList>
323  : is_address_space_compliant_impl<remove_cv_t<T>, SpaceList> {};
324 
325 // make_type_t
326 template <typename T, typename TL> struct make_type_impl {
328 };
329 
330 template <typename T, int N, typename TL> struct make_type_impl<vec<T, N>, TL> {
333 };
334 
335 template <typename T, typename TL>
337 
338 // make_larger_t
339 template <typename T, typename Enable = void> struct make_larger_impl;
340 template <typename T>
342  T, std::enable_if_t<is_contained<T, gtl::scalar_floating_list>::value, T>> {
344 };
345 
346 template <typename T>
348  T, std::enable_if_t<is_contained<T, gtl::scalar_signed_integer_list>::value,
349  T>> {
351 };
352 
353 template <typename T>
355  T, std::enable_if_t<
356  is_contained<T, gtl::scalar_unsigned_integer_list>::value, T>> {
358 };
359 
360 template <typename T, int N> struct make_larger_impl<vec<T, N>, vec<T, N>> {
364  static constexpr bool found = !std::is_same<upper_type, void>::value;
366 };
367 
368 template <typename T> struct make_larger {
370 };
371 
372 template <typename T> using make_larger_t = typename make_larger<T>::type;
373 
374 #if defined(RESTRICT_WRITE_ACCESS_TO_CONSTANT_PTR)
375 template <access::address_space AS, class DataT>
376 using const_if_const_AS =
377  typename std::conditional<AS == access::address_space::constant_space,
378  const DataT, DataT>::type;
379 #else
380 template <access::address_space AS, class DataT>
381 using const_if_const_AS = DataT;
382 #endif
383 
384 template <typename T> struct function_traits {};
385 
386 template <typename Ret, typename... Args> struct function_traits<Ret(Args...)> {
387  using ret_type = Ret;
388  using args_type = std::tuple<Args...>;
389 };
390 
391 } // namespace detail
392 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
393 } // namespace sycl
Provides constructors for address space qualified and non address space qualified pointers to allow i...
Definition: multi_ptr.hpp:78
Provides a cross-patform vector class template that works efficiently on SYCL devices as well as in h...
Definition: types.hpp:557
#define __SYCL_INLINE_CONSTEXPR
#define __SYCL_INLINE_VER_NAMESPACE(X)
typename change_base_type< T, B >::type change_base_type_t
typename remove_pointer< T >::type remove_pointer_t
typename make_larger< T >::type make_larger_t
typename make_unsigned< T >::type make_unsigned_t
typename vector_element_impl< T >::type vector_element_impl_t
typename std::conditional< B, T, F >::type conditional_t
typename make_unsigned_impl< T, T >::type make_unsigned_impl_t
std::integral_constant< bool, V > bool_constant
typename make_signed_impl< T, T >::type make_signed_impl_t
bool_constant< S< T >::value &&(sizeof(vector_element_t< T >)==N)> is_gen_based_on_type_sizeof
typename vector_element< T >::type vector_element_t
typename copy_cv_qualifiers< T, R >::type copy_cv_qualifiers_t
Definition: type_traits.hpp:87
typename make_signed< T >::type make_signed_t
std::array< unsigned char, 16 > uuid_type
Definition: type_traits.hpp:82
find_type_t< TypeList, is_type_size_equal, T > find_same_size_type_t
Definition: type_list.hpp:125
find_type_t< TypeList, is_type_size_double_of, T > find_twice_as_large_type_t
Definition: type_list.hpp:139
std::integral_constant< int, V > int_constant
Definition: type_traits.hpp:89
typename make_type_impl< T, TL >::type make_type_t
__SYCL_INLINE_CONSTEXPR bool is_group_helper_v
Definition: type_traits.hpp:74
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS Dimensions
Definition: accessor.hpp:2782
__SYCL_INLINE_CONSTEXPR bool is_group_v
Definition: type_traits.hpp:69
sycl::detail::half_impl::half half
Definition: aliases.hpp:77
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
typename copy_cv_qualifiers_impl< T, remove_cv_t< R > >::type type
typename make_larger_impl< base_type, base_type >::type upper_type
typename make_larger_impl< T, T >::type type
make_signed_impl_t< remove_cv_t< T > > new_type_wo_cv_qualifiers
copy_cv_qualifiers_t< T, new_type_wo_cv_qualifiers > type
typename make_type_impl< T, TL >::type scalar_type
find_same_size_type_t< TL, T > type
make_unsigned_impl_t< remove_cv_t< T > > new_type_wo_cv_qualifiers
copy_cv_qualifiers_t< T, new_type_wo_cv_qualifiers > type
copy_cv_qualifiers_t< T, vector_element_impl_t< remove_cv_t< T > >> type