330 lines · plain
1// RUN: mlir-opt %s -generate-runtime-verification \2// RUN: -one-shot-bufferize="bufferize-function-boundaries" \3// RUN: -buffer-deallocation-pipeline \4// RUN: -convert-bufferization-to-memref \5// RUN: -convert-linalg-to-loops \6// RUN: -expand-strided-metadata \7// RUN: -lower-affine \8// RUN: -convert-scf-to-cf \9// RUN: -test-cf-assert \10// RUN: -convert-index-to-llvm \11// RUN: -finalize-memref-to-llvm \12// RUN: -convert-func-to-llvm \13// RUN: -convert-arith-to-llvm \14// RUN: -convert-cf-to-llvm \15// RUN: -reconcile-unrealized-casts | \16// RUN: mlir-runner -e main -entry-point-result=void \17// RUN: -shared-libs=%mlir_runner_utils \18// RUN: -shared-libs=%mlir_c_runner_utils 2>&1 | \19// RUN: FileCheck %s20 21func.func @main() {22 %c5x = arith.constant dense<0.0> : tensor<5xf32>23 %c4x = arith.constant dense<0.0> : tensor<4xf32>24 %d5x = tensor.cast %c5x : tensor<5xf32> to tensor<?xf32>25 %d4x = tensor.cast %c4x : tensor<4xf32> to tensor<?xf32>26 27 // CHECK: ERROR: Runtime op verification failed28 // CHECK-NEXT: linalg.generic29 // CHECK-NEXT: ^ dimension #0 of input/output operand #1 is incompatible with inferred dimension size30 func.call @simple_add(%d5x, %d4x) : (tensor<?xf32>, tensor<?xf32>) -> (tensor<?xf32>)31 32 // CHECK: ERROR: Runtime op verification failed33 // CHECK-NEXT: linalg.generic34 // CHECK-NEXT: ^ dimension #0 of input/output operand #1 is incompatible with inferred dimension size35 func.call @simple_add(%d4x, %d5x) : (tensor<?xf32>, tensor<?xf32>) -> (tensor<?xf32>)36 37 %c1x1 = arith.constant dense<0.0> : tensor<1x1xf32>38 %c1x4 = arith.constant dense<0.0> : tensor<1x4xf32>39 %c4x4 = arith.constant dense<0.0> : tensor<4x4xf32>40 %c4x5 = arith.constant dense<0.0> : tensor<4x5xf32>41 %c5x4 = arith.constant dense<0.0> : tensor<5x4xf32>42 %d1x1 = tensor.cast %c1x1 : tensor<1x1xf32> to tensor<?x?xf32>43 %d1x4 = tensor.cast %c1x4 : tensor<1x4xf32> to tensor<?x?xf32>44 %d4x4 = tensor.cast %c4x4 : tensor<4x4xf32> to tensor<?x?xf32>45 %d4x5 = tensor.cast %c4x5 : tensor<4x5xf32> to tensor<?x?xf32>46 %d5x4 = tensor.cast %c5x4 : tensor<5x4xf32> to tensor<?x?xf32>47 48 // CHECK: ERROR: Runtime op verification failed49 // CHECK-NEXT: linalg.generic50 // CHECK-NEXT: ^ dimension #1 of input/output operand #1 is incompatible with inferred dimension size51 52 // CHECK: ERROR: Runtime op verification failed53 // CHECK-NEXT: linalg.generic54 // CHECK-NEXT: ^ dimension #1 of input/output operand #2 is incompatible with inferred dimension size55 func.call @broadcast_add(%d1x4, %d4x5) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)56 57 // CHECK: ERROR: Runtime op verification failed58 // CHECK-NEXT: linalg.generic59 // CHECK-NEXT: ^ dimension #0 of input/output operand #1 is incompatible with inferred dimension size 60 61 // CHECK: ERROR: Runtime op verification failed62 // CHECK-NEXT: linalg.generic63 // CHECK-NEXT: ^ dimension #1 of input/output operand #1 is incompatible with inferred dimension size64 65 // CHECK: ERROR: Runtime op verification failed66 // CHECK-NEXT: linalg.generic67 // CHECK-NEXT: ^ dimension #1 of input/output operand #2 is incompatible with inferred dimension size68 func.call @broadcast_add(%d5x4, %d4x5) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)69 70 // CHECK: ERROR: Runtime op verification failed71 // CHECK-NEXT: linalg.generic72 // CHECK-NEXT: ^ dimension #0 of input/output operand #1 is incompatible with inferred dimension size73 func.call @matmul_generic(%d4x5, %d4x5) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)74 75 // CHECK: ERROR: Runtime op verification failed76 // CHECK-NEXT: linalg.matmul77 // CHECK-NEXT: ^ dimension #0 of input/output operand #1 is incompatible with inferred dimension size78 func.call @matmul_named(%d4x5, %d4x5) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)79 80 %c64x57 = arith.constant dense<0.0> : tensor<16x29xf32>81 %c3x4 = arith.constant dense<0.0> : tensor<3x4xf32>82 83 // CHECK: ERROR: Runtime op verification failed84 // CHECK-NEXT: linalg.generic85 // CHECK-NEXT: unexpected negative result on dimension #0 of input/output operand #086 func.call @reverse_from_3(%d5x) : (tensor<?xf32>) -> (tensor<?xf32>)87 88 %c0x = arith.constant dense<1.0> : tensor<0xf32>89 %d0x = tensor.cast %c0x : tensor<0xf32> to tensor<?xf32>90 91 %c0x5 = arith.constant dense<0.0> : tensor<0x5xf32>92 %d0x5 = tensor.cast %c0x5 : tensor<0x5xf32> to tensor<?x?xf32>93 94 // CHECK-NOT: ERROR: Runtime op verification failed95 func.call @fill_empty_1d(%d0x) : (tensor<?xf32>) -> (tensor<?xf32>)96 97 // CHECK-NOT: ERROR: Runtime op verification failed98 func.call @simple_add(%d5x, %d5x) : (tensor<?xf32>, tensor<?xf32>) -> (tensor<?xf32>)99 100 // CHECK-NOT: ERROR: Runtime op verification failed101 func.call @fill_empty_2d(%d0x5) : (tensor<?x?xf32>) -> (tensor<?x?xf32>)102 103 // CHECK-NOT: ERROR: Runtime op verification failed104 func.call @conv(%c64x57, %c3x4) : (tensor<16x29xf32>, tensor<3x4xf32>) -> (tensor<5x7xf32>)105 106 // CHECK-NOT: ERROR: Runtime op verification failed107 func.call @reverse_from_3(%d4x) : (tensor<?xf32>) -> (tensor<?xf32>)108 109 // CHECK-NOT: ERROR: Runtime op verification failed110 func.call @matmul_named(%d5x4, %d4x5) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)111 112 // CHECK-NOT: ERROR: Runtime op verification failed113 func.call @matmul_generic(%d5x4, %d4x5) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)114 115 // CHECK-NOT: ERROR: Runtime op verification failed116 func.call @broadcast_add(%d1x1, %d1x1) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)117 118 // CHECK-NOT: ERROR: Runtime op verification failed119 func.call @broadcast_add(%d1x1, %d4x5) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)120 121 // CHECK-NOT: ERROR: Runtime op verification failed122 func.call @broadcast_add(%d4x4, %d1x4) : (tensor<?x?xf32>, tensor<?x?xf32>) -> (tensor<?x?xf32>)123 124 return125}126 127 128#identity1D = affine_map<(d0) -> (d0)>129 130func.func @simple_add(%arg0: tensor<?xf32>, %arg1: tensor<?xf32>) -> (tensor<?xf32>) {131 %0 = linalg.generic {132 indexing_maps = [#identity1D, #identity1D],133 iterator_types = ["parallel"]134 } ins(%arg0 : tensor<?xf32>)135 outs(%arg1 : tensor<?xf32>) {136 ^bb0(%gen_arg1: f32, %gen_arg2: f32) :137 %tmp1 = arith.addf %gen_arg1, %gen_arg2 : f32138 linalg.yield %tmp1 : f32139 } -> tensor<?xf32>140 return %0 : tensor<?xf32>141}142 143#broadcastD0 = affine_map<(d0, d1) -> (0, d1)>144#broadcastD1 = affine_map<(d0, d1) -> (d0, 0)>145#identity2D = affine_map<(d0, d1) -> (d0, d1)>146 147func.func @broadcast_add(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>) -> tensor<?x?xf32> {148 // Calculate maximum dimension 0149 %c0 = arith.constant 0 : index150 %dim = tensor.dim %arg0, %c0 : tensor<?x?xf32>151 %dim_0 = tensor.dim %arg1, %c0 : tensor<?x?xf32>152 %0 = arith.maxui %dim, %dim_0 : index153 154 // Calculate maximum dimension 1155 %c1 = arith.constant 1 : index156 %dim_1 = tensor.dim %arg0, %c1 : tensor<?x?xf32>157 %dim_2 = tensor.dim %arg1, %c1 : tensor<?x?xf32>158 %1 = arith.maxui %dim_1, %dim_2 : index159 160 // Broadcast dimension 0 of %arg0161 %dim_3 = tensor.dim %arg0, %c0 : tensor<?x?xf32>162 %2 = arith.cmpi eq, %dim_3, %c1 : index163 %3 = scf.if %2 -> (tensor<?x?xf32>) {164 %dim_7 = tensor.dim %arg0, %c1 : tensor<?x?xf32>165 %12 = tensor.empty(%0, %dim_7) : tensor<?x?xf32>166 %13 = linalg.generic {167 indexing_maps = [#broadcastD0, #identity2D],168 iterator_types = ["parallel", "parallel"]169 } ins(%arg0 : tensor<?x?xf32>) outs(%12 : tensor<?x?xf32>) {170 ^bb0(%in: f32, %out: f32):171 linalg.yield %in : f32172 } -> tensor<?x?xf32>173 scf.yield %13 : tensor<?x?xf32>174 } else {175 scf.yield %arg0 : tensor<?x?xf32>176 }177 178 // Broadcast dimension 1 of %arg0179 %dim_4 = tensor.dim %3, %c1 : tensor<?x?xf32>180 %4 = arith.cmpi eq, %dim_4, %c1 : index181 %5 = scf.if %4 -> (tensor<?x?xf32>) {182 %dim_7 = tensor.dim %3, %c0 : tensor<?x?xf32>183 %12 = tensor.empty(%dim_7, %1) : tensor<?x?xf32>184 %13 = linalg.generic {185 indexing_maps = [#broadcastD1, #identity2D],186 iterator_types = ["parallel", "parallel"]187 } ins(%3 : tensor<?x?xf32>) outs(%12 : tensor<?x?xf32>) {188 ^bb0(%in: f32, %out: f32):189 linalg.yield %in : f32190 } -> tensor<?x?xf32>191 scf.yield %13 : tensor<?x?xf32>192 } else {193 scf.yield %3 : tensor<?x?xf32>194 }195 196 // Broadcast dimension 0 of %arg1197 %dim_5 = tensor.dim %arg1, %c0 : tensor<?x?xf32>198 %6 = arith.cmpi eq, %dim_5, %c1 : index199 %7 = scf.if %6 -> (tensor<?x?xf32>) {200 %dim_7 = tensor.dim %arg1, %c1 : tensor<?x?xf32>201 %12 = tensor.empty(%0, %dim_7) : tensor<?x?xf32>202 %13 = linalg.generic {203 indexing_maps = [#broadcastD0, #identity2D],204 iterator_types = ["parallel", "parallel"]205 } ins(%arg1 : tensor<?x?xf32>) outs(%12 : tensor<?x?xf32>) {206 ^bb0(%in: f32, %out: f32):207 linalg.yield %in : f32208 } -> tensor<?x?xf32>209 scf.yield %13 : tensor<?x?xf32>210 } else {211 scf.yield %arg1 : tensor<?x?xf32>212 }213 214 // Broadcast dimension 1 of %arg1215 %dim_6 = tensor.dim %7, %c1 : tensor<?x?xf32>216 %8 = arith.cmpi eq, %dim_6, %c1 : index217 %9 = scf.if %8 -> (tensor<?x?xf32>) {218 %dim_7 = tensor.dim %7, %c0 : tensor<?x?xf32>219 %12 = tensor.empty(%dim_7, %1) : tensor<?x?xf32>220 %13 = linalg.generic {221 indexing_maps = [#broadcastD1, #identity2D],222 iterator_types = ["parallel", "parallel"]223 } ins(%7 : tensor<?x?xf32>) outs(%12 : tensor<?x?xf32>) {224 ^bb0(%in: f32, %out: f32):225 linalg.yield %in : f32226 } -> tensor<?x?xf32>227 scf.yield %13 : tensor<?x?xf32>228 } else {229 scf.yield %7 : tensor<?x?xf32>230 }231 232 // Perform element-wise computation233 %10 = tensor.empty(%0, %1) : tensor<?x?xf32>234 %11 = linalg.generic {235 indexing_maps = [#identity2D, #identity2D, #identity2D],236 iterator_types = ["parallel", "parallel"]237 } ins(%5, %9 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%10 : tensor<?x?xf32>) {238 ^bb0(%in: f32, %in_7: f32, %out: f32):239 %12 = arith.addf %in, %in_7 : f32240 linalg.yield %12 : f32241 } -> tensor<?x?xf32>242 return %11 : tensor<?x?xf32>243}244 245#matmul_accesses = [246 affine_map<(m, n, k) -> (m, k)>,247 affine_map<(m, n, k) -> (k, n)>,248 affine_map<(m, n, k) -> (m, n)>249]250#matmul_trait = {251 iterator_types = ["parallel", "parallel", "reduction"],252 indexing_maps = #matmul_accesses253}254 255func.func @matmul_generic(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>) -> tensor<?x?xf32> {256 %cf0 = arith.constant 0.0 : f32 257 %ci0 = arith.constant 0 : index 258 %ci1 = arith.constant 1 : index 259 %d0 = tensor.dim %arg0, %ci0 : tensor<?x?xf32>260 %d1 = tensor.dim %arg1, %ci1 : tensor<?x?xf32>261 %splat = tensor.splat %cf0[%d0, %d1] : tensor<?x?xf32>262 %0 = linalg.generic #matmul_trait ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%splat : tensor<?x?xf32>) {263 ^bb0(%in: f32, %in_0: f32, %out: f32):264 %1 = arith.mulf %in, %in_0 : f32265 %2 = arith.addf %out, %1 : f32266 linalg.yield %2 : f32267 } -> tensor<?x?xf32>268 return %0 : tensor<?x?xf32>269}270 271func.func @matmul_named(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>) -> tensor<?x?xf32> {272 %cf0 = arith.constant 0.0 : f32 273 %ci0 = arith.constant 0 : index 274 %ci1 = arith.constant 1 : index 275 %d0 = tensor.dim %arg0, %ci0 : tensor<?x?xf32>276 %d1 = tensor.dim %arg1, %ci1 : tensor<?x?xf32>277 %splat = tensor.splat %cf0[%d0, %d1] : tensor<?x?xf32>278 %0 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%splat : tensor<?x?xf32>) -> tensor<?x?xf32>279 return %0 : tensor<?x?xf32>280}281 282#conv_trait = {283 indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0 * 3 + d2, d1 * 4 + d3)>, affine_map<(d0, d1, d2, d3) -> (d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1)>],284 iterator_types = ["parallel", "parallel", "reduction", "reduction"]285}286 287func.func @conv(%arg0: tensor<16x29xf32>, %arg1: tensor<3x4xf32>) -> (tensor<5x7xf32>) {288 %c0 = arith.constant 0.0 : f32 289 %splat = tensor.splat %c0 : tensor<5x7xf32>290 %result = linalg.generic #conv_trait ins(%arg0, %arg1 : tensor<16x29xf32>, tensor<3x4xf32>) outs(%splat : tensor<5x7xf32>) {291 ^bb0(%in: f32, %in_64: f32, %out: f32):292 %5 = arith.mulf %in, %in_64 : f32293 %6 = arith.addf %out, %5 : f32294 linalg.yield %6 : f32295 } -> tensor<5x7xf32>296 return %result : tensor<5x7xf32>297}298 299#reverse_trait = {300 indexing_maps = [301 affine_map<(i) -> (3 - i)>,302 affine_map<(i) -> (i)>303 ],304 iterator_types = ["parallel"]305}306 307func.func @reverse_from_3(%arg0: tensor<?xf32>) -> (tensor<?xf32>) {308 %cf0 = arith.constant 0.0 : f32 309 %ci0 = arith.constant 0 : index 310 %d0 = tensor.dim %arg0, %ci0 : tensor<?xf32>311 %splat = tensor.splat %cf0[%d0] : tensor<?xf32>312 %result = linalg.generic #reverse_trait ins(%arg0: tensor<?xf32>) outs(%splat: tensor<?xf32>) {313 ^bb0(%a: f32, %b: f32):314 linalg.yield %a : f32315 } -> tensor<?xf32>316 return %result : tensor<?xf32>317}318 319func.func @fill_empty_1d(%arg0: tensor<?xf32>) -> (tensor<?xf32>) {320 %c0 = arith.constant 0.0 : f32321 %0 = linalg.fill ins(%c0 : f32) outs(%arg0 : tensor<?xf32>) -> tensor<?xf32>322 return %0 : tensor<?xf32>323}324 325func.func @fill_empty_2d(%arg0: tensor<?x?xf32>) -> (tensor<?x?xf32>) {326 %c0 = arith.constant 0.0 : f32327 %0 = linalg.fill ins(%c0 : f32) outs(%arg0 : tensor<?x?xf32>) -> tensor<?x?xf32>328 return %0 : tensor<?x?xf32>329}330