DPC++ Runtime
Runtime libraries for oneAPI DPC++
alloc_util.hpp
Go to the documentation of this file.
1 //==-------- alloc_util.hpp - Utilities for annotated usm allocation -------==//
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 <numeric>
14 #include <sycl/property_list.hpp>
15 
16 namespace sycl {
17 inline namespace _V1 {
18 namespace ext {
19 namespace oneapi {
20 namespace experimental {
21 
22 namespace detail {
23 
25 // Type traits for USM allocation with property support
27 
28 // Merge a property list with the usm_kind property
29 template <sycl::usm::alloc Kind, typename PropertyListT>
30 using MergeUsmKind =
31  detail::merged_properties_t<PropertyListT,
32  decltype(properties{usm_kind<Kind>})>;
33 
34 // Check if a property list contains the a certain property
35 template <typename PropKey, typename PropertyListT> struct HasProperty {};
36 
37 template <typename PropKey, typename... Props>
38 struct HasProperty<PropKey, detail::properties_t<Props...>>
39  : detail::ContainsProperty<PropKey, std::tuple<Props...>> {};
40 
41 template <typename PropertyListT>
43 template <typename PropertyListT>
45 template <typename PropertyListT>
47 
48 // Get the value of a property from a property list
49 template <typename PropKey, typename ConstType, typename DefaultPropVal,
50  typename PropertyListT>
52 
53 template <typename PropKey, typename ConstType, typename DefaultPropVal,
54  typename... Props>
55 struct GetPropertyValueFromPropList<PropKey, ConstType, DefaultPropVal,
56  detail::properties_t<Props...>> {
57  using prop_val_t = std::conditional_t<
58  detail::ContainsProperty<PropKey, std::tuple<Props...>>::value,
60  PropKey, std::tuple<Props...>>::type,
61  DefaultPropVal>;
62  static constexpr ConstType value =
64 };
65 
66 // Get the value of alignment from a property list
67 // If alignment is not present in the property list, set to default value 0
68 template <typename PropertyListT>
70  GetPropertyValueFromPropList<alignment_key, size_t, decltype(alignment<0>),
71  PropertyListT>;
72 // Get the value of usm_kind from a property list
73 // The usm_kind is sycl::usm::alloc::unknown by default
74 template <typename PropertyListT>
77  decltype(usm_kind<sycl::usm::alloc::unknown>),
78  PropertyListT>;
79 // Get the value of buffer_location from a property list
80 // The buffer location is -1 by default
81 template <typename PropertyListT>
84  decltype(sycl::ext::intel::experimental::buffer_location<-1>),
85  PropertyListT>;
86 
87 // Check if a runtime property is valid
88 template <typename Prop> struct IsRuntimePropertyValid : std::false_type {};
89 
90 // Validate the property list of annotated USM allocations by checking each
91 // property is
92 // 1. a valid compile-time property for annotated_ptr, or
93 // 2. a valid runtime property for annotated USM allocations
94 template <typename T, typename propertyList>
95 struct ValidAllocPropertyList : std::false_type {};
96 template <typename T>
97 struct ValidAllocPropertyList<T, detail::empty_properties_t> : std::true_type {
98 };
99 template <typename T, typename Prop, typename... Props>
100 struct ValidAllocPropertyList<T, detail::properties_t<Prop, Props...>>
101  : std::integral_constant<
102  bool, (is_valid_property<T *, Prop>::value ||
103  IsRuntimePropertyValid<Prop>::value) &&
104  ValidAllocPropertyList<
105  T, detail::properties_t<Props...>>::value> {
106  // check if a compile-time property is valid for annotated_ptr
109  "Found invalid compile-time property in the property list.");
110  // check if a runtime property is valid for malloc
113  "Found invalid runtime property in the property list.");
114 };
115 
116 // The utility to filter a given property list to get the properties for
117 // annotated_ptr
118 template <typename PropertyListT> struct GetCompileTimeProperties {};
119 
120 template <> struct GetCompileTimeProperties<detail::empty_properties_t> {
122 };
123 
124 template <typename Prop>
125 struct GetCompileTimeProperties<detail::properties_t<Prop>> {
126  using type =
127  std::conditional_t<detail::IsCompileTimePropertyValue<Prop>::value,
130 };
131 
132 template <typename Prop, typename... Props>
133 struct GetCompileTimeProperties<detail::properties_t<Prop, Props...>> {
135  std::conditional_t<detail::IsCompileTimePropertyValue<Prop>::value,
142 };
143 
144 // Given the input property list `PropertyListT` and the usm kind `Kind` of
145 // annotated USM allocation functions, generate the property list for the
146 // returned annotated_ptr by following the rules:
147 // 1. all the compile-time properties in PropertyListT should appear in the
148 // output properties
149 // 2. if PropertyListT contains usm_kind, the value should be the same as
150 // `Kind`, usm_kind property should appear in the ouput properties
151 // 3. if PropertyListT does not contain usm_kind, a usm_kind property with value
152 // `Kind` is inserted in the ouput properties
153 template <sycl::usm::alloc Kind, typename PropertyListT>
155 template <sycl::usm::alloc Kind, typename... Props>
157  detail::properties_t<Props...>> {
161 
162  static_assert(!HasUsmKind<input_properties_t>::value ||
164  "Input property list contains conflicting USM kind.");
165 
166  using type =
168  decltype(properties{usm_kind<Kind>})>;
169 };
170 
171 // Check if the 3 template parameters of annotated USM allocation functions,
172 // i.e.
173 // T: allocated data type
174 // propertyListA: input property list
175 // propertyListB: property list for output anntoated_ptr
176 // meet the following conditions:
177 // 1. T is not a property list
178 // 2. propertyListB == all the compile-time properties in propertyListA
179 template <typename T, typename propertyListA, typename propertyListB>
180 struct CheckTAndPropLists : std::false_type {};
181 
182 template <typename T, typename... PropsA, typename... PropsB>
183 struct CheckTAndPropLists<T, detail::properties_t<PropsA...>,
184  detail::properties_t<PropsB...>>
185  : std::integral_constant<
186  bool,
187  !is_property_list<T>::value &&
188  std::is_same_v<detail::properties_t<PropsB...>,
189  typename GetCompileTimeProperties<
190  detail::properties_t<PropsA...>>::type>> {};
191 
192 // Check if the 3 template parameters of annotated USM allocation functions of a
193 // certain usm kind:
194 // T: allocated data type
195 // propertyListA: input property list
196 // propertyListB: property list for output anntoated_ptr
197 // meet the following conditions:
198 // 1. T is not a property list
199 // 2. if propertyListA contains usm_kind, the usm_kind must match with the
200 // specified usm kind.
201 // propertyListB is all the compile-time properties in propertyListA
202 // 3. if propertyListA does not contain usm_kind, propertyListB is all the
203 // compile-time
204 // properties in propertyListA with the usm_kind property inserted
205 template <sycl::usm::alloc Kind, typename T, typename propertyListA,
206  typename propertyListB>
207 struct CheckTAndPropListsWithUsmKind : std::false_type {};
208 
209 template <sycl::usm::alloc Kind, typename T, typename... PropsA,
210  typename... PropsB>
211 struct CheckTAndPropListsWithUsmKind<Kind, T, detail::properties_t<PropsA...>,
212  detail::properties_t<PropsB...>>
213  : std::integral_constant<
214  bool, !is_property_list<T>::value &&
215  std::is_same_v<
216  detail::properties_t<PropsB...>,
217  typename GetAnnotatedPtrPropertiesWithUsmKind<
218  Kind, detail::properties_t<PropsA...>>::type>> {};
219 
220 } // namespace detail
221 
223 // Utility functions for USM allocation with property support
225 
226 // Transform a compile-time property list to a USM property_list (working at
227 // runtime). Right now only the `buffer_location<N>` has its corresponding USM
228 // runtime property and is transformable
229 template <typename PropertyListT> inline property_list get_usm_property_list() {
231  return property_list{
234  }
235  return {};
236 }
237 
238 // Combine two alignment requirements for a pointer in the following way:
239 // 1. if either of the alignments is 0, return the other alignment
240 // 2. otherwise return the least common multiple of the two alignments
241 inline size_t combine_align(size_t alignA, size_t alignB) {
242  return alignA == 0 ? alignB
243  : (alignB == 0 ? alignA : std::lcm(alignA, alignB));
244 }
245 
246 } // namespace experimental
247 } // namespace oneapi
248 } // namespace ext
249 } // namespace _V1
250 } // namespace sycl
Objects of the property_list class are containers for the SYCL properties.
sycl::ext::oneapi::experimental::alignment_key alignment_key
The 'alignment' property is used to specify the alignment of memory accessed in ESIMD memory operatio...
constexpr buffer_location_key::value_t< N > buffer_location
detail::merged_properties_t< PropertyListT, decltype(properties{usm_kind< Kind >})> MergeUsmKind
Definition: alloc_util.hpp:32
typename merged_properties< LHSPropertiesT, RHSPropertiesT >::type merged_properties_t
Definition: properties.hpp:225
properties< std::tuple<> > empty_properties_t
Definition: properties.hpp:207
intel::experimental::buffer_location_key buffer_location_key
size_t combine_align(size_t alignA, size_t alignB)
Definition: alloc_util.hpp:241
Definition: access.hpp:18
detail::merged_properties_t< filtered_input_properties_t, decltype(properties{usm_kind< Kind >})> type
Definition: alloc_util.hpp:168
std::conditional_t< detail::IsCompileTimePropertyValue< Prop >::value, detail::properties_t< Prop >, detail::empty_properties_t > filtered_this_property_t
Definition: alloc_util.hpp:137
detail::merged_properties_t< filtered_this_property_t, filtered_other_properties_t > type
Definition: alloc_util.hpp:141
typename GetCompileTimeProperties< detail::properties_t< Props... > >::type filtered_other_properties_t
Definition: alloc_util.hpp:139
std::conditional_t< detail::IsCompileTimePropertyValue< Prop >::value, detail::properties_t< Prop >, detail::empty_properties_t > type
Definition: alloc_util.hpp:129
std::conditional_t< detail::ContainsProperty< PropKey, std::tuple< Props... > >::value, typename detail::FindCompileTimePropertyValueType< PropKey, std::tuple< Props... > >::type, DefaultPropVal > prop_val_t
Definition: alloc_util.hpp:61