<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/147096>147096</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Bugs in `loop-invariant-subset-hoisting` implementation
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
christopherbate
</td>
</tr>
</table>
<pre>
I was reviewing existing implementation of `loop-invariant-subset-hoisting` and founds a couple issues. They are related to the [last change](https://github.com/llvm/llvm-project/pull/70623) from ~2years ago dealing with nested loops:
- In [this function](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp#L242), the `collectHoistableOps` parameter is not used. This means that the if you enumerate LoopLikeOps in pre-order and apply the transformation `mlir::hoistLoopInvariantSubsets`, you can produce invalid IR since it may try to hoist args not belonging to the current loop at [this point](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp#L332).
- When checking use-def chain in the body of the loop and there is a nested loop user in the use-def chain, at [this point](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp#L268) there is no `if(nextValue) return failure()` like in the insertion user branch, which looks very suspicious. I wasn't able to create a failing test case. However since user order is not deterministic, it doesn't make sense for the check to be omitted.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVU9v47YT_TT0ZWBDpizZPuiQbGBsgPzwA9ptex6RI4kNRQr846wv_ezFUHHa3Nqe9kRLFmfmzXtvBmM0oyPqRPMomqcN5jT50KkpmJj8MlHoMdGm9_rWPcMbRgh0NfRm3Aj03cTEP8y8WJrJJUzGO_ADiLay3i9b464YDLq0jbmPlLaTX--ItgJ0GgafnY6AoHxeLIGJMVPcwbeJboCBIJDFRBqShzQRiObRYkygJnQjieZJyNOU0hJF_SDkRcjLaNKU-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