[clang] [CIR] Fold ComplexRealOp from ComplexCreateOp (PR #147592)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 8 13:10:19 PDT 2025
https://github.com/AmrDeveloper created https://github.com/llvm/llvm-project/pull/147592
Folding ComplexRealOp if the operand is ComplexCreateOp, inspired by MLIR Complex dialect
Ref: https://github.com/llvm/llvm-project/blob/8b65c9d1ed298a9f4be675d1da9d678fd61ff2b0/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp#L237-L245
https://github.com/llvm/llvm-project/issues/141365
>From 316b23e92cf55a407fdaa7d9aa5fd92a24754fee Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Tue, 8 Jul 2025 22:07:09 +0200
Subject: [PATCH] [CIR] Fold ComplexRealOp from ComplexCreateOp
---
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 5 +++++
.../test/CIR/Transforms/complex-real-fold.cir | 18 +++++++++++++++++-
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 8512b229c2663..ed70e52aaefe6 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2066,6 +2066,11 @@ 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..630dd679f67af 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 -o - -split-input-file | 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: }
+}
More information about the cfe-commits
mailing list