1 //=== WebAssemblyLowerEmscriptenEHSjLj.cpp - Lower exceptions for Emscripten =//
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
7 //===----------------------------------------------------------------------===//
10 /// This file lowers exception-related instructions and setjmp/longjmp function
11 /// calls to use Emscripten's library functions. The pass uses JavaScript's try
12 /// and catch mechanism in case of Emscripten EH/SjLj and Wasm EH intrinsics in
13 /// case of Emscripten SjLJ.
15 /// * Emscripten exception handling
16 /// This pass lowers invokes and landingpads into library functions in JS glue
17 /// code. Invokes are lowered into function wrappers called invoke wrappers that
18 /// exist in JS side, which wraps the original function call with JS try-catch.
19 /// If an exception occurred, cxa_throw() function in JS side sets some
20 /// variables (see below) so we can check whether an exception occurred from
21 /// wasm code and handle it appropriately.
23 /// * Emscripten setjmp-longjmp handling
24 /// This pass lowers setjmp to a reasonably-performant approach for emscripten.
25 /// The idea is that each block with a setjmp is broken up into two parts: the
26 /// part containing setjmp and the part right after the setjmp. The latter part
27 /// is either reached from the setjmp, or later from a longjmp. To handle the
28 /// longjmp, all calls that might longjmp are also called using invoke wrappers
29 /// and thus JS / try-catch. JS longjmp() function also sets some variables so
30 /// we can check / whether a longjmp occurred from wasm code. Each block with a
31 /// function call that might longjmp is also split up after the longjmp call.
32 /// After the longjmp call, we check whether a longjmp occurred, and if it did,
33 /// which setjmp it corresponds to, and jump to the right post-setjmp block.
34 /// We assume setjmp-longjmp handling always run after EH handling, which means
35 /// we don't expect any exception-related instructions when SjLj runs.
36 /// FIXME Currently this scheme does not support indirect call of setjmp,
37 /// because of the limitation of the scheme itself. fastcomp does not support it
40 /// In detail, this pass does following things:
42 /// 1) Assumes the existence of global variables: __THREW__, __threwValue
43 /// __THREW__ and __threwValue are defined in compiler-rt in Emscripten.
44 /// These variables are used for both exceptions and setjmp/longjmps.
45 /// __THREW__ indicates whether an exception or a longjmp occurred or not. 0
46 /// means nothing occurred, 1 means an exception occurred, and other numbers
47 /// mean a longjmp occurred. In the case of longjmp, __THREW__ variable
48 /// indicates the corresponding setjmp buffer the longjmp corresponds to.
49 /// __threwValue is 0 for exceptions, and the argument to longjmp in case of
52 /// * Emscripten exception handling
54 /// 2) We assume the existence of setThrew and setTempRet0/getTempRet0 functions
55 /// at link time. setThrew exists in Emscripten's compiler-rt:
57 /// void setThrew(uintptr_t threw, int value) {
58 /// if (__THREW__ == 0) {
59 /// __THREW__ = threw;
60 /// __threwValue = value;
64 /// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code.
65 /// In exception handling, getTempRet0 indicates the type of an exception
66 /// caught, and in setjmp/longjmp, it means the second argument to longjmp
70 /// invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad
73 /// call @__invoke_SIG(func, arg1, arg2)
74 /// %__THREW__.val = __THREW__;
76 /// if (%__THREW__.val == 1)
80 /// SIG is a mangled string generated based on the LLVM IR-level function
81 /// signature. After LLVM IR types are lowered to the target wasm types,
82 /// the names for these wrappers will change based on wasm types as well,
83 /// as in invoke_vi (function takes an int and returns void). The bodies of
84 /// these wrappers will be generated in JS glue code, and inside those
85 /// wrappers we use JS try-catch to generate actual exception effects. It
86 /// also calls the original callee function. An example wrapper in JS code
87 /// would look like this:
88 /// function invoke_vi(index,a1) {
90 /// Module["dynCall_vi"](index,a1); // This calls original callee
92 /// if (typeof e !== 'number' && e !== 'longjmp') throw e;
93 /// _setThrew(1, 0); // setThrew is called here
96 /// If an exception is thrown, __THREW__ will be set to true in a wrapper,
97 /// so we can jump to the right BB based on this value.
100 /// %val = landingpad catch c1 catch c2 catch c3 ...
103 /// %fmc = call @__cxa_find_matching_catch_N(c1, c2, c3, ...)
104 /// %val = {%fmc, getTempRet0()}
106 /// Here N is a number calculated based on the number of clauses.
107 /// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code.
112 /// call @__resumeException(%a)
113 /// where __resumeException() is a function in JS glue code.
116 /// call @llvm.eh.typeid.for(type) (intrinsic)
118 /// call @llvm_eh_typeid_for(type)
119 /// llvm_eh_typeid_for function will be generated in JS glue code.
121 /// * Emscripten setjmp / longjmp handling
123 /// If there are calls to longjmp()
126 /// longjmp(env, val)
128 /// emscripten_longjmp(env, val)
130 /// If there are calls to setjmp()
132 /// 2) In the function entry that calls setjmp, initialize
133 /// functionInvocationId as follows:
135 /// functionInvocationId = alloca(4)
137 /// Note: the alloca size is not important as this pointer is
138 /// merely used for pointer comparisions.
143 /// __wasm_setjmp(env, label, functionInvocationId)
145 /// __wasm_setjmp records the necessary info (the label and
146 /// functionInvocationId) to the "env".
147 /// A BB with setjmp is split into two after setjmp call in order to
148 /// make the post-setjmp BB the possible destination of longjmp BB.
150 /// 4) Lower every call that might longjmp into
152 /// call @__invoke_SIG(func, arg1, arg2)
153 /// %__THREW__.val = __THREW__;
155 /// %__threwValue.val = __threwValue;
156 /// if (%__THREW__.val != 0 & %__threwValue.val != 0) {
157 /// %label = __wasm_setjmp_test(%__THREW__.val, functionInvocationId);
159 /// emscripten_longjmp(%__THREW__.val, %__threwValue.val);
160 /// setTempRet0(%__threwValue.val);
164 /// longjmp_result = getTempRet0();
166 /// label 1: goto post-setjmp BB 1
167 /// label 2: goto post-setjmp BB 2
169 /// default: goto splitted next BB
172 /// __wasm_setjmp_test examines the jmp buf to see if it was for a matching
173 /// setjmp call. After calling an invoke wrapper, if a longjmp occurred,
174 /// __THREW__ will be the address of matching jmp_buf buffer and
175 /// __threwValue be the second argument to longjmp.
176 /// __wasm_setjmp_test returns a setjmp label, a unique ID to each setjmp
177 /// callsite. Label 0 means this longjmp buffer does not correspond to one
178 /// of the setjmp callsites in this function, so in this case we just chain
179 /// the longjmp to the caller. Label -1 means no longjmp occurred.
180 /// Otherwise we jump to the right post-setjmp BB based on the label.
182 /// * Wasm setjmp / longjmp handling
183 /// This mode still uses some Emscripten library functions but not JavaScript's
184 /// try-catch mechanism. It instead uses Wasm exception handling intrinsics,
185 /// which will be lowered to exception handling instructions.
187 /// If there are calls to longjmp()
190 /// longjmp(env, val)
192 /// __wasm_longjmp(env, val)
194 /// If there are calls to setjmp()
196 /// 2) and 3): The same as 2) and 3) in Emscripten SjLj.
197 /// (functionInvocationId initialization + setjmp callsite transformation)
199 /// 4) Create a catchpad with a wasm.catch() intrinsic, which returns the value
200 /// thrown by __wasm_longjmp function. In the runtime library, we have an
201 /// equivalent of the following struct:
203 /// struct __WasmLongjmpArgs {
208 /// The thrown value here is a pointer to the struct. We use this struct to
209 /// transfer two values by throwing a single value. Wasm throw and catch
210 /// instructions are capable of throwing and catching multiple values, but
211 /// it also requires multivalue support that is currently not very reliable.
212 /// TODO Switch to throwing and catching two values without using the struct
214 /// All longjmpable function calls will be converted to an invoke that will
215 /// unwind to this catchpad in case a longjmp occurs. Within the catchpad, we
216 /// test the thrown values using __wasm_setjmp_test function as we do for
217 /// Emscripten SjLj. The main difference is, in Emscripten SjLj, we need to
218 /// transform every longjmpable callsite into a sequence of code including
219 /// __wasm_setjmp_test() call; in Wasm SjLj we do the testing in only one
220 /// place, in this catchpad.
222 /// After testing calling __wasm_setjmp_test(), if the longjmp does not
223 /// correspond to one of the setjmps within the current function, it rethrows
224 /// the longjmp by calling __wasm_longjmp(). If it corresponds to one of
225 /// setjmps in the function, we jump to the beginning of the function, which
226 /// contains a switch to each post-setjmp BB. Again, in Emscripten SjLj, this
227 /// switch is added for every longjmpable callsite; in Wasm SjLj we do this
228 /// only once at the top of the function. (after functionInvocationId
231 /// The below is the pseudocode for what we have described
234 /// Initialize functionInvocationId
238 /// label 1: goto post-setjmp BB 1
239 /// label 2: goto post-setjmp BB 2
241 /// default: goto splitted next BB
246 /// invoke void @foo() ;; foo is a longjmpable function
247 /// to label %next unwind label %catch.dispatch.longjmp
250 /// catch.dispatch.longjmp:
251 /// %0 = catchswitch within none [label %catch.longjmp] unwind to caller
254 /// %longjmp.args = wasm.catch() ;; struct __WasmLongjmpArgs
255 /// %env = load 'env' field from __WasmLongjmpArgs
256 /// %val = load 'val' field from __WasmLongjmpArgs
257 /// %label = __wasm_setjmp_test(%env, functionInvocationId);
259 /// __wasm_longjmp(%env, %val)
260 /// catchret to %setjmp.dispatch
262 ///===----------------------------------------------------------------------===//
264 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
265 #include "WebAssembly.h"
266 #include "WebAssemblyTargetMachine.h"
267 #include "llvm/ADT/StringExtras.h"
268 #include "llvm/CodeGen/TargetPassConfig.h"
269 #include "llvm/CodeGen/WasmEHFuncInfo.h"
270 #include "llvm/IR/DebugInfoMetadata.h"
271 #include "llvm/IR/Dominators.h"
272 #include "llvm/IR/IRBuilder.h"
273 #include "llvm/IR/IntrinsicsWebAssembly.h"
274 #include "llvm/IR/Module.h"
275 #include "llvm/Support/CommandLine.h"
276 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
277 #include "llvm/Transforms/Utils/Local.h"
278 #include "llvm/Transforms/Utils/SSAUpdater.h"
279 #include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
282 using namespace llvm
;
284 #define DEBUG_TYPE "wasm-lower-em-ehsjlj"
286 static cl::list
<std::string
>
287 EHAllowlist("emscripten-cxx-exceptions-allowed",
288 cl::desc("The list of function names in which Emscripten-style "
289 "exception handling is enabled (see emscripten "
290 "EMSCRIPTEN_CATCHING_ALLOWED options)"),
294 class WebAssemblyLowerEmscriptenEHSjLj final
: public ModulePass
{
295 bool EnableEmEH
; // Enable Emscripten exception handling
296 bool EnableEmSjLj
; // Enable Emscripten setjmp/longjmp handling
297 bool EnableWasmSjLj
; // Enable Wasm setjmp/longjmp handling
298 bool DoSjLj
; // Whether we actually perform setjmp/longjmp handling
300 GlobalVariable
*ThrewGV
= nullptr; // __THREW__ (Emscripten)
301 GlobalVariable
*ThrewValueGV
= nullptr; // __threwValue (Emscripten)
302 Function
*GetTempRet0F
= nullptr; // getTempRet0() (Emscripten)
303 Function
*SetTempRet0F
= nullptr; // setTempRet0() (Emscripten)
304 Function
*ResumeF
= nullptr; // __resumeException() (Emscripten)
305 Function
*EHTypeIDF
= nullptr; // llvm.eh.typeid.for() (intrinsic)
306 Function
*EmLongjmpF
= nullptr; // emscripten_longjmp() (Emscripten)
307 Function
*WasmSetjmpF
= nullptr; // __wasm_setjmp() (Emscripten)
308 Function
*WasmSetjmpTestF
= nullptr; // __wasm_setjmp_test() (Emscripten)
309 Function
*WasmLongjmpF
= nullptr; // __wasm_longjmp() (Emscripten)
310 Function
*CatchF
= nullptr; // wasm.catch() (intrinsic)
312 // type of 'struct __WasmLongjmpArgs' defined in emscripten
313 Type
*LongjmpArgsTy
= nullptr;
315 // __cxa_find_matching_catch_N functions.
316 // Indexed by the number of clauses in an original landingpad instruction.
317 DenseMap
<int, Function
*> FindMatchingCatches
;
318 // Map of <function signature string, invoke_ wrappers>
319 StringMap
<Function
*> InvokeWrappers
;
320 // Set of allowed function names for exception handling
321 std::set
<std::string
, std::less
<>> EHAllowlistSet
;
322 // Functions that contains calls to setjmp
323 SmallPtrSet
<Function
*, 8> SetjmpUsers
;
325 StringRef
getPassName() const override
{
326 return "WebAssembly Lower Emscripten Exceptions";
329 using InstVector
= SmallVectorImpl
<Instruction
*>;
330 bool runEHOnFunction(Function
&F
);
331 bool runSjLjOnFunction(Function
&F
);
332 void handleLongjmpableCallsForEmscriptenSjLj(
333 Function
&F
, Instruction
*FunctionInvocationId
,
334 SmallVectorImpl
<PHINode
*> &SetjmpRetPHIs
);
336 handleLongjmpableCallsForWasmSjLj(Function
&F
,
337 Instruction
*FunctionInvocationId
,
338 SmallVectorImpl
<PHINode
*> &SetjmpRetPHIs
);
339 Function
*getFindMatchingCatch(Module
&M
, unsigned NumClauses
);
341 Value
*wrapInvoke(CallBase
*CI
);
342 void wrapTestSetjmp(BasicBlock
*BB
, DebugLoc DL
, Value
*Threw
,
343 Value
*FunctionInvocationId
, Value
*&Label
,
344 Value
*&LongjmpResult
, BasicBlock
*&CallEmLongjmpBB
,
345 PHINode
*&CallEmLongjmpBBThrewPHI
,
346 PHINode
*&CallEmLongjmpBBThrewValuePHI
,
348 Function
*getInvokeWrapper(CallBase
*CI
);
350 bool areAllExceptionsAllowed() const { return EHAllowlistSet
.empty(); }
351 bool supportsException(const Function
*F
) const {
353 (areAllExceptionsAllowed() || EHAllowlistSet
.count(F
->getName()));
355 void replaceLongjmpWith(Function
*LongjmpF
, Function
*NewF
);
357 void rebuildSSA(Function
&F
);
362 WebAssemblyLowerEmscriptenEHSjLj()
363 : ModulePass(ID
), EnableEmEH(WebAssembly::WasmEnableEmEH
),
364 EnableEmSjLj(WebAssembly::WasmEnableEmSjLj
),
365 EnableWasmSjLj(WebAssembly::WasmEnableSjLj
) {
366 assert(!(EnableEmSjLj
&& EnableWasmSjLj
) &&
367 "Two SjLj modes cannot be turned on at the same time");
368 assert(!(EnableEmEH
&& EnableWasmSjLj
) &&
369 "Wasm SjLj should be only used with Wasm EH");
370 EHAllowlistSet
.insert(EHAllowlist
.begin(), EHAllowlist
.end());
372 bool runOnModule(Module
&M
) override
;
374 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
375 AU
.addRequired
<DominatorTreeWrapperPass
>();
378 } // End anonymous namespace
380 char WebAssemblyLowerEmscriptenEHSjLj::ID
= 0;
381 INITIALIZE_PASS(WebAssemblyLowerEmscriptenEHSjLj
, DEBUG_TYPE
,
382 "WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp",
385 ModulePass
*llvm::createWebAssemblyLowerEmscriptenEHSjLj() {
386 return new WebAssemblyLowerEmscriptenEHSjLj();
389 static bool canThrow(const Value
*V
) {
390 if (const auto *F
= dyn_cast
<const Function
>(V
)) {
391 // Intrinsics cannot throw
392 if (F
->isIntrinsic())
394 StringRef Name
= F
->getName();
395 // leave setjmp and longjmp (mostly) alone, we process them properly later
396 if (Name
== "setjmp" || Name
== "longjmp" || Name
== "emscripten_longjmp")
398 return !F
->doesNotThrow();
400 // not a function, so an indirect call - can throw, we can't tell
404 // Get a thread-local global variable with the given name. If it doesn't exist
405 // declare it, which will generate an import and assume that it will exist at
407 static GlobalVariable
*getGlobalVariable(Module
&M
, Type
*Ty
,
408 WebAssemblyTargetMachine
&TM
,
410 auto *GV
= dyn_cast
<GlobalVariable
>(M
.getOrInsertGlobal(Name
, Ty
));
412 report_fatal_error(Twine("unable to create global: ") + Name
);
414 // Variables created by this function are thread local. If the target does not
415 // support TLS, we depend on CoalesceFeaturesAndStripAtomics to downgrade it
416 // to non-thread-local ones, in which case we don't allow this object to be
417 // linked with other objects using shared memory.
418 GV
->setThreadLocalMode(GlobalValue::GeneralDynamicTLSModel
);
422 // Simple function name mangler.
423 // This function simply takes LLVM's string representation of parameter types
424 // and concatenate them with '_'. There are non-alphanumeric characters but llc
425 // is ok with it, and we need to postprocess these names after the lowering
427 static std::string
getSignature(FunctionType
*FTy
) {
429 raw_string_ostream
OS(Sig
);
430 OS
<< *FTy
->getReturnType();
431 for (Type
*ParamTy
: FTy
->params())
432 OS
<< "_" << *ParamTy
;
436 erase_if(Sig
, isSpace
);
437 // When s2wasm parses .s file, a comma means the end of an argument. So a
438 // mangled function name can contain any character but a comma.
439 std::replace(Sig
.begin(), Sig
.end(), ',', '.');
443 static Function
*getEmscriptenFunction(FunctionType
*Ty
, const Twine
&Name
,
445 Function
* F
= Function::Create(Ty
, GlobalValue::ExternalLinkage
, Name
, M
);
446 // Tell the linker that this function is expected to be imported from the
448 if (!F
->hasFnAttribute("wasm-import-module")) {
449 llvm::AttrBuilder
B(M
->getContext());
450 B
.addAttribute("wasm-import-module", "env");
453 if (!F
->hasFnAttribute("wasm-import-name")) {
454 llvm::AttrBuilder
B(M
->getContext());
455 B
.addAttribute("wasm-import-name", F
->getName());
461 // Returns an integer type for the target architecture's address space.
462 // i32 for wasm32 and i64 for wasm64.
463 static Type
*getAddrIntType(Module
*M
) {
464 IRBuilder
<> IRB(M
->getContext());
465 return IRB
.getIntNTy(M
->getDataLayout().getPointerSizeInBits());
468 // Returns an integer pointer type for the target architecture's address space.
469 // i32* for wasm32 and i64* for wasm64. With opaque pointers this is just a ptr
470 // in address space zero.
471 static Type
*getAddrPtrType(Module
*M
) {
472 return PointerType::getUnqual(M
->getContext());
475 // Returns an integer whose type is the integer type for the target's address
476 // space. Returns (i32 C) for wasm32 and (i64 C) for wasm64, when C is the
478 static Value
*getAddrSizeInt(Module
*M
, uint64_t C
) {
479 IRBuilder
<> IRB(M
->getContext());
480 return IRB
.getIntN(M
->getDataLayout().getPointerSizeInBits(), C
);
483 // Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2.
484 // This is because a landingpad instruction contains two more arguments, a
485 // personality function and a cleanup bit, and __cxa_find_matching_catch_N
486 // functions are named after the number of arguments in the original landingpad
489 WebAssemblyLowerEmscriptenEHSjLj::getFindMatchingCatch(Module
&M
,
490 unsigned NumClauses
) {
491 if (FindMatchingCatches
.count(NumClauses
))
492 return FindMatchingCatches
[NumClauses
];
493 PointerType
*Int8PtrTy
= PointerType::getUnqual(M
.getContext());
494 SmallVector
<Type
*, 16> Args(NumClauses
, Int8PtrTy
);
495 FunctionType
*FTy
= FunctionType::get(Int8PtrTy
, Args
, false);
496 Function
*F
= getEmscriptenFunction(
497 FTy
, "__cxa_find_matching_catch_" + Twine(NumClauses
+ 2), &M
);
498 FindMatchingCatches
[NumClauses
] = F
;
502 // Generate invoke wrapper seqence with preamble and postamble
506 // %__THREW__.val = __THREW__; __THREW__ = 0;
507 // Returns %__THREW__.val, which indicates whether an exception is thrown (or
508 // whether longjmp occurred), for future use.
509 Value
*WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallBase
*CI
) {
510 Module
*M
= CI
->getModule();
511 LLVMContext
&C
= M
->getContext();
514 IRB
.SetInsertPoint(CI
);
518 IRB
.CreateStore(getAddrSizeInt(M
, 0), ThrewGV
);
520 // Invoke function wrapper in JavaScript
521 SmallVector
<Value
*, 16> Args
;
522 // Put the pointer to the callee as first argument, so it can be called
523 // within the invoke wrapper later
524 Args
.push_back(CI
->getCalledOperand());
525 Args
.append(CI
->arg_begin(), CI
->arg_end());
526 CallInst
*NewCall
= IRB
.CreateCall(getInvokeWrapper(CI
), Args
);
527 NewCall
->takeName(CI
);
528 NewCall
->setCallingConv(CallingConv::WASM_EmscriptenInvoke
);
529 NewCall
->setDebugLoc(CI
->getDebugLoc());
531 // Because we added the pointer to the callee as first argument, all
532 // argument attribute indices have to be incremented by one.
533 SmallVector
<AttributeSet
, 8> ArgAttributes
;
534 const AttributeList
&InvokeAL
= CI
->getAttributes();
536 // No attributes for the callee pointer.
537 ArgAttributes
.push_back(AttributeSet());
538 // Copy the argument attributes from the original
539 for (unsigned I
= 0, E
= CI
->arg_size(); I
< E
; ++I
)
540 ArgAttributes
.push_back(InvokeAL
.getParamAttrs(I
));
542 AttrBuilder
FnAttrs(CI
->getContext(), InvokeAL
.getFnAttrs());
543 if (auto Args
= FnAttrs
.getAllocSizeArgs()) {
544 // The allocsize attribute (if any) referes to parameters by index and needs
546 auto [SizeArg
, NEltArg
] = *Args
;
549 NEltArg
= *NEltArg
+ 1;
550 FnAttrs
.addAllocSizeAttr(SizeArg
, NEltArg
);
552 // In case the callee has 'noreturn' attribute, We need to remove it, because
553 // we expect invoke wrappers to return.
554 FnAttrs
.removeAttribute(Attribute::NoReturn
);
556 // Reconstruct the AttributesList based on the vector we constructed.
557 AttributeList NewCallAL
= AttributeList::get(
558 C
, AttributeSet::get(C
, FnAttrs
), InvokeAL
.getRetAttrs(), ArgAttributes
);
559 NewCall
->setAttributes(NewCallAL
);
561 CI
->replaceAllUsesWith(NewCall
);
564 // %__THREW__.val = __THREW__; __THREW__ = 0;
566 IRB
.CreateLoad(getAddrIntType(M
), ThrewGV
, ThrewGV
->getName() + ".val");
567 IRB
.CreateStore(getAddrSizeInt(M
, 0), ThrewGV
);
571 // Get matching invoke wrapper based on callee signature
572 Function
*WebAssemblyLowerEmscriptenEHSjLj::getInvokeWrapper(CallBase
*CI
) {
573 Module
*M
= CI
->getModule();
574 SmallVector
<Type
*, 16> ArgTys
;
575 FunctionType
*CalleeFTy
= CI
->getFunctionType();
577 std::string Sig
= getSignature(CalleeFTy
);
578 auto It
= InvokeWrappers
.find(Sig
);
579 if (It
!= InvokeWrappers
.end())
582 // Put the pointer to the callee as first argument
583 ArgTys
.push_back(PointerType::getUnqual(CalleeFTy
));
584 // Add argument types
585 ArgTys
.append(CalleeFTy
->param_begin(), CalleeFTy
->param_end());
587 FunctionType
*FTy
= FunctionType::get(CalleeFTy
->getReturnType(), ArgTys
,
588 CalleeFTy
->isVarArg());
589 Function
*F
= getEmscriptenFunction(FTy
, "__invoke_" + Sig
, M
);
590 InvokeWrappers
[Sig
] = F
;
594 static bool canLongjmp(const Value
*Callee
) {
595 if (auto *CalleeF
= dyn_cast
<Function
>(Callee
))
596 if (CalleeF
->isIntrinsic())
599 // Attempting to transform inline assembly will result in something like:
600 // call void @__invoke_void(void ()* asm ...)
601 // which is invalid because inline assembly blocks do not have addresses
602 // and can't be passed by pointer. The result is a crash with illegal IR.
603 if (isa
<InlineAsm
>(Callee
))
605 StringRef CalleeName
= Callee
->getName();
607 // TODO Include more functions or consider checking with mangled prefixes
609 // The reason we include malloc/free here is to exclude the malloc/free
610 // calls generated in setjmp prep / cleanup routines.
611 if (CalleeName
== "setjmp" || CalleeName
== "malloc" || CalleeName
== "free")
614 // There are functions in Emscripten's JS glue code or compiler-rt
615 if (CalleeName
== "__resumeException" || CalleeName
== "llvm_eh_typeid_for" ||
616 CalleeName
== "__wasm_setjmp" || CalleeName
== "__wasm_setjmp_test" ||
617 CalleeName
== "getTempRet0" || CalleeName
== "setTempRet0")
620 // __cxa_find_matching_catch_N functions cannot longjmp
621 if (Callee
->getName().starts_with("__cxa_find_matching_catch_"))
624 // Exception-catching related functions
626 // We intentionally treat __cxa_end_catch longjmpable in Wasm SjLj even though
627 // it surely cannot longjmp, in order to maintain the unwind relationship from
628 // all existing catchpads (and calls within them) to catch.dispatch.longjmp.
630 // In Wasm EH + Wasm SjLj, we
631 // 1. Make all catchswitch and cleanuppad that unwind to caller unwind to
632 // catch.dispatch.longjmp instead
633 // 2. Convert all longjmpable calls to invokes that unwind to
634 // catch.dispatch.longjmp
635 // But catchswitch BBs are removed in isel, so if an EH catchswitch (generated
636 // from an exception)'s catchpad does not contain any calls that are converted
637 // into invokes unwinding to catch.dispatch.longjmp, this unwind relationship
638 // (EH catchswitch BB -> catch.dispatch.longjmp BB) is lost and
639 // catch.dispatch.longjmp BB can be placed before the EH catchswitch BB in
641 // int ret = setjmp(buf);
643 // foo(); // longjmps
646 // Then in this code, if 'foo' longjmps, it first unwinds to 'catch (...)'
647 // catchswitch, and is not caught by that catchswitch because it is a longjmp,
648 // then it should next unwind to catch.dispatch.longjmp BB. But if this 'catch
649 // (...)' catchswitch -> catch.dispatch.longjmp unwind relationship is lost,
650 // it will not unwind to catch.dispatch.longjmp, producing an incorrect
653 // Every catchpad generated by Wasm C++ contains __cxa_end_catch, so we
654 // intentionally treat it as longjmpable to work around this problem. This is
655 // a hacky fix but an easy one.
657 // The comment block in findWasmUnwindDestinations() in
658 // SelectionDAGBuilder.cpp is addressing a similar problem.
659 if (CalleeName
== "__cxa_end_catch")
660 return WebAssembly::WasmEnableSjLj
;
661 if (CalleeName
== "__cxa_begin_catch" ||
662 CalleeName
== "__cxa_allocate_exception" || CalleeName
== "__cxa_throw" ||
663 CalleeName
== "__clang_call_terminate")
666 // std::terminate, which is generated when another exception occurs while
667 // handling an exception, cannot longjmp.
668 if (CalleeName
== "_ZSt9terminatev")
671 // Otherwise we don't know
675 static bool isEmAsmCall(const Value
*Callee
) {
676 StringRef CalleeName
= Callee
->getName();
677 // This is an exhaustive list from Emscripten's <emscripten/em_asm.h>.
678 return CalleeName
== "emscripten_asm_const_int" ||
679 CalleeName
== "emscripten_asm_const_double" ||
680 CalleeName
== "emscripten_asm_const_int_sync_on_main_thread" ||
681 CalleeName
== "emscripten_asm_const_double_sync_on_main_thread" ||
682 CalleeName
== "emscripten_asm_const_async_on_main_thread";
685 // Generate __wasm_setjmp_test function call seqence with preamble and
686 // postamble. The code this generates is equivalent to the following
688 // %__threwValue.val = __threwValue;
689 // if (%__THREW__.val != 0 & %__threwValue.val != 0) {
690 // %label = __wasm_setjmp_test(%__THREW__.val, functionInvocationId);
692 // emscripten_longjmp(%__THREW__.val, %__threwValue.val);
693 // setTempRet0(%__threwValue.val);
697 // %longjmp_result = getTempRet0();
699 // As output parameters. returns %label, %longjmp_result, and the BB the last
700 // instruction (%longjmp_result = ...) is in.
701 void WebAssemblyLowerEmscriptenEHSjLj::wrapTestSetjmp(
702 BasicBlock
*BB
, DebugLoc DL
, Value
*Threw
, Value
*FunctionInvocationId
,
703 Value
*&Label
, Value
*&LongjmpResult
, BasicBlock
*&CallEmLongjmpBB
,
704 PHINode
*&CallEmLongjmpBBThrewPHI
, PHINode
*&CallEmLongjmpBBThrewValuePHI
,
705 BasicBlock
*&EndBB
) {
706 Function
*F
= BB
->getParent();
707 Module
*M
= F
->getParent();
708 LLVMContext
&C
= M
->getContext();
710 IRB
.SetCurrentDebugLocation(DL
);
712 // if (%__THREW__.val != 0 & %__threwValue.val != 0)
713 IRB
.SetInsertPoint(BB
);
714 BasicBlock
*ThenBB1
= BasicBlock::Create(C
, "if.then1", F
);
715 BasicBlock
*ElseBB1
= BasicBlock::Create(C
, "if.else1", F
);
716 BasicBlock
*EndBB1
= BasicBlock::Create(C
, "if.end", F
);
717 Value
*ThrewCmp
= IRB
.CreateICmpNE(Threw
, getAddrSizeInt(M
, 0));
718 Value
*ThrewValue
= IRB
.CreateLoad(IRB
.getInt32Ty(), ThrewValueGV
,
719 ThrewValueGV
->getName() + ".val");
720 Value
*ThrewValueCmp
= IRB
.CreateICmpNE(ThrewValue
, IRB
.getInt32(0));
721 Value
*Cmp1
= IRB
.CreateAnd(ThrewCmp
, ThrewValueCmp
, "cmp1");
722 IRB
.CreateCondBr(Cmp1
, ThenBB1
, ElseBB1
);
724 // Generate call.em.longjmp BB once and share it within the function
725 if (!CallEmLongjmpBB
) {
726 // emscripten_longjmp(%__THREW__.val, %__threwValue.val);
727 CallEmLongjmpBB
= BasicBlock::Create(C
, "call.em.longjmp", F
);
728 IRB
.SetInsertPoint(CallEmLongjmpBB
);
729 CallEmLongjmpBBThrewPHI
= IRB
.CreatePHI(getAddrIntType(M
), 4, "threw.phi");
730 CallEmLongjmpBBThrewValuePHI
=
731 IRB
.CreatePHI(IRB
.getInt32Ty(), 4, "threwvalue.phi");
732 CallEmLongjmpBBThrewPHI
->addIncoming(Threw
, ThenBB1
);
733 CallEmLongjmpBBThrewValuePHI
->addIncoming(ThrewValue
, ThenBB1
);
734 IRB
.CreateCall(EmLongjmpF
,
735 {CallEmLongjmpBBThrewPHI
, CallEmLongjmpBBThrewValuePHI
});
736 IRB
.CreateUnreachable();
738 CallEmLongjmpBBThrewPHI
->addIncoming(Threw
, ThenBB1
);
739 CallEmLongjmpBBThrewValuePHI
->addIncoming(ThrewValue
, ThenBB1
);
742 // %label = __wasm_setjmp_test(%__THREW__.val, functionInvocationId);
744 IRB
.SetInsertPoint(ThenBB1
);
745 BasicBlock
*EndBB2
= BasicBlock::Create(C
, "if.end2", F
);
747 IRB
.CreateIntToPtr(Threw
, getAddrPtrType(M
), Threw
->getName() + ".p");
748 Value
*ThenLabel
= IRB
.CreateCall(WasmSetjmpTestF
,
749 {ThrewPtr
, FunctionInvocationId
}, "label");
750 Value
*Cmp2
= IRB
.CreateICmpEQ(ThenLabel
, IRB
.getInt32(0));
751 IRB
.CreateCondBr(Cmp2
, CallEmLongjmpBB
, EndBB2
);
753 // setTempRet0(%__threwValue.val);
754 IRB
.SetInsertPoint(EndBB2
);
755 IRB
.CreateCall(SetTempRet0F
, ThrewValue
);
756 IRB
.CreateBr(EndBB1
);
758 IRB
.SetInsertPoint(ElseBB1
);
759 IRB
.CreateBr(EndBB1
);
761 // longjmp_result = getTempRet0();
762 IRB
.SetInsertPoint(EndBB1
);
763 PHINode
*LabelPHI
= IRB
.CreatePHI(IRB
.getInt32Ty(), 2, "label");
764 LabelPHI
->addIncoming(ThenLabel
, EndBB2
);
766 LabelPHI
->addIncoming(IRB
.getInt32(-1), ElseBB1
);
768 // Output parameter assignment
771 LongjmpResult
= IRB
.CreateCall(GetTempRet0F
, {}, "longjmp_result");
774 void WebAssemblyLowerEmscriptenEHSjLj::rebuildSSA(Function
&F
) {
775 DominatorTree
&DT
= getAnalysis
<DominatorTreeWrapperPass
>(F
).getDomTree();
776 DT
.recalculate(F
); // CFG has been changed
779 for (BasicBlock
&BB
: F
) {
780 for (Instruction
&I
: BB
) {
781 if (I
.getType()->isVoidTy())
783 unsigned VarID
= SSA
.AddVariable(I
.getName(), I
.getType());
784 // If a value is defined by an invoke instruction, it is only available in
785 // its normal destination and not in its unwind destination.
786 if (auto *II
= dyn_cast
<InvokeInst
>(&I
))
787 SSA
.AddAvailableValue(VarID
, II
->getNormalDest(), II
);
789 SSA
.AddAvailableValue(VarID
, &BB
, &I
);
790 for (auto &U
: I
.uses()) {
791 auto *User
= cast
<Instruction
>(U
.getUser());
792 if (auto *UserPN
= dyn_cast
<PHINode
>(User
))
793 if (UserPN
->getIncomingBlock(U
) == &BB
)
795 if (DT
.dominates(&I
, User
))
797 SSA
.AddUse(VarID
, &U
);
801 SSA
.RewriteAllUses(&DT
);
804 // Replace uses of longjmp with a new longjmp function in Emscripten library.
805 // In Emscripten SjLj, the new function is
806 // void emscripten_longjmp(uintptr_t, i32)
807 // In Wasm SjLj, the new function is
808 // void __wasm_longjmp(i8*, i32)
809 // Because the original libc longjmp function takes (jmp_buf*, i32), we need a
810 // ptrtoint/bitcast instruction here to make the type match. jmp_buf* will
811 // eventually be lowered to i32/i64 in the wasm backend.
812 void WebAssemblyLowerEmscriptenEHSjLj::replaceLongjmpWith(Function
*LongjmpF
,
814 assert(NewF
== EmLongjmpF
|| NewF
== WasmLongjmpF
);
815 Module
*M
= LongjmpF
->getParent();
816 SmallVector
<CallInst
*, 8> ToErase
;
817 LLVMContext
&C
= LongjmpF
->getParent()->getContext();
820 // For calls to longjmp, replace it with emscripten_longjmp/__wasm_longjmp and
821 // cast its first argument (jmp_buf*) appropriately
822 for (User
*U
: LongjmpF
->users()) {
823 auto *CI
= dyn_cast
<CallInst
>(U
);
824 if (CI
&& CI
->getCalledFunction() == LongjmpF
) {
825 IRB
.SetInsertPoint(CI
);
826 Value
*Env
= nullptr;
827 if (NewF
== EmLongjmpF
)
829 IRB
.CreatePtrToInt(CI
->getArgOperand(0), getAddrIntType(M
), "env");
831 Env
= IRB
.CreateBitCast(CI
->getArgOperand(0), IRB
.getPtrTy(), "env");
832 IRB
.CreateCall(NewF
, {Env
, CI
->getArgOperand(1)});
833 ToErase
.push_back(CI
);
836 for (auto *I
: ToErase
)
837 I
->eraseFromParent();
839 // If we have any remaining uses of longjmp's function pointer, replace it
840 // with (void(*)(jmp_buf*, int))emscripten_longjmp / __wasm_longjmp.
841 if (!LongjmpF
->uses().empty()) {
843 IRB
.CreateBitCast(NewF
, LongjmpF
->getType(), "longjmp.cast");
844 LongjmpF
->replaceAllUsesWith(NewLongjmp
);
848 static bool containsLongjmpableCalls(const Function
*F
) {
849 for (const auto &BB
: *F
)
850 for (const auto &I
: BB
)
851 if (const auto *CB
= dyn_cast
<CallBase
>(&I
))
852 if (canLongjmp(CB
->getCalledOperand()))
857 // When a function contains a setjmp call but not other calls that can longjmp,
858 // we don't do setjmp transformation for that setjmp. But we need to convert the
859 // setjmp calls into "i32 0" so they don't cause link time errors. setjmp always
860 // returns 0 when called directly.
861 static void nullifySetjmp(Function
*F
) {
862 Module
&M
= *F
->getParent();
863 IRBuilder
<> IRB(M
.getContext());
864 Function
*SetjmpF
= M
.getFunction("setjmp");
865 SmallVector
<Instruction
*, 1> ToErase
;
867 for (User
*U
: make_early_inc_range(SetjmpF
->users())) {
868 auto *CB
= cast
<CallBase
>(U
);
869 BasicBlock
*BB
= CB
->getParent();
870 if (BB
->getParent() != F
) // in other function
872 CallInst
*CI
= nullptr;
873 // setjmp cannot throw. So if it is an invoke, lower it to a call
874 if (auto *II
= dyn_cast
<InvokeInst
>(CB
))
875 CI
= llvm::changeToCall(II
);
877 CI
= cast
<CallInst
>(CB
);
878 ToErase
.push_back(CI
);
879 CI
->replaceAllUsesWith(IRB
.getInt32(0));
881 for (auto *I
: ToErase
)
882 I
->eraseFromParent();
885 bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module
&M
) {
886 LLVM_DEBUG(dbgs() << "********** Lower Emscripten EH & SjLj **********\n");
888 LLVMContext
&C
= M
.getContext();
891 Function
*SetjmpF
= M
.getFunction("setjmp");
892 Function
*LongjmpF
= M
.getFunction("longjmp");
894 // In some platforms _setjmp and _longjmp are used instead. Change these to
895 // use setjmp/longjmp instead, because we later detect these functions by
897 Function
*SetjmpF2
= M
.getFunction("_setjmp");
898 Function
*LongjmpF2
= M
.getFunction("_longjmp");
901 if (SetjmpF
->getFunctionType() != SetjmpF2
->getFunctionType())
902 report_fatal_error("setjmp and _setjmp have different function types");
904 SetjmpF
= Function::Create(SetjmpF2
->getFunctionType(),
905 GlobalValue::ExternalLinkage
, "setjmp", M
);
907 SetjmpF2
->replaceAllUsesWith(SetjmpF
);
911 if (LongjmpF
->getFunctionType() != LongjmpF2
->getFunctionType())
913 "longjmp and _longjmp have different function types");
915 LongjmpF
= Function::Create(LongjmpF2
->getFunctionType(),
916 GlobalValue::ExternalLinkage
, "setjmp", M
);
918 LongjmpF2
->replaceAllUsesWith(LongjmpF
);
921 auto *TPC
= getAnalysisIfAvailable
<TargetPassConfig
>();
922 assert(TPC
&& "Expected a TargetPassConfig");
923 auto &TM
= TPC
->getTM
<WebAssemblyTargetMachine
>();
925 // Declare (or get) global variables __THREW__, __threwValue, and
926 // getTempRet0/setTempRet0 function which are used in common for both
927 // exception handling and setjmp/longjmp handling
928 ThrewGV
= getGlobalVariable(M
, getAddrIntType(&M
), TM
, "__THREW__");
929 ThrewValueGV
= getGlobalVariable(M
, IRB
.getInt32Ty(), TM
, "__threwValue");
930 GetTempRet0F
= getEmscriptenFunction(
931 FunctionType::get(IRB
.getInt32Ty(), false), "getTempRet0", &M
);
932 SetTempRet0F
= getEmscriptenFunction(
933 FunctionType::get(IRB
.getVoidTy(), IRB
.getInt32Ty(), false),
935 GetTempRet0F
->setDoesNotThrow();
936 SetTempRet0F
->setDoesNotThrow();
938 bool Changed
= false;
940 // Function registration for exception handling
942 // Register __resumeException function
943 FunctionType
*ResumeFTy
=
944 FunctionType::get(IRB
.getVoidTy(), IRB
.getPtrTy(), false);
945 ResumeF
= getEmscriptenFunction(ResumeFTy
, "__resumeException", &M
);
946 ResumeF
->addFnAttr(Attribute::NoReturn
);
948 // Register llvm_eh_typeid_for function
949 FunctionType
*EHTypeIDTy
=
950 FunctionType::get(IRB
.getInt32Ty(), IRB
.getPtrTy(), false);
951 EHTypeIDF
= getEmscriptenFunction(EHTypeIDTy
, "llvm_eh_typeid_for", &M
);
954 // Functions that contains calls to setjmp but don't have other longjmpable
955 // calls within them.
956 SmallPtrSet
<Function
*, 4> SetjmpUsersToNullify
;
958 if ((EnableEmSjLj
|| EnableWasmSjLj
) && SetjmpF
) {
959 // Precompute setjmp users
960 for (User
*U
: SetjmpF
->users()) {
961 if (auto *CB
= dyn_cast
<CallBase
>(U
)) {
962 auto *UserF
= CB
->getFunction();
963 // If a function that calls setjmp does not contain any other calls that
964 // can longjmp, we don't need to do any transformation on that function,
966 if (containsLongjmpableCalls(UserF
))
967 SetjmpUsers
.insert(UserF
);
969 SetjmpUsersToNullify
.insert(UserF
);
972 raw_string_ostream
SS(S
);
974 report_fatal_error(Twine("Indirect use of setjmp is not supported: ") +
980 bool SetjmpUsed
= SetjmpF
&& !SetjmpUsers
.empty();
981 bool LongjmpUsed
= LongjmpF
&& !LongjmpF
->use_empty();
982 DoSjLj
= (EnableEmSjLj
| EnableWasmSjLj
) && (SetjmpUsed
|| LongjmpUsed
);
984 // Function registration and data pre-gathering for setjmp/longjmp handling
986 assert(EnableEmSjLj
|| EnableWasmSjLj
);
988 // Register emscripten_longjmp function
989 FunctionType
*FTy
= FunctionType::get(
990 IRB
.getVoidTy(), {getAddrIntType(&M
), IRB
.getInt32Ty()}, false);
991 EmLongjmpF
= getEmscriptenFunction(FTy
, "emscripten_longjmp", &M
);
992 EmLongjmpF
->addFnAttr(Attribute::NoReturn
);
993 } else { // EnableWasmSjLj
994 Type
*Int8PtrTy
= IRB
.getPtrTy();
995 // Register __wasm_longjmp function, which calls __builtin_wasm_longjmp.
996 FunctionType
*FTy
= FunctionType::get(
997 IRB
.getVoidTy(), {Int8PtrTy
, IRB
.getInt32Ty()}, false);
998 WasmLongjmpF
= getEmscriptenFunction(FTy
, "__wasm_longjmp", &M
);
999 WasmLongjmpF
->addFnAttr(Attribute::NoReturn
);
1003 Type
*Int8PtrTy
= IRB
.getPtrTy();
1004 Type
*Int32PtrTy
= IRB
.getPtrTy();
1005 Type
*Int32Ty
= IRB
.getInt32Ty();
1007 // Register __wasm_setjmp function
1008 FunctionType
*SetjmpFTy
= SetjmpF
->getFunctionType();
1009 FunctionType
*FTy
= FunctionType::get(
1010 IRB
.getVoidTy(), {SetjmpFTy
->getParamType(0), Int32Ty
, Int32PtrTy
},
1012 WasmSetjmpF
= getEmscriptenFunction(FTy
, "__wasm_setjmp", &M
);
1014 // Register __wasm_setjmp_test function
1015 FTy
= FunctionType::get(Int32Ty
, {Int32PtrTy
, Int32PtrTy
}, false);
1016 WasmSetjmpTestF
= getEmscriptenFunction(FTy
, "__wasm_setjmp_test", &M
);
1018 // wasm.catch() will be lowered down to wasm 'catch' instruction in
1019 // instruction selection.
1020 CatchF
= Intrinsic::getOrInsertDeclaration(&M
, Intrinsic::wasm_catch
);
1021 // Type for struct __WasmLongjmpArgs
1022 LongjmpArgsTy
= StructType::get(Int8PtrTy
, // env
1028 // Exception handling transformation
1030 for (Function
&F
: M
) {
1031 if (F
.isDeclaration())
1033 Changed
|= runEHOnFunction(F
);
1037 // Setjmp/longjmp handling transformation
1039 Changed
= true; // We have setjmp or longjmp somewhere
1041 replaceLongjmpWith(LongjmpF
, EnableEmSjLj
? EmLongjmpF
: WasmLongjmpF
);
1042 // Only traverse functions that uses setjmp in order not to insert
1043 // unnecessary prep / cleanup code in every function
1045 for (Function
*F
: SetjmpUsers
)
1046 runSjLjOnFunction(*F
);
1049 // Replace unnecessary setjmp calls with 0
1050 if ((EnableEmSjLj
|| EnableWasmSjLj
) && !SetjmpUsersToNullify
.empty()) {
1053 for (Function
*F
: SetjmpUsersToNullify
)
1057 // Delete unused global variables and functions
1058 for (auto *V
: {ThrewGV
, ThrewValueGV
})
1059 if (V
&& V
->use_empty())
1060 V
->eraseFromParent();
1061 for (auto *V
: {GetTempRet0F
, SetTempRet0F
, ResumeF
, EHTypeIDF
, EmLongjmpF
,
1062 WasmSetjmpF
, WasmSetjmpTestF
, WasmLongjmpF
, CatchF
})
1063 if (V
&& V
->use_empty())
1064 V
->eraseFromParent();
1069 bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function
&F
) {
1070 Module
&M
= *F
.getParent();
1071 LLVMContext
&C
= F
.getContext();
1073 bool Changed
= false;
1074 SmallVector
<Instruction
*, 64> ToErase
;
1075 SmallPtrSet
<LandingPadInst
*, 32> LandingPads
;
1077 // rethrow.longjmp BB that will be shared within the function.
1078 BasicBlock
*RethrowLongjmpBB
= nullptr;
1079 // PHI node for the loaded value of __THREW__ global variable in
1080 // rethrow.longjmp BB
1081 PHINode
*RethrowLongjmpBBThrewPHI
= nullptr;
1083 for (BasicBlock
&BB
: F
) {
1084 auto *II
= dyn_cast
<InvokeInst
>(BB
.getTerminator());
1088 LandingPads
.insert(II
->getLandingPadInst());
1089 IRB
.SetInsertPoint(II
);
1091 const Value
*Callee
= II
->getCalledOperand();
1092 bool NeedInvoke
= supportsException(&F
) && canThrow(Callee
);
1094 // Wrap invoke with invoke wrapper and generate preamble/postamble
1095 Value
*Threw
= wrapInvoke(II
);
1096 ToErase
.push_back(II
);
1098 // If setjmp/longjmp handling is enabled, the thrown value can be not an
1099 // exception but a longjmp. If the current function contains calls to
1100 // setjmp, it will be appropriately handled in runSjLjOnFunction. But even
1101 // if the function does not contain setjmp calls, we shouldn't silently
1102 // ignore longjmps; we should rethrow them so they can be correctly
1103 // handled in somewhere up the call chain where setjmp is. __THREW__'s
1104 // value is 0 when nothing happened, 1 when an exception is thrown, and
1105 // other values when longjmp is thrown.
1107 // if (%__THREW__.val == 0 || %__THREW__.val == 1)
1110 // goto %longjmp.rethrow
1112 // rethrow.longjmp: ;; This is longjmp. Rethrow it
1113 // %__threwValue.val = __threwValue
1114 // emscripten_longjmp(%__THREW__.val, %__threwValue.val);
1116 // tail: ;; Nothing happened or an exception is thrown
1117 // ... Continue exception handling ...
1118 if (DoSjLj
&& EnableEmSjLj
&& !SetjmpUsers
.count(&F
) &&
1119 canLongjmp(Callee
)) {
1120 // Create longjmp.rethrow BB once and share it within the function
1121 if (!RethrowLongjmpBB
) {
1122 RethrowLongjmpBB
= BasicBlock::Create(C
, "rethrow.longjmp", &F
);
1123 IRB
.SetInsertPoint(RethrowLongjmpBB
);
1124 RethrowLongjmpBBThrewPHI
=
1125 IRB
.CreatePHI(getAddrIntType(&M
), 4, "threw.phi");
1126 RethrowLongjmpBBThrewPHI
->addIncoming(Threw
, &BB
);
1127 Value
*ThrewValue
= IRB
.CreateLoad(IRB
.getInt32Ty(), ThrewValueGV
,
1128 ThrewValueGV
->getName() + ".val");
1129 IRB
.CreateCall(EmLongjmpF
, {RethrowLongjmpBBThrewPHI
, ThrewValue
});
1130 IRB
.CreateUnreachable();
1132 RethrowLongjmpBBThrewPHI
->addIncoming(Threw
, &BB
);
1135 IRB
.SetInsertPoint(II
); // Restore the insert point back
1136 BasicBlock
*Tail
= BasicBlock::Create(C
, "tail", &F
);
1138 IRB
.CreateICmpEQ(Threw
, getAddrSizeInt(&M
, 1), "cmp.eq.one");
1140 IRB
.CreateICmpEQ(Threw
, getAddrSizeInt(&M
, 0), "cmp.eq.zero");
1141 Value
*Or
= IRB
.CreateOr(CmpEqZero
, CmpEqOne
, "or");
1142 IRB
.CreateCondBr(Or
, Tail
, RethrowLongjmpBB
);
1143 IRB
.SetInsertPoint(Tail
);
1144 BB
.replaceSuccessorsPhiUsesWith(&BB
, Tail
);
1147 // Insert a branch based on __THREW__ variable
1148 Value
*Cmp
= IRB
.CreateICmpEQ(Threw
, getAddrSizeInt(&M
, 1), "cmp");
1149 IRB
.CreateCondBr(Cmp
, II
->getUnwindDest(), II
->getNormalDest());
1152 // This can't throw, and we don't need this invoke, just replace it with a
1158 // Process resume instructions
1159 for (BasicBlock
&BB
: F
) {
1160 // Scan the body of the basic block for resumes
1161 for (Instruction
&I
: BB
) {
1162 auto *RI
= dyn_cast
<ResumeInst
>(&I
);
1167 // Split the input into legal values
1168 Value
*Input
= RI
->getValue();
1169 IRB
.SetInsertPoint(RI
);
1170 Value
*Low
= IRB
.CreateExtractValue(Input
, 0, "low");
1171 // Create a call to __resumeException function
1172 IRB
.CreateCall(ResumeF
, {Low
});
1173 // Add a terminator to the block
1174 IRB
.CreateUnreachable();
1175 ToErase
.push_back(RI
);
1179 // Process llvm.eh.typeid.for intrinsics
1180 for (BasicBlock
&BB
: F
) {
1181 for (Instruction
&I
: BB
) {
1182 auto *CI
= dyn_cast
<CallInst
>(&I
);
1185 const Function
*Callee
= CI
->getCalledFunction();
1188 if (Callee
->getIntrinsicID() != Intrinsic::eh_typeid_for
)
1192 IRB
.SetInsertPoint(CI
);
1194 IRB
.CreateCall(EHTypeIDF
, CI
->getArgOperand(0), "typeid");
1195 CI
->replaceAllUsesWith(NewCI
);
1196 ToErase
.push_back(CI
);
1200 // Look for orphan landingpads, can occur in blocks with no predecessors
1201 for (BasicBlock
&BB
: F
) {
1202 Instruction
*I
= BB
.getFirstNonPHI();
1203 if (auto *LPI
= dyn_cast
<LandingPadInst
>(I
))
1204 LandingPads
.insert(LPI
);
1206 Changed
|= !LandingPads
.empty();
1208 // Handle all the landingpad for this function together, as multiple invokes
1209 // may share a single lp
1210 for (LandingPadInst
*LPI
: LandingPads
) {
1211 IRB
.SetInsertPoint(LPI
);
1212 SmallVector
<Value
*, 16> FMCArgs
;
1213 for (unsigned I
= 0, E
= LPI
->getNumClauses(); I
< E
; ++I
) {
1214 Constant
*Clause
= LPI
->getClause(I
);
1215 // TODO Handle filters (= exception specifications).
1216 // https://github.com/llvm/llvm-project/issues/49740
1217 if (LPI
->isCatch(I
))
1218 FMCArgs
.push_back(Clause
);
1221 // Create a call to __cxa_find_matching_catch_N function
1222 Function
*FMCF
= getFindMatchingCatch(M
, FMCArgs
.size());
1223 CallInst
*FMCI
= IRB
.CreateCall(FMCF
, FMCArgs
, "fmc");
1224 Value
*Poison
= PoisonValue::get(LPI
->getType());
1225 Value
*Pair0
= IRB
.CreateInsertValue(Poison
, FMCI
, 0, "pair0");
1226 Value
*TempRet0
= IRB
.CreateCall(GetTempRet0F
, {}, "tempret0");
1227 Value
*Pair1
= IRB
.CreateInsertValue(Pair0
, TempRet0
, 1, "pair1");
1229 LPI
->replaceAllUsesWith(Pair1
);
1230 ToErase
.push_back(LPI
);
1233 // Erase everything we no longer need in this function
1234 for (Instruction
*I
: ToErase
)
1235 I
->eraseFromParent();
1240 // This tries to get debug info from the instruction before which a new
1241 // instruction will be inserted, and if there's no debug info in that
1242 // instruction, tries to get the info instead from the previous instruction (if
1243 // any). If none of these has debug info and a DISubprogram is provided, it
1244 // creates a dummy debug info with the first line of the function, because IR
1245 // verifier requires all inlinable callsites should have debug info when both a
1246 // caller and callee have DISubprogram. If none of these conditions are met,
1247 // returns empty info.
1248 static DebugLoc
getOrCreateDebugLoc(const Instruction
*InsertBefore
,
1250 assert(InsertBefore
);
1251 if (InsertBefore
->getDebugLoc())
1252 return InsertBefore
->getDebugLoc();
1253 const Instruction
*Prev
= InsertBefore
->getPrevNode();
1254 if (Prev
&& Prev
->getDebugLoc())
1255 return Prev
->getDebugLoc();
1257 return DILocation::get(SP
->getContext(), SP
->getLine(), 1, SP
);
1261 bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function
&F
) {
1262 assert(EnableEmSjLj
|| EnableWasmSjLj
);
1263 Module
&M
= *F
.getParent();
1264 LLVMContext
&C
= F
.getContext();
1266 SmallVector
<Instruction
*, 64> ToErase
;
1268 // Setjmp preparation
1270 BasicBlock
*Entry
= &F
.getEntryBlock();
1271 DebugLoc FirstDL
= getOrCreateDebugLoc(&*Entry
->begin(), F
.getSubprogram());
1272 SplitBlock(Entry
, &*Entry
->getFirstInsertionPt());
1274 IRB
.SetInsertPoint(Entry
->getTerminator()->getIterator());
1275 // This alloca'ed pointer is used by the runtime to identify function
1276 // invocations. It's just for pointer comparisons. It will never be
1278 Instruction
*FunctionInvocationId
=
1279 IRB
.CreateAlloca(IRB
.getInt32Ty(), nullptr, "functionInvocationId");
1280 FunctionInvocationId
->setDebugLoc(FirstDL
);
1282 // Setjmp transformation
1283 SmallVector
<PHINode
*, 4> SetjmpRetPHIs
;
1284 Function
*SetjmpF
= M
.getFunction("setjmp");
1285 for (auto *U
: make_early_inc_range(SetjmpF
->users())) {
1286 auto *CB
= cast
<CallBase
>(U
);
1287 BasicBlock
*BB
= CB
->getParent();
1288 if (BB
->getParent() != &F
) // in other function
1290 if (CB
->getOperandBundle(LLVMContext::OB_funclet
)) {
1292 raw_string_ostream
SS(S
);
1293 SS
<< "In function " + F
.getName() +
1294 ": setjmp within a catch clause is not supported in Wasm EH:\n";
1296 report_fatal_error(StringRef(SS
.str()));
1299 CallInst
*CI
= nullptr;
1300 // setjmp cannot throw. So if it is an invoke, lower it to a call
1301 if (auto *II
= dyn_cast
<InvokeInst
>(CB
))
1302 CI
= llvm::changeToCall(II
);
1304 CI
= cast
<CallInst
>(CB
);
1306 // The tail is everything right after the call, and will be reached once
1307 // when setjmp is called, and later when longjmp returns to the setjmp
1308 BasicBlock
*Tail
= SplitBlock(BB
, CI
->getNextNode());
1309 // Add a phi to the tail, which will be the output of setjmp, which
1310 // indicates if this is the first call or a longjmp back. The phi directly
1311 // uses the right value based on where we arrive from
1312 IRB
.SetInsertPoint(Tail
, Tail
->getFirstNonPHIIt());
1313 PHINode
*SetjmpRet
= IRB
.CreatePHI(IRB
.getInt32Ty(), 2, "setjmp.ret");
1315 // setjmp initial call returns 0
1316 SetjmpRet
->addIncoming(IRB
.getInt32(0), BB
);
1317 // The proper output is now this, not the setjmp call itself
1318 CI
->replaceAllUsesWith(SetjmpRet
);
1319 // longjmp returns to the setjmp will add themselves to this phi
1320 SetjmpRetPHIs
.push_back(SetjmpRet
);
1323 // Our index in the function is our place in the array + 1 to avoid index
1324 // 0, because index 0 means the longjmp is not ours to handle.
1325 IRB
.SetInsertPoint(CI
);
1326 Value
*Args
[] = {CI
->getArgOperand(0), IRB
.getInt32(SetjmpRetPHIs
.size()),
1327 FunctionInvocationId
};
1328 IRB
.CreateCall(WasmSetjmpF
, Args
);
1329 ToErase
.push_back(CI
);
1332 // Handle longjmpable calls.
1334 handleLongjmpableCallsForEmscriptenSjLj(F
, FunctionInvocationId
,
1336 else // EnableWasmSjLj
1337 handleLongjmpableCallsForWasmSjLj(F
, FunctionInvocationId
, SetjmpRetPHIs
);
1339 // Erase everything we no longer need in this function
1340 for (Instruction
*I
: ToErase
)
1341 I
->eraseFromParent();
1343 // Finally, our modifications to the cfg can break dominance of SSA variables.
1344 // For example, in this code,
1345 // if (x()) { .. setjmp() .. }
1346 // if (y()) { .. longjmp() .. }
1347 // We must split the longjmp block, and it can jump into the block splitted
1348 // from setjmp one. But that means that when we split the setjmp block, it's
1349 // first part no longer dominates its second part - there is a theoretically
1350 // possible control flow path where x() is false, then y() is true and we
1351 // reach the second part of the setjmp block, without ever reaching the first
1352 // part. So, we rebuild SSA form here.
1357 // Update each call that can longjmp so it can return to the corresponding
1358 // setjmp. Refer to 4) of "Emscripten setjmp/longjmp handling" section in the
1359 // comments at top of the file for details.
1360 void WebAssemblyLowerEmscriptenEHSjLj::handleLongjmpableCallsForEmscriptenSjLj(
1361 Function
&F
, Instruction
*FunctionInvocationId
,
1362 SmallVectorImpl
<PHINode
*> &SetjmpRetPHIs
) {
1363 Module
&M
= *F
.getParent();
1364 LLVMContext
&C
= F
.getContext();
1366 SmallVector
<Instruction
*, 64> ToErase
;
1368 // call.em.longjmp BB that will be shared within the function.
1369 BasicBlock
*CallEmLongjmpBB
= nullptr;
1370 // PHI node for the loaded value of __THREW__ global variable in
1371 // call.em.longjmp BB
1372 PHINode
*CallEmLongjmpBBThrewPHI
= nullptr;
1373 // PHI node for the loaded value of __threwValue global variable in
1374 // call.em.longjmp BB
1375 PHINode
*CallEmLongjmpBBThrewValuePHI
= nullptr;
1376 // rethrow.exn BB that will be shared within the function.
1377 BasicBlock
*RethrowExnBB
= nullptr;
1379 // Because we are creating new BBs while processing and don't want to make
1380 // all these newly created BBs candidates again for longjmp processing, we
1381 // first make the vector of candidate BBs.
1382 std::vector
<BasicBlock
*> BBs
;
1383 for (BasicBlock
&BB
: F
)
1386 // BBs.size() will change within the loop, so we query it every time
1387 for (unsigned I
= 0; I
< BBs
.size(); I
++) {
1388 BasicBlock
*BB
= BBs
[I
];
1389 for (Instruction
&I
: *BB
) {
1390 if (isa
<InvokeInst
>(&I
)) {
1392 raw_string_ostream
SS(S
);
1393 SS
<< "In function " << F
.getName()
1394 << ": When using Wasm EH with Emscripten SjLj, there is a "
1395 "restriction that `setjmp` function call and exception cannot be "
1396 "used within the same function:\n";
1398 report_fatal_error(StringRef(SS
.str()));
1400 auto *CI
= dyn_cast
<CallInst
>(&I
);
1404 const Value
*Callee
= CI
->getCalledOperand();
1405 if (!canLongjmp(Callee
))
1407 if (isEmAsmCall(Callee
))
1408 report_fatal_error("Cannot use EM_ASM* alongside setjmp/longjmp in " +
1410 ". Please consider using EM_JS, or move the "
1411 "EM_ASM into another function.",
1414 Value
*Threw
= nullptr;
1416 if (Callee
->getName().starts_with("__invoke_")) {
1417 // If invoke wrapper has already been generated for this call in
1418 // previous EH phase, search for the load instruction
1419 // %__THREW__.val = __THREW__;
1420 // in postamble after the invoke wrapper call
1421 LoadInst
*ThrewLI
= nullptr;
1422 StoreInst
*ThrewResetSI
= nullptr;
1423 for (auto I
= std::next(BasicBlock::iterator(CI
)), IE
= BB
->end();
1425 if (auto *LI
= dyn_cast
<LoadInst
>(I
))
1426 if (auto *GV
= dyn_cast
<GlobalVariable
>(LI
->getPointerOperand()))
1427 if (GV
== ThrewGV
) {
1428 Threw
= ThrewLI
= LI
;
1432 // Search for the store instruction after the load above
1434 for (auto I
= std::next(BasicBlock::iterator(ThrewLI
)), IE
= BB
->end();
1436 if (auto *SI
= dyn_cast
<StoreInst
>(I
)) {
1437 if (auto *GV
= dyn_cast
<GlobalVariable
>(SI
->getPointerOperand())) {
1438 if (GV
== ThrewGV
&&
1439 SI
->getValueOperand() == getAddrSizeInt(&M
, 0)) {
1446 assert(Threw
&& ThrewLI
&& "Cannot find __THREW__ load after invoke");
1447 assert(ThrewResetSI
&& "Cannot find __THREW__ store after invoke");
1448 Tail
= SplitBlock(BB
, ThrewResetSI
->getNextNode());
1451 // Wrap call with invoke wrapper and generate preamble/postamble
1452 Threw
= wrapInvoke(CI
);
1453 ToErase
.push_back(CI
);
1454 Tail
= SplitBlock(BB
, CI
->getNextNode());
1456 // If exception handling is enabled, the thrown value can be not a
1457 // longjmp but an exception, in which case we shouldn't silently ignore
1458 // exceptions; we should rethrow them.
1459 // __THREW__'s value is 0 when nothing happened, 1 when an exception is
1460 // thrown, other values when longjmp is thrown.
1462 // if (%__THREW__.val == 1)
1467 // eh.rethrow: ;; Rethrow exception
1468 // %exn = call @__cxa_find_matching_catch_2() ;; Retrieve thrown ptr
1469 // __resumeException(%exn)
1472 // <-- Insertion point. Will insert sjlj handling code from here
1477 if (supportsException(&F
) && canThrow(Callee
)) {
1478 // We will add a new conditional branch. So remove the branch created
1479 // when we split the BB
1480 ToErase
.push_back(BB
->getTerminator());
1482 // Generate rethrow.exn BB once and share it within the function
1483 if (!RethrowExnBB
) {
1484 RethrowExnBB
= BasicBlock::Create(C
, "rethrow.exn", &F
);
1485 IRB
.SetInsertPoint(RethrowExnBB
);
1487 IRB
.CreateCall(getFindMatchingCatch(M
, 0), {}, "exn");
1488 IRB
.CreateCall(ResumeF
, {Exn
});
1489 IRB
.CreateUnreachable();
1492 IRB
.SetInsertPoint(CI
);
1493 BasicBlock
*NormalBB
= BasicBlock::Create(C
, "normal", &F
);
1495 IRB
.CreateICmpEQ(Threw
, getAddrSizeInt(&M
, 1), "cmp.eq.one");
1496 IRB
.CreateCondBr(CmpEqOne
, RethrowExnBB
, NormalBB
);
1498 IRB
.SetInsertPoint(NormalBB
);
1500 BB
= NormalBB
; // New insertion point to insert __wasm_setjmp_test()
1504 // We need to replace the terminator in Tail - SplitBlock makes BB go
1505 // straight to Tail, we need to check if a longjmp occurred, and go to the
1506 // right setjmp-tail if so
1507 ToErase
.push_back(BB
->getTerminator());
1509 // Generate a function call to __wasm_setjmp_test function and
1510 // preamble/postamble code to figure out (1) whether longjmp
1511 // occurred (2) if longjmp occurred, which setjmp it corresponds to
1512 Value
*Label
= nullptr;
1513 Value
*LongjmpResult
= nullptr;
1514 BasicBlock
*EndBB
= nullptr;
1515 wrapTestSetjmp(BB
, CI
->getDebugLoc(), Threw
, FunctionInvocationId
, Label
,
1516 LongjmpResult
, CallEmLongjmpBB
, CallEmLongjmpBBThrewPHI
,
1517 CallEmLongjmpBBThrewValuePHI
, EndBB
);
1518 assert(Label
&& LongjmpResult
&& EndBB
);
1520 // Create switch instruction
1521 IRB
.SetInsertPoint(EndBB
);
1522 IRB
.SetCurrentDebugLocation(EndBB
->back().getDebugLoc());
1523 SwitchInst
*SI
= IRB
.CreateSwitch(Label
, Tail
, SetjmpRetPHIs
.size());
1524 // -1 means no longjmp happened, continue normally (will hit the default
1525 // switch case). 0 means a longjmp that is not ours to handle, needs a
1526 // rethrow. Otherwise the index is the same as the index in P+1 (to avoid
1528 for (unsigned I
= 0; I
< SetjmpRetPHIs
.size(); I
++) {
1529 SI
->addCase(IRB
.getInt32(I
+ 1), SetjmpRetPHIs
[I
]->getParent());
1530 SetjmpRetPHIs
[I
]->addIncoming(LongjmpResult
, EndBB
);
1533 // We are splitting the block here, and must continue to find other calls
1534 // in the block - which is now split. so continue to traverse in the Tail
1535 BBs
.push_back(Tail
);
1539 for (Instruction
*I
: ToErase
)
1540 I
->eraseFromParent();
1543 static BasicBlock
*getCleanupRetUnwindDest(const CleanupPadInst
*CPI
) {
1544 for (const User
*U
: CPI
->users())
1545 if (const auto *CRI
= dyn_cast
<CleanupReturnInst
>(U
))
1546 return CRI
->getUnwindDest();
1550 // Create a catchpad in which we catch a longjmp's env and val arguments, test
1551 // if the longjmp corresponds to one of setjmps in the current function, and if
1552 // so, jump to the setjmp dispatch BB from which we go to one of post-setjmp
1553 // BBs. Refer to 4) of "Wasm setjmp/longjmp handling" section in the comments at
1554 // top of the file for details.
1555 void WebAssemblyLowerEmscriptenEHSjLj::handleLongjmpableCallsForWasmSjLj(
1556 Function
&F
, Instruction
*FunctionInvocationId
,
1557 SmallVectorImpl
<PHINode
*> &SetjmpRetPHIs
) {
1558 Module
&M
= *F
.getParent();
1559 LLVMContext
&C
= F
.getContext();
1562 // A function with catchswitch/catchpad instruction should have a personality
1563 // function attached to it. Search for the wasm personality function, and if
1564 // it exists, use it, and if it doesn't, create a dummy personality function.
1565 // (SjLj is not going to call it anyway.)
1566 if (!F
.hasPersonalityFn()) {
1567 StringRef PersName
= getEHPersonalityName(EHPersonality::Wasm_CXX
);
1568 FunctionType
*PersType
=
1569 FunctionType::get(IRB
.getInt32Ty(), /* isVarArg */ true);
1570 Value
*PersF
= M
.getOrInsertFunction(PersName
, PersType
).getCallee();
1572 cast
<Constant
>(IRB
.CreateBitCast(PersF
, IRB
.getPtrTy())));
1575 // Use the entry BB's debugloc as a fallback
1576 BasicBlock
*Entry
= &F
.getEntryBlock();
1577 DebugLoc FirstDL
= getOrCreateDebugLoc(&*Entry
->begin(), F
.getSubprogram());
1578 IRB
.SetCurrentDebugLocation(FirstDL
);
1580 // Add setjmp.dispatch BB right after the entry block. Because we have
1581 // initialized functionInvocationId in the entry block and split the
1582 // rest into another BB, here 'OrigEntry' is the function's original entry
1583 // block before the transformation.
1586 // functionInvocationId initialization
1588 // switch will be inserted here later
1589 // entry.split: (OrigEntry)
1590 // the original function starts here
1591 BasicBlock
*OrigEntry
= Entry
->getNextNode();
1592 BasicBlock
*SetjmpDispatchBB
=
1593 BasicBlock::Create(C
, "setjmp.dispatch", &F
, OrigEntry
);
1594 cast
<BranchInst
>(Entry
->getTerminator())->setSuccessor(0, SetjmpDispatchBB
);
1596 // Create catch.dispatch.longjmp BB and a catchswitch instruction
1597 BasicBlock
*CatchDispatchLongjmpBB
=
1598 BasicBlock::Create(C
, "catch.dispatch.longjmp", &F
);
1599 IRB
.SetInsertPoint(CatchDispatchLongjmpBB
);
1600 CatchSwitchInst
*CatchSwitchLongjmp
=
1601 IRB
.CreateCatchSwitch(ConstantTokenNone::get(C
), nullptr, 1);
1603 // Create catch.longjmp BB and a catchpad instruction
1604 BasicBlock
*CatchLongjmpBB
= BasicBlock::Create(C
, "catch.longjmp", &F
);
1605 CatchSwitchLongjmp
->addHandler(CatchLongjmpBB
);
1606 IRB
.SetInsertPoint(CatchLongjmpBB
);
1607 CatchPadInst
*CatchPad
= IRB
.CreateCatchPad(CatchSwitchLongjmp
, {});
1609 // Wasm throw and catch instructions can throw and catch multiple values, but
1610 // that requires multivalue support in the toolchain, which is currently not
1611 // very reliable. We instead throw and catch a pointer to a struct value of
1612 // type 'struct __WasmLongjmpArgs', which is defined in Emscripten.
1613 Instruction
*LongjmpArgs
=
1614 IRB
.CreateCall(CatchF
, {IRB
.getInt32(WebAssembly::C_LONGJMP
)}, "thrown");
1616 IRB
.CreateConstGEP2_32(LongjmpArgsTy
, LongjmpArgs
, 0, 0, "env_gep");
1618 IRB
.CreateConstGEP2_32(LongjmpArgsTy
, LongjmpArgs
, 0, 1, "val_gep");
1619 // void *env = __wasm_longjmp_args.env;
1620 Instruction
*Env
= IRB
.CreateLoad(IRB
.getPtrTy(), EnvField
, "env");
1621 // int val = __wasm_longjmp_args.val;
1622 Instruction
*Val
= IRB
.CreateLoad(IRB
.getInt32Ty(), ValField
, "val");
1624 // %label = __wasm_setjmp_test(%env, functionInvocatinoId);
1626 // __wasm_longjmp(%env, %val)
1627 // catchret to %setjmp.dispatch
1628 BasicBlock
*ThenBB
= BasicBlock::Create(C
, "if.then", &F
);
1629 BasicBlock
*EndBB
= BasicBlock::Create(C
, "if.end", &F
);
1630 Value
*EnvP
= IRB
.CreateBitCast(Env
, getAddrPtrType(&M
), "env.p");
1631 Value
*Label
= IRB
.CreateCall(WasmSetjmpTestF
, {EnvP
, FunctionInvocationId
},
1632 OperandBundleDef("funclet", CatchPad
), "label");
1633 Value
*Cmp
= IRB
.CreateICmpEQ(Label
, IRB
.getInt32(0));
1634 IRB
.CreateCondBr(Cmp
, ThenBB
, EndBB
);
1636 IRB
.SetInsertPoint(ThenBB
);
1637 CallInst
*WasmLongjmpCI
= IRB
.CreateCall(
1638 WasmLongjmpF
, {Env
, Val
}, OperandBundleDef("funclet", CatchPad
));
1639 IRB
.CreateUnreachable();
1641 IRB
.SetInsertPoint(EndBB
);
1642 // Jump to setjmp.dispatch block
1643 IRB
.CreateCatchRet(CatchPad
, SetjmpDispatchBB
);
1645 // Go back to setjmp.dispatch BB
1648 // label 1: goto post-setjmp BB 1
1649 // label 2: goto post-setjmp BB 2
1651 // default: goto splitted next BB
1653 IRB
.SetInsertPoint(SetjmpDispatchBB
);
1654 PHINode
*LabelPHI
= IRB
.CreatePHI(IRB
.getInt32Ty(), 2, "label.phi");
1655 LabelPHI
->addIncoming(Label
, EndBB
);
1656 LabelPHI
->addIncoming(IRB
.getInt32(-1), Entry
);
1657 SwitchInst
*SI
= IRB
.CreateSwitch(LabelPHI
, OrigEntry
, SetjmpRetPHIs
.size());
1658 // -1 means no longjmp happened, continue normally (will hit the default
1659 // switch case). 0 means a longjmp that is not ours to handle, needs a
1660 // rethrow. Otherwise the index is the same as the index in P+1 (to avoid
1662 for (unsigned I
= 0; I
< SetjmpRetPHIs
.size(); I
++) {
1663 SI
->addCase(IRB
.getInt32(I
+ 1), SetjmpRetPHIs
[I
]->getParent());
1664 SetjmpRetPHIs
[I
]->addIncoming(Val
, SetjmpDispatchBB
);
1667 // Convert all longjmpable call instructions to invokes that unwind to the
1668 // newly created catch.dispatch.longjmp BB.
1669 SmallVector
<CallInst
*, 64> LongjmpableCalls
;
1670 for (auto *BB
= &*F
.begin(); BB
; BB
= BB
->getNextNode()) {
1671 for (auto &I
: *BB
) {
1672 auto *CI
= dyn_cast
<CallInst
>(&I
);
1675 const Value
*Callee
= CI
->getCalledOperand();
1676 if (!canLongjmp(Callee
))
1678 if (isEmAsmCall(Callee
))
1679 report_fatal_error("Cannot use EM_ASM* alongside setjmp/longjmp in " +
1681 ". Please consider using EM_JS, or move the "
1682 "EM_ASM into another function.",
1684 // This is __wasm_longjmp() call we inserted in this function, which
1685 // rethrows the longjmp when the longjmp does not correspond to one of
1686 // setjmps in this function. We should not convert this call to an invoke.
1687 if (CI
== WasmLongjmpCI
)
1689 LongjmpableCalls
.push_back(CI
);
1693 SmallDenseMap
<BasicBlock
*, SmallSetVector
<BasicBlock
*, 4>, 4>
1694 UnwindDestToNewPreds
;
1695 for (auto *CI
: LongjmpableCalls
) {
1696 // Even if the callee function has attribute 'nounwind', which is true for
1697 // all C functions, it can longjmp, which means it can throw a Wasm
1699 CI
->removeFnAttr(Attribute::NoUnwind
);
1700 if (Function
*CalleeF
= CI
->getCalledFunction())
1701 CalleeF
->removeFnAttr(Attribute::NoUnwind
);
1703 // Change it to an invoke and make it unwind to the catch.dispatch.longjmp
1704 // BB. If the call is enclosed in another catchpad/cleanuppad scope, unwind
1705 // to its parent pad's unwind destination instead to preserve the scope
1706 // structure. It will eventually unwind to the catch.dispatch.longjmp.
1707 SmallVector
<OperandBundleDef
, 1> Bundles
;
1708 BasicBlock
*UnwindDest
= nullptr;
1709 if (auto Bundle
= CI
->getOperandBundle(LLVMContext::OB_funclet
)) {
1710 Instruction
*FromPad
= cast
<Instruction
>(Bundle
->Inputs
[0]);
1711 while (!UnwindDest
) {
1712 if (auto *CPI
= dyn_cast
<CatchPadInst
>(FromPad
)) {
1713 UnwindDest
= CPI
->getCatchSwitch()->getUnwindDest();
1716 if (auto *CPI
= dyn_cast
<CleanupPadInst
>(FromPad
)) {
1717 // getCleanupRetUnwindDest() can return nullptr when
1718 // 1. This cleanuppad's matching cleanupret uwninds to caller
1719 // 2. There is no matching cleanupret because it ends with
1721 // In case of 2, we need to traverse the parent pad chain.
1722 UnwindDest
= getCleanupRetUnwindDest(CPI
);
1723 Value
*ParentPad
= CPI
->getParentPad();
1724 if (isa
<ConstantTokenNone
>(ParentPad
))
1726 FromPad
= cast
<Instruction
>(ParentPad
);
1731 UnwindDest
= CatchDispatchLongjmpBB
;
1732 // Because we are changing a longjmpable call to an invoke, its unwind
1733 // destination can be an existing EH pad that already have phis, and the BB
1734 // with the newly created invoke will become a new predecessor of that EH
1735 // pad. In this case we need to add the new predecessor to those phis.
1736 UnwindDestToNewPreds
[UnwindDest
].insert(CI
->getParent());
1737 changeToInvokeAndSplitBasicBlock(CI
, UnwindDest
);
1740 SmallVector
<Instruction
*, 16> ToErase
;
1741 for (auto &BB
: F
) {
1742 if (auto *CSI
= dyn_cast
<CatchSwitchInst
>(BB
.getFirstNonPHI())) {
1743 if (CSI
!= CatchSwitchLongjmp
&& CSI
->unwindsToCaller()) {
1744 IRB
.SetInsertPoint(CSI
);
1745 ToErase
.push_back(CSI
);
1746 auto *NewCSI
= IRB
.CreateCatchSwitch(CSI
->getParentPad(),
1747 CatchDispatchLongjmpBB
, 1);
1748 NewCSI
->addHandler(*CSI
->handler_begin());
1749 NewCSI
->takeName(CSI
);
1750 CSI
->replaceAllUsesWith(NewCSI
);
1754 if (auto *CRI
= dyn_cast
<CleanupReturnInst
>(BB
.getTerminator())) {
1755 if (CRI
->unwindsToCaller()) {
1756 IRB
.SetInsertPoint(CRI
);
1757 ToErase
.push_back(CRI
);
1758 IRB
.CreateCleanupRet(CRI
->getCleanupPad(), CatchDispatchLongjmpBB
);
1763 for (Instruction
*I
: ToErase
)
1764 I
->eraseFromParent();
1766 // Add entries for new predecessors to phis in unwind destinations. We use
1767 // 'undef' as a placeholder value. We should make sure the phis have a valid
1768 // set of predecessors before running SSAUpdater, because SSAUpdater
1769 // internally can use existing phis to gather predecessor info rather than
1770 // scanning the actual CFG (See FindPredecessorBlocks in SSAUpdater.cpp for
1772 for (auto &[UnwindDest
, NewPreds
] : UnwindDestToNewPreds
) {
1773 for (PHINode
&PN
: UnwindDest
->phis()) {
1774 for (auto *NewPred
: NewPreds
) {
1775 assert(PN
.getBasicBlockIndex(NewPred
) == -1);
1776 PN
.addIncoming(UndefValue::get(PN
.getType()), NewPred
);
1781 // For unwind destinations for newly added invokes to longjmpable functions,
1782 // calculate incoming values for the newly added predecessors using
1783 // SSAUpdater. We add existing values in the phis to SSAUpdater as available
1784 // values and let it calculate what the value should be at the end of new
1786 for (auto &[UnwindDest
, NewPreds
] : UnwindDestToNewPreds
) {
1787 for (PHINode
&PN
: UnwindDest
->phis()) {
1789 SSA
.Initialize(PN
.getType(), PN
.getName());
1790 for (unsigned Idx
= 0, E
= PN
.getNumIncomingValues(); Idx
!= E
; ++Idx
) {
1791 if (NewPreds
.contains(PN
.getIncomingBlock(Idx
)))
1793 Value
*V
= PN
.getIncomingValue(Idx
);
1794 if (auto *II
= dyn_cast
<InvokeInst
>(V
))
1795 SSA
.AddAvailableValue(II
->getNormalDest(), II
);
1796 else if (auto *I
= dyn_cast
<Instruction
>(V
))
1797 SSA
.AddAvailableValue(I
->getParent(), I
);
1799 SSA
.AddAvailableValue(PN
.getIncomingBlock(Idx
), V
);
1801 for (auto *NewPred
: NewPreds
)
1802 PN
.setIncomingValueForBlock(NewPred
, SSA
.GetValueAtEndOfBlock(NewPred
));
1803 assert(PN
.isComplete());