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__)
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.
printI64(int64_t i)38 extern "C" void printI64(int64_t i) { fprintf(stdout, "%" PRId64, i); }
printU64(uint64_t u)39 extern "C" void printU64(uint64_t u) { fprintf(stdout, "%" PRIu64, u); }
printF32(float f)40 extern "C" void printF32(float f) { fprintf(stdout, "%g", f); }
printF64(double d)41 extern "C" void printF64(double d) { fprintf(stdout, "%lg", d); }
printOpen()42 extern "C" void printOpen() { fputs("( ", stdout); }
printClose()43 extern "C" void printClose() { fputs(" )", stdout); }
printComma()44 extern "C" void printComma() { fputs(", ", stdout); }
printNewline()45 extern "C" void printNewline() { fputc('\n', stdout); }
46
47 extern "C" MLIR_CRUNNERUTILS_EXPORT void
memrefCopy(int64_t elemSize,UnrankedMemRefType<char> * srcArg,UnrankedMemRefType<char> * dstArg)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 char *srcPtr = src.data + src.offset * elemSize;
55 char *dstPtr = dst.data + dst.offset * elemSize;
56
57 if (rank == 0) {
58 memcpy(dstPtr, srcPtr, elemSize);
59 return;
60 }
61
62 int64_t *indices = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
63 int64_t *srcStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
64 int64_t *dstStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
65
66 // Initialize index and scale strides.
67 for (int rankp = 0; rankp < rank; ++rankp) {
68 indices[rankp] = 0;
69 srcStrides[rankp] = src.strides[rankp] * elemSize;
70 dstStrides[rankp] = dst.strides[rankp] * elemSize;
71 }
72
73 int64_t readIndex = 0, writeIndex = 0;
74 for (;;) {
75 // Copy over the element, byte by byte.
76 memcpy(dstPtr + writeIndex, srcPtr + readIndex, elemSize);
77 // Advance index and read position.
78 for (int64_t axis = rank - 1; axis >= 0; --axis) {
79 // Advance at current axis.
80 auto newIndex = ++indices[axis];
81 readIndex += srcStrides[axis];
82 writeIndex += dstStrides[axis];
83 // If this is a valid index, we have our next index, so continue copying.
84 if (src.sizes[axis] != newIndex)
85 break;
86 // We reached the end of this axis. If this is axis 0, we are done.
87 if (axis == 0)
88 return;
89 // Else, reset to 0 and undo the advancement of the linear index that
90 // this axis had. The continue with the axis one outer.
91 indices[axis] = 0;
92 readIndex -= src.sizes[axis] * srcStrides[axis];
93 writeIndex -= dst.sizes[axis] * dstStrides[axis];
94 }
95 }
96 }
97
98 /// Prints GFLOPS rating.
print_flops(double flops)99 extern "C" void print_flops(double flops) {
100 fprintf(stderr, "%lf GFLOPS\n", flops / 1.0E9);
101 }
102
103 /// Returns the number of seconds since Epoch 1970-01-01 00:00:00 +0000 (UTC).
rtclock()104 extern "C" double rtclock() {
105 #ifndef _WIN32
106 struct timeval tp;
107 int stat = gettimeofday(&tp, NULL);
108 if (stat != 0)
109 fprintf(stderr, "Error returning time from gettimeofday: %d\n", stat);
110 return (tp.tv_sec + tp.tv_usec * 1.0e-6);
111 #else
112 fprintf(stderr, "Timing utility not implemented on Windows\n");
113 return 0.0;
114 #endif // _WIN32
115 }
116
117 #endif // MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
118