Skip to content

Commit 753dc0a

Browse files
[mlir][verifyMemref] Fix bug and support more types for verifyMemref (llvm#77682)
1. Fix a bug in verifyMemref to pass in `data` instead of `baseptr`, which didn't verify data correctly. 2. Add `==` for f16 and bf16. 3. Add a comprehensive test of verifyMemref for all supported types.
1 parent 03be448 commit 753dc0a

File tree

5 files changed

+217
-3
lines changed

5 files changed

+217
-3
lines changed

mlir/include/mlir/ExecutionEngine/Float16bits.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ MLIR_FLOAT16_EXPORT std::ostream &operator<<(std::ostream &os, const f16 &f);
4848
// Outputs a bfloat value.
4949
MLIR_FLOAT16_EXPORT std::ostream &operator<<(std::ostream &os, const bf16 &d);
5050

51+
MLIR_FLOAT16_EXPORT bool operator==(const f16 &f1, const f16 &f2);
52+
MLIR_FLOAT16_EXPORT bool operator==(const bf16 &bf1, const bf16 &bf2);
53+
5154
extern "C" MLIR_FLOAT16_EXPORT void printF16(uint16_t bits);
5255
extern "C" MLIR_FLOAT16_EXPORT void printBF16(uint16_t bits);
5356

mlir/include/mlir/ExecutionEngine/RunnerUtils.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ int64_t verifyMemRef(const DynamicMemRefType<T> &actual,
331331
}
332332
// Return the number of errors.
333333
int64_t printCounter = 0;
334-
return MemRefDataVerifier<T>::verify(
335-
std::cerr, actual.basePtr, expected.basePtr, actual.rank, actual.offset,
336-
actual.sizes, actual.strides, printCounter);
334+
return MemRefDataVerifier<T>::verify(std::cerr, actual.data, expected.data,
335+
actual.rank, actual.offset, actual.sizes,
336+
actual.strides, printCounter);
337337
}
338338

339339
/// Verify the equivalence of two unranked memrefs and return the number of
@@ -429,8 +429,18 @@ _mlir_ciface_printMemref1dC64(StridedMemRefType<impl::complex64, 1> *m);
429429
extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_printMemrefVector4x4xf32(
430430
StridedMemRefType<Vector2D<4, 4, float>, 2> *m);
431431

