LLVM 19.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"
89
90using namespace llvm;
91
92#define DEBUG_TYPE "wasm-eh-prepare"
93
94namespace {
95class WasmEHPrepareImpl {
96 friend class WasmEHPrepare;
97
98 Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
99 GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context
100
101 // Field addresses of struct _Unwind_LandingPadContext
102 Value *LPadIndexField = nullptr; // lpad_index field
103 Value *LSDAField = nullptr; // lsda field
104 Value *SelectorField = nullptr; // selector
105
106 Function *ThrowF = nullptr; // wasm.throw() intrinsic
107 Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic
108 Function *LSDAF = nullptr; // wasm.lsda() intrinsic
109 Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
110 Function *CatchF = nullptr; // wasm.catch() intrinsic
111 Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
112 FunctionCallee CallPersonalityF =
113 nullptr; // _Unwind_CallPersonality() wrapper
114
115 bool prepareThrows(Function &F);
116 bool prepareEHPads(Function &F);
117 void prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index = 0);
118
119public:
120 WasmEHPrepareImpl() = default;
121 WasmEHPrepareImpl(Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
122 bool runOnFunction(Function &F);
123};
124
125class WasmEHPrepare : public FunctionPass {
126 WasmEHPrepareImpl P;
127
128public:
129 static char ID; // Pass identification, replacement for typeid
130
131 WasmEHPrepare() : FunctionPass(ID) {}
132 bool doInitialization(Module &M) override;
133 bool runOnFunction(Function &F) override { return P.runOnFunction(F); }
134
135 StringRef getPassName() const override {
136 return "WebAssembly Exception handling preparation";
137 }
138};
139
140} // end anonymous namespace
141
144 auto &Context = F.getContext();
145 auto *I32Ty = Type::getInt32Ty(Context);
146 auto *PtrTy = PointerType::get(Context, 0);
147 auto *LPadContextTy =
148 StructType::get(I32Ty /*lpad_index*/, PtrTy /*lsda*/, I32Ty /*selector*/);
149 WasmEHPrepareImpl P(LPadContextTy);
150 bool Changed = P.runOnFunction(F);
151 return Changed ? PreservedAnalyses::none() : PreservedAnalyses ::all();
152}
153
154char WasmEHPrepare::ID = 0;
156 "Prepare WebAssembly exceptions", false, false)
157INITIALIZE_PASS_END(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions",
159
160FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
161
162bool WasmEHPrepare::doInitialization(Module &M) {
163 IRBuilder<> IRB(M.getContext());
164 P.LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
165 IRB.getPtrTy(), // lsda
166 IRB.getInt32Ty() // selector
167 );
168 return false;
169}
170
171// Erase the specified BBs if the BB does not have any remaining predecessors,
172// and also all its dead children.
173template <typename Container>
174static void eraseDeadBBsAndChildren(const Container &BBs) {
175 SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end());
176 while (!WL.empty()) {
177 auto *BB = WL.pop_back_val();
178 if (!pred_empty(BB))
179 continue;
180 WL.append(succ_begin(BB), succ_end(BB));
181 DeleteDeadBlock(BB);
182 }
183}
184
185bool WasmEHPrepareImpl::runOnFunction(Function &F) {
186 bool Changed = false;
187 Changed |= prepareThrows(F);
188 Changed |= prepareEHPads(F);
189 return Changed;
190}
191
192bool WasmEHPrepareImpl::prepareThrows(Function &F) {
193 Module &M = *F.getParent();
194 IRBuilder<> IRB(F.getContext());
195 bool Changed = false;
196
197 // wasm.throw() intinsic, which will be lowered to wasm 'throw' instruction.
198 ThrowF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_throw);
199 // Insert an unreachable instruction after a call to @llvm.wasm.throw and
200 // delete all following instructions within the BB, and delete all the dead
201 // children of the BB as well.
202 for (User *U : ThrowF->users()) {
203 // A call to @llvm.wasm.throw() is only generated from __cxa_throw()
204 // builtin call within libcxxabi, and cannot be an InvokeInst.
205 auto *ThrowI = cast<CallInst>(U);
206 if (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 auto *Pad = BB.getFirstNonPHI();
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 = cast<GlobalVariable>(
252 M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy));
253 LPadContextGV->setThreadLocalMode(GlobalValue::GeneralDynamicTLSModel);
254
255 LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
256 "lpad_index_gep");
257 LSDAField =
258 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1, "lsda_gep");
259 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
260 "selector_gep");
261
262 // wasm.landingpad.index() intrinsic, which is to specify landingpad index
263 LPadIndexF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_landingpad_index);
264 // wasm.lsda() intrinsic. Returns the address of LSDA table for the current
265 // function.
266 LSDAF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_lsda);
267 // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these
268 // are generated in clang.
269 GetExnF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_exception);
270 GetSelectorF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_ehselector);
271
272 // wasm.catch() will be lowered down to wasm 'catch' instruction in
273 // instruction selection.
274 CatchF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_catch);
275
276 // _Unwind_CallPersonality() wrapper function, which calls the personality
277 CallPersonalityF = M.getOrInsertFunction("_Unwind_CallPersonality",
278 IRB.getInt32Ty(), IRB.getPtrTy());
279 if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
280 F->setDoesNotThrow();
281
282 unsigned Index = 0;
283 for (auto *BB : CatchPads) {
284 auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI());
285 // In case of a single catch (...), we don't need to emit a personalify
286 // function call
287 if (CPI->arg_size() == 1 &&
288 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
289 prepareEHPad(BB, false);
290 else
291 prepareEHPad(BB, true, Index++);
292 }
293
294 // Cleanup pads don't need a personality function call.
295 for (auto *BB : CleanupPads)
296 prepareEHPad(BB, false);
297
298 return true;
299}
300
301// Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
302// ignored.
303void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
304 unsigned Index) {
305 assert(BB->isEHPad() && "BB is not an EHPad!");
306 IRBuilder<> IRB(BB->getContext());
307 IRB.SetInsertPoint(BB, BB->getFirstInsertionPt());
308
309 auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHI());
310 Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr;
311 for (auto &U : FPI->uses()) {
312 if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
313 if (CI->getCalledOperand() == GetExnF)
314 GetExnCI = CI;
315 if (CI->getCalledOperand() == GetSelectorF)
316 GetSelectorCI = CI;
317 }
318 }
319
320 // Cleanup pads do not have any of wasm.get.exception() or
321 // wasm.get.ehselector() calls. We need to do nothing.
322 if (!GetExnCI) {
323 assert(!GetSelectorCI &&
324 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
325 return;
326 }
327
328 // Replace wasm.get.exception intrinsic with wasm.catch intrinsic, which will
329 // be lowered to wasm 'catch' instruction. We do this mainly because
330 // instruction selection cannot handle wasm.get.exception intrinsic's token
331 // argument.
332 Instruction *CatchCI =
333 IRB.CreateCall(CatchF, {IRB.getInt32(WebAssembly::CPP_EXCEPTION)}, "exn");
334 GetExnCI->replaceAllUsesWith(CatchCI);
335 GetExnCI->eraseFromParent();
336
337 // In case it is a catchpad with single catch (...) or a cleanuppad, we don't
338 // need to call personality function because we don't need a selector.
339 if (!NeedPersonality) {
340 if (GetSelectorCI) {
341 assert(GetSelectorCI->use_empty() &&
342 "wasm.get.ehselector() still has uses!");
343 GetSelectorCI->eraseFromParent();
344 }
345 return;
346 }
347 IRB.SetInsertPoint(CatchCI->getNextNode());
348
349 // This is to create a map of <landingpad EH label, landingpad index> in
350 // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables.
351 // Pseudocode: wasm.landingpad.index(Index);
352 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)});
353
354 // Pseudocode: __wasm_lpad_context.lpad_index = index;
355 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
356
357 auto *CPI = cast<CatchPadInst>(FPI);
358 // TODO Sometimes storing the LSDA address every time is not necessary, in
359 // case it is already set in a dominating EH pad and there is no function call
360 // between from that EH pad to here. Consider optimizing those cases.
361 // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda();
362 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
363
364 // Pseudocode: _Unwind_CallPersonality(exn);
365 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI,
366 OperandBundleDef("funclet", CPI));
367 PersCI->setDoesNotThrow();
368
369 // Pseudocode: int selector = __wasm_lpad_context.selector;
370 Instruction *Selector =
371 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField, "selector");
372
373 // Replace the return value from wasm.get.ehselector() with the selector value
374 // loaded from __wasm_lpad_context.selector.
375 assert(GetSelectorCI && "wasm.get.ehselector() call does not exist");
376 GetSelectorCI->replaceAllUsesWith(Selector);
377 GetSelectorCI->eraseFromParent();
378}
379
381 // If an exception is not caught by a catchpad (i.e., it is a foreign
382 // exception), it will unwind to its parent catchswitch's unwind destination.
383 // We don't record an unwind destination for cleanuppads because every
384 // exception should be caught by it.
385 for (const auto &BB : *F) {
386 if (!BB.isEHPad())
387 continue;
388 const Instruction *Pad = BB.getFirstNonPHI();
389
390 if (const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
391 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
392 if (!UnwindBB)
393 continue;
394 const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
395 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
396 // Currently there should be only one handler per a catchswitch.
397 EHInfo.setUnwindDest(&BB, *CatchSwitch->handlers().begin());
398 else // cleanuppad
399 EHInfo.setUnwindDest(&BB, UnwindBB);
400 }
401 }
402}
Prepare DWARF exceptions
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
#define F(x, y, z)
Definition: MD5.cpp:55
LLVMContext & Context
#define P(N)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void eraseDeadBBsAndChildren(const Container &BBs)
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:429
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:398
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:349
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:164
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:157
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition: BasicBlock.h:656
void setDoesNotThrow()
Definition: InstrTypes.h:2240
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...
Definition: DerivedTypes.h:168
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2644
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:65
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:119
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
static 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:109
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:112
bool empty() const
Definition: SmallVector.h:94
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
static 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:373
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:316
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1459
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:102
auto successors(const MachineBasicBlock *BB)
FunctionPass * createWasmEHPass()
createWasmEHPass - This pass adapts exception handling code to use WebAssembly's exception handling s...
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:99
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,...
OperandBundleDefT< Value * > OperandBundleDef
Definition: AutoUpgrade.h:33
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo)
bool pred_empty(const BasicBlock *BB)
Definition: CFG.h:118
void setUnwindDest(const BasicBlock *BB, const BasicBlock *Dest)