Skip to content

Commit 48c2115

Browse files
authored
[CIR] Implement CompoundLiteralExpr for ComplexType (#1701)
Implement CompoundLiteralExpr for ComplexType
1 parent 30718ac commit 48c2115

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,9 +2170,12 @@ void CIRGenFunction::emitAnyExprToMem(const Expr *E, Address Location,
21702170
Qualifiers Quals, bool IsInit) {
21712171
// FIXME: This function should take an LValue as an argument.
21722172
switch (getEvaluationKind(E->getType())) {
2173-
case cir::TEK_Complex:
2174-
assert(0 && "NYI");
2173+
case cir::TEK_Complex: {
2174+
RValue RV = RValue::get(emitComplexExpr(E));
2175+
LValue LV = makeAddrLValue(Location, E->getType());
2176+
emitStoreThroughLValue(RV, LV);
21752177
return;
2178+
}
21762179

21772180
case cir::TEK_Aggregate: {
21782181
emitAggExpr(E, AggValueSlot::forAddr(Location, Quals,

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
321321
mlir::Value VisitInitListExpr(InitListExpr *E);
322322

323323
mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
324-
llvm_unreachable("NYI");
324+
return emitLoadOfLValue(E);
325325
}
326326

327327
mlir::Value VisitVAArgExpr(VAArgExpr *E) { llvm_unreachable("NYI"); }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5+
6+
void foo() {
7+
int _Complex c = (int _Complex){1, 2};
8+
}
9+
10+
// CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["c", init]
11+
// CIR: %[[COMPOUND:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, [".compoundliteral", init]
12+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.complex<#cir.int<1> : !s32i, #cir.int<2> : !s32i> : !cir.complex<!s32i>
13+
// CIR: cir.store{{.*}} %[[COMPLEX]], %[[COMPOUND]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
14+
// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[COMPOUND]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
15+
// CIR: cir.store{{.*}} %[[TMP]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
16+
17+
// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4
18+
// LLVM: %[[COMPOUND:.*]] = alloca { i32, i32 }, i64 1, align 4
19+
// LLVM: store { i32, i32 } { i32 1, i32 2 }, ptr %[[COMPOUND]], align 4
20+
// LLVM: %[[TMP:.*]] = load { i32, i32 }, ptr %[[COMPOUND:.*]], align 4
21+
// LLVM: store { i32, i32 } %[[TMP]], ptr %[[INIT]], align 4
22+
23+
void foo2(float a, float b) {
24+
float _Complex c = (float _Complex){a, b};
25+
}
26+
27+
// CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["c", init]
28+
// CIR: %[[COMPOUND:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, [".compoundliteral", init]
29+
// CIR: %[[A:.*]] = cir.load{{.*}} {{.*}} : !cir.ptr<!cir.float>, !cir.float
30+
// CIR: %[[B:.*]] = cir.load{{.*}} {{.*}} : !cir.ptr<!cir.float>, !cir.float
31+
// CIR: %[[COMPLEX:.*]] = cir.complex.create %[[A]], %[[B]] : !cir.float -> !cir.complex<!cir.float>
32+
// CIR: cir.store{{.*}} %[[COMPLEX]], %[[COMPOUND]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
33+
// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[COMPOUND]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
34+
// CIR: cir.store{{.*}} %[[TMP]], %[[INIT]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
35+
36+
// LLVM: %[[INIT:.*]] = alloca { float, float }, i64 1, align 4
37+
// LLVM: %[[COMPOUND:.*]] = alloca { float, float }, i64 1, align 4
38+
// LLVM: %[[A:.*]] = load float, ptr {{.*}}, align 4
39+
// LLVM: %[[B:.*]] = load float, ptr {{.*}}, align 4
40+
// LLVM: %[[INSERT:.*]] = insertvalue { float, float } {{.*}}, float %[[A]], 0
41+
// LLVM: %[[INSERT_2:.*]] = insertvalue { float, float } %[[INSERT]], float %[[B]], 1
42+
// LLVM: store { float, float } %[[INSERT_2]], ptr %[[COMPOUND]], align 4
43+
// LLVM: %[[TMP:.*]] = load { float, float }, ptr %[[COMPOUND]], align 4
44+
// LLVM: store { float, float } %[[TMP]], ptr %[[INIT]], align 4

0 commit comments

Comments
 (0)