Skip to content

Commit 90a4b02

Browse files
committed
Address code review comments
1 parent 7e06895 commit 90a4b02

File tree

6 files changed

+234
-224
lines changed

6 files changed

+234
-224
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ struct MissingFeatures {
335335
static bool vtableRelativeLayout() { return false; }
336336
static bool weakRefReference() { return false; }
337337
static bool writebacks() { return false; }
338+
static bool msvcCXXPersonality() { return false; }
339+
static bool functionUsesSEHTry() { return false; }
340+
static bool nothrowAttr() { return false; }
338341

339342
// Missing types
340343
static bool dataMemberType() { return false; }
@@ -359,6 +362,7 @@ struct MissingFeatures {
359362
static bool tryOp() { return false; }
360363
static bool vecTernaryOp() { return false; }
361364
static bool zextOp() { return false; }
365+
static bool catchParamOp() { return false; }
362366

363367
// Future CIR attributes
364368
static bool optInfoAttr() { return false; }

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo,
661661
indirectFuncVal = calleePtr->getResult(0);
662662
}
663663

664-
// TODO(cir): currentFunctionUsesSEHTry
665-
// TODO(cir): check for MSVCXXPersonality
666-
// TODO(cir): Create NoThrowAttr
664+
assert(!cir::MissingFeatures::msvcCXXPersonality());
665+
assert(!cir::MissingFeatures::functionUsesSEHTry());
666+
assert(!cir::MissingFeatures::nothrowAttr());
667+
667668
bool cannotThrow = attrs.getNamed("nothrow").has_value();
668669
bool isInvoke = !cannotThrow && isCatchOrCleanupRequired();
669670

clang/lib/CIR/CodeGen/CIRGenCleanup.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,13 @@ void EHScopeStack::popCleanup() {
190190

191191
bool EHScopeStack::requiresCatchOrCleanup() const {
192192
for (stable_iterator si = getInnermostEHScope(); si != stable_end();) {
193-
// TODO(cir): Skip lifetime markers.
194-
assert(!cir::MissingFeatures::emitLifetimeMarkers());
193+
if (auto *cleanup = dyn_cast<EHCleanupScope>(&*find(si))) {
194+
if (cleanup->isLifetimeMarker()) {
195+
// Skip lifetime markers and continue from the enclosing EH scope
196+
assert(!cir::MissingFeatures::emitLifetimeMarkers());
197+
continue;
198+
}
199+
}
195200
return true;
196201
}
197202
return false;

clang/lib/CIR/CodeGen/CIRGenCleanup.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "CIRGenModule.h"
1919
#include "EHScopeStack.h"
2020
#include "mlir/IR/Value.h"
21+
#include "clang/AST/StmtCXX.h"
2122

2223
namespace clang::CIRGen {
2324

@@ -124,6 +125,9 @@ class EHCatchScope : public EHScope {
124125
/// The catch handler for this type.
125126
mlir::Region *region;
126127

128+
/// The catch handler stmt.
129+
const CXXCatchStmt *stmt;
130+
127131
bool isCatchAll() const { return type.rtti == nullptr; }
128132
};
129133

@@ -150,10 +154,13 @@ class EHCatchScope : public EHScope {
150154

151155
unsigned getNumHandlers() const { return catchBits.numHandlers; }
152156

153-
void setHandler(unsigned i, CatchTypeInfo type, mlir::Region *region) {
157+
void setHandler(unsigned i, CatchTypeInfo type, mlir::Region *region,
158+
const CXXCatchStmt *stmt) {
154159
assert(i < getNumHandlers());
155-
getHandlers()[i].type = type;
156-
getHandlers()[i].region = region;
160+
Handler *handler = &getHandlers()[i];
161+
handler->type = type;
162+
handler->region = region;
163+
handler->stmt = stmt;
157164
}
158165

159166
const Handler &getHandler(unsigned i) const {
@@ -234,6 +241,8 @@ class alignas(EHScopeStack::ScopeStackAlignment) EHCleanupScope
234241
bool isActive() const { return cleanupBits.isActive; }
235242
void setActive(bool isActive) { cleanupBits.isActive = isActive; }
236243

244+
bool isLifetimeMarker() const { return cleanupBits.isLifetimeMarker; }
245+
237246
unsigned getFixupDepth() const { return fixupDepth; }
238247
EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
239248
return enclosingNormal;

clang/lib/CIR/CodeGen/CIRGenException.cpp

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CIRGenFunction.h"
1515

1616
#include "clang/AST/StmtVisitor.h"
17+
#include "clang/CIR/MissingFeatures.h"
1718
#include "llvm/Support/SaveAndRestore.h"
1819

1920
using namespace clang;
@@ -344,7 +345,8 @@ void CIRGenFunction::enterCXXTryStmt(const CXXTryStmt &s, cir::TryOp tryOp,
344345

345346
// No exception decl indicates '...', a catch-all.
346347
mlir::Region *handler = &tryOp.getHandlerRegions()[i];
347-
catchScope->setHandler(i, cgm.getCXXABI().getCatchAllTypeInfo(), handler);
348+
catchScope->setHandler(i, cgm.getCXXABI().getCatchAllTypeInfo(), handler,
349+
s.getHandler(i));
348350

349351
// Under async exceptions, catch(...) needs to catch HW exception too
350352
// Mark scope with SehTryBegin as a SEH __try scope
@@ -385,8 +387,8 @@ void CIRGenFunction::exitCXXTryStmt(const CXXTryStmt &s, bool isFnTryBlock) {
385387

386388
// Copy the handler blocks off before we pop the EH stack. Emitting
387389
// the handlers might scribble on this memory.
388-
SmallVector<EHCatchScope::Handler, 8> handlers(
389-
catchScope.begin(), catchScope.begin() + numHandlers);
390+
SmallVector<EHCatchScope::Handler> handlers(catchScope.begin(),
391+
catchScope.begin() + numHandlers);
390392

391393
ehStack.popCatch();
392394

@@ -404,21 +406,20 @@ void CIRGenFunction::exitCXXTryStmt(const CXXTryStmt &s, bool isFnTryBlock) {
404406
}
405407

406408
bool hasCatchAll = false;
407-
for (unsigned i = numHandlers; i != 0; --i) {
408-
hasCatchAll |= handlers[i - 1].isCatchAll();
409-
mlir::Region *catchRegion = handlers[i - 1].region;
409+
for (auto &handler : llvm::reverse(handlers)) {
410+
hasCatchAll |= handler.isCatchAll();
411+
mlir::Region *catchRegion = handler.region;
412+
const CXXCatchStmt *catchStmt = handler.stmt;
410413

411414
mlir::OpBuilder::InsertionGuard guard(builder);
412415
builder.setInsertionPointToStart(&catchRegion->front());
413416

414-
const CXXCatchStmt *catchStmt = s.getHandler(i - 1);
415-
416417
// Enter a cleanup scope, including the catch variable and the
417418
// end-catch.
418419
RunCleanupsScope catchScope(*this);
419420

420421
// Initialize the catch variable and set up the cleanups.
421-
// TODO: emitBeginCatch
422+
assert(!cir::MissingFeatures::catchParamOp());
422423

423424
// Emit the PGO counter increment.
424425
assert(!cir::MissingFeatures::incrementProfileCounter());
@@ -428,7 +429,7 @@ void CIRGenFunction::exitCXXTryStmt(const CXXTryStmt &s, bool isFnTryBlock) {
428429
emitStmt(catchStmt->getHandlerBlock(), /*useCurrentScope=*/true);
429430
assert(emitResult.succeeded() && "failed to emit catch handler block");
430431

431-
// TODO(cir): This yeild should replaced by CatchParamOp once it upstreamed
432+
assert(!cir::MissingFeatures::catchParamOp());
432433
cir::YieldOp::create(builder, tryOp->getLoc());
433434

434435
// [except.handle]p11:
@@ -469,7 +470,7 @@ void CIRGenFunction::populateCatchHandlers(cir::TryOp tryOp) {
469470
EHScope &innermostEHScope = *ehStack.find(ehStack.getInnermostEHScope());
470471
switch (innermostEHScope.getKind()) {
471472
case EHScope::Terminate:
472-
cgm.errorNYI("emitLandingPad: terminate");
473+
cgm.errorNYI("populateCatchHandlers: terminate");
473474
return;
474475

475476
case EHScope::Catch:
@@ -491,29 +492,25 @@ void CIRGenFunction::populateCatchHandlers(cir::TryOp tryOp) {
491492
for (EHScopeStack::iterator i = ehStack.begin(), e = ehStack.end(); i != e;
492493
++i) {
493494
switch (i->getKind()) {
494-
case EHScope::Cleanup: {
495+
case EHScope::Cleanup:
495496
cgm.errorNYI("emitLandingPad: Cleanup");
496497
return;
497-
}
498498

499-
case EHScope::Filter: {
499+
case EHScope::Filter:
500500
cgm.errorNYI("emitLandingPad: Filter");
501501
return;
502-
}
503502

504-
case EHScope::Terminate: {
503+
case EHScope::Terminate:
505504
cgm.errorNYI("emitLandingPad: Terminate");
506505
return;
507-
}
508506

509507
case EHScope::Catch:
510508
break;
511-
}
509+
} // end switch
512510

513511
EHCatchScope &catchScope = cast<EHCatchScope>(*i);
514-
for (unsigned handlerIdx = 0, he = catchScope.getNumHandlers();
515-
handlerIdx != he; ++handlerIdx) {
516-
EHCatchScope::Handler handler = catchScope.getHandler(handlerIdx);
512+
for (const EHCatchScope::Handler &handler :
513+
llvm::make_range(catchScope.begin(), catchScope.end())) {
517514
assert(handler.type.flags == 0 &&
518515
"landingpads do not support catch handler flags");
519516

@@ -617,15 +614,14 @@ void CIRGenFunction::populateEHCatchRegions(EHScopeStack::stable_iterator scope,
617614
// `getInvokeDestImpl`, in CIR we need the condition to know if the EH scope may
618615
// throw exception or now.
619616
bool CIRGenFunction::isCatchOrCleanupRequired() {
620-
if (!ehStack.requiresCatchOrCleanup())
621-
return false;
622-
623617
// If exceptions are disabled/ignored and SEH is not in use, then there is no
624618
// invoke destination. SEH "works" even if exceptions are off. In practice,
625619
// this means that C++ destructors and other EH cleanups don't run, which is
626620
// consistent with MSVC's behavior, except in the presence of -EHa
627621
const LangOptions &lo = cgm.getLangOpts();
628622
if (!lo.Exceptions || lo.IgnoreExceptions) {
623+
if (!lo.Borland && !lo.MicrosoftExt)
624+
return false;
629625
cgm.errorNYI("isInvokeDest: no exceptions or ignore exception");
630626
return false;
631627
}
@@ -634,7 +630,7 @@ bool CIRGenFunction::isCatchOrCleanupRequired() {
634630
if (lo.CUDA && lo.CUDAIsDevice)
635631
return false;
636632

637-
return true;
633+
return ehStack.requiresCatchOrCleanup();
638634
}
639635

640636
// In classic codegen this function is equivalent to `getInvokeDestImpl`, in

0 commit comments

Comments
 (0)