Skip to content

Commit cdbeea7

Browse files
committed
[CIR] Upstream Support Init ComplexType from 1 size InitList
1 parent 9d11acc commit cdbeea7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
6060
mlir::Value VisitDeclRefExpr(DeclRefExpr *e);
6161
mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *e);
6262
mlir::Value VisitImplicitCastExpr(ImplicitCastExpr *e);
63-
mlir::Value VisitInitListExpr(const InitListExpr *e);
63+
mlir::Value VisitInitListExpr(InitListExpr *e);
6464

6565
mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) {
6666
return emitLoadOfLValue(e);
@@ -448,18 +448,16 @@ mlir::Value ComplexExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *e) {
448448
return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
449449
}
450450

451-
mlir::Value ComplexExprEmitter::VisitInitListExpr(const InitListExpr *e) {
451+
mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *e) {
452452
mlir::Location loc = cgf.getLoc(e->getExprLoc());
453453
if (e->getNumInits() == 2) {
454454
mlir::Value real = cgf.emitScalarExpr(e->getInit(0));
455455
mlir::Value imag = cgf.emitScalarExpr(e->getInit(1));
456456
return builder.createComplexCreate(loc, real, imag);
457457
}
458458

459-
if (e->getNumInits() == 1) {
460-
cgf.cgm.errorNYI("Create Complex with InitList with size 1");
461-
return {};
462-
}
459+
if (e->getNumInits() == 1)
460+
return Visit(e->getInit(0));
463461

464462
assert(e->getNumInits() == 0 && "Unexpected number of inits");
465463
mlir::Type complexTy = cgf.convertType(e->getType());

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,22 @@ void foo29() {
780780
// OGCG: %[[INIT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 1
781781
// OGCG: store i32 0, ptr %[[INIT_REAL_PTR]], align 4
782782
// OGCG: store i32 0, ptr %[[INIT_IMAG_PTR]], align 4
783+
784+
void foo30() {
785+
float _Complex a = { 1.0f };
786+
}
787+
788+
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["a", init]
789+
// CIR: %[[CONST_1F:.*]] = cir.const #cir.fp<1.000000e+00> : !cir.float
790+
// CIR: %[[CONST_0F:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
791+
// CIR: %[[COMPLEX:.*]] = cir.complex.create %[[CONST_1F]], %[[CONST_0F]] : !cir.float -> !cir.complex<!cir.float>
792+
// CIR: cir.store{{.*}} %[[COMPLEX]], %[[A_ADDR]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
793+
794+
// LLVM: %[[A_ADDR:.*]] = alloca { float, float }, i64 1, align 4
795+
// LLVM: store { float, float } { float 1.000000e+00, float 0.000000e+00 }, ptr %[[A_ADDR]], align 4
796+
797+
// OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
798+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
799+
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
800+
// OGCG: store float 1.000000e+00, ptr %[[A_REAL_PTR]], align 4
801+
// OGCG: store float 0.000000e+00, ptr %[[A_IMAG_PTR]], align 4

0 commit comments

Comments
 (0)