[clang] [CIR] Fold ComplexRealOp from ComplexCreateOp (PR #147592)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 9 09:28:36 PDT 2025
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/147592
>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 1/2] [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: }
+}
>From 111efdbc27f2775ffe5d810ae70fb276a650ee4d Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Wed, 9 Jul 2025 18:07:30 +0200
Subject: [PATCH 2/2] Address code review comments
---
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 3 +--
clang/test/CIR/Transforms/complex-real-fold.cir | 2 +-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index ed70e52aaefe6..191f1648ee481 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2067,9 +2067,8 @@ LogicalResult cir::ComplexRealOp::verify() {
OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
if (auto complexCreateOp = dyn_cast_or_null<cir::ComplexCreateOp>(
- getOperand().getDefiningOp())) {
+ getOperand().getDefiningOp()))
return complexCreateOp.getOperand(0);
- }
auto complex =
mlir::cast_if_present<cir::ConstComplexAttr>(adaptor.getOperand());
diff --git a/clang/test/CIR/Transforms/complex-real-fold.cir b/clang/test/CIR/Transforms/complex-real-fold.cir
index 630dd679f67af..ad32c677c36b6 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 - -split-input-file | FileCheck %s
+// RUN: cir-opt %s -cir-canonicalize -o - -split-input-file | FileCheck %s
!s32i = !cir.int<s, 32>
More information about the cfe-commits
mailing list