//===-- WasmEHPrepare - Prepare excepton handling for WebAssembly --------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This transformation is designed for use by code generators which use // WebAssembly exception handling scheme. This currently supports C++ // exceptions. // // WebAssembly exception handling uses Windows exception IR for the middle level // representation. This pass does the following transformation for every // catchpad block: // (In C-style pseudocode) // // - Before: // catchpad ... // exn = wasm.get.exception(); // selector = wasm.get.selector(); // ... // // - After: // catchpad ... // exn = wasm.catch(WebAssembly::CPP_EXCEPTION); // // Only add below in case it's not a single catch (...) // wasm.landingpad.index(index); // __wasm_lpad_context.lpad_index = index; // __wasm_lpad_context.lsda = wasm.lsda(); // _Unwind_CallPersonality(exn); // selector = __wasm_lpad_context.selector; // ... // // // * Background: Direct personality function call // In WebAssembly EH, the VM is responsible for unwinding the stack once an // exception is thrown. After the stack is unwound, the control flow is // transfered to WebAssembly 'catch' instruction. // // Unwinding the stack is not done by libunwind but the VM, so the personality // function in libcxxabi cannot be called from libunwind during the unwinding // process. So after a catch instruction, we insert a call to a wrapper function // in libunwind that in turn calls the real personality function. // // In Itanium EH, if the personality function decides there is no matching catch // clause in a call frame and no cleanup action to perform, the unwinder doesn't // stop there and continues unwinding. But in Wasm EH, the unwinder stops at // every call frame with a catch intruction, after which the personality // function is called from the compiler-generated user code here. // // In libunwind, we have this struct that serves as a communincation channel // between the compiler-generated user code and the personality function in // libcxxabi. // // struct _Unwind_LandingPadContext { // uintptr_t lpad_index; // uintptr_t lsda; // uintptr_t selector; // }; // struct _Unwind_LandingPadContext __wasm_lpad_context = ...; // // And this wrapper in libunwind calls the personality function. // // _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) { // struct _Unwind_Exception *exception_obj = // (struct _Unwind_Exception *)exception_ptr; // _Unwind_Reason_Code ret = __gxx_personality_v0( // 1, _UA_CLEANUP_PHASE, exception_obj->exception_class, exception_obj, // (struct _Unwind_Context *)__wasm_lpad_context); // return ret; // } // // We pass a landing pad index, and the address of LSDA for the current function // to the wrapper function _Unwind_CallPersonality in libunwind, and we retrieve // the selector after it returns. // //===----------------------------------------------------------------------===// #include "llvm/CodeGen/WasmEHPrepare.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/WasmEHFuncInfo.h" #include "llvm/IR/EHPersonalities.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/IntrinsicsWebAssembly.h" #include "llvm/IR/Module.h" #include "llvm/InitializePasses.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" usingnamespacellvm; #define DEBUG_TYPE … namespace { class WasmEHPrepareImpl { … }; class WasmEHPrepare : public FunctionPass { … }; } // end anonymous namespace PreservedAnalyses WasmEHPreparePass::run(Function &F, FunctionAnalysisManager &) { … } char WasmEHPrepare::ID = …; INITIALIZE_PASS_BEGIN(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", false, false) INITIALIZE_PASS_END(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", false, false) FunctionPass *llvm::createWasmEHPass() { … } bool WasmEHPrepare::doInitialization(Module &M) { … } // Erase the specified BBs if the BB does not have any remaining predecessors, // and also all its dead children. template <typename Container> static void eraseDeadBBsAndChildren(const Container &BBs) { … } bool WasmEHPrepareImpl::runOnFunction(Function &F) { … } bool WasmEHPrepareImpl::prepareThrows(Function &F) { … } bool WasmEHPrepareImpl::prepareEHPads(Function &F) { … } // Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is // ignored. void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index) { … } void llvm::calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo) { … }