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