Tosa.Mul i32 shift incorrect result

Hello,
Got a simple example of a Tosa.Mul i32 with right shift. The inputs are constants so the result gets computed directly :

$ mlir-opt test.mlir --canonicalize -mlir-print-ir-before-all

// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
module {
  func.func @main() -> tensor<*xi32> {
    %0 = "tosa.const"() <{values = dense<-23661> : tensor<i32>}> : () -> tensor<i32>
    %1 = "tosa.const"() <{values = dense<-33022> : tensor<i32>}> : () -> tensor<i32>
    %2 = "tosa.const"() <{values = dense<30> : tensor<1xi8>}> : () -> tensor<1xi8>
    %3 = tosa.mul %0, %1, %2 : (tensor<i32>, tensor<i32>, tensor<1xi8>) -> tensor<i32>
    %cast = tensor.cast %3 : tensor<i32> to tensor<*xi32>
    return %cast : tensor<*xi32>
  }
}


module {
  func.func @main() -> tensor<*xi32> {
    %0 = "tosa.const"() <{values = dense<0> : tensor<i32>}> : () -> tensor<i32>
    %cast = tensor.cast %0 : tensor<i32> to tensor<*xi32>
    return %cast : tensor<*xi32>
  }
}

So it determined the result to be 0, but actually the expected result is 1.
In Tosa spec Mul should add a rounding bit when doing i32 shift:

 -23661 * -33022 = 781333542
781333542 + (1<<(30-1)) = 781333542 + 536870912 = 1318204454
1318204454 >> 30 = 1

Expected result has been checked with Tosa reference model.
Using LLVM b9891715af7ab055ccb7ad424aefe54c67d77988.

Thanks

Hi @OMaghiarIMG, thanks for reporting this issue, I’ve created a fix here: [mlir][tosa] Fix mul folder conformance to the spec by lhutton1 · Pull Request #137601 · llvm/llvm-project · GitHub

2 Likes