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