Skip to content

Commit fccab5d

Browse files
authored
[CIR] Upstream ComplexType ImaginaryLiteral (#144223)
This change adds support for ComplexType ImaginaryLiteral #141365
1 parent 492d25b commit fccab5d

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
2121
bool isInit);
2222

2323
mlir::Value VisitInitListExpr(InitListExpr *e);
24+
25+
mlir::Value VisitImaginaryLiteral(const ImaginaryLiteral *il);
2426
};
2527

2628
} // namespace
@@ -66,6 +68,34 @@ mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *e) {
6668
return builder.create<cir::ConstantOp>(loc, complexAttr);
6769
}
6870

71+
mlir::Value
72+
ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *il) {
73+
auto ty = mlir::cast<cir::ComplexType>(cgf.convertType(il->getType()));
74+
mlir::Type elementTy = ty.getElementType();
75+
mlir::Location loc = cgf.getLoc(il->getExprLoc());
76+
77+
mlir::TypedAttr realValueAttr;
78+
mlir::TypedAttr imagValueAttr;
79+
80+
if (mlir::isa<cir::IntType>(elementTy)) {
81+
llvm::APInt imagValue = cast<IntegerLiteral>(il->getSubExpr())->getValue();
82+
realValueAttr = cir::IntAttr::get(elementTy, 0);
83+
imagValueAttr = cir::IntAttr::get(elementTy, imagValue);
84+
} else {
85+
assert(mlir::isa<cir::CIRFPTypeInterface>(elementTy) &&
86+
"Expected complex element type to be floating-point");
87+
88+
llvm::APFloat imagValue =
89+
cast<FloatingLiteral>(il->getSubExpr())->getValue();
90+
realValueAttr = cir::FPAttr::get(
91+
elementTy, llvm::APFloat::getZero(imagValue.getSemantics()));
92+
imagValueAttr = cir::FPAttr::get(elementTy, imagValue);
93+
}
94+
95+
auto complexAttr = cir::ConstComplexAttr::get(realValueAttr, imagValueAttr);
96+
return builder.create<cir::ConstantOp>(loc, complexAttr);
97+
}
98+
6999
mlir::Value CIRGenFunction::emitComplexExpr(const Expr *e) {
70100
assert(e && getComplexType(e->getType()) &&
71101
"Invalid complex expression to emit");

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,32 @@ void foo7() {
176176
// OGCG: store float %[[TMP_A]], ptr %[[C_REAL_PTR]], align 4
177177
// OGCG: store float 2.000000e+00, ptr %[[C_IMAG_PTR]], align 4
178178

179+
void foo8() {
180+
double _Complex c = 2.00i;
181+
}
182+
183+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.fp<0.000000e+00> : !cir.double, #cir.fp<2.000000e+00> : !cir.double> : !cir.complex<!cir.double>
184+
185+
// LLVM: %[[COMPLEX:.*]] = alloca { double, double }, i64 1, align 8
186+
// LLVM: store { double, double } { double 0.000000e+00, double 2.000000e+00 }, ptr %[[COMPLEX]], align 8
187+
188+
// OGCG: %[[COMPLEX:.*]] = alloca { double, double }, align 8
189+
// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { double, double }, ptr %[[COMPLEX]], i32 0, i32 0
190+
// OGCG: %[[C_IMAG_PTR:.*]] = getelementptr inbounds nuw { double, double }, ptr %[[COMPLEX]], i32 0, i32 1
191+
// OGCG: store double 0.000000e+00, ptr %[[C_REAL_PTR]], align 8
192+
// OGCG: store double 2.000000e+00, ptr %[[C_IMAG_PTR]], align 8
193+
194+
void foo14() {
195+
int _Complex c = 2i;
196+
}
197+
198+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.int<0> : !s32i, #cir.int<2> : !s32i> : !cir.complex<!s32i>
199+
200+
// LLVM: %[[COMPLEX:.*]] = alloca { i32, i32 }, i64 1, align 4
201+
// LLVM: store { i32, i32 } { i32 0, i32 2 }, ptr %[[COMPLEX]], align 4
202+
203+
// OGCG: %[[COMPLEX:.*]] = alloca { i32, i32 }, align 4
204+
// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX]], i32 0, i32 0
205+
// OGCG: %[[C_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX]], i32 0, i32 1
206+
// OGCG: store i32 0, ptr %[[C_REAL_PTR]], align 4
207+
// OGCG: store i32 2, ptr %[[C_IMAG_PTR]], align 4

0 commit comments

Comments
 (0)