Skip to content

Commit 4a1d030

Browse files
[SYCL][Headers cleanup] Introduce <sycl/detail/is_device_copyable.hpp> (#13211)
This allows us to drop several `<sycl/buffer.hpp>`'s dependencies.
1 parent 9aa4bc0 commit 4a1d030

File tree

7 files changed

+133
-119
lines changed

7 files changed

+133
-119
lines changed

sycl/include/sycl/buffer.hpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,27 @@
88

99
#pragma once
1010

11-
#include <sycl/access/access.hpp> // for placeholder
12-
#include <sycl/backend_types.hpp> // for backend, backe...
13-
#include <sycl/context.hpp> // for context
14-
#include <sycl/detail/array.hpp> // for array
15-
#include <sycl/detail/common.hpp> // for code_location
16-
#include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEP...
17-
#include <sycl/detail/export.hpp> // for __SYCL_EXPORT
18-
#include <sycl/detail/helpers.hpp> // for buffer_impl
19-
#include <sycl/detail/owner_less_base.hpp> // for OwnerLessBase
11+
#include <sycl/access/access.hpp>
12+
#include <sycl/backend_types.hpp>
13+
#include <sycl/context.hpp>
14+
#include <sycl/detail/array.hpp>
15+
#include <sycl/detail/common.hpp>
16+
#include <sycl/detail/defines_elementary.hpp>
17+
#include <sycl/detail/export.hpp>
18+
#include <sycl/detail/helpers.hpp>
19+
#include <sycl/detail/is_device_copyable.hpp>
20+
#include <sycl/detail/owner_less_base.hpp>
2021
#include <sycl/detail/pi.h> // for pi_native_handle and PI_ERROR_INVAL
21-
#include <sycl/detail/property_helper.hpp> // for PropWithDataKind
22-
#include <sycl/detail/stl_type_traits.hpp> // for iterator_value...
23-
#include <sycl/detail/stl_type_traits.hpp> // for iterator_to_const_type_t
24-
#include <sycl/detail/sycl_mem_obj_allocator.hpp> // for SYCLMemObjAllo...
25-
#include <sycl/detail/type_traits.hpp> // for remove_pointer_t
26-
#include <sycl/event.hpp> // for event
27-
#include <sycl/exception.hpp> // for invalid_object...
28-
#include <sycl/ext/oneapi/accessor_property_list.hpp> // for accessor_prope...
29-
#include <sycl/id.hpp> // for id
30-
#include <sycl/property_list.hpp> // for property_list
31-
#include <sycl/range.hpp> // for range, rangeTo...
32-
#include <sycl/types.hpp> // for is_device_copyable
22+
#include <sycl/detail/property_helper.hpp>
23+
#include <sycl/detail/stl_type_traits.hpp>
24+
#include <sycl/detail/sycl_mem_obj_allocator.hpp>
25+
#include <sycl/detail/type_traits.hpp>
26+
#include <sycl/event.hpp>
27+
#include <sycl/exception.hpp>
28+
#include <sycl/ext/oneapi/accessor_property_list.hpp>
29+
#include <sycl/id.hpp>
30+
#include <sycl/property_list.hpp>
31+
#include <sycl/range.hpp>
3332

3433
#include <cstddef> // for size_t, nullptr_t
3534
#include <functional> // for function
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//==------------ is_device_copyable.hpp - ----------------------------------==//
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+
#pragma once
9+
10+
#include <array>
11+
#include <optional>
12+
#include <type_traits>
13+
#include <variant>
14+
15+
/// This macro must be defined to 1 when SYCL implementation allows user
16+
/// applications to explicitly declare certain class types as device copyable
17+
/// by adding specializations of is_device_copyable type trait class.
18+
#define SYCL_DEVICE_COPYABLE 1
19+
20+
namespace sycl {
21+
inline namespace _V1 {
22+
/// is_device_copyable is a user specializable class template to indicate
23+
/// that a type T is device copyable, which means that SYCL implementation
24+
/// may copy objects of the type T between host and device or between two
25+
/// devices.
26+
/// Specializing is_device_copyable such a way that
27+
/// is_device_copyable_v<T> == true on a T that does not satisfy all
28+
/// the requirements of a device copyable type is undefined behavior.
29+
template <typename T> struct is_device_copyable;
30+
31+
namespace detail {
32+
template <typename T, typename = void>
33+
struct is_device_copyable_impl : std::is_trivially_copyable<T> {};
34+
35+
template <typename T>
36+
struct is_device_copyable_impl<
37+
T, std::enable_if_t<!std::is_same_v<T, std::remove_cv_t<T>>>>
38+
// Cannot express this "recursion" (to take user's partial non-cv
39+
// specializations into account) without this helper struct.
40+
: is_device_copyable<std::remove_cv_t<T>> {};
41+
} // namespace detail
42+
43+
template <typename T>
44+
struct is_device_copyable : detail::is_device_copyable_impl<T> {};
45+
46+
// std::array<T, 0> is implicitly device copyable type.
47+
template <typename T>
48+
struct is_device_copyable<std::array<T, 0>> : std::true_type {};
49+
50+
// std::array<T, N> is implicitly device copyable type if T is device copyable.
51+
template <typename T, std::size_t N>
52+
struct is_device_copyable<std::array<T, N>> : is_device_copyable<T> {};
53+
54+
// std::optional<T> is implicitly device copyable type if T is device copyable.
55+
template <typename T>
56+
struct is_device_copyable<std::optional<T>> : is_device_copyable<T> {};
57+
58+
// std::pair<T1, T2> is implicitly device copyable type if T1 and T2 are device
59+
// copyable.
60+
template <typename T1, typename T2>
61+
struct is_device_copyable<std::pair<T1, T2>>
62+
: std::bool_constant<is_device_copyable<T1>::value &&
63+
is_device_copyable<T2>::value> {};
64+
65+
// std::tuple<Ts...> is implicitly device copyable type if each type T of Ts...
66+
// is device copyable.
67+
template <typename... Ts>
68+
struct is_device_copyable<std::tuple<Ts...>>
69+
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};
70+
71+
// std::variant<Ts...> is implicitly device copyable type if each type T of
72+
// Ts... is device copyable.
73+
template <typename... Ts>
74+
struct is_device_copyable<std::variant<Ts...>>
75+
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};
76+
77+
// array is device copyable if element type is device copyable.
78+
template <typename T, std::size_t N>
79+
struct is_device_copyable<T[N]> : is_device_copyable<T> {};
80+
81+
template <typename T>
82+
inline constexpr bool is_device_copyable_v = is_device_copyable<T>::value;
83+
} // namespace _V1
84+
} // namespace sycl

