xref: /llvm-project/mlir/lib/ExecutionEngine/CRunnerUtils.cpp (revision cb898e26f3321e0b861609f5fec7f7410e5b6778)
14a966e5dSNicolas Vasilache //===- CRunnerUtils.cpp - Utils for MLIR execution ------------------------===//
24a966e5dSNicolas Vasilache //
34a966e5dSNicolas Vasilache // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44a966e5dSNicolas Vasilache // See https://llvm.org/LICENSE.txt for license information.
54a966e5dSNicolas Vasilache // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64a966e5dSNicolas Vasilache //
74a966e5dSNicolas Vasilache //===----------------------------------------------------------------------===//
84a966e5dSNicolas Vasilache //
94a966e5dSNicolas Vasilache // This file implements basic functions to manipulate structured MLIR types at
104a966e5dSNicolas Vasilache // runtime. Entities in this file are meant to be retargetable, including on
114a966e5dSNicolas Vasilache // targets without a C++ runtime, and must be kept C compatible.
124a966e5dSNicolas Vasilache //
134a966e5dSNicolas Vasilache //===----------------------------------------------------------------------===//
144a966e5dSNicolas Vasilache 
15c3108404SMason Remy #include "mlir/ExecutionEngine/CRunnerUtils.h"
16b35b9e30SEugene Zhulenev #include "mlir/ExecutionEngine/Msan.h"
17c3108404SMason Remy 
189cbef8c9SNicolas Vasilache #ifndef _WIN32
19a157a82bSBrad Smith #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
20a157a82bSBrad Smith     defined(__DragonFly__)
21ab4b4684SDimitry Andric #include <cstdlib>
22ab4b4684SDimitry Andric #else
23e6450d88SStephan Herhut #include <alloca.h>
24ab4b4684SDimitry Andric #endif
259cbef8c9SNicolas Vasilache #include <sys/time.h>
26e6450d88SStephan Herhut #else
27e6450d88SStephan Herhut #include "malloc.h"
289cbef8c9SNicolas Vasilache #endif // _WIN32
299cbef8c9SNicolas Vasilache 
309889753cSbixia1 #include <algorithm>
314a966e5dSNicolas Vasilache #include <cinttypes>
324a966e5dSNicolas Vasilache #include <cstdio>
339c1d133cSAlex Zinenko #include <cstdlib>
34412d7841SYinying Li #include <numeric>
353032c07dSbixia1 #include <random>
3688d5eba1SStephan Herhut #include <string.h>
374a966e5dSNicolas Vasilache 
3866778159SAlexandre Ganea #ifdef MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
3966778159SAlexandre Ganea 
409889753cSbixia1 namespace {
419889753cSbixia1 template <typename V>
stdSort(uint64_t n,V * p)429889753cSbixia1 void stdSort(uint64_t n, V *p) {
439889753cSbixia1   std::sort(p, p + n);
449889753cSbixia1 }
459889753cSbixia1 
469889753cSbixia1 } // namespace
479889753cSbixia1 
484a966e5dSNicolas Vasilache // Small runtime support "lib" for vector.print lowering.
494a966e5dSNicolas Vasilache // By providing elementary printing methods only, this
504a966e5dSNicolas Vasilache // library can remain fully unaware of low-level implementation
514a966e5dSNicolas Vasilache // details of our vectors. Also useful for direct LLVM IR output.
printI64(int64_t i)5254759cefSAart Bik extern "C" void printI64(int64_t i) { fprintf(stdout, "%" PRId64, i); }
printU64(uint64_t u)5354759cefSAart Bik extern "C" void printU64(uint64_t u) { fprintf(stdout, "%" PRIu64, u); }
printF32(float f)54*cb898e26SKai Sasaki extern "C" void printF32(float f) {
55*cb898e26SKai Sasaki   if (std::isnan(f) && std::signbit(f)) {
56*cb898e26SKai Sasaki     fprintf(stdout, "-nan");
57*cb898e26SKai Sasaki   } else {
58*cb898e26SKai Sasaki     fprintf(stdout, "%g", f);
59*cb898e26SKai Sasaki   }
60*cb898e26SKai Sasaki }
printF64(double d)61*cb898e26SKai Sasaki extern "C" void printF64(double d) {
62*cb898e26SKai Sasaki   if (std::isnan(d) && std::signbit(d)) {
63*cb898e26SKai Sasaki     fprintf(stdout, "-nan");
64*cb898e26SKai Sasaki   } else {
65*cb898e26SKai Sasaki     fprintf(stdout, "%lg", d);
66*cb898e26SKai Sasaki   }
67*cb898e26SKai Sasaki }
printString(char const * s)683be3883eSBenjamin Maxwell extern "C" void printString(char const *s) { fputs(s, stdout); }
printOpen()6954759cefSAart Bik extern "C" void printOpen() { fputs("( ", stdout); }
printClose()7054759cefSAart Bik extern "C" void printClose() { fputs(" )", stdout); }
printComma()7154759cefSAart Bik extern "C" void printComma() { fputs(", ", stdout); }
printNewline()7254759cefSAart Bik extern "C" void printNewline() { fputc('\n', stdout); }
7366778159SAlexandre Ganea 
memrefCopy(int64_t elemSize,UnrankedMemRefType<char> * srcArg,UnrankedMemRefType<char> * dstArg)74d4555698SStella Stamenova extern "C" void memrefCopy(int64_t elemSize, UnrankedMemRefType<char> *srcArg,
7588d5eba1SStephan Herhut                            UnrankedMemRefType<char> *dstArg) {
7688d5eba1SStephan Herhut   DynamicMemRefType<char> src(*srcArg);
7788d5eba1SStephan Herhut   DynamicMemRefType<char> dst(*dstArg);
7888d5eba1SStephan Herhut 
7988d5eba1SStephan Herhut   int64_t rank = src.rank;
80b35b9e30SEugene Zhulenev   MLIR_MSAN_MEMORY_IS_INITIALIZED(src.sizes, rank * sizeof(int64_t));
81b35b9e30SEugene Zhulenev 
8276fd3d44SAdrian Kuegel   // Handle empty shapes -> nothing to copy.
8376fd3d44SAdrian Kuegel   for (int rankp = 0; rankp < rank; ++rankp)
8476fd3d44SAdrian Kuegel     if (src.sizes[rankp] == 0)
8576fd3d44SAdrian Kuegel       return;
8676fd3d44SAdrian Kuegel 
8704bddb6cSAart Bik   char *srcPtr = src.data + src.offset * elemSize;
8804bddb6cSAart Bik   char *dstPtr = dst.data + dst.offset * elemSize;
8904bddb6cSAart Bik 
9004bddb6cSAart Bik   if (rank == 0) {
9104bddb6cSAart Bik     memcpy(dstPtr, srcPtr, elemSize);
9204bddb6cSAart Bik     return;
9304bddb6cSAart Bik   }
9404bddb6cSAart Bik 
9588d5eba1SStephan Herhut   int64_t *indices = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
9688d5eba1SStephan Herhut   int64_t *srcStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
9788d5eba1SStephan Herhut   int64_t *dstStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
9888d5eba1SStephan Herhut 
9988d5eba1SStephan Herhut   // Initialize index and scale strides.
10088d5eba1SStephan Herhut   for (int rankp = 0; rankp < rank; ++rankp) {
10188d5eba1SStephan Herhut     indices[rankp] = 0;
10288d5eba1SStephan Herhut     srcStrides[rankp] = src.strides[rankp] * elemSize;
10388d5eba1SStephan Herhut     dstStrides[rankp] = dst.strides[rankp] * elemSize;
10488d5eba1SStephan Herhut   }
10588d5eba1SStephan Herhut 
10688d5eba1SStephan Herhut   int64_t readIndex = 0, writeIndex = 0;
10788d5eba1SStephan Herhut   for (;;) {
10888d5eba1SStephan Herhut     // Copy over the element, byte by byte.
10988d5eba1SStephan Herhut     memcpy(dstPtr + writeIndex, srcPtr + readIndex, elemSize);
11088d5eba1SStephan Herhut     // Advance index and read position.
11188d5eba1SStephan Herhut     for (int64_t axis = rank - 1; axis >= 0; --axis) {
11288d5eba1SStephan Herhut       // Advance at current axis.
11388d5eba1SStephan Herhut       auto newIndex = ++indices[axis];
11488d5eba1SStephan Herhut       readIndex += srcStrides[axis];
11588d5eba1SStephan Herhut       writeIndex += dstStrides[axis];
11688d5eba1SStephan Herhut       // If this is a valid index, we have our next index, so continue copying.
11788d5eba1SStephan Herhut       if (src.sizes[axis] != newIndex)
11888d5eba1SStephan Herhut         break;
11988d5eba1SStephan Herhut       // We reached the end of this axis. If this is axis 0, we are done.
12088d5eba1SStephan Herhut       if (axis == 0)
12188d5eba1SStephan Herhut         return;
12288d5eba1SStephan Herhut       // Else, reset to 0 and undo the advancement of the linear index that
12376fd3d44SAdrian Kuegel       // this axis had. Then continue with the axis one outer.
12488d5eba1SStephan Herhut       indices[axis] = 0;
12588d5eba1SStephan Herhut       readIndex -= src.sizes[axis] * srcStrides[axis];
12688d5eba1SStephan Herhut       writeIndex -= dst.sizes[axis] * dstStrides[axis];
12788d5eba1SStephan Herhut     }
12888d5eba1SStephan Herhut   }
12988d5eba1SStephan Herhut }
13088d5eba1SStephan Herhut 
1319cbef8c9SNicolas Vasilache /// Prints GFLOPS rating.
printFlops(double flops)132d4555698SStella Stamenova extern "C" void printFlops(double flops) {
1339cbef8c9SNicolas Vasilache   fprintf(stderr, "%lf GFLOPS\n", flops / 1.0E9);
1349cbef8c9SNicolas Vasilache }
1359cbef8c9SNicolas Vasilache 
1369cbef8c9SNicolas Vasilache /// Returns the number of seconds since Epoch 1970-01-01 00:00:00 +0000 (UTC).
rtclock()1379cbef8c9SNicolas Vasilache extern "C" double rtclock() {
1389cbef8c9SNicolas Vasilache #ifndef _WIN32
1399cbef8c9SNicolas Vasilache   struct timeval tp;
140e5639b3fSMehdi Amini   int stat = gettimeofday(&tp, nullptr);
1419cbef8c9SNicolas Vasilache   if (stat != 0)
1429cbef8c9SNicolas Vasilache     fprintf(stderr, "Error returning time from gettimeofday: %d\n", stat);
1439cbef8c9SNicolas Vasilache   return (tp.tv_sec + tp.tv_usec * 1.0e-6);
1449cbef8c9SNicolas Vasilache #else
1459cbef8c9SNicolas Vasilache   fprintf(stderr, "Timing utility not implemented on Windows\n");
1469cbef8c9SNicolas Vasilache   return 0.0;
1479cbef8c9SNicolas Vasilache #endif // _WIN32
1489cbef8c9SNicolas Vasilache }
1499cbef8c9SNicolas Vasilache 
mlirAlloc(uint64_t size)150c599650aSMehdi Amini extern "C" void *mlirAlloc(uint64_t size) { return malloc(size); }
1519c1d133cSAlex Zinenko 
mlirAlignedAlloc(uint64_t alignment,uint64_t size)152c599650aSMehdi Amini extern "C" void *mlirAlignedAlloc(uint64_t alignment, uint64_t size) {
1539c1d133cSAlex Zinenko #ifdef _WIN32
1549c1d133cSAlex Zinenko   return _aligned_malloc(size, alignment);
15548a1a993SBenjamin Kramer #elif defined(__APPLE__)
15648a1a993SBenjamin Kramer   // aligned_alloc was added in MacOS 10.15. Fall back to posix_memalign to also
15748a1a993SBenjamin Kramer   // support older versions.
15848a1a993SBenjamin Kramer   void *result = nullptr;
15948a1a993SBenjamin Kramer   (void)::posix_memalign(&result, alignment, size);
16048a1a993SBenjamin Kramer   return result;
1619c1d133cSAlex Zinenko #else
1629c1d133cSAlex Zinenko   return aligned_alloc(alignment, size);
1639c1d133cSAlex Zinenko #endif
1649c1d133cSAlex Zinenko }
1659c1d133cSAlex Zinenko 
mlirFree(void * ptr)166c599650aSMehdi Amini extern "C" void mlirFree(void *ptr) { free(ptr); }
1679c1d133cSAlex Zinenko 
mlirAlignedFree(void * ptr)168c599650aSMehdi Amini extern "C" void mlirAlignedFree(void *ptr) {
1699c1d133cSAlex Zinenko #ifdef _WIN32
1709c1d133cSAlex Zinenko   _aligned_free(ptr);
1719c1d133cSAlex Zinenko #else
1729c1d133cSAlex Zinenko   free(ptr);
1739c1d133cSAlex Zinenko #endif
1749c1d133cSAlex Zinenko }
1759c1d133cSAlex Zinenko 
rtsrand(uint64_t s)1763032c07dSbixia1 extern "C" void *rtsrand(uint64_t s) {
1773032c07dSbixia1   // Standard mersenne_twister_engine seeded with s.
1783032c07dSbixia1   return new std::mt19937(s);
1793032c07dSbixia1 }
1803032c07dSbixia1 
rtrand(void * g,uint64_t m)1813032c07dSbixia1 extern "C" uint64_t rtrand(void *g, uint64_t m) {
1823032c07dSbixia1   std::mt19937 *generator = static_cast<std::mt19937 *>(g);
1833032c07dSbixia1   std::uniform_int_distribution<uint64_t> distrib(0, m);
1843032c07dSbixia1   return distrib(*generator);
1853032c07dSbixia1 }
1863032c07dSbixia1 
rtdrand(void * g)1873032c07dSbixia1 extern "C" void rtdrand(void *g) {
1883032c07dSbixia1   std::mt19937 *generator = static_cast<std::mt19937 *>(g);
1893032c07dSbixia1   delete generator;
1903032c07dSbixia1 }
1913032c07dSbixia1 
_mlir_ciface_shuffle(StridedMemRefType<uint64_t,1> * mref,void * g)192412d7841SYinying Li extern "C" void _mlir_ciface_shuffle(StridedMemRefType<uint64_t, 1> *mref,
193412d7841SYinying Li                                      void *g) {
194412d7841SYinying Li   assert(mref);
195412d7841SYinying Li   assert(mref->strides[0] == 1); // consecutive
196412d7841SYinying Li   std::mt19937 *generator = static_cast<std::mt19937 *>(g);
197412d7841SYinying Li   uint64_t s = mref->sizes[0];
198412d7841SYinying Li   uint64_t *data = mref->data + mref->offset;
199412d7841SYinying Li   std::iota(data, data + s, 0);
200412d7841SYinying Li   std::shuffle(data, data + s, *generator);
201412d7841SYinying Li }
202412d7841SYinying Li 
2039889753cSbixia1 #define IMPL_STDSORT(VNAME, V)                                                 \
2049889753cSbixia1   extern "C" void _mlir_ciface_stdSort##VNAME(uint64_t n,                      \
2059889753cSbixia1                                               StridedMemRefType<V, 1> *vref) { \
2069889753cSbixia1     assert(vref);                                                              \
2079889753cSbixia1     assert(vref->strides[0] == 1);                                             \
2089889753cSbixia1     V *values = vref->data + vref->offset;                                     \
2099889753cSbixia1     stdSort(n, values);                                                        \
2109889753cSbixia1   }
2119889753cSbixia1 IMPL_STDSORT(I64, int64_t)
2129889753cSbixia1 IMPL_STDSORT(F64, double)
2139889753cSbixia1 IMPL_STDSORT(F32, float)
2149889753cSbixia1 #undef IMPL_STDSORT
2159889753cSbixia1 
21608466596SAart Bik #endif // MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
217