|
| 1 | +//==----- accessor_property_list.hpp --- SYCL accessor 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 | + |
| 11 | +#include <CL/sycl/access/access.hpp> |
| 12 | +#include <CL/sycl/detail/common.hpp> |
| 13 | +#include <CL/sycl/detail/property_list_base.hpp> |
| 14 | +#include <CL/sycl/property_list.hpp> |
| 15 | + |
| 16 | +#include <type_traits> |
| 17 | + |
| 18 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 19 | +namespace sycl { |
| 20 | +// Forward declaration |
| 21 | +template <typename, int, access::mode, access::target, access::placeholder, |
| 22 | + typename PropertyListT> |
| 23 | +class accessor; |
| 24 | +namespace detail { |
| 25 | +// This helper template must be specialized for nested instance template |
| 26 | +// of each compile-time-constant property. |
| 27 | +template <typename T> struct IsCompileTimePropertyInstance : std::false_type {}; |
| 28 | +} // namespace detail |
| 29 | +namespace ONEAPI { |
| 30 | + |
| 31 | +template <typename T> struct is_compile_time_property : std::false_type {}; |
| 32 | + |
| 33 | +/// Objects of the accessor_property_list class are containers for the SYCL |
| 34 | +/// properties. |
| 35 | +/// |
| 36 | +/// Unlike \c property_list, accessor_property_list can take |
| 37 | +/// compile-time-constant properties. |
| 38 | +/// |
| 39 | +/// \sa accessor |
| 40 | +/// \sa property_list |
| 41 | +/// |
| 42 | +/// \ingroup sycl_api |
| 43 | +template <typename... PropsT> |
| 44 | +class accessor_property_list : protected sycl::detail::PropertyListBase { |
| 45 | + // These structures check if compile-time-constant property is present in |
| 46 | + // list. For runtime properties this check is always true. |
| 47 | + template <class T, class U> struct AreSameTemplate : std::is_same<T, U> {}; |
| 48 | + template <template <class...> class T, class T1, class T2> |
| 49 | + struct AreSameTemplate<T<T1>, T<T2>> : std::true_type {}; |
| 50 | +#if __cplusplus >= 201703L |
| 51 | + // Declaring non-type template parameters with auto is a C++17 feature. Since |
| 52 | + // the extension is written against SYCL 2020, which implies use of C++17, |
| 53 | + // there's no need to provide alternative implementations for older standards. |
| 54 | + template <template <auto...> class T, auto... T1, auto... T2> |
| 55 | + struct AreSameTemplate<T<T1...>, T<T2...>> : std::true_type {}; |
| 56 | +#endif |
| 57 | + // This template helps to identify if PropListT parameter pack contains |
| 58 | + // property of PropT type, where PropT is a nested instance template of |
| 59 | + // compile-time-constant property. |
| 60 | + template <typename PropT, typename... PropListT> struct ContainsProperty; |
| 61 | + template <typename PropT> struct ContainsProperty<PropT> : std::false_type {}; |
| 62 | + template <typename PropT, typename Head, typename... Tail> |
| 63 | + struct ContainsProperty<PropT, Head, Tail...> |
| 64 | + : std::conditional<AreSameTemplate<PropT, Head>::value, std::true_type, |
| 65 | + ContainsProperty<PropT, Tail...>>::type {}; |
| 66 | + |
| 67 | + // PropertyContainer is a helper structure, that holds list of properties. |
| 68 | + // It is used to avoid multiple parameter packs in templates. |
| 69 | + template <typename...> struct PropertyContainer { |
| 70 | + using Head = void; |
| 71 | + using Rest = void; |
| 72 | + }; |
| 73 | + template <typename T, typename... Other> |
| 74 | + struct PropertyContainer<T, Other...> { |
| 75 | + using Head = T; |
| 76 | + using Rest = PropertyContainer<Other...>; |
| 77 | + }; |
| 78 | + template <typename T> struct PropertyContainer<T> { |
| 79 | + using Head = T; |
| 80 | + using Rest = void; |
| 81 | + }; |
| 82 | + |
| 83 | +#if __cplusplus >= 201703L |
| 84 | + // This template serves the same purpose as ContainsProperty, but operates on |
| 85 | + // template template arguments. |
| 86 | + template <typename ContainerT, template <auto...> typename PropT, |
| 87 | + auto... Args> |
| 88 | + struct ContainsPropertyInstance |
| 89 | + : std::conditional_t< |
| 90 | + !std::is_same_v<typename ContainerT::Head, void> && |
| 91 | + AreSameTemplate<PropT<Args...>, |
| 92 | + typename ContainerT::Head>::value, |
| 93 | + std::true_type, |
| 94 | + ContainsPropertyInstance<typename ContainerT::Rest, PropT, |
| 95 | + Args...>> {}; |
| 96 | + |
| 97 | + template <template <auto...> typename PropT, auto... Args> |
| 98 | + struct ContainsPropertyInstance<void, PropT, Args...> : std::false_type {}; |
| 99 | +#endif |
| 100 | + |
| 101 | + // This template checks if two lists of properties contain the same set of |
| 102 | + // compile-time-constant properties in any order. Run time properties are |
| 103 | + // skipped. |
| 104 | + template <typename ContainerT, typename... OtherProps> |
| 105 | + struct ContainsSameProperties |
| 106 | + : std::conditional< |
| 107 | + !detail::IsCompileTimePropertyInstance< |
| 108 | + typename ContainerT::Head>::value || |
| 109 | + ContainsProperty<typename ContainerT::Head, |
| 110 | + OtherProps...>::value, |
| 111 | + ContainsSameProperties<typename ContainerT::Rest, OtherProps...>, |
| 112 | + std::false_type>::type {}; |
| 113 | + template <typename... OtherProps> |
| 114 | + struct ContainsSameProperties<void, OtherProps...> : std::true_type {}; |
| 115 | + |
| 116 | +#if __cplusplus >= 201703L |
| 117 | + // This template helps to extract exact property instance type based on |
| 118 | + // template template argument. If there's an instance of target property in |
| 119 | + // ContainerT, find instance template and use it as type. Otherwise, just |
| 120 | + // use void as return type. |
| 121 | + template <typename ContainerT, template <auto...> class PropT, auto... Args> |
| 122 | + struct GetCompileTimePropertyHelper { |
| 123 | + using type = typename std::conditional_t< |
| 124 | + AreSameTemplate<typename ContainerT::Head, PropT<Args...>>::value, |
| 125 | + typename ContainerT::Head, |
| 126 | + typename GetCompileTimePropertyHelper<typename ContainerT::Rest, PropT, |
| 127 | + Args...>::type>; |
| 128 | + }; |
| 129 | + template <typename Head, template <auto...> class PropT, auto... Args> |
| 130 | + struct GetCompileTimePropertyHelper<PropertyContainer<Head>, PropT, Args...> { |
| 131 | + using type = typename std::conditional_t< |
| 132 | + AreSameTemplate<Head, PropT<Args...>>::value, Head, void>; |
| 133 | + }; |
| 134 | +#endif |
| 135 | + |
| 136 | + // The structs validate that all objects passed are SYCL properties. |
| 137 | + // Properties are either run time SYCL 1.2.1 properties, and thus derive from |
| 138 | + // either DataLessPropertyBase or from PropertyWithDataBase, or |
| 139 | + // compile-time-constant properties, and thus specialize |
| 140 | + // IsCompileTimePropertyInstance template. |
| 141 | + template <typename... Tail> struct AllProperties : std::true_type {}; |
| 142 | + template <typename T, typename... Tail> |
| 143 | + struct AllProperties<T, Tail...> |
| 144 | + : std::conditional< |
| 145 | + std::is_base_of<sycl::detail::DataLessPropertyBase, T>::value || |
| 146 | + std::is_base_of<sycl::detail::PropertyWithDataBase, T>::value || |
| 147 | + sycl::detail::IsCompileTimePropertyInstance<T>::value, |
| 148 | + AllProperties<Tail...>, std::false_type>::type {}; |
| 149 | + |
| 150 | + accessor_property_list( |
| 151 | + std::bitset<sycl::detail::DataLessPropKind::DataLessPropKindSize> |
| 152 | + DataLessProps, |
| 153 | + std::vector<std::shared_ptr<sycl::detail::PropertyWithDataBase>> |
| 154 | + PropsWithData) |
| 155 | + : sycl::detail::PropertyListBase(DataLessProps, PropsWithData) {} |
| 156 | + |
| 157 | +public: |
| 158 | + template < |
| 159 | + typename = typename std::enable_if<AllProperties<PropsT...>::value>::type> |
| 160 | + accessor_property_list(PropsT... Props) |
| 161 | + : sycl::detail::PropertyListBase(false) { |
| 162 | + ctorHelper(Props...); |
| 163 | + } |
| 164 | + |
| 165 | + accessor_property_list(const sycl::property_list &Props) |
| 166 | + : sycl::detail::PropertyListBase(Props.MDataLessProps, |
| 167 | + Props.MPropsWithData) {} |
| 168 | + |
| 169 | + template <typename... OtherProps, |
| 170 | + typename = typename std::enable_if< |
| 171 | + ContainsSameProperties<PropertyContainer<PropsT...>, |
| 172 | + OtherProps...>::value && |
| 173 | + ContainsSameProperties<PropertyContainer<OtherProps...>, |
| 174 | + PropsT...>::value>::type> |
| 175 | + accessor_property_list(const accessor_property_list<OtherProps...> &OtherList) |
| 176 | + : sycl::detail::PropertyListBase(OtherList.MDataLessProps, |
| 177 | + OtherList.MPropsWithData) {} |
| 178 | + |
| 179 | + template <typename PropT, typename = typename std::enable_if< |
| 180 | + !is_compile_time_property<PropT>::value>::type> |
| 181 | + PropT get_property() const { |
| 182 | + if (!has_property<PropT>()) |
| 183 | + throw sycl::invalid_object_error("The property is not found", |
| 184 | + PI_INVALID_VALUE); |
| 185 | + |
| 186 | + return get_property_helper<PropT>(); |
| 187 | + } |
| 188 | + |
| 189 | + template <class PropT> |
| 190 | + typename std::enable_if<!is_compile_time_property<PropT>::value, bool>::type |
| 191 | + has_property() const { |
| 192 | + return has_property_helper<PropT>(); |
| 193 | + } |
| 194 | + |
| 195 | +#if __cplusplus >= 201703L |
| 196 | + template <typename T> |
| 197 | + static constexpr |
| 198 | + typename std::enable_if_t<is_compile_time_property<T>::value, bool> |
| 199 | + has_property() { |
| 200 | + return ContainsPropertyInstance<PropertyContainer<PropsT...>, |
| 201 | + T::template instance>::value; |
| 202 | + } |
| 203 | + |
| 204 | + template <typename T, |
| 205 | + typename = typename std::enable_if_t< |
| 206 | + is_compile_time_property<T>::value && has_property<T>()>> |
| 207 | + static constexpr auto get_property() { |
| 208 | + return typename GetCompileTimePropertyHelper<PropertyContainer<PropsT...>, |
| 209 | + T::template instance>::type{}; |
| 210 | + } |
| 211 | +#endif |
| 212 | + |
| 213 | +private: |
| 214 | + template <typename, int, access::mode, access::target, access::placeholder, |
| 215 | + typename PropertyListT> |
| 216 | + friend class sycl::accessor; |
| 217 | + |
| 218 | + template <typename... OtherProps> friend class accessor_property_list; |
| 219 | + |
| 220 | + friend class sycl::property_list; |
| 221 | + |
| 222 | + // Helper method, used by accessor to restrict conversions to compatible |
| 223 | + // property lists. |
| 224 | + template <typename... OtherPropsT> |
| 225 | + static constexpr bool areSameCompileTimeProperties() { |
| 226 | + return ContainsSameProperties<PropertyContainer<OtherPropsT...>, |
| 227 | + PropsT...>::value; |
| 228 | + } |
| 229 | +}; |
| 230 | +} // namespace ONEAPI |
| 231 | +} // namespace sycl |
| 232 | +} // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments