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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR]Inconsistent Results with `-linalg-fuse-elementwise-ops` Pass
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          sweead
      </td>
    </tr>
</table>

<pre>
    test commit: [98e8f01](https://github.com/llvm/llvm-project/commit/98e8f01d183177a4f54187c23183da50a7cf6daf)

## Description:
This issue occurs when the optimization pass `-linalg-fuse-elementwise-ops` is applied.  When running the provided MLIR program with this pass, the expected result of [2, 2, 2, 2] is not obtained. Instead, the output is inconsistent, with possible results such as [0, 0, 0, 0] or [94090626694814, 0, 0, 0]. 

## Steps to Reproduce:

### Minimal MLIR program (test.mlir):
```
module {
  func.func private @printMemrefI64(tensor<*xi64>)
  func.func @main() {
    %0 = "tosa.const"() <{values = dense<-134> : tensor<4xi64>}> : () -> tensor<4xi64>
    %1 = tosa.abs %0 : (tensor<4xi64>) -> tensor<4xi64>
 %2 = tosa.arithmetic_right_shift %1, %1 {round = true} : (tensor<4xi64>, tensor<4xi64>) -> tensor<4xi64>
    %cast = tensor.cast %2 : tensor<4xi64> to tensor<*xi64>
    call @printMemrefI64(%cast) : (tensor<*xi64>) -> ()
 return
  }
}
```
#### Command:
### 1. Without `-linalg-fuse-elementwise-ops`:
```
mlir-opt test.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" |\
mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries=1" -convert-linalg-to-affine-loops   -lower-affine  -convert-scf-to-cf -convert-to-llvm  | \
/home/workdir/llvm-project/build/bin/mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project/build/lib/libmlir_runner_utils.so
```
#### Output:
```
[2, 2, 2, 2]
```
### 2. With `-linalg-fuse-elementwise-ops`:
```
mlir-opt test.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | \
mlir-opt -tosa-to-arith  -linalg-fuse-elementwise-ops -one-shot-bufferize="bufferize-function-boundaries=1" -convert-linalg-to-affine-loops   -lower-affine  -convert-scf-to-cf -convert-to-llvm  | \
/home/workdir/llvm-project/build/bin/mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project/build/lib/libmlir_runner_utils.so
```
#### Output:
```
[0,  0,  0,  0]
```



</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVl-Pq7YT_TTOy8gIDAR4yEM2-UVa6XfValvpPq4MDMGtsZFtknvvp68Gkmy2u11t31qpEnJkM5wzf44nI71XR4O4YfkDy_crOYXeuo0_I8p2Vdv2-yagD9DYYVCBpVtg-UNVYtnFCcv3TJR9CKNn6ZaJAxOHowr9VEeNHZg4aH26_vDR2d-wCUwcLlDicIFpkzJNikJmXZ4lZdGINCnTVuaxLJpu3cqOiYrFW3pEykQKe_SNU2NQ1hBvvP21Vx6U9xOCbZrJeTj3aCD0CHYMalA_JBnDKL0Hto65VkbqI-8mjxw1DmjCWXnkdvRsHYPyIMdRK2wjgK8E5SZjlDnOkKOzJ9ViC1_-__hEu6OTA5xV6CGQI8TCxG62xW8jNgFbcOgnHcB2lD9Br--XfE-cxgawdZDKEPGj8QFle0WyUxinQGbKNNZ45QOaQG9n5tF6r2qNFyIPfmp6kJ7oYrK6X_I9WDcXMoureC3W6york-ytWQSvEv9LwNFDsPCEo7Pt1OCS_5sFGX1RRg1Sv84OEyXJKBq0clTO5bN1fHni7WDbSSOw4oHFW4BuMk1EC4xOnWRAYFk8OmXCFxwcdo_rbIY03jqW7pjYflPrjKX_W7RyD8CyeJDKMFEyUd0IAJjIY2DpHpgQwXoZUVYDE-Jqme5Y8XCSekI_27VoPLJ0x5OUmIAuw82D7Mpf7K_vLjic9m_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