Skip to content

Commit 7bc6c4a

Browse files
[mlir][print]Add functions for printing memref f16/bf16/i16 (#75094)
1. Added functions for printMemrefI16/f16/bf16. 2. Added a new integration test for all the printMemref functions.
1 parent c6351b4 commit 7bc6c4a

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

mlir/include/mlir/ExecutionEngine/RunnerUtils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <iostream>
3939

4040
#include "mlir/ExecutionEngine/CRunnerUtils.h"
41+
#include "mlir/ExecutionEngine/Float16bits.h"
4142

4243
template <typename T, typename StreamType>
4344
void printMemRefMetaData(StreamType &os, const DynamicMemRefType<T> &v) {
@@ -369,10 +370,16 @@ _mlir_ciface_printMemrefShapeC64(UnrankedMemRefType<impl::complex64> *m);
369370
extern "C" MLIR_RUNNERUTILS_EXPORT void
370371
_mlir_ciface_printMemrefI8(UnrankedMemRefType<int8_t> *m);
371372
extern "C" MLIR_RUNNERUTILS_EXPORT void
373+
_mlir_ciface_printMemrefI16(UnrankedMemRefType<int16_t> *m);
374+
extern "C" MLIR_RUNNERUTILS_EXPORT void
372375
_mlir_ciface_printMemrefI32(UnrankedMemRefType<int32_t> *m);
373376
extern "C" MLIR_RUNNERUTILS_EXPORT void
374377
_mlir_ciface_printMemrefI64(UnrankedMemRefType<int64_t> *m);
375378
extern "C" MLIR_RUNNERUTILS_EXPORT void
379+
_mlir_ciface_printMemrefF16(UnrankedMemRefType<f16> *m);
380+
extern "C" MLIR_RUNNERUTILS_EXPORT void
381+
_mlir_ciface_printMemrefBF16(UnrankedMemRefType<bf16> *m);
382+
extern "C" MLIR_RUNNERUTILS_EXPORT void
376383
_mlir_ciface_printMemrefF32(UnrankedMemRefType<float> *m);
377384
extern "C" MLIR_RUNNERUTILS_EXPORT void
378385
_mlir_ciface_printMemrefF64(UnrankedMemRefType<double> *m);

mlir/lib/ExecutionEngine/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ if(LLVM_ENABLE_PIC)
157157
RunnerUtils.cpp
158158

159159
EXCLUDE_FROM_LIBMLIR
160+
161+
LINK_LIBS PUBLIC
162+
mlir_float16_utils
160163
)
161164
target_compile_definitions(mlir_runner_utils PRIVATE mlir_runner_utils_EXPORTS)
162165

mlir/lib/ExecutionEngine/RunnerUtils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ extern "C" void _mlir_ciface_printMemrefI8(UnrankedMemRefType<int8_t> *M) {
8181
impl::printMemRef(*M);
8282
}
8383

84+
extern "C" void _mlir_ciface_printMemrefI16(UnrankedMemRefType<int16_t> *M) {
85+
impl::printMemRef(*M);
86+
}
87+
8488
extern "C" void _mlir_ciface_printMemrefI32(UnrankedMemRefType<int32_t> *M) {
8589
impl::printMemRef(*M);
8690
}
@@ -89,6 +93,14 @@ extern "C" void _mlir_ciface_printMemrefI64(UnrankedMemRefType<int64_t> *M) {
8993
impl::printMemRef(*M);
9094
}
9195

96+
extern "C" void _mlir_ciface_printMemrefF16(UnrankedMemRefType<f16> *M) {
97+
impl::printMemRef(*M);
98+
}
99+
100+
extern "C" void _mlir_ciface_printMemrefBF16(UnrankedMemRefType<bf16> *M) {
101+
impl::printMemRef(*M);
102+
}
103+
92104
extern "C" void _mlir_ciface_printMemrefF32(UnrankedMemRefType<float> *M) {
93105
impl::printMemRef(*M);
94106
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// RUN: mlir-opt %s \
2+
// RUN: -func-bufferize -arith-bufferize --canonicalize \
3+
// RUN: -finalize-memref-to-llvm\
4+
// RUN: -convert-func-to-llvm -reconcile-unrealized-casts |\
5+
// RUN: mlir-cpu-runner \
6+
// RUN: -e entry -entry-point-result=void \
7+
// RUN: -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils |\
8+
// RUN: FileCheck %s
9+
10+
module {
11+
12+
func.func private @printMemrefI8(%ptr : tensor<*xi8>) attributes { llvm.emit_c_interface }
13+
func.func private @printMemrefI16(%ptr : tensor<*xi16>) attributes { llvm.emit_c_interface }
14+
func.func private @printMemrefI32(%ptr : tensor<*xi32>) attributes { llvm.emit_c_interface }
15+
func.func private @printMemrefI64(%ptr : tensor<*xi64>) attributes { llvm.emit_c_interface }
16+
func.func private @printMemrefBF16(%ptr : tensor<*xbf16>) attributes { llvm.emit_c_interface }
17+
func.func private @printMemrefF16(%ptr : tensor<*xf16>) attributes { llvm.emit_c_interface }
18+
func.func private @printMemrefF32(%ptr : tensor<*xf32>) attributes { llvm.emit_c_interface }
19+
func.func private @printMemrefF64(%ptr : tensor<*xf64>) attributes { llvm.emit_c_interface }
20+
func.func private @printMemrefC32(%ptr : tensor<*xcomplex<f32>>) attributes { llvm.emit_c_interface }
21+
func.func private @printMemrefC64(%ptr : tensor<*xcomplex<f64>>) attributes { llvm.emit_c_interface }
22+
func.func private @printMemrefInd(%ptr : tensor<*xindex>) attributes { llvm.emit_c_interface }
23+
24+
func.func @entry() {
25+
%i8 = arith.constant dense<90> : tensor<3x3xi8>
26+
%i16 = arith.constant dense<1> : tensor<3x3xi16>
27+
%i32 = arith.constant dense<2> : tensor<3x3xi32>
28+
%i64 = arith.constant dense<3> : tensor<3x3xi64>
29+
%f16 = arith.constant dense<1.5> : tensor<3x3xf16>
30+
%bf16 = arith.constant dense<2.5> : tensor<3x3xbf16>
31+
%f32 = arith.constant dense<3.5> : tensor<3x3xf32>
32+
%f64 = arith.constant dense<4.5> : tensor<3x3xf64>
33+
%c32 = arith.constant dense<(1.000000e+01,5.000000e+00)> : tensor<3x3xcomplex<f32>>
34+
%c64 = arith.constant dense<(2.000000e+01,5.000000e+00)> : tensor<3x3xcomplex<f64>>
35+
%ind = arith.constant dense<4> : tensor<3x3xindex>
36+
37+
%1 = tensor.cast %i8 : tensor<3x3xi8> to tensor<*xi8>
38+
%2 = tensor.cast %i16 : tensor<3x3xi16> to tensor<*xi16>
39+
%3 = tensor.cast %i32 : tensor<3x3xi32> to tensor<*xi32>
40+
%4 = tensor.cast %i64 : tensor<3x3xi64> to tensor<*xi64>
41+
%5 = tensor.cast %f16 : tensor<3x3xf16> to tensor<*xf16>
42+
%6 = tensor.cast %bf16 : tensor<3x3xbf16> to tensor<*xbf16>
43+
%7 = tensor.cast %f32 : tensor<3x3xf32> to tensor<*xf32>
44+
%8 = tensor.cast %f64 : tensor<3x3xf64> to tensor<*xf64>
45+
%9 = tensor.cast %c32 : tensor<3x3xcomplex<f32>> to tensor<*xcomplex<f32>>
46+
%10 = tensor.cast %c64 : tensor<3x3xcomplex<f64>> to tensor<*xcomplex<f64>>
47+
%11 = tensor.cast %ind : tensor<3x3xindex> to tensor<*xindex>
48+
49+
// CHECK: data =
50+
// CHECK-NEXT: {{\[}}[Z, Z, Z],
51+
// CHECK-NEXT: [Z, Z, Z],
52+
// CHECK-NEXT: [Z, Z, Z]]
53+
//
54+
call @printMemrefI8(%1) : (tensor<*xi8>) -> ()
55+
56+
// CHECK-NEXT: data =
57+
// CHECK-NEXT: {{\[}}[1, 1, 1],
58+
// CHECK-NEXT: [1, 1, 1],
59+
// CHECK-NEXT: [1, 1, 1]]
60+
//
61+
call @printMemrefI16(%2) : (tensor<*xi16>) -> ()
62+
63+
// CHECK-NEXT: data =
64+
// CHECK-NEXT: {{\[}}[2, 2, 2],
65+
// CHECK-NEXT: [2, 2, 2],
66+
// CHECK-NEXT: [2, 2, 2]]
67+
//
68+
call @printMemrefI32(%3) : (tensor<*xi32>) -> ()
69+
70+
// CHECK-NEXT: data =
71+
// CHECK-NEXT: {{\[}}[3, 3, 3],
72+
// CHECK-NEXT: [3, 3, 3],
73+
// CHECK-NEXT: [3, 3, 3]]
74+
//
75+
call @printMemrefI64(%4) : (tensor<*xi64>) -> ()
76+
77+
// CHECK-NEXT: data =
78+
// CHECK-NEXT: {{\[}}[1.5, 1.5, 1.5],
79+
// CHECK-NEXT: [1.5, 1.5, 1.5],
80+
// CHECK-NEXT: [1.5, 1.5, 1.5]]
81+
//
82+
call @printMemrefF16(%5) : (tensor<*xf16>) -> ()
83+
84+
// CHECK-NEXT: data =
85+
// CHECK-NEXT: {{\[}}[2.5, 2.5, 2.5],
86+
// CHECK-NEXT: [2.5, 2.5, 2.5],
87+
// CHECK-NEXT: [2.5, 2.5, 2.5]]
88+
//
89+
call @printMemrefBF16(%6) : (tensor<*xbf16>) -> ()
90+
91+
// CHECK-NEXT: data =
92+
// CHECK-NEXT: {{\[}}[3.5, 3.5, 3.5],
93+
// CHECK-NEXT: [3.5, 3.5, 3.5],
94+
// CHECK-NEXT: [3.5, 3.5, 3.5]]
95+
//
96+
call @printMemrefF32(%7) : (tensor<*xf32>) -> ()
97+
98+
// CHECK-NEXT: data =
99+
// CHECK-NEXT: {{\[}}[4.5, 4.5, 4.5],
100+
// CHECK-NEXT: [4.5, 4.5, 4.5],
101+
// CHECK-NEXT: [4.5, 4.5, 4.5]]
102+
//
103+
call @printMemrefF64(%8) : (tensor<*xf64>) -> ()
104+
105+
// CHECK-NEXT: data =
106+
// CHECK-NEXT: {{\[}}[(10,5), (10,5), (10,5)],
107+
// CHECK-NEXT: [(10,5), (10,5), (10,5)],
108+
// CHECK-NEXT: [(10,5), (10,5), (10,5)]]
109+
//
110+
call @printMemrefC32(%9) : (tensor<*xcomplex<f32>>) -> ()
111+
112+
// CHECK-NEXT: data =
113+
// CHECK-NEXT: {{\[}}[(20,5), (20,5), (20,5)],
114+
// CHECK-NEXT: [(20,5), (20,5), (20,5)],
115+
// CHECK-NEXT: [(20,5), (20,5), (20,5)]]
116+
//
117+
call @printMemrefC64(%10) : (tensor<*xcomplex<f64>>) -> ()
118+
119+
// CHECK-NEXT: data =
120+
// CHECK-NEXT: {{\[}}[4, 4, 4],
121+
// CHECK-NEXT: [4, 4, 4],
122+
// CHECK-NEXT: [4, 4, 4]]
123+
//
124+
call @printMemrefInd(%11) : (tensor<*xindex>) -> ()
125+
126+
return
127+
}
128+
}

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9132,6 +9132,7 @@ cc_library(
91329132
includes = ["include"],
91339133
deps = [
91349134
":mlir_c_runner_utils",
9135+
":mlir_float16_utils",
91359136
"//llvm:Support",
91369137
],
91379138
)

0 commit comments

Comments
 (0)