[Mlir-commits] [mlir] 4e5bd0e - [mlir][complex] Fix miscompile with log(exp) fold (#212781)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Aug 11 01:26:44 PDT 2026


Author: Bryth
Date: 2026-08-11T10:26:39+02:00
New Revision: 4e5bd0e846bd5f00ab618fe0ef778cfc0d102373

URL: https://github.com/llvm/llvm-project/commit/4e5bd0e846bd5f00ab618fe0ef778cfc0d102373
DIFF: https://github.com/llvm/llvm-project/commit/4e5bd0e846bd5f00ab618fe0ef778cfc0d102373.diff

LOG: [mlir][complex] Fix miscompile with log(exp) fold (#212781)

The complex folder `log(exp(z)) -> z` leads to a miscompile if |Im(z)|
\> pi since `clog` is expected to return the principal value of the
complex log, whose imaginary part is in `(pi, pi]`.

Since we cannot assume properties on `z`, this PR removes the incorrect
folder and updates the associated test.

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
    mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
    mlir/test/Dialect/Complex/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
index c6a0b8e9dfeea..06ab64f4dd132 100644
--- a/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
+++ b/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td
@@ -337,8 +337,6 @@ def LogOp : ComplexUnaryOp<"log", [SameOperandsAndResultType]> {
   }];
 
   let results = (outs Complex<AnyFloat>:$result);
-
-  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
index c7c7d4584e801..1d953ca81d751 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -307,18 +307,6 @@ OpFoldResult NegOp::fold(FoldAdaptor adaptor) {
   return {};
 }
 
-//===----------------------------------------------------------------------===//
-// LogOp
-//===----------------------------------------------------------------------===//
-
-OpFoldResult LogOp::fold(FoldAdaptor adaptor) {
-  // complex.log(complex.exp(a)) -> a
-  if (auto expOp = getOperand().getDefiningOp<ExpOp>())
-    return expOp.getOperand();
-
-  return {};
-}
-
 //===----------------------------------------------------------------------===//
 // ExpOp
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Complex/canonicalize.mlir b/mlir/test/Dialect/Complex/canonicalize.mlir
index 3afe226dca030..f41b0a6c2bc8b 100644
--- a/mlir/test/Dialect/Complex/canonicalize.mlir
+++ b/mlir/test/Dialect/Complex/canonicalize.mlir
@@ -95,13 +95,14 @@ func.func @complex_neg_neg() -> complex<f32> {
   return %neg2 : complex<f32>
 }
 
+// This identity is correct iff |Im(z)| <= pi
 // CHECK-LABEL: func @complex_log_exp
 func.func @complex_log_exp() -> complex<f32> {
-  %complex1 = complex.constant [1.0 : f32, 0.0 : f32] : complex<f32>
-  // CHECK: %[[CPLX:.*]] = complex.constant [1.000000e+00 : f32, 0.000000e+00 : f32] : complex<f32>
-  // CHECK-NEXT: return %[[CPLX]] : complex<f32>
+  %complex1 = complex.constant [0.0 : f32, 4.0 : f32] : complex<f32>
+  // CHECK: %[[CPLX:.*]] = complex.constant [0.000000e+00 : f32, 4.000000e+00 : f32] : complex<f32>
   %exp = complex.exp %complex1 : complex<f32>
   %log = complex.log %exp : complex<f32>
+  // CHECK-NOT: return %[[CPLX]] : complex<f32>
   return %log : complex<f32>
 }
 


        


More information about the Mlir-commits mailing list