How to lower linalg.generic to affine loops

Hi everyone!
I’m trying a conversion procedure tosa → linalg → loops for further optimization, the original mlir is:
func.func @transpose(%arg0: tensor<16x32x64xf32>) → tensor<32x64x16xf32> {
%perms = “tosa.const”() {value = dense<[1, 2, 0]> : tensor<3xi32>} : () → tensor<3xi32>
%0 = “tosa.transpose”(%arg0, %perms) : (tensor<16x32x64xf32>, tensor<3xi32>) → tensor<32x64x16xf32>
return %0 : tensor<32x64x16xf32>
}

after implement
mlir-opt --pass-pipeline=“builtin.module(func.func(tosa-to-linalg, tosa-to-arith))”
it turns out:
#map = affine_map<(d0, d1, d2) → (d2, d0, d1)>
#map1 = affine_map<(d0, d1, d2) → (d0, d1, d2)>
module {
func.func @transpose(%arg0: tensor<16x32x64xf32>) → tensor<32x64x16xf32> {
%cst = arith.constant dense<[1, 2, 0]> : tensor<3xi32>
%0 = tensor.empty() : tensor<32x64x16xf32>
%1 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = [“parallel”, “parallel”, “parallel”]} ins(%arg0 : tensor<16x32x64xf32>) outs(%0 : tensor<32x64x16xf32>) {
^bb0(%in: f32, %out: f32):
linalg.yield %in : f32
} → tensor<32x64x16xf32>
return %1 : tensor<32x64x16xf32>
}
}

I want to further lower it to affine.loops format, so type
mlir-opt --convert-linalg-to-affine-loops
but get an error:
mlir::FailureOr<llvm::SmallVector<mlir::Operation*, 4> > linalgOpToLoopsImpl(mlir::PatternRewriter&, mlir::linalg::LinalgOp) [with LoopTy = mlir::AffineForOp]: Assertion `linalgOp.hasBufferSemantics() && “expected linalg op with buffer semantics”’ failed.

Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var LLVM_SYMBOLIZER_PATH to point to it):
0 mlir-opt 0x000056153249c85a
1 mlir-opt 0x0000561532499e6c
2 libpthread.so.0 0x00007fb62c9fe980
3 libc.so.6 0x00007fb62b6afe87 gsignal + 199
4 libc.so.6 0x00007fb62b6b17f1 abort + 321
5 libc.so.6 0x00007fb62b6a13fa
6 libc.so.6 0x00007fb62b6a1472
7 mlir-opt 0x0000561532a92f41
8 mlir-opt 0x0000561532a92fd8
9 mlir-opt 0x0000561535d5a78d
10 mlir-opt 0x0000561533c10adb
11 mlir-opt 0x0000561532a8a124
12 mlir-opt 0x0000561532a8ad96
13 mlir-opt 0x0000561533b85839
14 mlir-opt 0x0000561533b85b40
15 mlir-opt 0x0000561533b85e80
16 mlir-opt 0x0000561533b84985
17 mlir-opt 0x0000561533b857a2
18 mlir-opt 0x0000561533b865c9
19 mlir-opt 0x0000561533b86cfd
20 mlir-opt 0x0000561533b78aab
21 mlir-opt 0x0000561533b791a8
22 mlir-opt 0x0000561533b795af
23 mlir-opt 0x0000561533c3b3b0
24 mlir-opt 0x0000561533b77966
25 mlir-opt 0x0000561533b799eb
26 mlir-opt 0x0000561532402a20
27 libc.so.6 0x00007fb62b692c87 __libc_start_main + 231
28 mlir-opt 0x000056153247a52a
Aborted

So what is the correct way to lower linalg.generic to affine loops ?
Thanks!

You need to bufferize before lowering to affine loops. See Bufferization - MLIR.