Skip to content

Commit 0605fc9

Browse files
committed
[CIR] Implement CXXScalarValueInitExpr for ComplexType
1 parent 968410f commit 0605fc9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
4747
mlir::Value VisitCallExpr(const CallExpr *e);
4848
mlir::Value VisitCastExpr(CastExpr *e);
4949
mlir::Value VisitChooseExpr(ChooseExpr *e);
50+
mlir::Value VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e);
5051
mlir::Value VisitDeclRefExpr(DeclRefExpr *e);
5152
mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *e);
5253
mlir::Value VisitImplicitCastExpr(ImplicitCastExpr *e);
@@ -201,6 +202,17 @@ mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) {
201202
return Visit(e->getChosenSubExpr());
202203
}
203204

205+
mlir::Value
206+
ComplexExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) {
207+
mlir::Location loc = cgf.getLoc(e->getExprLoc());
208+
QualType complexElemTy =
209+
e->getType()->castAs<clang::ComplexType>()->getElementType();
210+
mlir::Type complexElemLLVMTy = cgf.convertType(complexElemTy);
211+
mlir::TypedAttr defaultValue = builder.getZeroInitAttr(complexElemLLVMTy);
212+
auto complexAttr = cir::ConstComplexAttr::get(defaultValue, defaultValue);
213+
return builder.create<cir::ConstantOp>(loc, complexAttr);
214+
}
215+
204216
mlir::Value ComplexExprEmitter::VisitDeclRefExpr(DeclRefExpr *e) {
205217
if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e))
206218
return emitConstant(constant, e);

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,24 @@ void foo27(bool cond, int _Complex a, int _Complex b) {
717717
// OGCG: store i32 %[[REAL]], ptr %[[RESULT_REAL_PTR]], align 4
718718
// OGCG: store i32 %[[IMAG]], ptr %[[RESULT_IMAG_PTR]], align 4
719719

720+
void foo28() {
721+
using IntComplex = int _Complex;
722+
int _Complex a = IntComplex();
723+
}
724+
725+
// CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a", init]
726+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i>
727+
// CIR: cir.store align(4) %[[COMPLEX]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
728+
729+
// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4
730+
// LLVM: store { i32, i32 } zeroinitializer, ptr %[[INIT]], align 4
731+
732+
// OGCG: %[[INIT:.*]] = alloca { i32, i32 }, align 4
733+
// OGCG: %[[INIT_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 0
734+
// OGCG: %[[INIT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 1
735+
// OGCG: store i32 0, ptr %[[INIT_REAL_PTR]], align 4
736+
// OGCG: store i32 0, ptr %[[INIT_IMAG_PTR]], align 4
737+
720738
void foo29() {
721739
using IntComplex = int _Complex;
722740
int _Complex a = IntComplex{};

0 commit comments

Comments
 (0)