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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Inconsistent output when executing MLIR program with and without `-convert-math-to-spirv`
        </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 [5971b41](https://github.com/llvm/llvm-project/commit/5971b419199942fd8023070f81f37499d4d4738b).

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

## Steps to Reproduce:

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

test.mlir:

```
module {
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() {
    %0 = "tosa.const"() <{values = dense<-8.227000e+01> : tensor<1x4x4x2xf32>}> : () -> tensor<1x4x4x2xf32>
    %1 = "tosa.const"() <{values = dense<-1.486000e+02> : tensor<2x2x2x1xf32>}> : () -> tensor<2x2x2x1xf32>
 %2 = "tosa.const"() <{values = dense<-1.022500e+02> : tensor<2xf32>}> : () -> tensor<2xf32>
    %3 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
    %4 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
    %5 = tosa.depthwise_conv2d %0, %1, %2, %3, %4 {acc_type = f32, dilation = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 2, 2>} : (tensor<1x4x4x2xf32>, tensor<2x2x2x1xf32>, tensor<2xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x2x2x2xf32>
    %6 = tosa.tanh %5 : (tensor<1x2x2x2xf32>) -> tensor<1x2x2x2xf32>
    %cast = tensor.cast %6 : tensor<1x2x2x2xf32> to tensor<*xf32>
    call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
    return
  }
}
```

### 2. **Command to Run Without `-convert-math-to-spirv`:**

```
/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-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-math-to-libm -convert-cf-to-llvm -convert-arith-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -convert-spirv-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 `-convert-math-to-spirv`:**

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

### 4. **Command to Run With `-convert-math-to-spirv`:**

```
/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-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-affine-loops -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-math-to-spirv -convert-math-to-libm -convert-cf-to-llvm -convert-arith-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -convert-spirv-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 `-convert-math-to-spirv`:**

```
[[[[-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="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWF2P6rwR_jXmZpTImSRALrhg4V1ppXPU6vSil0dOMoCrxI5sh4X--spOgGVh6XZ7KvWVDhvlYzJ-5vF4PrIW1sqtIlqw_Inl64no3U6bxTfRltpgNil1fVx8P8JWOtiTsVIrkBZY_pQXs6TMEpavGc53znWWpUuGzwyft9Lt-jKudMvwuWn2p0vUGf0PqhzD50q3rfQ3I0yRFEWR4aaec0z5jG_mySadZUVRZ3U2S-clwyJmfOkPTBmmsCZbGdk5qZU3zJcvIFqgQ0dGkqqk2oJQIFWllZXWkXJgyPaNg9cdKaADVb3zWm5HYEVL8P3byw_ojN4a0cKrdDsQqg43undBjU15VGm1J-OiVrhd5HRkO2n2bMqv2f3NUWfBafhBndF1X9FA8qzhlZIYGC4ZLoPlv46WGc4dWRe3jTQMi0HjPPry6ow35ePBl62u-4aAzZ4YXwJselXF_gSdkXvhCFjGOyOV-06toc1zisGastqwdMVwedikyNI_vN1rAJbxVkjFcM6wOBsAYJhzYOkaGKLTVsTe344hnjTTFZs97UXTkw16NSlLLF1F8xhxxjknhk88YekfwNIlnMkkh-yQHfBEaLY-aYzAkX_-SPvMLfkatyTO5tMTN7zhhgf_l3yS2ztt7tVy_Coxjpg_IPZZSu8dlX6JD495-AU6_M4afnb13tPJ_r_o5MFM4FJT53av0tJPXwmwDhnAcBWibbzieE3Ha-YTRlTVT3fsKEB5A7iCWjbCV7AgE8aII0tXcpp5agEsCcm4gk7U93SC4benQds6I2u6NyBYHX1wcsBHSYSrD2P4-tUd4V3Ni_DW44OB936fXvzuhNqdluI97avBnwavhHUDftCMh-fB6PKj4b6m3yuYA2olmuZujR3NDdF7Tf-q6g7Uh6gcMQ253qjw4AOXL8fzm7J_1VXw1FVWum19B_NdqFfw97GRPWpioYWH46a1MHzuhNvdtvKyl03tr747PP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