diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp index 775cc3b5cd80..a147f8d119f4 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp @@ -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 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 trys; - auto funcOp = dyn_cast(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(); assert(tryOp && "expected available cir.try"); auto *nextAction = getEHDispatchBlock(EHParent, tryOp); (void)nextAction; diff --git a/clang/test/CIR/CodeGen/try-catch-dtors.cpp b/clang/test/CIR/CodeGen/try-catch-dtors.cpp index c8ca7e1765b1..3f5e2b9d46d2 100644 --- a/clang/test/CIR/CodeGen/try-catch-dtors.cpp +++ b/clang/test/CIR/CodeGen/try-catch-dtors.cpp @@ -263,4 +263,50 @@ void yo3(bool x) { // LLVM: call ptr @__cxa_begin_catch // LLVM: br label %[[RET]], // LLVM: [[RET]]: -// LLVM: ret void \ No newline at end of file +// 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, ["v1" +// CIR: %[[V2:.*]] = cir.alloca ![[VecTy]], !cir.ptr, ["v2" +// CIR: cir.try { +// CIR: cir.call exception @_ZN3VecC1Ev(%[[V1]]) : (!cir.ptr) -> () +// CIR: cir.call exception @_ZN3VecC1Ev(%[[V2]]) : (!cir.ptr) -> () cleanup { +// CIR: cir.call @_ZN3VecD1Ev(%[[V1]]) : (!cir.ptr) -> () +// CIR: cir.yield +// CIR: } +// CIR: cir.scope { +// CIR: %[[V3:.*]] = cir.alloca ![[VecTy]], !cir.ptr, ["v3" +// CIR: %[[V4:.*]] = cir.alloca ![[VecTy]], !cir.ptr, ["v4" +// CIR: cir.try { +// CIR: cir.call exception @_ZN3VecC1Ev(%[[V3]]) : (!cir.ptr) -> () +// CIR: cir.call exception @_ZN3VecC1Ev(%[[V4]]) : (!cir.ptr) -> () cleanup { +// CIR: cir.call @_ZN3VecD1Ev(%[[V3]]) : (!cir.ptr) -> () +// CIR: cir.yield +// CIR: } +// CIR: cir.call @_ZN3VecD1Ev(%[[V4]]) : (!cir.ptr) -> () +// CIR: cir.call @_ZN3VecD1Ev(%[[V3]]) : (!cir.ptr) -> () +// CIR: cir.yield +// CIR: } catch [type #cir.all { +// CIR: cir.catch_param -> !cir.ptr +// CIR: }] +// CIR: } +// CIR: cir.call @_ZN3VecD1Ev(%[[V2]]) : (!cir.ptr) -> () +// CIR: cir.call @_ZN3VecD1Ev(%[[V1]]) : (!cir.ptr) -> () +// CIR: cir.yield +// CIR: } catch [type #cir.all { +// CIR: cir.catch_param -> !cir.ptr +// CIR: }]