Skip to content

Commit cfad41c

Browse files
authored
[CIR] Upstream l-value emission for ExprWithCleanups (#167938)
This adds the necessary handler for emitting an l-value for an ExprWithCleanups expression.
1 parent b1262d1 commit cfad41c

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ struct MissingFeatures {
226226
static bool cleanupAppendInsts() { return false; }
227227
static bool cleanupBranchThrough() { return false; }
228228
static bool cleanupIndexAndBIAdjustment() { return false; }
229+
static bool cleanupWithPreservedValues() { return false; }
229230
static bool cleanupsToDeactivate() { return false; }
230231
static bool constEmitterAggILE() { return false; }
231232
static bool constEmitterArrayILE() { return false; }

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,13 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
921921
case Expr::CXXOperatorCallExprClass:
922922
case Expr::UserDefinedLiteralClass:
923923
return emitCallExprLValue(cast<CallExpr>(e));
924+
case Expr::ExprWithCleanupsClass: {
925+
const auto *cleanups = cast<ExprWithCleanups>(e);
926+
RunCleanupsScope scope(*this);
927+
LValue lv = emitLValue(cleanups->getSubExpr());
928+
assert(!cir::MissingFeatures::cleanupWithPreservedValues());
929+
return lv;
930+
}
924931
case Expr::ParenExprClass:
925932
return emitLValue(cast<ParenExpr>(e)->getSubExpr());
926933
case Expr::GenericSelectionExprClass:
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3+
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
5+
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
7+
8+
int make_int();
9+
10+
int test() {
11+
const int &x = make_int();
12+
return x;
13+
}
14+
15+
// CIR: cir.func {{.*}} @_Z4testv()
16+
// CIR: %[[TEMP_SLOT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["ref.tmp0", init]
17+
// CIR-NEXT: %[[X:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["x", init, const]
18+
// CIR-NEXT: %[[TEMP_VALUE:.*]] = cir.call @_Z8make_intv() : () -> !s32i
19+
// CIR-NEXT: cir.store{{.*}} %[[TEMP_VALUE]], %[[TEMP_SLOT]]
20+
// CIR-NEXT: cir.store{{.*}} %[[TEMP_SLOT]], %[[X]]
21+
22+
// LLVM: define {{.*}} i32 @_Z4testv()
23+
// LLVM: %[[RETVAL:.*]] = alloca i32
24+
// LLVM: %[[TEMP_SLOT:.*]] = alloca i32
25+
// LLVM: %[[X:.*]] = alloca ptr
26+
// LLVM: %[[TEMP_VALUE:.*]] = call i32 @_Z8make_intv()
27+
// LLVM: store i32 %[[TEMP_VALUE]], ptr %[[TEMP_SLOT]]
28+
// LLVM: store ptr %[[TEMP_SLOT]], ptr %[[X]]
29+
30+
// OGCG: define {{.*}} i32 @_Z4testv()
31+
// OGCG: %[[X:.*]] = alloca ptr
32+
// OGCG: %[[TEMP_SLOT:.*]] = alloca i32
33+
// OGCG: %[[TEMP_VALUE:.*]] = call noundef i32 @_Z8make_intv()
34+
// OGCG: store i32 %[[TEMP_VALUE]], ptr %[[TEMP_SLOT]]
35+
// OGCG: store ptr %[[TEMP_SLOT]], ptr %[[X]]
36+
37+
int test_scoped() {
38+
int x = make_int();
39+
{
40+
const int &y = make_int();
41+
x = y;
42+
}
43+
return x;
44+
}
45+
46+
// CIR: cir.func {{.*}} @_Z11test_scopedv()
47+
// CIR: %[[X:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init]
48+
// CIR: cir.scope {
49+
// CIR-NEXT: %[[TEMP_SLOT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["ref.tmp0", init]
50+
// CIR-NEXT: %[[Y_ADDR:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["y", init, const]
51+
// CIR-NEXT: %[[TEMP_VALUE:.*]] = cir.call @_Z8make_intv() : () -> !s32i
52+
// CIR-NEXT: cir.store{{.*}} %[[TEMP_VALUE]], %[[TEMP_SLOT]] : !s32i, !cir.ptr<!s32i>
53+
// CIR-NEXT: cir.store{{.*}} %[[TEMP_SLOT]], %[[Y_ADDR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
54+
// CIR-NEXT: %[[Y_REF:.*]] = cir.load %[[Y_ADDR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
55+
// CIR-NEXT: %[[Y_VALUE:.*]] = cir.load{{.*}} %[[Y_REF]] : !cir.ptr<!s32i>, !s32i
56+
// CIR-NEXT: cir.store{{.*}} %[[Y_VALUE]], %[[X]] : !s32i, !cir.ptr<!s32i>
57+
// CIR-NEXT: }
58+
59+
// LLVM: define {{.*}} i32 @_Z11test_scopedv()
60+
// LLVM: %[[TEMP_SLOT:.*]] = alloca i32
61+
// LLVM: %[[Y_ADDR:.*]] = alloca ptr
62+
// LLVM: %[[RETVAL:.*]] = alloca i32
63+
// LLVM: %[[X:.*]] = alloca i32
64+
// LLVM: %[[TEMP_VALUE1:.*]] = call i32 @_Z8make_intv()
65+
// LLVM: store i32 %[[TEMP_VALUE1]], ptr %[[X]]
66+
// LLVM: br label %[[SCOPE_LABEL:.*]]
67+
// LLVM: [[SCOPE_LABEL]]:
68+
// LLVM: %[[TEMP_VALUE2:.*]] = call i32 @_Z8make_intv()
69+
// LLVM: store i32 %[[TEMP_VALUE2]], ptr %[[TEMP_SLOT]]
70+
// LLVM: store ptr %[[TEMP_SLOT]], ptr %[[Y_ADDR]]
71+
// LLVM: %[[Y_REF:.*]] = load ptr, ptr %[[Y_ADDR]]
72+
// LLVM: %[[Y_VALUE:.*]] = load i32, ptr %[[Y_REF]]
73+
// LLVM: store i32 %[[Y_VALUE]], ptr %[[X]]
74+
75+
// OGCG: define {{.*}} i32 @_Z11test_scopedv()
76+
// OGCG: %[[X:.*]] = alloca i32
77+
// OGCG: %[[Y_ADDR:.*]] = alloca ptr
78+
// OGCG: %[[TEMP_SLOT:.*]] = alloca i32
79+
// OGCG: %[[TEMP_VALUE1:.*]] = call noundef i32 @_Z8make_intv()
80+
// OGCG: store i32 %[[TEMP_VALUE1]], ptr %[[X]]
81+
// OGCG: %[[TEMP_VALUE2:.*]] = call noundef i32 @_Z8make_intv()
82+
// OGCG: store i32 %[[TEMP_VALUE2]], ptr %[[TEMP_SLOT]]
83+
// OGCG: store ptr %[[TEMP_SLOT]], ptr %[[Y_ADDR]]
84+
// OGCG: %[[Y_REF:.*]] = load ptr, ptr %[[Y_ADDR]]
85+
// OGCG: %[[Y_VALUE:.*]] = load i32, ptr %[[Y_REF]]
86+
// OGCG: store i32 %[[Y_VALUE]], ptr %[[X]]

0 commit comments

Comments
 (0)