Skip to content

Commit 6633ffe

Browse files
committed
Replace creating cir::ConstComplexAttr with getNullValue
1 parent 0605fc9 commit 6633ffe

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,8 @@ mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) {
205205
mlir::Value
206206
ComplexExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) {
207207
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);
208+
mlir::Type complexLLVMTy = cgf.convertType(e->getType());
209+
return builder.getNullValue(complexLLVMTy, loc);
214210
}
215211

216212
mlir::Value ComplexExprEmitter::VisitDeclRefExpr(DeclRefExpr *e) {

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,19 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
10441044
getTypeConverter()));
10451045
return mlir::success();
10461046
} else if (auto complexTy = mlir::dyn_cast<cir::ComplexType>(op.getType())) {
1047-
auto complexAttr = mlir::cast<cir::ConstComplexAttr>(op.getValue());
1048-
mlir::Type complexElemTy = complexTy.getElementType();
1047+
auto complexElemTy = complexTy.getElementType();
10491048
mlir::Type complexElemLLVMTy = typeConverter->convertType(complexElemTy);
10501049

1050+
if (auto zeroInitAttr = mlir::dyn_cast<cir::ZeroAttr>(op.getValue())) {
1051+
mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(complexElemLLVMTy);
1052+
mlir::ArrayAttr array = rewriter.getArrayAttr({zeroAttr, zeroAttr});
1053+
rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(
1054+
op, getTypeConverter()->convertType(op.getType()), array);
1055+
return mlir::success();
1056+
}
1057+
1058+
auto complexAttr = mlir::cast<cir::ConstComplexAttr>(op.getValue());
1059+
10511060
mlir::Attribute components[2];
10521061
if (mlir::isa<cir::IntType>(complexElemTy)) {
10531062
components[0] = rewriter.getIntegerAttr(

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ void foo28() {
723723
}
724724

725725
// 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>
726+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.zero : !cir.complex<!s32i>
727727
// CIR: cir.store align(4) %[[COMPLEX]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
728728

729729
// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4

0 commit comments

Comments
 (0)