<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/132155>132155</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[MLIR] Inconsistent output when executing MLIR program with and without `-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>
Lambor24
</td>
</tr>
</table>
<pre>
My git version is [6003c30](https://github.com/llvm/llvm-project/commit/6003c3055a4630be31cc3d459cdbb88248a007b9).
## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without the `-affine-loop-fusion`.
## Steps to Reproduce:
### 1. **MLIR Program (test.mlir)**:
test.mlir:
```
module {
func.func private @printMemrefF32(tensor<*xf32>)
func.func @main() {
%0 = "tosa.const"() <{values = dense<2.000000e+00> : tensor<1x3x9x4xf32>}> : () -> tensor<1x3x9x4xf32>
%1 = "tosa.const"() <{values = dense<3.000000e+00> : tensor<12x2x5x4xf32>}> : () -> tensor<12x2x5x4xf32>
%2 = "tosa.const"() <{values = dense<4.000000e+00> : tensor<12xf32>}> : () -> tensor<12xf32>
%3 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
%4 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
%5 = tosa.conv2d %0, %1, %2, %3, %4 {acc_type = f32, dilation = array<i64: 1, 2>, pad = array<i64: 1, 1, 1, 1>, stride = array<i64: 1, 1>} : (tensor<1x3x9x4xf32>, tensor<12x2x5x4xf32>, tensor<12xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x4x3x12xf32>
%6 = tosa.reduce_sum %5 {axis = 1 : i32} : (tensor<1x4x3x12xf32>) -> tensor<1x1x3x12xf32>
%cast = tensor.cast %6 : tensor<1x1x3x12xf32> to tensor<*xf32>
call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
return
}
}
```
### 2. **Command to Run Without `-affine-loop-fusion`:**
```
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -pass-pipeline='builtin.module(func.func(tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so
```
### 3. **Output Without `-affine-loop-fusion`:**
```
[[[[592, 592, 592, 592, 592, 592, 592, 592, 592, 592, 592, 592],
[736, 736, 736, 736, 736, 736, 736, 736, 736, 736, 736, 736],
[592, 592, 592, 592, 592, 592, 592, 592, 592, 592, 592, 592]]]]
```
### 4. **Command to Run With `-affine-loop-fusion`:**
```
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -pass-pipeline='builtin.module(func.func(tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops -affine-loop-fusion -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so
```
### 5. **Output With `-affine-loop-fusion`:**
```
[[[[16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16],
[16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16],
[16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16]]]]
```
I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused this result.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWE2P4jgQ_TXmUgoy5STAgQMNg9TStHY1e9hjy0kK8CqxI9uhYX79ykmA6W7onv4YaaUdFMWO81z1yh_1gqVzaqOJZiy5YclyIBu_NXb2VVaZsRgPMlMcZncH2CgPO7JOGQ3KAUtuUs5FLjhLlgwnW-9rx8Sc4YrhaqP8tsmGuakYrspydyyi2pp_KPcMV7mpKhUqvZkkkXEqeEZilOeiiJNpXmTZZILxRHI-zqYMp0PG5-FCwVDAklxuVe2V0cExn9-CrID2NVlFOld6A1KD0rnRTjlP2oMl15QeHrakgfaUNz6g_JbAyYrg7uvtN6it2VhZwYPyW5C6aCum8S2MpTyS67XSFJXG1NG6CQPCUv6Y2l-eagfewDeqrSmanDqGJ0QAjYbAcM5w3rr9s3fLcOLJ-WFVKstw2iFOvc-vTvZS3l98XpmiKQnY-IbxOcC60fkw3KC2aic9AYt5bZX2d1RZWq8Ett60M5aJBcP5fi2QiS_B72MDLOaVVJrhhOH05ACAYcKBiSUwRG-cHIbB9gzxiBQLNr7ZybIh1-IK0o6YWOCQtz9ieMM5E1-AiTmcuIz2Yj_dx0c-4-UR0duNwvM19Ina6F3UxCvUcI_75Oe5PYHzgEvwXcziV5n9NKenQyXeRYi_Nos_O39P6cT_LTpJ6-bIZYdFu_AZLtpV1pfYl6Iv47BPZJ7f-0NNrYFgFhdQqFKGrNW2SWvlgYmFSuNAqDXW7cEF1LK4ivnx1qGdt6qgFzq0kR_DvrZ5cHF97T55d6n15cbnIx3vxf7CckzPA24p5M9711T9TIxv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