sycl/include/sycl/marray.hpp

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

99
#pragma once
1010

11-
#include <sycl/aliases.hpp> // for half
12-
#include <sycl/detail/common.hpp> // for ArrayCreator
13-
#include <sycl/half_type.hpp> // for half
11+
#include <sycl/aliases.hpp>
12+
#include <sycl/detail/common.hpp>
13+
#include <sycl/detail/is_device_copyable.hpp>
14+
#include <sycl/half_type.hpp>
1415

15-
#include <array> // for array
16-
#include <cstddef> // for size_t
17-
#include <cstdint> // for int64_t, int8_t, uint64_t, int16_t
18-
#include <type_traits> // for enable_if_t, remove_const, is_conv...
19-
#include <utility> // for index_sequence, make_index_sequence
16+
#include <array>
17+
#include <cstddef>
18+
#include <cstdint>
19+
#include <type_traits>
20+
#include <utility>
2021

2122
namespace sycl {
2223
inline namespace _V1 {
@@ -363,6 +364,10 @@ template <typename Type, std::size_t NumElements> class marray {
363364
}
364365
};
365366

367+
// marray is device copyable if element type is device copyable.
368+
template <typename T, std::size_t N>
369+
struct is_device_copyable<sycl::marray<T, N>> : is_device_copyable<T> {};
370+
366371
#define __SYCL_MAKE_MARRAY_ALIAS(ALIAS, TYPE, N) \
367372
using ALIAS##N = sycl::marray<TYPE, N>;
368373

sycl/include/sycl/types.hpp

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
3737
#include <sycl/detail/generic_type_lists.hpp> // for vector_basic_list
3838
#include <sycl/detail/generic_type_traits.hpp> // for is_sigeninteger, is_s...
39+
#include <sycl/detail/is_device_copyable.hpp>
3940
#include <sycl/detail/memcpy.hpp> // for memcpy
4041
#include <sycl/detail/type_list.hpp> // for is_contained
4142
#include <sycl/detail/type_traits.hpp> // for is_floating_point
@@ -2195,77 +2196,6 @@ __SYCL_DEFINE_BF16_VECSTORAGE(16)
21952196
#undef __SYCL_DEFINE_BF16_VECSTORAGE
21962197
} // namespace detail
21972198