432+
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI8(
433+
UnrankedMemRefType<int8_t> *actual, UnrankedMemRefType<int8_t> *expected);
434+
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI16(
435+
UnrankedMemRefType<int16_t> *actual, UnrankedMemRefType<int16_t> *expected);
432436
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI32(
433437
UnrankedMemRefType<int32_t> *actual, UnrankedMemRefType<int32_t> *expected);
438+
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI64(
439+
UnrankedMemRefType<int64_t> *actual, UnrankedMemRefType<int64_t> *expected);
440+
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefBF16(
441+
UnrankedMemRefType<bf16> *actual, UnrankedMemRefType<bf16> *expected);
442+
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefF16(
443+
UnrankedMemRefType<f16> *actual, UnrankedMemRefType<f16> *expected);
434444
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefF32(
435445
UnrankedMemRefType<float> *actual, UnrankedMemRefType<float> *expected);
436446
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefF64(

mlir/lib/ExecutionEngine/Float16bits.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ std::ostream &operator<<(std::ostream &os, const bf16 &d) {
150150
return os;
151151
}
152152

153+
bool operator==(const f16 &f1, const f16 &f2) { return f1.bits == f2.bits; }
154+
155+
bool operator==(const bf16 &f1, const bf16 &f2) { return f1.bits == f2.bits; }
156+
153157
// Mark these symbols as weak so they don't conflict when compiler-rt also
154158
// defines them.
155159
#define ATTR_WEAK

mlir/lib/ExecutionEngine/RunnerUtils.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,42 @@ _mlir_ciface_printMemref1dC64(StridedMemRefType<impl::complex64, 1> *M) {
219219
impl::printMemRef(*M);
220220
}
221221

222+
extern "C" int64_t
223+
_mlir_ciface_verifyMemRefI8(UnrankedMemRefType<int8_t> *actual,
224+
UnrankedMemRefType<int8_t> *expected) {
225+
return impl::verifyMemRef(*actual, *expected);
226+
}
227+
228+
extern "C" int64_t
229+
_mlir_ciface_verifyMemRefI16(UnrankedMemRefType<int16_t> *actual,
230+
UnrankedMemRefType<int16_t> *expected) {
231+
return impl::verifyMemRef(*actual, *expected);
232+
}
233+
222234
extern "C" int64_t
223235
_mlir_ciface_verifyMemRefI32(UnrankedMemRefType<int32_t> *actual,
224236
UnrankedMemRefType<int32_t> *expected) {
225237
return impl::verifyMemRef(*actual, *expected);
226238
}
227239

240+
extern "C" int64_t
241+
_mlir_ciface_verifyMemRefI64(UnrankedMemRefType<int64_t> *actual,
242+
UnrankedMemRefType<int64_t> *expected) {
243+
return impl::verifyMemRef(*actual, *expected);
244+
}
245+
246+
extern "C" int64_t
247+
_mlir_ciface_verifyMemRefF16(UnrankedMemRefType<f16> *actual,
248+
UnrankedMemRefType<f16> *expected) {
249+
return impl::verifyMemRef(*actual, *expected);
250+
}
251+
252+
extern "C" int64_t
253+
_mlir_ciface_verifyMemRefBF16(UnrankedMemRefType<bf16> *actual,
254+
UnrankedMemRefType<bf16> *expected) {
255+
return impl::verifyMemRef(*actual, *expected);
256+
}
257+
228258
extern "C" int64_t
229259
_mlir_ciface_verifyMemRefF32(UnrankedMemRefType<float> *actual,
230260
UnrankedMemRefType<float> *expected) {
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// RUN: mlir-opt %s \
2+
// RUN: -func-bufferize -arith-bufferize --canonicalize \
3+
// RUN: -convert-vector-to-scf -convert-scf-to-cf -convert-vector-to-llvm -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+
func.func private @verifyMemRefI8(%a : tensor<*xi8>, %b : tensor<*xi8>) -> i64 attributes { llvm.emit_c_interface }
12+
func.func private @verifyMemRefI16(%a : tensor<*xi16>, %b : tensor<*xi16>) -> i64 attributes { llvm.emit_c_interface }
13+
func.func private @verifyMemRefI32(%a : tensor<*xi32>, %b : tensor<*xi32>) -> i64 attributes { llvm.emit_c_interface }
14+
func.func private @verifyMemRefI64(%a : tensor<*xi64>, %b : tensor<*xi64>) -> i64 attributes { llvm.emit_c_interface }
15+
func.func private @verifyMemRefBF16(%a : tensor<*xbf16>, %b : tensor<*xbf16>) -> i64 attributes { llvm.emit_c_interface }
16+
func.func private @verifyMemRefF16(%a : tensor<*xf16>, %b : tensor<*xf16>) -> i64 attributes { llvm.emit_c_interface }
17+
func.func private @verifyMemRefF32(%a : tensor<*xf32>, %b : tensor<*xf32>) -> i64 attributes { llvm.emit_c_interface }
18+
func.func private @verifyMemRefF64(%a : tensor<*xf64>, %b : tensor<*xf64>) -> i64 attributes { llvm.emit_c_interface }
19+
func.func private @verifyMemRefC32(%a : tensor<*xcomplex<f32>>, %b : tensor<*xcomplex<f32>>) -> i64 attributes { llvm.emit_c_interface }
20+
func.func private @verifyMemRefC64(%a : tensor<*xcomplex<f64>>, %b : tensor<*xcomplex<f64>>) -> i64 attributes { llvm.emit_c_interface }
21+
func.func private @verifyMemRefInd(%a : tensor<*xindex>, %b : tensor<*xindex>) -> i64 attributes { llvm.emit_c_interface }
22+
23+
func.func @entry() {
24+
%i8 = arith.constant dense<90> : tensor<3x3xi8>
25+
%i16 = arith.constant dense<1> : tensor<3x3xi16>
26+
%i32 = arith.constant dense<2> : tensor<3x3xi32>
27+
%i64 = arith.constant dense<3> : tensor<3x3xi64>
28+
%f16 = arith.constant dense<1.5> : tensor<3x3xf16>
29+
%bf16 = arith.constant dense<2.5> : tensor<3x3xbf16>
30+
%f32 = arith.constant dense<3.5> : tensor<3x3xf32>
31+
%f64 = arith.constant dense<4.5> : tensor<3x3xf64>
32+
%c32 = arith.constant dense<(1.000000e+01,5.000000e+00)> : tensor<3x3xcomplex<f32>>
33+
%c64 = arith.constant dense<(2.000000e+01,5.000000e+00)> : tensor<3x3xcomplex<f64>>
34+
%ind = arith.constant dense<4> : tensor<3x3xindex>
35+
36+
%1 = tensor.cast %i8 : tensor<3x3xi8> to tensor<*xi8>
37+
%2 = tensor.cast %i16 : tensor<3x3xi16> to tensor<*xi16>
38+
%3 = tensor.cast %i32 : tensor<3x3xi32> to tensor<*xi32>
39+
%4 = tensor.cast %i64 : tensor<3x3xi64> to tensor<*xi64>
40+
%5 = tensor.cast %f16 : tensor<3x3xf16> to tensor<*xf16>
41+
%6 = tensor.cast %bf16 : tensor<3x3xbf16> to tensor<*xbf16>
42+
%7 = tensor.cast %f32 : tensor<3x3xf32> to tensor<*xf32>
43+
%8 = tensor.cast %f64 : tensor<3x3xf64> to tensor<*xf64>
44+
%9 = tensor.cast %c32 : tensor<3x3xcomplex<f32>> to tensor<*xcomplex<f32>>
45+
%10 = tensor.cast %c64 : tensor<3x3xcomplex<f64>> to tensor<*xcomplex<f64>>
46+
%11 = tensor.cast %ind : tensor<3x3xindex> to tensor<*xindex>
47+
48+
//
49+
// Ensure that verifyMemRef could detect equal memrefs.
50+
//
51+
// CHECK: 0
52+
%res0 = call @verifyMemRefI8(%1, %1) : (tensor<*xi8>, tensor<*xi8>) -> (i64)
53+
vector.print %res0 : i64
54+
55+
// CHECK-NEXT: 0
56+
%res1 = call @verifyMemRefI16(%2, %2) : (tensor<*xi16>, tensor<*xi16>) -> (i64)
57+
vector.print %res1 : i64
58+
59+
// CHECK-NEXT: 0
60+
%res2 = call @verifyMemRefI32(%3, %3) : (tensor<*xi32>, tensor<*xi32>) -> (i64)
61+
vector.print %res2 : i64
62+
63+
// CHECK-NEXT: 0
64+
%res3 = call @verifyMemRefI64(%4, %4) : (tensor<*xi64>, tensor<*xi64>) -> (i64)
65+
vector.print %res3 : i64
66+
67+
// CHECK-NEXT: 0
68+
%res4 = call @verifyMemRefF16(%5, %5) : (tensor<*xf16>, tensor<*xf16>) -> (i64)
69+
vector.print %res4 : i64
70+
71+
// CHECK-NEXT: 0
72+
%res5 = call @verifyMemRefBF16(%6, %6) : (tensor<*xbf16>, tensor<*xbf16>) -> (i64)
73+
vector.print %res5 : i64
74+
75+
// CHECK-NEXT: 0
76+
%res6 = call @verifyMemRefF32(%7, %7) : (tensor<*xf32>, tensor<*xf32>) -> (i64)
77+
vector.print %res6 : i64
78+
79+
// CHECK-NEXT: 0
80+
%res7 = call @verifyMemRefF64(%8, %8) : (tensor<*xf64>, tensor<*xf64>) -> (i64)
81+
vector.print %res7 : i64
82+
83+
// CHECK-NEXT: 0
84+
%res8 = call @verifyMemRefC32(%9, %9) : (tensor<*xcomplex<f32>>, tensor<*xcomplex<f32>>) -> (i64)
85+
vector.print %res8 : i64
86+
87+
// CHECK-NEXT: 0
88+
%res9 = call @verifyMemRefC64(%10, %10) : (tensor<*xcomplex<f64>>, tensor<*xcomplex<f64>>) -> (i64)
89+
vector.print %res9 : i64
90+
91+
// CHECK-NEXT: 0
92+
%res10 = call @verifyMemRefInd(%11, %11) : (tensor<*xindex>, tensor<*xindex>) -> (i64)
93+
vector.print %res10 : i64
94+
95+
//
96+
// Ensure that verifyMemRef could detect the correct number of errors
97+
// for unequal memrefs.
98+
//
99+
%m1 = arith.constant dense<100> : tensor<3x3xi8>
100+
%f1 = tensor.cast %m1 : tensor<3x3xi8> to tensor<*xi8>
101+
%fail_res1 = call @verifyMemRefI8(%1, %f1) : (tensor<*xi8>, tensor<*xi8>) -> (i64)
102+
// CHECK-NEXT: 9
103+
vector.print %fail_res1 : i64
104+
105+
%m2 = arith.constant dense<100> : tensor<3x3xi16>
106+
%f2 = tensor.cast %m2 : tensor<3x3xi16> to tensor<*xi16>
107+
%fail_res2 = call @verifyMemRefI16(%2, %f2) : (tensor<*xi16>, tensor<*xi16>) -> (i64)
108+
// CHECK-NEXT: 9
109+
vector.print %fail_res2 : i64
110+
111+
%m3 = arith.constant dense<100> : tensor<3x3xi32>
112+
%f3 = tensor.cast %m3 : tensor<3x3xi32> to tensor<*xi32>
113+
%fail_res3 = call @verifyMemRefI32(%3, %f3) : (tensor<*xi32>, tensor<*xi32>) -> (i64)
114+
// CHECK-NEXT: 9
115+
vector.print %fail_res3 : i64
116+
117+
%m4 = arith.constant dense<100> : tensor<3x3xi64>
118+
%f4 = tensor.cast %m4 : tensor<3x3xi64> to tensor<*xi64>
119+
%fail_res4 = call @verifyMemRefI64(%4, %f4) : (tensor<*xi64>, tensor<*xi64>) -> (i64)
120+
// CHECK-NEXT: 9
121+
vector.print %fail_res4 : i64
122+
123+
%m5 = arith.constant dense<100.0> : tensor<3x3xf16>
124+
%f5 = tensor.cast %m5 : tensor<3x3xf16> to tensor<*xf16>
125+
%fail_res5 = call @verifyMemRefF16(%5, %f5) : (tensor<*xf16>, tensor<*xf16>) -> (i64)
126+
// CHECK-NEXT: 9
127+
vector.print %fail_res5 : i64
128+
129+
%m6 = arith.constant dense<100.0> : tensor<3x3xbf16>
130+
%f6 = tensor.cast %m6 : tensor<3x3xbf16> to tensor<*xbf16>
131+
%fail_res6 = call @verifyMemRefBF16(%6, %f6) : (tensor<*xbf16>, tensor<*xbf16>) -> (i64)
132+
// CHECK-NEXT: 9
133+
vector.print %fail_res6 : i64
134+
135+
%m7 = arith.constant dense<100.0> : tensor<3x3xf32>
136+
%f7 = tensor.cast %m7 : tensor<3x3xf32> to tensor<*xf32>
137+
%fail_res7 = call @verifyMemRefF32(%7, %f7) : (tensor<*xf32>, tensor<*xf32>) -> (i64)
138+
// CHECK-NEXT: 9
139+
vector.print %fail_res7 : i64
140+
141+
%m8 = arith.constant dense<100.0> : tensor<3x3xf64>
142+
%f8 = tensor.cast %m8 : tensor<3x3xf64> to tensor<*xf64>
143+
%fail_res8 = call @verifyMemRefF64(%8, %f8) : (tensor<*xf64>, tensor<*xf64>) -> (i64)
144+
// CHECK-NEXT: 9
145+
vector.print %fail_res8 : i64
146+
147+
%m9 = arith.constant dense<(5.000000e+01,1.000000e+00)> : tensor<3x3xcomplex<f32>>
148+
%f9 = tensor.cast %m9 : tensor<3x3xcomplex<f32>> to tensor<*xcomplex<f32>>
149+
%fail_res9 = call @verifyMemRefC32(%9, %f9) : (tensor<*xcomplex<f32>>, tensor<*xcomplex<f32>>) -> (i64)
150+
// CHECK-NEXT: 9
151+
vector.print %fail_res9 : i64
152+
153+
%m10 = arith.constant dense<(5.000000e+01,1.000000e+00)> : tensor<3x3xcomplex<f64>>
154+
%f10 = tensor.cast %m10 : tensor<3x3xcomplex<f64>> to tensor<*xcomplex<f64>>
155+
%fail_res10 = call @verifyMemRefC64(%10, %f10) : (tensor<*xcomplex<f64>>, tensor<*xcomplex<f64>>) -> (i64)
156+
// CHECK-NEXT: 9
157+
vector.print %fail_res10 : i64
158+
159+
%m11 = arith.constant dense<100> : tensor<3x3xindex>
160+
%f11 = tensor.cast %m11 : tensor<3x3xindex> to tensor<*xindex>
161+
%fail_res11 = call @verifyMemRefInd(%11, %f11) : (tensor<*xindex>, tensor<*xindex>) -> (i64)
162+
// CHECK-NEXT: 9
163+
vector.print %fail_res11 : i64
164+
165+
return
166+
}
167+
}

0 commit comments

Comments
 (0)