Skip to content

Commit 05fb5a2

Browse files
committed
[CIR][CIRGen] Exceptions: support nested scope cleanup
FlattenCFG will soon get the necessary support for lowering to LLVM, this is CIRGen only for now.
1 parent 6d5fb38 commit 05fb5a2

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenCleanup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,9 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
327327
// active or was used before it was deactivated.
328328
if (EHActiveFlag.isValid() || IsActive) {
329329
cleanupFlags.setIsForEHCleanup();
330-
assert(tryOp.isCleanupActive() && "expected active cleanup");
331330
mlir::OpBuilder::InsertionGuard guard(builder);
331+
if (!tryOp.isCleanupActive())
332+
builder.createBlock(&tryOp.getCleanupRegion());
332333
mlir::Block *cleanup = &tryOp.getCleanupRegion().back();
333334
if (cleanup->empty()) {
334335
builder.setInsertionPointToEnd(cleanup);

clang/test/CIR/CodeGen/try-catch-dtors.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -mconstructor-aliases -fclangir -emit-cir %s -o %t.cir
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fcxx-exceptions -fexceptions -mconstructor-aliases -fclangir -emit-cir %s -o %t.cir
22
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3-
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -mconstructor-aliases -fclangir -emit-llvm %s -o %t.ll
3+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -DLLVM_IMPLEMENTED -fcxx-exceptions -fexceptions -mconstructor-aliases -fclangir -emit-llvm %s -o %t.ll
44
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
55

66
struct Vec {
@@ -19,6 +19,7 @@ void yo() {
1919
}
2020

2121
// CIR-DAG: ![[VecTy:.*]] = !cir.struct<struct "Vec" {!cir.int<u, 8>}>
22+
// CIR-DAG: ![[S1:.*]] = !cir.struct<struct "S1" {!cir.struct<struct "Vec" {!cir.int<u, 8>}>}>
2223

2324
// CIR: cir.scope {
2425
// CIR: %[[VADDR:.*]] = cir.alloca ![[VecTy]], !cir.ptr<![[VecTy]]>, ["v", init]
@@ -58,4 +59,43 @@ void yo() {
5859
// LLVM: br label %[[RET_BB:.*]],
5960

6061
// LLVM: [[RET_BB]]:
61-
// LLVM: ret void
62+
// LLVM: ret void
63+
64+
#ifndef LLVM_IMPLEMENTED
65+
struct S1 {
66+
Vec v;
67+
};
68+
69+
void yo2() {
70+
int r = 1;
71+
try {
72+
Vec v;
73+
S1((Vec&&) v);
74+
} catch (...) {
75+
r++;
76+
}
77+
}
78+
#endif
79+
80+
// CIR: cir.func @_Z3yo2v()
81+
// CIR: cir.scope {
82+
// CIR: cir.alloca ![[VecTy]]
83+
// CIR: cir.try {
84+
// CIR: cir.call exception @_ZN3VecC1Ev
85+
// CIR: cir.scope {
86+
// CIR: cir.alloca ![[S1:.*]], !cir.ptr<![[S1:.*]]>, ["agg.tmp.ensured"]
87+
// CIR: cir.call exception @_ZN3VecC1EOS_
88+
// CIR: cir.call @_ZN2S1D2Ev
89+
// CIR: }
90+
// CIR: cir.call @_ZN3VecD1Ev
91+
// CIR: cir.yield
92+
// CIR: } cleanup {
93+
// CIR: cir.call @_ZN3VecD1Ev
94+
// CIR: cir.yield
95+
// CIR: } catch [type #cir.all {
96+
// CIR: cir.catch_param -> !cir.ptr<!void>
97+
// CIR: cir.yield
98+
// CIR: }]
99+
// CIR: }
100+
// CIR: cir.return
101+
// CIR: }

0 commit comments

Comments
 (0)