Skip to content

Commit

Permalink
[CIR][NFC] Add helpers for cir.try and do some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardosolopes committed Sep 27, 2024
1 parent 37397b3 commit fe5310b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
9 changes: 9 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3570,6 +3570,15 @@ def TryOp : CIR_Op<"try",
attr-dict
}];

let extraClassDeclaration = [{
private:
mlir::Region *getCatchLastRegion();
public:
mlir::Block *getCatchAllEntryBlock();
mlir::Block *getCatchUnwindEntryBlock();
bool isCatchAllOnly();
}];

// Everything already covered elsewhere.
let hasVerifier = 0;
let builders = [
Expand Down
13 changes: 2 additions & 11 deletions clang/lib/CIR/CodeGen/CIRGenException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,6 @@ void CIRGenFunction::buildAnyExprToExn(const Expr *e, Address addr) {
DeactivateCleanupBlock(cleanup, op);
}

static mlir::Block *getResumeBlockFromCatch(mlir::cir::TryOp &tryOp,
mlir::cir::GlobalOp globalParent) {
assert(tryOp && "cir.try expected");
unsigned numCatchRegions = tryOp.getCatchRegions().size();
assert(numCatchRegions && "expected at least one region");
auto &fallbackRegion = tryOp.getCatchRegions()[numCatchRegions - 1];
return &fallbackRegion.getBlocks().back();
return nullptr;
}

mlir::Block *CIRGenFunction::getEHResumeBlock(bool isCleanup,
mlir::cir::TryOp tryOp) {

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

Expand Down
21 changes: 21 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,27 @@ void TryOp::build(
catchBuilder(builder, result.location, result);
}

mlir::Region *TryOp::getCatchLastRegion() {
unsigned numCatchRegions = getCatchRegions().size();
assert(numCatchRegions && "expected at least one region");
auto &lastRegion = getCatchRegions()[numCatchRegions - 1];
return &lastRegion;
}

mlir::Block *TryOp::getCatchUnwindEntryBlock() {
return &getCatchLastRegion()->getBlocks().front();
}

mlir::Block *TryOp::getCatchAllEntryBlock() {
return &getCatchLastRegion()->getBlocks().front();
}

bool TryOp::isCatchAllOnly() {
mlir::ArrayAttr catchAttrList = getCatchTypesAttr();
return catchAttrList.size() == 1 &&
isa<mlir::cir::CatchAllAttr>(catchAttrList[0]);
}

void TryOp::getSuccessorRegions(mlir::RegionBranchPoint point,
SmallVectorImpl<RegionSuccessor> &regions) {
// If any index all the underlying regions branch back to the parent
Expand Down
10 changes: 2 additions & 8 deletions clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ class CIRTryOpFlattening : public mlir::OpRewritePattern<mlir::cir::TryOp> {
// Do not update `nextDispatcher`, no more business in try/catch
} else if (auto catchUnwind =
dyn_cast<mlir::cir::CatchUnwindAttr>(catchAttr)) {
// assert(dispatcher->empty() && "expect empty dispatcher");
// assert(!dispatcher->args_empty() && "expected block argument");
assert(dispatcher->getArguments().size() == 2 &&
"expected two block argument");
buildUnwindCase(rewriter, catchRegion, dispatcher);
Expand All @@ -440,22 +438,18 @@ class CIRTryOpFlattening : public mlir::OpRewritePattern<mlir::cir::TryOp> {
rewriter.setInsertionPointToEnd(beforeCatch);
rewriter.replaceOpWithNewOp<mlir::cir::BrOp>(tryBodyYield, afterTry);

// Retrieve catch list and some properties.
mlir::ArrayAttr catchAttrList = tryOp.getCatchTypesAttr();
bool tryOnlyHasCatchAll = catchAttrList.size() == 1 &&
isa<mlir::cir::CatchAllAttr>(catchAttrList[0]);

// Start the landing pad by getting the inflight exception information.
mlir::Block *nextDispatcher =
buildLandingPads(tryOp, rewriter, beforeCatch, afterTry, callsToRewrite,
landingPads, tryOnlyHasCatchAll);
landingPads, tryOp.isCatchAllOnly());

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

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

0 comments on commit fe5310b

Please sign in to comment.