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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Inconsistent output when executing MLIR program between `-convert-linalg-to-affine-loops -lower-affine` and `-convert-linalg-to-loops`
        </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: adf892d743d

system: `Ubuntu 18.04.6 LTS`

## Description:
I am experiencing an inconsistent result when executing the same MLIR program between `-convert-linalg-to-affine-loops  -lower-affine` and `-convert-linalg-to-loops`.
The outputs are also inconsistent when running multiple times with the `-convert-linalg-to-affine-loops -lower-affine`

## 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() {
    %0 = "tosa.const"() <{value = dense<1560> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
    %1 = "tosa.const"() <{value = dense<524> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
    %2 = "tosa.const"() <{value = dense<8655> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
    %3 = "tosa.const"() <{value = dense<2173> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32>
    %4 = tosa.arithmetic_right_shift %0, %2 {round = true} : (tensor<1x2x2xi32>, tensor<1x2x2xi32>) -> tensor<1x2x2xi32>
    %5 = tosa.minimum %1, %4 : (tensor<1x2x2xi32>, tensor<1x2x2xi32>) -> tensor<1x2x2xi32>
    %6 = tosa.clamp %3 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor<1x2x2xi32>) -> tensor<1x2x2xi32>
    %7 = tosa.clamp %5 {max_fp = 1.07374182E+9 : f32, max_int = 1073741823 : i64, min_fp = -1.07374182E+9 : f32, min_int = -1073741824 : i64} : (tensor<1x2x2xi32>) -> tensor<1x2x2xi32>
    %8 = tosa.add %6, %7 : (tensor<1x2x2xi32>, tensor<1x2x2xi32>) -> tensor<1x2x2xi32>
    %cast = tensor.cast %8 : tensor<1x2x2xi32> to tensor<*xi32>
    call @printMemrefI32(%cast) : (tensor<*xi32>) -> ()
    return
  }
}

``` 

 ### 2. **Command to Run with `-convert-linalg-to-affine-loops -lower-affine`:**

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

``` 
[[[2237,    2697], 
  [2174,    2173]]]

``` 

### 4. **Command to Run With `-convert-linalg-to-loops`:**


``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt  -tosa-to-arith -convert-arith-to-llvm  -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops   -convert-scf-to-cf -finalize-memref-to-llvm -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 `-convert-linalg-to-loops`:**

``` 
[[[2173, 2173], 
  [2173,    2173]]]
``` 

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWF-PmzgQ_zTOi2UENgR4yEP-bKSVWt2p7ekeVwaGxCdjI9tks_30JxuS3W2T63av24e7SghkPJ75zXj4zWBurdgpgAXKVijbzPjg9tosbjohH_gRZpVuHhYoXu6EwwcwVmiF2BLzpi1K2uQpa1C8RPHSPlgHnZ9C8_iPalBuwEkRxWk0x-8-fUTzeJRDlCHK8AZsbUTvRnUoXt5i3mE49mAEqFqoHeYKC1VrZYV1oBw2YAfp8P0eFIYj1IPzUm4P2PIO8Pt3tx9wb_TO8A5X4O4BlMdCaq0OYByRQnG5I04T3rZCAZFa9xZjIvU9mOklmseYq-bKwrACzeMIxctPe8B6cP3gLOYGMJdWPwcckJpBKY-zG6QTvQTsRAcW3wu3D9hfgPBLgM8C-dFBb7HT-AP0RjdDDWM8nwp5uSTCiC4RXYY4_T7FCdGCR50UBtFynB5XTy_9bsZLbzJcftDpZpCAUb5C8RLjdlB15G-4N-LAHWCUxr0Ryr2HzkB7yyiihQNltUFsjejyKBhF7MYbfImC7dcK2isKUBp3XChEC0TLM0KMEc1ijNgGI0qdtjzye-QQpSdJtkb56sDlAEGsAWUBsXWSzWPEbrAPwxlAcqRHevIh35zmJ1XEjy_LnrEkr8GS0fQNoNDXQCnmWfYGWNhrsNAkZ2-AJQ1GAhBuhNt34ER9Z8Ru7-7sXrQu5BSi6ymI-crowbOGX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