[Mlir-commits] [mlir] [mlir][ComplexToStandard] Fix Conj and Neg ops fmf propagation (PR #222622)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Sep 10 05:06:25 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Mattéo Rizza Murgier (matteo-rm)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/222622.diff


2 Files Affected:

- (modified) mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp (+6-3) 
- (modified) mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir (+29) 


``````````diff
diff --git a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
index 9804d9d835a97..37876c441b1ae 100644
--- a/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
+++ b/mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
@@ -592,13 +592,14 @@ struct NegOpConversion : public OpConversionPattern<complex::NegOp> {
     auto loc = op.getLoc();
     auto type = cast<ComplexType>(adaptor.getComplex().getType());
     auto elementType = cast<FloatType>(type.getElementType());
+    arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
 
     Value real =
         complex::ReOp::create(rewriter, loc, elementType, adaptor.getComplex());
     Value imag =
         complex::ImOp::create(rewriter, loc, elementType, adaptor.getComplex());
-    Value negReal = arith::NegFOp::create(rewriter, loc, real);
-    Value negImag = arith::NegFOp::create(rewriter, loc, imag);
+    Value negReal = arith::NegFOp::create(rewriter, loc, real, fmf);
+    Value negImag = arith::NegFOp::create(rewriter, loc, imag, fmf);
     rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, negReal, negImag);
     return success();
   }
@@ -851,11 +852,13 @@ struct ConjOpConversion : public OpConversionPattern<complex::ConjOp> {
     auto loc = op.getLoc();
     auto type = cast<ComplexType>(adaptor.getComplex().getType());
     auto elementType = cast<FloatType>(type.getElementType());
+    arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
     Value real =
         complex::ReOp::create(rewriter, loc, elementType, adaptor.getComplex());
     Value imag =
         complex::ImOp::create(rewriter, loc, elementType, adaptor.getComplex());
-    Value negImag = arith::NegFOp::create(rewriter, loc, elementType, imag);
+    Value negImag =
+        arith::NegFOp::create(rewriter, loc, elementType, imag, fmf);
 
     rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, real, negImag);
 
diff --git a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
index 895343fa1baed..10cfe2b061f56 100644
--- a/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
+++ b/mlir/test/Conversion/ComplexToStandard/convert-to-standard.mlir
@@ -1543,6 +1543,35 @@ func.func @complex_tanh_nnan_ninf(%arg: complex<f32>) -> complex<f32> {
 
 // -----
 
+// CHECK-LABEL: func @complex_neg_with_fmf
+// CHECK-SAME: %[[ARG:.*]]: complex<f32>
+func.func @complex_neg_with_fmf(%arg: complex<f32>) -> complex<f32> {
+  %neg = complex.neg %arg fastmath<nnan,contract> : complex<f32>
+  return %neg : complex<f32>
+}
+// CHECK: %[[REAL:.*]] = complex.re %[[ARG]] : complex<f32>
+// CHECK: %[[IMAG:.*]] = complex.im %[[ARG]] : complex<f32>
+// CHECK-DAG: %[[NEG_REAL:.*]] = arith.negf %[[REAL]] fastmath<nnan,contract> : f32
+// CHECK-DAG: %[[NEG_IMAG:.*]] = arith.negf %[[IMAG]] fastmath<nnan,contract> : f32
+// CHECK: %[[RESULT:.*]] = complex.create %[[NEG_REAL]], %[[NEG_IMAG]] : complex<f32>
+// CHECK: return %[[RESULT]] : complex<f32>
+
+// -----
+
+// CHECK-LABEL: func @complex_conj_with_fmf
+// CHECK-SAME: %[[ARG:.*]]: complex<f32>
+func.func @complex_conj_with_fmf(%arg: complex<f32>) -> complex<f32> {
+  %conj = complex.conj %arg fastmath<nnan,contract> : complex<f32>
+  return %conj : complex<f32>
+}
+// CHECK: %[[REAL:.*]] = complex.re %[[ARG]] : complex<f32>
+// CHECK: %[[IMAG:.*]] = complex.im %[[ARG]] : complex<f32>
+// CHECK: %[[NEG_IMAG:.*]] = arith.negf %[[IMAG]] fastmath<nnan,contract> : f32
+// CHECK: %[[RESULT:.*]] = complex.create %[[REAL]], %[[NEG_IMAG]] : complex<f32>
+// CHECK: return %[[RESULT]] : complex<f32>
+
+// -----
+
 // CHECK-LABEL:   func.func @complex_angle_with_fmf
 // CHECK-SAME: %[[ARG:.*]]: complex<f32>
 func.func @complex_angle_with_fmf(%arg: complex<f32>) -> f32 {

``````````

</details>


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


More information about the Mlir-commits mailing list