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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Inconsistent behavior when executing MLIR program with and without --scf-for-to-while and --test-scf-while-op-builder optimizations
        </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: 0a44b24

system: `Ubuntu 18.04.6 LTS`

## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without the `--scf-for-to-while` and `--test-scf-while-op-builder` optimizations. When these optimizations are applied, the program enters a dead loop.

## Steps to Reproduce:



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

``` 
module {
  func.func private @printMemrefI32(tensor<*xi32>)
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() {
    %2 = "tosa.const"() <{value = dense<42> : tensor<1x3x7xi32>}> : () -> tensor<1x3x7xi32>
    %4 = "tosa.const"() <{value = dense<35> : tensor<1x3x7xi32>}> : () -> tensor<1x3x7xi32>
    %12 = tosa.minimum %2, %4 : (tensor<1x3x7xi32>, tensor<1x3x7xi32>) -> tensor<1x3x7xi32>
    %cast = tensor.cast %12 : tensor<1x3x7xi32> to tensor<*xi32>
    call @printMemrefI32(%cast) : (tensor<*xi32>) -> ()
    return
  }
}

``` 

 ### 2. **Command to Run Without Optimizations:**:

``` 
/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-linalg-to-parallel-loops -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops  -finalize-memref-to-llvm  -convert-arith-to-llvm -convert-scf-to-cf  -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 

``` 

### 3. **Output WithOut Optimizations:**:

``` 
Unranked Memref base@ = 0x561bcc0853c0 rank = 3 offset = 0 sizes = [1, 3, 7] strides = [21, 7, 1] data = 
[[[35,    35,    35,    35,    35,    35,    35], 
 [35,    35,    35,    35,    35,    35,    35], 
  [35,    35,    35,    35, 35,    35,    35]]]

``` 

### 4. **Command to Run With --scf-for-to-while and --test-scf-while-op-builder Optimizations:**:


``` 
/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt /data/szy/workspace/mlir-inconsistent/report_bug/tosa2.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-linalg-to-parallel-loops -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops    --scf-for-to-while   --test-scf-while-op-builder -finalize-memref-to-llvm  -convert-arith-to-llvm   -convert-scf-to-cf -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
``` 

### 5. **Behavior with Optimizations:**:

When I apply the --scf-for-to-while and --test-scf-while-op-builder optimizations, the program enters a dead loop and doesn't produce any output.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWF1v2zoP_jXKDSFDlmwnuchF2ixAgQ17sb3DLgvZZhKdyZIhyenHrz-QnKTtlrXdF3AuBhhuJdIPKYp8JEZ6r7YGcUHKC1KuJnI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