<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58034>58034</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Type mismatch error occurred when converting cf to llvm
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
peanutRen
</td>
</tr>
</table>
<pre>
Hello,
I think I just encountered a bug. This bug is related to the `cf` dialect when converting the `cf` dialect to the `llvm` dialect; please see the attached test case and execution command.
During the `cf` dialect lowering, `%c0 = arith.constant 0 : index` will be converted to `%0 = llvm.mlir.constant(0 : index) : i64`. However, due to failing to convert `func.func` to `llvm.func`, the input parameter type of the basic block in `^bb1(%2: index)` is not changed from `index` convert to `i64`. So there is a type mismatch problem.
It seems that this bug is related to the return value type. If you remove the return value or replace the return value with `memref` or `vector` type, there is no problem. In addition, if you replace the type `index` of %c0 with`i32`, you will not see this error.
So the problem just occurs when the `FuncOp` has return value with `tensor` type, and the input parameter type of the basic block is `index` type.
**version**
```
llvm-project-llvmorg-15.0.0
```
**Operation System**
```
Ubuntu version: 16.04
Linux Kernel version: 4.15.0-142-generic
```
**test 1**
On the official test case `/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir`, we use this command line :
`mlir-opt tosa-to-linalg.mlir -pass-pipeline="func.func(tosa-to-linalg)" -tosa-to-arith -linalg-bufferize -convert-linalg-to-loops -convert-scf-to-cf -convert-arith-to-llvm -convert-cf-to-llvm`.
Many similar type mismatch errors are like:
```
within split at tosa-to-linalg.mlir:102 offset :15:8: note: see current operation: "llvm.br"(%1)[^bb1] : (i64) -> ()
within split at tosa-to-linalg.mlir:120 offset :17:8: error: type mismatch for bb argument #0 of successor #0
%0 = "tosa.add"(%arg0, %arg1) : (tensor<1x3xf32>, tensor<2x1xf32>) -> tensor<2x3xf32>
^
within split at tosa-to-linalg.mlir:120 offset :17:8: note: see current operation: "llvm.br"(%2)[^bb1] : (i64) -> ()
// -----
within split at tosa-to-linalg.mlir:140 offset :7:8: error: type mismatch for bb argument #0 of successor #0
%0 = "tosa.tanh"(%arg0) : (tensor<1xf32>) -> tensor<1xf32>
^
within split at tosa-to-linalg.mlir:140 offset :7:8: note: see current operation: "llvm.br"(%1)[^bb1] : (i64) -> ()
// -----
within split at tosa-to-linalg.mlir:267 offset :8:8: error: type mismatch for bb argument #0 of successor #0
%0 = "tosa.cast"(%arg0) : (tensor<1xf16>) -> tensor<1xf32>
^
within split at tosa-to-linalg.mlir:267 offset :8:8: note: see current operation: "llvm.br"(%1)[^bb1] : (i64) -> ()
within split at tosa-to-linalg.mlir:279 offset :9:8: error: type mismatch for bb argument #0 of successor #0
// -----
// -----
%0 = "tosa.mul"(%arg0, %arg0) {shift = 0 : i32} : (tensor<1xi16>, tensor<1xi16>) -> tensor<1xi32>
^
```
**test 2**
We further tested and found that the problem just occurs when converting `cf dialect` to `llvm dialect`.
The following code is rejected by `mlir-opt --convert-cf-to-llvm`:
```
module {
func.func @test() -> tensor<2xi32> {
%c0 = arith.constant 0 : index
cf.br ^bb1(%c0 : index)
^bb1(%0: index):
%cst = arith.constant dense<0> : tensor<2xi32>
return %cst : tensor<2xi32>
}
}
```
And get this error message:
```
cf.mlir:4:5: error: type mismatch for bb argument #0 of successor #0
cf.br ^bb1(%c0 : index)
^
cf.mlir:4:5: note: see current operation: "llvm.br"(%1)[^bb1] : (i64) -> ()
// -----// IR Dump After ConvertControlFlowToLLVM Failed (convert-cf-to-llvm) //----- //
mlir-asm-printer: Verifying operation: builtin.module
type mismatch for bb argument #0 of successor #0
mlir-asm-printer: 'builtin.module' failed to verify and will be printed in generic form
ImplicitTypeIDRegistry::lookupOrInsert(mlir::detail::StorageUserTrait::IsMutable<Empty>)
ImplicitTypeIDRegistry::lookupOrInsert(mlir::SubElementTypeInterface::Trait<Empty>)
ImplicitTypeIDRegistry::lookupOrInsert(mlir::ShapedType::Trait<Empty>)
"builtin.module"() ({
"func.func"() ({
%0 = "arith.constant"() {value = 0 : index} : () -> index
%1 = "builtin.unrealized_conversion_cast"(%0) : (index) -> i64
"llvm.br"(%1)[^bb1] : (i64) -> ()
^bb1(%2: index): // pred: ^bb0
%3 = "arith.constant"() {value = dense<0> : tensor<2xi32>} : () -> tensor<2xi32>
"func.return"(%3) : (tensor<2xi32>) -> ()
}) {function_type = () -> tensor<2xi32>, sym_name = "test"} : () -> ()
}) : () -> ()
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNWUtv2zgQ_jX2hbAhU34efEjjBBvsFgXadPdYUNLI5lYSBZJK4v31O0NKshTLadN2gy1cPUgO5_1xRolUctz-BlmmRvx6FOxGwZW_3jF7kMVXdsf-roxlUMSqKixoSJhgUbWfsvuDNPTE8KYhExanrEIyYKNlEKd4YYkUGcSWPR6gYLEqHkBbWeyHF52Is-wh78yMwneszEAYYAbALRLWivhAHAGli2lKFAmDJ4grKxUxy3McmbKuUrtKX-SeqUegWbQDTY74Ig7YKNwxoaU9TFF4Y0VhGQ1eMVkk8ETkjzLLWASNct4Gnt6Tky7TPJO63WLE191N-Ma_LOdINmW_oRy4E4mRVEC7pUJmTmzVcCEGaVXEU7qQFJ6nY1UPET3pKYuysqwUWuSA7mP2WAJTqZuLhJExizIVf8V1TurFTRTNUEAUn3dFJCbo50KhsQ-i2KOeqVY50bSmaITzwjT6fHJe1UDkwrPPpcmFjQ-s1CrKIJ_2As-Sk3ODVMJSEF4KMg220gV7EBmZCfedsruUHVWFM7l6gPNFSuN7mYl4YO4RnUxi55BrcIGBq_H2gLGhtLMxsqit6rUpVKsAuyuYSBJJoUdrZCPIiZvTvGsudIIPMmJNEyGv_UakLq7I3D7ikR1orXTPVN60jRA-UVUcV9r4hKvj_BYj4kNJLA_CDGttoTB9LSmbXhU_pqec80dX1t6V0w9jxThzuXc_gfr7n3uleJ6gdn-jDyb0ovR-MltMg2kwuN5v9aEELRwGfDoaC_kLDD5HiGkVG13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