1 //===-- WasmEHPrepare - Prepare excepton handling for WebAssembly --------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This transformation is designed for use by code generators which use
10 // WebAssembly exception handling scheme. This currently supports C++
13 // WebAssembly exception handling uses Windows exception IR for the middle level
14 // representation. This pass does the following transformation for every
16 // (In C-style pseudocode)
20 // exn = wasm.get.exception();
21 // selector = wasm.get.selector();
26 // exn = wasm.extract.exception();
27 // // Only add below in case it's not a single catch (...)
28 // wasm.landingpad.index(index);
29 // __wasm_lpad_context.lpad_index = index;
30 // __wasm_lpad_context.lsda = wasm.lsda();
31 // _Unwind_CallPersonality(exn);
32 // selector = __wasm.landingpad_context.selector;
36 // * Background: Direct personality function call
37 // In WebAssembly EH, the VM is responsible for unwinding the stack once an
38 // exception is thrown. After the stack is unwound, the control flow is
39 // transfered to WebAssembly 'catch' instruction.
41 // Unwinding the stack is not done by libunwind but the VM, so the personality
42 // function in libcxxabi cannot be called from libunwind during the unwinding
43 // process. So after a catch instruction, we insert a call to a wrapper function
44 // in libunwind that in turn calls the real personality function.
46 // In Itanium EH, if the personality function decides there is no matching catch
47 // clause in a call frame and no cleanup action to perform, the unwinder doesn't
48 // stop there and continues unwinding. But in Wasm EH, the unwinder stops at
49 // every call frame with a catch intruction, after which the personality
50 // function is called from the compiler-generated user code here.
52 // In libunwind, we have this struct that serves as a communincation channel
53 // between the compiler-generated user code and the personality function in
56 // struct _Unwind_LandingPadContext {
57 // uintptr_t lpad_index;
59 // uintptr_t selector;
61 // struct _Unwind_LandingPadContext __wasm_lpad_context = ...;
63 // And this wrapper in libunwind calls the personality function.
65 // _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
66 // struct _Unwind_Exception *exception_obj =
67 // (struct _Unwind_Exception *)exception_ptr;
68 // _Unwind_Reason_Code ret = __gxx_personality_v0(
69 // 1, _UA_CLEANUP_PHASE, exception_obj->exception_class, exception_obj,
70 // (struct _Unwind_Context *)__wasm_lpad_context);
74 // We pass a landing pad index, and the address of LSDA for the current function
75 // to the wrapper function _Unwind_CallPersonality in libunwind, and we retrieve
76 // the selector after it returns.
78 //===----------------------------------------------------------------------===//
80 #include "llvm/ADT/BreadthFirstIterator.h"
81 #include "llvm/ADT/SetVector.h"
82 #include "llvm/ADT/Statistic.h"
83 #include "llvm/ADT/Triple.h"
84 #include "llvm/Analysis/DomTreeUpdater.h"
85 #include "llvm/CodeGen/Passes.h"
86 #include "llvm/CodeGen/TargetLowering.h"
87 #include "llvm/CodeGen/TargetSubtargetInfo.h"
88 #include "llvm/CodeGen/WasmEHFuncInfo.h"
89 #include "llvm/IR/Dominators.h"
90 #include "llvm/IR/IRBuilder.h"
91 #include "llvm/IR/Intrinsics.h"
92 #include "llvm/IR/IntrinsicsWebAssembly.h"
93 #include "llvm/InitializePasses.h"
94 #include "llvm/Pass.h"
95 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
99 #define DEBUG_TYPE "wasmehprepare"
102 class WasmEHPrepare : public FunctionPass {
103 Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
104 GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context
106 // Field addresses of struct _Unwind_LandingPadContext
107 Value *LPadIndexField = nullptr; // lpad_index field
108 Value *LSDAField = nullptr; // lsda field
109 Value *SelectorField = nullptr; // selector
111 Function *ThrowF = nullptr; // wasm.throw() intrinsic
112 Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic
113 Function *LSDAF = nullptr; // wasm.lsda() intrinsic
114 Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
115 Function *ExtractExnF = nullptr; // wasm.extract.exception() intrinsic
116 Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
117 FunctionCallee CallPersonalityF =
118 nullptr; // _Unwind_CallPersonality() wrapper
120 bool prepareEHPads(Function &F);
121 bool prepareThrows(Function &F);
123 bool IsEHPadFunctionsSetUp = false;
124 void setupEHPadFunctions(Function &F);
125 void prepareEHPad(BasicBlock *BB, bool NeedPersonality, bool NeedLSDA = false,
127 void prepareTerminateCleanupPad(BasicBlock *BB);
130 static char ID; // Pass identification, replacement for typeid
132 WasmEHPrepare() : FunctionPass(ID) {}
133 void getAnalysisUsage(AnalysisUsage &AU) const override;
134 bool doInitialization(Module &M) override;
135 bool runOnFunction(Function &F) override;
137 StringRef getPassName() const override {
138 return "WebAssembly Exception handling preparation";
141 } // end anonymous namespace
143 char WasmEHPrepare::ID = 0;
144 INITIALIZE_PASS_BEGIN(WasmEHPrepare, DEBUG_TYPE,
145 "Prepare WebAssembly exceptions", false, false)
146 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
147 INITIALIZE_PASS_END(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions",
150 FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
152 void WasmEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
153 AU.addRequired<DominatorTreeWrapperPass>();
156 bool WasmEHPrepare::doInitialization(Module &M) {
157 IRBuilder<> IRB(M.getContext());
158 LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
159 IRB.getInt8PtrTy(), // lsda
160 IRB.getInt32Ty() // selector
165 // Erase the specified BBs if the BB does not have any remaining predecessors,
166 // and also all its dead children.
167 template <typename Container>
168 static void eraseDeadBBsAndChildren(const Container &BBs, DomTreeUpdater *DTU) {
169 SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end());
170 while (!WL.empty()) {
171 auto *BB = WL.pop_back_val();
172 if (pred_begin(BB) != pred_end(BB))
174 WL.append(succ_begin(BB), succ_end(BB));
175 DeleteDeadBlock(BB, DTU);
179 bool WasmEHPrepare::runOnFunction(Function &F) {
180 IsEHPadFunctionsSetUp = false;
181 bool Changed = false;
182 Changed |= prepareThrows(F);
183 Changed |= prepareEHPads(F);
187 bool WasmEHPrepare::prepareThrows(Function &F) {
188 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
189 DomTreeUpdater DTU(&DT, /*PostDominatorTree*/ nullptr,
190 DomTreeUpdater::UpdateStrategy::Eager);
191 Module &M = *F.getParent();
192 IRBuilder<> IRB(F.getContext());
193 bool Changed = false;
195 // wasm.throw() intinsic, which will be lowered to wasm 'throw' instruction.
196 ThrowF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_throw);
197 // Insert an unreachable instruction after a call to @llvm.wasm.throw and
198 // delete all following instructions within the BB, and delete all the dead
199 // children of the BB as well.
200 for (User *U : ThrowF->users()) {
201 // A call to @llvm.wasm.throw() is only generated from __cxa_throw()
202 // builtin call within libcxxabi, and cannot be an InvokeInst.
203 auto *ThrowI = cast<CallInst>(U);
204 if (ThrowI->getFunction() != &F)
207 auto *BB = ThrowI->getParent();
208 SmallVector<BasicBlock *, 4> Succs(succ_begin(BB), succ_end(BB));
209 auto &InstList = BB->getInstList();
210 InstList.erase(std::next(BasicBlock::iterator(ThrowI)), InstList.end());
211 IRB.SetInsertPoint(BB);
212 IRB.CreateUnreachable();
213 eraseDeadBBsAndChildren(Succs, &DTU);
219 bool WasmEHPrepare::prepareEHPads(Function &F) {
220 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
221 bool Changed = false;
223 // There are two things to decide: whether we need a personality function call
224 // and whether we need a `wasm.lsda()` call and its store.
226 // For the personality function call, catchpads with `catch (...)` and
227 // cleanuppads don't need it, because exceptions are always caught. Others all
230 // For `wasm.lsda()` and its store, in order to minimize the number of them,
231 // we need a way to figure out whether we have encountered `wasm.lsda()` call
232 // in any of EH pads that dominates the current EH pad. To figure that out, we
233 // now visit EH pads in BFS order in the dominator tree so that we visit
234 // parent BBs first before visiting its child BBs in the domtree.
236 // We keep a set named `ExecutedLSDA`, which basically means "Do we have
237 // `wasm.lsda() either in the current EH pad or any of its parent EH pads in
238 // the dominator tree?". This is to prevent scanning the domtree up to the
239 // root every time we examine an EH pad, in the worst case: each EH pad only
240 // needs to check its immediate parent EH pad.
242 // - If any of its parent EH pads in the domtree has `wasm.lsda`, this means
243 // we don't need `wasm.lsda()` in the current EH pad. We also insert the
244 // current EH pad in `ExecutedLSDA` set.
245 // - If none of its parent EH pad has `wasm.lsda()`,
246 // - If the current EH pad is a `catch (...)` or a cleanuppad, done.
247 // - If the current EH pad is neither a `catch (...)` nor a cleanuppad,
248 // add `wasm.lsda()` and the store in the current EH pad, and add the
249 // current EH pad to `ExecutedLSDA` set.
251 // TODO Can we not store LSDA address in user function but make libcxxabi
253 DenseSet<Value *> ExecutedLSDA;
255 for (auto DomNode : breadth_first(&DT)) {
256 auto *BB = DomNode->getBlock();
257 auto *Pad = BB->getFirstNonPHI();
258 if (!Pad || (!isa<CatchPadInst>(Pad) && !isa<CleanupPadInst>(Pad)))
262 Value *ParentPad = nullptr;
263 if (CatchPadInst *CPI = dyn_cast<CatchPadInst>(Pad)) {
264 ParentPad = CPI->getCatchSwitch()->getParentPad();
265 if (ExecutedLSDA.count(ParentPad)) {
266 ExecutedLSDA.insert(CPI);
267 // We insert its associated catchswitch too, because
268 // FuncletPadInst::getParentPad() returns a CatchSwitchInst if the child
269 // FuncletPadInst is a CleanupPadInst.
270 ExecutedLSDA.insert(CPI->getCatchSwitch());
272 } else { // CleanupPadInst
273 ParentPad = cast<CleanupPadInst>(Pad)->getParentPad();
274 if (ExecutedLSDA.count(ParentPad))
275 ExecutedLSDA.insert(Pad);
278 if (CatchPadInst *CPI = dyn_cast<CatchPadInst>(Pad)) {
279 if (CPI->getNumArgOperands() == 1 &&
280 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
281 // In case of a single catch (...), we need neither personality call nor
283 prepareEHPad(BB, false);
285 if (ExecutedLSDA.count(CPI))
286 // catch (type), but one of parents already has wasm.lsda() call
287 prepareEHPad(BB, true, false, Index++);
289 // catch (type), and none of parents has wasm.lsda() call. We have to
290 // add the call in this EH pad, and record this EH pad in
292 ExecutedLSDA.insert(CPI);
293 ExecutedLSDA.insert(CPI->getCatchSwitch());
294 prepareEHPad(BB, true, true, Index++);
297 } else if (isa<CleanupPadInst>(Pad)) {
298 // Cleanup pads need neither personality call nor wasm.lsda() call
299 prepareEHPad(BB, false);
306 void WasmEHPrepare::setupEHPadFunctions(Function &F) {
307 Module &M = *F.getParent();
308 IRBuilder<> IRB(F.getContext());
309 assert(F.hasPersonalityFn() && "Personality function not found");
311 // __wasm_lpad_context global variable
312 LPadContextGV = cast<GlobalVariable>(
313 M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy));
314 LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
317 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1, "lsda_gep");
318 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
321 // wasm.landingpad.index() intrinsic, which is to specify landingpad index
322 LPadIndexF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_landingpad_index);
323 // wasm.lsda() intrinsic. Returns the address of LSDA table for the current
325 LSDAF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_lsda);
326 // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these
327 // are generated in clang.
328 GetExnF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_exception);
329 GetSelectorF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_ehselector);
331 // wasm.extract.exception() is the same as wasm.get.exception() but it does
332 // not take a token argument. This will be lowered down to EXTRACT_EXCEPTION
333 // pseudo instruction in instruction selection, which will be expanded using
334 // 'br_on_exn' instruction later.
336 Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception);
338 // _Unwind_CallPersonality() wrapper function, which calls the personality
339 CallPersonalityF = M.getOrInsertFunction(
340 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
341 if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
342 F->setDoesNotThrow();
345 // Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
347 void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
348 bool NeedLSDA, unsigned Index) {
349 if (!IsEHPadFunctionsSetUp) {
350 IsEHPadFunctionsSetUp = true;
351 setupEHPadFunctions(*BB->getParent());
353 assert(BB->isEHPad() && "BB is not an EHPad!");
354 IRBuilder<> IRB(BB->getContext());
355 IRB.SetInsertPoint(&*BB->getFirstInsertionPt());
357 auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHI());
358 Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr;
359 for (auto &U : FPI->uses()) {
360 if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
361 if (CI->getCalledOperand() == GetExnF)
363 if (CI->getCalledOperand() == GetSelectorF)
368 // Cleanup pads w/o __clang_call_terminate call do not have any of
369 // wasm.get.exception() or wasm.get.ehselector() calls. We need to do nothing.
371 assert(!GetSelectorCI &&
372 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
376 Instruction *ExtractExnCI = IRB.CreateCall(ExtractExnF, {}, "exn");
377 GetExnCI->replaceAllUsesWith(ExtractExnCI);
378 GetExnCI->eraseFromParent();
380 // In case it is a catchpad with single catch (...) or a cleanuppad, we don't
381 // need to call personality function because we don't need a selector.
382 if (!NeedPersonality) {
384 assert(GetSelectorCI->use_empty() &&
385 "wasm.get.ehselector() still has uses!");
386 GetSelectorCI->eraseFromParent();
390 IRB.SetInsertPoint(ExtractExnCI->getNextNode());
392 // This is to create a map of <landingpad EH label, landingpad index> in
393 // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables.
394 // Pseudocode: wasm.landingpad.index(Index);
395 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)});
397 // Pseudocode: __wasm_lpad_context.lpad_index = index;
398 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
400 auto *CPI = cast<CatchPadInst>(FPI);
402 // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda();
403 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
405 // Pseudocode: _Unwind_CallPersonality(exn);
406 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, ExtractExnCI,
407 OperandBundleDef("funclet", CPI));
408 PersCI->setDoesNotThrow();
410 // Pseudocode: int selector = __wasm.landingpad_context.selector;
411 Instruction *Selector =
412 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField, "selector");
414 // Replace the return value from wasm.get.ehselector() with the selector value
415 // loaded from __wasm_lpad_context.selector.
416 assert(GetSelectorCI && "wasm.get.ehselector() call does not exist");
417 GetSelectorCI->replaceAllUsesWith(Selector);
418 GetSelectorCI->eraseFromParent();
421 void llvm::calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo) {
422 // If an exception is not caught by a catchpad (i.e., it is a foreign
423 // exception), it will unwind to its parent catchswitch's unwind destination.
424 // We don't record an unwind destination for cleanuppads because every
425 // exception should be caught by it.
426 for (const auto &BB : *F) {
429 const Instruction *Pad = BB.getFirstNonPHI();
431 if (const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
432 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
435 const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
436 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
437 // Currently there should be only one handler per a catchswitch.
438 EHInfo.setEHPadUnwindDest(&BB, *CatchSwitch->handlers().begin());
440 EHInfo.setEHPadUnwindDest(&BB, UnwindBB);