xref: /llvm-project/mlir/test/Integration/Dialect/MemRef/verify-memref.mlir (revision eb206e9ea84eff0a0596fed2de8316d924f946d1)
1// RUN: mlir-opt %s \
2// RUN:   -one-shot-bufferize="bufferize-function-boundaries" --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-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
10module {
11  func.func private @verifyMemRefI8(%a : tensor<*xi8> {bufferization.access = "read"}, %b : tensor<*xi8> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
12  func.func private @verifyMemRefI16(%a : tensor<*xi16> {bufferization.access = "read"}, %b : tensor<*xi16> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
13  func.func private @verifyMemRefI32(%a : tensor<*xi32> {bufferization.access = "read"}, %b : tensor<*xi32> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
14  func.func private @verifyMemRefI64(%a : tensor<*xi64> {bufferization.access = "read"}, %b : tensor<*xi64> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
15  func.func private @verifyMemRefBF16(%a : tensor<*xbf16> {bufferization.access = "read"}, %b : tensor<*xbf16> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
16  func.func private @verifyMemRefF16(%a : tensor<*xf16> {bufferization.access = "read"}, %b : tensor<*xf16> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
17  func.func private @verifyMemRefF32(%a : tensor<*xf32> {bufferization.access = "read"}, %b : tensor<*xf32> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
18  func.func private @verifyMemRefF64(%a : tensor<*xf64> {bufferization.access = "read"}, %b : tensor<*xf64> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
19  func.func private @verifyMemRefC32(%a : tensor<*xcomplex<f32>> {bufferization.access = "read"}, %b : tensor<*xcomplex<f32>> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
20  func.func private @verifyMemRefC64(%a : tensor<*xcomplex<f64>> {bufferization.access = "read"}, %b : tensor<*xcomplex<f64>> {bufferization.access = "read"}) -> i64 attributes { llvm.emit_c_interface }
21  func.func private @verifyMemRefInd(%a : tensor<*xindex> {bufferization.access = "read"}, %b : tensor<*xindex> {bufferization.access = "read"}) -> 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}
168