Skip to content

Commit b8277b4

Browse files
authored
[CIR] Upstream Support Init ComplexType from 1 size InitList (#150293)
This change adds support for init ComplexType from InitList with 1 size #141365
1 parent e4dea2d commit b8277b4

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);
@@ -452,18 +452,16 @@ mlir::Value ComplexExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *e) {
452452
return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
453453
}
454454

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

463-
if (e->getNumInits() == 1) {
464-
cgf.cgm.errorNYI("Create Complex with InitList with size 1");
465-
return {};
466-
}
463+
if (e->getNumInits() == 1)
464+
return Visit(e->getInit(0));
467465

468466
assert(e->getNumInits() == 0 && "Unexpected number of inits");
469467
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)