[clang] bbefd33 - [CIR] Implement CXXScalarValueInitExpr for ComplexType (#147143)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 8 08:36:44 PDT 2025
Author: Amr Hesham
Date: 2025-07-08T17:36:40+02:00
New Revision: bbefd33ae619977d44b221e9917143e7f92aab94
URL: https://github.com/llvm/llvm-project/commit/bbefd33ae619977d44b221e9917143e7f92aab94
DIFF: https://github.com/llvm/llvm-project/commit/bbefd33ae619977d44b221e9917143e7f92aab94.diff
LOG: [CIR] Implement CXXScalarValueInitExpr for ComplexType (#147143)
Implement CXXScalarValueInitExpr support for ComplexType
https://github.com/llvm/llvm-project/issues/141365
Added:
Modified:
clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
clang/test/CIR/CodeGen/complex.cpp
Removed:
################################################################################
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
index 55c7db7b851b2..84fad959ebf49 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
@@ -47,6 +47,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
mlir::Value VisitCallExpr(const CallExpr *e);
mlir::Value VisitCastExpr(CastExpr *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);
@@ -201,6 +202,13 @@ mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) {
return Visit(e->getChosenSubExpr());
}
+mlir::Value
+ComplexExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) {
+ mlir::Location loc = cgf.getLoc(e->getExprLoc());
+ mlir::Type complexTy = cgf.convertType(e->getType());
+ return builder.getNullValue(complexTy, loc);
+}
+
mlir::Value ComplexExprEmitter::VisitDeclRefExpr(DeclRefExpr *e) {
if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e))
return emitConstant(constant, e);
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 5ac42b6a63b09..af307f6ad673d 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();
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 25d81785ef482..6e7e889df146f 100644
--- a/clang/test/CIR/CodeGen/complex.cpp
+++ b/clang/test/CIR/CodeGen/complex.cpp
@@ -717,6 +717,24 @@ void foo27(bool cond, int _Complex a, int _Complex b) {
// OGCG: store i32 %[[REAL]], ptr %[[RESULT_REAL_PTR]], align 4
// OGCG: store i32 %[[IMAG]], ptr %[[RESULT_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.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
+// 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
+
void foo29() {
using IntComplex = int _Complex;
int _Complex a = IntComplex{};
More information about the cfe-commits
mailing list