Skip to content

Commit 4a966e5

Browse files
author
Nicolas Vasilache
committed
[mlir] NFC - Split out RunnerUtils that don't require a C++ runtime
Summary: This revision split out a new CRunnerUtils library that supports MLIR execution on targets without a C++ runtime. Differential Revision: https://reviews.llvm.org/D75257
1 parent 9227a74 commit 4a966e5

File tree

7 files changed

+158
-96
lines changed

7 files changed

+158
-96
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===- CRunnerUtils.h - Utils for debugging 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 declares basic classes and functions to manipulate structured MLIR
10+
// types at runtime. Entities in this file are must be retargetable, including
11+
// on targets without a C++ runtime.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef EXECUTIONENGINE_CRUNNERUTILS_H_
16+
#define EXECUTIONENGINE_CRUNNERUTILS_H_
17+
18+
#ifdef _WIN32
19+
#ifndef MLIR_CRUNNERUTILS_EXPORT
20+
#ifdef mlir_c_runner_utils_EXPORTS
21+
/* We are building this library */
22+
#define MLIR_CRUNNERUTILS_EXPORT __declspec(dllexport)
23+
#else
24+
/* We are using this library */
25+
#define MLIR_CRUNNERUTILS_EXPORT __declspec(dllimport)
26+
#endif // mlir_c_runner_utils_EXPORTS
27+
#endif // MLIR_CRUNNERUTILS_EXPORT
28+
#else
29+
#define MLIR_CRUNNERUTILS_EXPORT
30+
#endif // _WIN32
31+
32+
#include <cstdint>
33+
34+
template <int N> void dropFront(int64_t arr[N], int64_t *res) {
35+
for (unsigned i = 1; i < N; ++i)
36+
*(res + i - 1) = arr[i];
37+
}
38+
39+
/// StridedMemRef descriptor type with static rank.
40+
template <typename T, int N> struct StridedMemRefType {
41+
T *basePtr;
42+
T *data;
43+
int64_t offset;
44+
int64_t sizes[N];
45+
int64_t strides[N];
46+
// This operator[] is extremely slow and only for sugaring purposes.
47+
StridedMemRefType<T, N - 1> operator[](int64_t idx) {
48+
StridedMemRefType<T, N - 1> res;
49+
res.basePtr = basePtr;
50+
res.data = data;
51+
res.offset = offset + idx * strides[0];
52+
dropFront<N>(sizes, res.sizes);
53+
dropFront<N>(strides, res.strides);
54+
return res;
55+
}
56+
};
57+
58+
/// StridedMemRef descriptor type specialized for rank 1.
59+
template <typename T> struct StridedMemRefType<T, 1> {
60+
T *basePtr;
61+
T *data;
62+
int64_t offset;
63+
int64_t sizes[1];
64+
int64_t strides[1];
65+
T &operator[](int64_t idx) { return *(data + offset + idx * strides[0]); }
66+
};
67+
68+
/// StridedMemRef descriptor type specialized for rank 0.
69+
template <typename T> struct StridedMemRefType<T, 0> {
70+
T *basePtr;
71+
T *data;
72+
int64_t offset;
73+
};
74+
75+
// Unranked MemRef
76+
template <typename T> struct UnrankedMemRefType {
77+
int64_t rank;
78+
void *descriptor;
79+
};
80+
81+
// Small runtime support "lib" for vector.print lowering.
82+
extern "C" MLIR_CRUNNERUTILS_EXPORT void print_f32(float f);
83+
extern "C" MLIR_CRUNNERUTILS_EXPORT void print_f64(double d);
84+
extern "C" MLIR_CRUNNERUTILS_EXPORT void print_open();
85+
extern "C" MLIR_CRUNNERUTILS_EXPORT void print_close();
86+
extern "C" MLIR_CRUNNERUTILS_EXPORT void print_comma();
87+
extern "C" MLIR_CRUNNERUTILS_EXPORT void print_newline();
88+
89+
#endif // EXECUTIONENGINE_CRUNNERUTILS_H_

