DPC++ Runtime
Runtime libraries for oneAPI DPC++
region.hpp
Go to the documentation of this file.
1 //==-------------- region.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 // Region type to implement the Explicit SIMD APIs.
9 //===----------------------------------------------------------------------===//
10 
11 #pragma once
12 
14 
15 #include <cstdint>
16 #include <sycl/detail/defines.hpp>
17 #include <type_traits>
18 #include <utility>
19 
20 namespace sycl {
21 inline namespace _V1 {
22 namespace ext::intel::esimd {
23 
25 // TODO move to detail?
26 
27 // The common base type of region types.
28 template <bool Is2D, typename T, int SizeY, int StrideY, int SizeX, int StrideX>
29 struct region_base {
30  using element_type = T;
31  static constexpr int length = SizeX * SizeY;
32  static constexpr int Is_2D = Is2D;
33  static constexpr int Size_x = SizeX;
34  static constexpr int Stride_x = StrideX;
35  static constexpr int Size_y = SizeY;
36  static constexpr int Stride_y = StrideY;
37  static constexpr int Size_in_bytes = sizeof(T) * length;
38 
39  static_assert(Size_x > 0 && Stride_x >= 0, "illegal region in x-dimension");
40  static_assert(Size_y > 0 && Stride_y >= 0, "illegal region in y-dimension");
41 
42  uint16_t M_offset_y;
43  uint16_t M_offset_x;
44 
45  explicit region_base() : M_offset_y(0), M_offset_x(0) {}
46 
47  explicit region_base(uint16_t OffsetX) : M_offset_y(0), M_offset_x(OffsetX) {}
48 
49  explicit region_base(uint16_t OffsetY, uint16_t OffsetX)
50  : M_offset_y(OffsetY), M_offset_x(OffsetX) {}
51 };
52 
53 // A basic 1D region type.
54 template <typename T, int Size, int Stride>
55 using region1d_t = region_base<false, T, 1, 1, Size, Stride>;
56 
57 // A basic 2D region type.
58 template <typename T, int SizeY, int StrideY, int SizeX, int StrideX>
59 using region2d_t = region_base<true, T, SizeY, StrideY, SizeX, StrideX>;
60 
61 // A region with a single element.
62 template <typename T>
63 using region1d_scalar_t =
64  region_base<false, T, 1 /*SizeY*/, 1 /*StrideY*/, 1, 1>;
65 
66 // simd_view forward declaration.
67 template <typename BaseTy, typename RegionTy> class simd_view;
68 
69 // Compute the region type of a simd_view type.
70 //
71 // A region type could be either
72 // - region1d_t
73 // - region2d_t
74 // - a pair (top_region_type, base_region_type)
75 //
76 // This is a recursive definition to capture the following rvalue region stack:
77 //
78 // simd<int 16> v;
79 // v.bit_cast_view<int, 4, 4>().select<1, 0, 4, 1>(0, 0).bit_cast_view<short>()
80 // = 0;
81 //
82 // The LHS will be represented as a rvalue
83 //
84 // simd_view({v, { region1d_t<short, 8, 1>(0, 0),
85 // { region2d_t<int, 1, 1, 4, 1>(0, 0),
86 // region2d_t<int, 4, 1, 4, 1>(0, 0)
87 // }}})
88 //
89 template <typename Ty> struct shape_type {
90  using element_type = Ty;
91  using type = void;
92 };
93 
94 template <typename Ty, int Size, int Stride>
95 struct shape_type<region1d_t<Ty, Size, Stride>> {
96  using element_type = Ty;
97  using type = region1d_t<Ty, Size, Stride>;
98  static constexpr int length = type::length;
99 };
100 
101 template <typename Ty> struct shape_type<region1d_scalar_t<Ty>> {
102  using element_type = Ty;
103  using type = region1d_t<Ty, 1, 1>;
104  static constexpr int length = type::length;
105 };
106 
107 template <typename Ty, int SizeY, int StrideY, int SizeX, int StrideX>
108 struct shape_type<region2d_t<Ty, SizeY, StrideY, SizeX, StrideX>> {
109  using element_type = Ty;
110  using type = region2d_t<Ty, SizeY, StrideY, SizeX, StrideX>;
111  static constexpr int length = type::length;
112 };
113 
114 // Forward the shape computation on the top region type.
115 template <typename TopRegionTy, typename BaseRegionTy>
116 struct shape_type<std::pair<TopRegionTy, BaseRegionTy>>
117  : public shape_type<TopRegionTy> {};
118 
119 // Forward the shape computation on the region type.
120 template <typename BaseTy, typename RegionTy>
121 struct shape_type<simd_view<BaseTy, RegionTy>> : public shape_type<RegionTy> {};
122 
123 // Utility functions to access region components.
124 template <typename T> T getTopRegion(T Reg) { return Reg; }
125 template <typename T, typename U> T getTopRegion(std::pair<T, U> Reg) {
126  return Reg.first;
127 }
128 
129 template <typename T> T getBaseRegion(T Reg) { return Reg; }
130 template <typename T, typename U> T getBaseRegion(std::pair<T, U> Reg) {
131  return Reg.second;
132 }
133 
135 
136 } // namespace ext::intel::esimd
137 } // namespace _V1
138 } // namespace sycl
139 
Definition: access.hpp:18
ValueT length(const ValueT *a, const int len)
Calculate the square root of the input array.
Definition: math.hpp:161