Failed to legalize operation 'linalg.map' when lowering tosa.pad

Hi All,

I’m new to MLIR, so I might well be doing something silly or non-sensical.

What I have is the following MLIR:

module {
  func.func @global_tosa_pad_kernel(%arg0: tensor<3x3xf32>, %arg1: tensor<2x2xi32>, %arg2: tensor<f32>) -> (tensor<5x5xf32>) {
    %0 = "tosa.pad"(%arg0, %arg1, %arg2) {} : (tensor<3x3xf32>, tensor<2x2xi32>, tensor<f32>) -> (tensor<5x5xf32>)
    return %0 : tensor<5x5xf32>
  }
}

and from the command line, I’m running:
mlir-opt --tosa-to-tensor --tensor-bufferize --func-bufferize --buffer-results-to-out-params --finalizing-bufferize test.mlir -o test.out

The passes I’m using above are part of a larger pass pipeline that should eventually lower the function to SPIR-V.

I’m getting the following error when the FinalizingBufferize pass is run:

./test.mlir:3:10: error: failed to legalize operation 'linalg.map'
    %0 = "tosa.pad"(%arg0, %arg1, %arg2) {} : (tensor<3x3xf32>, tensor<2x2xi32>, tensor<f32>) -> (tensor<5x5xf32>)
         ^
./bin/test.mlir:3:10: note: see current operation: 
%24 = "linalg.map"(%23) ({
  %29 = "linalg.index"() <{dim = 0 : i64}> : () -> index loc("./bin/test.mlir":3:10)
  %30 = "linalg.index"() <{dim = 1 : i64}> : () -> index loc("./bin/test.mlir":3:10)
  "linalg.yield"(%9) : (f32) -> () loc("./bin/test.mlir":3:10)
}) : (tensor<5x5xf32>) -> tensor<5x5xf32> loc("./bin/test.mlir":3:10)

Am I missing a pass here?

Any help would be appreciated.

Thanks,
Grant

Hi @grant-arm,

You could try with one-shot bufferization (Bufferization - MLIR) instead of using partial bufferizations (i.e., tensor-bufferize). For example, mlir-opt your_exmaple.mlir --tosa-to-tensor -one-shot-bufferize="bufferize-function-boundaries allow-return-allocs".

I suggest to switch on one-shot-bufferize and drop the partial bufferization passes.

Hi @chelini ,

Thank you for your reply.

Using one-shot bufferization with mlir-opt does indeed seem to solve my problem.

I’ll try replacing the partial bufferizations in my pass pipeline with the one-shot bufferization.

Thanks for your help.

Regards
Grant