https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/147143
>From d8c01a7c98bea2440ab8b704501f7e3efb33549d Mon Sep 17 00:00:00 2001 From: AmrDeveloper <am...@programmer.net> Date: Sat, 5 Jul 2025 14:27:21 +0200 Subject: [PATCH 1/2] [CIR] Implement CXXScalarValueInitExpr for ComplexType --- clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp | 12 ++++++++++++ clang/test/CIR/CodeGen/complex.cpp | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp index c000b225f31f3..48024b457f848 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp @@ -44,6 +44,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> { mlir::Value VisitBinComma(const BinaryOperator *e); mlir::Value VisitCallExpr(const CallExpr *e); mlir::Value VisitChooseExpr(ChooseExpr *e); + mlir::Value VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e); mlir::Value VisitDeclRefExpr(DeclRefExpr *e); mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *e); mlir::Value VisitImplicitCastExpr(ImplicitCastExpr *e); @@ -161,6 +162,17 @@ mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) { return Visit(e->getChosenSubExpr()); } +mlir::Value +ComplexExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) { + mlir::Location loc = cgf.getLoc(e->getExprLoc()); + QualType complexElemTy = + e->getType()->castAs<clang::ComplexType>()->getElementType(); + mlir::Type complexElemLLVMTy = cgf.convertType(complexElemTy); + mlir::TypedAttr defaultValue = builder.getZeroInitAttr(complexElemLLVMTy); + auto complexAttr = cir::ConstComplexAttr::get(defaultValue, defaultValue); + return builder.create<cir::ConstantOp>(loc, complexAttr); +} + mlir::Value ComplexExprEmitter::VisitDeclRefExpr(DeclRefExpr *e) { if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e)) return emitConstant(constant, e); diff --git a/clang/test/CIR/CodeGen/complex.cpp b/clang/test/CIR/CodeGen/complex.cpp index 09e0ca03aa540..567fc40df6236 100644 --- a/clang/test/CIR/CodeGen/complex.cpp +++ b/clang/test/CIR/CodeGen/complex.cpp @@ -654,3 +654,21 @@ void foo26(int _Complex* a) { // OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_B]], i32 0, i32 1 // OGCG: store i32 %[[A_REAL]], ptr %[[B_REAL_PTR]], align 4 // OGCG: store i32 %[[A_IMAG]], ptr %[[B_IMAG_PTR]], align 4 + +void foo28() { + using IntComplex = int _Complex; + int _Complex a = IntComplex(); +} + +// CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a", init] +// CIR: %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i> +// CIR: cir.store align(4) %[[COMPLEX]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>> + +// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4 +// LLVM: store { i32, i32 } zeroinitializer, ptr %[[INIT]], align 4 + +// OGCG: %[[INIT:.*]] = alloca { i32, i32 }, align 4 +// OGCG: %[[INIT_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 0 +// OGCG: %[[INIT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 1 +// OGCG: store i32 0, ptr %[[INIT_REAL_PTR]], align 4 +// OGCG: store i32 0, ptr %[[INIT_IMAG_PTR]], align 4 >From d19361b0e27c6e52d5ef055713ded68961f4f4c4 Mon Sep 17 00:00:00 2001 From: AmrDeveloper <am...@programmer.net> Date: Mon, 7 Jul 2025 21:51:44 +0200 Subject: [PATCH 2/2] Replace creating cir::ConstComplexAttr with getNullValue --- clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp | 8 ++------ clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 13 +++++++++++-- clang/test/CIR/CodeGen/complex.cpp | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp index 48024b457f848..27c73870e1c4a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp @@ -165,12 +165,8 @@ mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) { mlir::Value ComplexExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) { mlir::Location loc = cgf.getLoc(e->getExprLoc()); - QualType complexElemTy = - e->getType()->castAs<clang::ComplexType>()->getElementType(); - mlir::Type complexElemLLVMTy = cgf.convertType(complexElemTy); - mlir::TypedAttr defaultValue = builder.getZeroInitAttr(complexElemLLVMTy); - auto complexAttr = cir::ConstComplexAttr::get(defaultValue, defaultValue); - return builder.create<cir::ConstantOp>(loc, complexAttr); + mlir::Type complexLLVMTy = cgf.convertType(e->getType()); + return builder.getNullValue(complexLLVMTy, loc); } mlir::Value ComplexExprEmitter::VisitDeclRefExpr(DeclRefExpr *e) { diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 5ac42b6a63b09..09dea282e5718 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -1044,10 +1044,19 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite( getTypeConverter())); return mlir::success(); } else if (auto complexTy = mlir::dyn_cast<cir::ComplexType>(op.getType())) { - auto complexAttr = mlir::cast<cir::ConstComplexAttr>(op.getValue()); - mlir::Type complexElemTy = complexTy.getElementType(); + auto complexElemTy = complexTy.getElementType(); mlir::Type complexElemLLVMTy = typeConverter->convertType(complexElemTy); + if (auto zeroInitAttr = mlir::dyn_cast<cir::ZeroAttr>(op.getValue())) { + mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(complexElemLLVMTy); + mlir::ArrayAttr array = rewriter.getArrayAttr({zeroAttr, zeroAttr}); + rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>( + op, getTypeConverter()->convertType(op.getType()), array); + return mlir::success(); + } + + auto complexAttr = mlir::cast<cir::ConstComplexAttr>(op.getValue()); + mlir::Attribute components[2]; if (mlir::isa<cir::IntType>(complexElemTy)) { components[0] = rewriter.getIntegerAttr( diff --git a/clang/test/CIR/CodeGen/complex.cpp b/clang/test/CIR/CodeGen/complex.cpp index 567fc40df6236..2984a46f3bad8 100644 --- a/clang/test/CIR/CodeGen/complex.cpp +++ b/clang/test/CIR/CodeGen/complex.cpp @@ -661,7 +661,7 @@ void foo28() { } // CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a", init] -// CIR: %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i> +// CIR: %[[COMPLEX:.*]] = cir.const #cir.zero : !cir.complex<!s32i> // CIR: cir.store align(4) %[[COMPLEX]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>> // LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits