<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/118797>118797</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Inconsistent output when executing MLIR program with and without `-linalg-fuse-elementwise-ops`
</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: adf892d743d
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`.
The outputs are also inconsistent when running multiple times with the `-linalg-fuse-elementwise-ops`
## Steps to Reproduce:
### 1. **MLIR Program (a.mlir)**:
a.mlir:
```
module {
func.func private @printMemrefI32(tensor<*xi32>)
func.func private @printMemrefF32(tensor<*xf32>)
func.func @main() {
%0 = "tosa.const"() <{value = dense<1641> : tensor<1x15x2xi32>}> : () -> tensor<1x15x2xi32>
%1 = "tosa.const"() <{value = dense<3819> : tensor<1x15x2xi32>}> : () -> tensor<1x15x2xi32>
%2 = tosa.abs %0 : (tensor<1x15x2xi32>) -> tensor<1x15x2xi32>
%3 = tosa.logical_right_shift %2, %1 : (tensor<1x15x2xi32>, tensor<1x15x2xi32>) -> tensor<1x15x2xi32>
%4 = tosa.reduce_sum %3 {axis = 1 : i32} : (tensor<1x15x2xi32>) -> tensor<1x1x2xi32>
%5 = tosa.cast %4 : (tensor<1x1x2xi32>) -> tensor<1x1x2xf32>
%cast = tensor.cast %5 : tensor<1x1x2xf32> to tensor<*xf32>
call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
return
}
}
```
### 2. **Command to Run without `-linalg-fuse-elementwise-ops`:**
```
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-parallel-loops -convert-scf-to-cf -convert-arith-to-llvm -expand-strided-metadata -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | /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 `-linalg-fuse-elementwise-ops`:**:
```
[[[0, 0]]]
```
### 4. **Command to Run With `-linalg-fuse-elementwise-ops`:**
```
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith --linalg-fuse-elementwise-ops -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-parallel-loops -convert-scf-to-cf -convert-arith-to-llvm -expand-strided-metadata -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | /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 `-linalg-fuse-elementwise-ops`:**
```
[[[1.63388e+09, 109440]]]
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWNtu27oS_Rr6haBAkZJlPfjBiWsgQItz0PbgPAaUNLK5QZECL67Tr98g5UvSOE13LxsoUEBQIntmrTUjcpG0cE5uNcASlTeoXM9E8Dtjl28Gq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