<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/119683>119683</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Inconsistent output when executing MLIR program with `--linalg-fuse-elementwise-ops` `linalg-fold-unit-extent-dims` `--affine-loop-fusion`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Emilyaxe
</td>
</tr>
</table>
<pre>
git version: 953838dceaf
system: `Ubuntu 18.04.6 LTS`
## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without `--linalg-fuse-elementwise-ops` `linalg-fold-unit-extent-dims` `--affine-loop-fusion` .
The output also varies when running the process with optimizations multiple times.
The output becomes correct when any of these three options is removed, so I'm unsure which optimization contains the bug.
## Steps to Reproduce:
### 1. **MLIR Program (tosa.mlir)**:
tosa.mlir:
```
module {
func.func private @printMemrefI32(tensor<*xi32>)
func.func private @printMemrefF32(tensor<*xf32>)
func.func @main() {
%1 = "tosa.const"() <{value = dense<-7982> : tensor<1x22x10xi32>}> : () -> tensor<1x22x10xi32>
%73 = tosa.argmax %1 {axis = 1 : i32} : (tensor<1x22x10xi32>) -> tensor<1x10xi32>
%88 = tosa.abs %73 : (tensor<1x10xi32>) -> tensor<1x10xi32>
%89 = tosa.clamp %88 {max_fp = 1.600000e+01 : f32, max_int = 16 : i64, min_fp = 0.000000e+00 : f32, min_int = 0 : i64} : (tensor<1x10xi32>) -> tensor<1x10xi32>
%90 = tosa.arithmetic_right_shift %73, %89 {round = true} : (tensor<1x10xi32>, tensor<1x10xi32>) -> tensor<1x10xi32>
%cast = tensor.cast %90 : tensor<1x10xi32> to tensor<*xi32>
call @printMemrefI32(%cast) : (tensor<*xi32>) -> ()
return
}
}
```
### 2. **Command to Run without optimizations :**
```
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -finalize-memref-to-llvm -convert-linalg-to-affine-loops --expand-strided-metadata -convert-vector-to-llvm -convert-arith-to-llvm -lower-affine -convert-math-to-llvm -finalize-memref-to-llvm -convert-scf-to-cf -convert-func-to-llvm -reconcile-unrealized-casts | timeout 10 /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_c_runner_utils.so --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_runner_utils.so --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_async_runtime.so
```
### 3. **Output without optimizations :**:
```
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
```
### 4. **Command to Run with optimizations :**
```
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith --linalg-fuse-elementwise-ops --linalg-fold-unit-extent-dims="use-rank-reducing-slices" -one-shot-bufferize="bufferize-function-boundaries" -finalize-memref-to-llvm -convert-linalg-to-affine-loops --affine-loop-fusion --expand-strided-metadata -convert-vector-to-llvm -convert-arith-to-llvm -lower-affine -convert-math-to-llvm -finalize-memref-to-llvm -convert-scf-to-cf -convert-func-to-llvm -reconcile-unrealized-casts | timeout 10 /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_c_runner_utils.so --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_runner_utils.so --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_async_runtime.so
```
### 5. **Output with optimizations :**
```
[[-17909, -17909, -17909, -17909, -17909, -17909, -17909, -17909, -17909, -17909]]
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWFtv6zYS_jX0y4ACTfmmBz84cQ0EaLGLtvscUNTI5i5FCrw4zvn1C1Ky4yROT862Z_tygsC6cOabbzjkR4rCe7U3iGsyvyPz7UTEcLBu_VOn9LM44aS2zfN6rwIc0XllDSk3UM3LVblqJIqWsA1hG__sA3apiSzYv-poQoTpqmCzYgE___4bWbDBjvCS8BK26KVTfRjgCNs8gOgATz06hUYqswdhQBlpjVc-oAng0Ecd4OmABvCEMoZkFQ4IXnQIv_z88Cv0zu6d6OBJhQMI0-QbG0MiRalWRug9baNHiho7NOFJeaS292TBks3ZwuqGRqMCxVOKTRvVnU0oFW2rDFJtbZ-wUgoLBgVhm98PCDaGPgYQ2ls4CqfQD5RdNOZMuHdWovcDTdsH1akvIvWFhy7qoHqNEFSH_g1ojdJ26EFa51COfSHMM9g24XqEcHCIGTKBKQ8OO3vEhvB78BYeCF92EI2PDuHpoOTr8CCtCUIZn1nWcV-8KtpvAXsPwcKv2DvbRIlD7a6Nkt20AMI3hG9yTf451oTwVbBeFJ1WjvBqsBgAXt6n8cM2abTk__TQ2SZqBLK8I2wD0EYji_QDvVNHERDIjPVOmfALdg7bh5KnSGi8daS8J3xzUiUn5U8p5mcAdu8B2g8AyIx1QhnCV4RXF4YAhM-nQMotEM5zbmkYB8L52bK8J8u7o9ARs1mDxiMp7-myWqVIkPrhwmB64vw0Zec0ltuzxYhG0_NH1hdCyzKHynSE23fiNNJc3omT8rlxmmGT53J7jvAR8I3AN6KuVldRa38h8hb6m3GrF1ypRdefgy3vOnF6bPshn2LB0h8SfseG5FIp-T0kI2XCYLUY0l7McosyZ3dWsBd39spdmYs7u3jf7LRvzaxi13VS4dBhUPLRqf0hPPqDasPQiYnG2BHLO2ejaQY_F_GrRO5vc_gcQSn8kPhgVgzPI_HN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