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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Inconsistent output when executing MLIR program with `--test-affine-data-copy`
        </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: 953838dceaf

system: `Ubuntu 18.04.6 LTS`

## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without `--test-affine-data-copy="for-memref-region" `. (The last three numbers in the tensor are incorrect.)
The output also becomes correct when `"for-memref-region" ` is removed.

## Steps to Reproduce:


### 1. **MLIR Program (tosa.mlir)**:
tosa.mlir: 
``` 
module {
  func.func private @printMemrefI32(tensor<*xi32>)
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() {
    %0 = "tosa.const"() <{value = dense<-6925> : tensor<1x1x20xi32>}> : () -> tensor<1x1x20xi32>
    %1 = tosa.cast %0 : (tensor<1x1x20xi32>) -> tensor<1x1x20xf32>
    %2 = tosa.sin %1 : (tensor<1x1x20xf32>) -> tensor<1x1x20xf32>
    %cast = tensor.cast %2 : tensor<1x1x20xf32> to tensor<*xf32>
    call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
 return
  }
}

``` 

 ### 2. **Command to Run with `--test-affine-data-copy `  :**

``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt tosa.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith --arith-int-range-narrowing  -convert-math-to-llvm -one-shot-bufferize="bufferize-function-boundaries" -finalize-memref-to-llvm    -convert-linalg-to-affine-loops --affine-loop-unroll  --test-affine-data-copy   -expand-strided-metadata -finalize-memref-to-llvm  -lower-affine -convert-arith-to-llvm -finalize-memref-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 with  `--test-affine-data-copy` :**:

``` 
[[[-0.801495,    -0.801495,    -0.801495,    -0.801495,    -0.801495, -0.801495,    -0.801495,    -0.801495,    -0.801495,    -0.801495, -0.801495,    -0.801495,    -0.801495,    -0.801495,    -0.801495, -0.801495,    -0.801495,    -0.801495,    -0.801495,    -0.801495]]]

``` 

### 4. **Command to Run With `--test-affine-data-copy="for-memref-region"` :**


``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt tosa.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -tosa-to-arith --arith-int-range-narrowing  -convert-math-to-llvm -one-shot-bufferize="bufferize-function-boundaries" -finalize-memref-to-llvm    -convert-linalg-to-affine-loops --affine-loop-unroll  --test-affine-data-copy="for-memref-region" -expand-strided-metadata -finalize-memref-to-llvm  -lower-affine -convert-arith-to-llvm   -finalize-memref-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 `--test-affine-data-copy="for-memref-region"` :**

``` 
[[[-0.801495,    -0.801495,    -0.801495,    -0.801495, -0.801495,    -0.801495,    -0.801495,    -0.801495,    -0.801495, -0.801495,    -0.801495,    -0.801495,    -0.801495,    -0.801495, -0.801495,    -0.801495,    -0.801495,    3.07781e-41,    4.62428e-44, 0]]]
``` 

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWF9v2zYQ_zT0y4ECRUm2_OAHN66BAC02tB32WFDSyeZAkQJJOUk__UBKtpPWTtc1HQa0gCGH9PF3v_uvUDgndxpxRYpXpNjMxOD3xq5ed1I9iHucVaZ5WO2khwNaJ40m2RqWRVZmZVOjaAlbE7Z2D85jF34ic_ZHNWg_QFomLE_m8ObDezJnoxzhGeEZbNDVVvZ-hCNsfQuiA7zv0UrUtdQ7EBqkro120nnUHiy6QXm426MGvMd68EHK7xGc6BDevrl9B701Oys6uJN-D0I38Q8z-ECKUo_OU9G2UiNthBe0Nv0DyTaE89ZY2mFnsaUWd4EV5-FQAoSXH_YISjgPfm8RQQ9dhdaB1FG7R-2MBWEx8rUWa58QviRsHQ6awfeDB6GcgQpr06GDSWq0JbjmOgGQDix25oBN8sSD7z32DryBd9hb0ww1jo58LBTk0mDCmvB1dNDvk4MIL71xIumUtIFrlBgBzvshmGwd-MVPWHSmGRQCWbwibA3QDrpOwgN6Kw_CI5Cc9VZq_zbacpvxoCl6iGQ3hK_vZcZJ9nr0z9cBtl8CtFcASM46ITXhJeHLE0MAwgsGJNsA4TzaFnLKE86PktkNWbw6CDVgFGtQOyTZDZ0veUGy1xD8cGKQ3qf3nB2tWGyOAhMYDesrwic6adQzcglpNTGMIFfOXoNuP4PmZ2gn9VHZReT225BHpgE8Cp6Y84v-GU-H9LwUuxG0FkpdDPekbQzOU-5PEmAkPno-YFr0g9URPUSGrafnZzkcBM71wY_1cWO6LrSMUFKDHlvI9bYRazPQm0rnghq-DdKEb92nB8K3of4I3yp16KhFhcLhcdlb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