<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/94179>94179</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir][linalg] `linalg.generic` possible miscompilation?
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pingshiyu
</td>
</tr>
</table>
<pre>
I've been toying with the semantics of `linalg.generic` lately for learning, and I came across this program:
```mlir
module {
func.func private @printMemrefI64(tensor<*xi64>)
func.func private @printMemrefI32(tensor<*xi32>)
func.func private @printMemrefF64(tensor<*xf64>)
func.func private @printMemrefF32(tensor<*xf32>)
func.func @main() {
%cst = arith.constant dense<14> : tensor<1xi64>
%cst_0 = arith.constant dense<15> : tensor<1xi64>
%cst_1 = arith.constant dense<[[[-1], [-2], [10]]]> : tensor<1x3x1xi64>
%0:2 = linalg.generic {
indexing_maps = [
// iteration space:
// d0 -> [0, 0]
// d1 -> [0, 2]
affine_map<(d0, d1) -> (d0)>,
affine_map<(d0, d1) -> (d0)>,
affine_map<(d0, d1) -> (d0, d1, d0)>],
iterator_types = ["reduction", "reduction"]}
ins(%cst : tensor<1xi64>) outs(%cst_0, %cst_1 : tensor<1xi64>, tensor<1x3x1xi64>) {
^bb0(%in: i64, %out: i64, %out_3: i64):
// %in=14, %out=15, %out_3=-1 -> %1=-1; expected: %0#0[0]=-1, %0#1[0,0,0]=15
// %in=14, %out=15, %out_3=-2 -> %1=-2; expected: %0#0[0]=-2, %0#1[0,1,0]=15
// %in=14, %out=15, %out_3=10 -> %1=10; expected: %0#0[0]=10, %0#1[0,2,0]=15
%1 = arith.minsi %out_3, %out_3 : i64
linalg.yield %1, %out : i64, i64
} -> (tensor<1xi64>, tensor<1x3x1xi64>)
vector.print str "%0#0="
%cast = tensor.cast %0#0 : tensor<1xi64> to tensor<*xi64>
// expect: [10]
call @printMemrefI64(%cast) : (tensor<*xi64>) -> ()
vector.print str "%0#1="
%cast_2 = tensor.cast %0#1 : tensor<1x3x1xi64> to tensor<*xi64>
// expect: [[[15], [15], [15]]]
// actual: [[[15], [-1], [-2]]]
call @printMemrefI64(%cast_2) : (tensor<*xi64>) -> ()
return
}
}
```
where I left my understanding of the iteration space, and the expected outputs in the comments.
When lowered and executed with the interpreter like so:
```mlir-opt prog.mlir -one-shot-bufferize -func-bufferize -cse -canonicalize -convert-vector-to-scf -test-lower-to-llvm | mlir-cpu-runner -e main --entry-point-result void --shared-libs="lib/mlir/libmlir_c_runner_utils.so,lib/mlir/libmlir_runner_utils.so"```
The program seems to produce an output that doesn't align with my intuition - did I misunderstand something about `linalg.generic` or is the compiler wrong here?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V0Fvs7oS_TXOZkQEJiRlkUWbfJG6eLsnvWVkYAh-19jINmlzf_2VbUhCQ9uv90q3osQ24-PDzBl7YMbwk0TckuyFZPsF622j9Lbj8mQafukXhaou21dCN2eEAlGCVRcuT_DGbQO2QTDYMml5aUDVQNax4JKJ0_KEEjUvyToGwSyKC9RKg0CmJZcnQnfAZAWvULIWgZVaGQO24QY6rU6atSR9JvGexM9kHYerFVyHoVZVvUAgm5fQBwCoe1ku3Q06zc_MIpBV3Gku7X-w1Vi_rleEPlmURmmS7gh9fufrFUl_EZr_ACWlDygp_SnK4ZFL_XMuh0cu9VdcyCpuGZeEPhGaT53n_gjNSmOBpHtgmttmWSppLJMWKpQGSbpLHEUg6TNcF01GJ85AHeMvwbKfgSVfgXnxuitKSLZ34nJtemsnsWuH63HZ9P3TpWOSPlO_9FTXd_7jssJ3Lk_HlnXGmzouE6Ab4IHQA3CLmlmuJJiOlXiV-vi8iiHyNLOX2L2AJ_8VYJVMJ9DbBFbXXKLj5jXyVHmDKnEaCHPCWO6Fs5tf5l8FCWM7uAIOURy87X2n9NFeOry5m1KNVV86pxJKvf2HoWxPNnuYUuPS-HQYlD-nRZqD6u3N7BgH8Ks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