DPC++ Runtime
Runtime libraries for oneAPI DPC++
sycl_util.hpp
Go to the documentation of this file.
1 //==------------- sycl_util.hpp - DPC++ Explicit SIMD API -----------------==//
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 // Utility functions related to interaction with generic SYCL and used for
9 // implementing Explicit SIMD APIs.
10 //===----------------------------------------------------------------------===//
11 
12 #pragma once
13 
15 
16 #include <sycl/accessor.hpp>
17 
18 namespace sycl {
19 inline namespace _V1 {
20 namespace ext::intel::esimd::detail {
21 
22 // Checks that given type is a SYCL accessor type. Sets its static field
23 // \c value accordingly. Also, if the check is succesful, sets \c mode and
24 // \c target static fields to the accessor type's access mode and access target
25 // respectively. Otherwise they are set to -1.
26 template <typename T> struct is_sycl_accessor : public std::false_type {
27  static constexpr sycl::access::mode mode =
28  static_cast<sycl::access::mode>(-1);
29  static constexpr sycl::access::target target =
30  static_cast<sycl::access::target>(-1);
31 };
32 
33 template <typename DataT, int Dimensions, sycl::access::mode AccessMode,
34  sycl::access::target AccessTarget,
35  sycl::access::placeholder IsPlaceholder, typename PropertyListT>
36 struct is_sycl_accessor<sycl::accessor<
37  DataT, Dimensions, AccessMode, AccessTarget, IsPlaceholder, PropertyListT>>
38  : public std::true_type {
39  static constexpr sycl::access::mode mode = AccessMode;
40  static constexpr sycl::access::target target = AccessTarget;
41 };
42 
43 using accessor_mode_cap_val_t = bool;
44 
45 // Denotes an accessor's capability - whether it can read or write.
46 struct accessor_mode_cap {
47  static constexpr accessor_mode_cap_val_t can_read = false;
48  static constexpr accessor_mode_cap_val_t can_write = true;
49 };
50 
51 template <sycl::access::mode Mode, accessor_mode_cap_val_t Cap>
52 constexpr bool accessor_mode_has_capability() {
53  static_assert(Cap == accessor_mode_cap::can_read ||
54  Cap == accessor_mode_cap::can_write,
55  "unsupported capability");
56 
57  if constexpr (Mode == sycl::access::mode::atomic ||
59  Mode == sycl::access::mode::discard_read_write)
60  return true; // atomic and *read_write accessors can read/write
61 
62  return (Cap == accessor_mode_cap::can_read) ==
63  (Mode == sycl::access::mode::read);
64 }
65 
66 template <typename T> struct local_accessor_access_mode {
67  static constexpr sycl::access::mode mode =
68  static_cast<sycl::access::mode>(-1);
69 };
70 
71 template <typename DataT, int Dimensions>
72 struct local_accessor_access_mode<local_accessor<DataT, Dimensions>> {
73  static constexpr sycl::access::mode mode =
74  sycl::detail::accessModeFromConstness<DataT>();
75 };
76 
77 // Checks that given type is a SYCL accessor type with given capability and
78 // target.
79 template <typename T, accessor_mode_cap_val_t Capability>
80 struct is_device_accessor_with
81  : public std::conditional_t<
82  accessor_mode_has_capability<is_sycl_accessor<T>::mode,
83  Capability>() &&
84  (is_sycl_accessor<T>::target == sycl::access::target::device),
85  std::true_type, std::false_type> {};
86 
87 template <typename T, accessor_mode_cap_val_t Capability>
88 struct is_local_accessor_with
89  : public std::conditional_t<
90  sycl::detail::acc_properties::is_local_accessor_v<T> &&
91  accessor_mode_has_capability<local_accessor_access_mode<T>::mode,
92  Capability>(),
93  std::true_type, std::false_type> {};
94 
95 template <typename T, accessor_mode_cap_val_t Capability>
96 inline constexpr bool is_local_accessor_with_v =
97  is_local_accessor_with<T, Capability>::value;
98 
99 template <typename T, accessor_mode_cap_val_t Capability>
100 inline constexpr bool is_device_accessor_with_v =
101  is_device_accessor_with<T, Capability>::value;
102 
103 template <typename T, accessor_mode_cap_val_t Capability>
104 inline constexpr bool is_accessor_with_v =
105  is_device_accessor_with_v<T, Capability> ||
106  is_local_accessor_with_v<T, Capability>;
107 
108 template <typename T>
109 inline constexpr bool is_rw_device_accessor_v =
110  is_device_accessor_with_v<T, accessor_mode_cap::can_read> &&
111  is_device_accessor_with_v<T, accessor_mode_cap::can_write>;
112 
113 template <typename T>
114 inline constexpr bool is_rw_local_accessor_v =
115  is_local_accessor_with_v<T, accessor_mode_cap::can_read> &&
116  is_local_accessor_with_v<T, accessor_mode_cap::can_write>;
117 
118 template <typename T>
119 inline constexpr bool is_rw_accessor_v =
120  is_rw_device_accessor_v<T> || is_rw_local_accessor_v<T>;
121 
122 template <typename T, accessor_mode_cap_val_t Capability, typename RetT>
123 using EnableIfAccessor =
124  std::enable_if_t<detail::is_device_accessor_with_v<T, Capability>, RetT>;
125 
126 template <typename T, int Dimensions>
127 __ESIMD_API uint32_t localAccessorToOffset(local_accessor<T, Dimensions> acc) {
128  return static_cast<uint32_t>(
129  reinterpret_cast<std::uintptr_t>(acc.get_pointer().get()));
130 }
131 
132 } // namespace ext::intel::esimd::detail
133 } // namespace _V1
134 } // namespace sycl
135 
The file contains implementations of accessor class.
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor accessor(buffer< DataT, Dimensions, AllocatorT >) -> accessor< DataT, Dimensions, access::mode::read_write, target::device, access::placeholder::true_t >
Buffer accessor.
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS Dimensions
Definition: accessor.hpp:3233
constexpr mode_tag_t< access_mode::read_write > read_write
Definition: access.hpp:85
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS IsPlaceholder
Definition: accessor.hpp:3234
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS AccessMode
Definition: accessor.hpp:3233
Definition: access.hpp:18