[Mlir-commits] [mlir] [mlir][complex] Require fastmath for the add/sub and exp/log folds (PR #221384)

Hung-Kuan Tseng llvmlistbot at llvm.org
Fri Sep 11 07:36:18 PDT 2026


================
@@ -279,9 +284,13 @@ OpFoldResult AddOp::fold(FoldAdaptor adaptor) {
 
 OpFoldResult SubOp::fold(FoldAdaptor adaptor) {
   // complex.sub(complex.add(a, b), b) -> a
-  if (auto add = getLhs().getDefiningOp<AddOp>())
-    if (getRhs() == add.getRhs())
-      return add.getLhs();
+  // The intermediate result is rounded, so this needs `reassoc`, and
+  // (-0.0 + b) - b is +0.0, so it also needs `nsz`.
+  if (arith::bitEnumContainsAll(getFastmath(), arith::FastMathFlags::reassoc |
+                                                   arith::FastMathFlags::nsz))
----------------
Tim096 wrote:

LLVM only looks at the flags of the op being folded: `simplifyFAddInst` checks `FMF.noSignedZeros() && FMF.allowReassoc()` and then matches the inner `fsub` without looking at its flags (`InstructionSimplify.cpp`). I kept that. Happy to require `reassoc` on the inner op as well if you prefer.

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


More information about the Mlir-commits mailing list