<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/140259>140259</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Inconsistent output when executing MLIR program with `--scf-parallel-loop-fusion`
</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: ba631508ae7f
system: `Ubuntu 18.04.6 LTS`
## Description:
I am experiencing an inconsistent result when executing the same MLIR program with and without `--scf-parallel-loop-fusion`.
## Steps to Reproduce:
### 1. **MLIR Program (a.mlir)**:
a.mlir:
```
module {
func.func private @printMemrefI32(tensor<*xi32>)
func.func private @printMemrefF32(tensor<*xf32>)
func.func @main() {
%idx0 = index.constant 0
%1 = "tosa.const"() <{values = dense<-5857> : tensor<1x4x4xi32>}> : () -> tensor<1x4x4xi32>
%10 = tosa.clz %1 : (tensor<1x4x4xi32>) -> tensor<1x4x4xi32>
%28 = tosa.cast %10 : (tensor<1x4x4xi32>) -> tensor<1x4x4xf32>
%48 = tosa.floor %28 : (tensor<1x4x4xf32>) -> tensor<1x4x4xf32>
%55 = tosa.reverse %48 {axis = 1 : i32} : (tensor<1x4x4xf32>) -> tensor<1x4x4xf32>
%cast_5 = tensor.cast %55 : tensor<1x4x4xf32> to tensor<*xf32>
call @printMemrefF32(%cast_5) : (tensor<*xf32>) -> ()
return
}
}
```
### 2. **Command to Run without `--scf-parallel-loop-fusion` :**
```
/pathto/mlir-opt a.mlir -tosa-to-scf -tosa-to-arith \
| /pathto/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /pathto/mlir-opt -convert-scf-to-cf --cse \
-one-shot-bufferize="bufferize-function-boundaries" -finalize-memref-to-llvm -convert-linalg-to-parallel-loops \
-convert-scf-to-cf -finalize-memref-to-llvm -convert-math-to-llvm -convert-arith-to-llvm \
-convert-index-to-llvm -convert-cf-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts \
| timeout 10 /pathto/mlir-cpu-runner -e main -entry-point-result=void \
--shared-libs=/pathto/libmlir_c_runner_utils.so \
--shared-libs=/pathto/libmlir_runner_utils.so \
--shared-libs=/pathto/libmlir_async_runtime.so
```
### 3. **Output without `--scf-parallel-loop-fusion` :**:
```
Unranked Memref base@ = 0x55a554495b40 rank = 3 offset = 0 sizes = [1, 4, 4] strides = [16, 4, 1] data =
[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]]
```
### 4. **Command to Run with `--scf-parallel-loop-fusion` :**
```
/pathto/mlir-opt a.mlir -tosa-to-scf -tosa-to-arith \
| /pathto/mlir-opt -pass-pipeline="builtin.module(func.func(tosa-to-linalg))" \
| /pathto/mlir-opt -convert-scf-to-cf --cse \
-one-shot-bufferize="bufferize-function-boundaries" -finalize-memref-to-llvm -convert-linalg-to-parallel-loops \
--scf-parallel-loop-fusion -convert-scf-to-cf -finalize-memref-to-llvm -convert-math-to-llvm -convert-arith-to-llvm \
-convert-index-to-llvm -convert-cf-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts \
| timeout 10 /pathto/mlir-cpu-runner -e main -entry-point-result=void \
--shared-libs=/pathto/libmlir_c_runner_utils.so \
--shared-libs=/pathto/libmlir_runner_utils.so \
--shared-libs=/pathto/libmlir_async_runtime.so
```
### 5. **Output with `--scf-parallel-loop-fusion` :**
```
Unranked Memref base@ = 0x5625e8d1c780 rank = 3 offset = 0 sizes = [1, 4, 4] strides = [16, 4, 1] data =
[[[2.68938e+20, 7.20647e+31, 2.89575e+32, 2.48354e+27],
[5.12554e-11, 2.63753e+23, 7.00624e+22, 5.61567e+13],
[0, 0, 0, 0],
[0, 0, 0, 0]]]
```
### 6. **Analysis for this case :**
This issue can be consistently reproduced by enabling the `--scf-parallel-loop-fusion` option, which fuses the last two `scf.parallel` loops.
I'm not sure whether the root cause lies in the `--scf-parallel-loop-fusion` transformation itself or in the subsequent `-convert-cf-to-llvm` lowering.
For reference:
[input.txt](https://github.com/user-attachments/files/20254061/input.txt)
contains the IR before applying `--scf-parallel-loop-fusion`.
[output.txt](https://github.com/user-attachments/files/20254107/output.txt)
contains the IR after applying `--scf-parallel-loop-fusion`.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWEFv6zYS_jX0ZSBBokTJPvjgxM9AgBa7eG3PD5Q0irlLkVqSSuz36xckLdt5cZom2-2pgCFbnOE3H2eGnyxya8WjQlwTdkfYdsEnt9dm_WUQ8sgPuGh0d1w_CgdPaKzQihQbaHhV5Cxbcqx7km1ItrFH63DwNlJlvzWTchPkyzQr0wp--vUXUmXRj9CC0AK2aFsjRhfxSLZ5AD4AHkY0AlUr1CNwBUK1WllhHSoHBu0kHTzvUQEesJ2c93J7BMsHhJ9_evgKo9GPhg_wLNweuOrCDz05TypJbNsnIzdcSpSJ1HpM-imsqMpSeEHvF4ejBafhK45Gd1OLkeXZwzvlKRC6IXQTQv_zFJrQJU8HKQyhq2iOU0-DPkHZxqcjfPzNoLtJIpD6jmQb6CfVpv4CoxFP3CGQMhuNUO5nHAz2DwUldOlQWW1IcU_o5iAKSoovPl62gXcBdq8B-jcASJkNXChCl4SuZoIAQCgT3SEDUmxBqA4Pqa-T48pBdnHJg51Q6rTl0YNQOoMV96S-e-JyQhv8OlQWSXGfsCWrSfEFfKrOLPNDeSjnhdbb2X4CS_z9bd8Lm0g3cpHfZ4IB4_bUPwhMl1fA3LpzsI9D9z9Al1fQvdTanOPdwu4_hM3YBdug39s4h6zv-EHEqsQUedb19k8J6zP07RQ6eJ5zFgi9Knmc77firY6NsC2X8maTn-PFjnvJ_kXjR-6xnU6oBt1kVLjxHZdtTtcfdq93uGgCnTXhXg-DFyCvIZM669C7QuRpnmTjRiy6G7nbO03ozqtJokcHUVgg8ZVMnPbgcLnjxkshAGH3YQn3cBMkGbm1yShGlEIhKbaE0mYS0gmVRn0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