mlir/include/mlir/ExecutionEngine/RunnerUtils.h

+25-76
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,35 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares basic classes and functions to debug structured MLIR
10+
// types at runtime. Entities in this file may not be compatible with targets
11+
// without a C++ runtime. These may be progressively migrated to CRunnerUtils.h
12+
// over time.
13+
//
14+
//===----------------------------------------------------------------------===//
815

916
#ifndef EXECUTIONENGINE_RUNNERUTILS_H_
1017
#define EXECUTIONENGINE_RUNNERUTILS_H_
1118

12-
#include <assert.h>
13-
#include <cstdint>
14-
#include <iostream>
15-
1619
#ifdef _WIN32
17-
#ifndef MLIR_RUNNER_UTILS_EXPORT
20+
#ifndef MLIR_RUNNERUTILS_EXPORT
1821
#ifdef mlir_runner_utils_EXPORTS
1922
/* We are building this library */
20-
#define MLIR_RUNNER_UTILS_EXPORT __declspec(dllexport)
23+
#define MLIR_RUNNERUTILS_EXPORT __declspec(dllexport)
2124
#else
2225
/* We are using this library */
23-
#define MLIR_RUNNER_UTILS_EXPORT __declspec(dllimport)
26+
#define MLIR_RUNNERUTILS_EXPORT __declspec(dllimport)
2427
#endif // mlir_runner_utils_EXPORTS
25-
#endif // MLIR_RUNNER_UTILS_EXPORT
28+
#endif // MLIR_RUNNERUTILS_EXPORT
2629
#else
27-
#define MLIR_RUNNER_UTILS_EXPORT
30+
#define MLIR_RUNNERUTILS_EXPORT
2831
#endif // _WIN32
2932

30-
template <typename T, int N> struct StridedMemRefType;
31-
template <typename StreamType, typename T, int N>
32-
void printMemRefMetaData(StreamType &os, StridedMemRefType<T, N> &V);
33-
34-
template <int N> void dropFront(int64_t arr[N], int64_t *res) {
35-
for (unsigned i = 1; i < N; ++i)
36-
*(res + i - 1) = arr[i];
37-
}
38-
39-
/// StridedMemRef descriptor type with static rank.
40-
template <typename T, int N> struct StridedMemRefType {
41-
T *basePtr;
42-
T *data;
43-
int64_t offset;
44-
int64_t sizes[N];
45-
int64_t strides[N];
46-
// This operator[] is extremely slow and only for sugaring purposes.
47-
StridedMemRefType<T, N - 1> operator[](int64_t idx) {
48-
StridedMemRefType<T, N - 1> res;
49-
res.basePtr = basePtr;
50-
res.data = data;
51-
res.offset = offset + idx * strides[0];
52-
dropFront<N>(sizes, res.sizes);
53-
dropFront<N>(strides, res.strides);
54-
return res;
55-
}
56-
};
57-
58-
/// StridedMemRef descriptor type specialized for rank 1.
59-
template <typename T> struct StridedMemRefType<T, 1> {
60-
T *basePtr;
61-
T *data;
62-
int64_t offset;
63-
int64_t sizes[1];
64-
int64_t strides[1];
65-
T &operator[](int64_t idx) { return *(data + offset + idx * strides[0]); }
66-
};
67-
68-
/// StridedMemRef descriptor type specialized for rank 0.
69-
template <typename T> struct StridedMemRefType<T, 0> {
70-
T *basePtr;
71-
T *data;
72-
int64_t offset;
73-
};
33+
#include <assert.h>
34+
#include <iostream>
7435

75-
// Unranked MemRef
76-
template <typename T> struct UnrankedMemRefType {
77-
int64_t rank;
78-
void *descriptor;
79-
};
36+
#include "mlir/ExecutionEngine/CRunnerUtils.h"
8037

