1 //===- RunnerUtils.cpp - Utils for MLIR CPU 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 // Utilities for interfacing MLIR types with C code as well as printing,
10 // debugging etc.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/ExecutionEngine/RunnerUtils.h"
15 
16 #include <cinttypes>
17 #include <cstdio>
18 
19 extern "C" void _mlir_ciface_print_memref_vector_4x4xf32(
20     StridedMemRefType<Vector2D<4, 4, float>, 2> *M) {
21   impl::printMemRef(*M);
22 }
23 
24 #define MEMREF_CASE(TYPE, RANK)                                                \
25   case RANK:                                                                   \
26     impl::printMemRef(*(static_cast<StridedMemRefType<TYPE, RANK> *>(ptr)));   \
27     break
28 
29 extern "C" void _mlir_ciface_print_memref_i8(UnrankedMemRefType<int8_t> *M) {
30   printUnrankedMemRefMetaData(std::cout, *M);
31   int rank = M->rank;
32   void *ptr = M->descriptor;
33 
34   switch (rank) {
35     MEMREF_CASE(int8_t, 0);
36     MEMREF_CASE(int8_t, 1);
37     MEMREF_CASE(int8_t, 2);
38     MEMREF_CASE(int8_t, 3);
39     MEMREF_CASE(int8_t, 4);
40   default:
41     assert(0 && "Unsupported rank to print");
42   }
43 }
44 
45 extern "C" void _mlir_ciface_print_memref_f32(UnrankedMemRefType<float> *M) {
46   printUnrankedMemRefMetaData(std::cout, *M);
47   int rank = M->rank;
48   void *ptr = M->descriptor;
49 
50   switch (rank) {
51     MEMREF_CASE(float, 0);
52     MEMREF_CASE(float, 1);
53     MEMREF_CASE(float, 2);
54     MEMREF_CASE(float, 3);
55     MEMREF_CASE(float, 4);
56   default:
57     assert(0 && "Unsupported rank to print");
58   }
59 }
60 
61 extern "C" void print_memref_f32(int64_t rank, void *ptr) {
62   UnrankedMemRefType<float> descriptor;
63   descriptor.rank = rank;
64   descriptor.descriptor = ptr;
65   _mlir_ciface_print_memref_f32(&descriptor);
66 }
67 
68 extern "C" void
69 _mlir_ciface_print_memref_0d_f32(StridedMemRefType<float, 0> *M) {
70   impl::printMemRef(*M);
71 }
72 extern "C" void
73 _mlir_ciface_print_memref_1d_f32(StridedMemRefType<float, 1> *M) {
74   impl::printMemRef(*M);
75 }
76 extern "C" void
77 _mlir_ciface_print_memref_2d_f32(StridedMemRefType<float, 2> *M) {
78   impl::printMemRef(*M);
79 }
80 extern "C" void
81 _mlir_ciface_print_memref_3d_f32(StridedMemRefType<float, 3> *M) {
82   impl::printMemRef(*M);
83 }
84 extern "C" void
85 _mlir_ciface_print_memref_4d_f32(StridedMemRefType<float, 4> *M) {
86   impl::printMemRef(*M);
87 }
88 
89 // Small runtime support "lib" for vector.print lowering.
90 // By providing elementary printing methods only, this
91 // library can remain fully unaware of low-level implementation
92 // details of our vectors. Also useful for direct LLVM IR output.
93 extern "C" void print_i32(int32_t i) { fprintf(stdout, "%" PRId32, i); }
94 extern "C" void print_i64(int64_t l) { fprintf(stdout, "%" PRId64, l); }
95 extern "C" void print_f32(float f) { fprintf(stdout, "%g", f); }
96 extern "C" void print_f64(double d) { fprintf(stdout, "%lg", d); }
97 extern "C" void print_open() { fputs("( ", stdout); }
98 extern "C" void print_close() { fputs(" )", stdout); }
99 extern "C" void print_comma() { fputs(", ", stdout); }
100 extern "C" void print_newline() { fputc('\n', stdout); }
101