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 <sys/time.h> 19 #endif // _WIN32 20 21 #include <cinttypes> 22 #include <cstdio> 23 24 #ifdef MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS 25 26 // Small runtime support "lib" for vector.print lowering. 27 // By providing elementary printing methods only, this 28 // library can remain fully unaware of low-level implementation 29 // details of our vectors. Also useful for direct LLVM IR output. 30 extern "C" void printI64(int64_t i) { fprintf(stdout, "%" PRId64, i); } 31 extern "C" void printU64(uint64_t u) { fprintf(stdout, "%" PRIu64, u); } 32 extern "C" void printF32(float f) { fprintf(stdout, "%g", f); } 33 extern "C" void printF64(double d) { fprintf(stdout, "%lg", d); } 34 extern "C" void printOpen() { fputs("( ", stdout); } 35 extern "C" void printClose() { fputs(" )", stdout); } 36 extern "C" void printComma() { fputs(", ", stdout); } 37 extern "C" void printNewline() { fputc('\n', stdout); } 38 39 /// Prints GFLOPS rating. 40 extern "C" void print_flops(double flops) { 41 fprintf(stderr, "%lf GFLOPS\n", flops / 1.0E9); 42 } 43 44 /// Returns the number of seconds since Epoch 1970-01-01 00:00:00 +0000 (UTC). 45 extern "C" double rtclock() { 46 #ifndef _WIN32 47 struct timeval tp; 48 int stat = gettimeofday(&tp, NULL); 49 if (stat != 0) 50 fprintf(stderr, "Error returning time from gettimeofday: %d\n", stat); 51 return (tp.tv_sec + tp.tv_usec * 1.0e-6); 52 #else 53 fprintf(stderr, "Timing utility not implemented on Windows\n"); 54 return 0.0; 55 #endif // _WIN32 56 } 57 58 #endif // MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS 59