<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/148366>148366</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[MLIR] Inconsistent Results with `scf-for-loop-peeling`
</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: [f2e244f](https://github.com/llvm/llvm-project/commit/f2e244f443c99f814216bef37a82f1b8d3e5975d)
## Description:
I am experiencing an inconsistent result when running the same MLIR input through two pipelines that differ only in the peel-front flag of the scf-for-loop-peeling pass
## Steps to Reproduce:
### Minimal MLIR program (test.mlir):
```
module {
func.func private @printMemrefI8(tensor<*xi8>) attributes {llvm.emit_c_interface}
func.func @main() {
%0 = "tosa.const"() <{values = dense<17> : tensor<2x3x4x2x1x3xi8>}> : () -> tensor<2x3x4x2x1x3xi8>
%1 = "tosa.const"() <{values = dense<-2> : tensor<2x1x1x1x1x3xi8>}> : () -> tensor<2x1x1x1x1x3xi8>
%3 = tosa.maximum %0, %1 : (tensor<2x3x4x2x1x3xi8>, tensor<2x1x1x1x1x3xi8>) -> tensor<2x3x4x2x1x3xi8>
%cast_0 = tensor.cast %3 : tensor<2x3x4x2x1x3xi8> to tensor<*xi8>
call @printMemrefI8(%cast_0) : (tensor<*xi8>) -> ()
return
}
}
```
### 1. Using -scf-for-loop-peeling=peel-front=`false`:
#### Command:
```
/home/workdir/llvm-project/build/bin/mlir-opt test.mlir --pass-pipeline="builtin.module(func.func(tosa-to-linalg))" | \
/home/workdir/llvm-project/build/bin/mlir-opt -sparsification-and-bufferization -tosa-to-arith -convert-linalg-to-loops \
-scf-for-loop-peeling=peel-front=false -test-lower-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. Using -scf-for-loop-peeling=peel-front=`true `:
```
/home/workdir/llvm-project/build/bin/mlir-opt test.mlir --pass-pipeline="builtin.module(func.func(tosa-to-linalg))" | \
/home/workdir/llvm-project/build/bin/mlir-opt -sparsification-and-bufferization -tosa-to-arith -convert-linalg-to-loops \
-scf-for-loop-peeling=peel-front=true -test-lower-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, V]]], [[[, , ]], [[, , ]]], [[[, , ]], [[, �, �]]], [[[, L, 5]], [[V, , ]]]], [[[[, , ]], [[L, 5, V]]], [[[, , ]], [[�, l, L]]], [[[, , ]], [[, �, �]]], [[[, , ]], [[V, , ]]]], [[[[, , ]], [[L, 5, V]]], [[[, , ]], [[�, l, L]]], [[[, , ]], [[, �, �]]], [[[, , ]], [[V, , ]]]]], [[[[[, , ]], [[, , ]]], [[[, , P]], [[, l, L]]], [[[, V, ]], [[, �, �]]], [[[, L, 5]], [[, , ]]]], [[[[, , ]], [[, , ]]], [[[, , ]], [[, , ]]], [[[, , ]], [[, , ]]], [[[, , ]], [[, , ]]]], [[[[, , ]], [[, , ]]], [[[, , ]], [[, , ]]], [[[, , ]], [[, , ]]], [[[, , ]], [[, , ]]]]]]
```
It seems that the loop peeling process caused undefined values in memory.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWFtv27gS_jX0y4CGROpiP_jBsWogQINzkIPT14CWRhZ3KVIgKcfZX7-gJDs3t5u2aFFsgxBSJJHffHPlmMI5udeIK5JekbSYid43xq7cPaKoZjtTPaw8Og-laVvpCV8DSa9qhixJapIWhC0a7ztH-JqwLWHbvfRNv5uXpiVsq9ThdKOdNX9g6QnbTlBsO8EkCS-Xy3oRJyzOdljzXCxYHe8WFcd0macVYUsSrcNgnDAOBbrSys5Lo4Pc4dM1iBbw2KGVqEup9yA0SF0a7aTzqD1YdL3ycN-gBttrHeb4BsGJFuHm4_UtSN31HnxjTb9vwN8b6GSHSmp04BvhoZJ1jRaMVg8g9bC6Q1S0tkZ7qJXYg6lH0LKmtbFUGdPRMCdI64RzkyJnXf7nsXPgDdxiZ03Vl3hWaZwRJt1ILVuhRpadNXsrWiBsETwzb5W0wULnZVk0jWjdmqpXCCS_ItEaoO51OQ8X6Kw8CI9AkqizUvsbbC3W14sBVDtjCd8Qtj7KBeEfCFuC8N7KXe_RBbTg0Tm20t-Vd1J7tLUokeTFCykkiVohNWGLAHFiAUBYGgHhBRDGvHFiHtzkCWOnmXxD8quDUH0QxwuoUDskfBPnhH-AEIVnluzIj8mRHeMjn9jmxWnSBEfD8xcWnEnF30SKsguk4unvzaReLjiT4oOwgVErjrLt28F-hG1OlNfw1G2v9WObLwn6GgOVwvm70XXj9Hl4c2L5Ja-EEL8QWSNyKZS6FIpniaP9n-v5LDwHDUbLTpgWfW_18DAG5nR9kh2PCRbP4f8upCi9lLiEF49pTnhBsqgWymFAeZWsAW5j2lboavr4TOK2MS0Str039s8qJO7L6rjrparCPeTNNiQ3NZ2Hc6oDpaGO0FNpCnwYC6u81PMx4QlbnJMwmMw4Qb2hSmq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