-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[CIR] Implement CXXScalarValueInitExpr for ComplexType #147143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CIR] Implement CXXScalarValueInitExpr for ComplexType #147143
Conversation
@llvm/pr-subscribers-clang Author: Amr Hesham (AmrDeveloper) ChangesImplement CXXScalarValueInitExpr support for ComplexType Full diff: https://github.com/llvm/llvm-project/pull/147143.diff 2 Files Affected:
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
|
@llvm/pr-subscribers-clangir Author: Amr Hesham (AmrDeveloper) ChangesImplement CXXScalarValueInitExpr support for ComplexType Full diff: https://github.com/llvm/llvm-project/pull/147143.diff 2 Files Affected:
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after minor nit
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: inline the cir::ConstComplexAttr::get
directly into the create call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getZeroInitAttr
also works with ComplexType
that returns cir::ZeroAttr::get(complexType)
I guess that would be more appropriate use here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And even nicer return builder.getNullValue(loc, complexType);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used getNullValue, it's nicer and required some changes in CIRToLLVMConstantOpLowering::matchAndRewrite
because previously we expected the value to be ConstComplexAttr, so I supported ZeroAttr too, I will backport this change after merging
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is cir not llvm type. Rename to complexTy
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right, I will rename it
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good after nits are addressed.
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use auto here.
d19361b
to
9a9db0c
Compare
9a9db0c
to
7732455
Compare
Implement CXXScalarValueInitExpr support for ComplexType
#141365