[Mlir-commits] [mlir] [MLIR][Arith] Fix unsound addi/subi constant-fold when merged overflow flags would be violated (PR #189164)

Fehr Mathieu llvmlistbot at llvm.org
Sat Mar 28 12:34:47 PDT 2026


math-fehr wrote:

I just checked this and it's true that the old rewrites are wrong in the general case when there is an overflow.
However, there are actually some cases that were correct before that are now missed.

For instance, the first test you modified was actually still correct: https://alive2.llvm.org/ce/z/dSPjY8

* If you only have `nuw` in the merge flags for `(x + c0) + c1`, then it is always safe to fold and keep `nuw` I believe.
* If you only have `nsw` in the merge flags, then `(x + c0) + c1` can be folded if `c0` has a different sign than `c1`.
* When you have both, I am not sure yet, maybe just having a different sign is enough.

I have a checker for these kind of rules, if you want I can try again to make this work (I'll just have to manually encode these things in PDL). I won't have until the 2nd or 3nd of April for this though!

https://github.com/llvm/llvm-project/pull/189164


More information about the Mlir-commits mailing list