[clang] [CodeGen] Implement Objective-C WebAssembly exception handling support (PR #215562)
Heejin Ahn via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 22 01:29:58 PDT 2026
Hendrik =?utf-8?q?Hübner?= <hhuebner at hhuebnerMacBookPro.local>,
Hendrik =?utf-8?q?Hübner?= <hhuebner at hhuebnerMacBookPro.local>,
Hendrik =?utf-8?q?Hübner?= <hhuebner at MacBookPro.lan>,
Hendrik =?utf-8?q?Hübner?= <hhuebner at MacBookPro.lan>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/215562 at github.com>
================
@@ -182,61 +195,122 @@ void CGObjCRuntime::EmitTryCatchStmt(CodeGenFunction &CGF,
Handler.TypeInfo = GetEHType(CatchDecl->getType());
}
+ // Create a new catch scope
EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
Catch->setHandler(I, { Handlers[I].TypeInfo, Handlers[I].Flags }, Handlers[I].Block);
}
- if (useFunclets)
+ if (IsMSVC) {
if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt()) {
- CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true);
- if (!CGF.CurSEHParent)
- CGF.CurSEHParent = cast<NamedDecl>(CGF.CurFuncDecl);
- // Outline the finally block.
- const Stmt *FinallyBlock = Finally->getFinallyBody();
- HelperCGF.startOutlinedSEHHelper(CGF, /*isFilter*/false, FinallyBlock);
-
- // Emit the original filter expression, convert to i32, and return.
- HelperCGF.EmitStmt(FinallyBlock);
+ CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true);
+ if (!CGF.CurSEHParent)
+ CGF.CurSEHParent = cast<NamedDecl>(CGF.CurFuncDecl);
+ // Outline the finally block.
+ const Stmt *FinallyBlock = Finally->getFinallyBody();
+ HelperCGF.startOutlinedSEHHelper(CGF, /*isFilter*/ false, FinallyBlock);
- HelperCGF.FinishFunction(FinallyBlock->getEndLoc());
+ // Emit the original filter expression, convert to i32, and return.
+ HelperCGF.EmitStmt(FinallyBlock);
- llvm::Function *FinallyFunc = HelperCGF.CurFn;
+ HelperCGF.FinishFunction(FinallyBlock->getEndLoc());
+ llvm::Function *FinallyFunc = HelperCGF.CurFn;
- // Push a cleanup for __finally blocks.
- CGF.pushSEHCleanup(NormalAndEHCleanup, FinallyFunc);
+ // Push a cleanup for __finally blocks.
+ CGF.pushSEHCleanup(NormalAndEHCleanup, FinallyFunc);
}
-
+ }
// Emit the try body.
CGF.EmitStmt(S.getTryBody());
+ // lpad or catch.dispatch (the dispatch block) has now been emitted
+ //
+ // Here an example:
+ // void may_throw();
+ // @try {
+ // may_throw();
+ // } @catch(id a) {
+ // } @catch(id b) {
+ // [...]
+ //
+ // With funclet-based exception handling, the dispatch block is created in
+ // getEHDispatchBlock() <- getInvokeDestImpl() <- EmitCall().
+ // The following IR is emitted in this case:
+ // On aarch64-linux-gnu (landing-pad based)
+ // %call = invoke i32 @may_throw()
+ // to label %invoke.cont unwind label %lpad, !dbg !19
+ // On aarch64-pc-windows-msvc (funclet based)
+ // %call = invoke i32 @may_throw()
+ // to label %invoke.cont unwind label %catch.dispatch, !dbg !17
+
// Leave the try.
- if (S.getNumCatchStmts())
- CGF.popCatchScope();
+ llvm::BasicBlock *DispatchBlock = nullptr;
+ if (S.getNumCatchStmts()) {
+ // The dispatch block that was created during the emission of the try block
+ // was cached. We retrieve it when popping the current catch scope.
+ DispatchBlock = CGF.popCatchScope();
----------------
aheejin wrote:
Should we change `popCatchScope`'s signature? I think that `popCatchScope` returns not a scope but its EH dispatch block is a little weird. Also this way `popCatchScope` calls `catchScope.getCachedEHDispatchBlock()`, which can create an EH dispatch block if one doesn't exist. This is a side effect that other callsites of `popCatchScope` don't expect.
Can we do this from the callsite without changing `popCatchScope`?
```cpp
auto &CatchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
DispatchBlock = CatchScope.getCachedEHDispatchBlock();
CGF.popCatchScope();
```
https://github.com/llvm/llvm-project/pull/215562
More information about the cfe-commits
mailing list