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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Inconsistent output when executing MLIR program with `--test-loop-fusion="test-loop-fusion-transformation"`
        </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: 953838dceaff

system: `Ubuntu 18.04.6 LTS`

## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without `--test-loop-fusion="test-loop-fusion-transformation"`. 


## 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 @entry(%arg0: index) -> tensor<1x2xi32> {
    %11 = "tosa.const"() <{value = dense<0> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
    %12 = tosa.while_loop (%arg1 = %11) : (tensor<1x2x2xi32>) -> tensor<1x2x2xi32> {
      %51 = "tosa.const"() <{value = dense<6> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
      %52 = tosa.greater %51, %arg1 : (tensor<1x2x2xi32>, tensor<1x2x2xi32>) -> tensor<1x2x2xi1>
      %extracted = tensor.extract %52[%arg0, %arg0, %arg0] : tensor<1x2x2xi1>
      %from_elements = tensor.from_elements %extracted : tensor<i1>
      tosa.yield %from_elements : tensor<i1>
    } do {
    ^bb0(%arg1: tensor<1x2x2xi32>):
      %51 = "tosa.const"() <{value = dense<1> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
      %52 = tosa.add %arg1, %51 : (tensor<1x2x2xi32>, tensor<1x2x2xi32>) -> tensor<1x2x2xi32>
      tosa.yield %52 : tensor<1x2x2xi32>
    }
    %50 = tosa.argmax %12 {axis = 1 : i32} : (tensor<1x2x2xi32>) -> tensor<1x2xi32>
    return %50 : tensor<1x2xi32>
  }
  func.func @main() {
    %idx0 = index.constant 0
    %0 = call @entry(%idx0) : (index) -> tensor<1x2xi32>
    %cast = tensor.cast %0 : tensor<1x2xi32> to tensor<*xi32>
    call @printMemrefI32(%cast) : (tensor<*xi32>) -> ()
    return
  }
}

``` 

 ### 2. **Command to Run without  `--test-loop-fusion` :**

``` 
/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -tosa-to-scf \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))"  \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-arith  -convert-scf-to-cf   -convert-arith-to-llvm   \
-convert-linalg-to-loops   -convert-linalg-to-parallel-loops  -convert-linalg-to-loops  -one-shot-bufferize="bufferize-function-boundaries" \
--expand-strided-metadata   -convert-linalg-to-affine-loops   -finalize-memref-to-llvm   -lower-affine  -convert-scf-to-cf   \
-convert-cf-to-llvm   -finalize-memref-to-llvm  -convert-func-to-llvm   -convert-index-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   `--test-loop-fusion` :**:

``` 
[[0,   0]]

``` 

### 4. **Command to Run with  `--test-loop-fusion`  :**


``` 
/data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt /data/szy/workspace/mlir-inconsistent/a.mlir -tosa-to-scf \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))"  \
| /data/szy/MLIR/llvm-release/llvm-project/install/mlir-opt -tosa-to-arith  -convert-scf-to-cf   -convert-arith-to-llvm   \
-convert-linalg-to-loops   -convert-linalg-to-parallel-loops  -convert-linalg-to-loops  -one-shot-bufferize="bufferize-function-boundaries" \
--expand-strided-metadata   -convert-linalg-to-affine-loops   -finalize-memref-to-llvm  --test-loop-fusion="test-loop-fusion-transformation" \ -lower-affine  -convert-scf-to-cf   -convert-cf-to-llvm   -finalize-memref-to-llvm  -convert-func-to-llvm   -convert-index-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  `--test-loop-fusion` :**

``` 
[[1,   1]]
``` 

### 6. **Analysis for this case :**

As some developers have discussed in issue https://github.com/llvm/llvm-project/issues/118631, the test-pass should also preserve the semantics of MLIR.

This MLIR program is expected to correctly output [0, 0] for `%50 = tosa.argmax %12`, given that all elements in` %12` are equal. However, instead of the expected result, it incorrectly outputs [1, 1], which corresponds to the last index of` %12`.

To debug this issue, I printed the IR after each pass and found that the input IR [input.txt](https://github.com/user-attachments/files/19105934/input.txt) is correct before applying the `--test-loop-fusion="test-loop-fusion-transformation"` pass. As shown in the first image, %173 is initialized before the three-level computation (lines 221–237). However, after running `--test-loop-fusion="test-loop-fusion-transformation"`, %173 is instead initialized inside the innermost loop, which causes it to overwrite the assigned value at line 253 in the second image [output.txt](https://github.com/user-attachments/files/19105936/output.txt).
Additionally, I try the `affine-loop-fusion` pass on the same input IR. As shown in the third image, %173 should be initialized in the second loop, leading to the correct result.

![Image](https://github.com/user-attachments/assets/c51e876d-1dba-4a4f-a879-f7b1c6ece89d)

![Image](https://github.com/user-attachments/assets/5146deeb-e21c-4444-b0e1-b817dc255dcf)

![Image](https://github.com/user-attachments/assets/80d2538e-fa51-4e99-bf10-3175f1aab002)









</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWd1u4zjSfRrmhpAhUZZ_LnLhJG18AWbwLXpmrxuUWLK4S5FaknLsefpFkZItO3FmkHQvsIsGAscWyapTp0qnKIo7J3ca4J4UD6R4uuO9b4y9_9JKdeQHuCuNON7vpKd7sE4aTfINXRf5Kl-JCnhdk3RD0o07Og8tjpFF-vey176n2WqWzmcL-svvv5FFGucRlhOW0ydwlZWdj_ZIunmmvKVw6MBK0JXUO8o1lboy2knnQXtqwfXK05cGNIUDVL3HWb4B6ngL9Ndfnr_Szpqd5S19kb6hXIvwxfQeQSWJB-cTZUyX1H2M5Ikwdn018ZZrVxvb8gCPMbJIZ3SAf4rgNw-do97Qr9BZI_oKYiCnGTgpm1HCNoRtArq_DegIW_FZq6QlbB2H49LhInKYbpCx8Ic_WiN6BZQsH0i6obTudTXDD9pZueceKJmnnZXa_wqthfo5Z4StPGhnLMkfCdscZM5I_gUd_hUD29cG6hsGyDwF7e2RsBVhBbe7FAOQWsCBsDVNSP6FngxlBzYgOcVCKWFFllGSP1HMhnF8hln3SDzaXFNEsHzYc9VDmCZAOyD5Yxrs5JtL-6MHsnwaxwc7r7Gc5p6RsOAiwHhppIJvWBr0FN0ItMiyCC1Yf9vouy4nBATHxYcoWHxvCiKWCQk7C9yDjRAJe6RnHt4L_fGGl1sYsisIcPCWVx5ERBJmz4aLESLK1VBwJ1QXX4unN5m59lRb034DBS1o76bergYuMU3sXloMpB0lKPGW8RvLyPKJCjO9J4ovZZmeq-52itk6iscnCyn74YXEhRhrZ8hS8b2L6NL_ZSYClFvhnbJwFoIinUC3u5YfRnlYPvCDjJUSA0Ajy6cPaMGFdwu-t_rkenNz6gjzQoNbLvWY4qmySnGIgQR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