Skip to content

Commit 0243dba

Browse files
committed
Address code review comments
1 parent 052ef39 commit 0243dba

File tree

7 files changed

+46
-112
lines changed

7 files changed

+46
-112
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -389,25 +389,6 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
389389
return createCallOp(loc, callee, cir::VoidType(), operands, attrs);
390390
}
391391

392-
cir::CallOp createTryCallOp(
393-
mlir::Location loc, mlir::SymbolRefAttr callee = mlir::SymbolRefAttr(),
394-
mlir::Type returnType = cir::VoidType(),
395-
mlir::ValueRange operands = mlir::ValueRange(),
396-
[[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
397-
assert(!cir::MissingFeatures::opCallCallConv());
398-
assert(!cir::MissingFeatures::opCallSideEffect());
399-
return createCallOp(loc, callee, returnType, operands);
400-
}
401-
402-
cir::CallOp createTryCallOp(
403-
mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands,
404-
[[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
405-
assert(!cir::MissingFeatures::opCallCallConv());
406-
assert(!cir::MissingFeatures::opCallSideEffect());
407-
return createTryCallOp(loc, mlir::SymbolRefAttr::get(callee),
408-
callee.getFunctionType().getReturnType(), operands);
409-
}
410-
411392
//===--------------------------------------------------------------------===//
412393
// Cast/Conversion Operators
413394
//===--------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,11 @@ emitCallLikeOp(CIRGenFunction &cgf, mlir::Location callLoc,
472472
assert(!cir::MissingFeatures::opCallSurroundingTry());
473473

474474
if (isInvoke) {
475-
// This call can throw, few options:
476-
// - If this call does not have an associated cir.try, use the
477-
// one provided by InvokeDest,
478-
// - User written try/catch clauses require calls to handle
479-
// exceptions under cir.try.
475+
// This call may throw and requires catch and/or cleanup handling.
476+
// If this call does not appear within the `try` region of an existing
477+
// TryOp, we must create a synthetic TryOp to contain the call. This
478+
// happens when a call that may throw appears within a cleanup
479+
// scope.
480480

481481
// In OG, we build the landing pad for this scope. In CIR, we emit a
482482
// synthetic cir.try because this didn't come from code generating from a
@@ -501,10 +501,9 @@ emitCallLikeOp(CIRGenFunction &cgf, mlir::Location callLoc,
501501
}
502502

503503
callOpWithExceptions =
504-
builder.createTryCallOp(callLoc, directFuncOp, cirCallArgs);
505-
506-
(void)cgf.getInvokeDest(tryOp);
504+
builder.createCallOp(callLoc, directFuncOp, cirCallArgs);
507505

506+
cgf.populateCatchHandlersIfRequired(tryOp);
508507
return callOpWithExceptions;
509508
}
510509

@@ -668,7 +667,7 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo,
668667
// TODO(cir): check for MSVCXXPersonality
669668
// TODO(cir): Create NoThrowAttr
670669
bool cannotThrow = attrs.getNamed("nothrow").has_value();
671-
bool isInvoke = !cannotThrow && isInvokeDest();
670+
bool isInvoke = !cannotThrow && isCatchOrCleanupRequired();
672671

673672
mlir::Location callLoc = loc;
674673
cir::CIRCallOpInterface theCall =

clang/lib/CIR/CodeGen/CIRGenCleanup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void EHScopeStack::popCleanup() {
188188
}
189189
}
190190

191-
bool EHScopeStack::requiresLandingPad() const {
191+
bool EHScopeStack::requiresCatchOrCleanup() const {
192192
for (stable_iterator si = getInnermostEHScope(); si != stable_end();) {
193193
// TODO(cir): Skip lifetime markers.
194194
assert(!cir::MissingFeatures::emitLifetimeMarkers());

clang/lib/CIR/CodeGen/CIRGenCleanup.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class EHScope {
3838
};
3939
enum { NumCommonBits = 3 };
4040

41-
bool isScopeMayThrow;
41+
bool scopeMayThrow;
4242

4343
protected:
4444
class CatchBitFields {
@@ -94,10 +94,10 @@ class EHScope {
9494
// Traditional LLVM codegen also checks for `!block->use_empty()`, but
9595
// in CIRGen the block content is not important, just used as a way to
9696
// signal `hasEHBranches`.
97-
return isScopeMayThrow;
97+
return scopeMayThrow;
9898
}
9999

100-
void setMayThrow(bool mayThrow) { isScopeMayThrow = mayThrow; }
100+
void setMayThrow(bool mayThrow) { scopeMayThrow = mayThrow; }
101101

102102
EHScopeStack::stable_iterator getEnclosingEHScope() const {
103103
return enclosingEHScope;

clang/lib/CIR/CodeGen/CIRGenException.cpp

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -355,33 +355,6 @@ void CIRGenFunction::enterCXXTryStmt(const CXXTryStmt &s, cir::TryOp tryOp,
355355
}
356356
}
357357

358-
/// Emit the structure of the dispatch block for the given catch scope.
359-
/// It is an invariant that the dispatch block already exists.
360-
static void emitCatchDispatchBlock(CIRGenFunction &cgf,
361-
EHCatchScope &catchScope, cir::TryOp tryOp) {
362-
if (EHPersonality::get(cgf).isWasmPersonality()) {
363-
cgf.cgm.errorNYI("emitCatchDispatchBlock: WASM personality");
364-
return;
365-
}
366-
367-
if (EHPersonality::get(cgf).usesFuncletPads()) {
368-
cgf.cgm.errorNYI("emitCatchDispatchBlock: usesFuncletPads");
369-
return;
370-
}
371-
372-
assert(catchScope.mayThrow() &&
373-
"Expected catchScope that may throw exception");
374-
375-
// If there's only a single catch-all, getEHDispatchBlock returned
376-
// that catch-all as the dispatch block.
377-
if (catchScope.getNumHandlers() == 1 &&
378-
catchScope.getHandler(0).isCatchAll()) {
379-
return;
380-
}
381-
382-
cgf.cgm.errorNYI("emitCatchDispatchBlock: non-catch all handler");
383-
}
384-
385358
void CIRGenFunction::exitCXXTryStmt(const CXXTryStmt &s, bool isFnTryBlock) {
386359
unsigned numHandlers = s.getNumHandlers();
387360
EHCatchScope &catchScope = cast<EHCatchScope>(*ehStack.begin());
@@ -410,9 +383,6 @@ void CIRGenFunction::exitCXXTryStmt(const CXXTryStmt &s, bool isFnTryBlock) {
410383
return;
411384
}
412385

413-
// Emit the structure of the EH dispatch for this catch.
414-
emitCatchDispatchBlock(*this, catchScope, tryOp);
415-
416386
// Copy the handler blocks off before we pop the EH stack. Emitting
417387
// the handlers might scribble on this memory.
418388
SmallVector<EHCatchScope::Handler, 8> handlers(
@@ -490,8 +460,8 @@ void CIRGenFunction::exitCXXTryStmt(const CXXTryStmt &s, bool isFnTryBlock) {
490460
assert(!cir::MissingFeatures::incrementProfileCounter());
491461
}
492462

493-
mlir::Operation *CIRGenFunction::emitLandingPad(cir::TryOp tryOp) {
494-
assert(ehStack.requiresLandingPad());
463+
void CIRGenFunction::populateCatchHandlers(cir::TryOp tryOp) {
464+
assert(ehStack.requiresCatchOrCleanup());
495465
assert(!cgm.getLangOpts().IgnoreExceptions &&
496466
"LandingPad should not be emitted when -fignore-exceptions are in "
497467
"effect.");
@@ -500,7 +470,7 @@ mlir::Operation *CIRGenFunction::emitLandingPad(cir::TryOp tryOp) {
500470
switch (innermostEHScope.getKind()) {
501471
case EHScope::Terminate:
502472
cgm.errorNYI("emitLandingPad: terminate");
503-
return {};
473+
return;
504474

505475
case EHScope::Catch:
506476
case EHScope::Cleanup:
@@ -523,17 +493,17 @@ mlir::Operation *CIRGenFunction::emitLandingPad(cir::TryOp tryOp) {
523493
switch (i->getKind()) {
524494
case EHScope::Cleanup: {
525495
cgm.errorNYI("emitLandingPad: Cleanup");
526-
return {};
496+
return;
527497
}
528498

529499
case EHScope::Filter: {
530500
cgm.errorNYI("emitLandingPad: Filter");
531-
return {};
501+
return;
532502
}
533503

534504
case EHScope::Terminate: {
535505
cgm.errorNYI("emitLandingPad: Terminate");
536-
return {};
506+
return;
537507
}
538508

539509
case EHScope::Catch:
@@ -551,22 +521,22 @@ mlir::Operation *CIRGenFunction::emitLandingPad(cir::TryOp tryOp) {
551521
if (handler.isCatchAll()) {
552522
assert(!hasCatchAll);
553523
hasCatchAll = true;
554-
goto done;
524+
break;
555525
}
556526

557527
cgm.errorNYI("emitLandingPad: non catch-all");
558-
return {};
528+
return;
559529
}
560530

561-
goto done;
531+
if (hasCatchAll)
532+
break;
562533
}
563534

564-
done:
565535
if (hasCatchAll) {
566536
handlerAttrs.push_back(cir::CatchAllAttr::get(&getMLIRContext()));
567537
} else {
568538
cgm.errorNYI("emitLandingPad: non catch-all");
569-
return {};
539+
return;
570540
}
571541

572542
// Add final array of clauses into TryOp.
@@ -576,23 +546,19 @@ mlir::Operation *CIRGenFunction::emitLandingPad(cir::TryOp tryOp) {
576546

577547
// In traditional LLVM codegen. this tells the backend how to generate the
578548
// landing pad by generating a branch to the dispatch block. In CIR,
579-
// getEHDispatchBlock is used to populate blocks for later filing during
549+
// this is used to populate blocks for later filing during
580550
// cleanup handling.
581-
(void)getEHDispatchBlock(ehStack.getInnermostEHScope(), tryOp);
582-
583-
return tryOp;
551+
populateEHCatchRegions(ehStack.getInnermostEHScope(), tryOp);
584552
}
585553

586554
// Differently from LLVM traditional codegen, there are no dispatch blocks
587555
// to look at given cir.try_call does not jump to blocks like invoke does.
588-
// However, we keep this around since other parts of CIRGen use
589-
// getCachedEHDispatchBlock to infer state.
590-
mlir::Block *
591-
CIRGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator scope,
592-
cir::TryOp tryOp) {
556+
// However.
557+
void CIRGenFunction::populateEHCatchRegions(EHScopeStack::stable_iterator scope,
558+
cir::TryOp tryOp) {
593559
if (EHPersonality::get(*this).usesFuncletPads()) {
594560
cgm.errorNYI("getEHDispatchBlock: usesFuncletPads");
595-
return {};
561+
return;
596562
}
597563

598564
// Otherwise, we should look at the actual scope.
@@ -606,7 +572,7 @@ CIRGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator scope,
606572
// - Update the map to enqueue new dispatchBlock to also get a cleanup. See
607573
// code at the end of the function.
608574
cgm.errorNYI("getEHDispatchBlock: mayThrow & tryOp");
609-
return {};
575+
return;
610576
}
611577

612578
if (!mayThrow) {
@@ -621,34 +587,33 @@ CIRGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator scope,
621587
break;
622588
}
623589
cgm.errorNYI("getEHDispatchBlock: mayThrow non-catch all");
624-
return {};
590+
return;
625591
}
626592
case EHScope::Cleanup: {
627593
cgm.errorNYI("getEHDispatchBlock: mayThrow & cleanup");
628-
return {};
594+
return;
629595
}
630596
case EHScope::Filter: {
631597
cgm.errorNYI("getEHDispatchBlock: mayThrow & Filter");
632-
return {};
598+
return;
633599
}
634600
case EHScope::Terminate: {
635601
cgm.errorNYI("getEHDispatchBlock: mayThrow & Terminate");
636-
return {};
602+
return;
637603
}
638604
}
639605
}
640606

641607
if (originalBlock) {
642608
cgm.errorNYI("getEHDispatchBlock: originalBlock");
643-
return {};
609+
return;
644610
}
645611

646612
ehScope.setMayThrow(mayThrow);
647-
return {};
648613
}
649614

650-
bool CIRGenFunction::isInvokeDest() {
651-
if (!ehStack.requiresLandingPad())
615+
bool CIRGenFunction::isCatchOrCleanupRequired() {
616+
if (!ehStack.requiresCatchOrCleanup())
652617
return false;
653618

654619
// If exceptions are disabled/ignored and SEH is not in use, then there is no
@@ -668,21 +633,17 @@ bool CIRGenFunction::isInvokeDest() {
668633
return true;
669634
}
670635

671-
mlir::Operation *CIRGenFunction::getInvokeDestImpl(cir::TryOp tryOp) {
672-
assert(ehStack.requiresLandingPad());
636+
void CIRGenFunction::populateCatchHandlersIfRequired(cir::TryOp tryOp) {
637+
assert(ehStack.requiresCatchOrCleanup());
673638
assert(!ehStack.empty());
674639

675640
// TODO(cir): add personality function
676641

677642
// CIR does not cache landing pads.
678643
const EHPersonality &personality = EHPersonality::get(*this);
679-
680-
mlir::Operation *lp = nullptr;
681644
if (personality.usesFuncletPads()) {
682645
cgm.errorNYI("getInvokeDestImpl: usesFuncletPads");
683646
} else {
684-
lp = emitLandingPad(tryOp);
647+
populateCatchHandlers(tryOp);
685648
}
686-
687-
return lp;
688649
}

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -916,22 +916,15 @@ class CIRGenFunction : public CIRGenTypeCache {
916916
return false;
917917
}
918918

919-
mlir::Block *getEHDispatchBlock(EHScopeStack::stable_iterator scope,
920-
cir::TryOp tryOp);
919+
void populateEHCatchRegions(EHScopeStack::stable_iterator scope,
920+
cir::TryOp tryOp);
921921

922922
/// The cleanup depth enclosing all the cleanups associated with the
923923
/// parameters.
924924
EHScopeStack::stable_iterator prologueCleanupDepth;
925925

926-
mlir::Operation *getInvokeDestImpl(cir::TryOp tryOp);
927-
mlir::Operation *getInvokeDest(cir::TryOp tryOp) {
928-
if (!ehStack.requiresLandingPad())
929-
return nullptr;
930-
// Return the respective cir.try, this can be used to compute
931-
// any other relevant information.
932-
return getInvokeDestImpl(tryOp);
933-
}
934-
bool isInvokeDest();
926+
bool isCatchOrCleanupRequired();
927+
void populateCatchHandlersIfRequired(cir::TryOp tryOp);
935928

936929
/// Takes the old cleanup stack size and emits the cleanup blocks
937930
/// that have been added.
@@ -1626,7 +1619,7 @@ class CIRGenFunction : public CIRGenTypeCache {
16261619
void emitLambdaDelegatingInvokeBody(const CXXMethodDecl *md);
16271620
void emitLambdaStaticInvokeBody(const CXXMethodDecl *md);
16281621

1629-
mlir::Operation *emitLandingPad(cir::TryOp tryOp);
1622+
void populateCatchHandlers(cir::TryOp tryOp);
16301623

16311624
mlir::LogicalResult emitIfStmt(const clang::IfStmt &s);
16321625

clang/lib/CIR/CodeGen/EHScopeStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class EHScopeStack {
217217
/// Determines whether the exception-scopes stack is empty.
218218
bool empty() const { return startOfData == endOfBuffer; }
219219

220-
bool requiresLandingPad() const;
220+
bool requiresCatchOrCleanup() const;
221221

222222
/// Determines whether there are any normal cleanups on the stack.
223223
bool hasNormalCleanups() const {

0 commit comments

Comments
 (0)