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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Inconsistent output when executing MLIR program with and without `-tosa-layerwise-constant-fold`
        </td>
    </tr>

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

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

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

<pre>
    My git version is [55fdecc](https://github.com/llvm/llvm-project/commit/55fdeccc4567bcd4e3f8df0d177195880a194a6a).

## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without the `-tosa-layerwise-constant-fold`.

## Steps to Reproduce:

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

test.mlir:

```
module {
  func.func private @printMemrefI16(%ptr : tensor<*xi16>) attributes { llvm.emit_c_interface }
 func.func @main() -> () {
    %arg0 = "tosa.const"() {value = dense<0> : tensor<1x1xi64>} : () -> tensor<1x1xi64>
    %5 = tosa.equal %arg0, %arg0 : (tensor<1x1xi64>, tensor<1x1xi64>) -> tensor<1x1xi1>
    %6 = tosa.cast %5 : (tensor<1x1xi1>) -> tensor<1x1xi16>
    %rtn1 = tensor.cast %6 : tensor<1x1xi16> to tensor<*xi16>
    call @printMemrefI16(%rtn1) : (tensor<*xi16>) -> ()
    return
  }
}
```

### 2. **Command to Run Without `-tosa-layerwise-constant-fold`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-parallel-loops -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -finalize-memref-to-llvm -convert-func-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 `-tosa-layerwise-constant-fold`:**

```
[[1]]
```

### 4. **Command to Run With `-tosa-layerwise-constant-fold`:**

```
/path/llvm-project/build/bin/mlir-opt test.mlir -tosa-layerwise-constant-fold | \
/path/llvm-project/build/bin/mlir-opt -pass-pipeline='builtin.module(func.func(tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-parallel-loops -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -finalize-memref-to-llvm -convert-func-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 `-tosa-layerwise-constant-fold`:**

```
[[-1]]
```

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/eJzsV0-Po7gT_TTOpeQITIBwyCHdmUgtzej30-xhjy0DReKVsVm7nO7sp18Z8qd7OtM7OzurvYyECJjyew-7XoWS3qudQVyx_I7lm5kMtLdu9VH2tXViMatte1x9OsJOERzQeWUNKA8sv8vzrsWmYfmGieWeaPAsWzOxZWK7U7QP9byxPRNbrQ_nHz44-xs2xMS2sX2v4sUJplnkRVk37QKzbtl2SZuWZVrly2Ui02ohC8lENWfJOh4iYyKDDfrGqYGUNZE4WT-A7AGfB3QKTaPMDqQBZRprvPKEhsChD5rgaY8G8BmbQDGK9ghe9gifPj58hsHZnZM9PCnagzTteGEDjWGsSDhZL7mWR3RPyiOP8CQN8c7qlhXJa5G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