<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/113690>113690</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Inconsistent results for scf.if and linalg.copy
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
wangyongj1a
</td>
</tr>
</table>
<pre>
I have the following MLIR program:
test.mlir:
```
module {
func.func @tensor_i16(%arg0: tensor<10xi16>) -> i16 {
%c0_i16 = arith.constant 0 : i16
return %c0_i16 : i16
}
func.func @func1() -> f32 {
%idx0 = index.constant 0
%idx1 = index.constant 1
%idx2 = index.constant 2
%idx5 = index.constant 5
%true = arith.constant true
%c0_i16 = arith.constant 0 : i16
%c1_i16 = arith.constant 1 : i16
// %49 can not be replaced with %c0_i16
%alloc_76 = memref.alloc() : memref<24xi16>
linalg.fill ins(%c0_i16 : i16) outs(%alloc_76 : memref<24xi16>)
%49 = affine.load %alloc_76[%idx0] : memref<24xi16>
%from_elements_117 = tensor.from_elements %49, %49, %49, %49, %49, %49, %49, %49, %49, %49 : tensor<5x2xi16>
// scf.if can not be removed
%171 = scf.if %true -> (tensor<5x2xi16>) {
scf.yield %from_elements_117 : tensor<5x2xi16>
} else {
scf.yield %from_elements_117 : tensor<5x2xi16>
}
%cast_258 = tensor.splat %c1_i16[%idx5, %idx2] : tensor<?x?xi16>
%212 = linalg.copy ins(%cast_258 : tensor<?x?xi16>) outs(%from_elements_117 : tensor<5x2xi16>) -> tensor<5x2xi16>
%collapsed_217 = tensor.collapse_shape %171 [[0, 1]] : tensor<5x2xi16> into tensor<10xi16>
%dim = index.constant 10
%0 = scf.for %arg1 = %idx0 to %dim step %idx1 iter_args(%arg2 = %c0_i16) -> (i16) {
%extracted = tensor.extract %collapsed_217[%arg1] : tensor<10xi16>
%1 = arith.addi %arg2, %extracted : i16
vector.print %extracted : i16
scf.yield %1 : i16
}
// can not be removed
%collapsed_213 = tensor.collapse_shape %from_elements_117 [[0, 1]] : tensor<5x2xi16> into tensor<10xi16>
%collapsed_222 = tensor.collapse_shape %212 [[0, 1]] : tensor<5x2xi16> into tensor<10xi16>
%490 = call @tensor_i16(%collapsed_222) : (tensor<10xi16>) -> i16
%1 = arith.sitofp %0 : i16 to f32
return %1 : f32
}
}
```
When I ran ```/data/tmp/v1025/llvm-project/build/bin/mlir-opt --lower-affine --one-shot-bufferize="dialect-filter=tensor,linalg,bufferization" --convert-index-to-llvm --convert-linalg-to-loops --convert-vector-to-llvm --expand-strided-metadata --convert-scf-to-cf --func-bufferize --one-shot-bufferize="dialect-filter=tensor" --convert-arith-to-llvm --finalize-memref-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts test.mlir | /data/tmp/v1025/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/v1025/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/v1025/llvm-project/build/lib/libmlir_c_runner_utils.so``` on the program, I got the result of:
```
0
0
0
0
0
0
0
0
0
0
0.000000e+00
```
However, when I ran ```/data/tmp/v1025/llvm-project/build/bin/mlir-opt --lower-affine --linalg-fuse-elementwise-ops --one-shot-bufferize="dialect-filter=tensor,linalg,bufferization" --convert-index-to-llvm --convert-linalg-to-loops --convert-vector-to-llvm --expand-strided-metadata --convert-scf-to-cf --func-bufferize --one-shot-bufferize="dialect-filter=tensor" --convert-arith-to-llvm --finalize-memref-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts test.mlir | /data/tmp/v1025/llvm-project/build/bin/mlir-cpu-runner -e func1 --shared-libs=/data/tmp/v1025/llvm-project/build/lib/libmlir_runner_utils.so,/data/tmp/v1025/llvm-project/build/lib/libmlir_c_runner_utils.so``` on the program, I got the result of:
```
1
1
1
1
1
1
1
1
1
1
1.000000e+01
```
The above two results seem to be inconsistent. I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused these results.
My git version is 2d26ef09fc87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