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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Inconsistent output when executing MLIR program with and without `-test-expand-math`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jzc-1122
      </td>
    </tr>
</table>

<pre>
    My git version is [35619c7](https://github.com/llvm/llvm-project/commit/35619c791d1f5128d16c7a8e099e8856e12ab39c).

## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without the `-test-expand-math`.

## Steps to Reproduce:

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

test.mlir:

```
module {
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() -> () {
    %arg0 = "tosa.const"() {value = dense<2.88> : tensor<51x5x89xf32>} : () -> tensor<51x5x89xf32>
    %0 = tosa.ceil %arg0 : (tensor<51x5x89xf32>) -> tensor<51x5x89xf32>
    %1 = tosa.reduce_sum %0 {axis = 2 : i32} : (tensor<51x5x89xf32>) -> tensor<51x5x1xf32>
    %2 = tosa.exp %1 : (tensor<51x5x1xf32>) -> tensor<51x5x1xf32>
    %3 = tosa.exp %2 : (tensor<51x5x1xf32>) -> tensor<51x5x1xf32>
    %4 = tosa.reciprocal %3 : (tensor<51x5x1xf32>) -> tensor<51x5x1xf32>
    %5 = tosa.reshape %4 {new_shape = array<i64: 5, 51>} : (tensor<51x5x1xf32>) -> tensor<5x51xf32>
 %6 = tosa.reduce_min %5 {axis = 0 : i32} : (tensor<5x51xf32>) -> tensor<1x51xf32>
    %7 = tosa.pow %6, %6 : (tensor<1x51xf32>, tensor<1x51xf32>) -> tensor<1x51xf32>
    %rtn1 = tensor.cast %7 : tensor<1x51xf32> to tensor<*xf32>
    call @printMemrefF32(%rtn1) : (tensor<*xf32>) -> ()
    return
  }
}
```

### 2. **Command to Run Without `-test-expand-math`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-tensor -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops -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-cpu-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-expand-math`:**

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

### 4. **Command to Run With `-test-expand-math`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-tensor -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops -test-expand-math -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-cpu-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-expand-math`:**

```
[[-nan,   -nan,   -nan, -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan, -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan, -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan, -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan, -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan, -nan]]
```

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="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWF9vo7gX_TTOy5UR2CHAQx7SZCJVmur30-zDPlYGLolHYCPbpMl8-pWB_Gvazky3q9WsRkLYmOtzjo3vcWJhrdwoxDmJ70i8mojObbWZf_1W0ChibJLr8jB_OMBGOtihsVIrkBZIfMfjWZQVCYlXhKVb51pL-IKwNWHrjXTbLg8K3RC2ruvdsaCt0V-xcIStC9000ldGmCwqoyqOWFpGsyIRKYZZhmkazzBiIudZQVgWkHDhL8YJ47BCWxjZOqmVJw4X9yAawH2LRqIqpNqAUCBVoZWV1qFyYNB2tYOnLSrAPRad81Fui2BFg_Dw-f4LtEZvjGjgSbotCFX2Fd25PozMQurQOor7VqiSNsJtySy8FvaHw9aC0_AFW6PLrsBB3ynCB0UBELYgbNGT_n8kJSz18EFTS0NYNkScep9fnfBm4XiFi0aXXY1AkjsSLgCqThWBv0Fr5E44BDINWyOVe8DGYLXmrGdTVhvCl4Qt9hVnhH_yvNcAZBo2QirCUsIyoIR_grF-JAMgLBZmEwLhKyCMOW1F4GfeEcbOwTtRd9jHlKgsEr5kQZr2gHwBJzFxtI_3aXYUlKz615f8r4WexAxKBhko6wt9C7gc97P-P4wfnfEN-m_8aLtmJE7uxN7nCF8B6wklZxeD-Dnu6Iaanalx3x7FvAAd_Swyv0FmH4Q8vZyuQrZGF6I-Un4EQXxJYLeixZE2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