8138
template <typename StreamType, typename T, int N>
8239
void printMemRefMetaData(StreamType &os, StridedMemRefType<T, N> &V) {
@@ -261,35 +218,27 @@ template <typename T> void printMemRef(StridedMemRefType<T, 0> &M) {
261218
////////////////////////////////////////////////////////////////////////////////
262219
// Currently exposed C API.
263220
////////////////////////////////////////////////////////////////////////////////
264-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
221+
extern "C" MLIR_RUNNERUTILS_EXPORT void
265222
_mlir_ciface_print_memref_i8(UnrankedMemRefType<int8_t> *M);
266-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
223+
extern "C" MLIR_RUNNERUTILS_EXPORT void
267224
_mlir_ciface_print_memref_f32(UnrankedMemRefType<float> *M);
268225

269-
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_memref_f32(int64_t rank,
270-
void *ptr);
226+
extern "C" MLIR_RUNNERUTILS_EXPORT void print_memref_f32(int64_t rank,
227+
void *ptr);
271228

272-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
229+
extern "C" MLIR_RUNNERUTILS_EXPORT void
273230
_mlir_ciface_print_memref_0d_f32(StridedMemRefType<float, 0> *M);
274-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
231+
extern "C" MLIR_RUNNERUTILS_EXPORT void
275232
_mlir_ciface_print_memref_1d_f32(StridedMemRefType<float, 1> *M);
276-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
233+
extern "C" MLIR_RUNNERUTILS_EXPORT void
277234
_mlir_ciface_print_memref_2d_f32(StridedMemRefType<float, 2> *M);
278-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
235+
extern "C" MLIR_RUNNERUTILS_EXPORT void
279236
_mlir_ciface_print_memref_3d_f32(StridedMemRefType<float, 3> *M);
280-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
237+
extern "C" MLIR_RUNNERUTILS_EXPORT void
281238
_mlir_ciface_print_memref_4d_f32(StridedMemRefType<float, 4> *M);
282239

283-
extern "C" MLIR_RUNNER_UTILS_EXPORT void
240+
extern "C" MLIR_RUNNERUTILS_EXPORT void
284241
_mlir_ciface_print_memref_vector_4x4xf32(
285242
StridedMemRefType<Vector2D<4, 4, float>, 2> *M);
286243

287-
// Small runtime support "lib" for vector.print lowering.
288-
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_f32(float f);
289-
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_f64(double d);
290-
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_open();
291-
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_close();
292-
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_comma();
293-
extern "C" MLIR_RUNNER_UTILS_EXPORT void print_newline();
294-
295244
#endif // EXECUTIONENGINE_RUNNERUTILS_H_

mlir/lib/ExecutionEngine/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(LLVM_OPTIONAL_SOURCES
2+
CRunnerUtils.cpp
23
ExecutionEngine.cpp
34
RunnerUtils.cpp
45
OptUtils.cpp
@@ -34,5 +35,12 @@ target_link_libraries(MLIRExecutionEngine
3435

3536
${outlibs})
3637

38+
add_llvm_library(mlir_c_runner_utils SHARED CRunnerUtils.cpp)
39+
target_compile_definitions(mlir_c_runner_utils PRIVATE mlir_c_runner_utils_EXPORTS)
40+
3741
add_llvm_library(mlir_runner_utils SHARED RunnerUtils.cpp)
42+
target_link_libraries(mlir_runner_utils
43+
44+
mlir_c_runner_utils
45+
)
3846
target_compile_definitions(mlir_runner_utils PRIVATE mlir_runner_utils_EXPORTS)
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 <cinttypes>
16+
#include <cstdio>
17+
18+
// Small runtime support "lib" for vector.print lowering.
19+
// By providing elementary printing methods only, this
20+
// library can remain fully unaware of low-level implementation
21+
// details of our vectors. Also useful for direct LLVM IR output.
22+
extern "C" void print_i32(int32_t i) { fprintf(stdout, "%" PRId32, i); }
23+
extern "C" void print_i64(int64_t l) { fprintf(stdout, "%" PRId64, l); }
24+
extern "C" void print_f32(float f) { fprintf(stdout, "%g", f); }
25+
extern "C" void print_f64(double d) { fprintf(stdout, "%lg", d); }
26+
extern "C" void print_open() { fputs("( ", stdout); }
27+
extern "C" void print_close() { fputs(" )", stdout); }
28+
extern "C" void print_comma() { fputs(", ", stdout); }
29+
extern "C" void print_newline() { fputc('\n', stdout); }

mlir/lib/ExecutionEngine/RunnerUtils.cpp

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
//===- RunnerUtils.cpp - Utils for MLIR CPU execution ---------------------===//
1+
//===- RunnerUtils.cpp - Utils for MLIR exec on targets with a C++ runtime ===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// Utilities for interfacing MLIR types with C code as well as printing,
10-
// debugging etc.
9+
// This file imlpements basic functions to debug structured MLIR types at
10+
// runtime. Entities in this file may not be compatible with targets without a
11+
// C++ runtime. These may be progressively migrated to CRunnerUtils.cpp over
12+
// time.
1113
//
1214
//===----------------------------------------------------------------------===//
1315

1416
#include "mlir/ExecutionEngine/RunnerUtils.h"
1517

16-
#include <cinttypes>
17-
#include <cstdio>
18-
1918
extern "C" void _mlir_ciface_print_memref_vector_4x4xf32(
2019
StridedMemRefType<Vector2D<4, 4, float>, 2> *M) {
2120
impl::printMemRef(*M);
@@ -85,16 +84,3 @@ extern "C" void
8584
_mlir_ciface_print_memref_4d_f32(StridedMemRefType<float, 4> *M) {
8685
impl::printMemRef(*M);
8786
}
88-
89-
// Small runtime support "lib" for vector.print lowering.
90-
// By providing elementary printing methods only, this
91-
// library can remain fully unaware of low-level implementation
92-
// details of our vectors. Also useful for direct LLVM IR output.
93-
extern "C" void print_i32(int32_t i) { fprintf(stdout, "%" PRId32, i); }
94-
extern "C" void print_i64(int64_t l) { fprintf(stdout, "%" PRId64, l); }
95-
extern "C" void print_f32(float f) { fprintf(stdout, "%g", f); }
96-
extern "C" void print_f64(double d) { fprintf(stdout, "%lg", d); }
97-
extern "C" void print_open() { fputs("( ", stdout); }
98-
extern "C" void print_close() { fputs(" )", stdout); }
99-
extern "C" void print_comma() { fputs(", ", stdout); }
100-
extern "C" void print_newline() { fputc('\n', stdout); }

mlir/test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(MLIR_TEST_DEPENDS
4141
cblas
4242
cblas_interface
4343
mlir_runner_utils
44+
mlir_c_runner_utils
4445
)
4546

4647
if(LLVM_BUILD_EXAMPLES)

mlir/test/mlir-cpu-runner/bare_ptr_call_conv.mlir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s -convert-loop-to-std -convert-std-to-llvm='use-bare-ptr-memref-call-conv=1' | mlir-cpu-runner -shared-libs=%linalg_test_lib_dir/libmlir_runner_utils%shlibext -entry-point-result=void | FileCheck %s
1+
// RUN: mlir-opt %s -convert-loop-to-std -convert-std-to-llvm='use-bare-ptr-memref-call-conv=1' | mlir-cpu-runner -shared-libs=%linalg_test_lib_dir/libmlir_c_runner_utils%shlibext -entry-point-result=void | FileCheck %s
22

33
// Verify bare pointer memref calling convention. `simple_add1_add2_test`
44
// gets two 2xf32 memrefs, adds 1.0f to the first one and 2.0f to the second

0 commit comments

Comments
 (0)