mlir_runner_utils.h
9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//===- mlir_runner_utils.h - Utils for debugging MLIR CPU execution -------===//
//
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_CPU_RUNNER_MLIRUTILS_H_
#define MLIR_CPU_RUNNER_MLIRUTILS_H_
#include <assert.h>
#include <cstdint>
#include <iostream>
#ifdef _WIN32
#ifndef MLIR_RUNNER_UTILS_EXPORT
#ifdef mlir_runner_utils_EXPORTS
/* We are building this library */
#define MLIR_RUNNER_UTILS_EXPORT __declspec(dllexport)
#else
/* We are using this library */
#define MLIR_RUNNER_UTILS_EXPORT __declspec(dllimport)
#endif // mlir_runner_utils_EXPORTS
#endif // MLIR_RUNNER_UTILS_EXPORT
#else
#define MLIR_RUNNER_UTILS_EXPORT
#endif // _WIN32
template <typename T, int N> struct StridedMemRefType;
template <typename StreamType, typename T, int N>
void printMemRefMetaData(StreamType &os, StridedMemRefType<T, N> &V);
template <int N> void dropFront(int64_t arr[N], int64_t *res) {
for (unsigned i = 1; i < N; ++i)
*(res + i - 1) = arr[i];
}
/// StridedMemRef descriptor type with static rank.
template <typename T, int N> struct StridedMemRefType {
T *basePtr;
T *data;
int64_t offset;
int64_t sizes[N];
int64_t strides[N];
// This operator[] is extremely slow and only for sugaring purposes.
StridedMemRefType<T, N - 1> operator[](int64_t idx) {
StridedMemRefType<T, N - 1> res;
res.basePtr = basePtr;
res.data = data;
res.offset = offset + idx * strides[0];
dropFront<N>(sizes, res.sizes);
dropFront<N>(strides, res.strides);
return res;
}
};
/// StridedMemRef descriptor type specialized for rank 1.
template <typename T> struct StridedMemRefType<T, 1> {
T *basePtr;
T *data;
int64_t offset;
int64_t sizes[1];
int64_t strides[1];
T &operator[](int64_t idx) { return *(data + offset + idx * strides[0]); }
};
/// StridedMemRef descriptor type specialized for rank 0.
template <typename T> struct StridedMemRefType<T, 0> {
T *basePtr;
T *data;
int64_t offset;
};
// Unranked MemRef
template <typename T> struct UnrankedMemRefType {
int64_t rank;
void *descriptor;
};
template <typename StreamType, typename T, int N>
void printMemRefMetaData(StreamType &os, StridedMemRefType<T, N> &V) {
static_assert(N > 0, "Expected N > 0");
os << "Memref base@ = " << reinterpret_cast<void *>(V.data) << " rank = " << N
<< " offset = " << V.offset << " sizes = [" << V.sizes[0];
for (unsigned i = 1; i < N; ++i)
os << ", " << V.sizes[i];
os << "] strides = [" << V.strides[0];
for (unsigned i = 1; i < N; ++i)
os << ", " << V.strides[i];
os << "]";
}
template <typename StreamType, typename T>
void printMemRefMetaData(StreamType &os, StridedMemRefType<T, 0> &V) {
os << "Memref base@ = " << reinterpret_cast<void *>(V.data) << " rank = 0"
<< " offset = " << V.offset;
}
template <typename T, typename StreamType>
void printUnrankedMemRefMetaData(StreamType &os, UnrankedMemRefType<T> &V) {
os << "Unranked Memref rank = " << V.rank << " "
<< "descriptor@ = " << reinterpret_cast<void *>(V.descriptor) << "\n";
}
template <typename T, int Dim, int... Dims> struct Vector {
Vector<T, Dims...> vector[Dim];
};
template <typename T, int Dim> struct Vector<T, Dim> { T vector[Dim]; };
template <int D1, typename T> using Vector1D = Vector<T, D1>;
template <int D1, int D2, typename T> using Vector2D = Vector<T, D1, D2>;
template <int D1, int D2, int D3, typename T>
using Vector3D = Vector<T, D1, D2, D3>;
template <int D1, int D2, int D3, int D4, typename T>
using Vector4D = Vector<T, D1, D2, D3, D4>;
////////////////////////////////////////////////////////////////////////////////
// Templated instantiation follows.
////////////////////////////////////////////////////////////////////////////////
namespace impl {
template <typename T, int M, int... Dims>
std::ostream &operator<<(std::ostream &os, const Vector<T, M, Dims...> &v);
template <int... Dims> struct StaticSizeMult {
static constexpr int value = 1;
};
template <int N, int... Dims> struct StaticSizeMult<N, Dims...> {
static constexpr int value = N * StaticSizeMult<Dims...>::value;
};
static inline void printSpace(std::ostream &os, int count) {
for (int i = 0; i < count; ++i) {
os << ' ';
}
}
template <typename T, int M, int... Dims> struct VectorDataPrinter {
static void print(std::ostream &os, const Vector<T, M, Dims...> &val);
};
template <typename T, int M, int... Dims>
void VectorDataPrinter<T, M, Dims...>::print(std::ostream &os,
const Vector<T, M, Dims...> &val) {
static_assert(M > 0, "0 dimensioned tensor");
static_assert(sizeof(val) == M * StaticSizeMult<Dims...>::value * sizeof(T),
"Incorrect vector size!");
// First
os << "(" << val.vector[0];
if (M > 1)
os << ", ";
if (sizeof...(Dims) > 1)
os << "\n";
// Kernel
for (unsigned i = 1; i + 1 < M; ++i) {
printSpace(os, 2 * sizeof...(Dims));
os << val.vector[i] << ", ";
if (sizeof...(Dims) > 1)
os << "\n";
}
// Last
if (M > 1) {
printSpace(os, sizeof...(Dims));
os << val.vector[M - 1];
}
os << ")";
}
template <typename T, int M, int... Dims>
std::ostream &operator<<(std::ostream &os, const Vector<T, M, Dims...> &v) {
VectorDataPrinter<T, M, Dims...>::print(os, v);
return os;
}
template <typename T, int N> struct MemRefDataPrinter {
static void print(std::ostream &os, T *base, int64_t rank, int64_t offset,
int64_t *sizes, int64_t *strides);
static void printFirst(std::ostream &os, T *base, int64_t rank,
int64_t offset, int64_t *sizes, int64_t *strides);
static void printLast(std::ostream &os, T *base, int64_t rank, int64_t offset,
int64_t *sizes, int64_t *strides);
};
template <typename T> struct MemRefDataPrinter<T, 0> {
static void print(std::ostream &os, T *base, int64_t rank, int64_t offset,
int64_t *sizes = nullptr, int64_t *strides = nullptr);
};
template <typename T, int N>
void MemRefDataPrinter<T, N>::printFirst(std::ostream &os, T *base,
int64_t rank, int64_t offset,
int64_t *sizes, int64_t *strides) {
os << "[";
MemRefDataPrinter<T, N - 1>::print(os, base, rank, offset, sizes + 1,
strides + 1);
// If single element, close square bracket and return early.
if (sizes[0] <= 1) {
os << "]";
return;
}
os << ", ";
if (N > 1)
os << "\n";
}
template <typename T, int N>
void MemRefDataPrinter<T, N>::print(std::ostream &os, T *base, int64_t rank,
int64_t offset, int64_t *sizes,
int64_t *strides) {
printFirst(os, base, rank, offset, sizes, strides);
for (unsigned i = 1; i + 1 < sizes[0]; ++i) {
printSpace(os, rank - N + 1);
MemRefDataPrinter<T, N - 1>::print(os, base, rank, offset + i * strides[0],
sizes + 1, strides + 1);
os << ", ";
if (N > 1)
os << "\n";
}
if (sizes[0] <= 1)
return;
printLast(os, base, rank, offset, sizes, strides);
}
template <typename T, int N>
void MemRefDataPrinter<T, N>::printLast(std::ostream &os, T *base, int64_t rank,
int64_t offset, int64_t *sizes,
int64_t *strides) {
printSpace(os, rank - N + 1);
MemRefDataPrinter<T, N - 1>::print(os, base, rank,
offset + (sizes[0] - 1) * (*strides),
sizes + 1, strides + 1);
os << "]";
}
template <typename T>
void MemRefDataPrinter<T, 0>::print(std::ostream &os, T *base, int64_t rank,
int64_t offset, int64_t *sizes,
int64_t *strides) {
os << base[offset];
}
template <typename T, int N> void printMemRef(StridedMemRefType<T, N> &M) {
static_assert(N > 0, "Expected N > 0");
printMemRefMetaData(std::cout, M);
std::cout << " data = " << std::endl;
MemRefDataPrinter<T, N>::print(std::cout, M.data, N, M.offset, M.sizes,
M.strides);
std::cout << std::endl;
}
template <typename T> void printMemRef(StridedMemRefType<T, 0> &M) {
printMemRefMetaData(std::cout, M);
std::cout << " data = " << std::endl;
std::cout << "[";
MemRefDataPrinter<T, 0>::print(std::cout, M.data, 0, M.offset);
std::cout << "]" << std::endl;
}
} // namespace impl
////////////////////////////////////////////////////////////////////////////////
// Currently exposed C API.
////////////////////////////////////////////////////////////////////////////////
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_i8(UnrankedMemRefType<int8_t> *M);
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_f32(UnrankedMemRefType<float> *M);
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_0d_f32(StridedMemRefType<float, 0> *M);
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_1d_f32(StridedMemRefType<float, 1> *M);
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_2d_f32(StridedMemRefType<float, 2> *M);
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_3d_f32(StridedMemRefType<float, 3> *M);
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_4d_f32(StridedMemRefType<float, 4> *M);
extern "C" MLIR_RUNNER_UTILS_EXPORT void
print_memref_vector_4x4xf32(StridedMemRefType<Vector2D<4, 4, float>, 2> *M);
// Small runtime support "lib" for vector.print lowering.
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_f32(float f);
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_f64(double d);
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_open();
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_close();
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_comma();
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_newline();
#endif // MLIR_CPU_RUNNER_MLIRUTILS_H_