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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Inconsistent output when executing MLIR program with and without  `--affine-data-copy-generate --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>
          Emilyaxe
      </td>
    </tr>
</table>

<pre>
    git version: 953838dceaff3728

system: `Ubuntu 18.04.6 LTS`

## Description:
I am experiencing an inconsistent result when executing the same MLIR program  with and without   `--affine-data-copy-generate --affine-loop-fusion` .

## 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() {
    %4 = "tosa.const"() <{value = dense<-7643> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
    %6 = "tosa.const"() <{value = dense<-1782> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
    %14 = tosa.cast %6 : (tensor<1x2x2xi32>) -> tensor<1x2x2xf32>
    %40 = tosa.cast %4 : (tensor<1x2x2xi32>) -> tensor<1x2x2xf32>
    %41 = tosa.clamp %14 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor<1x2x2xf32>) -> tensor<1x2x2xf32>
    %42 = tosa.clamp %40 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor<1x2x2xf32>) -> tensor<1x2x2xf32>
    %43 = tosa.sub %41, %42 : (tensor<1x2x2xf32>, tensor<1x2x2xf32>) -> tensor<1x2x2xf32>
    %cast = tensor.cast %43 : tensor<1x2x2xf32> to tensor<*xf32>
    call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
 return
  }
}

``` 

 ### 2. **Command to Run without `--affine-data-copy-generate --affine-loop-fusion` :**

``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir -tosa-to-arith  --tosa-layerwise-constant-fold  | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))"  | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -convert-arith-to-llvm -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops  -lower-affine  --expand-strided-metadata -finalize-memref-to-llvm  -convert-math-to-llvm  -convert-scf-to-cf -convert-func-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 `--affine-data-copy-generate --affine-loop-fusion`:**:

``` 
[[[5861,    5861],
  [5861, 5861]]]

``` 

### 4. **Command to Run With `--affine-data-copy-generate --affine-loop-fusion`:**


``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir -tosa-to-arith  --tosa-layerwise-constant-fold  | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))"  | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -convert-arith-to-llvm -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops --affine-data-copy-generate="generate-dma=false fast-mem-space=0 skip-non-unit-stride-loops"  --affine-loop-fusion  -lower-affine --expand-strided-metadata  -finalize-memref-to-llvm  -convert-math-to-llvm -convert-scf-to-cf  -convert-func-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 `--affine-data-copy-generate --affine-loop-fusion`:**

``` 
[[[7643,    7643],
  [7643, 7643]]]
``` 


### 6. **Comparation of mlir between with and without `--affine-data-copy-generate --affine-loop-fusion`:**
![Image](https://github.com/user-attachments/assets/74018f2e-c8e8-486e-9715-bdcc46bdb124)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWFuP4yoS_jXkBRHZ4Fse8pDudKSWztGuZna1jy1slxN2MViA08n8-hUQJ31J986lZx_OjBQ5til_9VVR1GfDrRVbBbBE-Q3K1zM-up02y7teyCM_wKzW7XG5FQ7vwVihFWIrvMhZxaq2Ad51rKQVSlYoWdmjddD7cVQk_6xH5UacVvMkmxf4j398RkUS7RBliDK8BtsYMbiIiZLVPeY9hsMARoBqhNpirrBQjVZWWAfKYQN2lA4_7kBhOEAzOm_ldoAt7wH_-cf9JzwYvTW8x_hRuB3mqg0nenQYe16E8K4TCkjLHSeNHo5kCwoMd4DPY1LrgXRjCLdI8PwZ788OBoudxp9gMLodG4j0nxp5u3SOEV0hugq0_n6ihWjF570UBtFFHI5Pn2765CUrn6rw8xe9bkcJGJU3KFlh3I2qmfsDHozYe9ooSwYjlPsTegPdPaOIVg6U1QaxW0RXB8EoYnfe4dcAbF4DdG8AoCzpuVCIVoguzgwxRjTPMGJrjCh12vK5n0OHKJ0s2S0qb_ZcjhDMWlAWELslZZExxO6wz8OZQXqgBzoFUa6n8RMW8dfXbc9kiu8ik5YV_Qlk0piaSIVbNxEMINeffQO5e4GcJa-Rs49BTp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