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 
18 #ifndef _WIN32
19 #include <sys/time.h>
20 #endif // _WIN32
21 
22 extern "C" void _mlir_ciface_print_memref_vector_4x4xf32(
23     StridedMemRefType<Vector2D<4, 4, float>, 2> *M) {
24   impl::printMemRef(*M);
25 }
26 
27 #define MEMREF_CASE(TYPE, RANK)                                                \
28   case RANK:                                                                   \
29     impl::printMemRef(*(static_cast<StridedMemRefType<TYPE, RANK> *>(ptr)));   \
30     break
31 
32 extern "C" void _mlir_ciface_print_memref_i8(UnrankedMemRefType<int8_t> *M) {
33   printUnrankedMemRefMetaData(std::cout, *M);
34   int64_t rank = M->rank;
35   void *ptr = M->descriptor;
36 
37   switch (rank) {
38     MEMREF_CASE(int8_t, 0);
39     MEMREF_CASE(int8_t, 1);
40     MEMREF_CASE(int8_t, 2);
41     MEMREF_CASE(int8_t, 3);
42     MEMREF_CASE(int8_t, 4);
43   default:
44     assert(0 && "Unsupported rank to print");
45   }
46 }
47 
48 extern "C" void _mlir_ciface_print_memref_i32(UnrankedMemRefType<int32_t> *M) {
49   printUnrankedMemRefMetaData(std::cout, *M);
50   int64_t rank = M->rank;
51   void *ptr = M->descriptor;
52 
53   switch (rank) {
54     MEMREF_CASE(int32_t, 0);
55     MEMREF_CASE(int32_t, 1);
56     MEMREF_CASE(int32_t, 2);
57     MEMREF_CASE(int32_t, 3);
58     MEMREF_CASE(int32_t, 4);
59   default:
60     assert(0 && "Unsupported rank to print");
61   }
62 }
63 
64 extern "C" void _mlir_ciface_print_memref_f32(UnrankedMemRefType<float> *M) {
65   printUnrankedMemRefMetaData(std::cout, *M);
66   int64_t rank = M->rank;
67   void *ptr = M->descriptor;
68 
69   switch (rank) {
70     MEMREF_CASE(float, 0);
71     MEMREF_CASE(float, 1);
72     MEMREF_CASE(float, 2);
73     MEMREF_CASE(float, 3);
74     MEMREF_CASE(float, 4);
75   default:
76     assert(0 && "Unsupported rank to print");
77   }
78 }
79 
80 extern "C" void print_memref_i32(int64_t rank, void *ptr) {
81   UnrankedMemRefType<int32_t> descriptor = {rank, ptr};
82   _mlir_ciface_print_memref_i32(&descriptor);
83 }
84 
85 extern "C" void print_memref_f32(int64_t rank, void *ptr) {
86   UnrankedMemRefType<float> descriptor = {rank, ptr};
87   _mlir_ciface_print_memref_f32(&descriptor);
88 }
89 
90 extern "C" void
91 _mlir_ciface_print_memref_0d_f32(StridedMemRefType<float, 0> *M) {
92   impl::printMemRef(*M);
93 }
94 extern "C" void
95 _mlir_ciface_print_memref_1d_f32(StridedMemRefType<float, 1> *M) {
96   impl::printMemRef(*M);
97 }
98 extern "C" void
99 _mlir_ciface_print_memref_2d_f32(StridedMemRefType<float, 2> *M) {
100   impl::printMemRef(*M);
101 }
102 extern "C" void
103 _mlir_ciface_print_memref_3d_f32(StridedMemRefType<float, 3> *M) {
104   impl::printMemRef(*M);
105 }
106 extern "C" void
107 _mlir_ciface_print_memref_4d_f32(StridedMemRefType<float, 4> *M) {
108   impl::printMemRef(*M);
109 }
110 
111 /// Prints GFLOPS rating.
112 extern "C" void print_flops(double flops) {
113   fprintf(stderr, "%lf GFLOPS\n", flops / 1.0E9);
114 }
115 
116 /// Returns the number of seconds since Epoch 1970-01-01 00:00:00 +0000 (UTC).
117 extern "C" double rtclock() {
118 #ifndef _WIN32
119   struct timeval tp;
120   int stat = gettimeofday(&tp, NULL);
121   if (stat != 0)
122     fprintf(stderr, "Error returning time from gettimeofday: %d\n", stat);
123   return (tp.tv_sec + tp.tv_usec * 1.0e-6);
124 #else
125   fprintf(stderr, "Timing utility not implemented on Windows\n");
126   return 0.0;
127 #endif // _WIN32
128 }
129