From 0a1fc66d43dad587dfaf3d098cb2c3c6ba75c5cd Mon Sep 17 00:00:00 2001 From: Yuri Iozzelli Date: Tue, 19 Nov 2024 15:06:04 +0100 Subject: [PATCH] feat: exception handling rework: unwind argument as integer. Instead of using void* to represent the unwind argument, switch to using an int. The argument was really just an int all along ,and we were pretending it was a pointer to conform to the ABI. Future changes related to Address Spaces will require changes to the ABI anyway (the pointer would need an address space, probably multiple), so the least painful option is to just use int. --- clang/lib/CodeGen/CGException.cpp | 49 +++++------- clang/lib/CodeGen/CGObjCRuntime.cpp | 5 ++ clang/lib/CodeGen/CodeGenFunction.h | 2 +- clang/lib/CodeGen/ItaniumCXXABI.cpp | 43 +++++++--- compiler-rt/lib/asan/asan_interceptors.cpp | 7 +- compiler-rt/lib/asan/asan_interceptors.h | 2 +- libcxx/include/exception | 9 ++- .../runtime/exception_pointer_cxxabi.ipp | 2 +- libcxxabi/include/cxxabi.h | 14 +++- libcxxabi/src/cxa_cheerp.cpp | 80 ++++++++++++++----- llvm/lib/CheerpUtils/InvokeWrapping.cpp | 16 +--- 11 files changed, 149 insertions(+), 80 deletions(-) diff --git a/clang/lib/CodeGen/CGException.cpp b/clang/lib/CodeGen/CGException.cpp index d5673a4c23c7..80c9a48f66cc 100644 --- a/clang/lib/CodeGen/CGException.cpp +++ b/clang/lib/CodeGen/CGException.cpp @@ -62,8 +62,12 @@ static llvm::FunctionCallee getSehTryEndFn(CodeGenModule &CGM) { static llvm::FunctionCallee getUnexpectedFn(CodeGenModule &CGM) { // void __cxa_call_unexpected(void *thrown_exception); + llvm::Type* ArgTy = CGM.Int8PtrTy; + if (CGM.getLangOpts().Cheerp) { + ArgTy = CGM.Int32Ty; + } llvm::FunctionType *FTy = - llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); + llvm::FunctionType::get(CGM.VoidTy, ArgTy, /*isVarArg=*/false); return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); } @@ -96,8 +100,12 @@ llvm::FunctionCallee CodeGenModule::getTerminateFn() { static llvm::FunctionCallee getCatchallRethrowFn(CodeGenModule &CGM, StringRef Name) { + llvm::Type* ArgTy = CGM.Int8PtrTy; + if (CGM.getLangOpts().Cheerp) { + ArgTy = CGM.Int32Ty; + } llvm::FunctionType *FTy = - llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); + llvm::FunctionType::get(CGM.VoidTy, ArgTy, /*isVarArg=*/false); return CGM.CreateRuntimeFunction(FTy, Name); } @@ -460,9 +468,13 @@ void CodeGenFunction::EmitTypedPtrExprToExn(const Expr *e, Address addr) { Address CodeGenFunction::getExceptionSlot() { + llvm::Type* Ty = Int8PtrTy; + if(CGM.getLangOpts().Cheerp) { + Ty = Int32Ty; + } if (!ExceptionSlot) - ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot"); - return Address(ExceptionSlot, Int8PtrTy, getPointerAlign()); + ExceptionSlot = CreateTempAlloca(Ty, "exn.slot"); + return Address(ExceptionSlot, Ty, getPointerAlign()); } Address CodeGenFunction::getEHSelectorSlot() { @@ -878,14 +890,6 @@ llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { LPadInst = Builder.CreateLandingPad(LPadTy, 0); llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0); - if(!CGM.getTarget().isByteAddressable()) { - if (CGM.getTarget().getTriple().isCheerpWasm()) { - LPadExn = Builder.CreateIntToPtr(LPadExn, Int8PtrTy); - } else { - llvm::Function *MakeReg = CGM.getIntrinsic(llvm::Intrinsic::cheerp_make_regular, {Int8PtrTy, Int8PtrTy}); - LPadExn = Builder.CreateCall(MakeReg, {llvm::ConstantPointerNull::get(Int8PtrTy), LPadExn}); - } - } Builder.CreateStore(LPadExn, getExceptionSlot()); llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1); Builder.CreateStore(LPadSel, getEHSelectorSlot()); @@ -1538,6 +1542,11 @@ void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) { // If there's a begin-catch function, call it. if (BeginCatchFn) { exn = CGF.getExceptionFromSlot(); + // In Cheerp Exn has type int, but the ObjC runtime expects void*. + // Just add an inttoptr here, we are doing it just to pass tests + if (CGF.getLangOpts().Cheerp && CGF.getLangOpts().ObjC) { + exn = CGF.Builder.CreateIntToPtr(exn, CGF.VoidPtrTy); + } CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn); } @@ -1584,14 +1593,6 @@ llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { llvm::Value *Exn = nullptr; if (getLangOpts().CPlusPlus) { Exn = Builder.CreateExtractValue(LPadInst, 0); - if(!CGM.getTarget().isByteAddressable()) { - if (CGM.getTarget().getTriple().isCheerpWasm()) { - Exn = Builder.CreateIntToPtr(Exn, Int8PtrTy); - } else { - llvm::Function *MakeReg = CGM.getIntrinsic(llvm::Intrinsic::cheerp_make_regular, {Int8PtrTy, Int8PtrTy}); - Exn = Builder.CreateCall(MakeReg, {llvm::ConstantPointerNull::get(Int8PtrTy), Exn}); - } - } } llvm::CallInst *terminateCall = CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); @@ -1692,14 +1693,6 @@ llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) { llvm::Type *LPadType = GetLandingPadTy(); llvm::Value *LPadVal = llvm::UndefValue::get(LPadType); - if (!CGM.getTarget().isByteAddressable()) { - if (CGM.getTarget().getTriple().isCheerpWasm()) { - Exn = Builder.CreatePtrToInt(Exn, Int32Ty); - } else { - llvm::Function *PtrOffset = CGM.getIntrinsic(llvm::Intrinsic::cheerp_pointer_offset, {Int8PtrTy}); - Exn = Builder.CreateCall(PtrOffset, Exn); - } - } LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val"); LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val"); Builder.CreateResume(LPadVal); diff --git a/clang/lib/CodeGen/CGObjCRuntime.cpp b/clang/lib/CodeGen/CGObjCRuntime.cpp index fbf6d21bd407..16738f25a86a 100644 --- a/clang/lib/CodeGen/CGObjCRuntime.cpp +++ b/clang/lib/CodeGen/CGObjCRuntime.cpp @@ -241,6 +241,11 @@ void CGObjCRuntime::EmitTryCatchStmt(CodeGenFunction &CGF, } llvm::Value *RawExn = CGF.getExceptionFromSlot(); + // In Cheerp Exn has type int, but the ObjC runtime expects void*. + // Just add an inttoptr here, we are doing it just to pass tests + if (CGM.getLangOpts().Cheerp) { + RawExn = CGF.Builder.CreateIntToPtr(RawExn, CGM.VoidPtrTy); + } // Enter the catch. llvm::Value *Exn = RawExn; diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index cff3c2290f84..3f9e4ba869e9 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -2010,7 +2010,7 @@ class CodeGenFunction : public CodeGenTypeCache { /// Get the named landingpad/resume type (Cheerp) llvm::StructType* GetLandingPadTy() { - if (getTarget().isByteAddressable()) { + if(!getLangOpts().Cheerp) { return llvm::StructType::get(Int8PtrTy, Int32Ty); } auto* Ret = llvm::StructType::getTypeByName(CGM.getLLVMContext(), "struct._ZN10__cxxabiv119__cheerp_landingpadE"); diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp index 1f57a56dd1a0..29491c7e049c 100644 --- a/clang/lib/CodeGen/ItaniumCXXABI.cpp +++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -4823,12 +4823,20 @@ void ItaniumCXXABI::emitCXXStructor(GlobalDecl GD) { } } -static llvm::FunctionCallee getBeginCatchFn(CodeGenModule &CGM) { +static llvm::FunctionCallee getBeginCatchFn(CodeGenModule &CGM, bool asmjs) { + llvm::Type* RetTy = CGM.Int8PtrTy; + llvm::Type* ArgTy = CGM.Int8PtrTy; + const char* name = "__cxa_begin_catch"; + if (CGM.getLangOpts().Cheerp) { + ArgTy = CGM.Int32Ty; + if (asmjs) + name = "__cxa_begin_catch_wasm"; + } // void *__cxa_begin_catch(void*); llvm::FunctionType *FTy = llvm::FunctionType::get( - CGM.Int8PtrTy, CGM.Int8PtrTy, /*isVarArg=*/false); + RetTy, ArgTy, /*isVarArg=*/false); - return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch"); + return CGM.CreateRuntimeFunction(FTy, name); } static llvm::FunctionCallee getEndCatchFn(CodeGenModule &CGM) { @@ -4839,12 +4847,20 @@ static llvm::FunctionCallee getEndCatchFn(CodeGenModule &CGM) { return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch"); } -static llvm::FunctionCallee getGetExceptionPtrFn(CodeGenModule &CGM) { +static llvm::FunctionCallee getGetExceptionPtrFn(CodeGenModule &CGM, bool asmjs) { + llvm::Type* RetTy = CGM.Int8PtrTy; + llvm::Type* ArgTy = CGM.Int8PtrTy; + const char* name = "__cxa_get_exception_ptr"; + if (CGM.getLangOpts().Cheerp) { + ArgTy = CGM.Int32Ty; + if (asmjs) + name = "__cxa_get_exception_ptr_wasm"; + } // void *__cxa_get_exception_ptr(void*); llvm::FunctionType *FTy = llvm::FunctionType::get( - CGM.Int8PtrTy, CGM.Int8PtrTy, /*isVarArg=*/false); + RetTy, ArgTy, /*isVarArg=*/false); - return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr"); + return CGM.CreateRuntimeFunction(FTy, name); } namespace { @@ -4882,8 +4898,9 @@ namespace { static llvm::Value *CallBeginCatch(CodeGenFunction &CGF, llvm::Value *Exn, bool EndMightThrow) { + bool asmjs = CGF.CurFn->getSection() == "asmjs"; llvm::CallInst *call = - CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn); + CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM, asmjs), Exn); CGF.EHStack.pushCleanup(NormalAndEHCleanup, EndMightThrow); @@ -5035,10 +5052,11 @@ static void InitCatchParam(CodeGenFunction &CGF, return; } + bool asmjs = CGF.CurFn->getSection() == "asmjs"; // We have to call __cxa_get_exception_ptr to get the adjusted // pointer before copying. llvm::CallInst *rawAdjustedExn = - CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn); + CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM, asmjs), Exn); // Cast that to the appropriate type. Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy), @@ -5116,8 +5134,13 @@ void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF, /// This code is used only in C++. static llvm::FunctionCallee getClangCallTerminateFn(CodeGenModule &CGM) { ASTContext &C = CGM.getContext(); + bool asmjs = CGM.getTriple().isCheerpWasm(); + CanQualType ArgTy = C.getPointerType(C.CharTy); + if (CGM.getLangOpts().Cheerp) { + ArgTy = C.IntTy; + } const CGFunctionInfo &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( - C.VoidTy, {C.getPointerType(C.CharTy)}); + C.VoidTy, {ArgTy}); llvm::FunctionType *fnTy = CGM.getTypes().GetFunctionType(FI); llvm::FunctionCallee fnRef = CGM.CreateRuntimeFunction( fnTy, "__clang_call_terminate", llvm::AttributeList(), /*Local=*/true); @@ -5149,7 +5172,7 @@ static llvm::FunctionCallee getClangCallTerminateFn(CodeGenModule &CGM) { llvm::Value *exn = &*fn->arg_begin(); // Call __cxa_begin_catch(exn). - llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn); + llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM, asmjs), exn); catchCall->setDoesNotThrow(); catchCall->setCallingConv(CGM.getRuntimeCC()); diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp index 962e191d0a2e..613290b1da89 100644 --- a/compiler-rt/lib/asan/asan_interceptors.cpp +++ b/compiler-rt/lib/asan/asan_interceptors.cpp @@ -349,8 +349,13 @@ extern "C" void __cxa_throw_wasm(void *a, void *b, void *c) { #endif +#ifdef __CHEERP__ +#define UNWIND_ARG_TYPE int +#else +#define UNWIND_ARG_TYPE void* +#endif #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION -INTERCEPTOR(void, __cxa_rethrow_primary_exception, void *a) { +INTERCEPTOR(void, __cxa_rethrow_primary_exception, UNWIND_ARG_TYPE a) { __asan_handle_no_return(); REAL(__cxa_rethrow_primary_exception)(a); } diff --git a/compiler-rt/lib/asan/asan_interceptors.h b/compiler-rt/lib/asan/asan_interceptors.h index 60707f05d0f8..487342f0f1b5 100644 --- a/compiler-rt/lib/asan/asan_interceptors.h +++ b/compiler-rt/lib/asan/asan_interceptors.h @@ -172,7 +172,7 @@ DECLARE_REAL(size_t, mbstowcs, wchar_t *dest, const char *src, size_t len); DECLARE_REAL(size_t, mbsrtowcs, wchar_t *dest, const char **src, size_t len, void *ps); extern "C" void __cheerp___cxa_throw(void *, void *, void *); -extern "C" void __cheerp___cxa_rethrow_primary_exception(void*); +extern "C" void __cheerp___cxa_rethrow_primary_exception(int); #endif // SANITIZER_CHEERPWASM # if !SANITIZER_APPLE diff --git a/libcxx/include/exception b/libcxx/include/exception index 98b355f9bdd9..6e8e53073375 100644 --- a/libcxx/include/exception +++ b/libcxx/include/exception @@ -187,7 +187,12 @@ _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr); class _LIBCPP_TYPE_VIS exception_ptr { - void* __ptr_; +#ifdef __CHEERP__ + unsigned +#else + void* +#endif + __ptr_; public: _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {} _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {} @@ -197,7 +202,7 @@ public: ~exception_ptr() _NOEXCEPT; _LIBCPP_INLINE_VISIBILITY explicit operator bool() const _NOEXCEPT - {return __ptr_ != nullptr;} + {return __ptr_ != 0;} friend _LIBCPP_INLINE_VISIBILITY bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp index 33aa94502b73..3fa6d1caa891 100644 --- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp +++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp @@ -47,7 +47,7 @@ _LIBCPP_NORETURN void nested_exception::rethrow_nested() const { - if (__ptr_ == nullptr) + if (__ptr_ == 0) terminate(); rethrow_exception(__ptr_); } diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h index 5a240534f809..171befec93bc 100644 --- a/libcxxabi/include/cxxabi.h +++ b/libcxxabi/include/cxxabi.h @@ -146,15 +146,21 @@ extern _LIBCXXABI_FUNC_VIS char *__cxa_demangle(const char *mangled_name, char *output_buffer, size_t *length, int *status); +#ifdef __CHEERP__ +#define UNWIND_TY int +#else +#define UNWIND_TY void* +#endif + // Apple additions to support C++ 0x exception_ptr class // These are primitives to wrap a smart pointer around an exception object -extern _LIBCXXABI_FUNC_VIS void *__cxa_current_primary_exception() throw(); +extern _LIBCXXABI_FUNC_VIS UNWIND_TY __cxa_current_primary_exception() throw(); extern _LIBCXXABI_FUNC_VIS void -__cxa_rethrow_primary_exception(void *primary_exception); +__cxa_rethrow_primary_exception(UNWIND_TY primary_exception); extern _LIBCXXABI_FUNC_VIS void -__cxa_increment_exception_refcount(void *primary_exception) throw(); +__cxa_increment_exception_refcount(UNWIND_TY primary_exception) throw(); extern _LIBCXXABI_FUNC_VIS void -__cxa_decrement_exception_refcount(void *primary_exception) throw(); +__cxa_decrement_exception_refcount(UNWIND_TY primary_exception) throw(); // Apple extension to support std::uncaught_exception() extern _LIBCXXABI_FUNC_VIS bool __cxa_uncaught_exception() throw(); diff --git a/libcxxabi/src/cxa_cheerp.cpp b/libcxxabi/src/cxa_cheerp.cpp index ebfa9a409890..6fa37c0e3918 100644 --- a/libcxxabi/src/cxa_cheerp.cpp +++ b/libcxxabi/src/cxa_cheerp.cpp @@ -292,25 +292,25 @@ static int uncaughtExceptions = 0; // current in-flight non-native (e.g. from js) exception static client::Object* curNonNativeException = nullptr; -static Exception* find_exception_from_unwind_ptr(void* unwind) +static Exception* find_exception_from_unwind_ptr(int unwind) { return Exception::allocator()->get_object(unwind); } extern "C" { -void __cxa_decrement_exception_refcount(void* obj) noexcept +void __cxa_decrement_exception_refcount(int obj) noexcept { - if(obj != nullptr) + if(obj != 0) { Exception* ex = find_exception_from_unwind_ptr(obj); if(ex->decRef() == 0) ex->deallocate(); } } -void __cxa_increment_exception_refcount(void* obj) noexcept +void __cxa_increment_exception_refcount(int obj) noexcept { - if(obj != nullptr) + if(obj != 0) { find_exception_from_unwind_ptr(obj)->incRef(); } @@ -357,7 +357,7 @@ __cxa_throw_wasm(void *thrown_object, std::type_info *tinfo, void (*dest)(void * __attribute((noinline)) void* -__cxa_begin_catch(void* unwind_arg) noexcept +__cxa_begin_catch(int unwind_arg) noexcept { Exception* ex = find_exception_from_unwind_ptr(unwind_arg); // Increment the handler count, removing the flag about being rethrown @@ -374,13 +374,44 @@ __cxa_begin_catch(void* unwind_arg) noexcept return ex->adjustedPtr; } +#ifdef __ASMJS__ +int +__cxa_begin_catch_wasm_adapter(int unwind_arg) noexcept +{ + return __builtin_cheerp_pointer_offset(__cxa_begin_catch(unwind_arg)); +} +[[cheerp::wasm]] __attribute((noinline)) void* -__cxa_get_exception_ptr(void* unwind_arg) noexcept +__cxa_begin_catch_wasm(int unwind_arg) noexcept +{ + return reinterpret_cast(__cxa_begin_catch_wasm_adapter(unwind_arg)); +} +#endif + +__attribute((noinline)) +void* +__cxa_get_exception_ptr(int unwind_arg) noexcept { return find_exception_from_unwind_ptr(unwind_arg)->adjustedPtr; } +#ifdef __ASMJS__ +int +__cxa_get_exception_ptr_wasm_adapter(int unwind_arg) noexcept +{ + Exception* ex = find_exception_from_unwind_ptr(unwind_arg); + return __builtin_cheerp_pointer_offset(ex->adjustedPtr); +} +[[cheerp::wasm]] +__attribute((noinline)) +void* +__cxa_get_exception_ptr_wasm(int unwind_arg) noexcept +{ + return reinterpret_cast(__cxa_get_exception_ptr_wasm_adapter(unwind_arg)); +} +#endif + __attribute((noinline)) void __cxa_end_catch() noexcept { Exception* ex = thrown_exceptions; @@ -415,7 +446,8 @@ void __cxa_end_catch() noexcept { dep->deallocate(); } // Destroy the primary exception only if its refCount goes to 0 - __cxa_decrement_exception_refcount(ex); + if(ex->decRef() == 0) + ex->deallocate(); } } } @@ -430,8 +462,8 @@ void __cxa_rethrow() { [[noreturn]] __attribute((noinline)) -void __cxa_resume(void* val) { - if (reinterpret_cast(val) == 0) +void __cxa_resume(int val) { + if (val == 0) { auto* e = curNonNativeException; curNonNativeException = nullptr; @@ -441,23 +473,23 @@ void __cxa_resume(void* val) { __builtin_cheerp_throw(ex->jsObj); } -void* __cxa_current_primary_exception() noexcept +int __cxa_current_primary_exception() noexcept { Exception* ex = thrown_exceptions; if(ex == nullptr) - return ex; + return 0; if(ex->primary != nullptr) ex = ex->primary; - __cxa_increment_exception_refcount(ex); - return ex; + ex->incRef(); + return __builtin_cheerp_pointer_offset(ex); } -[[noreturn]] -void ____cxa_rethrow_primary_exception(void* obj) +[[noreturn]] +void ____cxa_rethrow_primary_exception(int obj) { Exception* ex = find_exception_from_unwind_ptr(obj); - __cxa_increment_exception_refcount(ex); + ex->incRef(); Exception* dep = Exception::allocate(ex->objBase, ex->objOffset, ex->tinfo, nullptr, ex); do_throw(dep); } @@ -467,6 +499,16 @@ int __cxa_uncaught_exceptions() return uncaughtExceptions; } + +// TODO:figure out what this should actually do +__attribute__((noreturn)) +_LIBCXXABI_FUNC_VIS void +__cxa_call_unexpected(int unwind_arg) +{ + __terminate_impl(); +} + + struct __cheerp_clause { std::type_info* val; @@ -574,8 +616,8 @@ __gxx_personality_v0 return lp; } -__attribute__ ((__weak__, alias("____cxa_rethrow_primary_exception"))) void __cxa_rethrow_primary_exception(void* obj); -__attribute__ ((alias("____cxa_rethrow_primary_exception"))) void __cheerp___cxa_rethrow_primary_exception(void* obj); +__attribute__ ((__weak__, alias("____cxa_rethrow_primary_exception"))) void __cxa_rethrow_primary_exception(int obj); +__attribute__ ((alias("____cxa_rethrow_primary_exception"))) void __cheerp___cxa_rethrow_primary_exception(int obj); __attribute__ ((__weak__, alias("____cxa_throw"))) void __cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)); __attribute__ ((alias("____cxa_throw"))) void __cheerp___cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)); } diff --git a/llvm/lib/CheerpUtils/InvokeWrapping.cpp b/llvm/lib/CheerpUtils/InvokeWrapping.cpp index c856327aa55c..dfcdd7cf2e11 100644 --- a/llvm/lib/CheerpUtils/InvokeWrapping.cpp +++ b/llvm/lib/CheerpUtils/InvokeWrapping.cpp @@ -56,9 +56,8 @@ static GlobalVariable* getOrInsertLPHelperGlobal(Module& M) assert(Ty); GlobalVariable* G = cast(M.getOrInsertGlobal("__cheerpLandingPadHelperGlobal", Ty, [&M, Ty]() { - auto* g = new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, UndefValue::get(Ty)); - g->setName("__cheerpLandingPadHelperGlobal"); - g->setLinkage(GlobalVariable::ExternalLinkage); + unsigned AS = 0; + auto* g = new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, UndefValue::get(Ty), "__cheerpLandingPadHelperGlobal", nullptr, GlobalVariable::NotThreadLocal, AS); if (Ty->hasAsmJS()) g->setSection("asmjs"); return g; @@ -71,10 +70,8 @@ static GlobalVariable* getOrInsertCondHelperGlobal(Module& M) auto* Ty = IntegerType::get(M.getContext(), 32); GlobalVariable* G = cast(M.getOrInsertGlobal("__cheerpInvokeHelperGlobal", Ty, [&M, Ty]() { - auto* g = new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, UndefValue::get(Ty)); - g->setName("__cheerpInvokeHelperGlobal"); - g->setLinkage(GlobalVariable::ExternalLinkage); GlobalVariable* LPHelper = getOrInsertLPHelperGlobal(M); + auto* g = new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, UndefValue::get(Ty), "__cheerpInvokeHelperGlobal", nullptr, GlobalVariable::NotThreadLocal, LPHelper->getAddressSpace()); g->setSection(LPHelper->getSection()); return g; })); @@ -363,18 +360,11 @@ static Function* wrapInvoke(Module& M, InvokeInst& IV, DenseSet& T static Function* wrapResume(Module& M, ResumeInst* RS) { - PointerType* Int8PtrTy = Type::getInt8Ty(M.getContext())->getPointerTo(0); Function* CxaResume = M.getFunction("__cxa_resume"); assert(CxaResume); IRBuilder<> Builder(RS); Value* LP = RS->getOperand(0); Value* Val = Builder.CreateExtractValue(LP, {0}); - if (Triple(M.getTargetTriple()).isCheerpWasm()) { - Val = Builder.CreateIntToPtr(Val, Int8PtrTy); - } else { - llvm::Function *MakeReg = Intrinsic::getDeclaration(&M, Intrinsic::cheerp_make_regular, {Int8PtrTy, Int8PtrTy}); - Val = Builder.CreateCall(MakeReg, {ConstantPointerNull::get(Int8PtrTy), Val}); - } Value* Call = Builder.CreateCall(CxaResume->getFunctionType(), CxaResume, Val); RS->replaceAllUsesWith(Call); Builder.CreateUnreachable();