[clang] [CIR] Implement functional cast to ComplexType (PR #147147)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 5 06:59:17 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Amr Hesham (AmrDeveloper)
<details>
<summary>Changes</summary>
Implement functional cast to ComplexType
https://github.com/llvm/llvm-project/issues/141365
---
Full diff: https://github.com/llvm/llvm-project/pull/147147.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp (+18-1)
- (modified) clang/test/CIR/CodeGen/complex.cpp (+18)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
index c000b225f31f3..82b6a74a54b6c 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
@@ -43,6 +43,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
mlir::Value VisitBinAssign(const BinaryOperator *e);
mlir::Value VisitBinComma(const BinaryOperator *e);
mlir::Value VisitCallExpr(const CallExpr *e);
+ mlir::Value VisitCastExpr(CastExpr *e);
mlir::Value VisitChooseExpr(ChooseExpr *e);
mlir::Value VisitDeclRefExpr(DeclRefExpr *e);
mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *e);
@@ -83,12 +84,13 @@ LValue ComplexExprEmitter::emitBinAssignLValue(const BinaryOperator *e,
mlir::Value ComplexExprEmitter::emitCast(CastKind ck, Expr *op,
QualType destTy) {
switch (ck) {
+ case CK_NoOp:
case CK_LValueToRValue:
return Visit(op);
default:
- cgf.cgm.errorNYI("ComplexType Cast");
break;
}
+ cgf.cgm.errorNYI("ComplexType Cast");
return {};
}
@@ -157,6 +159,21 @@ mlir::Value ComplexExprEmitter::VisitCallExpr(const CallExpr *e) {
return cgf.emitCallExpr(e).getValue();
}
+mlir::Value ComplexExprEmitter::VisitCastExpr(CastExpr *e) {
+ if (const auto *ece = dyn_cast<ExplicitCastExpr>(e)) {
+ // Bind VLAs in the cast type.
+ if (ece->getType()->isVariablyModifiedType()) {
+ cgf.cgm.errorNYI("VisitCastExpr Bind VLAs in the cast type");
+ return {};
+ }
+ }
+
+ if (e->changesVolatileQualification())
+ return emitLoadOfLValue(e);
+
+ return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
+}
+
mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) {
return Visit(e->getChosenSubExpr());
}
diff --git a/clang/test/CIR/CodeGen/complex.cpp b/clang/test/CIR/CodeGen/complex.cpp
index 09e0ca03aa540..5d75f0e8a77c4 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 foo29() {
+ 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
``````````
</details>
https://github.com/llvm/llvm-project/pull/147147
More information about the cfe-commits
mailing list