diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp index c98d586ade9c..03548c5d3ffe 100644 --- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp +++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp @@ -435,6 +435,10 @@ class CIRTryOpFlattening : public mlir::OpRewritePattern { if (auto tryBodyYield = dyn_cast(afterBody->getTerminator())) rewriter.replaceOpWithNewOp(tryBodyYield, afterTry); + mlir::ArrayAttr catches = tryOp.getCatchTypesAttr(); + if (!catches || catches.empty()) + return; + // Start the landing pad by getting the inflight exception information. mlir::Block *nextDispatcher = buildLandingPads(tryOp, rewriter, beforeCatch, afterTry, callsToRewrite, diff --git a/clang/test/CIR/CodeGen/try-catch.cpp b/clang/test/CIR/CodeGen/try-catch.cpp index 0b2c166cda46..908e899174d8 100644 --- a/clang/test/CIR/CodeGen/try-catch.cpp +++ b/clang/test/CIR/CodeGen/try-catch.cpp @@ -1,5 +1,7 @@ // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -mconstructor-aliases -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -mconstructor-aliases -fclangir -emit-cir-flat %s -o %t.cir.flat +// RUN: FileCheck --check-prefix=FLAT --input-file=%t.cir.flat %s double division(int a, int b); @@ -168,3 +170,38 @@ void tc7() { // CHECK: cir.return // CHECK: } // CHECK: } + +struct S2 { + int a, b; +}; + +void tc8() { + try { + S2 s{1, 2}; + } catch (...) { + } +} + +// CHECK: cir.scope { +// CHECK: %[[V0:.*]] = cir.alloca !rec_S2, !cir.ptr, ["s"] {alignment = 4 : i64} +// CHECK: cir.try { +// CHECK: %[[V1:.*]] = cir.const #cir.const_record<{#cir.int<1> : !s32i, #cir.int<2> : !s32i}> : !rec_S2 +// CHECK: cir.store align(4) %[[V1]], %[[V0]] : !rec_S2, !cir.ptr +// CHECK: cir.yield +// CHECK: } +// CHECK: } + +// FLAT: cir.func @_Z3tc8v() +// FLAT: %[[V0:.*]] = cir.alloca !rec_S2, !cir.ptr, ["s"] {alignment = 4 : i64} +// FLAT: cir.br ^bb[[#B1:]] +// FLAT: ^bb[[#B1]]: +// FLAT: cir.br ^bb[[#B2:]] +// FLAT: ^bb[[#B2]]: +// FLAT: %[[V1:.*]] = cir.const #cir.const_record<{#cir.int<1> : !s32i, #cir.int<2> : !s32i}> : !rec_S2 +// FLAT: cir.store align(4) %[[V1]], %[[V0]] : !rec_S2, !cir.ptr +// FLAT: cir.br ^bb[[#B3:]] +// FLAT: ^bb[[#B3]]: +// FLAT: cir.br ^bb[[#B4:]] +// FLAT: ^bb[[#B4]]: +// FLAT: cir.return +// FLAT: }