Skip to content

Commit a8000bb

Browse files
authored
[CIR] Use ZeroInit to init null value ComplexType (#1960)
Use ZeroInitAttr to initialize a null value for a ComplexType with 0 number of inits, not building ConstComplexType similar to the upstream
1 parent c0f9f45 commit a8000bb

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -918,25 +918,19 @@ ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
918918
}
919919

920920
mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
921+
mlir::Location loc = CGF.getLoc(E->getExprLoc());
921922
if (E->getNumInits() == 2) {
922923
mlir::Value Real = CGF.emitScalarExpr(E->getInit(0));
923924
mlir::Value Imag = CGF.emitScalarExpr(E->getInit(1));
924-
return Builder.createComplexCreate(CGF.getLoc(E->getExprLoc()), Real, Imag);
925+
return Builder.createComplexCreate(loc, Real, Imag);
925926
}
926927

927928
if (E->getNumInits() == 1)
928929
return Visit(E->getInit(0));
929930

930931
assert(E->getNumInits() == 0 && "Unexpected number of inits");
931-
mlir::Location loc = CGF.getLoc(E->getExprLoc());
932-
QualType complexElemTy =
933-
E->getType()->castAs<clang::ComplexType>()->getElementType();
934-
mlir::Type complexElemLLVMTy = CGF.convertType(complexElemTy);
935-
mlir::TypedAttr defaultValue = Builder.getZeroInitAttr(complexElemLLVMTy);
936-
auto complexTy = cir::ComplexType::get(complexElemLLVMTy);
937-
auto complexAttr =
938-
cir::ComplexAttr::get(complexTy, defaultValue, defaultValue);
939-
return Builder.create<cir::ConstantOp>(loc, complexAttr);
932+
mlir::Type complexTy = CGF.convertType(E->getType());
933+
return Builder.getNullValue(complexTy, loc);
940934
}
941935

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

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void complex_functional_cast() {
1111
}
1212

1313
// CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a", init]
14-
// CIR: %[[COMPLEX:.*]] = cir.const #cir.complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i>
14+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.zero : !cir.complex<!s32i>
1515
// CIR: cir.store{{.*}} %[[COMPLEX]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
1616

1717
// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4
@@ -90,7 +90,7 @@ int _Complex complex_real_operator_on_rvalue() {
9090
// CIR: %[[CALL:.*]] = cir.call @_Z31complex_real_operator_on_rvaluev() : () -> !cir.complex<!s32i>
9191
// CIR: %[[REAL:.*]] = cir.complex.real %[[CALL]] : !cir.complex<!s32i> -> !s32i
9292
// CIR: cir.store{{.*}} %[[REAL]], %[[REAL_ADDR]] : !s32i, !cir.ptr<!s32i>
93-
// CIR: %[[RET_COMPLEX:.*]] = cir.const #cir.complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i>
93+
// CIR: %[[RET_COMPLEX:.*]] = cir.const #cir.zero : !cir.complex<!s32i>
9494
// CIR: cir.store{{.*}} %[[RET_COMPLEX]], %[[RET_ADDR]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
9595
// CIR: %[[TMP_RET:.*]] = cir.load %[[RET_ADDR]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
9696
// CIR: cir.return %[[TMP_RET]] : !cir.complex<!s32i>
@@ -137,7 +137,7 @@ int _Complex complex_imag_operator_on_rvalue() {
137137
// CIR: %[[CALL:.*]] = cir.call @_Z31complex_imag_operator_on_rvaluev() : () -> !cir.complex<!s32i>
138138
// CIR: %[[IMAG:.*]] = cir.complex.imag %[[CALL]] : !cir.complex<!s32i> -> !s32i
139139
// CIR: cir.store{{.*}} %[[IMAG]], %[[IMAG_ADDR]] : !s32i, !cir.ptr<!s32i>
140-
// CIR: %[[RET_COMPLEX:.*]] = cir.const #cir.complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i>
140+
// CIR: %[[RET_COMPLEX:.*]] = cir.const #cir.zero : !cir.complex<!s32i>
141141
// CIR: cir.store{{.*}} %[[RET_COMPLEX]], %[[RET_ADDR]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
142142
// CIR: %[[TMP_RET:.*]] = cir.load %[[RET_ADDR]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
143143
// CIR: cir.return %[[TMP_RET]] : !cir.complex<!s32i>
@@ -197,7 +197,7 @@ void complex_cxx_default_init_expr() {
197197

198198
// CIR: %[[W_ADDR:.*]] = cir.alloca !rec_FPComplexWrapper, !cir.ptr<!rec_FPComplexWrapper>, ["w", init]
199199
// CIR: %[[C_ADDR:.*]] = cir.get_member %[[W_ADDR]][0] {name = "c"} : !cir.ptr<!rec_FPComplexWrapper> -> !cir.ptr<!cir.complex<!cir.float>>
200-
// CIR: %[[CONST_COMPLEX:.*]] = cir.const #cir.complex<#cir.fp<0.000000e+00> : !cir.float, #cir.fp<0.000000e+00> : !cir.float> : !cir.complex<!cir.float>
200+
// CIR: %[[CONST_COMPLEX:.*]] = cir.const #cir.zero : !cir.complex<!cir.float>
201201
// CIR: cir.store{{.*}} %[[CONST_COMPLEX]], %[[C_ADDR]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
202202

203203
// LLVM: %[[W_ADDR:.*]] = alloca %struct.FPComplexWrapper, i64 1, align 4

0 commit comments

Comments
 (0)