[Question] How to properly lower MLIR to LLVM-IR?

Hello all,

I am new to MLIR and trying to figure out the proper set of passes to lower MLIR in TOSA dialect to LLVM-IR, so that I can then execute it via mlir-cpu-runner.

For example, I have the following MLIR input:

module  {
  func @main(%arg0: tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32> {
    %0 = "tosa.clamp"(%arg0) {max_fp = 3.40282347E+38 : f32, max_int = 2147483647 : i64, min_fp = 0.000000e+00 : f32, min_int = 0 : i64} : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32>
    return %0 : tensor<1x2x2x3xf32>
  }
}

I used the following pipeline pass:

mlir-opt network.mlir --pass-pipeline="func.func(tosa-infer-shapes, tosa-to-linalg-named, tosa-to-linalg, canonicalize, arith-expand, canonicalize)" --one-shot-bufferize="allow-return-allocs allow-unknown-ops create-deallocs=0" --func-bufferize --pass-pipeline="func.func(canonicalize, convert-linalg-to-affine-loops, affine-loop-fusion, lower-affine, finalizing-bufferize, buffer-deallocation, convert-scf-to-cf, convert-math-to-llvm)" --convert-arith-to-llvm --convert-vector-to-llvm --convert-cf-to-llvm --convert-memref-to-llvm --convert-func-to-llvm --canonicalize --reconcile-unrealized-casts --canonicalize --llvm-legalize-for-export > network-llvm.mlir

With that the conversion process fails after --convert-cf-to-llvm with

network.mlir:3:10: error: type mismatch for bb argument #0 of successor #0 
%0 = "tosa.clamp"(%arg0) {max_fp = 3.40282347E+38 : f32, max_int = 2147483647 : i64, min_fp = 0.000000e+00 : f32, min_int = 0 : i64} : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32>                                                                  ^                                                                                                              
network.mlir:3:10: note: see current operation: "llvm.br"(%3)[^bb1] : (i64) -> ()

My guess is that the ordering of the passes I am using is incorrect. Any suggestions on what the correct ordering should be?

Thanks!

Run --convert-func-to-llvm before --convert-cf-to-llvm.

I’d consider this a bug though, the order of lowering passes shouldn’t matter

That solved the issue. Created [mlir] Ordering of lowering passes to convert different dialects to llvm may lead to conversion failute · Issue #55028 · llvm/llvm-project · GitHub for tracking the issue. Thanks for the help.