2198-
/// This macro must be defined to 1 when SYCL implementation allows user
2199-
/// applications to explicitly declare certain class types as device copyable
2200-
/// by adding specializations of is_device_copyable type trait class.
2201-
#define SYCL_DEVICE_COPYABLE 1
2202-
2203-
/// is_device_copyable is a user specializable class template to indicate
2204-
/// that a type T is device copyable, which means that SYCL implementation
2205-
/// may copy objects of the type T between host and device or between two
2206-
/// devices.
2207-
/// Specializing is_device_copyable such a way that
2208-
/// is_device_copyable_v<T> == true on a T that does not satisfy all
2209-
/// the requirements of a device copyable type is undefined behavior.
2210-
template <typename T> struct is_device_copyable;
2211-
2212-
namespace detail {
2213-
template <typename T, typename = void>
2214-
struct is_device_copyable_impl : std::is_trivially_copyable<T> {};
2215-
2216-
template <typename T>
2217-
struct is_device_copyable_impl<
2218-
T, std::enable_if_t<!std::is_same_v<T, std::remove_cv_t<T>>>>
2219-
// Cannot express this "recursion" (to take user's partial non-cv
2220-
// specializations into account) without this helper struct.
2221-
: is_device_copyable<std::remove_cv_t<T>> {};
2222-
} // namespace detail
2223-
2224-
template <typename T>
2225-
struct is_device_copyable : detail::is_device_copyable_impl<T> {};
2226-
2227-
// std::array<T, 0> is implicitly device copyable type.
2228-
template <typename T>
2229-
struct is_device_copyable<std::array<T, 0>> : std::true_type {};
2230-
2231-
// std::array<T, N> is implicitly device copyable type if T is device copyable.
2232-
template <typename T, std::size_t N>
2233-
struct is_device_copyable<std::array<T, N>> : is_device_copyable<T> {};
2234-
2235-
// std::optional<T> is implicitly device copyable type if T is device copyable.
2236-
template <typename T>
2237-
struct is_device_copyable<std::optional<T>> : is_device_copyable<T> {};
2238-
2239-
// std::pair<T1, T2> is implicitly device copyable type if T1 and T2 are device
2240-
// copyable.
2241-
template <typename T1, typename T2>
2242-
struct is_device_copyable<std::pair<T1, T2>>
2243-
: std::bool_constant<is_device_copyable<T1>::value &&
2244-
is_device_copyable<T2>::value> {};
2245-
2246-
// std::tuple<Ts...> is implicitly device copyable type if each type T of Ts...
2247-
// is device copyable.
2248-
template <typename... Ts>
2249-
struct is_device_copyable<std::tuple<Ts...>>
2250-
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};
2251-
2252-
// std::variant<Ts...> is implicitly device copyable type if each type T of
2253-
// Ts... is device copyable.
2254-
template <typename... Ts>
2255-
struct is_device_copyable<std::variant<Ts...>>
2256-
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};
2257-
2258-
// marray is device copyable if element type is device copyable.
2259-
template <typename T, std::size_t N>
2260-
struct is_device_copyable<sycl::marray<T, N>> : is_device_copyable<T> {};
2261-
2262-
// array is device copyable if element type is device copyable.
2263-
template <typename T, std::size_t N>
2264-
struct is_device_copyable<T[N]> : is_device_copyable<T> {};
2265-
2266-
template <typename T>
2267-
inline constexpr bool is_device_copyable_v = is_device_copyable<T>::value;
2268-
22692199
namespace detail {
22702200
template <typename T, typename = void>
22712201
struct IsDeprecatedDeviceCopyable : std::false_type {};

sycl/test/include_deps/sycl_accessor.hpp.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,24 @@
106106
// CHECK-NEXT: detail/property_helper.hpp
107107
// CHECK-NEXT: detail/property_list_base.hpp
108108
// CHECK-NEXT: properties/property_traits.hpp
109+
// CHECK-NEXT: detail/is_device_copyable.hpp
109110
// CHECK-NEXT: detail/stl_type_traits.hpp
110111
// CHECK-NEXT: detail/sycl_mem_obj_allocator.hpp
111112
// CHECK-NEXT: detail/aligned_allocator.hpp
112113
// CHECK-NEXT: event.hpp
113114
// CHECK-NEXT: ext/oneapi/accessor_property_list.hpp
114-
// CHECK-NEXT: types.hpp
115-
// CHECK-NEXT: detail/generic_type_traits.hpp
116-
// CHECK-NEXT: detail/memcpy.hpp
117-
// CHECK-NEXT: detail/vector_convert.hpp
118-
// CHECK-NEXT: marray.hpp
119-
// CHECK-NEXT: swizzles.def
120115
// CHECK-NEXT: detail/accessor_iterator.hpp
116+
// CHECK-NEXT: detail/generic_type_traits.hpp
121117
// CHECK-NEXT: detail/handler_proxy.hpp
122118
// CHECK-NEXT: detail/image_accessor_util.hpp
123119
// CHECK-NEXT: image.hpp
124120
// CHECK-NEXT: detail/backend_traits.hpp
125121
// CHECK-NEXT: sampler.hpp
122+
// CHECK-NEXT: types.hpp
123+
// CHECK-NEXT: detail/memcpy.hpp
124+
// CHECK-NEXT: detail/vector_convert.hpp
125+
// CHECK-NEXT: marray.hpp
126+
// CHECK-NEXT: swizzles.def
126127
// CHECK-NEXT: pointers.hpp
127128
// CHECK-NEXT: properties/accessor_properties.hpp
128129
// CHECK-NEXT: properties/buffer_properties.hpp

sycl/test/include_deps/sycl_buffer.hpp.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,10 @@
103103
// CHECK-NEXT: detail/property_helper.hpp
104104
// CHECK-NEXT: detail/property_list_base.hpp
105105
// CHECK-NEXT: properties/property_traits.hpp
106+
// CHECK-NEXT: detail/is_device_copyable.hpp
106107
// CHECK-NEXT: detail/stl_type_traits.hpp
107108
// CHECK-NEXT: detail/sycl_mem_obj_allocator.hpp
108109
// CHECK-NEXT: detail/aligned_allocator.hpp
109110
// CHECK-NEXT: event.hpp
110111
// CHECK-NEXT: ext/oneapi/accessor_property_list.hpp
111-
// CHECK-NEXT: types.hpp
112-
// CHECK-NEXT: detail/generic_type_traits.hpp
113-
// CHECK-NEXT: multi_ptr.hpp
114-
// CHECK-NEXT: detail/memcpy.hpp
115-
// CHECK-NEXT: detail/vector_convert.hpp
116-
// CHECK-NEXT: marray.hpp
117-
// CHECK-NEXT: swizzles.def
118112
// CHECK-EMPTY:

sycl/test/include_deps/sycl_detail_core.hpp.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,24 @@
107107
// CHECK-NEXT: detail/property_helper.hpp
108108
// CHECK-NEXT: detail/property_list_base.hpp
109109
// CHECK-NEXT: properties/property_traits.hpp
110+
// CHECK-NEXT: detail/is_device_copyable.hpp
110111
// CHECK-NEXT: detail/stl_type_traits.hpp
111112
// CHECK-NEXT: detail/sycl_mem_obj_allocator.hpp
112113
// CHECK-NEXT: detail/aligned_allocator.hpp
113114
// CHECK-NEXT: event.hpp
114115
// CHECK-NEXT: ext/oneapi/accessor_property_list.hpp
115-
// CHECK-NEXT: types.hpp
116-
// CHECK-NEXT: detail/generic_type_traits.hpp
117-
// CHECK-NEXT: detail/memcpy.hpp
118-
// CHECK-NEXT: detail/vector_convert.hpp
119-
// CHECK-NEXT: marray.hpp
120-
// CHECK-NEXT: swizzles.def
121116
// CHECK-NEXT: detail/accessor_iterator.hpp
117+
// CHECK-NEXT: detail/generic_type_traits.hpp
122118
// CHECK-NEXT: detail/handler_proxy.hpp
123119
// CHECK-NEXT: detail/image_accessor_util.hpp
124120
// CHECK-NEXT: image.hpp
125121
// CHECK-NEXT: detail/backend_traits.hpp
126122
// CHECK-NEXT: sampler.hpp
123+
// CHECK-NEXT: types.hpp
124+
// CHECK-NEXT: detail/memcpy.hpp
125+
// CHECK-NEXT: detail/vector_convert.hpp
126+
// CHECK-NEXT: marray.hpp
127+
// CHECK-NEXT: swizzles.def
127128
// CHECK-NEXT: pointers.hpp
128129
// CHECK-NEXT: properties/accessor_properties.hpp
129130
// CHECK-NEXT: properties/buffer_properties.hpp

0 commit comments

Comments
 (0)