MLIR 24.0.0git
CRunnerUtils.cpp
Go to the documentation of this file.
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
17
18#ifndef _WIN32
19#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
20 defined(__DragonFly__)
21#include <cstdlib>
22#else
23#include <alloca.h>
24#endif
25#include <sys/time.h>
26#else
27#include "malloc.h"
28#endif // _WIN32
29
30#include <algorithm>
31#include <cinttypes>
32#include <cmath>
33#include <cstdio>
34#include <cstdlib>
35#include <numeric>
36#include <random>
37#include <string.h>
38
39#ifdef MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
40
41namespace {
42template <typename V>
43void stdSort(uint64_t n, V *p) {
44 std::sort(p, p + n);
45}
46
47} // namespace
48
49// Small runtime support "lib" for vector.print lowering.
50// By providing elementary printing methods only, this
51// library can remain fully unaware of low-level implementation
52// details of our vectors. Also useful for direct LLVM IR output.
53extern "C" void printI64(int64_t i) { fprintf(stdout, "%" PRId64, i); }
54extern "C" void printU64(uint64_t u) { fprintf(stdout, "%" PRIu64, u); }
55extern "C" void printF32(float f) {
56 if (std::isnan(f) && std::signbit(f)) {
57 fprintf(stdout, "-nan");
58 } else {
59 fprintf(stdout, "%g", f);
60 }
61}
62extern "C" void printF64(double d) {
63 if (std::isnan(d) && std::signbit(d)) {
64 fprintf(stdout, "-nan");
65 } else {
66 fprintf(stdout, "%lg", d);
67 }
68}
69extern "C" void printString(char const *s) { fputs(s, stdout); }
70extern "C" void printOpen() { fputs("( ", stdout); }
71extern "C" void printClose() { fputs(" )", stdout); }
72extern "C" void printComma() { fputs(", ", stdout); }
73extern "C" void printNewline() { fputc('\n', stdout); }
74
75extern "C" void memrefCopy(int64_t elemSize, UnrankedMemRefType<char> *srcArg,
77 DynamicMemRefType<char> src(*srcArg);
78 DynamicMemRefType<char> dst(*dstArg);
79
80 int64_t rank = src.rank;
81 MLIR_MSAN_MEMORY_IS_INITIALIZED(src.sizes, rank * sizeof(int64_t));
82
83 // Handle empty shapes -> nothing to copy.
84 for (int rankp = 0; rankp < rank; ++rankp)
85 if (src.sizes[rankp] == 0)
86 return;
87
88 char *srcPtr = src.data + src.offset * elemSize;
89 char *dstPtr = dst.data + dst.offset * elemSize;
90
91 if (rank == 0) {
92 memcpy(dstPtr, srcPtr, elemSize);
93 return;
94 }
95
96 int64_t *indices = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
97 int64_t *srcStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
98 int64_t *dstStrides = static_cast<int64_t *>(alloca(sizeof(int64_t) * rank));
99
100 // Initialize index and scale strides.
101 for (int rankp = 0; rankp < rank; ++rankp) {
102 indices[rankp] = 0;
103 srcStrides[rankp] = src.strides[rankp] * elemSize;
104 dstStrides[rankp] = dst.strides[rankp] * elemSize;
105 }
106
107 int64_t readIndex = 0, writeIndex = 0;
108 for (;;) {
109 // Copy over the element, byte by byte.
110 memcpy(dstPtr + writeIndex, srcPtr + readIndex, elemSize);
111 // Advance index and read position.
112 for (int64_t axis = rank - 1; axis >= 0; --axis) {
113 // Advance at current axis.
114 auto newIndex = ++indices[axis];
115 readIndex += srcStrides[axis];
116 writeIndex += dstStrides[axis];
117 // If this is a valid index, we have our next index, so continue copying.
118 if (src.sizes[axis] != newIndex)
119 break;
120 // We reached the end of this axis. If this is axis 0, we are done.
121 if (axis == 0)
122 return;
123 // Else, reset to 0 and undo the advancement of the linear index that
124 // this axis had. Then continue with the axis one outer.
125 indices[axis] = 0;
126 readIndex -= src.sizes[axis] * srcStrides[axis];
127 writeIndex -= dst.sizes[axis] * dstStrides[axis];
128 }
129 }
130}
131
132/// Prints GFLOPS rating.
133extern "C" void printFlops(double flops) {
134 fprintf(stderr, "%lf GFLOPS\n", flops / 1.0E9);
135}
136
137/// Returns the number of seconds since Epoch 1970-01-01 00:00:00 +0000 (UTC).
138extern "C" double rtclock() {
139#ifndef _WIN32
140 struct timeval tp;
141 int stat = gettimeofday(&tp, nullptr);
142 if (stat != 0)
143 fprintf(stderr, "Error returning time from gettimeofday: %d\n", stat);
144 return (tp.tv_sec + tp.tv_usec * 1.0e-6);
145#else
146 fprintf(stderr, "Timing utility not implemented on Windows\n");
147 return 0.0;
148#endif // _WIN32
149}
150
151extern "C" void *mlirAlloc(uint64_t size) { return malloc(size); }
152
153extern "C" void *mlirAlignedAlloc(uint64_t alignment, uint64_t size) {
154#ifdef _WIN32
155 return _aligned_malloc(size, alignment);
156#elif defined(__APPLE__)
157 // aligned_alloc was added in MacOS 10.15. Fall back to posix_memalign to also
158 // support older versions.
159 void *result = nullptr;
160 (void)::posix_memalign(&result, alignment, size);
161 return result;
162#else
163 return aligned_alloc(alignment, size);
164#endif
165}
166
167extern "C" void mlirFree(void *ptr) { free(ptr); }
168
169extern "C" void mlirAlignedFree(void *ptr) {
170#ifdef _WIN32
171 _aligned_free(ptr);
172#else
173 free(ptr);
174#endif
175}
176
177extern "C" void *rtsrand(uint64_t s) {
178 // Standard mersenne_twister_engine seeded with s.
179 return new std::mt19937(s);
180}
181
182extern "C" uint64_t rtrand(void *g, uint64_t m) {
183 std::mt19937 *generator = static_cast<std::mt19937 *>(g);
184 std::uniform_int_distribution<uint64_t> distrib(0, m);
185 return distrib(*generator);
186}
187
188extern "C" void rtdrand(void *g) {
189 std::mt19937 *generator = static_cast<std::mt19937 *>(g);
190 delete generator;
191}
192
194 void *g) {
195 assert(mref);
196 assert(mref->strides[0] == 1); // consecutive
197 std::mt19937 *generator = static_cast<std::mt19937 *>(g);
198 uint64_t s = mref->sizes[0];
199 uint64_t *data = mref->data + mref->offset;
200 std::iota(data, data + s, 0);
201 std::shuffle(data, data + s, *generator);
202}
203
204#define IMPL_STDSORT(VNAME, V) \
205 extern "C" void _mlir_ciface_stdSort##VNAME(uint64_t n, \
206 StridedMemRefType<V, 1> *vref) { \
207 assert(vref); \
208 assert(vref->strides[0] == 1); \
209 V *values = vref->data + vref->offset; \
210 stdSort(n, values); \
211 }
212IMPL_STDSORT(I64, int64_t)
213IMPL_STDSORT(F64, double)
214IMPL_STDSORT(F32, float)
215#undef IMPL_STDSORT
216
217#endif // MLIR_CRUNNERUTILS_DEFINE_FUNCTIONS
MLIR_CRUNNERUTILS_EXPORT void printI64(int64_t i)
MLIR_CRUNNERUTILS_EXPORT void printF32(float f)
MLIR_CRUNNERUTILS_EXPORT void printString(char const *s)
MLIR_CRUNNERUTILS_EXPORT void printU64(uint64_t u)
MLIR_CRUNNERUTILS_EXPORT void memrefCopy(int64_t elemSize, ::UnrankedMemRefType< char > *src, ::UnrankedMemRefType< char > *dst)
MLIR_CRUNNERUTILS_EXPORT void printNewline()
MLIR_CRUNNERUTILS_EXPORT void printOpen()
MLIR_CRUNNERUTILS_EXPORT void printFlops(double flops)
MLIR_CRUNNERUTILS_EXPORT void * rtsrand(uint64_t s)
MLIR_CRUNNERUTILS_EXPORT void rtdrand(void *g)
MLIR_CRUNNERUTILS_EXPORT double rtclock()
MLIR_CRUNNERUTILS_EXPORT void printClose()
MLIR_CRUNNERUTILS_EXPORT uint64_t rtrand(void *g, uint64_t m)
MLIR_CRUNNERUTILS_EXPORT void printF64(double d)
MLIR_CRUNNERUTILS_EXPORT void _mlir_ciface_shuffle(StridedMemRefType< uint64_t, 1 > *mref, void *g)
MLIR_CRUNNERUTILS_EXPORT void printComma()
static const mlir::GenInfo * generator
#define MLIR_MSAN_MEMORY_IS_INITIALIZED(p, s)
Definition Msan.h:32
StridedMemRef descriptor type with static rank.