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