<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/174580>174580</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR]Inconsistent Results When Using the `-affine-loop-fusion` Pass
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          BealZephyr
      </td>
    </tr>
</table>

<pre>
    **test commit**: [5cfd02](https://github.com/llvm/llvm-project/commit/5cfd02f44a43a2e2a085a633b022a62f64ba2b93)

## Description:
I observed an inconsistency in the results depending on whether the `-affine-loop-fusion` pass is used.

## test case
```
module {
 func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() {
 %0 = "tosa.const"() <{values = dense<4.000000e+00> : tensor<3x7x5xf32>}> : () -> tensor<3x7x5xf32>
    %1 = tosa.rsqrt %0 : (tensor<3x7x5xf32>) -> tensor<3x7x5xf32>
    %2 = tosa.reduce_sum %1 {axis = 0 : i32} : (tensor<3x7x5xf32>) -> tensor<1x7x5xf32>
    %3 = tosa.sigmoid %2 : (tensor<1x7x5xf32>) -> tensor<1x7x5xf32>
    %cast = tensor.cast %3 : tensor<1x7x5xf32> to tensor<*xf32>
    call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
    return
  }
}
```

## Command:
### 1. Without `-affine-loop-fusion` 
#### cmd:
```
mlir-opt test.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" | mlir-opt -linalg-fuse-elementwise-ops \
-one-shot-bufferize="bufferize-function-boundaries=1" -expand-strided-metadata  -convert-linalg-to-affine-loops \
-lower-affine -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:
```
[[[0.817574,    0.817574,    0.817574,    0.817574, 0.817574], ...]]]
```
### 2. With `-affine-loop-fusion`:
#### cmd:
```
mlir-opt test.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" | mlir-opt -linalg-fuse-elementwise-ops \
-one-shot-bufferize="bufferize-function-boundaries=1" -expand-strided-metadata  -convert-linalg-to-affine-loops \
-affine-loop-fusion  -lower-affine  -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:
```
[[[0.952574,    0.952574,    0.952574,    0.952574, 0.952574], ...]]]
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVk2v4ygW_TU3G4SFwY6TRRb5qEglTUmjkkYlzaaEzXXCDAY34Lz3-te3sJ28vKp01ate9aIj5Jivc86FczEyBH2yiBsod1AeFnKIZ-c3O5Tmv9ifX_yiduplA3wLfBsxRNK4rtNxagCxJVDuyqZVjEN5AL46x9gHEFvgR-DHk47noc4a1wE_GnO5_tHeu_9hE4Efr3jHCaUtClkIyZFLtirlUoiacS6XvF0WteT1WgBfA9umwgVwQQ4YGq_7qJ1NvGz7kbg6oL-gItISbRtngw4RbfNCtCXxjMRjGEwMRGGPVml7Is6SpzPGM_pxACwZlW2rLVLjXE_bIST8JSO9DIHoQIaAKnsjZFoeGTA1Ldlc2LZzajBIoNoB25J2sE2WHqT3-iIjEihY77WNn7Dz2B4FB76KaIPzIPbAt8-t4CA-THGTOwAoWCe1Bb4Cvr7iAy8ZAXEgwHl0QWYp_AicX4eJPVS7izQDhnGcQhsQxL7I2PhD4DvGQHwgaXtvQsRz9VxepVSHa_-MSlP98dikmSRZ-Ug3avLhNx-vUkeQx3PficzvkFENDX4NQzdTVjv5rKdIJzItOFSHX-TNH_KKV96gT53T6qrmLXb-F7AbGeIEP47LpvrEuX08mURHHhlnwmykMQ-9NpNN5ngr_I37JtnThs-YHuPg7VhJnmDb-Xnn_tf82Luuk1ZNSTq1peY8I190PLsh_iDr7mekSU13xblPNKM9dX0cMzFLN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