Skip to content

Commit df10df8

Browse files
authored
[CIR] Fold ComplexImagOp from ComplexCreateOp (#148010)
Folding ComplexImagOp if the operand is ComplexCreateOp, inspired by MLIR Complex dialect #141365
1 parent b9d8d1e commit df10df8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,10 @@ LogicalResult cir::ComplexImagOp::verify() {
20842084
}
20852085

20862086
OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
2087+
if (auto complexCreateOp =
2088+
dyn_cast_or_null<cir::ComplexCreateOp>(getOperand().getDefiningOp()))
2089+
return complexCreateOp.getOperand(1);
2090+
20872091
auto complex =
20882092
mlir::cast_if_present<cir::ConstComplexAttr>(adaptor.getOperand());
20892093
return complex ? complex.getImag() : nullptr;

clang/test/CIR/Transforms/complex-imag-fold.cir

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
1+
// RUN: cir-opt %s -cir-canonicalize -split-input-file -o - | FileCheck %s
22

33
!s32i = !cir.int<s, 32>
44

@@ -21,3 +21,19 @@ module {
2121
// CHECK: }
2222

2323
}
24+
25+
// -----
26+
27+
!s32i = !cir.int<s, 32>
28+
29+
module {
30+
cir.func dso_local @fold_complex_imag_from_create_test(%arg0: !s32i, %arg1: !s32i) -> !s32i {
31+
%0 = cir.complex.create %arg0, %arg1 : !s32i -> !cir.complex<!s32i>
32+
%1 = cir.complex.imag %0 : !cir.complex<!s32i> -> !s32i
33+
cir.return %1 : !s32i
34+
}
35+
36+
// CHECK: cir.func dso_local @fold_complex_imag_from_create_test(%[[ARG_0:.*]]: !s32i, %[[ARG_1:.*]]: !s32i) -> !s32i {
37+
// CHECK: cir.return %[[ARG_1]] : !s32i
38+
// CHECK: }
39+
}

0 commit comments

Comments
 (0)