Issue 174550
Summary [MLIR] `-linalg-generalize-named-ops -linalg-specialize-generic-ops` remove `cast_unsigned` in linalg.matmul, leading to Inconsistent Results
Labels mlir
Assignees
Reporter BealZephyr
    **test commit**: [5cfd02](https://github.com/llvm/llvm-project/commit/5cfd02f44a43a2e2a085a633b022a62f64ba2b93)

## Description:

With `cast_unsigned`, the values are first converted to unsigned integers:
   The tensor values `-2` in `i16` are interpreted as `65534` in `uint16`.
    The matrix multiplication is performed using these unsigned values:
     `65534 * 3 = 196602`
     `196602 * 2 = 393204`
So, the resulting matrix will be filled with the value `393204`.

Adding` -linalg-generalize-named-ops and -linalg-specialize-generic-ops`, the effect of cast_unsigned seems to be bypassed, and the operation uses the original signed values, leading to the result -12

## test case
```
module {
 func.func private @printMemrefI32(tensor<*xi32>)
  func.func @main() {
 %0 = "tosa.const"() <{values = dense<-2> : tensor<4x2xi16>}> : () -> tensor<4x2xi16>
    %1 = "tosa.const"() <{values = dense<3> : tensor<2x8xi64>}> : () -> tensor<2x8xi64>
    %2 = "tosa.const"() <{values = dense<0> : tensor<4x8xi32>}> : () -> tensor<4x8xi32>
    %3 = linalg.matmul {cast = #linalg.type_fn<cast_unsigned>} ins(%0, %1 : tensor<4x2xi16>, tensor<2x8xi64>) outs(%2 : tensor<4x8xi32>) -> tensor<4x8xi32>
    %cast = tensor.cast %3 : tensor<4x8xi32> to tensor<*xi32>
    call @printMemrefI32(%cast) : (tensor<*xi32>) -> ()
 return
  }
}
```

## Command:
### 1. without `-linalg-generalize-named-ops  -linalg-specialize-generic-ops` 
#### cmd
```
mlir-opt test.mlir -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries=1"  -convert-scf-to-cf -convert-linalg-to-loops  \
-convert-scf-to-cf  -convert-to-llvm | mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project-latest/build/lib/libmlir_runner_utils.so
```

#### output:
```
[[393204,   393204,   393204,   393204,   393204,   393204, 393204,   393204], 
 [393204,   393204,   393204,   393204,   393204, 393204,   393204,   393204], 
 [393204,   393204,   393204,   393204, 393204,   393204,   393204,   393204], 
 [393204,   393204,   393204, 393204,   393204,   393204,   393204,   393204]]
```
### 2. With `-linalg-generalize-named-ops  -linalg-specialize-generic-ops` 
#### cmd
```
mlir-opt test.mlir -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries=1"  -convert-scf-to-cf -linalg-generalize-named-ops \
 -linalg-specialize-generic-ops -convert-linalg-to-loops  -convert-scf-to-cf  -convert-to-llvm |  mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project-latest/build/lib/libmlir_runner_utils.so
```
#### output:
```
[[-12,   -12,   -12,   -12,   -12,   -12,   -12,   -12], 
 [-12,   -12,   -12,   -12,   -12,   -12,   -12,   -12], 
 [-12,   -12, -12,   -12,   -12,   -12,   -12,   -12], 
 [-12,   -12,   -12,   -12,   -12, -12,   -12,   -12]
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to