LLVM 19.0.0git
Macros | Functions | Variables
WebAssemblyLowerEmscriptenEHSjLj.cpp File Reference

This file lowers exception-related instructions and setjmp/longjmp function calls to use Emscripten's library functions. More...

#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "WebAssembly.h"
#include "WebAssemblyTargetMachine.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/WasmEHFuncInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IntrinsicsWebAssembly.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/SSAUpdater.h"
#include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
#include <set>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "wasm-lower-em-ehsjlj"
 

Functions

 INITIALIZE_PASS (WebAssemblyLowerEmscriptenEHSjLj, DEBUG_TYPE, "WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp", false, false) ModulePass *llvm
 
static bool canThrow (const Value *V)
 
static GlobalVariablegetGlobalVariable (Module &M, Type *Ty, WebAssemblyTargetMachine &TM, const char *Name)
 
static std::string getSignature (FunctionType *FTy)
 
static FunctiongetEmscriptenFunction (FunctionType *Ty, const Twine &Name, Module *M)
 
static TypegetAddrIntType (Module *M)
 
static TypegetAddrPtrType (Module *M)
 
static ValuegetAddrSizeInt (Module *M, uint64_t C)
 
static bool canLongjmp (const Value *Callee)
 
static bool isEmAsmCall (const Value *Callee)
 
static bool containsLongjmpableCalls (const Function *F)
 
static void nullifySetjmp (Function *F)
 
static DebugLoc getOrCreateDebugLoc (const Instruction *InsertBefore, DISubprogram *SP)
 
static BasicBlockgetCleanupRetUnwindDest (const CleanupPadInst *CPI)
 

Variables

static cl::list< std::string > EHAllowlist ("emscripten-cxx-exceptions-allowed", cl::desc("The list of function names in which Emscripten-style " "exception handling is enabled (see emscripten " "EMSCRIPTEN_CATCHING_ALLOWED options)"), cl::CommaSeparated)
 

Detailed Description

This file lowers exception-related instructions and setjmp/longjmp function calls to use Emscripten's library functions.

The pass uses JavaScript's try and catch mechanism in case of Emscripten EH/SjLj and Wasm EH intrinsics in case of Emscripten SjLJ.

In detail, this pass does following things:

1) Assumes the existence of global variables: THREW, threwValue __THREW and threwValue are defined in compiler-rt in Emscripten. These variables are used for both exceptions and setjmp/longjmps. __THREW indicates whether an exception or a longjmp occurred or not. 0 means nothing occurred, 1 means an exception occurred, and other numbers mean a longjmp occurred. In the case of longjmp, THREW variable indicates the corresponding setjmp buffer the longjmp corresponds to. __threwValue is 0 for exceptions, and the argument to longjmp in case of longjmp.

2) We assume the existence of setThrew and setTempRet0/getTempRet0 functions at link time. setThrew exists in Emscripten's compiler-rt:

void setThrew(uintptr_t threw, int value) { if (THREW == 0) { THREW = threw; __threwValue = value; } }

Definition in file WebAssemblyLowerEmscriptenEHSjLj.cpp.

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "wasm-lower-em-ehsjlj"

Definition at line 283 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

Function Documentation

◆ canLongjmp()

static bool canLongjmp ( const Value Callee)
static

◆ canThrow()

static bool canThrow ( const Value V)
static

Definition at line 388 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

References F, and Name.

◆ containsLongjmpableCalls()

static bool containsLongjmpableCalls ( const Function F)
static

Definition at line 844 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

References canLongjmp(), F, and I.

◆ getAddrIntType()

static Type * getAddrIntType ( Module M)
static

◆ getAddrPtrType()

static Type * getAddrPtrType ( Module M)
static

Definition at line 470 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

◆ getAddrSizeInt()

static Value * getAddrSizeInt ( Module M,
uint64_t  C 
)
static

◆ getCleanupRetUnwindDest()

static BasicBlock * getCleanupRetUnwindDest ( const CleanupPadInst CPI)
static

Definition at line 1539 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

References llvm::Value::users().

◆ getEmscriptenFunction()

static Function * getEmscriptenFunction ( FunctionType Ty,
const Twine Name,
Module M 
)
static

◆ getGlobalVariable()

static GlobalVariable * getGlobalVariable ( Module M,
Type Ty,
WebAssemblyTargetMachine TM,
const char Name 
)
static

◆ getOrCreateDebugLoc()

static DebugLoc getOrCreateDebugLoc ( const Instruction InsertBefore,
DISubprogram SP 
)
static

◆ getSignature()

static std::string getSignature ( FunctionType FTy)
static

Definition at line 426 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

References llvm::erase_if(), and OS.

Referenced by llvm::DWARFUnitIndex::getFromHash().

◆ INITIALIZE_PASS()

INITIALIZE_PASS ( WebAssemblyLowerEmscriptenEHSjLj  ,
DEBUG_TYPE  ,
"WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp"  ,
false  ,
false   
)

Definition at line 380 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

◆ isEmAsmCall()

static bool isEmAsmCall ( const Value Callee)
static

Definition at line 673 of file WebAssemblyLowerEmscriptenEHSjLj.cpp.

◆ nullifySetjmp()

static void nullifySetjmp ( Function F)
static

Variable Documentation

◆ EHAllowlist

cl::list< std::string > EHAllowlist("emscripten-cxx-exceptions-allowed", cl::desc("The list of function names in which Emscripten-style " "exception handling is enabled (see emscripten " "EMSCRIPTEN_CATCHING_ALLOWED options)"), cl::CommaSeparated) ( "emscripten-cxx-exceptions-allowed"  ,
cl::desc("The list of function names in which Emscripten-style " "exception handling is enabled (see emscripten " "EMSCRIPTEN_CATCHING_ALLOWED options)")  ,
cl::CommaSeparated   
)
static