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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Inconsistent output when executing MLIR program with and without `-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>
          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 `-test-loop-fusion=test-loop-fusion-transformation`.

## Steps to Reproduce:

### 1. **MLIR Program (test.mlir)**:

test.mlir:

```
module {
  memref.global "private" constant @__constant_5x1x2x3xi16 : memref<5x1x2x3xi16> = dense<81> {alignment = 64 : i64}
  func.func private @printMemrefI16(memref<*xi16>) attributes {llvm.emit_c_interface}
  func.func @main() {
    %c81_i16 = arith.constant 81 : i16
    %c1_i16 = arith.constant 1 : i16
    %alloc = memref.alloc() {alignment = 64 : i64} : memref<1x2x3xi16>
    affine.for %arg0 = 0 to 1 {
 affine.for %arg1 = 0 to 2 {
        affine.for %arg2 = 0 to 3 {
 affine.store %c1_i16, %alloc[%arg0, %arg1, %arg2] : memref<1x2x3xi16>
 }
      }
    }
    affine.for %arg0 = 0 to 5 {
      affine.for %arg1 = 0 to 1 {
        affine.for %arg2 = 0 to 2 {
          affine.for %arg3 = 0 to 3 {
            %0 = affine.load %alloc[%arg1, %arg2, %arg3] : memref<1x2x3xi16>
            %1 = arith.muli %0, %c81_i16 : i16
 affine.store %1, %alloc[%arg1, %arg2, %arg3] : memref<1x2x3xi16>
 }
        }
      }
    }
    %expand_shape = memref.expand_shape %alloc [[0, 1], [2], [3]] output_shape [1, 1, 2, 3] : memref<1x2x3xi16> into memref<1x1x2x3xi16>
    %cast = memref.cast %expand_shape : memref<1x1x2x3xi16> to memref<*xi16>
    call @printMemrefI16(%cast) : (memref<*xi16>) -> ()
    return
  }
}
```

### 2. **Command to Run Without `-test-loop-fusion=test-loop-fusion-transformation`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/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 `-test-loop-fusion=test-loop-fusion-transformation`:**

```
[[[[7057, 7057,     7057], 
   [7057,     7057,     7057]]]]
```

### 4. **Command to Run With `-test-loop-fusion=test-loop-fusion-transformation`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -test-loop-fusion=test-loop-fusion-transformation -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/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 `-test-loop-fusion=test-loop-fusion-transformation`:**

```
[[[[81, 81,     81], 
   [81,     81,     81]]]]
```

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/eJzsGE2P4yry15BLyZGNP3PIId3Zllqa0a5mD3uMsF1OWGGwAPfH-_VPYMdxPjqtnvdGeoexkI2Lor6p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