Skip to content

Commit bc6df70

Browse files
authored
[CIR] Backport Complex init with empty init list (#1668)
Backport support for Complex value initialization from the empty InitList. Backported from llvm/llvm-project#143192
1 parent 960c0b0 commit bc6df70

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -864,10 +864,16 @@ mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
864864
if (E->getNumInits() == 1)
865865
return Visit(E->getInit(0));
866866

867-
// Empty init list initializes to null
868867
assert(E->getNumInits() == 0 && "Unexpected number of inits");
869-
QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
870-
return Builder.getZero(CGF.getLoc(E->getExprLoc()), CGF.convertType(Ty));
868+
mlir::Location loc = CGF.getLoc(E->getExprLoc());
869+
QualType complexElemTy =
870+
E->getType()->castAs<clang::ComplexType>()->getElementType();
871+
mlir::Type complexElemLLVMTy = CGF.convertType(complexElemTy);
872+
mlir::TypedAttr defaultValue = Builder.getZeroInitAttr(complexElemLLVMTy);
873+
auto complexTy = cir::ComplexType::get(complexElemLLVMTy);
874+
auto complexAttr =
875+
cir::ComplexAttr::get(complexTy, defaultValue, defaultValue);
876+
return Builder.create<cir::ConstantOp>(loc, complexAttr);
871877
}
872878

873879
mlir::Value CIRGenFunction::emitPromotedComplexExpr(const Expr *E,

clang/test/CIR/CodeGen/complex.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,7 @@ void extract_imag() {
335335
// LLVM: %{{.+}} = load double, ptr getelementptr inbounds nuw (i8, ptr @c, i64 8), align 8
336336
// LLVM: %{{.+}} = load i32, ptr getelementptr inbounds nuw (i8, ptr @ci, i64 4), align 4
337337
// LLVM: }
338+
339+
void complex_with_empty_init() { int _Complex c = {}; }
340+
341+
// CHECK: {{.*}} = cir.const #cir.complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i>

0 commit comments

Comments
 (0)