Skip to content

Commit

Permalink
[CIR][CIRGen] Exceptions: unlock nested try/catch support
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardosolopes committed Sep 20, 2024
1 parent c947dc7 commit d00c3a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
17 changes: 2 additions & 15 deletions clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,8 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {

// Emit the EH cleanup if required.
if (RequiresEHCleanup) {
mlir::cir::TryOp tryOp = nullptr;
if (CGM.globalOpContext) {
SmallVector<mlir::cir::TryOp> trys;
CGM.globalOpContext.walk(
[&](mlir::cir::TryOp op) { trys.push_back(op); });
assert(trys.size() == 1 && "unknow global initialization style");
tryOp = trys[0];
} else {
SmallVector<mlir::cir::TryOp> trys;
auto funcOp = dyn_cast<mlir::cir::FuncOp>(CurFn);
funcOp.walk([&](mlir::cir::TryOp op) { trys.push_back(op); });
assert(trys.size() == 1 && "nested or multiple try/catch NYI");
tryOp = trys[0];
}

mlir::cir::TryOp tryOp =
ehEntry->getParentOp()->getParentOfType<mlir::cir::TryOp>();
assert(tryOp && "expected available cir.try");
auto *nextAction = getEHDispatchBlock(EHParent, tryOp);
(void)nextAction;
Expand Down
48 changes: 47 additions & 1 deletion clang/test/CIR/CodeGen/try-catch-dtors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,50 @@ void yo3(bool x) {
// LLVM: call ptr @__cxa_begin_catch
// LLVM: br label %[[RET]],
// LLVM: [[RET]]:
// LLVM: ret void
// LLVM: ret void

void yo2(bool x) {
int r = 1;
try {
Vec v1, v2;
try {
Vec v3, v4;
} catch (...) {
r++;
}
} catch (...) {
r++;
}
}

// CIR: cir.scope {
// CIR: %[[V1:.*]] = cir.alloca ![[VecTy]], !cir.ptr<![[VecTy]]>, ["v1"
// CIR: %[[V2:.*]] = cir.alloca ![[VecTy]], !cir.ptr<![[VecTy]]>, ["v2"
// CIR: cir.try {
// CIR: cir.call exception @_ZN3VecC1Ev(%[[V1]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.call exception @_ZN3VecC1Ev(%[[V2]]) : (!cir.ptr<![[VecTy]]>) -> () cleanup {
// CIR: cir.call @_ZN3VecD1Ev(%[[V1]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.yield
// CIR: }
// CIR: cir.scope {
// CIR: %[[V3:.*]] = cir.alloca ![[VecTy]], !cir.ptr<![[VecTy]]>, ["v3"
// CIR: %[[V4:.*]] = cir.alloca ![[VecTy]], !cir.ptr<![[VecTy]]>, ["v4"
// CIR: cir.try {
// CIR: cir.call exception @_ZN3VecC1Ev(%[[V3]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.call exception @_ZN3VecC1Ev(%[[V4]]) : (!cir.ptr<![[VecTy]]>) -> () cleanup {
// CIR: cir.call @_ZN3VecD1Ev(%[[V3]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.yield
// CIR: }
// CIR: cir.call @_ZN3VecD1Ev(%[[V4]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.call @_ZN3VecD1Ev(%[[V3]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.yield
// CIR: } catch [type #cir.all {
// CIR: cir.catch_param -> !cir.ptr<!void>
// CIR: }]
// CIR: }
// CIR: cir.call @_ZN3VecD1Ev(%[[V2]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.call @_ZN3VecD1Ev(%[[V1]]) : (!cir.ptr<![[VecTy]]>) -> ()
// CIR: cir.yield
// CIR: } catch [type #cir.all {
// CIR: cir.catch_param -> !cir.ptr<!void>
// CIR: }]

0 comments on commit d00c3a3

Please sign in to comment.