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