Skip to content

Commit 2b2e9e3

Browse files
committed
[CIR][FlattenCFG] Exceptions: support catch all only try's
1 parent e290bc0 commit 2b2e9e3

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,28 +236,20 @@ class CIRTryOpFlattening : public mlir::OpRewritePattern<mlir::cir::TryOp> {
236236
loc, mlir::cir::ExceptionInfoType::get(rewriter.getContext()));
237237
auto selector = rewriter.create<mlir::cir::EhSelectorOp>(loc, exception);
238238

239-
// TODO: direct catch all needs no dispatch?
240-
241239
// Handle dispatch. In could in theory use a switch, but let's just
242240
// mimic LLVM more closely since we have no specific thing to achieve
243241
// doing that (might not play as well with existing optimizers either).
244-
auto *dispatchBlock =
242+
auto *nextDispatcher =
245243
rewriter.splitBlock(catchBegin, rewriter.getInsertionPoint());
246244
rewriter.setInsertionPointToEnd(catchBegin);
247-
rewriter.create<mlir::cir::BrOp>(loc, dispatchBlock);
245+
rewriter.create<mlir::cir::BrOp>(loc, nextDispatcher);
248246

249247
// Fill in dispatcher.
250-
rewriter.setInsertionPointToEnd(dispatchBlock);
251-
252-
// FIXME: we should have an extra block for the dispatcher, just in case
253-
// there isn't one later.
254-
248+
rewriter.setInsertionPointToEnd(nextDispatcher);
255249
llvm::MutableArrayRef<mlir::Region> caseRegions = tryOp.getCatchRegions();
256250
mlir::ArrayAttr caseAttrList = tryOp.getCatchTypesAttr();
257251
unsigned caseCnt = 0;
258252

259-
mlir::Block *nextDispatcher = rewriter.getInsertionBlock();
260-
261253
for (mlir::Attribute caseAttr : caseAttrList) {
262254
if (auto typeIdGlobal = dyn_cast<mlir::cir::GlobalViewAttr>(caseAttr)) {
263255
auto typeId = rewriter.create<mlir::cir::EhTypeIdOp>(
@@ -275,7 +267,8 @@ class CIRTryOpFlattening : public mlir::OpRewritePattern<mlir::cir::TryOp> {
275267
nextDispatcher);
276268
rewriter.setInsertionPointToEnd(nextDispatcher);
277269
} else if (auto catchAll = dyn_cast<mlir::cir::CatchAllAttr>(caseAttr)) {
278-
assert(nextDispatcher->empty() && "expect empty dispatcher");
270+
// In case the catch(...) is all we got, `nextDispatcher` shall be
271+
// non-empty.
279272
buildAllCase(rewriter, caseRegions[caseCnt], afterTry, nextDispatcher);
280273
nextDispatcher = nullptr; // No more business in try/catch
281274
} else if (auto catchUnwind =

clang/test/CIR/Lowering/try-catch.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ unsigned long long tc() {
3636
// CIR_FLAT: ^[[BB_INT_IDX_CATCH]]: // pred: ^[[BB_INT_IDX_SEL]]
3737
// CIR_FLAT: %[[PARAM_INT_IDX:.*]] = cir.catch_param -> !cir.ptr<!s32i>
3838
// CIR_FLAT: cir.const #cir.int<98>
39-
// CIR_FLAT: cir.br ^[[AFTER_TRY]]
39+
// CIR_FLAT: cir.br ^[[AFTER_TRY]] loc
4040
z = 98;
4141
idx++;
4242
} catch (const char* msg) {
@@ -87,3 +87,25 @@ unsigned long long tc2() {
8787

8888
return z;
8989
}
90+
91+
// CIR_FLAT: cir.func @_Z3tc3v
92+
unsigned long long tc3() {
93+
int x = 50, y = 3;
94+
unsigned long long z;
95+
96+
try {
97+
z = division(x, y);
98+
} catch (...) {
99+
// CIR_FLAT: cir.eh.selector
100+
// CIR_FLAT: cir.br ^[[CATCH_ALL:.*]] loc
101+
// CIR_FLAT: ^[[CATCH_ALL]]:
102+
// CIR_FLAT: cir.catch_param -> !cir.ptr<!void>
103+
// CIR_FLAT: cir.const #cir.int<100> : !s32i
104+
// CIR_FLAT: cir.br ^[[AFTER_TRY:.*]] loc
105+
// CIR_FLAT: ^[[AFTER_TRY]]: // 2 preds
106+
// CIR_FLAT: cir.load
107+
z = 100;
108+
}
109+
110+
return z;
111+
}

0 commit comments

Comments
 (0)