diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 8512b229c2663..9069c9b37a424 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2084,6 +2084,10 @@ LogicalResult cir::ComplexImagOp::verify() { } OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) { + if (auto complexCreateOp = + dyn_cast_or_null(getOperand().getDefiningOp())) + return complexCreateOp.getOperand(1); + auto complex = mlir::cast_if_present(adaptor.getOperand()); return complex ? complex.getImag() : nullptr; diff --git a/clang/test/CIR/Transforms/complex-imag-fold.cir b/clang/test/CIR/Transforms/complex-imag-fold.cir index 0d9a4e43142a3..56e062d5285a2 100644 --- a/clang/test/CIR/Transforms/complex-imag-fold.cir +++ b/clang/test/CIR/Transforms/complex-imag-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 @@ -21,3 +21,19 @@ module { // CHECK: } } + +// ----- + +!s32i = !cir.int + +module { + cir.func dso_local @fold_complex_imag_from_create_test(%arg0: !s32i, %arg1: !s32i) -> !s32i { + %0 = cir.complex.create %arg0, %arg1 : !s32i -> !cir.complex + %1 = cir.complex.imag %0 : !cir.complex -> !s32i + cir.return %1 : !s32i + } + + // CHECK: cir.func dso_local @fold_complex_imag_from_create_test(%[[ARG_0:.*]]: !s32i, %[[ARG_1:.*]]: !s32i) -> !s32i { + // CHECK: cir.return %[[ARG_1]] : !s32i + // CHECK: } +}