1 //===- CRunnerUtils.cpp - Utils for 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 implements basic functions to manipulate structured MLIR types at
10 // runtime. Entities in this file are meant to be retargetable, including on
11 // targets without a C++ runtime, and must be kept C compatible.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "mlir/ExecutionEngine/CRunnerUtils.h"
16 
17 #ifndef _WIN32
18 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
19 #include <cstdlib>
20 #else
21 #include <alloca.h>
22 #endif
23 #include <sys/time.h>
24 #else
25 #include "malloc.h"
26 #endif // _WIN32
27 
28 #include <cinttypes>
29 #include <cstdio>
30 #include <string.h>
31 
32 #ifdef MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
33 
34 // Small runtime support "lib" for vector.print lowering.
35 // By providing elementary printing methods only, this
36 // library can remain fully unaware of low-level implementation
37 // details of our vectors. Also useful for direct LLVM IR output.
38 extern "C" void printI64(int64_t i) { fprintf(stdout, "%" PRId64, i); }
39 extern "C" void printU64(uint64_t u) { fprintf(stdout, "%" PRIu64, u); }
40 extern "C" void printF32(float f) { fprintf(stdout, "%g", f); }
41 extern "C" void printF64(double d) { fprintf(stdout, "%lg", d); }
42 extern "C" void printOpen() { fputs("( ", stdout); }
43 extern "C" void printClose() { fputs(" )", stdout); }
44 extern "C" void printComma() { fputs(", ", stdout); }
45 extern "C" void printNewline() { fputc('\n', stdout); }
46 
47 extern "C" MLIR_CRUNNERUTILS_EXPORT void
48 memrefCopy(int64_t elemSize, UnrankedMemRefType<char> *srcArg,
49            UnrankedMemRefType<char> *dstArg) {
50   DynamicMemRefType<char> src(*srcArg);
51   DynamicMemRefType<char> dst(*dstArg);
52 
53   int64_t rank = src.rank;
54   // Handle empty shapes -> nothing to copy.
55   for (int rankp = 0; rankp < rank; ++rankp)
56     if (src.sizes[rankp] == 0)
57       return;
58 
59   char *srcPtr = src.data + src.offset * elemSize;
60   char *dstPtr = dst.data + dst.offset * elemSize;
61 
62   if (rank == 0) {
63     memcpy(dstPtr, srcPtr, elemSize);
64     return;
65   }
66 
67   int64_t *indices = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
68   int64_t *srcStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
69   int64_t *dstStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
70 
71   // Initialize index and scale strides.
72   for (int rankp = 0; rankp < rank; ++rankp) {
73     indices[rankp] = 0;
74     srcStrides[rankp] = src.strides[rankp] * elemSize;
75     dstStrides[rankp] = dst.strides[rankp] * elemSize;
76   }
77 
78   int64_t readIndex = 0, writeIndex = 0;
79   for (;;) {
80     // Copy over the element, byte by byte.
81     memcpy(dstPtr + writeIndex, srcPtr + readIndex, elemSize);
82     // Advance index and read position.
83     for (int64_t axis = rank - 1; axis >= 0; --axis) {
84       // Advance at current axis.
85       auto newIndex = ++indices[axis];
86       readIndex += srcStrides[axis];
87       writeIndex += dstStrides[axis];
88       // If this is a valid index, we have our next index, so continue copying.
89       if (src.sizes[axis] != newIndex)
90         break;
91       // We reached the end of this axis. If this is axis 0, we are done.
92       if (axis == 0)
93         return;
94       // Else, reset to 0 and undo the advancement of the linear index that
95       // this axis had. Then continue with the axis one outer.
96       indices[axis] = 0;
97       readIndex -= src.sizes[axis] * srcStrides[axis];
98       writeIndex -= dst.sizes[axis] * dstStrides[axis];
99     }
100   }
101 }
102 
103 /// Prints GFLOPS rating.
104 extern "C" void print_flops(double flops) {
105   fprintf(stderr, "%lf GFLOPS\n", flops / 1.0E9);
106 }
107 
108 /// Returns the number of seconds since Epoch 1970-01-01 00:00:00 +0000 (UTC).
109 extern "C" double rtclock() {
110 #ifndef _WIN32
111   struct timeval tp;
112   int stat = gettimeofday(&tp, NULL);
113   if (stat != 0)
114     fprintf(stderr, "Error returning time from gettimeofday: %d\n", stat);
115   return (tp.tv_sec + tp.tv_usec * 1.0e-6);
116 #else
117   fprintf(stderr, "Timing utility not implemented on Windows\n");
118   return 0.0;
119 #endif // _WIN32
120 }
121 
122 #endif // MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
123