DPC++ Runtime
Runtime libraries for oneAPI DPC++
properties.hpp
Go to the documentation of this file.
1 //==---------- properties.hpp --- SYCL extended property list --------------==//
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 
15 #include <sycl/types.hpp>
16 
17 #include <tuple>
18 #include <type_traits>
19 
20 namespace sycl {
22 namespace ext::oneapi::experimental {
23 
24 namespace detail {
25 
26 // Checks if a tuple of properties contains a property.
27 template <typename PropT, typename PropertiesT>
28 struct ContainsProperty : std::false_type {};
29 template <typename PropT, typename T, typename... Ts>
30 struct ContainsProperty<PropT, std::tuple<T, Ts...>>
31  : ContainsProperty<PropT, std::tuple<Ts...>> {};
32 template <typename PropT, typename... Rest>
33 struct ContainsProperty<PropT, std::tuple<PropT, Rest...>> : std::true_type {};
34 template <typename PropT, typename... PropValuesTs, typename... Rest>
36  PropT, std::tuple<property_value<PropT, PropValuesTs...>, Rest...>>
37  : std::true_type {};
38 
39 // Finds the full property_value type of a property in a tuple of properties.
40 // type is void if the type was not found in the tuple of properties.
41 template <typename CTPropertyT, typename PropertiesT = void>
43  using type = void;
44 };
45 template <typename CTPropertyT, typename OtherProperty, typename... Rest>
47  std::tuple<OtherProperty, Rest...>> {
48  using type =
49  typename FindCompileTimePropertyValueType<CTPropertyT,
50  std::tuple<Rest...>>::type;
51 };
52 template <typename CTPropertyT, typename... CTPropertyValueTs, typename... Rest>
54  CTPropertyT,
55  std::tuple<property_value<CTPropertyT, CTPropertyValueTs...>, Rest...>> {
56  using type = property_value<CTPropertyT, CTPropertyValueTs...>;
57 };
58 
59 template <typename CTPropertyT, bool HasProperty, typename PropertiesT = void>
60 static constexpr std::enable_if_t<
61  HasProperty,
64  return {};
65 }
66 
67 template <typename CTPropertyT, bool HasProperty, typename PropertiesT = void>
68 static constexpr std::enable_if_t<!HasProperty, void> get_property() {
69  return;
70 }
71 
72 // Filters for all runtime properties with data in a tuple of properties.
73 // NOTE: We only need storage for runtime properties with data.
74 template <typename T> struct RuntimePropertyStorage {};
75 template <typename... Ts> struct RuntimePropertyStorage<std::tuple<Ts...>> {
76  using type = std::tuple<>;
77 };
78 template <typename T, typename... Ts>
79 struct RuntimePropertyStorage<std::tuple<T, Ts...>>
81  IsRuntimeProperty<T>::value,
82  PrependTuple<
83  T, typename RuntimePropertyStorage<std::tuple<Ts...>>::type>,
84  RuntimePropertyStorage<std::tuple<Ts...>>> {};
85 
86 // Helper class to extract a subset of elements from a tuple.
87 // NOTES: This assumes no duplicate properties and that all properties in the
88 // struct template argument appear in the tuple passed to Extract.
89 template <typename PropertiesT> struct ExtractProperties {};
90 template <typename... PropertiesTs>
91 struct ExtractProperties<std::tuple<PropertiesTs...>> {
92  template <typename... PropertyValueTs>
93  using ExtractedPropertiesT = std::tuple<>;
94 
95  template <typename... PropertyValueTs>
96  static constexpr ExtractedPropertiesT<PropertyValueTs...>
97  Extract(std::tuple<PropertyValueTs...>) {
98  return {};
99  }
100 };
101 template <typename PropertyT, typename... PropertiesTs>
102 struct ExtractProperties<std::tuple<PropertyT, PropertiesTs...>> {
103  template <typename... PropertyValueTs>
105  typename ExtractProperties<std::tuple<PropertiesTs...>>::
106  template ExtractedPropertiesT<PropertyValueTs...>;
107  template <typename... PropertyValueTs>
108  using ExtractedPropertiesT =
109  typename PrependTuple<PropertyT,
110  NextExtractedPropertiesT<PropertyValueTs...>>::type;
111 
112  template <typename... PropertyValueTs>
113  static constexpr ExtractedPropertiesT<PropertyValueTs...>
114  Extract(std::tuple<PropertyValueTs...> PropertyValues) {
115  PropertyT ThisExtractedProperty = std::get<PropertyT>(PropertyValues);
116  NextExtractedPropertiesT<PropertyValueTs...> NextExtractedProperties =
117  ExtractProperties<std::tuple<PropertiesTs...>>::template Extract<
118  PropertyValueTs...>(PropertyValues);
119  return std::tuple_cat(std::tuple<PropertyT>{ThisExtractedProperty},
120  NextExtractedProperties);
121  }
122 };
123 
124 } // namespace detail
125 
126 template <typename PropertiesT> class properties {
128  "Properties must be in a tuple.");
130  "Unrecognized property in property list.");
132  "Properties in property list are not sorted.");
133  static_assert(detail::SortedAllUnique<PropertiesT>::value,
134  "Duplicate properties in property list.");
135 
136 public:
137  template <typename... PropertyValueTs>
138  constexpr properties(PropertyValueTs... props)
139  : Storage(detail::ExtractProperties<StorageT>::Extract(
140  std::tuple<PropertyValueTs...>{props...})) {}
141 
142  template <typename PropertyT>
143  static constexpr std::enable_if_t<detail::IsProperty<PropertyT>::value, bool>
146  }
147 
148  template <typename PropertyT>
149  typename std::enable_if_t<detail::IsRuntimeProperty<PropertyT>::value &&
150  has_property<PropertyT>(),
151  PropertyT>
152  get_property() const {
153  return std::get<PropertyT>(Storage);
154  }
155 
156  template <typename PropertyT>
157  typename std::enable_if_t<detail::IsRuntimeProperty<PropertyT>::value &&
158  !has_property<PropertyT>(),
159  void>
160  get_property() const {
161  static_assert(has_property<PropertyT>(),
162  "Property list does not contain the requested property.");
163  return;
164  }
165 
166  template <typename PropertyT>
167  static constexpr auto get_property(
169  * = 0) {
170  static_assert(has_property<PropertyT>(),
171  "Property list does not contain the requested property.");
172  return detail::get_property<PropertyT, has_property<PropertyT>(),
173  PropertiesT>();
174  }
175 
176 private:
178 
179  StorageT Storage;
180 };
181 
182 #ifdef __cpp_deduction_guides
183 // Deduction guides
184 template <typename... PropertyValueTs>
185 properties(PropertyValueTs... props)
186  -> properties<typename detail::Sorted<PropertyValueTs...>::type>;
187 #endif
188 
189 // Property list traits
190 template <typename propertiesT> struct is_property_list : std::false_type {};
191 template <typename... PropertyValueTs>
192 struct is_property_list<properties<std::tuple<PropertyValueTs...>>>
193  : std::is_same<
194  properties<std::tuple<PropertyValueTs...>>,
195  properties<typename detail::Sorted<PropertyValueTs...>::type>> {};
196 
197 #if __cplusplus > 201402L
198 template <typename propertiesT>
199 inline constexpr bool is_property_list_v = is_property_list<propertiesT>::value;
200 #endif
201 
202 namespace detail {
203 // Helper for default properties when deduction guides are not enabled.
205 
206 // Helper for reconstructing a properties type. This assumes that
207 // PropertyValueTs is sorted and contains only valid properties.
208 template <typename... PropertyValueTs>
209 using properties_t = properties<std::tuple<PropertyValueTs...>>;
210 
211 // Helper for merging two property lists;
212 template <typename LHSPropertiesT, typename RHSPropertiesT>
214 template <typename... LHSPropertiesTs, typename... RHSPropertiesTs>
215 struct merged_properties<properties_t<LHSPropertiesTs...>,
216  properties_t<RHSPropertiesTs...>> {
217  using type = properties<typename MergeProperties<
218  std::tuple<LHSPropertiesTs...>, std::tuple<RHSPropertiesTs...>>::type>;
219 };
220 template <typename LHSPropertiesT, typename RHSPropertiesT>
221 using merged_properties_t =
223 
224 } // namespace detail
225 } // namespace ext::oneapi::experimental
226 
227 // If property_list is not trivially copyable, allow properties to propagate
228 // is_device_copyable
229 template <typename PropertiesT>
231  ext::oneapi::experimental::properties<PropertiesT>,
232  std::enable_if_t<!std::is_trivially_copyable<
233  ext::oneapi::experimental::properties<PropertiesT>>::value>>
234  : is_device_copyable<PropertiesT> {};
235 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
236 } // namespace sycl
sycl::_V1::ext::oneapi::experimental::detail::FindCompileTimePropertyValueType< CTPropertyT, std::tuple< OtherProperty, Rest... > >::type
typename FindCompileTimePropertyValueType< CTPropertyT, std::tuple< Rest... > >::type type
Definition: properties.hpp:50
sycl::_V1::ext::oneapi::experimental::detail::ExtractProperties< std::tuple< PropertyT, PropertiesTs... > >::ExtractedPropertiesT
typename PrependTuple< PropertyT, NextExtractedPropertiesT< PropertyValueTs... > >::type ExtractedPropertiesT
Definition: properties.hpp:110
sycl::_V1::ext::oneapi::experimental::detail::IsSorted
Definition: property_utils.hpp:127
sycl::_V1::ext::oneapi::experimental::detail::RuntimePropertyStorage
Definition: properties.hpp:74
conditional_t
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
types.hpp
sycl::_V1::ext::oneapi::experimental::detail::IsCompileTimeProperty
Definition: property.hpp:209
sycl::_V1::ext::oneapi::experimental::properties
Definition: properties.hpp:126
sycl::_V1::ext::oneapi::experimental::detail::merged_properties
Definition: properties.hpp:213
sycl::_V1::ext::oneapi::experimental::properties::get_property
std::enable_if_t< detail::IsRuntimeProperty< PropertyT >::value &&!has_property< PropertyT >), void > get_property() const
Definition: properties.hpp:160
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
sycl::_V1::is_device_copyable
is_device_copyable is a user specializable class template to indicate that a type T is device copyabl...
Definition: types.hpp:2225
sycl::_V1::ext::oneapi::experimental::properties::get_property
static constexpr auto get_property(typename std::enable_if_t< detail::IsCompileTimeProperty< PropertyT >::value > *=0)
Definition: properties.hpp:167
sycl::_V1::ext::oneapi::experimental::detail::AllPropertyValues
Definition: property_utils.hpp:74
sycl::_V1::ext::oneapi::experimental::detail::ExtractProperties< std::tuple< PropertyT, PropertiesTs... > >::Extract
static constexpr ExtractedPropertiesT< PropertyValueTs... > Extract(std::tuple< PropertyValueTs... > PropertyValues)
Definition: properties.hpp:114
property_value.hpp
sycl::_V1::ext::oneapi::experimental::detail::IsTuple
Definition: property_utils.hpp:31
sycl::_V1::ext::oneapi::experimental::detail::ExtractProperties< std::tuple< PropertyT, PropertiesTs... > >::NextExtractedPropertiesT
typename ExtractProperties< std::tuple< PropertiesTs... > >::template ExtractedPropertiesT< PropertyValueTs... > NextExtractedPropertiesT
Definition: properties.hpp:106
sycl::_V1::ext::oneapi::experimental::detail::merged_properties_t
typename merged_properties< LHSPropertiesT, RHSPropertiesT >::type merged_properties_t
Definition: properties.hpp:222
sycl::_V1::ext::oneapi::experimental::is_property_list
Definition: properties.hpp:190
sycl::_V1::ext::oneapi::experimental::properties::properties
constexpr properties(PropertyValueTs... props)
Definition: properties.hpp:138
property_helper.hpp
sycl::_V1::ext::oneapi::experimental::properties::get_property
std::enable_if_t< detail::IsRuntimeProperty< PropertyT >::value &&has_property< PropertyT >), PropertyT > get_property() const
Definition: properties.hpp:152
sycl::_V1::ext::oneapi::experimental::detail::get_property
static constexpr std::enable_if_t<!HasProperty, void > get_property()
Definition: properties.hpp:68
std
Definition: accessor.hpp:3230
property_utils.hpp
sycl::_V1::ext::oneapi::experimental::detail::FindCompileTimePropertyValueType
Definition: properties.hpp:42
sycl::_V1::ext::oneapi::experimental::detail::RuntimePropertyStorage< std::tuple< Ts... > >::type
std::tuple<> type
Definition: properties.hpp:76
sycl::_V1::detail::half_impl::StorageT
detail::host_half_impl::half StorageT
Definition: half_type.hpp:246
sycl::_V1::ext::oneapi::experimental::detail::PrependTuple
Definition: property_utils.hpp:39
sycl::_V1::ext::oneapi::experimental::detail::ContainsProperty
Definition: properties.hpp:28
sycl::_V1::ext::oneapi::experimental::detail::ExtractProperties
Definition: properties.hpp:89
property.hpp
sycl::_V1::ext::oneapi::experimental::property_value
Definition: property_utils.hpp:22
sycl::_V1::ext::oneapi::experimental::properties::has_property
static constexpr std::enable_if_t< detail::IsProperty< PropertyT >::value, bool > has_property()
Definition: properties.hpp:144
sycl::_V1::ext::oneapi::experimental::detail::ExtractProperties< std::tuple< PropertiesTs... > >::ExtractedPropertiesT
std::tuple<> ExtractedPropertiesT
Definition: properties.hpp:93
sycl::_V1::ext::oneapi::experimental::detail::ExtractProperties< std::tuple< PropertiesTs... > >::Extract
static constexpr ExtractedPropertiesT< PropertyValueTs... > Extract(std::tuple< PropertyValueTs... >)
Definition: properties.hpp:97
sycl::_V1::ext::oneapi::experimental::detail::FindCompileTimePropertyValueType::type
void type
Definition: properties.hpp:43