-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[CIR] Upstream Exception CXXTryStmt #162528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,13 @@ | |
|
|
||
| namespace clang::CIRGen { | ||
|
|
||
| /// The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the | ||
| /// type of a catch handler, so we use this wrapper. | ||
| struct CatchTypeInfo { | ||
| mlir::TypedAttr rtti; | ||
| unsigned flags; | ||
| }; | ||
|
|
||
| /// A protected scope for zero-cost EH handling. | ||
| class EHScope { | ||
| class CommonBitFields { | ||
|
|
@@ -29,6 +36,12 @@ class EHScope { | |
| enum { NumCommonBits = 3 }; | ||
|
|
||
| protected: | ||
| class CatchBitFields { | ||
| friend class EHCatchScope; | ||
| unsigned : NumCommonBits; | ||
| unsigned numHandlers : 32 - NumCommonBits; | ||
| }; | ||
|
|
||
| class CleanupBitFields { | ||
| friend class EHCleanupScope; | ||
| unsigned : NumCommonBits; | ||
|
|
@@ -58,6 +71,7 @@ class EHScope { | |
|
|
||
| union { | ||
| CommonBitFields commonBits; | ||
| CatchBitFields catchBits; | ||
| CleanupBitFields cleanupBits; | ||
| }; | ||
|
|
||
|
|
@@ -67,6 +81,72 @@ class EHScope { | |
| EHScope(Kind kind) { commonBits.kind = kind; } | ||
|
|
||
| Kind getKind() const { return static_cast<Kind>(commonBits.kind); } | ||
|
|
||
| bool hasEHBranches() const { | ||
| // Traditional LLVM codegen also checks for `!block->use_empty()`, but | ||
| // in CIRGen the block content is not important, just used as a way to | ||
| // signal `hasEHBranches`. | ||
| assert(!cir::MissingFeatures::ehstackBranches()); | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| /// A scope which attempts to handle some, possibly all, types of | ||
| /// exceptions. | ||
| /// | ||
| /// Objective C \@finally blocks are represented using a cleanup scope | ||
| /// after the catch scope. | ||
|
|
||
| class EHCatchScope : public EHScope { | ||
| // In effect, we have a flexible array member | ||
| // Handler Handlers[0]; | ||
| // But that's only standard in C99, not C++, so we have to do | ||
| // annoying pointer arithmetic instead. | ||
|
|
||
| public: | ||
| struct Handler { | ||
| /// A type info value, or null (C++ null, not an LLVM null pointer) | ||
| /// for a catch-all. | ||
AmrDeveloper marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CatchTypeInfo type; | ||
|
|
||
| /// The catch handler for this type. | ||
| mlir::Block *block; | ||
|
||
| }; | ||
|
|
||
| private: | ||
| friend class EHScopeStack; | ||
|
|
||
| Handler *getHandlers() { return reinterpret_cast<Handler *>(this + 1); } | ||
|
|
||
| public: | ||
| static size_t getSizeForNumHandlers(unsigned n) { | ||
| return sizeof(EHCatchScope) + n * sizeof(Handler); | ||
| } | ||
|
|
||
| EHCatchScope(unsigned numHandlers) : EHScope(Catch) { | ||
| catchBits.numHandlers = numHandlers; | ||
| assert(catchBits.numHandlers == numHandlers && "NumHandlers overflow?"); | ||
| } | ||
|
|
||
| unsigned getNumHandlers() const { return catchBits.numHandlers; } | ||
|
|
||
| void setHandler(unsigned i, CatchTypeInfo type, mlir::Block *block) { | ||
| assert(i < getNumHandlers()); | ||
| getHandlers()[i].type = type; | ||
| getHandlers()[i].block = block; | ||
| } | ||
|
|
||
| // Clear all handler blocks. | ||
| // FIXME: it's better to always call clearHandlerBlocks in DTOR and have a | ||
| // 'takeHandler' or some such function which removes ownership from the | ||
| // EHCatchScope object if the handlers should live longer than EHCatchScope. | ||
| void clearHandlerBlocks() { | ||
| // The blocks are owned by TryOp, nothing to delete. | ||
| } | ||
|
|
||
| static bool classof(const EHScope *scope) { | ||
| return scope->getKind() == Catch; | ||
| } | ||
| }; | ||
|
|
||
| /// A cleanup scope which generates the cleanup blocks lazily. | ||
|
|
@@ -147,5 +227,13 @@ EHScopeStack::find(stable_iterator savePoint) const { | |
| return iterator(endOfBuffer - savePoint.size); | ||
| } | ||
|
|
||
| inline void EHScopeStack::popCatch() { | ||
| assert(!empty() && "popping exception stack when not empty"); | ||
|
|
||
| EHCatchScope &scope = llvm::cast<EHCatchScope>(*begin()); | ||
| assert(!cir::MissingFeatures::innermostEHScope()); | ||
| deallocate(EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers())); | ||
| } | ||
|
|
||
| } // namespace clang::CIRGen | ||
| #endif // CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -69,6 +69,151 @@ mlir::LogicalResult CIRGenFunction::emitCXXTryStmt(const CXXTryStmt &s) { | |||||||||||||
| if (s.getTryBlock()->body_empty()) | ||||||||||||||
| return mlir::LogicalResult::success(); | ||||||||||||||
|
|
||||||||||||||
| cgm.errorNYI("exitCXXTryStmt: CXXTryStmt with non-empty body"); | ||||||||||||||
| return mlir::LogicalResult::success(); | ||||||||||||||
| mlir::Location loc = getLoc(s.getSourceRange()); | ||||||||||||||
| // Create a scope to hold try local storage for catch params. | ||||||||||||||
|
|
||||||||||||||
| mlir::OpBuilder::InsertPoint scopeIP; | ||||||||||||||
| cir::ScopeOp::create( | ||||||||||||||
| builder, loc, | ||||||||||||||
| /*scopeBuilder=*/[&](mlir::OpBuilder &b, mlir::Location loc) { | ||||||||||||||
| scopeIP = builder.saveInsertionPoint(); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| mlir::OpBuilder::InsertionGuard guard(builder); | ||||||||||||||
| builder.restoreInsertionPoint(scopeIP); | ||||||||||||||
| mlir::LogicalResult result = emitCXXTryStmtUnderScope(s); | ||||||||||||||
| cir::YieldOp::create(builder, loc); | ||||||||||||||
| return result; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| mlir::LogicalResult | ||||||||||||||
| CIRGenFunction::emitCXXTryStmtUnderScope(const CXXTryStmt &s) { | ||||||||||||||
| const llvm::Triple &t = getTarget().getTriple(); | ||||||||||||||
| // If we encounter a try statement on in an OpenMP target region offloaded to | ||||||||||||||
| // a GPU, we treat it as a basic block. | ||||||||||||||
| const bool isTargetDevice = | ||||||||||||||
| (cgm.getLangOpts().OpenMPIsTargetDevice && (t.isNVPTX() || t.isAMDGCN())); | ||||||||||||||
| if (isTargetDevice) { | ||||||||||||||
| cgm.errorNYI( | ||||||||||||||
| "emitCXXTryStmtUnderScope: OpenMP target region offloaded to GPU"); | ||||||||||||||
| return mlir::success(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| unsigned numHandlers = s.getNumHandlers(); | ||||||||||||||
| mlir::Location tryLoc = getLoc(s.getBeginLoc()); | ||||||||||||||
| mlir::OpBuilder::InsertPoint beginInsertTryBody; | ||||||||||||||
|
|
||||||||||||||
| bool hasCatchAll = false; | ||||||||||||||
| for (unsigned i = 0; i != numHandlers; ++i) { | ||||||||||||||
andykaylor marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| hasCatchAll |= s.getHandler(i)->getExceptionDecl() == nullptr; | ||||||||||||||
| if (hasCatchAll) | ||||||||||||||
| break; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Create the scope to represent only the C/C++ `try {}` part. However, | ||||||||||||||
| // don't populate right away. Reserve some space to store the exception | ||||||||||||||
| // info but don't emit the bulk right away, for now only make sure the | ||||||||||||||
|
||||||||||||||
| // don't populate right away. Reserve some space to store the exception | |
| // info but don't emit the bulk right away, for now only make sure the | |
| // don't populate right away. Create regions for the catch handlers, | |
| // but don't emit the handler bodies yet. For now, only make sure the |
I think this is what the comment meant? It's not clear to me in what way this is making sure the scope returns the exception info.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| for (unsigned i = 0; i != numRegionsToCreate; ++i) | |
| builder.createBlock(result.addRegion()); | |
| for (unsigned i = 0; i != numRegionsToCreate; ++i) { | |
| mlir::Region r = result.addRegion(); | |
| builder.createBlock(r); | |
| } |
I think this improves readability.
It seems a little odd that we're creating a block before we emit the handler. Is that because the EHCatchScope::Handler structure stores an mlir::Block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think yes, also to take care of the actual number of regions (Including unwind), because later when we create the handlers, we don't need to check and create an extra region then
AmrDeveloper marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (!catchScope.hasEHBranches()) { | |
| if (!catchScope.mayThrow()) { |
I'm concerned about the lack of alignment between the names we're keeping from the OGCG implementation of exception handling and the actual structure of the CIR implementation. For example, hasEHBranches here is referring to the fact that when the generated LLVM IR executes an invoke instruction, there is a control flow path to the basic block containing a landingpad instruction which literally branches to the basic block for a catch handler. We won't have anything like that in CIR.
I believe what this condition means is "catchScope contains instructions that might throw an exception" so I suggest we use a more appropriate name. (I also suggest that we shouldn't base it on CachedEHDispatchBlock as the incubator does.)
Uh oh!
There was an error while loading. Please reload this page.