Author: Amr Hesham
Date: 2025-07-11T19:27:54+02:00
New Revision: 53183be294e92093a37f3875a8c586c74fbd4d7c

URL: 
https://github.com/llvm/llvm-project/commit/53183be294e92093a37f3875a8c586c74fbd4d7c
DIFF: 
https://github.com/llvm/llvm-project/commit/53183be294e92093a37f3875a8c586c74fbd4d7c.diff

LOG: [CIR] Fold ComplexRealOp from ComplexCreateOp (#147592)

Folding ComplexRealOp if the operand is ComplexCreateOp, inspired by
MLIR Complex dialect

https://github.com/llvm/llvm-project/issues/141365

Added: 
    

Modified: 
    clang/lib/CIR/Dialect/IR/CIRDialect.cpp
    clang/test/CIR/Transforms/complex-real-fold.cir

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 9069c9b37a424..9e7615bdd7a7f 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2066,6 +2066,10 @@ LogicalResult cir::ComplexRealOp::verify() {
 }
 
 OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
+  if (auto complexCreateOp =
+          dyn_cast_or_null<cir::ComplexCreateOp>(getOperand().getDefiningOp()))
+    return complexCreateOp.getOperand(0);
+
   auto complex =
       mlir::cast_if_present<cir::ConstComplexAttr>(adaptor.getOperand());
   return complex ? complex.getReal() : nullptr;

diff  --git a/clang/test/CIR/Transforms/complex-real-fold.cir 
b/clang/test/CIR/Transforms/complex-real-fold.cir
index 1cab9be616af0..29b03276f822d 100644
--- a/clang/test/CIR/Transforms/complex-real-fold.cir
+++ b/clang/test/CIR/Transforms/complex-real-fold.cir
@@ -1,4 +1,4 @@
-// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
+// RUN: cir-opt %s -cir-canonicalize -split-input-file -o - | FileCheck %s
 
 !s32i = !cir.int<s, 32>
 
@@ -21,3 +21,19 @@ module {
   // CHECK: }
 
 }
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func dso_local @fold_complex_real_from_create_test(%arg0: !s32i, %arg1: 
!s32i) -> !s32i {
+    %0 = cir.complex.create %arg0, %arg1 : !s32i -> !cir.complex<!s32i>
+    %1 = cir.complex.real %0 : !cir.complex<!s32i> -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK: cir.func dso_local 
@fold_complex_real_from_create_test(%[[ARG_0:.*]]: !s32i, %[[ARG_1:.*]]: !s32i) 
-> !s32i {
+  // CHECK:   cir.return %[[ARG_0]] : !s32i
+  // CHECK: }
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to