<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60909>60909</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Using `convert-scf-to-cf` and 'test-print-liveness` together cause segment fault
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Jokeren
</td>
</tr>
</table>
<pre>
The problem only appears if we use a single command line on the IR below.
Instead, if we first do `convert-scf-to-cf`, copy the output IR to a file, and then apply `test-print-liveness` on the file, we don't observe the problem.
(Please bear with me on the weird variable names; it's modified from triton IR)
We found one possible explanation.
Maybe `isBeforeInBlock` should add an edge case for `other == this` because `updateOrderIfNecessary` is expected to only be invoked on blocks with more than one. After skipping `updateOrderIfNecessary` when `other == this`, we did solve the problem but it doesn't explain why separating `convert-scf-to-cf` and `test-print-liveness` could avoid the problem.
https://github.com/llvm/llvm-project/blob/fd5d92e6220905f7d942a81108266d427babe143/mlir/lib/IR/Operation.cpp#L274
https://github.com/llvm/llvm-project/blob/fd5d92e6220905f7d942a81108266d427babe143/mlir/lib/IR/Operation.cpp#L304
cc @ptillet
Tested with f50cad2c55dfab4ff3da010a64119b4a93522289
Reproduced with:
```
mlir-opt -pass-pipeline="builtin.module(func.func(convert-scf-to-cf), func.func(test-print-liveness))" ./test.mlir
```
```
func.func @for_if_for(%lb : index, %ub : index, %step : index, %i1 : i1) {
%a_shared_init = arith.constant dense<0.00e+00> : tensor<128x32xf16>
%b_shared_init = arith.constant dense<0.00e+00> : tensor<128x32xf16>
%c_shared_init = arith.constant dense<0.00e+00> : tensor<128x32xf16>
%c_blocked = arith.negf %c_shared_init : tensor<128x32xf16>
%a_shared, %b_shared, %c_shared = scf.for %iv = %lb to %ub step %step iter_args(%a_shared = %a_shared_init, %b_shared = %b_shared_init, %c_shared = %c_shared_init) -> (tensor<128x32xf16>, tensor<128x32xf16>, tensor<128x32xf16>) {
%c_shared_next_next = scf.if %i1 -> tensor<128x32xf16> {
%cst0 = arith.constant dense<0.00e+00> : tensor<128x32xf16>
scf.yield %cst0 : tensor<128x32xf16>
} else {
%c_shared_ = scf.for %jv = %lb to %ub step %step iter_args(%c_shared_next = %c_shared) -> (tensor<128x32xf16>) {
%c_blocked_next = arith.negf %c_shared_next : tensor<128x32xf16>
scf.yield %c_shared_next : tensor<128x32xf16>
}
scf.yield %c_shared_ : tensor<128x32xf16>
}
%b_blocked_next = arith.negf %b_shared: tensor<128x32xf16>
scf.yield %a_shared, %b_shared, %c_shared_next_next : tensor<128x32xf16>, tensor<128x32xf16>, tensor<128x32xf16>
}
return
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV11vIjsS_TXmpQQybrqhH3jIxyBltbuzGs3qPkb-KIMnxm7Zbgj__sqmyUBCJjNXd6QrRR1hl49PnTrldvMYzdohLkl9S-r7Ee_Txoflv_wTBnQj4dVh-XWD0AUvLG7BO3sA3nXIQwSjYY_QRwQO0bi1RZB-u-VOgTUOwTtIG4SHLyDQ-v0ECL0n9ObBxYRcEXY3IGgTYgLlgTRUerfDkMZR6nHyY6lJQ3Ok9N2hoPk-dX3KoMkDB20s5vm8adqgy-TsISMljGncBePS2JodOoyRNPRE6rRuj6C8I2yewIuIYYdlesh3cmRM2OJ_FnlEEMgD7E3awPYlvz2aoGDHg-HCIji-xUiqWzCJsHmErVdGG1Sgg99CCiZ5Bw9fCGsH8PL8A0H73inwDqHzMZqMhc-d5Y4n491A5T_8IDCnZ-Itah_wwd1aL59yanHje6uAKwXcAao1gsyktQ95hU8bDECqe1LdQ9qYIodAyXMFSUP7TvGEn4PC8KD_ixJj5OGQg0zMTFAmVFn1YgKBYNzOP2GmDCJziIMyPmQRucu5TOBGJwwQn0zXGbf-8Ub7XMF3qJ6qZRREby_rBKJPYLKHMB6LWZQzDvabA0TseOBp2P2aw4p93veMPOq680Zdc0d5blLqIqluCFsRtlq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