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