1336809Sdim//===-- WasmEHPrepare - Prepare excepton handling for WebAssembly --------===// 2336809Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6336809Sdim// 7336809Sdim//===----------------------------------------------------------------------===// 8336809Sdim// 9336809Sdim// This transformation is designed for use by code generators which use 10353358Sdim// WebAssembly exception handling scheme. This currently supports C++ 11353358Sdim// exceptions. 12336809Sdim// 13336809Sdim// WebAssembly exception handling uses Windows exception IR for the middle level 14336809Sdim// representation. This pass does the following transformation for every 15336809Sdim// catchpad block: 16336809Sdim// (In C-style pseudocode) 17336809Sdim// 18336809Sdim// - Before: 19336809Sdim// catchpad ... 20336809Sdim// exn = wasm.get.exception(); 21336809Sdim// selector = wasm.get.selector(); 22336809Sdim// ... 23336809Sdim// 24336809Sdim// - After: 25336809Sdim// catchpad ... 26353358Sdim// exn = wasm.extract.exception(); 27353358Sdim// // Only add below in case it's not a single catch (...) 28336809Sdim// wasm.landingpad.index(index); 29336809Sdim// __wasm_lpad_context.lpad_index = index; 30336809Sdim// __wasm_lpad_context.lsda = wasm.lsda(); 31336809Sdim// _Unwind_CallPersonality(exn); 32353358Sdim// selector = __wasm.landingpad_context.selector; 33336809Sdim// ... 34336809Sdim// 35336809Sdim// 36336809Sdim// * Background: Direct personality function call 37336809Sdim// In WebAssembly EH, the VM is responsible for unwinding the stack once an 38336809Sdim// exception is thrown. After the stack is unwound, the control flow is 39353358Sdim// transfered to WebAssembly 'catch' instruction. 40336809Sdim// 41336809Sdim// Unwinding the stack is not done by libunwind but the VM, so the personality 42336809Sdim// function in libcxxabi cannot be called from libunwind during the unwinding 43336809Sdim// process. So after a catch instruction, we insert a call to a wrapper function 44336809Sdim// in libunwind that in turn calls the real personality function. 45336809Sdim// 46336809Sdim// In Itanium EH, if the personality function decides there is no matching catch 47336809Sdim// clause in a call frame and no cleanup action to perform, the unwinder doesn't 48336809Sdim// stop there and continues unwinding. But in Wasm EH, the unwinder stops at 49336809Sdim// every call frame with a catch intruction, after which the personality 50336809Sdim// function is called from the compiler-generated user code here. 51336809Sdim// 52336809Sdim// In libunwind, we have this struct that serves as a communincation channel 53336809Sdim// between the compiler-generated user code and the personality function in 54336809Sdim// libcxxabi. 55336809Sdim// 56336809Sdim// struct _Unwind_LandingPadContext { 57336809Sdim// uintptr_t lpad_index; 58336809Sdim// uintptr_t lsda; 59336809Sdim// uintptr_t selector; 60336809Sdim// }; 61336809Sdim// struct _Unwind_LandingPadContext __wasm_lpad_context = ...; 62336809Sdim// 63336809Sdim// And this wrapper in libunwind calls the personality function. 64336809Sdim// 65336809Sdim// _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) { 66336809Sdim// struct _Unwind_Exception *exception_obj = 67336809Sdim// (struct _Unwind_Exception *)exception_ptr; 68336809Sdim// _Unwind_Reason_Code ret = __gxx_personality_v0( 69336809Sdim// 1, _UA_CLEANUP_PHASE, exception_obj->exception_class, exception_obj, 70336809Sdim// (struct _Unwind_Context *)__wasm_lpad_context); 71336809Sdim// return ret; 72336809Sdim// } 73336809Sdim// 74336809Sdim// We pass a landing pad index, and the address of LSDA for the current function 75336809Sdim// to the wrapper function _Unwind_CallPersonality in libunwind, and we retrieve 76336809Sdim// the selector after it returns. 77336809Sdim// 78336809Sdim//===----------------------------------------------------------------------===// 79336809Sdim 80336809Sdim#include "llvm/ADT/SetVector.h" 81336809Sdim#include "llvm/ADT/Statistic.h" 82336809Sdim#include "llvm/ADT/Triple.h" 83336809Sdim#include "llvm/CodeGen/Passes.h" 84336809Sdim#include "llvm/CodeGen/TargetLowering.h" 85336809Sdim#include "llvm/CodeGen/TargetSubtargetInfo.h" 86336809Sdim#include "llvm/CodeGen/WasmEHFuncInfo.h" 87336809Sdim#include "llvm/IR/Dominators.h" 88336809Sdim#include "llvm/IR/IRBuilder.h" 89336809Sdim#include "llvm/IR/Intrinsics.h" 90360784Sdim#include "llvm/IR/IntrinsicsWebAssembly.h" 91360784Sdim#include "llvm/InitializePasses.h" 92336809Sdim#include "llvm/Pass.h" 93336809Sdim#include "llvm/Transforms/Utils/BasicBlockUtils.h" 94336809Sdim 95336809Sdimusing namespace llvm; 96336809Sdim 97336809Sdim#define DEBUG_TYPE "wasmehprepare" 98336809Sdim 99336809Sdimnamespace { 100336809Sdimclass WasmEHPrepare : public FunctionPass { 101336809Sdim Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext' 102336809Sdim GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context 103336809Sdim 104336809Sdim // Field addresses of struct _Unwind_LandingPadContext 105336809Sdim Value *LPadIndexField = nullptr; // lpad_index field 106336809Sdim Value *LSDAField = nullptr; // lsda field 107336809Sdim Value *SelectorField = nullptr; // selector 108336809Sdim 109353358Sdim Function *ThrowF = nullptr; // wasm.throw() intrinsic 110353358Sdim Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic 111353358Sdim Function *LSDAF = nullptr; // wasm.lsda() intrinsic 112353358Sdim Function *GetExnF = nullptr; // wasm.get.exception() intrinsic 113353358Sdim Function *ExtractExnF = nullptr; // wasm.extract.exception() intrinsic 114353358Sdim Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic 115353358Sdim FunctionCallee CallPersonalityF = 116353358Sdim nullptr; // _Unwind_CallPersonality() wrapper 117336809Sdim 118344779Sdim bool prepareEHPads(Function &F); 119344779Sdim bool prepareThrows(Function &F); 120344779Sdim 121353358Sdim void prepareEHPad(BasicBlock *BB, bool NeedLSDA, unsigned Index = 0); 122336809Sdim void prepareTerminateCleanupPad(BasicBlock *BB); 123336809Sdim 124336809Sdimpublic: 125336809Sdim static char ID; // Pass identification, replacement for typeid 126336809Sdim 127336809Sdim WasmEHPrepare() : FunctionPass(ID) {} 128336809Sdim 129336809Sdim bool doInitialization(Module &M) override; 130336809Sdim bool runOnFunction(Function &F) override; 131336809Sdim 132336809Sdim StringRef getPassName() const override { 133336809Sdim return "WebAssembly Exception handling preparation"; 134336809Sdim } 135336809Sdim}; 136336809Sdim} // end anonymous namespace 137336809Sdim 138336809Sdimchar WasmEHPrepare::ID = 0; 139336809SdimINITIALIZE_PASS(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", 140336809Sdim false, false) 141336809Sdim 142336809SdimFunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); } 143336809Sdim 144336809Sdimbool WasmEHPrepare::doInitialization(Module &M) { 145336809Sdim IRBuilder<> IRB(M.getContext()); 146336809Sdim LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index 147336809Sdim IRB.getInt8PtrTy(), // lsda 148336809Sdim IRB.getInt32Ty() // selector 149336809Sdim ); 150336809Sdim return false; 151336809Sdim} 152336809Sdim 153344779Sdim// Erase the specified BBs if the BB does not have any remaining predecessors, 154344779Sdim// and also all its dead children. 155344779Sdimtemplate <typename Container> 156344779Sdimstatic void eraseDeadBBsAndChildren(const Container &BBs) { 157344779Sdim SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end()); 158344779Sdim while (!WL.empty()) { 159344779Sdim auto *BB = WL.pop_back_val(); 160344779Sdim if (pred_begin(BB) != pred_end(BB)) 161344779Sdim continue; 162344779Sdim WL.append(succ_begin(BB), succ_end(BB)); 163344779Sdim DeleteDeadBlock(BB); 164344779Sdim } 165344779Sdim} 166344779Sdim 167336809Sdimbool WasmEHPrepare::runOnFunction(Function &F) { 168344779Sdim bool Changed = false; 169344779Sdim Changed |= prepareThrows(F); 170344779Sdim Changed |= prepareEHPads(F); 171344779Sdim return Changed; 172344779Sdim} 173344779Sdim 174344779Sdimbool WasmEHPrepare::prepareThrows(Function &F) { 175344779Sdim Module &M = *F.getParent(); 176344779Sdim IRBuilder<> IRB(F.getContext()); 177344779Sdim bool Changed = false; 178344779Sdim 179344779Sdim // wasm.throw() intinsic, which will be lowered to wasm 'throw' instruction. 180344779Sdim ThrowF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_throw); 181344779Sdim // Insert an unreachable instruction after a call to @llvm.wasm.throw and 182344779Sdim // delete all following instructions within the BB, and delete all the dead 183344779Sdim // children of the BB as well. 184344779Sdim for (User *U : ThrowF->users()) { 185353358Sdim // A call to @llvm.wasm.throw() is only generated from __cxa_throw() 186353358Sdim // builtin call within libcxxabi, and cannot be an InvokeInst. 187344779Sdim auto *ThrowI = cast<CallInst>(U); 188344779Sdim if (ThrowI->getFunction() != &F) 189344779Sdim continue; 190344779Sdim Changed = true; 191344779Sdim auto *BB = ThrowI->getParent(); 192344779Sdim SmallVector<BasicBlock *, 4> Succs(succ_begin(BB), succ_end(BB)); 193344779Sdim auto &InstList = BB->getInstList(); 194344779Sdim InstList.erase(std::next(BasicBlock::iterator(ThrowI)), InstList.end()); 195344779Sdim IRB.SetInsertPoint(BB); 196344779Sdim IRB.CreateUnreachable(); 197344779Sdim eraseDeadBBsAndChildren(Succs); 198344779Sdim } 199344779Sdim 200344779Sdim return Changed; 201344779Sdim} 202344779Sdim 203344779Sdimbool WasmEHPrepare::prepareEHPads(Function &F) { 204344779Sdim Module &M = *F.getParent(); 205344779Sdim IRBuilder<> IRB(F.getContext()); 206344779Sdim 207336809Sdim SmallVector<BasicBlock *, 16> CatchPads; 208336809Sdim SmallVector<BasicBlock *, 16> CleanupPads; 209336809Sdim for (BasicBlock &BB : F) { 210336809Sdim if (!BB.isEHPad()) 211336809Sdim continue; 212336809Sdim auto *Pad = BB.getFirstNonPHI(); 213336809Sdim if (isa<CatchPadInst>(Pad)) 214336809Sdim CatchPads.push_back(&BB); 215336809Sdim else if (isa<CleanupPadInst>(Pad)) 216336809Sdim CleanupPads.push_back(&BB); 217336809Sdim } 218336809Sdim 219336809Sdim if (CatchPads.empty() && CleanupPads.empty()) 220336809Sdim return false; 221336809Sdim assert(F.hasPersonalityFn() && "Personality function not found"); 222336809Sdim 223336809Sdim // __wasm_lpad_context global variable 224336809Sdim LPadContextGV = cast<GlobalVariable>( 225336809Sdim M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy)); 226336809Sdim LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0, 227336809Sdim "lpad_index_gep"); 228336809Sdim LSDAField = 229336809Sdim IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1, "lsda_gep"); 230336809Sdim SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2, 231336809Sdim "selector_gep"); 232336809Sdim 233336809Sdim // wasm.landingpad.index() intrinsic, which is to specify landingpad index 234336809Sdim LPadIndexF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_landingpad_index); 235336809Sdim // wasm.lsda() intrinsic. Returns the address of LSDA table for the current 236336809Sdim // function. 237336809Sdim LSDAF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_lsda); 238336809Sdim // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these 239336809Sdim // are generated in clang. 240336809Sdim GetExnF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_exception); 241336809Sdim GetSelectorF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_ehselector); 242336809Sdim 243353358Sdim // wasm.extract.exception() is the same as wasm.get.exception() but it does 244353358Sdim // not take a token argument. This will be lowered down to EXTRACT_EXCEPTION 245353358Sdim // pseudo instruction in instruction selection, which will be expanded using 246353358Sdim // 'br_on_exn' instruction later. 247353358Sdim ExtractExnF = 248353358Sdim Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception); 249353358Sdim 250336809Sdim // _Unwind_CallPersonality() wrapper function, which calls the personality 251353358Sdim CallPersonalityF = M.getOrInsertFunction( 252353358Sdim "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()); 253353358Sdim if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee())) 254353358Sdim F->setDoesNotThrow(); 255336809Sdim 256336809Sdim unsigned Index = 0; 257336809Sdim for (auto *BB : CatchPads) { 258336809Sdim auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI()); 259336809Sdim // In case of a single catch (...), we don't need to emit LSDA 260336809Sdim if (CPI->getNumArgOperands() == 1 && 261336809Sdim cast<Constant>(CPI->getArgOperand(0))->isNullValue()) 262353358Sdim prepareEHPad(BB, false); 263336809Sdim else 264353358Sdim prepareEHPad(BB, true, Index++); 265336809Sdim } 266336809Sdim 267353358Sdim // Cleanup pads don't need LSDA. 268336809Sdim for (auto *BB : CleanupPads) 269353358Sdim prepareEHPad(BB, false); 270336809Sdim 271336809Sdim return true; 272336809Sdim} 273336809Sdim 274353358Sdim// Prepare an EH pad for Wasm EH handling. If NeedLSDA is false, Index is 275353358Sdim// ignored. 276353358Sdimvoid WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedLSDA, 277353358Sdim unsigned Index) { 278336809Sdim assert(BB->isEHPad() && "BB is not an EHPad!"); 279336809Sdim IRBuilder<> IRB(BB->getContext()); 280353358Sdim IRB.SetInsertPoint(&*BB->getFirstInsertionPt()); 281336809Sdim 282336809Sdim auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHI()); 283336809Sdim Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr; 284336809Sdim for (auto &U : FPI->uses()) { 285336809Sdim if (auto *CI = dyn_cast<CallInst>(U.getUser())) { 286336809Sdim if (CI->getCalledValue() == GetExnF) 287336809Sdim GetExnCI = CI; 288353358Sdim if (CI->getCalledValue() == GetSelectorF) 289336809Sdim GetSelectorCI = CI; 290336809Sdim } 291336809Sdim } 292336809Sdim 293353358Sdim // Cleanup pads w/o __clang_call_terminate call do not have any of 294353358Sdim // wasm.get.exception() or wasm.get.ehselector() calls. We need to do nothing. 295353358Sdim if (!GetExnCI) { 296353358Sdim assert(!GetSelectorCI && 297353358Sdim "wasm.get.ehselector() cannot exist w/o wasm.get.exception()"); 298353358Sdim return; 299353358Sdim } 300353358Sdim 301353358Sdim Instruction *ExtractExnCI = IRB.CreateCall(ExtractExnF, {}, "exn"); 302353358Sdim GetExnCI->replaceAllUsesWith(ExtractExnCI); 303336809Sdim GetExnCI->eraseFromParent(); 304336809Sdim 305336809Sdim // In case it is a catchpad with single catch (...) or a cleanuppad, we don't 306336809Sdim // need to call personality function because we don't need a selector. 307353358Sdim if (!NeedLSDA) { 308336809Sdim if (GetSelectorCI) { 309336809Sdim assert(GetSelectorCI->use_empty() && 310336809Sdim "wasm.get.ehselector() still has uses!"); 311336809Sdim GetSelectorCI->eraseFromParent(); 312336809Sdim } 313336809Sdim return; 314336809Sdim } 315353358Sdim IRB.SetInsertPoint(ExtractExnCI->getNextNode()); 316336809Sdim 317336809Sdim // This is to create a map of <landingpad EH label, landingpad index> in 318336809Sdim // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables. 319336809Sdim // Pseudocode: wasm.landingpad.index(Index); 320344779Sdim IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)}); 321336809Sdim 322336809Sdim // Pseudocode: __wasm_lpad_context.lpad_index = index; 323336809Sdim IRB.CreateStore(IRB.getInt32(Index), LPadIndexField); 324336809Sdim 325336809Sdim // Store LSDA address only if this catchpad belongs to a top-level 326336809Sdim // catchswitch. If there is another catchpad that dominates this pad, we don't 327336809Sdim // need to store LSDA address again, because they are the same throughout the 328336809Sdim // function and have been already stored before. 329336809Sdim // TODO Can we not store LSDA address in user function but make libcxxabi 330336809Sdim // compute it? 331336809Sdim auto *CPI = cast<CatchPadInst>(FPI); 332336809Sdim if (isa<ConstantTokenNone>(CPI->getCatchSwitch()->getParentPad())) 333336809Sdim // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda(); 334336809Sdim IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField); 335336809Sdim 336336809Sdim // Pseudocode: _Unwind_CallPersonality(exn); 337353358Sdim CallInst *PersCI = IRB.CreateCall(CallPersonalityF, ExtractExnCI, 338353358Sdim OperandBundleDef("funclet", CPI)); 339336809Sdim PersCI->setDoesNotThrow(); 340336809Sdim 341336809Sdim // Pseudocode: int selector = __wasm.landingpad_context.selector; 342353358Sdim Instruction *Selector = 343353358Sdim IRB.CreateLoad(IRB.getInt32Ty(), SelectorField, "selector"); 344336809Sdim 345336809Sdim // Replace the return value from wasm.get.ehselector() with the selector value 346336809Sdim // loaded from __wasm_lpad_context.selector. 347336809Sdim assert(GetSelectorCI && "wasm.get.ehselector() call does not exist"); 348336809Sdim GetSelectorCI->replaceAllUsesWith(Selector); 349336809Sdim GetSelectorCI->eraseFromParent(); 350336809Sdim} 351336809Sdim 352336809Sdimvoid llvm::calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo) { 353353358Sdim // If an exception is not caught by a catchpad (i.e., it is a foreign 354353358Sdim // exception), it will unwind to its parent catchswitch's unwind destination. 355353358Sdim // We don't record an unwind destination for cleanuppads because every 356353358Sdim // exception should be caught by it. 357336809Sdim for (const auto &BB : *F) { 358336809Sdim if (!BB.isEHPad()) 359336809Sdim continue; 360336809Sdim const Instruction *Pad = BB.getFirstNonPHI(); 361336809Sdim 362336809Sdim if (const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) { 363336809Sdim const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest(); 364336809Sdim if (!UnwindBB) 365336809Sdim continue; 366336809Sdim const Instruction *UnwindPad = UnwindBB->getFirstNonPHI(); 367336809Sdim if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad)) 368336809Sdim // Currently there should be only one handler per a catchswitch. 369336809Sdim EHInfo.setEHPadUnwindDest(&BB, *CatchSwitch->handlers().begin()); 370336809Sdim else // cleanuppad 371336809Sdim EHInfo.setEHPadUnwindDest(&BB, UnwindBB); 372336809Sdim } 373336809Sdim } 374336809Sdim} 375