LLVM 22.0.0git
WasmEHPrepare.cpp
Go to the documentation of this file.
1//===-- WasmEHPrepare - Prepare excepton handling for WebAssembly --------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This transformation is designed for use by code generators which use
10// WebAssembly exception handling scheme. This currently supports C++
11// exceptions.
12//
13// WebAssembly exception handling uses Windows exception IR for the middle level
14// representation. This pass does the following transformation for every
15// catchpad block:
16// (In C-style pseudocode)
17//
18// - Before:
19// catchpad ...
20// exn = wasm.get.exception();
21// selector = wasm.get.selector();
22// ...
23//
24// - After:
25// catchpad ...
26// exn = wasm.catch(WebAssembly::CPP_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_lpad_context.selector;
33// ...
34//
35//
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.
40//
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.
45//
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.
51//
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
54// libcxxabi.
55//
56// struct _Unwind_LandingPadContext {
57// uintptr_t lpad_index;
58// uintptr_t lsda;
59// uintptr_t selector;
60// };
61// struct _Unwind_LandingPadContext __wasm_lpad_context = ...;
62//
63// And this wrapper in libunwind calls the personality function.
64//
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);
71// return ret;
72// }
73//
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.
77//
78//===----------------------------------------------------------------------===//
79
82#include "llvm/CodeGen/Passes.h"
85#include "llvm/IR/IRBuilder.h"
86#include "llvm/IR/IntrinsicsWebAssembly.h"
87#include "llvm/IR/Module.h"
91
92using namespace llvm;
93
94#define DEBUG_TYPE "wasm-eh-prepare"
95
96namespace {
97class WasmEHPrepareImpl {
98 friend class WasmEHPrepare;
99
100 Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
101 GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context
102
103 // Field addresses of struct _Unwind_LandingPadContext
104 Value *LPadIndexField = nullptr; // lpad_index field
105 Value *LSDAField = nullptr; // lsda field
106 Value *SelectorField = nullptr; // selector
107
108 Function *ThrowF = nullptr; // wasm.throw() intrinsic
109 Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic
110 Function *LSDAF = nullptr; // wasm.lsda() intrinsic
111 Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
112 Function *CatchF = nullptr; // wasm.catch() intrinsic
113 Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
114 FunctionCallee CallPersonalityF =
115 nullptr; // _Unwind_CallPersonality() wrapper
116
117 bool prepareThrows(Function &F);
118 bool prepareEHPads(Function &F);
119 void prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index = 0);
120
121public:
122 WasmEHPrepareImpl() = default;
123 WasmEHPrepareImpl(Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
124 bool runOnFunction(Function &F);
125};
126
127class WasmEHPrepare : public FunctionPass {
128 WasmEHPrepareImpl P;
129
130public:
131 static char ID; // Pass identification, replacement for typeid
132
133 WasmEHPrepare() : FunctionPass(ID) {}
134 bool doInitialization(Module &M) override;
135 bool runOnFunction(Function &F) override { return P.runOnFunction(F); }
136
137 StringRef getPassName() const override {
138 return "WebAssembly Exception handling preparation";
139 }
140};
141
142} // end anonymous namespace
143
146 auto &Context = F.getContext();
147 auto *I32Ty = Type::getInt32Ty(Context);
148 auto *PtrTy = PointerType::get(Context, 0);
149 auto *LPadContextTy =
150 StructType::get(I32Ty /*lpad_index*/, PtrTy /*lsda*/, I32Ty /*selector*/);
151 WasmEHPrepareImpl P(LPadContextTy);
152 bool Changed = P.runOnFunction(F);
153 return Changed ? PreservedAnalyses::none() : PreservedAnalyses ::all();
154}
155
156char WasmEHPrepare::ID = 0;
158 "Prepare WebAssembly exceptions", false, false)
159INITIALIZE_PASS_END(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions",
161
162FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
163
164bool WasmEHPrepare::doInitialization(Module &M) {
165 IRBuilder<> IRB(M.getContext());
166 P.LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
167 IRB.getPtrTy(), // lsda
168 IRB.getInt32Ty() // selector
169 );
170 return false;
171}
172
173// Erase the specified BBs if the BB does not have any remaining predecessors,
174// and also all its dead children.
175template <typename Container>
176static void eraseDeadBBsAndChildren(const Container &BBs) {
177 SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end());
178 while (!WL.empty()) {
179 auto *BB = WL.pop_back_val();
180 if (!pred_empty(BB))
181 continue;
182 WL.append(succ_begin(BB), succ_end(BB));
183 DeleteDeadBlock(BB);
184 }
185}
186
187bool WasmEHPrepareImpl::runOnFunction(Function &F) {
188 bool Changed = false;
189 Changed |= prepareThrows(F);
190 Changed |= prepareEHPads(F);
191 return Changed;
192}
193
194bool WasmEHPrepareImpl::prepareThrows(Function &F) {
195 Module &M = *F.getParent();
196 IRBuilder<> IRB(F.getContext());
197 bool Changed = false;
198
199 // wasm.throw() intinsic, which will be lowered to wasm 'throw' instruction.
200 ThrowF = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_throw);
201 // Insert an unreachable instruction after a call to @llvm.wasm.throw and
202 // delete all following instructions within the BB, and delete all the dead
203 // children of the BB as well.
204 for (User *U : ThrowF->users()) {
205 auto *ThrowI = dyn_cast<CallInst>(U);
206 if (!ThrowI || ThrowI->getFunction() != &F)
207 continue;
208 Changed = true;
209 auto *BB = ThrowI->getParent();
211 BB->erase(std::next(BasicBlock::iterator(ThrowI)), BB->end());
212 IRB.SetInsertPoint(BB);
213 IRB.CreateUnreachable();
215 }
216
217 return Changed;
218}
219
220bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
221 Module &M = *F.getParent();
222 IRBuilder<> IRB(F.getContext());
223
226 for (BasicBlock &BB : F) {
227 if (!BB.isEHPad())
228 continue;
229 BasicBlock::iterator Pad = BB.getFirstNonPHIIt();
230 if (isa<CatchPadInst>(Pad))
231 CatchPads.push_back(&BB);
232 else if (isa<CleanupPadInst>(Pad))
233 CleanupPads.push_back(&BB);
234 }
235 if (CatchPads.empty() && CleanupPads.empty())
236 return false;
237
238 if (!F.hasPersonalityFn() ||
239 !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
240 report_fatal_error("Function '" + F.getName() +
241 "' does not have a correct Wasm personality function "
242 "'__gxx_wasm_personality_v0'");
243 }
244 assert(F.hasPersonalityFn() && "Personality function not found");
245
246 // __wasm_lpad_context global variable.
247 // This variable should be thread local. If the target does not support TLS,
248 // we depend on CoalesceFeaturesAndStripAtomics to downgrade it to
249 // non-thread-local ones, in which case we don't allow this object to be
250 // linked with other objects using shared memory.
251 LPadContextGV = M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy);
252 LPadContextGV->setThreadLocalMode(GlobalValue::GeneralDynamicTLSModel);
253
254 LPadIndexField = LPadContextGV;
255 LSDAField = IRB.CreateConstInBoundsGEP2_32(LPadContextTy, LPadContextGV, 0, 1,
256 "lsda_gep");
257 SelectorField = IRB.CreateConstInBoundsGEP2_32(LPadContextTy, LPadContextGV,
258 0, 2, "selector_gep");
259
260 // wasm.landingpad.index() intrinsic, which is to specify landingpad index
261 LPadIndexF =
262 Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_landingpad_index);
263 // wasm.lsda() intrinsic. Returns the address of LSDA table for the current
264 // function.
265 LSDAF = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_lsda);
266 // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these
267 // are generated in clang.
268 GetExnF =
269 Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_get_exception);
270 GetSelectorF =
271 Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_get_ehselector);
272
273 // wasm.catch() will be lowered down to wasm 'catch' instruction in
274 // instruction selection.
275 CatchF = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_catch);
276
277 // FIXME: Verify this is really supported for current module.
278 StringRef UnwindCallPersonalityName =
280 RTLIB::impl__Unwind_CallPersonality);
281
282 // _Unwind_CallPersonality() wrapper function, which calls the personality
283 CallPersonalityF = M.getOrInsertFunction(UnwindCallPersonalityName,
284 IRB.getInt32Ty(), IRB.getPtrTy());
285 if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
286 F->setDoesNotThrow();
287
288 unsigned Index = 0;
289 for (auto *BB : CatchPads) {
290 auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHIIt());
291 // In case of a single catch (...), we don't need to emit a personalify
292 // function call
293 if (CPI->arg_size() == 1 &&
294 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
295 prepareEHPad(BB, false);
296 else
297 prepareEHPad(BB, true, Index++);
298 }
299
300 // Cleanup pads don't need a personality function call.
301 for (auto *BB : CleanupPads)
302 prepareEHPad(BB, false);
303
304 return true;
305}
306
307// Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
308// ignored.
309void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
310 unsigned Index) {
311 assert(BB->isEHPad() && "BB is not an EHPad!");
312 IRBuilder<> IRB(BB->getContext());
313 IRB.SetInsertPoint(BB, BB->getFirstInsertionPt());
314
315 auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHIIt());
316 Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr;
317 for (auto &U : FPI->uses()) {
318 if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
319 if (CI->getCalledOperand() == GetExnF)
320 GetExnCI = CI;
321 if (CI->getCalledOperand() == GetSelectorF)
322 GetSelectorCI = CI;
323 }
324 }
325
326 // Cleanup pads do not have any of wasm.get.exception() or
327 // wasm.get.ehselector() calls. We need to do nothing.
328 if (!GetExnCI) {
329 assert(!GetSelectorCI &&
330 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
331 return;
332 }
333
334 // Replace wasm.get.exception intrinsic with wasm.catch intrinsic, which will
335 // be lowered to wasm 'catch' instruction. We do this mainly because
336 // instruction selection cannot handle wasm.get.exception intrinsic's token
337 // argument.
338 Instruction *CatchCI =
339 IRB.CreateCall(CatchF, {IRB.getInt32(WebAssembly::CPP_EXCEPTION)}, "exn");
340 GetExnCI->replaceAllUsesWith(CatchCI);
341 GetExnCI->eraseFromParent();
342
343 // In case it is a catchpad with single catch (...) or a cleanuppad, we don't
344 // need to call personality function because we don't need a selector.
345 if (!NeedPersonality) {
346 if (GetSelectorCI) {
347 assert(GetSelectorCI->use_empty() &&
348 "wasm.get.ehselector() still has uses!");
349 GetSelectorCI->eraseFromParent();
350 }
351 return;
352 }
353 IRB.SetInsertPoint(CatchCI->getNextNode());
354
355 // This is to create a map of <landingpad EH label, landingpad index> in
356 // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables.
357 // Pseudocode: wasm.landingpad.index(Index);
358 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)});
359
360 // Pseudocode: __wasm_lpad_context.lpad_index = index;
361 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
362
363 auto *CPI = cast<CatchPadInst>(FPI);
364 // TODO Sometimes storing the LSDA address every time is not necessary, in
365 // case it is already set in a dominating EH pad and there is no function call
366 // between from that EH pad to here. Consider optimizing those cases.
367 // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda();
368 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
369
370 // Pseudocode: _Unwind_CallPersonality(exn);
371 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI,
372 OperandBundleDef("funclet", CPI));
373 PersCI->setDoesNotThrow();
374
375 // Pseudocode: int selector = __wasm_lpad_context.selector;
376 Instruction *Selector =
377 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField, "selector");
378
379 // Replace the return value from wasm.get.ehselector() with the selector value
380 // loaded from __wasm_lpad_context.selector.
381 assert(GetSelectorCI && "wasm.get.ehselector() call does not exist");
382 GetSelectorCI->replaceAllUsesWith(Selector);
383 GetSelectorCI->eraseFromParent();
384}
385
387 // If an exception is not caught by a catchpad (i.e., it is a foreign
388 // exception), it will unwind to its parent catchswitch's unwind destination.
389 // We don't record an unwind destination for cleanuppads because every
390 // exception should be caught by it.
391 for (const auto &BB : *F) {
392 if (!BB.isEHPad())
393 continue;
394 const Instruction *Pad = &*BB.getFirstNonPHIIt();
395
396 if (const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
397 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
398 if (!UnwindBB)
399 continue;
400 const Instruction *UnwindPad = &*UnwindBB->getFirstNonPHIIt();
401 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
402 // Currently there should be only one handler per a catchswitch.
403 EHInfo.setUnwindDest(&BB, *CatchSwitch->handlers().begin());
404 else // cleanuppad
405 EHInfo.setUnwindDest(&BB, UnwindBB);
406 }
407 }
408}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define P(N)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
static void eraseDeadBBsAndChildren(const Container &BBs)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition BasicBlock.h:707
void setDoesNotThrow()
This class represents a function call, abstracting a target machine's calling convention.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:413
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
LLVM_ABI FunctionPass * createWasmEHPass()
createWasmEHPass - This pass adapts exception handling code to use WebAssembly's exception handling s...
LLVM_ABI void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
OperandBundleDefT< Value * > OperandBundleDef
Definition AutoUpgrade.h:34
void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:119
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
void setUnwindDest(const BasicBlock *BB, const BasicBlock *Dest)