diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 8512b229c2663..4ffe2a8d1cbb1 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(getOperand().getDefiningOp())) + return complexCreateOp.getOperand(0); + auto complex = mlir::cast_if_present(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..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 - | FileCheck %s +// RUN: cir-opt %s -cir-canonicalize -o - -split-input-file | FileCheck %s !s32i = !cir.int @@ -21,3 +21,19 @@ module { // CHECK: } } + +// ----- + +!s32i = !cir.int + +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 + %1 = cir.complex.real %0 : !cir.complex -> !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: } +}