1 //===- CRunnerUtils.h - Utils for debugging MLIR execution ----------------===//
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 // This file declares basic classes and functions to manipulate structured MLIR
10 // types at runtime. Entities in this file must be compliant with C++11 and be
11 // retargetable, including on targets without a C++ runtime.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef EXECUTIONENGINE_CRUNNERUTILS_H_
16 #define EXECUTIONENGINE_CRUNNERUTILS_H_
17 
18 #ifdef _WIN32
19 #ifndef MLIR_CRUNNERUTILS_EXPORT
20 #ifdef mlir_c_runner_utils_EXPORTS
21 // We are building this library
22 #define MLIR_CRUNNERUTILS_EXPORT __declspec(dllexport)
23 #define MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
24 #else
25 // We are using this library
26 #define MLIR_CRUNNERUTILS_EXPORT __declspec(dllimport)
27 #endif // mlir_c_runner_utils_EXPORTS
28 #endif // MLIR_CRUNNERUTILS_EXPORT
29 #else
30 #define MLIR_CRUNNERUTILS_EXPORT
31 #define MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
32 #endif // _WIN32
33 
34 #include <cstdint>
35 
36 //===----------------------------------------------------------------------===//
37 // Codegen-compatible structures for Vector type.
38 //===----------------------------------------------------------------------===//
39 namespace detail {
40 
isPowerOf2(int N)41 constexpr bool isPowerOf2(int N) { return (!(N & (N - 1))); }
42 
nextPowerOf2(int N)43 constexpr unsigned nextPowerOf2(int N) {
44   return (N <= 1) ? 1 : (isPowerOf2(N) ? N : (2 * nextPowerOf2((N + 1) / 2)));
45 }
46 
47 template <typename T, int Dim, bool IsPowerOf2>
48 struct Vector1D;
49 
50 template <typename T, int Dim>
51 struct Vector1D<T, Dim, /*IsPowerOf2=*/true> {
52   Vector1D() {
53     static_assert(detail::nextPowerOf2(sizeof(T[Dim])) == sizeof(T[Dim]),
54                   "size error");
55   }
56   inline T &operator[](unsigned i) { return vector[i]; }
57   inline const T &operator[](unsigned i) const { return vector[i]; }
58 
59 private:
60   T vector[Dim];
61 };
62 
63 // 1-D vector, padded to the next power of 2 allocation.
64 // Specialization occurs to avoid zero size arrays (which fail in -Werror).
65 template <typename T, int Dim>
66 struct Vector1D<T, Dim, /*IsPowerOf2=*/false> {
67   Vector1D() {
68     static_assert(detail::nextPowerOf2(sizeof(T[Dim])) > sizeof(T[Dim]),
69                   "size error");
70     static_assert(detail::nextPowerOf2(sizeof(T[Dim])) < 2 * sizeof(T[Dim]),
71                   "size error");
72   }
73   inline T &operator[](unsigned i) { return vector[i]; }
74   inline const T &operator[](unsigned i) const { return vector[i]; }
75 
76 private:
77   T vector[Dim];
78   char padding[detail::nextPowerOf2(sizeof(T[Dim])) - sizeof(T[Dim])];
79 };
80 } // end namespace detail
81 
82 // N-D vectors recurse down to 1-D.
83 template <typename T, int Dim, int... Dims>
84 struct Vector {
85   inline Vector<T, Dims...> &operator[](unsigned i) { return vector[i]; }
86   inline const Vector<T, Dims...> &operator[](unsigned i) const {
87     return vector[i];
88   }
89 
90 private:
91   Vector<T, Dims...> vector[Dim];
92 };
93 
94 // 1-D vectors in LLVM are automatically padded to the next power of 2.
95 // We insert explicit padding in to account for this.
96 template <typename T, int Dim>
97 struct Vector<T, Dim>
98     : public detail::Vector1D<T, Dim, detail::isPowerOf2(sizeof(T[Dim]))> {};
99 
100 template <int D1, typename T>
101 using Vector1D = Vector<T, D1>;
102 template <int D1, int D2, typename T>
103 using Vector2D = Vector<T, D1, D2>;
104 template <int D1, int D2, int D3, typename T>
105 using Vector3D = Vector<T, D1, D2, D3>;
106 template <int D1, int D2, int D3, int D4, typename T>
107 using Vector4D = Vector<T, D1, D2, D3, D4>;
108 
109 template <int N>
110 void dropFront(int64_t arr[N], int64_t *res) {
111   for (unsigned i = 1; i < N; ++i)
112     *(res + i - 1) = arr[i];
113 }
114 
115 //===----------------------------------------------------------------------===//
116 // Codegen-compatible structures for StridedMemRef type.
117 //===----------------------------------------------------------------------===//
118 /// StridedMemRef descriptor type with static rank.
119 template <typename T, int N>
120 struct StridedMemRefType {
121   T *basePtr;
122   T *data;
123   int64_t offset;
124   int64_t sizes[N];
125   int64_t strides[N];
126   // This operator[] is extremely slow and only for sugaring purposes.
127   StridedMemRefType<T, N - 1> operator[](int64_t idx) {
128     StridedMemRefType<T, N - 1> res;
129     res.basePtr = basePtr;
130     res.data = data;
131     res.offset = offset + idx * strides[0];
132     dropFront<N>(sizes, res.sizes);
133     dropFront<N>(strides, res.strides);
134     return res;
135   }
136 };
137 
138 /// StridedMemRef descriptor type specialized for rank 1.
139 template <typename T>
140 struct StridedMemRefType<T, 1> {
141   T *basePtr;
142   T *data;
143   int64_t offset;
144   int64_t sizes[1];
145   int64_t strides[1];
146   T &operator[](int64_t idx) { return *(data + offset + idx * strides[0]); }
147 };
148 
149 /// StridedMemRef descriptor type specialized for rank 0.
150 template <typename T>
151 struct StridedMemRefType<T, 0> {
152   T *basePtr;
153   T *data;
154   int64_t offset;
155 };
156 
157 //===----------------------------------------------------------------------===//
158 // Codegen-compatible structure for UnrankedMemRef type.
159 //===----------------------------------------------------------------------===//
160 // Unranked MemRef
161 template <typename T>
162 struct UnrankedMemRefType {
163   int64_t rank;
164   void *descriptor;
165 };
166 
167 //===----------------------------------------------------------------------===//
168 // DynamicMemRefType type.
169 //===----------------------------------------------------------------------===//
170 // A reference to one of the StridedMemRef types.
171 template <typename T>
172 class DynamicMemRefType {
173 public:
174   explicit DynamicMemRefType(const StridedMemRefType<T, 0> &mem_ref)
175       : rank(0), basePtr(mem_ref.basePtr), data(mem_ref.data),
176         offset(mem_ref.offset), sizes(nullptr), strides(nullptr) {}
177   template <int N>
178   explicit DynamicMemRefType(const StridedMemRefType<T, N> &mem_ref)
179       : rank(N), basePtr(mem_ref.basePtr), data(mem_ref.data),
180         offset(mem_ref.offset), sizes(mem_ref.sizes), strides(mem_ref.strides) {
181   }
182   explicit DynamicMemRefType(const UnrankedMemRefType<T> &mem_ref)
183       : rank(mem_ref.rank) {
184     auto *desc = static_cast<StridedMemRefType<T, 1> *>(mem_ref.descriptor);
185     basePtr = desc->basePtr;
186     data = desc->data;
187     offset = desc->offset;
188     sizes = rank == 0 ? nullptr : desc->sizes;
189     strides = sizes + rank;
190   }
191 
192   int64_t rank;
193   T *basePtr;
194   T *data;
195   int64_t offset;
196   const int64_t *sizes;
197   const int64_t *strides;
198 };
199 
200 //===----------------------------------------------------------------------===//
201 // Small runtime support "lib" for vector.print lowering during codegen.
202 //===----------------------------------------------------------------------===//
203 extern "C" MLIR_CRUNNERUTILS_EXPORT void printI64(int64_t i);
204 extern "C" MLIR_CRUNNERUTILS_EXPORT void printU64(uint64_t u);
205 extern "C" MLIR_CRUNNERUTILS_EXPORT void printF32(float f);
206 extern "C" MLIR_CRUNNERUTILS_EXPORT void printF64(double d);
207 extern "C" MLIR_CRUNNERUTILS_EXPORT void printOpen();
208 extern "C" MLIR_CRUNNERUTILS_EXPORT void printClose();
209 extern "C" MLIR_CRUNNERUTILS_EXPORT void printComma();
210 extern "C" MLIR_CRUNNERUTILS_EXPORT void printNewline();
211 
212 //===----------------------------------------------------------------------===//
213 // Small runtime support for sparse tensors.
214 //===----------------------------------------------------------------------===//
215 extern "C" MLIR_CRUNNERUTILS_EXPORT void openMatrixC(char *filename,
216                                                      uint64_t *mdata,
217                                                      uint64_t *ndata,
218                                                      uint64_t *nnzdata);
219 extern "C" MLIR_CRUNNERUTILS_EXPORT void
220 readMatrixItemC(uint64_t *idata, uint64_t *jdata, double *ddata);
221 extern "C" MLIR_CRUNNERUTILS_EXPORT void closeMatrix();
222 extern "C" MLIR_CRUNNERUTILS_EXPORT char *getMatrix(uint64_t id);
223 
224 #endif // EXECUTIONENGINE_CRUNNERUTILS_H_
225