Skip to content

Commit fe5310b

Browse files
committed
[CIR][NFC] Add helpers for cir.try and do some refactoring
1 parent 37397b3 commit fe5310b

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3570,6 +3570,15 @@ def TryOp : CIR_Op<"try",
35703570
attr-dict
35713571
}];
35723572

3573+
let extraClassDeclaration = [{
3574+
private:
3575+
mlir::Region *getCatchLastRegion();
3576+
public:
3577+
mlir::Block *getCatchAllEntryBlock();
3578+
mlir::Block *getCatchUnwindEntryBlock();
3579+
bool isCatchAllOnly();
3580+
}];
3581+
35733582
// Everything already covered elsewhere.
35743583
let hasVerifier = 0;
35753584
let builders = [

clang/lib/CIR/CodeGen/CIRGenException.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,6 @@ void CIRGenFunction::buildAnyExprToExn(const Expr *e, Address addr) {
252252
DeactivateCleanupBlock(cleanup, op);
253253
}
254254

255-
static mlir::Block *getResumeBlockFromCatch(mlir::cir::TryOp &tryOp,
256-
mlir::cir::GlobalOp globalParent) {
257-
assert(tryOp && "cir.try expected");
258-
unsigned numCatchRegions = tryOp.getCatchRegions().size();
259-
assert(numCatchRegions && "expected at least one region");
260-
auto &fallbackRegion = tryOp.getCatchRegions()[numCatchRegions - 1];
261-
return &fallbackRegion.getBlocks().back();
262-
return nullptr;
263-
}
264-
265255
mlir::Block *CIRGenFunction::getEHResumeBlock(bool isCleanup,
266256
mlir::cir::TryOp tryOp) {
267257

@@ -270,7 +260,8 @@ mlir::Block *CIRGenFunction::getEHResumeBlock(bool isCleanup,
270260
// Just like some other try/catch related logic: return the basic block
271261
// pointer but only use it to denote we're tracking things, but there
272262
// shouldn't be any changes to that block after work done in this function.
273-
ehResumeBlock = getResumeBlockFromCatch(tryOp, CGM.globalOpContext);
263+
assert(tryOp && "expected available cir.try");
264+
ehResumeBlock = tryOp.getCatchUnwindEntryBlock();
274265
if (!ehResumeBlock->empty())
275266
return ehResumeBlock;
276267

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,27 @@ void TryOp::build(
12431243
catchBuilder(builder, result.location, result);
12441244
}
12451245

1246+
mlir::Region *TryOp::getCatchLastRegion() {
1247+
unsigned numCatchRegions = getCatchRegions().size();
1248+
assert(numCatchRegions && "expected at least one region");
1249+
auto &lastRegion = getCatchRegions()[numCatchRegions - 1];
1250+
return &lastRegion;
1251+
}
1252+
1253+
mlir::Block *TryOp::getCatchUnwindEntryBlock() {
1254+
return &getCatchLastRegion()->getBlocks().front();
1255+
}
1256+
1257+
mlir::Block *TryOp::getCatchAllEntryBlock() {
1258+
return &getCatchLastRegion()->getBlocks().front();
1259+
}
1260+
1261+
bool TryOp::isCatchAllOnly() {
1262+
mlir::ArrayAttr catchAttrList = getCatchTypesAttr();
1263+
return catchAttrList.size() == 1 &&
1264+
isa<mlir::cir::CatchAllAttr>(catchAttrList[0]);
1265+
}
1266+
12461267
void TryOp::getSuccessorRegions(mlir::RegionBranchPoint point,
12471268
SmallVectorImpl<RegionSuccessor> &regions) {
12481269
// If any index all the underlying regions branch back to the parent

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ class CIRTryOpFlattening : public mlir::OpRewritePattern<mlir::cir::TryOp> {
418418
// Do not update `nextDispatcher`, no more business in try/catch
419419
} else if (auto catchUnwind =
420420
dyn_cast<mlir::cir::CatchUnwindAttr>(catchAttr)) {
421-
// assert(dispatcher->empty() && "expect empty dispatcher");
422-
// assert(!dispatcher->args_empty() && "expected block argument");
423421
assert(dispatcher->getArguments().size() == 2 &&
424422
"expected two block argument");
425423
buildUnwindCase(rewriter, catchRegion, dispatcher);
@@ -440,22 +438,18 @@ class CIRTryOpFlattening : public mlir::OpRewritePattern<mlir::cir::TryOp> {
440438
rewriter.setInsertionPointToEnd(beforeCatch);
441439
rewriter.replaceOpWithNewOp<mlir::cir::BrOp>(tryBodyYield, afterTry);
442440

443-
// Retrieve catch list and some properties.
444-
mlir::ArrayAttr catchAttrList = tryOp.getCatchTypesAttr();
445-
bool tryOnlyHasCatchAll = catchAttrList.size() == 1 &&
446-
isa<mlir::cir::CatchAllAttr>(catchAttrList[0]);
447-
448441
// Start the landing pad by getting the inflight exception information.
449442
mlir::Block *nextDispatcher =
450443
buildLandingPads(tryOp, rewriter, beforeCatch, afterTry, callsToRewrite,
451-
landingPads, tryOnlyHasCatchAll);
444+
landingPads, tryOp.isCatchAllOnly());
452445

453446
// Fill in dispatcher to all catch clauses.
454447
rewriter.setInsertionPointToEnd(nextDispatcher);
455448
llvm::MutableArrayRef<mlir::Region> catchRegions = tryOp.getCatchRegions();
456449
unsigned catchIdx = 0;
457450

458451
// Build control-flow for all catch clauses.
452+
mlir::ArrayAttr catchAttrList = tryOp.getCatchTypesAttr();
459453
for (mlir::Attribute catchAttr : catchAttrList) {
460454
mlir::Attribute nextCatchAttr;
461455
if (catchIdx + 1 < catchAttrList.size())

0 commit comments

Comments
 (0)