llvm.org GIT mirror llvm / a6e37da
[WebAssembly] Add Wasm exception handling prepare pass Summary: This adds a pass that transforms a program to be prepared for Wasm exception handling. This is using Windows EH instructions and based on the previous Wasm EH proposal. (https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md) Reviewers: dschuff, majnemer Subscribers: jfb, mgorny, sbc100, jgravelle-google, JDevlieghere, sunfish, llvm-commits Differential Revision: https://reviews.llvm.org/D43746 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333696 91177308-0d34-0410-b5e6-96231b3b80d8 Heejin Ahn 1 year, 10 months ago
10 changed file(s) with 698 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
328328
329329 /// createWinEHPass - Prepares personality functions used by MSVC on Windows,
330330 /// in addition to the Itanium LSDA based personalities.
331 FunctionPass *createWinEHPass();
331 FunctionPass *createWinEHPass(bool DemoteCatchSwitchPHIOnly = false);
332332
333333 /// createSjLjEHPreparePass - This pass adapts exception handling code to use
334334 /// the GCC-style builtin setjmp/longjmp (sjlj) to handling EH control flow.
335335 ///
336336 FunctionPass *createSjLjEHPreparePass();
337
338 /// createWasmEHPass - This pass adapts exception handling code to use
339 /// WebAssembly's exception handling scheme.
340 FunctionPass *createWasmEHPass();
337341
338342 /// LocalStackSlotAllocation - This pass assigns local frame indices to stack
339343 /// slots relative to one another and allocates base registers to access them
4141
4242 // Since wasm does not use landingpad instructions, these instructions return
4343 // exception pointer and selector values until we lower them in WasmEHPrepare.
44 def int_wasm_get_exception : Intrinsic<[llvm_ptr_ty], [], [IntrHasSideEffects]>;
45 def int_wasm_get_ehselector : Intrinsic<[llvm_i32_ty], [],
44 def int_wasm_get_exception : Intrinsic<[llvm_ptr_ty], [llvm_token_ty],
45 [IntrHasSideEffects]>;
46 def int_wasm_get_ehselector : Intrinsic<[llvm_i32_ty], [llvm_token_ty],
4647 [IntrHasSideEffects]>;
48
49 // wasm.catch returns the pointer to the exception object caught by wasm 'catch'
50 // instruction.
51 def int_wasm_catch : Intrinsic<[llvm_ptr_ty], [llvm_i32_ty],
52 [IntrHasSideEffects]>;
53
54 // WebAssembly EH must maintain the landingpads in the order assigned to them
55 // by WasmEHPrepare pass to generate landingpad table in EHStreamer. This is
56 // used in order to give them the indices in WasmEHPrepare.
57 def int_wasm_landingpad_index: Intrinsic<[], [llvm_i32_ty], [IntrNoMem]>;
58
59 // Returns LSDA address of the current function.
60 def int_wasm_lsda : Intrinsic<[llvm_ptr_ty], [], [IntrNoMem]>;
4761 }
388388 void initializeVerifierLegacyPassPass(PassRegistry&);
389389 void initializeVirtRegMapPass(PassRegistry&);
390390 void initializeVirtRegRewriterPass(PassRegistry&);
391 void initializeWasmEHPreparePass(PassRegistry&);
391392 void initializeWholeProgramDevirtPass(PassRegistry&);
392393 void initializeWinEHPreparePass(PassRegistry&);
393394 void initializeWriteBitcodePassPass(PassRegistry&);
157157 UnreachableBlockElim.cpp
158158 ValueTypes.cpp
159159 VirtRegMap.cpp
160 WasmEHPrepare.cpp
160161 WinEHPrepare.cpp
161162 XRayInstrumentation.cpp
162163
101101 initializeUnreachableMachineBlockElimPass(Registry);
102102 initializeVirtRegMapPass(Registry);
103103 initializeVirtRegRewriterPass(Registry);
104 initializeWasmEHPreparePass(Registry);
104105 initializeWinEHPreparePass(Registry);
105106 initializeXRayInstrumentationPass(Registry);
106107 initializeMIRCanonicalizerPass(Registry);
655655 addPass(createDwarfEHPass());
656656 break;
657657 case ExceptionHandling::Wasm:
658 // TODO to prevent warning
658 // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
659 // on catchpads and cleanuppads because it does not outline them into
660 // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
661 // should remove PHIs there.
662 addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/false));
663 addPass(createWasmEHPass());
659664 break;
660665 case ExceptionHandling::None:
661666 addPass(createLowerInvokePass());
0 //===-- WasmEHPrepare - Prepare excepton handling for WebAssembly --------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This transformation is designed for use by code generators which use
10 // WebAssembly exception handling scheme.
11 //
12 // WebAssembly exception handling uses Windows exception IR for the middle level
13 // representation. This pass does the following transformation for every
14 // catchpad block:
15 // (In C-style pseudocode)
16 //
17 // - Before:
18 // catchpad ...
19 // exn = wasm.get.exception();
20 // selector = wasm.get.selector();
21 // ...
22 //
23 // - After:
24 // catchpad ...
25 // exn = wasm.catch(0); // 0 is a tag for C++
26 // wasm.landingpad.index(index);
27 // // Only add below in case it's not a single catch (...)
28 // __wasm_lpad_context.lpad_index = index;
29 // __wasm_lpad_context.lsda = wasm.lsda();
30 // _Unwind_CallPersonality(exn);
31 // int selector = __wasm.landingpad_context.selector;
32 // ...
33 //
34 // Also, does the following for a cleanuppad block with a call to
35 // __clang_call_terminate():
36 // - Before:
37 // cleanuppad ...
38 // exn = wasm.get.exception();
39 // __clang_call_terminate(exn);
40 //
41 // - After:
42 // cleanuppad ...
43 // exn = wasm.catch(0); // 0 is a tag for C++
44 // __clang_call_terminate(exn);
45 //
46 //
47 // * Background: WebAssembly EH instructions
48 // WebAssembly's try and catch instructions are structured as follows:
49 // try
50 // instruction*
51 // catch (C++ tag)
52 // instruction*
53 // ...
54 // catch_all
55 // instruction*
56 // try_end
57 //
58 // A catch instruction in WebAssembly does not correspond to a C++ catch clause.
59 // In WebAssembly, there is a single catch instruction for all C++ exceptions.
60 // There can be more catch instructions for exceptions in other languages, but
61 // they are not generated for now. catch_all catches all exceptions including
62 // foreign exceptions (e.g. JavaScript). We turn catchpads into catch (C++ tag)
63 // and cleanuppads into catch_all, with one exception: cleanuppad with a call to
64 // __clang_call_terminate should be both in catch (C++ tag) and catch_all.
65 //
66 //
67 // * Background: Direct personality function call
68 // In WebAssembly EH, the VM is responsible for unwinding the stack once an
69 // exception is thrown. After the stack is unwound, the control flow is
70 // transfered to WebAssembly 'catch' instruction, which returns a caught
71 // exception object.
72 //
73 // Unwinding the stack is not done by libunwind but the VM, so the personality
74 // function in libcxxabi cannot be called from libunwind during the unwinding
75 // process. So after a catch instruction, we insert a call to a wrapper function
76 // in libunwind that in turn calls the real personality function.
77 //
78 // In Itanium EH, if the personality function decides there is no matching catch
79 // clause in a call frame and no cleanup action to perform, the unwinder doesn't
80 // stop there and continues unwinding. But in Wasm EH, the unwinder stops at
81 // every call frame with a catch intruction, after which the personality
82 // function is called from the compiler-generated user code here.
83 //
84 // In libunwind, we have this struct that serves as a communincation channel
85 // between the compiler-generated user code and the personality function in
86 // libcxxabi.
87 //
88 // struct _Unwind_LandingPadContext {
89 // uintptr_t lpad_index;
90 // uintptr_t lsda;
91 // uintptr_t selector;
92 // };
93 // struct _Unwind_LandingPadContext __wasm_lpad_context = ...;
94 //
95 // And this wrapper in libunwind calls the personality function.
96 //
97 // _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
98 // struct _Unwind_Exception *exception_obj =
99 // (struct _Unwind_Exception *)exception_ptr;
100 // _Unwind_Reason_Code ret = __gxx_personality_v0(
101 // 1, _UA_CLEANUP_PHASE, exception_obj->exception_class, exception_obj,
102 // (struct _Unwind_Context *)__wasm_lpad_context);
103 // return ret;
104 // }
105 //
106 // We pass a landing pad index, and the address of LSDA for the current function
107 // to the wrapper function _Unwind_CallPersonality in libunwind, and we retrieve
108 // the selector after it returns.
109 //
110 //===----------------------------------------------------------------------===//
111
112 #include "llvm/ADT/SetVector.h"
113 #include "llvm/ADT/Statistic.h"
114 #include "llvm/ADT/Triple.h"
115 #include "llvm/CodeGen/Passes.h"
116 #include "llvm/CodeGen/TargetLowering.h"
117 #include "llvm/CodeGen/TargetSubtargetInfo.h"
118 #include "llvm/IR/Dominators.h"
119 #include "llvm/IR/IRBuilder.h"
120 #include "llvm/IR/Intrinsics.h"
121 #include "llvm/Pass.h"
122 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
123
124 using namespace llvm;
125
126 #define DEBUG_TYPE "wasmehprepare"
127
128 namespace {
129 class WasmEHPrepare : public FunctionPass {
130 Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
131 GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context
132
133 // Field addresses of struct _Unwind_LandingPadContext
134 Value *LPadIndexField = nullptr; // lpad_index field
135 Value *LSDAField = nullptr; // lsda field
136 Value *SelectorField = nullptr; // selector
137
138 Function *CatchF = nullptr; // wasm.catch.extract() intrinsic
139 Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic
140 Function *LSDAF = nullptr; // wasm.lsda() intrinsic
141 Function *GetExnF = nullptr; // wasm.get.exception() intrinsic
142 Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
143 Function *CallPersonalityF = nullptr; // _Unwind_CallPersonality() wrapper
144 Function *ClangCallTermF = nullptr; // __clang_call_terminate() function
145
146 void prepareEHPad(BasicBlock *BB, unsigned Index);
147 void prepareTerminateCleanupPad(BasicBlock *BB);
148
149 public:
150 static char ID; // Pass identification, replacement for typeid
151
152 WasmEHPrepare() : FunctionPass(ID) {}
153
154 bool doInitialization(Module &M) override;
155 bool runOnFunction(Function &F) override;
156
157 StringRef getPassName() const override {
158 return "WebAssembly Exception handling preparation";
159 }
160 };
161 } // end anonymous namespace
162
163 char WasmEHPrepare::ID = 0;
164 INITIALIZE_PASS(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions",
165 false, false);
166
167 FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
168
169 bool WasmEHPrepare::doInitialization(Module &M) {
170 IRBuilder<> IRB(M.getContext());
171 LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
172 IRB.getInt8PtrTy(), // lsda
173 IRB.getInt32Ty() // selector
174 );
175 return false;
176 }
177
178 bool WasmEHPrepare::runOnFunction(Function &F) {
179 SmallVector CatchPads;
180 SmallVector CleanupPads;
181 for (BasicBlock &BB : F) {
182 if (!BB.isEHPad())
183 continue;
184 auto *Pad = BB.getFirstNonPHI();
185 if (isa(Pad))
186 CatchPads.push_back(&BB);
187 else if (isa(Pad))
188 CleanupPads.push_back(&BB);
189 }
190
191 if (CatchPads.empty() && CleanupPads.empty())
192 return false;
193 assert(F.hasPersonalityFn() && "Personality function not found");
194
195 Module &M = *F.getParent();
196 IRBuilder<> IRB(F.getContext());
197
198 // __wasm_lpad_context global variable
199 LPadContextGV = cast(
200 M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy));
201 LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
202 "lpad_index_gep");
203 LSDAField =
204 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1, "lsda_gep");
205 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
206 "selector_gep");
207
208 // wasm.catch() intinsic, which will be lowered to wasm 'catch' instruction.
209 CatchF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_catch);
210 // wasm.landingpad.index() intrinsic, which is to specify landingpad index
211 LPadIndexF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_landingpad_index);
212 // wasm.lsda() intrinsic. Returns the address of LSDA table for the current
213 // function.
214 LSDAF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_lsda);
215 // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these
216 // are generated in clang.
217 GetExnF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_exception);
218 GetSelectorF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_ehselector);
219
220 // _Unwind_CallPersonality() wrapper function, which calls the personality
221 CallPersonalityF = cast(M.getOrInsertFunction(
222 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()));
223 CallPersonalityF->setDoesNotThrow();
224
225 // __clang_call_terminate() function, which is inserted by clang in case a
226 // cleanup throws
227 ClangCallTermF = M.getFunction("__clang_call_terminate");
228
229 unsigned Index = 0;
230 for (auto *BB : CatchPads) {
231 auto *CPI = cast(BB->getFirstNonPHI());
232 // In case of a single catch (...), we don't need to emit LSDA
233 if (CPI->getNumArgOperands() == 1 &&
234 cast(CPI->getArgOperand(0))->isNullValue())
235 prepareEHPad(BB, -1);
236 else
237 prepareEHPad(BB, Index++);
238 }
239
240 if (!ClangCallTermF)
241 return !CatchPads.empty();
242
243 // Cleanuppads will turn into catch_all later, but cleanuppads with a call to
244 // __clang_call_terminate() is a special case. __clang_call_terminate() takes
245 // an exception object, so we have to duplicate call in both 'catch '
246 // and 'catch_all' clauses. Here we only insert a call to catch; the
247 // duplication will be done later. In catch_all, the exception object will be
248 // set to null.
249 for (auto *BB : CleanupPads)
250 for (auto &I : *BB)
251 if (auto *CI = dyn_cast(&I))
252 if (CI->getCalledValue() == ClangCallTermF)
253 prepareEHPad(BB, -1);
254
255 return true;
256 }
257
258 void WasmEHPrepare::prepareEHPad(BasicBlock *BB, unsigned Index) {
259 assert(BB->isEHPad() && "BB is not an EHPad!");
260 IRBuilder<> IRB(BB->getContext());
261
262 IRB.SetInsertPoint(&*BB->getFirstInsertionPt());
263 // The argument to wasm.catch() is the tag for C++ exceptions, which we set to
264 // 0 for this module.
265 // Pseudocode: void *exn = wasm.catch(0);
266 Instruction *Exn = IRB.CreateCall(CatchF, IRB.getInt32(0), "exn");
267 // Replace the return value of wasm.get.exception() with the return value from
268 // wasm.catch().
269 auto *FPI = cast(BB->getFirstNonPHI());
270 Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr;
271 for (auto &U : FPI->uses()) {
272 if (auto *CI = dyn_cast(U.getUser())) {
273 if (CI->getCalledValue() == GetExnF)
274 GetExnCI = CI;
275 else if (CI->getCalledValue() == GetSelectorF)
276 GetSelectorCI = CI;
277 }
278 }
279
280 assert(GetExnCI && "wasm.get.exception() call does not exist");
281 GetExnCI->replaceAllUsesWith(Exn);
282 GetExnCI->eraseFromParent();
283
284 // In case it is a catchpad with single catch (...) or a cleanuppad, we don't
285 // need to call personality function because we don't need a selector.
286 if (FPI->getNumArgOperands() == 0 ||
287 (FPI->getNumArgOperands() == 1 &&
288 cast(FPI->getArgOperand(0))->isNullValue())) {
289 if (GetSelectorCI) {
290 assert(GetSelectorCI->use_empty() &&
291 "wasm.get.ehselector() still has uses!");
292 GetSelectorCI->eraseFromParent();
293 }
294 return;
295 }
296 IRB.SetInsertPoint(Exn->getNextNode());
297
298 // This is to create a map of in
299 // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables.
300 // Pseudocode: wasm.landingpad.index(Index);
301 IRB.CreateCall(LPadIndexF, IRB.getInt32(Index));
302
303 // Pseudocode: __wasm_lpad_context.lpad_index = index;
304 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
305
306 // Store LSDA address only if this catchpad belongs to a top-level
307 // catchswitch. If there is another catchpad that dominates this pad, we don't
308 // need to store LSDA address again, because they are the same throughout the
309 // function and have been already stored before.
310 // TODO Can we not store LSDA address in user function but make libcxxabi
311 // compute it?
312 auto *CPI = cast(FPI);
313 if (isa(CPI->getCatchSwitch()->getParentPad()))
314 // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda();
315 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
316
317 // Pseudocode: _Unwind_CallPersonality(exn);
318 CallInst *PersCI =
319 IRB.CreateCall(CallPersonalityF, Exn, OperandBundleDef("funclet", CPI));
320 PersCI->setDoesNotThrow();
321
322 // Pseudocode: int selector = __wasm.landingpad_context.selector;
323 Instruction *Selector = IRB.CreateLoad(SelectorField, "selector");
324
325 // Replace the return value from wasm.get.ehselector() with the selector value
326 // loaded from __wasm_lpad_context.selector.
327 assert(GetSelectorCI && "wasm.get.ehselector() call does not exist");
328 GetSelectorCI->replaceAllUsesWith(Selector);
329 GetSelectorCI->eraseFromParent();
330 }
4848 cl::desc("Do not remove implausible terminators or other similar cleanups"),
4949 cl::init(false));
5050
51 static cl::opt DemoteCatchSwitchPHIOnlyOpt(
52 "demote-catchswitch-only", cl::Hidden,
53 cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false));
54
5155 namespace {
5256
5357 class WinEHPrepare : public FunctionPass {
5458 public:
5559 static char ID; // Pass identification, replacement for typeid.
56 WinEHPrepare() : FunctionPass(ID) {}
60 WinEHPrepare(bool DemoteCatchSwitchPHIOnly = false)
61 : FunctionPass(ID), DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {}
5762
5863 bool runOnFunction(Function &Fn) override;
5964
7681 bool prepareExplicitEH(Function &F);
7782 void colorFunclets(Function &F);
7883
79 void demotePHIsOnFunclets(Function &F);
84 void demotePHIsOnFunclets(Function &F, bool DemoteCatchSwitchPHIOnly);
8085 void cloneCommonBlocks(Function &F);
8186 void removeImplausibleInstructions(Function &F);
8287 void cleanupPreparedFunclets(Function &F);
8388 void verifyPreparedFunclets(Function &F);
8489
90 bool DemoteCatchSwitchPHIOnly;
91
8592 // All fields are reset by runOnFunction.
8693 EHPersonality Personality = EHPersonality::Unknown;
8794
96103 INITIALIZE_PASS(WinEHPrepare, DEBUG_TYPE, "Prepare Windows exceptions",
97104 false, false)
98105
99 FunctionPass *llvm::createWinEHPass() { return new WinEHPrepare(); }
106 FunctionPass *llvm::createWinEHPass(bool DemoteCatchSwitchPHIOnly) {
107 return new WinEHPrepare(DemoteCatchSwitchPHIOnly);
108 }
100109
101110 bool WinEHPrepare::runOnFunction(Function &Fn) {
102111 if (!Fn.hasPersonalityFn())
677686 }
678687 }
679688
680 void WinEHPrepare::demotePHIsOnFunclets(Function &F) {
689 void WinEHPrepare::demotePHIsOnFunclets(Function &F,
690 bool DemoteCatchSwitchPHIOnly) {
681691 // Strip PHI nodes off of EH pads.
682692 SmallVector PHINodes;
683693 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
684694 BasicBlock *BB = &*FI++;
685695 if (!BB->isEHPad())
686696 continue;
697 if (DemoteCatchSwitchPHIOnly && !isa(BB->getFirstNonPHI()))
698 continue;
699
687700 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
688701 Instruction *I = &*BI++;
689702 auto *PN = dyn_cast(I);
10311044 cloneCommonBlocks(F);
10321045
10331046 if (!DisableDemotion)
1034 demotePHIsOnFunclets(F);
1047 demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly ||
1048 DemoteCatchSwitchPHIOnlyOpt);
10351049
10361050 if (!DisableCleanups) {
10371051 LLVM_DEBUG(verifyFunction(F));
0 ; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s
1
2 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
3 target triple = "wasm32-unknown-unknown"
4
5 ; CHECK: @__wasm_lpad_context = external global { i32, i8*, i32 }
6
7 @_ZTIi = external constant i8*
8 %struct.Cleanup = type { i8 }
9
10 ; A single 'catch (int)' clause.
11 ; A wasm.catch() call, wasm.lsda() call, and personality call to generate a
12 ; selector should all be genereated after the catchpad.
13 define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
14 ; CHECK-LABEL: @test0()
15 entry:
16 invoke void @foo()
17 to label %try.cont unwind label %catch.dispatch
18
19 catch.dispatch: ; preds = %entry
20 %0 = catchswitch within none [label %catch.start] unwind to caller
21
22 catch.start: ; preds = %catch.dispatch
23 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
24 %2 = call i8* @llvm.wasm.get.exception(token %1)
25 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
26 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
27 %matches = icmp eq i32 %3, %4
28 br i1 %matches, label %catch, label %rethrow
29 ; CHECK: catch.start:
30 ; CHECK-NEXT: %[[CATCHPAD:.*]] = catchpad
31 ; CHECK-NEXT: %[[EXN:.*]] = call i8* @llvm.wasm.catch(i32 0)
32 ; CHECK-NEXT: call void @llvm.wasm.landingpad.index(i32 0)
33 ; CHECK-NEXT: store i32 0, i32* getelementptr inbounds ({ i32, i8*, i32 }, { i32, i8*, i32 }* @__wasm_lpad_context, i32 0, i32 0)
34 ; CHECK-NEXT: %[[LSDA:.*]] = call i8* @llvm.wasm.lsda()
35 ; CHECK-NEXT: store i8* %[[LSDA]], i8** getelementptr inbounds ({ i32, i8*, i32 }, { i32, i8*, i32 }* @__wasm_lpad_context, i32 0, i32 1)
36 ; CHECK-NEXT: call i32 @_Unwind_CallPersonality(i8* %[[EXN]]) {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
37 ; CHECK-NEXT: %[[SELECTOR:.*]] = load i32, i32* getelementptr inbounds ({ i32, i8*, i32 }, { i32, i8*, i32 }* @__wasm_lpad_context, i32 0, i32 2)
38 ; CHECK: icmp eq i32 %[[SELECTOR]]
39
40 catch: ; preds = %catch.start
41 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
42 call void @__cxa_end_catch() [ "funclet"(token %1) ]
43 catchret from %1 to label %try.cont
44 ; CHECK: catch:
45 ; CHECK-NEXT: call i8* @__cxa_begin_catch(i8* %[[EXN]])
46
47 rethrow: ; preds = %catch.start
48 call void @__cxa_rethrow() [ "funclet"(token %1) ]
49 unreachable
50
51 try.cont: ; preds = %entry, %catch
52 ret void
53 }
54
55 ; Two try-catches, one of them is with a single 'catch (...)' clause.
56 ; For the catchpad with a single 'catch (...)', only a wasm.catch() call should
57 ; be generated after the catchpad; wasm.landingpad.index() and personality call
58 ; should NOT be generated. For the other catchpad, the argument of
59 ; wasm.landingpad.index() should be not 1 but 0.
60 define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
61 ; CHECK-LABEL: @test1()
62 entry:
63 invoke void @foo()
64 to label %try.cont unwind label %catch.dispatch
65
66 catch.dispatch: ; preds = %entry
67 %0 = catchswitch within none [label %catch.start] unwind to caller
68
69 catch.start: ; preds = %catch.dispatch
70 %1 = catchpad within %0 [i8* null]
71 %2 = call i8* @llvm.wasm.get.exception(token %1)
72 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
73 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
74 call void @__cxa_end_catch() [ "funclet"(token %1) ]
75 catchret from %1 to label %try.cont
76 ; CHECK: catch.start:
77 ; CHECK-NEXT: catchpad within %0 [i8* null]
78 ; CHECK-NEXT: call i8* @llvm.wasm.catch(i32 0)
79 ; CHECK-NOT: call void @llvm.wasm.landingpad.index
80 ; CHECK-NOT: store {{.*}} @__wasm_lpad_context
81 ; CHECK-NOT: call i8* @llvm.wasm.lsda()
82 ; CHECK-NOT: call i32 @_Unwind_CallPersonality
83 ; CHECK-NOT: load {{.*}} @__wasm_lpad_context
84
85 try.cont: ; preds = %entry, %catch.start
86 invoke void @foo()
87 to label %try.cont7 unwind label %catch.dispatch2
88
89 catch.dispatch2: ; preds = %try.cont
90 %5 = catchswitch within none [label %catch.start3] unwind to caller
91
92 catch.start3: ; preds = %catch.dispatch2
93 %6 = catchpad within %5 [i8* bitcast (i8** @_ZTIi to i8*)]
94 %7 = call i8* @llvm.wasm.get.exception(token %6)
95 %8 = call i32 @llvm.wasm.get.ehselector(token %6)
96 %9 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
97 %matches = icmp eq i32 %8, %9
98 br i1 %matches, label %catch4, label %rethrow
99 ; CHECK: catch.start3:
100 ; CHECK: call void @llvm.wasm.landingpad.index(i32 0)
101
102 catch4: ; preds = %catch.start3
103 %10 = call i8* @__cxa_begin_catch(i8* %7) [ "funclet"(token %6) ]
104 call void @__cxa_end_catch() [ "funclet"(token %6) ]
105 catchret from %6 to label %try.cont7
106
107 rethrow: ; preds = %catch.start3
108 call void @__cxa_rethrow() [ "funclet"(token %6) ]
109 unreachable
110
111 try.cont7: ; preds = %try.cont, %catch4
112 ret void
113 }
114
115 ; A nested try-catch within a catch. Within the nested catchpad, wasm.lsda()
116 ; call should NOT be generated.
117 define void @test2() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
118 ; CHECK-LABEL: @test2()
119 entry:
120 invoke void @foo()
121 to label %try.cont9 unwind label %catch.dispatch
122
123 catch.dispatch: ; preds = %entry
124 %0 = catchswitch within none [label %catch.start] unwind to caller
125
126 catch.start: ; preds = %catch.dispatch
127 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
128 %2 = call i8* @llvm.wasm.get.exception(token %1)
129 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
130 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
131 %matches = icmp eq i32 %3, %4
132 br i1 %matches, label %catch, label %rethrow
133 ; CHECK: catch.start:
134 ; CHECK: call i8* @llvm.wasm.lsda()
135
136 catch: ; preds = %catch.start
137 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
138 invoke void @foo() [ "funclet"(token %1) ]
139 to label %try.cont unwind label %catch.dispatch2
140
141 catch.dispatch2: ; preds = %catch
142 %6 = catchswitch within %1 [label %catch.start3] unwind label %ehcleanup
143
144 catch.start3: ; preds = %catch.dispatch2
145 %7 = catchpad within %6 [i8* bitcast (i8** @_ZTIi to i8*)]
146 %8 = call i8* @llvm.wasm.get.exception(token %7)
147 %9 = call i32 @llvm.wasm.get.ehselector(token %7)
148 %10 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
149 %matches4 = icmp eq i32 %9, %10
150 br i1 %matches4, label %catch6, label %rethrow5
151 ; CHECK: catch.start3:
152 ; CHECK-NOT: call i8* @llvm.wasm.lsda()
153
154 catch6: ; preds = %catch.start3
155 %11 = call i8* @__cxa_begin_catch(i8* %8) [ "funclet"(token %7) ]
156 call void @__cxa_end_catch() [ "funclet"(token %7) ]
157 catchret from %7 to label %try.cont
158
159 rethrow5: ; preds = %catch.start3
160 invoke void @__cxa_rethrow() [ "funclet"(token %7) ]
161 to label %unreachable unwind label %ehcleanup
162
163 try.cont: ; preds = %catch, %catch6
164 call void @__cxa_end_catch() [ "funclet"(token %1) ]
165 catchret from %1 to label %try.cont9
166
167 rethrow: ; preds = %catch.start
168 call void @__cxa_rethrow() [ "funclet"(token %1) ]
169 unreachable
170
171 try.cont9: ; preds = %entry, %try.cont
172 ret void
173
174 ehcleanup: ; preds = %rethrow5, %catch.dispatch2
175 %12 = cleanuppad within %1 []
176 call void @__cxa_end_catch() [ "funclet"(token %12) ]
177 cleanupret from %12 unwind to caller
178 ; CHECK: ehcleanup:
179 ; CHECK-NEXT: cleanuppad
180 ; CHECK-NOT: call i8* @llvm.wasm.catch(i32 0)
181 ; CHECK-NOT: call void @llvm.wasm.landingpad.index
182 ; CHECK-NOT: store {{.*}} @__wasm_lpad_context
183 ; CHECK-NOT: call i8* @llvm.wasm.lsda()
184 ; CHECK-NOT: call i32 @_Unwind_CallPersonality
185 ; CHECK-NOT: load {{.*}} @__wasm_lpad_context
186
187 unreachable: ; preds = %rethrow5
188 unreachable
189 }
190
191 ; A cleanuppad with a call to __clang_call_terminate().
192 ; A call to wasm.catch() should be generated after the cleanuppad.
193 define hidden void @test3() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
194 ; CHECK-LABEL: @test3
195 entry:
196 invoke void @foo()
197 to label %try.cont unwind label %catch.dispatch
198
199 catch.dispatch: ; preds = %entry
200 %0 = catchswitch within none [label %catch.start] unwind to caller
201
202 catch.start: ; preds = %catch.dispatch
203 %1 = catchpad within %0 [i8* null]
204 %2 = call i8* @llvm.wasm.get.exception(token %1)
205 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
206 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
207 invoke void @foo() [ "funclet"(token %1) ]
208 to label %invoke.cont1 unwind label %ehcleanup
209
210 invoke.cont1: ; preds = %catch.start
211 call void @__cxa_end_catch() [ "funclet"(token %1) ]
212 catchret from %1 to label %try.cont
213
214 try.cont: ; preds = %entry, %invoke.cont1
215 ret void
216
217 ehcleanup: ; preds = %catch.start
218 %5 = cleanuppad within %1 []
219 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
220 to label %invoke.cont2 unwind label %terminate
221
222 invoke.cont2: ; preds = %ehcleanup
223 cleanupret from %5 unwind to caller
224
225 terminate: ; preds = %ehcleanup
226 %6 = cleanuppad within %5 []
227 %7 = call i8* @llvm.wasm.get.exception(token %6)
228 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
229 unreachable
230 ; CHECK: terminate:
231 ; CHECK-NEXT: cleanuppad
232 ; CHECK-NEXT: %[[EXN:.*]] = call i8* @llvm.wasm.catch(i32 0)
233 ; CHECK-NEXT: call void @__clang_call_terminate(i8* %[[EXN]])
234 }
235
236 ; PHI demotion test. Only the phi before catchswitch should be demoted; the phi
237 ; before cleanuppad should NOT.
238 define void @test5() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
239 ; CHECK-LABEL: @test5
240 entry:
241 %c = alloca %struct.Cleanup, align 1
242 invoke void @foo()
243 to label %invoke.cont unwind label %ehcleanup
244
245 invoke.cont: ; preds = %entry
246 invoke void @foo()
247 to label %invoke.cont1 unwind label %ehcleanup
248
249 invoke.cont1: ; preds = %invoke.cont
250 %call = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
251 br label %try.cont
252
253 ehcleanup: ; preds = %invoke.cont, %entry
254 %num.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
255 %0 = cleanuppad within none []
256 %call2 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %0) ]
257 cleanupret from %0 unwind label %catch.dispatch
258 ; CHECK: ehcleanup:
259 ; CHECK-NEXT: = phi
260
261 catch.dispatch: ; preds = %ehcleanup
262 %1 = catchswitch within none [label %catch.start] unwind to caller
263
264 catch.start: ; preds = %catch.dispatch
265 %2 = catchpad within %1 [i8* null]
266 %3 = call i8* @llvm.wasm.get.exception(token %2)
267 %4 = call i32 @llvm.wasm.get.ehselector(token %2)
268 %5 = call i8* @__cxa_begin_catch(i8* %3) [ "funclet"(token %2) ]
269 call void @func(i32 %num.0) [ "funclet"(token %2) ]
270 call void @__cxa_end_catch() [ "funclet"(token %2) ]
271 catchret from %2 to label %try.cont
272
273 try.cont: ; preds = %catch.start, %invoke.cont1
274 invoke void @foo()
275 to label %invoke.cont3 unwind label %catch.dispatch5
276
277 invoke.cont3: ; preds = %try.cont
278 invoke void @foo()
279 to label %try.cont10 unwind label %catch.dispatch5
280
281 catch.dispatch5: ; preds = %invoke.cont3, %try.cont
282 %num.1 = phi i32 [ 2, %invoke.cont3 ], [ 1, %try.cont ]
283 %6 = catchswitch within none [label %catch.start6] unwind to caller
284 ; CHECK: catch.dispatch5:
285 ; CHECK-NOT: = phi
286
287 catch.start6: ; preds = %catch.dispatch5
288 %7 = catchpad within %6 [i8* null]
289 %8 = call i8* @llvm.wasm.get.exception(token %7)
290 %9 = call i32 @llvm.wasm.get.ehselector(token %7)
291 %10 = call i8* @__cxa_begin_catch(i8* %8) [ "funclet"(token %7) ]
292 call void @func(i32 %num.1) [ "funclet"(token %7) ]
293 call void @__cxa_end_catch() [ "funclet"(token %7) ]
294 catchret from %7 to label %try.cont10
295
296 try.cont10: ; preds = %invoke.cont3, %catch.start6
297 ret void
298 }
299
300 declare void @foo()
301 declare void @func(i32)
302 declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
303 declare i32 @__gxx_wasm_personality_v0(...)
304 declare i8* @llvm.wasm.get.exception(token)
305 declare i32 @llvm.wasm.get.ehselector(token)
306 declare i32 @llvm.eh.typeid.for(i8*)
307 declare i8* @__cxa_begin_catch(i8*)
308 declare void @__cxa_end_catch()
309 declare void @__cxa_rethrow()
310 declare void @__clang_call_terminate(i8*)
311
312 ; CHECK-DAG: declare i8* @llvm.wasm.catch(i32)
313 ; CHECK-DAG: declare void @llvm.wasm.landingpad.index(i32)
314 ; CHECK-DAG: declare i8* @llvm.wasm.lsda()
315 ; CHECK-DAG: declare i32 @_Unwind_CallPersonality(i8*)
316
446446 initializePostInlineEntryExitInstrumenterPass(Registry);
447447 initializeUnreachableBlockElimLegacyPassPass(Registry);
448448 initializeExpandReductionsPass(Registry);
449 initializeWasmEHPreparePass(Registry);
449450 initializeWriteBitcodePassPass(Registry);
450451
451452 #ifdef LINK_POLLY_INTO_TOOLS