Skip to content

Commit 3c76a05

Browse files
authored
[CIR] Implement functional cast to ComplexType (#147147)
Implement functional cast to ComplexType #141365
1 parent b8f5cbb commit 3c76a05

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
4343
mlir::Value VisitBinAssign(const BinaryOperator *e);
4444
mlir::Value VisitBinComma(const BinaryOperator *e);
4545
mlir::Value VisitCallExpr(const CallExpr *e);
46+
mlir::Value VisitCastExpr(CastExpr *e);
4647
mlir::Value VisitChooseExpr(ChooseExpr *e);
4748
mlir::Value VisitDeclRefExpr(DeclRefExpr *e);
4849
mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *e);
@@ -83,12 +84,13 @@ LValue ComplexExprEmitter::emitBinAssignLValue(const BinaryOperator *e,
8384
mlir::Value ComplexExprEmitter::emitCast(CastKind ck, Expr *op,
8485
QualType destTy) {
8586
switch (ck) {
87+
case CK_NoOp:
8688
case CK_LValueToRValue:
8789
return Visit(op);
8890
default:
89-
cgf.cgm.errorNYI("ComplexType Cast");
9091
break;
9192
}
93+
cgf.cgm.errorNYI("ComplexType Cast");
9294
return {};
9395
}
9496

@@ -157,6 +159,21 @@ mlir::Value ComplexExprEmitter::VisitCallExpr(const CallExpr *e) {
157159
return cgf.emitCallExpr(e).getValue();
158160
}
159161

162+
mlir::Value ComplexExprEmitter::VisitCastExpr(CastExpr *e) {
163+
if (const auto *ece = dyn_cast<ExplicitCastExpr>(e)) {
164+
// Bind VLAs in the cast type.
165+
if (ece->getType()->isVariablyModifiedType()) {
166+
cgf.cgm.errorNYI("VisitCastExpr Bind VLAs in the cast type");
167+
return {};
168+
}
169+
}
170+
171+
if (e->changesVolatileQualification())
172+
return emitLoadOfLValue(e);
173+
174+
return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
175+
}
176+
160177
mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) {
161178
return Visit(e->getChosenSubExpr());
162179
}

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,21 @@ void foo26(int _Complex* a) {
654654
// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_B]], i32 0, i32 1
655655
// OGCG: store i32 %[[A_REAL]], ptr %[[B_REAL_PTR]], align 4
656656
// OGCG: store i32 %[[A_IMAG]], ptr %[[B_IMAG_PTR]], align 4
657+
658+
void foo29() {
659+
using IntComplex = int _Complex;
660+
int _Complex a = IntComplex{};
661+
}
662+
663+
// CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a", init]
664+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex<!s32i>
665+
// CIR: cir.store{{.*}} %[[COMPLEX]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
666+
667+
// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4
668+
// LLVM: store { i32, i32 } zeroinitializer, ptr %[[INIT]], align 4
669+
670+
// OGCG: %[[INIT:.*]] = alloca { i32, i32 }, align 4
671+
// OGCG: %[[INIT_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 0
672+
// OGCG: %[[INIT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[INIT]], i32 0, i32 1
673+
// OGCG: store i32 0, ptr %[[INIT_REAL_PTR]], align 4
674+
// OGCG: store i32 0, ptr %[[INIT_IMAG_PTR]], align 4

0 commit comments

Comments
 (0)