Skip to content

Commit f41f67d

Browse files
committedAug 1, 2016
[WebAssembly] Add asm.js-style exception handling support
Summary: This patch includes asm.js-style exception handling support for WebAssembly. The WebAssembly MVP does not have any support for unwinding or non-local control flow. In order to support C++ exceptions, emscripten currently uses JavaScript exceptions along with some support code (written in JavaScript) that is bundled by emscripten with the generated code. This scheme lowers exception-related instructions for wasm such that wasm modules can be compatible with emscripten's existing scheme and share the support code. Patch by Heejin Ahn Differential Revision: https://reviews.llvm.org/D22958 llvm-svn: 277391
1 parent 4a7130a commit f41f67d

File tree

5 files changed

+620
-0
lines changed

5 files changed

+620
-0
lines changed
 

‎llvm/lib/Target/WebAssembly/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_llvm_target(WebAssemblyCodeGen
2020
WebAssemblyISelLowering.cpp
2121
WebAssemblyInstrInfo.cpp
2222
WebAssemblyLowerBrUnless.cpp
23+
WebAssemblyLowerEmscriptenExceptions.cpp
2324
WebAssemblyMachineFunctionInfo.cpp
2425
WebAssemblyMCInstLower.cpp
2526
WebAssemblyOptimizeLiveIntervals.cpp

‎llvm/lib/Target/WebAssembly/WebAssembly.h

+4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLY_H
1717
#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLY_H
1818

19+
#include "llvm/PassRegistry.h"
1920
#include "llvm/Support/CodeGen.h"
2021

2122
namespace llvm {
2223

2324
class WebAssemblyTargetMachine;
25+
class ModulePass;
2426
class FunctionPass;
2527

2628
// LLVM IR passes.
29+
ModulePass *createWebAssemblyLowerEmscriptenExceptions();
30+
void initializeWebAssemblyLowerEmscriptenExceptionsPass(PassRegistry &);
2731
FunctionPass *createWebAssemblyOptimizeReturned();
2832

2933
// ISel and immediate followup passes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,458 @@
1+
// WebAssemblyLowerEmscriptenExceptions.cpp - Lower exceptions for Emscripten //
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// \brief This file lowers exception-related instructions in order to use
12+
/// Emscripten's JavaScript try and catch mechanism to handle exceptions.
13+
///
14+
/// To handle exceptions, this scheme relies on JavaScript's try and catch
15+
/// syntax and relevant exception-related libraries implemented in JavaScript
16+
/// glue code that will be produced by Emscripten. This is similar to the
17+
/// current Emscripten asm.js exception handling in fastcomp.
18+
/// For fastcomp's EH scheme, see these files in fastcomp LLVM branch:
19+
/// (Location: https://github.com/kripken/emscripten-fastcomp)
20+
/// lib/Target/JSBackend/NaCl/LowerEmExceptionsPass.cpp
21+
/// lib/Target/JSBackend/JSBackend.cpp
22+
/// lib/Target/JSBackend/CallHandlers.h
23+
///
24+
/// This pass does following things:
25+
///
26+
/// 1) Create three global variables: __THREW__, threwValue, and tempRet0.
27+
/// tempRet0 will be set within ___cxa_find_matching_catch() function in
28+
/// JS library, and __THREW__ and threwValue will be set in invoke wrappers
29+
/// in JS glue code. For what invoke wrappers are, refer to 3).
30+
///
31+
/// 2) Create setThrew and setTempRet0 functions.
32+
/// The global variables created in 1) will exist in wasm address space,
33+
/// but their values should be set in JS code, so we provide these functions
34+
/// as interfaces to JS glue code. These functions are equivalent to the
35+
/// following JS functions, which actually exist in asm.js version of JS
36+
/// library.
37+
///
38+
/// function setThrew(threw, value) {
39+
/// if (__THREW__ == 0) {
40+
/// __THREW__ = threw;
41+
/// threwValue = value;
42+
/// }
43+
/// }
44+
///
45+
/// function setTempRet0(value) {
46+
/// tempRet0 = value;
47+
/// }
48+
///
49+
/// 3) Lower
50+
/// invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad
51+
/// into
52+
/// __THREW__ = 0;
53+
/// call @invoke_SIG(func, arg1, arg2)
54+
/// %__THREW__.val = __THREW__;
55+
/// __THREW__ = 0;
56+
/// br %__THREW__.val, label %lpad, label %invoke.cont
57+
/// SIG is a mangled string generated based on the LLVM IR-level function
58+
/// signature. After LLVM IR types are lowered to the target wasm types,
59+
/// the names for these wrappers will change based on wasm types as well,
60+
/// as in invoke_vi (function takes an int and returns void). The bodies of
61+
/// these wrappers will be generated in JS glue code, and inside those
62+
/// wrappers we use JS try-catch to generate actual exception effects. It
63+
/// also calls the original callee function. An example wrapper in JS code
64+
/// would look like this:
65+
/// function invoke_vi(index,a1) {
66+
/// try {
67+
/// Module["dynCall_vi"](index,a1); // This calls original callee
68+
/// } catch(e) {
69+
/// if (typeof e !== 'number' && e !== 'longjmp') throw e;
70+
/// asm["setThrew"](1, 0); // setThrew is called here
71+
/// }
72+
/// }
73+
/// If an exception is thrown, __THREW__ will be set to true in a wrapper,
74+
/// so we can jump to the right BB based on this value.
75+
///
76+
/// 4) Lower
77+
/// %val = landingpad catch c1 catch c2 catch c3 ...
78+
/// ... use %val ...
79+
/// into
80+
/// %fmc = call @___cxa_find_matching_catch_N(c1, c2, c3, ...)
81+
/// %val = {%fmc, tempRet0}
82+
/// ... use %val ...
83+
/// Here N is a number calculated based on the number of clauses.
84+
/// Global variable tempRet0 is set within ___cxa_find_matching_catch() in
85+
/// JS glue code.
86+
///
87+
/// 5) Lower
88+
/// resume {%a, %b}
89+
/// into
90+
/// call @___resumeException(%a)
91+
/// where ___resumeException() is a function in JS glue code.
92+
///
93+
/// TODO: Handle i64 types
94+
///
95+
///===----------------------------------------------------------------------===//
96+
97+
#include "WebAssembly.h"
98+
#include "llvm/ADT/IndexedMap.h"
99+
#include "llvm/IR/Constants.h"
100+
#include "llvm/IR/IRBuilder.h"
101+
#include "llvm/IR/Instructions.h"
102+
#include "llvm/IR/Module.h"
103+
#include "llvm/Pass.h"
104+
#include "llvm/Support/raw_ostream.h"
105+
106+
using namespace llvm;
107+
108+
#define DEBUG_TYPE "wasm-lower-em-exceptions"
109+
110+
namespace {
111+
class WebAssemblyLowerEmscriptenExceptions final : public ModulePass {
112+
const char *getPassName() const override {
113+
return "WebAssembly Lower Emscripten Exceptions";
114+
}
115+
116+
bool runOnFunction(Function &F);
117+
// Returns ___cxa_find_matching_catch_N function, where N = NumClauses + 2.
118+
// This is because a landingpad instruction contains two more arguments,
119+
// a personality function and a cleanup bit, and ___cxa_find_matching_catch_N
120+
// functions are named after the number of arguments in the original
121+
// landingpad instruction.
122+
Function *getFindMatchingCatch(Module &M, unsigned NumClauses);
123+
124+
Function *getInvokeWrapper(Module &M, InvokeInst *II);
125+
126+
GlobalVariable *ThrewGV; // __THREW__
127+
GlobalVariable *ThrewValueGV; // threwValue
128+
GlobalVariable *TempRet0GV; // tempRet0
129+
Function *ResumeF;
130+
// ___cxa_find_matching_catch_N functions.
131+
// Indexed by the number of clauses in an original landingpad instruction.
132+
DenseMap<int, Function *> FindMatchingCatches;
133+
// Map of <function signature string, invoke_ wrappers>
134+
StringMap<Function *> InvokeWrappers;
135+
136+
public:
137+
static char ID;
138+
139+
WebAssemblyLowerEmscriptenExceptions()
140+
: ModulePass(ID), ThrewGV(nullptr), ThrewValueGV(nullptr),
141+
TempRet0GV(nullptr) {}
142+
bool runOnModule(Module &M) override;
143+
};
144+
} // End anonymous namespace
145+
146+
char WebAssemblyLowerEmscriptenExceptions::ID = 0;
147+
INITIALIZE_PASS(WebAssemblyLowerEmscriptenExceptions, DEBUG_TYPE,
148+
"WebAssembly Lower Emscripten Exceptions", false, false)
149+
150+
ModulePass *llvm::createWebAssemblyLowerEmscriptenExceptions() {
151+
return new WebAssemblyLowerEmscriptenExceptions();
152+
}
153+
154+
static bool canThrow(const Value *V) {
155+
if (const auto *F = dyn_cast<const Function>(V)) {
156+
// Intrinsics cannot throw
157+
if (F->isIntrinsic())
158+
return false;
159+
StringRef Name = F->getName();
160+
// leave setjmp and longjmp (mostly) alone, we process them properly later
161+
if (Name == "setjmp" || Name == "longjmp")
162+
return false;
163+
return true;
164+
}
165+
return true; // not a function, so an indirect call - can throw, we can't tell
166+
}
167+
168+
// Returns an available name for a global value.
169+
// If the proposed name already exists in the module, adds '_' at the end of
170+
// the name until the name is available.
171+
static inline std::string createGlobalValueName(const Module &M,
172+
const std::string &Propose) {
173+
std::string Name = Propose;
174+
while (M.getNamedGlobal(Name))
175+
Name += "_";
176+
return Name;
177+
}
178+
179+
// Simple function name mangler.
180+
// This function simply takes LLVM's string representation of parameter types
181+
// concatenate them with '_'. There are non-alphanumeric characters but llc is
182+
// ok with it, and we need to postprocess these names after the lowering phase
183+
// anyway.
184+
static std::string getSignature(FunctionType *FTy) {
185+
std::string Sig;
186+
raw_string_ostream OS(Sig);
187+
OS << *FTy->getReturnType();
188+
for (Type *ParamTy : FTy->params())
189+
OS << "_" << *ParamTy;
190+
if (FTy->isVarArg())
191+
OS << "_...";
192+
Sig = OS.str();
193+
Sig.erase(std::remove_if(Sig.begin(), Sig.end(), isspace), Sig.end());
194+
return Sig;
195+
}
196+
197+
Function *WebAssemblyLowerEmscriptenExceptions::getFindMatchingCatch(
198+
Module &M, unsigned NumClauses) {
199+
if (FindMatchingCatches.count(NumClauses))
200+
return FindMatchingCatches[NumClauses];
201+
PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
202+
SmallVector<Type *, 16> Args(NumClauses, Int8PtrTy);
203+
FunctionType *FTy = FunctionType::get(Int8PtrTy, Args, false);
204+
Function *F = Function::Create(
205+
FTy, GlobalValue::ExternalLinkage,
206+
"___cxa_find_matching_catch_" + Twine(NumClauses + 2), &M);
207+
FindMatchingCatches[NumClauses] = F;
208+
return F;
209+
}
210+
211+
Function *
212+
WebAssemblyLowerEmscriptenExceptions::getInvokeWrapper(Module &M,
213+
InvokeInst *II) {
214+
SmallVector<Type *, 16> ArgTys;
215+
Value *Callee = II->getCalledValue();
216+
FunctionType *CalleeFTy;
217+
if (auto *F = dyn_cast<Function>(Callee))
218+
CalleeFTy = F->getFunctionType();
219+
else {
220+
auto *CalleeTy = dyn_cast<PointerType>(Callee->getType())->getElementType();
221+
CalleeFTy = dyn_cast<FunctionType>(CalleeTy);
222+
}
223+
224+
std::string Sig = getSignature(CalleeFTy);
225+
if (InvokeWrappers.find(Sig) != InvokeWrappers.end())
226+
return InvokeWrappers[Sig];
227+
228+
// Put the pointer to the callee as first argument
229+
ArgTys.push_back(PointerType::getUnqual(CalleeFTy));
230+
// Add argument types
231+
ArgTys.append(CalleeFTy->param_begin(), CalleeFTy->param_end());
232+
233+
FunctionType *FTy = FunctionType::get(CalleeFTy->getReturnType(), ArgTys,
234+
CalleeFTy->isVarArg());
235+
Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage,
236+
"__invoke_" + Sig, &M);
237+
InvokeWrappers[Sig] = F;
238+
return F;
239+
}
240+
241+
bool WebAssemblyLowerEmscriptenExceptions::runOnModule(Module &M) {
242+
IRBuilder<> Builder(M.getContext());
243+
IntegerType *Int1Ty = Builder.getInt1Ty();
244+
PointerType *Int8PtrTy = Builder.getInt8PtrTy();
245+
IntegerType *Int32Ty = Builder.getInt32Ty();
246+
247+
// Create global variables __THREW__, threwValue, and tempRet0
248+
ThrewGV = new GlobalVariable(M, Int1Ty, false, GlobalValue::ExternalLinkage,
249+
Builder.getFalse(),
250+
createGlobalValueName(M, "__THREW__"));
251+
ThrewValueGV = new GlobalVariable(
252+
M, Int32Ty, false, GlobalValue::ExternalLinkage, Builder.getInt32(0),
253+
createGlobalValueName(M, "threwValue"));
254+
TempRet0GV = new GlobalVariable(
255+
M, Int32Ty, false, GlobalValue::ExternalLinkage, Builder.getInt32(0),
256+
createGlobalValueName(M, "tempRet0"));
257+
258+
// Register ___resumeException function
259+
Type *VoidTy = Type::getVoidTy(M.getContext());
260+
FunctionType *ResumeFTy = FunctionType::get(VoidTy, Int8PtrTy, false);
261+
ResumeF = Function::Create(ResumeFTy, GlobalValue::ExternalLinkage,
262+
"___resumeException", &M);
263+
264+
bool Changed = false;
265+
for (Function &F : M) {
266+
if (F.isDeclaration())
267+
continue;
268+
Changed |= runOnFunction(F);
269+
}
270+
271+
if (!Changed)
272+
return false;
273+
274+
assert(!M.getNamedGlobal("setThrew") && "setThrew already exists");
275+
assert(!M.getNamedGlobal("setTempRet0") && "setTempRet0 already exists");
276+
277+
// Create setThrew function
278+
SmallVector<Type *, 2> Params = {Int1Ty, Int32Ty};
279+
FunctionType *FTy = FunctionType::get(VoidTy, Params, false);
280+
Function *F =
281+
Function::Create(FTy, GlobalValue::ExternalLinkage, "setThrew", &M);
282+
Argument *Arg1 = &*(F->arg_begin());
283+
Argument *Arg2 = &*(++F->arg_begin());
284+
Arg1->setName("threw");
285+
Arg2->setName("value");
286+
BasicBlock *EntryBB = BasicBlock::Create(M.getContext(), "entry", F);
287+
BasicBlock *ThenBB = BasicBlock::Create(M.getContext(), "if.then", F);
288+
BasicBlock *EndBB = BasicBlock::Create(M.getContext(), "if.end", F);
289+
290+
Builder.SetInsertPoint(EntryBB);
291+
Value *Threw = Builder.CreateLoad(ThrewGV, ThrewGV->getName() + ".val");
292+
Value *Cmp = Builder.CreateICmpEQ(Threw, Builder.getFalse(), "cmp");
293+
Builder.CreateCondBr(Cmp, ThenBB, EndBB);
294+
295+
Builder.SetInsertPoint(ThenBB);
296+
Builder.CreateStore(Arg1, ThrewGV);
297+
Builder.CreateStore(Arg2, ThrewValueGV);
298+
Builder.CreateBr(EndBB);
299+
300+
Builder.SetInsertPoint(EndBB);
301+
Builder.CreateRetVoid();
302+
303+
// Create setTempRet0 function
304+
Params = {Int32Ty};
305+
FTy = FunctionType::get(VoidTy, Params, false);
306+
F = Function::Create(FTy, GlobalValue::ExternalLinkage, "setTempRet0", &M);
307+
F->arg_begin()->setName("value");
308+
EntryBB = BasicBlock::Create(M.getContext(), "entry", F);
309+
Builder.SetInsertPoint(EntryBB);
310+
Builder.CreateStore(&*F->arg_begin(), TempRet0GV);
311+
Builder.CreateRetVoid();
312+
313+
return true;
314+
}
315+
316+
bool WebAssemblyLowerEmscriptenExceptions::runOnFunction(Function &F) {
317+
Module &M = *F.getParent();
318+
IRBuilder<> Builder(M.getContext());
319+
bool Changed = false;
320+
SmallVector<Instruction *, 64> ToErase;
321+
SmallPtrSet<LandingPadInst *, 32> LandingPads;
322+
323+
for (BasicBlock &BB : F) {
324+
auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
325+
if (!II)
326+
continue;
327+
Changed = true;
328+
LandingPads.insert(II->getLandingPadInst());
329+
Builder.SetInsertPoint(II);
330+
331+
if (canThrow(II->getCalledValue())) {
332+
// If we are calling a function that is noreturn, we must remove that
333+
// attribute. The code we insert here does expect it to return, after we
334+
// catch the exception.
335+
if (II->doesNotReturn()) {
336+
if (auto *F = dyn_cast<Function>(II->getCalledValue()))
337+
F->removeFnAttr(Attribute::NoReturn);
338+
AttributeSet NewAttrs = II->getAttributes();
339+
NewAttrs.removeAttribute(M.getContext(), AttributeSet::FunctionIndex,
340+
Attribute::NoReturn);
341+
II->setAttributes(NewAttrs);
342+
}
343+
344+
// Pre-invoke
345+
// __THREW__ = 0;
346+
Builder.CreateStore(Builder.getFalse(), ThrewGV);
347+
348+
// Invoke function wrapper in JavaScript
349+
SmallVector<Value *, 16> CallArgs;
350+
// Put the pointer to the callee as first argument, so it can be called
351+
// within the invoke wrapper later
352+
CallArgs.push_back(II->getCalledValue());
353+
CallArgs.append(II->arg_begin(), II->arg_end());
354+
CallInst *NewCall = Builder.CreateCall(getInvokeWrapper(M, II), CallArgs);
355+
NewCall->takeName(II);
356+
NewCall->setCallingConv(II->getCallingConv());
357+
NewCall->setAttributes(II->getAttributes());
358+
NewCall->setDebugLoc(II->getDebugLoc());
359+
II->replaceAllUsesWith(NewCall);
360+
ToErase.push_back(II);
361+
362+
// Post-invoke
363+
// %__THREW__.val = __THREW__; __THREW__ = 0;
364+
Value *Threw = Builder.CreateLoad(ThrewGV, ThrewGV->getName() + ".val");
365+
Builder.CreateStore(Builder.getFalse(), ThrewGV);
366+
367+
// Insert a branch based on __THREW__ variable
368+
Builder.CreateCondBr(Threw, II->getUnwindDest(), II->getNormalDest());
369+
370+
} else {
371+
// This can't throw, and we don't need this invoke, just replace it with a
372+
// call+branch
373+
SmallVector<Value *, 16> CallArgs(II->arg_begin(), II->arg_end());
374+
CallInst *NewCall = Builder.CreateCall(II->getCalledValue(), CallArgs);
375+
NewCall->takeName(II);
376+
NewCall->setCallingConv(II->getCallingConv());
377+
NewCall->setAttributes(II->getAttributes());
378+
NewCall->setDebugLoc(II->getDebugLoc());
379+
II->replaceAllUsesWith(NewCall);
380+
ToErase.push_back(II);
381+
382+
Builder.CreateBr(II->getNormalDest());
383+
384+
// Remove any PHI node entries from the exception destination
385+
II->getUnwindDest()->removePredecessor(&BB);
386+
}
387+
}
388+
389+
// Process resume instructions
390+
for (BasicBlock &BB : F) {
391+
// Scan the body of the basic block for resumes
392+
for (Instruction &I : BB) {
393+
auto *RI = dyn_cast<ResumeInst>(&I);
394+
if (!RI)
395+
continue;
396+
397+
// Split the input into legal values
398+
Value *Input = RI->getValue();
399+
Builder.SetInsertPoint(RI);
400+
Value *Low = Builder.CreateExtractValue(Input, 0, "low");
401+
402+
// Create a call to ___resumeException function
403+
Value *Args[] = {Low};
404+
Builder.CreateCall(ResumeF, Args);
405+
406+
// Add a terminator to the block
407+
Builder.CreateUnreachable();
408+
ToErase.push_back(RI);
409+
}
410+
}
411+
412+
// Look for orphan landingpads, can occur in blocks with no predecesors
413+
for (BasicBlock &BB : F) {
414+
Instruction *I = BB.getFirstNonPHI();
415+
if (auto *LPI = dyn_cast<LandingPadInst>(I))
416+
LandingPads.insert(LPI);
417+
}
418+
419+
// Handle all the landingpad for this function together, as multiple invokes
420+
// may share a single lp
421+
for (LandingPadInst *LPI : LandingPads) {
422+
Builder.SetInsertPoint(LPI);
423+
SmallVector<Value *, 16> FMCArgs;
424+
for (unsigned i = 0, e = LPI->getNumClauses(); i < e; ++i) {
425+
Constant *Clause = LPI->getClause(i);
426+
// As a temporary workaround for the lack of aggregate varargs support
427+
// in the interface between JS and wasm, break out filter operands into
428+
// their component elements.
429+
if (LPI->isFilter(i)) {
430+
ArrayType *ATy = cast<ArrayType>(Clause->getType());
431+
for (unsigned j = 0, e = ATy->getNumElements(); j < e; ++j) {
432+
Value *EV =
433+
Builder.CreateExtractValue(Clause, makeArrayRef(j), "filter");
434+
FMCArgs.push_back(EV);
435+
}
436+
} else
437+
FMCArgs.push_back(Clause);
438+
}
439+
440+
// Create a call to ___cxa_find_matching_catch_N function
441+
Function *FMCF = getFindMatchingCatch(M, FMCArgs.size());
442+
CallInst *FMCI = Builder.CreateCall(FMCF, FMCArgs, "fmc");
443+
Value *Undef = UndefValue::get(LPI->getType());
444+
Value *Pair0 = Builder.CreateInsertValue(Undef, FMCI, 0, "pair0");
445+
Value *TempRet0 =
446+
Builder.CreateLoad(TempRet0GV, TempRet0GV->getName() + "val");
447+
Value *Pair1 = Builder.CreateInsertValue(Pair0, TempRet0, 1, "pair1");
448+
449+
LPI->replaceAllUsesWith(Pair1);
450+
ToErase.push_back(LPI);
451+
}
452+
453+
// Erase everything we no longer need in this function
454+
for (Instruction *I : ToErase)
455+
I->eraseFromParent();
456+
457+
return Changed;
458+
}

‎llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@ using namespace llvm;
2929

3030
#define DEBUG_TYPE "wasm"
3131

32+
// Emscripten's asm.js-style exception handling
33+
static cl::opt<bool> EnableEmExceptionHandling(
34+
"wasm-em-exception-handling",
35+
cl::desc("WebAssembly Emscripten-style exception handling"),
36+
cl::init(false));
37+
3238
extern "C" void LLVMInitializeWebAssemblyTarget() {
3339
// Register the target.
3440
RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32);
3541
RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64);
42+
43+
// Register exception handling pass to opt
44+
initializeWebAssemblyLowerEmscriptenExceptionsPass(
45+
*PassRegistry::getPassRegistry());
3646
}
3747

3848
//===----------------------------------------------------------------------===//
@@ -149,6 +159,10 @@ void WebAssemblyPassConfig::addIRPasses() {
149159
if (getOptLevel() != CodeGenOpt::None)
150160
addPass(createWebAssemblyOptimizeReturned());
151161

162+
// Handle exceptions.
163+
if (EnableEmExceptionHandling)
164+
addPass(createWebAssemblyLowerEmscriptenExceptions());
165+
152166
TargetPassConfig::addIRPasses();
153167
}
154168

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
; RUN: opt < %s -wasm-lower-em-exceptions -S | FileCheck %s
2+
3+
@_ZTIi = external constant i8*
4+
@_ZTIc = external constant i8*
5+
; CHECK: @[[__THREW__:__THREW__.*]] = global i1 false
6+
; CHECK: @[[THREWVALUE:threwValue.*]] = global i32 0
7+
; CHECK: @[[TEMPRET0:tempRet0.*]] = global i32 0
8+
9+
; Test invoke instruction with clauses (try-catch block)
10+
define void @clause() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
11+
; CHECK-LABEL: @clause(
12+
entry:
13+
invoke void @foo(i32 3)
14+
to label %invoke.cont unwind label %lpad
15+
; CHECK: entry:
16+
; CHECK-NEXT: store i1 false, i1* @[[__THREW__]]
17+
; CHECK-NEXT: call void @__invoke_void_i32(void (i32)* @foo, i32 3)
18+
; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i1, i1* @[[__THREW__]]
19+
; CHECK-NEXT: store i1 false, i1* @[[__THREW__]]
20+
; CHECK-NEXT: br i1 %[[__THREW__VAL]], label %lpad, label %invoke.cont
21+
22+
invoke.cont: ; preds = %entry
23+
br label %try.cont
24+
25+
lpad: ; preds = %entry
26+
%0 = landingpad { i8*, i32 }
27+
catch i8* bitcast (i8** @_ZTIi to i8*)
28+
catch i8* null
29+
%1 = extractvalue { i8*, i32 } %0, 0
30+
%2 = extractvalue { i8*, i32 } %0, 1
31+
br label %catch.dispatch
32+
; CHECK: lpad:
33+
; CHECK-NEXT: %[[FMC:.*]] = call i8* @___cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* null)
34+
; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
35+
; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = load i32, i32* @[[TEMPRET0]]
36+
; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
37+
; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
38+
; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 1
39+
40+
catch.dispatch: ; preds = %lpad
41+
%3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
42+
%matches = icmp eq i32 %2, %3
43+
br i1 %matches, label %catch1, label %catch
44+
45+
catch1: ; preds = %catch.dispatch
46+
%4 = call i8* @__cxa_begin_catch(i8* %1)
47+
%5 = bitcast i8* %4 to i32*
48+
%6 = load i32, i32* %5, align 4
49+
call void @__cxa_end_catch()
50+
br label %try.cont
51+
52+
try.cont: ; preds = %catch, %catch1, %invoke.cont
53+
ret void
54+
55+
catch: ; preds = %catch.dispatch
56+
%7 = call i8* @__cxa_begin_catch(i8* %1)
57+
call void @__cxa_end_catch()
58+
br label %try.cont
59+
}
60+
61+
; Test invoke instruction with filters (functions with throw(...) declaration)
62+
define void @filter() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
63+
; CHECK-LABEL: @filter(
64+
entry:
65+
invoke void @foo(i32 3)
66+
to label %invoke.cont unwind label %lpad
67+
; CHECK: entry:
68+
; CHECK-NEXT: store i1 false, i1* @[[__THREW__]]
69+
; CHECK-NEXT: call void @__invoke_void_i32(void (i32)* @foo, i32 3)
70+
; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i1, i1* @[[__THREW__]]
71+
; CHECK-NEXT: store i1 false, i1* @[[__THREW__]]
72+
; CHECK-NEXT: br i1 %[[__THREW__VAL]], label %lpad, label %invoke.cont
73+
74+
invoke.cont: ; preds = %entry
75+
ret void
76+
77+
lpad: ; preds = %entry
78+
%0 = landingpad { i8*, i32 }
79+
filter [2 x i8*] [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*)]
80+
%1 = extractvalue { i8*, i32 } %0, 0
81+
%2 = extractvalue { i8*, i32 } %0, 1
82+
br label %filter.dispatch
83+
; CHECK: lpad:
84+
; CHECK-NEXT: %[[FMC:.*]] = call i8* @___cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*))
85+
; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
86+
; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = load i32, i32* @[[TEMPRET0]]
87+
; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
88+
; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
89+
; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 1
90+
91+
filter.dispatch: ; preds = %lpad
92+
%ehspec.fails = icmp slt i32 %2, 0
93+
br i1 %ehspec.fails, label %ehspec.unexpected, label %eh.resume
94+
95+
ehspec.unexpected: ; preds = %filter.dispatch
96+
call void @__cxa_call_unexpected(i8* %1) #4
97+
unreachable
98+
99+
eh.resume: ; preds = %filter.dispatch
100+
%lpad.val = insertvalue { i8*, i32 } undef, i8* %1, 0
101+
%lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %2, 1
102+
resume { i8*, i32 } %lpad.val3
103+
; CHECK: eh.resume:
104+
; CHECK-NEXT: insertvalue
105+
; CHECK-NEXT: %[[LPAD_VAL:.*]] = insertvalue
106+
; CHECK-NEXT: %[[LOW:.*]] = extractvalue { i8*, i32 } %[[LPAD_VAL]], 0
107+
; CHECK-NEXT: call void @___resumeException(i8* %[[LOW]])
108+
; CHECK-NEXT: unreachable
109+
}
110+
111+
declare void @foo(i32)
112+
113+
declare i32 @__gxx_personality_v0(...)
114+
declare i32 @llvm.eh.typeid.for(i8*)
115+
declare i8* @__cxa_begin_catch(i8*)
116+
declare void @__cxa_end_catch()
117+
declare void @__cxa_call_unexpected(i8*)
118+
119+
; JS glue functions and invoke wrappers registration
120+
; CHECK: declare void @___resumeException(i8*)
121+
; CHECK: declare void @__invoke_void_i32(void (i32)*, i32)
122+
; CHECK: declare i8* @___cxa_find_matching_catch_4(i8*, i8*)
123+
124+
; setThrew function creation
125+
; CHECK-LABEL: define void @setThrew(i1 %threw, i32 %value) {
126+
; CHECK: entry:
127+
; CHECK-NEXT: %__THREW__.val = load i1, i1* @__THREW__
128+
; CHECK-NEXT: %cmp = icmp eq i1 %__THREW__.val, false
129+
; CHECK-NEXT: br i1 %cmp, label %if.then, label %if.end
130+
; CHECK: if.then:
131+
; CHECK-NEXT: store i1 %threw, i1* @__THREW__
132+
; CHECK-NEXT: store i32 %value, i32* @threwValue
133+
; CHECK-NEXT: br label %if.end
134+
; CHECK: if.end:
135+
; CHECK-NEXT: ret void
136+
; CHECK: }
137+
138+
; setTempRet0 function creation
139+
; CHECK-LABEL: define void @setTempRet0(i32 %value) {
140+
; CHECK: entry:
141+
; CHECK-NEXT: store i32 %value, i32* @tempRet0
142+
; CHECK-NEXT: ret void
143+
; CHECK: }

0 commit comments

Comments
 (0)
Please sign in to comment.