|
| 1 | +//==---------------- common.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 | +// definitions used in Explicit SIMD APIs. |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <CL/sycl/detail/defines.hpp> |
| 14 | + |
| 15 | +#include <cstdint> // for uint* types |
| 16 | +#include <type_traits> |
| 17 | + |
| 18 | +/// @cond ESIMD_DETAIL |
| 19 | + |
| 20 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 21 | +#define SYCL_ESIMD_KERNEL __attribute__((sycl_explicit_simd)) |
| 22 | +#define SYCL_ESIMD_FUNCTION __attribute__((sycl_explicit_simd)) |
| 23 | + |
| 24 | +// Mark a function being nodebug. |
| 25 | +#define ESIMD_NODEBUG __attribute__((nodebug)) |
| 26 | +// Mark a "ESIMD global": accessible from all functions in current translation |
| 27 | +// unit, separate copy per subgroup (work-item), mapped to SPIR-V private |
| 28 | +// storage class. |
| 29 | +#define ESIMD_PRIVATE \ |
| 30 | + __attribute__((opencl_private)) __attribute__((sycl_explicit_simd)) |
| 31 | +// Bind a ESIMD global variable to a specific register. |
| 32 | +#define ESIMD_REGISTER(n) __attribute__((register_num(n))) |
| 33 | + |
| 34 | +#define __ESIMD_API ESIMD_NODEBUG ESIMD_INLINE |
| 35 | + |
| 36 | +#define __ESIMD_UNSUPPORTED_ON_HOST |
| 37 | + |
| 38 | +#else // __SYCL_DEVICE_ONLY__ |
| 39 | +#define SYCL_ESIMD_KERNEL |
| 40 | +#define SYCL_ESIMD_FUNCTION |
| 41 | + |
| 42 | +// TODO ESIMD define what this means on Windows host |
| 43 | +#define ESIMD_NODEBUG |
| 44 | +// On host device ESIMD global is a thread local static var. This assumes that |
| 45 | +// each work-item is mapped to a separate OS thread on host device. |
| 46 | +#define ESIMD_PRIVATE thread_local |
| 47 | +#define ESIMD_REGISTER(n) |
| 48 | + |
| 49 | +#define __ESIMD_API ESIMD_INLINE |
| 50 | + |
| 51 | +#define __ESIMD_UNSUPPORTED_ON_HOST throw cl::sycl::feature_not_supported() |
| 52 | + |
| 53 | +#endif // __SYCL_DEVICE_ONLY__ |
| 54 | + |
| 55 | +// Mark a function being noinline |
| 56 | +#define ESIMD_NOINLINE __attribute__((noinline)) |
| 57 | +// Force a function to be inlined. 'inline' is used to preserve ODR for |
| 58 | +// functions defined in a header. |
| 59 | +#define ESIMD_INLINE inline __attribute__((always_inline)) |
| 60 | + |
| 61 | +// Macros for internal use |
| 62 | +#define __ESIMD_NS sycl::ext::intel::esimd |
| 63 | +#define __ESIMD_DNS sycl::ext::intel::esimd::detail |
| 64 | +#define __ESIMD_EMU_DNS sycl::ext::intel::esimd::emu::detail |
| 65 | + |
| 66 | +#define __ESIMD_QUOTE1(m) #m |
| 67 | +#define __ESIMD_QUOTE(m) __ESIMD_QUOTE1(m) |
| 68 | +#define __ESIMD_NS_QUOTED __ESIMD_QUOTE(__ESIMD_NS) |
| 69 | +#define __ESIMD_DEPRECATED(new_api) \ |
| 70 | + __SYCL_DEPRECATED("use " __ESIMD_NS_QUOTED "::" __ESIMD_QUOTE(new_api)) |
| 71 | + |
| 72 | +/// @endcond ESIMD_DETAIL |
| 73 | + |
| 74 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 75 | +namespace __ESIMD_NS { |
| 76 | + |
| 77 | +/// @addtogroup sycl_esimd_core |
| 78 | +/// @{ |
| 79 | + |
| 80 | +using uchar = unsigned char; |
| 81 | +using ushort = unsigned short; |
| 82 | +using uint = unsigned int; |
| 83 | + |
| 84 | +/// Gen hardware supports applying saturation to results of certain operations. |
| 85 | +/// This type tag represents "saturation on" behavior. |
| 86 | +struct saturation_on_tag : std::true_type {}; |
| 87 | + |
| 88 | +/// This type tag represents "saturation off" behavior. |
| 89 | +struct saturation_off_tag : std::false_type {}; |
| 90 | + |
| 91 | +/// Type tag object representing "saturation off" behavior. |
| 92 | +static inline constexpr saturation_off_tag saturation_off{}; |
| 93 | + |
| 94 | +/// Type tag object representing "saturation on" behavior. |
| 95 | +static inline constexpr saturation_on_tag saturation_on{}; |
| 96 | + |
| 97 | +/// Represents a pixel's channel. |
| 98 | +enum class rgba_channel : uint8_t { R, G, B, A }; |
| 99 | + |
| 100 | +/// Surface index type. Surface is an internal representation of a memory block |
| 101 | +/// addressable by GPU in "stateful" memory model, and each surface is |
| 102 | +/// identified by its "binding table index" - surface index. |
| 103 | +using SurfaceIndex = unsigned int; |
| 104 | + |
| 105 | +namespace detail { |
| 106 | +template <rgba_channel Ch> |
| 107 | +static inline constexpr uint8_t ch = 1 << static_cast<int>(Ch); |
| 108 | +static inline constexpr uint8_t chR = ch<rgba_channel::R>; |
| 109 | +static inline constexpr uint8_t chG = ch<rgba_channel::G>; |
| 110 | +static inline constexpr uint8_t chB = ch<rgba_channel::B>; |
| 111 | +static inline constexpr uint8_t chA = ch<rgba_channel::A>; |
| 112 | + |
| 113 | +// Shared Local Memory Binding Table Index (aka surface index). |
| 114 | +static inline constexpr SurfaceIndex SLM_BTI = 254; |
| 115 | +static inline constexpr SurfaceIndex INVALID_BTI = |
| 116 | + static_cast<SurfaceIndex>(-1); |
| 117 | +} // namespace detail |
| 118 | + |
| 119 | +/// Represents a pixel's channel mask - all possible combinations of enabled |
| 120 | +/// channels. |
| 121 | +enum class rgba_channel_mask : uint8_t { |
| 122 | + R = detail::chR, |
| 123 | + G = detail::chG, |
| 124 | + GR = detail::chG | detail::chR, |
| 125 | + B = detail::chB, |
| 126 | + BR = detail::chB | detail::chR, |
| 127 | + BG = detail::chB | detail::chG, |
| 128 | + BGR = detail::chB | detail::chG | detail::chR, |
| 129 | + A = detail::chA, |
| 130 | + AR = detail::chA | detail::chR, |
| 131 | + AG = detail::chA | detail::chG, |
| 132 | + AGR = detail::chA | detail::chG | detail::chR, |
| 133 | + AB = detail::chA | detail::chB, |
| 134 | + ABR = detail::chA | detail::chB | detail::chR, |
| 135 | + ABG = detail::chA | detail::chB | detail::chG, |
| 136 | + ABGR = detail::chA | detail::chB | detail::chG | detail::chR, |
| 137 | +}; |
| 138 | + |
| 139 | +constexpr int is_channel_enabled(rgba_channel_mask M, rgba_channel Ch) { |
| 140 | + int Pos = static_cast<int>(Ch); |
| 141 | + return (static_cast<int>(M) & (1 << Pos)) >> Pos; |
| 142 | +} |
| 143 | + |
| 144 | +constexpr int get_num_channels_enabled(rgba_channel_mask M) { |
| 145 | + return is_channel_enabled(M, rgba_channel::R) + |
| 146 | + is_channel_enabled(M, rgba_channel::G) + |
| 147 | + is_channel_enabled(M, rgba_channel::B) + |
| 148 | + is_channel_enabled(M, rgba_channel::A); |
| 149 | +} |
| 150 | + |
| 151 | +/// Represents an atomic operation. Operations always return the old value(s) of |
| 152 | +/// the target memory location(s) as it was before the operation was applied. |
| 153 | +/// Each operation is annotated with a pseudocode illustrating its semantics, |
| 154 | +/// \c addr is a memory address (one of the many, as the atomic operation is |
| 155 | +/// vector) the operation is applied at, \c src0 is its first argumnet, |
| 156 | +/// \c src1 - second. |
| 157 | +enum class atomic_op : uint8_t { |
| 158 | + /// Addition: <code>*addr = *addr + src0</code>. |
| 159 | + add = 0x0, |
| 160 | + /// Subtraction: <code>*addr = *addr - src0</code>. |
| 161 | + sub = 0x1, |
| 162 | + /// Increment: <code>*addr = *addr + 1</code>. |
| 163 | + inc = 0x2, |
| 164 | + /// Decrement: <code>*addr = *addr - 1</code>. |
| 165 | + dec = 0x3, |
| 166 | + /// Minimum: <code>*addr = min(*addr, src0)</code>. |
| 167 | + min = 0x4, |
| 168 | + /// Maximum: <code>*addr = max(*addr, src0)</code>. |
| 169 | + max = 0x5, |
| 170 | + /// Exchange. <code>*addr == src0;</code> |
| 171 | + xchg = 0x6, |
| 172 | + /// Compare and exchange. <code>if (*addr == src0) *sddr = src1;</code> |
| 173 | + cmpxchg = 0x7, |
| 174 | + /// Bit \c and: <code>*addr = *addr & src0</code>. |
| 175 | + bit_and = 0x8, |
| 176 | + /// Bit \c or: <code>*addr = *addr | src0</code>. |
| 177 | + bit_or = 0x9, |
| 178 | + /// Bit \c xor: <code>*addr = *addr | src0</code>. |
| 179 | + bit_xor = 0xa, |
| 180 | + /// Minimum (signed integer): <code>*addr = min(*addr, src0)</code>. |
| 181 | + minsint = 0xb, |
| 182 | + /// Maximum (signed integer): <code>*addr = max(*addr, src0)</code>. |
| 183 | + maxsint = 0xc, |
| 184 | + /// Minimum (floating point): <code>*addr = min(*addr, src0)</code>. |
| 185 | + fmax = 0x10, |
| 186 | + /// Maximum (floating point): <code>*addr = max(*addr, src0)</code>. |
| 187 | + fmin = 0x11, |
| 188 | + /// Compare and exchange (floating point). |
| 189 | + /// <code>if (*addr == src0) *addr = src1;</code> |
| 190 | + fcmpwr = 0x12, |
| 191 | + fadd = 0x13, |
| 192 | + fsub = 0x14, |
| 193 | + load = 0x15, |
| 194 | + store = 0x16, |
| 195 | + /// Decrement: <code>*addr = *addr - 1</code>. The only operation which |
| 196 | + /// returns new value of the destination rather than old. |
| 197 | + predec = 0xff, |
| 198 | +}; |
| 199 | + |
| 200 | +/// @} sycl_esimd_core |
| 201 | + |
| 202 | +} // namespace __ESIMD_NS |
| 203 | +} // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments