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

Hung-Kuan Tseng llvmlistbot at llvm.org
Mon Sep 14 08:05:02 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:

Agreed, `reassoc` is a rewrite-based flag in the LangRef, so every op in the rewritten expression needs it. The add/sub folds now also require `reassoc` on the inner op, with a test for each fold where only the inner op is missing it.


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


More information about the Mlir-commits mailing list