diff options
author | Amr Hesham <amr96@programmer.net> | 2025-07-07 17:48:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-07 17:48:50 +0200 |
commit | 3c76a054ac4c7c93b197f3f1aec6c9f72ece1377 (patch) | |
tree | eb0853a33891fa2a44991c7e9b8bf448d1317597 | |
parent | b8f5cbb4ffbf3901425a6e4f5d86eb8cba5b0ddd (diff) | |
download | llvm-3c76a054ac4c7c93b197f3f1aec6c9f72ece1377.zip llvm-3c76a054ac4c7c93b197f3f1aec6c9f72ece1377.tar.gz llvm-3c76a054ac4c7c93b197f3f1aec6c9f72ece1377.tar.bz2 |
[CIR] Implement functional cast to ComplexType (#147147)
Implement functional cast to ComplexType
https://github.com/llvm/llvm-project/issues/141365
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp | 19 | ||||
-rw-r--r-- | clang/test/CIR/CodeGen/complex.cpp | 18 |
2 files changed, 36 insertions, 1 deletions
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp index c000b22..82b6a74 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp @@ -43,6 +43,7 @@ public: 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 09e0ca0..25b0078 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{{.*}} %[[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 |