llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir] Type mismatch error occurred when converting cf to llvm

Open JiaweiRen2022 opened this issue 4 years ago • 3 comments

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 withi32, 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"} : () -> ()
}) : () -> ()

JiaweiRen2022 avatar Sep 28 '22 08:09 JiaweiRen2022

@llvm/issue-subscribers-mlir

llvmbot avatar Sep 28 '22 13:09 llvmbot

Anyone who can reproduce this on current main? I'm unable to reproduce Test 1 since I get syntax errors now. Test 2 works on current main (0cb2906cdfb3480382828890e9ac4c076c17f3fa).

rikhuijzer avatar Jul 24 '23 12:07 rikhuijzer

I confirmed this is fixed by https://github.com/llvm/llvm-project/commit/448adfee05b737a26dda34e7ae2cd4948760fff0.

Men-cotton avatar Mar 27 '26 07:03 Men-cotton