1//===- RunnerUtils.cpp - Utils for MLIR exec on targets with a C++ runtime ===//
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 implements basic functions to debug structured MLIR types at
10// runtime. Entities in this file may not be compatible with targets without a
11// C++ runtime. These may be progressively migrated to CRunnerUtils.cpp over
12// time.
13//
14//===----------------------------------------------------------------------===//
15
16#include "mlir/ExecutionEngine/RunnerUtils.h"
17#include <chrono>
18
19// NOLINTBEGIN(*-identifier-naming)
20
21extern "C" void _mlir_ciface_printMemrefShapeI8(UnrankedMemRefType<int8_t> *M) {
22 std::cout << "Unranked Memref ";
23 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<int8_t>(*M));
24 std::cout << "\n";
25}
26
27extern "C" void
28_mlir_ciface_printMemrefShapeI32(UnrankedMemRefType<int32_t> *M) {
29 std::cout << "Unranked Memref ";
30 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<int32_t>(*M));
31 std::cout << "\n";
32}
33
34extern "C" void
35_mlir_ciface_printMemrefShapeI64(UnrankedMemRefType<int64_t> *M) {
36 std::cout << "Unranked Memref ";
37 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<int64_t>(*M));
38 std::cout << "\n";
39}
40
41extern "C" void _mlir_ciface_printMemrefShapeF32(UnrankedMemRefType<float> *M) {
42 std::cout << "Unranked Memref ";
43 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<float>(*M));
44 std::cout << "\n";
45}
46
47extern "C" void
48_mlir_ciface_printMemrefShapeF64(UnrankedMemRefType<double> *M) {
49 std::cout << "Unranked Memref ";
50 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<double>(*M));
51 std::cout << "\n";
52}
53
54extern "C" void
55_mlir_ciface_printMemrefShapeInd(UnrankedMemRefType<impl::index_type> *M) {
56 std::cout << "Unranked Memref ";
57 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<impl::index_type>(*M));
58 std::cout << "\n";
59}
60
61extern "C" void
62_mlir_ciface_printMemrefShapeC32(UnrankedMemRefType<impl::complex32> *M) {
63 std::cout << "Unranked Memref ";
64 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<impl::complex32>(*M));
65 std::cout << "\n";
66}
67
68extern "C" void
69_mlir_ciface_printMemrefShapeC64(UnrankedMemRefType<impl::complex64> *M) {
70 std::cout << "Unranked Memref ";
71 printMemRefMetaData(os&: std::cout, v: DynamicMemRefType<impl::complex64>(*M));
72 std::cout << "\n";
73}
74
75extern "C" void _mlir_ciface_printMemrefVector4x4xf32(
76 StridedMemRefType<Vector2D<4, 4, float>, 2> *M) {
77 impl::printMemRef(m&: *M);
78}
79
80extern "C" void _mlir_ciface_printMemrefI8(UnrankedMemRefType<int8_t> *M) {
81 impl::printMemRef(m&: *M);
82}
83
84extern "C" void _mlir_ciface_printMemrefI16(UnrankedMemRefType<int16_t> *M) {
85 impl::printMemRef(m&: *M);
86}
87
88extern "C" void _mlir_ciface_printMemrefI32(UnrankedMemRefType<int32_t> *M) {
89 impl::printMemRef(m&: *M);
90}
91
92extern "C" void _mlir_ciface_printMemrefI64(UnrankedMemRefType<int64_t> *M) {
93 impl::printMemRef(m&: *M);
94}
95
96extern "C" void _mlir_ciface_printMemrefF16(UnrankedMemRefType<f16> *M) {
97 impl::printMemRef(m&: *M);
98}
99
100extern "C" void _mlir_ciface_printMemrefBF16(UnrankedMemRefType<bf16> *M) {
101 impl::printMemRef(m&: *M);
102}
103
104extern "C" void _mlir_ciface_printMemrefF32(UnrankedMemRefType<float> *M) {
105 impl::printMemRef(m&: *M);
106}
107
108extern "C" void _mlir_ciface_printMemrefF64(UnrankedMemRefType<double> *M) {
109 impl::printMemRef(m&: *M);
110}
111
112extern "C" void
113_mlir_ciface_printMemrefInd(UnrankedMemRefType<impl::index_type> *M) {
114 impl::printMemRef(m&: *M);
115}
116
117extern "C" void
118_mlir_ciface_printMemrefC32(UnrankedMemRefType<impl::complex32> *M) {
119 impl::printMemRef(m&: *M);
120}
121
122extern "C" void
123_mlir_ciface_printMemrefC64(UnrankedMemRefType<impl::complex64> *M) {
124 impl::printMemRef(m&: *M);
125}
126
127extern "C" int64_t _mlir_ciface_nanoTime() {
128 auto now = std::chrono::high_resolution_clock::now();
129 auto duration = now.time_since_epoch();
130 auto nanoseconds =
131 std::chrono::duration_cast<std::chrono::nanoseconds>(d: duration);
132 return nanoseconds.count();
133}
134
135extern "C" void printMemrefI32(int64_t rank, void *ptr) {
136 UnrankedMemRefType<int32_t> descriptor = {.rank: rank, .descriptor: ptr};
137 _mlir_ciface_printMemrefI32(M: &descriptor);
138}
139
140extern "C" void printMemrefI64(int64_t rank, void *ptr) {
141 UnrankedMemRefType<int64_t> descriptor = {.rank: rank, .descriptor: ptr};
142 _mlir_ciface_printMemrefI64(M: &descriptor);
143}
144
145extern "C" void printMemrefF32(int64_t rank, void *ptr) {
146 UnrankedMemRefType<float> descriptor = {.rank: rank, .descriptor: ptr};
147 _mlir_ciface_printMemrefF32(M: &descriptor);
148}
149
150extern "C" void printMemrefF64(int64_t rank, void *ptr) {
151 UnrankedMemRefType<double> descriptor = {.rank: rank, .descriptor: ptr};
152 _mlir_ciface_printMemrefF64(M: &descriptor);
153}
154
155// Assume index_type is in fact uint64_t.
156static_assert(std::is_same<impl::index_type, uint64_t>::value,
157 "Expected index_type == uint64_t");
158extern "C" void printMemrefInd(int64_t rank, void *ptr) {
159 UnrankedMemRefType<impl::index_type> descriptor = {.rank: rank, .descriptor: ptr};
160 _mlir_ciface_printMemrefInd(M: &descriptor);
161}
162
163extern "C" void printMemrefC32(int64_t rank, void *ptr) {
164 UnrankedMemRefType<impl::complex32> descriptor = {.rank: rank, .descriptor: ptr};
165 _mlir_ciface_printMemrefC32(M: &descriptor);
166}
167
168extern "C" void printMemrefC64(int64_t rank, void *ptr) {
169 UnrankedMemRefType<impl::complex64> descriptor = {.rank: rank, .descriptor: ptr};
170 _mlir_ciface_printMemrefC64(M: &descriptor);
171}
172
173extern "C" void _mlir_ciface_printMemref0dF32(StridedMemRefType<float, 0> *M) {
174 impl::printMemRef(m&: *M);
175}
176extern "C" void _mlir_ciface_printMemref1dF32(StridedMemRefType<float, 1> *M) {
177 impl::printMemRef(m&: *M);
178}
179extern "C" void _mlir_ciface_printMemref2dF32(StridedMemRefType<float, 2> *M) {
180 impl::printMemRef(m&: *M);
181}
182extern "C" void _mlir_ciface_printMemref3dF32(StridedMemRefType<float, 3> *M) {
183 impl::printMemRef(m&: *M);
184}
185extern "C" void _mlir_ciface_printMemref4dF32(StridedMemRefType<float, 4> *M) {
186 impl::printMemRef(m&: *M);
187}
188
189extern "C" void _mlir_ciface_printMemref1dI8(StridedMemRefType<int8_t, 1> *M) {
190 impl::printMemRef(m&: *M);
191}
192
193extern "C" void
194_mlir_ciface_printMemref1dI32(StridedMemRefType<int32_t, 1> *M) {
195 impl::printMemRef(m&: *M);
196}
197
198extern "C" void
199_mlir_ciface_printMemref1dI64(StridedMemRefType<int64_t, 1> *M) {
200 impl::printMemRef(m&: *M);
201}
202
203extern "C" void _mlir_ciface_printMemref1dF64(StridedMemRefType<double, 1> *M) {
204 impl::printMemRef(m&: *M);
205}
206
207extern "C" void
208_mlir_ciface_printMemref1dInd(StridedMemRefType<impl::index_type, 1> *M) {
209 impl::printMemRef(m&: *M);
210}
211
212extern "C" void
213_mlir_ciface_printMemref1dC32(StridedMemRefType<impl::complex32, 1> *M) {
214 impl::printMemRef(m&: *M);
215}
216
217extern "C" void
218_mlir_ciface_printMemref1dC64(StridedMemRefType<impl::complex64, 1> *M) {
219 impl::printMemRef(m&: *M);
220}
221
222extern "C" int64_t
223_mlir_ciface_verifyMemRefI8(UnrankedMemRefType<int8_t> *actual,
224 UnrankedMemRefType<int8_t> *expected) {
225 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
226}
227
228extern "C" int64_t
229_mlir_ciface_verifyMemRefI16(UnrankedMemRefType<int16_t> *actual,
230 UnrankedMemRefType<int16_t> *expected) {
231 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
232}
233
234extern "C" int64_t
235_mlir_ciface_verifyMemRefI32(UnrankedMemRefType<int32_t> *actual,
236 UnrankedMemRefType<int32_t> *expected) {
237 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
238}
239
240extern "C" int64_t
241_mlir_ciface_verifyMemRefI64(UnrankedMemRefType<int64_t> *actual,
242 UnrankedMemRefType<int64_t> *expected) {
243 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
244}
245
246extern "C" int64_t
247_mlir_ciface_verifyMemRefF16(UnrankedMemRefType<f16> *actual,
248 UnrankedMemRefType<f16> *expected) {
249 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
250}
251
252extern "C" int64_t
253_mlir_ciface_verifyMemRefBF16(UnrankedMemRefType<bf16> *actual,
254 UnrankedMemRefType<bf16> *expected) {
255 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
256}
257
258extern "C" int64_t
259_mlir_ciface_verifyMemRefF32(UnrankedMemRefType<float> *actual,
260 UnrankedMemRefType<float> *expected) {
261 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
262}
263
264extern "C" int64_t
265_mlir_ciface_verifyMemRefF64(UnrankedMemRefType<double> *actual,
266 UnrankedMemRefType<double> *expected) {
267 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
268}
269
270extern "C" int64_t
271_mlir_ciface_verifyMemRefInd(UnrankedMemRefType<impl::index_type> *actual,
272 UnrankedMemRefType<impl::index_type> *expected) {
273 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
274}
275
276extern "C" int64_t
277_mlir_ciface_verifyMemRefC32(UnrankedMemRefType<impl::complex32> *actual,
278 UnrankedMemRefType<impl::complex32> *expected) {
279 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
280}
281
282extern "C" int64_t
283_mlir_ciface_verifyMemRefC64(UnrankedMemRefType<impl::complex64> *actual,
284 UnrankedMemRefType<impl::complex64> *expected) {
285 return impl::verifyMemRef(actual&: *actual, expected&: *expected);
286}
287
288extern "C" int64_t verifyMemRefI32(int64_t rank, void *actualPtr,
289 void *expectedPtr) {
290 UnrankedMemRefType<int32_t> actualDesc = {.rank: rank, .descriptor: actualPtr};
291 UnrankedMemRefType<int32_t> expectedDesc = {.rank: rank, .descriptor: expectedPtr};
292 return _mlir_ciface_verifyMemRefI32(actual: &actualDesc, expected: &expectedDesc);
293}
294
295extern "C" int64_t verifyMemRefF32(int64_t rank, void *actualPtr,
296 void *expectedPtr) {
297 UnrankedMemRefType<float> actualDesc = {.rank: rank, .descriptor: actualPtr};
298 UnrankedMemRefType<float> expectedDesc = {.rank: rank, .descriptor: expectedPtr};
299 return _mlir_ciface_verifyMemRefF32(actual: &actualDesc, expected: &expectedDesc);
300}
301
302extern "C" int64_t verifyMemRefF64(int64_t rank, void *actualPtr,
303 void *expectedPtr) {
304 UnrankedMemRefType<double> actualDesc = {.rank: rank, .descriptor: actualPtr};
305 UnrankedMemRefType<double> expectedDesc = {.rank: rank, .descriptor: expectedPtr};
306 return _mlir_ciface_verifyMemRefF64(actual: &actualDesc, expected: &expectedDesc);
307}
308
309extern "C" int64_t verifyMemRefInd(int64_t rank, void *actualPtr,
310 void *expectedPtr) {
311 UnrankedMemRefType<impl::index_type> actualDesc = {.rank: rank, .descriptor: actualPtr};
312 UnrankedMemRefType<impl::index_type> expectedDesc = {.rank: rank, .descriptor: expectedPtr};
313 return _mlir_ciface_verifyMemRefInd(actual: &actualDesc, expected: &expectedDesc);
314}
315
316extern "C" int64_t verifyMemRefC32(int64_t rank, void *actualPtr,
317 void *expectedPtr) {
318 UnrankedMemRefType<impl::complex32> actualDesc = {.rank: rank, .descriptor: actualPtr};
319 UnrankedMemRefType<impl::complex32> expectedDesc = {.rank: rank, .descriptor: expectedPtr};
320 return _mlir_ciface_verifyMemRefC32(actual: &actualDesc, expected: &expectedDesc);
321}
322
323extern "C" int64_t verifyMemRefC64(int64_t rank, void *actualPtr,
324 void *expectedPtr) {
325 UnrankedMemRefType<impl::complex64> actualDesc = {.rank: rank, .descriptor: actualPtr};
326 UnrankedMemRefType<impl::complex64> expectedDesc = {.rank: rank, .descriptor: expectedPtr};
327 return _mlir_ciface_verifyMemRefC64(actual: &actualDesc, expected: &expectedDesc);
328}
329
330// NOLINTEND(*-identifier-naming)
331

source code of mlir/lib/ExecutionEngine/RunnerUtils.cpp