Skip to content

[WebAssembly] Call EH personality function instead of Unwind_CallPersonality - #209282

Merged
QuantumSegfault merged 6 commits into
llvm:mainfrom
QuantumSegfault:wasm-use-personality
Jul 29, 2026
Merged

QuantumSegfault merged 6 commits into
llvm:mainfrom
QuantumSegfault:wasm-use-personality

Conversation

@QuantumSegfault

@QuantumSegfault QuantumSegfault commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

On Wasm, it's not currently possibly to change which personality is attached to a given IR function. This is primarily because no matter what personality is attached, codegen creates a call to a Unwind_CallPersonality wrapper hard-coded to call c++abi's personality.

In order to allow alternative personalities without introducing bloat at each personality call site, this PR adjusts the ABI of the personality on Wasm. Unwind_CallPersonality is moved to libcxxabi under the name __gxx_wasm_personality_v0, with the "real" personality code renamed __gxx_personality_imp and called by __gxx_wasm_personality_v0 (after resetting the landing pad context).

The signature of EH personalities on Wasm is now int personality(void* exception_object) (where the int return is _Unwind_Reason_Code and the void* is _Unwind_Exception*), and personalities are expected to __wasm_lpad_context.selector = 0;.

Based on #175202

@QuantumSegfault
QuantumSegfault requested a review from aheejin July 13, 2026 19:42
@QuantumSegfault
QuantumSegfault requested review from a team as code owners July 13, 2026 19:42
@llvmorg-github-actions llvmorg-github-actions Bot added libc++abi libc++abi C++ Runtime Library. Not libc++. libunwind backend:WebAssembly clang:codegen IR generation bugs: mangling, exceptions, etc. llvm:codegen llvm:ir labels Jul 13, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-pgo
@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-libcxxabi
@llvm/pr-subscribers-clang-codegen
@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-backend-webassembly

Author: Demetrius Kanios (QuantumSegfault)

Changes

On Wasm, it's not currently possibly to change which personality is attached to a given IR function. This is primarily because no matter what personality is attached, codegen creates a call to a Unwind_CallPersonality wrapper hard-coded to call c++abi's personality.

In order to allow alternative personalities without introducing bloat at each personality call site, this PR adjusts the ABI of the personality on Wasm. Unwind_CallPersonality is moved to libcxxabi under the name __gxx_wasm_personality_v0, with the "real" personality code renamed __gxx_personality_imp and called by __gxx_wasm_personality_v0 (after resetting the landing pad context).

The signature of EH personalities on Wasm is now void personality(void* exception_object), and personalities are expected to __wasm_lpad_context.selector = 0;.

Based on #175202


Patch is 24.98 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/209282.diff

22 Files Affected:

  • (modified) clang/lib/CodeGen/CGException.cpp (+8-1)
  • (modified) libcxxabi/src/cxa_personality.cpp (+19-3)
  • (modified) libunwind/include/unwind.h (+4)
  • (added) libunwind/include/unwind_wasm.h (+27)
  • (modified) libunwind/src/Unwind-wasm.c (-39)
  • (modified) libunwind/src/config.h (+1-1)
  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.td (-4)
  • (modified) llvm/lib/CodeGen/WasmEHPrepare.cpp (+27-37)
  • (modified) llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.ll (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.mir (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll (+4-4)
  • (modified) llvm/test/CodeGen/WebAssembly/eh-lsda.ll (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/exception-legacy.ll (+2-2)
  • (modified) llvm/test/CodeGen/WebAssembly/exception-legacy.mir (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/exception.ll (+2-2)
  • (modified) llvm/test/CodeGen/WebAssembly/function-info.mir (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj-phi.ll (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj.ll (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/wasm-eh-em-sjlj-error.ll (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/wasm-eh-invalid-personality.ll (+1-1)
  • (modified) llvm/test/CodeGen/WebAssembly/wasm-eh-prepare.ll (+4-4)
  • (modified) llvm/test/CodeGen/WebAssembly/wasm-eh-sjlj-setjmp-within-catch.ll (+1-1)
diff --git a/clang/lib/CodeGen/CGException.cpp b/clang/lib/CodeGen/CGException.cpp
index 99dfaa80be429..25b57a0334413 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -265,7 +265,14 @@ const EHPersonality &EHPersonality::get(CodeGenFunction &CGF) {
 
 static llvm::FunctionCallee getPersonalityFn(CodeGenModule &CGM,
                                              const EHPersonality &Personality) {
-  return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
+  llvm::FunctionType *FTy;
+
+  if (Personality.isWasmPersonality()) {
+    FTy = llvm::FunctionType::get(CGM.VoidTy, {CGM.VoidPtrTy}, false);
+  } else {
+    FTy = llvm::FunctionType::get(CGM.Int32Ty, true);
+  }
+  return CGM.CreateRuntimeFunction(FTy,
                                    Personality.PersonalityFn,
                                    llvm::AttributeList(), /*Local=*/true);
 }
diff --git a/libcxxabi/src/cxa_personality.cpp b/libcxxabi/src/cxa_personality.cpp
index 6599f89740113..970cd0355ddf4 100644
--- a/libcxxabi/src/cxa_personality.cpp
+++ b/libcxxabi/src/cxa_personality.cpp
@@ -1011,9 +1011,7 @@ static inline void get_landing_pad(__cxa_catch_temp_type &dest,
 #endif
 }
 
-#ifdef __WASM_EXCEPTIONS__
-_Unwind_Reason_Code __gxx_personality_wasm0
-#elif defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
+#if (defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)) || defined(__WASM_EXCEPTIONS__)
 static _Unwind_Reason_Code __gxx_personality_imp
 #else
 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
@@ -1114,6 +1112,24 @@ __gxx_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame,
 }
 #endif
 
+
+#ifdef __WASM_EXCEPTIONS__
+extern "C" _LIBCXXABI_FUNC_VIS
+void __gxx_wasm_personality_v0(void* exception_ptr) {
+  struct _Unwind_Exception *exception_object =
+      (struct _Unwind_Exception *)exception_ptr;
+
+  // Reset the selector.
+  __wasm_lpad_context.selector = 0;
+
+  // Call personality function. Wasm does not have two-phase unwinding, so we
+  // only do the cleanup phase.
+  __gxx_personality_imp(
+      1, _UA_SEARCH_PHASE, exception_object->exception_class, exception_object,
+      (struct _Unwind_Context *)&__wasm_lpad_context);
+}
+#endif
+
 #else
 
 extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*, _Unwind_Context*);
diff --git a/libunwind/include/unwind.h b/libunwind/include/unwind.h
index b1775d3a3decc..93a9d92f327d3 100644
--- a/libunwind/include/unwind.h
+++ b/libunwind/include/unwind.h
@@ -61,6 +61,10 @@ typedef struct _Unwind_Context _Unwind_Context;   // opaque
 #include <unwind_itanium.h>
 #endif
 
+#if defined(__WASM_EXCEPTIONS__)
+#include <unwind_wasm.h>
+#endif
+
 typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
     (int version,
      _Unwind_Action actions,
diff --git a/libunwind/include/unwind_wasm.h b/libunwind/include/unwind_wasm.h
new file mode 100644
index 0000000000000..7bf3f30562bd8
--- /dev/null
+++ b/libunwind/include/unwind_wasm.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __WASM_UNWIND_H__
+#define __WASM_UNWIND_H__
+
+#include <threads.h>
+
+struct _Unwind_LandingPadContext {
+  // Input information to personality function
+  uintptr_t lpad_index; // landing pad index
+  uintptr_t lsda;       // LSDA address
+
+  // Output information computed by personality function
+  uintptr_t selector; // selector value
+};
+
+// Communication channel between compiler-generated user code and personality
+// function
+extern thread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
+
+#endif // __WASM_UNWIND_H__
diff --git a/libunwind/src/Unwind-wasm.c b/libunwind/src/Unwind-wasm.c
index c0ca9b775d244..81793b3f36060 100644
--- a/libunwind/src/Unwind-wasm.c
+++ b/libunwind/src/Unwind-wasm.c
@@ -19,47 +19,8 @@
 #include "unwind.h"
 #include <threads.h>
 
-_Unwind_Reason_Code __gxx_personality_wasm0(int version, _Unwind_Action actions,
-                                            uint64_t exceptionClass,
-                                            _Unwind_Exception *unwind_exception,
-                                            _Unwind_Context *context);
-
-struct _Unwind_LandingPadContext {
-  // Input information to personality function
-  uintptr_t lpad_index; // landing pad index
-  uintptr_t lsda;       // LSDA address
-
-  // Output information computed by personality function
-  uintptr_t selector; // selector value
-};
-
-// Communication channel between compiler-generated user code and personality
-// function
 thread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
 
-/// Calls to this function are in landing pads in compiler-generated user code.
-/// In other EH schemes, stack unwinding is done by libunwind library, which
-/// calls the personality function for each frame it lands. On the other hand,
-/// WebAssembly stack unwinding process is performed by a VM, and the
-/// personality function cannot be called from there. So the compiler inserts a
-/// call to this function in landing pads in the user code, which in turn calls
-/// the personality function.
-_Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
-  struct _Unwind_Exception *exception_object =
-      (struct _Unwind_Exception *)exception_ptr;
-  _LIBUNWIND_TRACE_API("_Unwind_CallPersonality(exception_object=%p)",
-                       (void *)exception_object);
-
-  // Reset the selector.
-  __wasm_lpad_context.selector = 0;
-
-  // Call personality function. Wasm does not have two-phase unwinding, so we
-  // only do the cleanup phase.
-  return __gxx_personality_wasm0(
-      1, _UA_SEARCH_PHASE, exception_object->exception_class, exception_object,
-      (struct _Unwind_Context *)&__wasm_lpad_context);
-}
-
 /// Called by __cxa_throw.
 _LIBUNWIND_EXPORT _Unwind_Reason_Code
 _Unwind_RaiseException(_Unwind_Exception *exception_object) {
diff --git a/libunwind/src/config.h b/libunwind/src/config.h
index f017403fa2234..fd1aba25e6822 100644
--- a/libunwind/src/config.h
+++ b/libunwind/src/config.h
@@ -75,7 +75,7 @@
   #define _LIBUNWIND_EXPORT
   #define _LIBUNWIND_HIDDEN
 #else
-  #if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX)
+  #if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX) && !defined(__wasm__)
     #define _LIBUNWIND_EXPORT __declspec(dllexport)
     #define _LIBUNWIND_HIDDEN
   #else
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index a7e45570c1b24..3745001b936f8 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -1849,9 +1849,6 @@ defset list<RuntimeLibcallImpl> SjLjExceptionHandlingLibcalls = {
   def _Unwind_SjLj_Unregister : RuntimeLibcallImpl<UNWIND_UNREGISTER>;
 }
 
-// Only used on wasm?
-def _Unwind_CallPersonality : RuntimeLibcallImpl<UNWIND_CALL_PERSONALITY>;
-
 // Used on OpenBSD
 def __stack_smash_handler : RuntimeLibcallImpl<STACK_SMASH_HANDLER>;
 
@@ -3567,7 +3564,6 @@ def WasmSystemLibrary
       (add DefaultRuntimeLibcallImpls, Int128RTLibcalls,
            CompilerRTOnlyInt64Libcalls, CompilerRTOnlyInt128Libcalls,
            exp10f, exp10,
-           _Unwind_CallPersonality,
            emscripten_return_address,
            LibcallImpls<(add __small_printf,
                              __small_sprintf,
diff --git a/llvm/lib/CodeGen/WasmEHPrepare.cpp b/llvm/lib/CodeGen/WasmEHPrepare.cpp
index b83bcf67716f9..363e7221f7118 100644
--- a/llvm/lib/CodeGen/WasmEHPrepare.cpp
+++ b/llvm/lib/CodeGen/WasmEHPrepare.cpp
@@ -28,7 +28,7 @@
 //   wasm.landingpad.index(index);
 //   __wasm_lpad_context.lpad_index = index;
 //   __wasm_lpad_context.lsda = wasm.lsda();
-//   _Unwind_CallPersonality(exn);
+//   personality_fn(exn);
 //   selector = __wasm_lpad_context.selector;
 //   ...
 //
@@ -38,18 +38,13 @@
 // exception is thrown. After the stack is unwound, the control flow is
 // transfered to WebAssembly 'catch' instruction.
 //
-// Unwinding the stack is not done by libunwind but the VM, so the personality
-// function in libcxxabi cannot be called from libunwind during the unwinding
-// process. So after a catch instruction, we insert a call to a wrapper function
-// in libunwind that in turn calls the real personality function.
-//
 // In Itanium EH, if the personality function decides there is no matching catch
 // clause in a call frame and no cleanup action to perform, the unwinder doesn't
 // stop there and continues unwinding. But in Wasm EH, the unwinder stops at
 // every call frame with a catch intruction, after which the personality
 // function is called from the compiler-generated user code here.
 //
-// In libunwind, we have this struct that serves as a communincation channel
+// In libunwind, we have this struct that serves as a communication channel
 // between the compiler-generated user code and the personality function in
 // libcxxabi.
 //
@@ -60,20 +55,8 @@
 // };
 // struct _Unwind_LandingPadContext __wasm_lpad_context = ...;
 //
-// And this wrapper in libunwind calls the personality function.
-//
-// _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
-//   struct _Unwind_Exception *exception_obj =
-//       (struct _Unwind_Exception *)exception_ptr;
-//   _Unwind_Reason_Code ret = __gxx_personality_v0(
-//       1, _UA_CLEANUP_PHASE, exception_obj->exception_class, exception_obj,
-//       (struct _Unwind_Context *)__wasm_lpad_context);
-//   return ret;
-// }
-//
 // We pass a landing pad index, and the address of LSDA for the current function
-// to the wrapper function _Unwind_CallPersonality in libunwind, and we retrieve
-// the selector after it returns.
+// to the personality function, and we retrieve the selector after it returns.
 //
 //===----------------------------------------------------------------------===//
 
@@ -111,8 +94,7 @@ class WasmEHPrepareImpl {
   Function *GetExnF = nullptr;      // wasm.get.exception() intrinsic
   Function *CatchF = nullptr;       // wasm.catch() intrinsic
   Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic
-  FunctionCallee CallPersonalityF =
-      nullptr; // _Unwind_CallPersonality() wrapper
+  FunctionCallee PersonalityF = nullptr;
 
   bool prepareThrows(Function &F);
   bool prepareEHPads(Function &F);
@@ -235,11 +217,14 @@ bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
   if (CatchPads.empty() && CleanupPads.empty())
     return false;
 
-  if (!F.hasPersonalityFn() ||
-      !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
+  if (!F.hasPersonalityFn())
+    return false;
+
+  auto Personality = classifyEHPersonality(F.getPersonalityFn());
+
+  if (!isScopedEHPersonality(Personality)) {
     report_fatal_error("Function '" + F.getName() +
-                       "' does not have a correct Wasm personality function "
-                       "'__gxx_wasm_personality_v0'");
+                       "' does not have a supported Wasm personality function");
   }
   assert(F.hasPersonalityFn() && "Personality function not found");
 
@@ -274,15 +259,12 @@ bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
   // instruction selection.
   CatchF = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_catch);
 
-  // FIXME: Verify this is really supported for current module.
-  StringRef UnwindCallPersonalityName =
-      RTLIB::RuntimeLibcallsInfo::getLibcallImplName(
-          RTLIB::impl__Unwind_CallPersonality);
+  auto *PersPrototype = FunctionType::get(
+      IRB.getInt32Ty(), {IRB.getPtrTy()}, false);
+  PersonalityF =
+      M.getOrInsertFunction(getEHPersonalityName(Personality), PersPrototype);
 
-  // _Unwind_CallPersonality() wrapper function, which calls the personality
-  CallPersonalityF = M.getOrInsertFunction(UnwindCallPersonalityName,
-                                           IRB.getInt32Ty(), IRB.getPtrTy());
-  if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
+  if (Function *F = dyn_cast<Function>(PersonalityF.getCallee()))
     F->setDoesNotThrow();
 
   unsigned Index = 0;
@@ -367,9 +349,17 @@ void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
   // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda();
   IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
 
-  // Pseudocode: _Unwind_CallPersonality(exn);
-  CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI,
-                                    OperandBundleDef("funclet", CPI));
+  // Pseudocode: personality_fn(exn);
+  CallInst *PersCI;
+
+  // Grab direct function when possible to use `call` instead of `call_indirect`
+  if (Function *F = dyn_cast<Function>(PersonalityF.getCallee()))
+    PersCI = IRB.CreateCall(F, CatchCI,
+                            OperandBundleDef("funclet", CPI));
+  else
+    PersCI = IRB.CreateCall(PersonalityF, CatchCI,
+                            OperandBundleDef("funclet", CPI));
+
   PersCI->setDoesNotThrow();
 
   // Pseudocode: int selector = __wasm_lpad_context.selector;
diff --git a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.ll b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.ll
index a35129effcdee..301f7d81a4274 100644
--- a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.ll
+++ b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.ll
@@ -1911,7 +1911,7 @@ declare ptr @_ZN7MyClassD2Ev(ptr returned) #0
 ; Function Attrs: nounwind
 declare ptr @_ZN7MyClassC2ERKS_(ptr returned, ptr dereferenceable(4)) #0
 
-declare i32 @__gxx_wasm_personality_v0(...)
+declare void @__gxx_wasm_personality_v0(ptr)
 ; Function Attrs: nounwind
 declare ptr @llvm.wasm.get.exception(token) #0
 ; Function Attrs: nounwind
diff --git a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.mir b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.mir
index 324dd29f2295e..f0341ed420241 100644
--- a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.mir
+++ b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.mir
@@ -3,7 +3,7 @@
 --- |
   target triple = "wasm32-unknown-unknown"
 
-  declare i32 @__gxx_wasm_personality_v0(...)
+  declare void @__gxx_wasm_personality_v0(ptr)
   declare void @foo()
   define void @rethrow_arg_test() personality ptr @__gxx_wasm_personality_v0 {
     ret void
diff --git a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
index a1e7616711ab9..a6611899806ea 100644
--- a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
+++ b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
@@ -35,7 +35,7 @@ target triple = "wasm32-unknown-unknown"
 ; CHECK:   local.set  2
 ; CHECK:   local.set  1
 ; CHECK:   local.get  0
-; CHECK:   call  _Unwind_CallPersonality
+; CHECK:   call  __gxx_wasm_personality_v0
 ; CHECK:   block
 ; CHECK:     br_if     0                                 # 0: down to label[[L2:[0-9]+]]
 ; CHECK:     call  __cxa_begin_catch
@@ -111,7 +111,7 @@ try.cont:                                         ; preds = %catch, %catch2, %en
 ; CHECK:         br        2                                      # 2: down to label[[L1:[0-9]+]]
 ; CHECK:       end_try_table
 ; CHECK:     end_block                                            # label[[L0]]:
-; CHECK:     call  _Unwind_CallPersonality
+; CHECK:     call  __gxx_wasm_personality_v0
 ; CHECK:     block
 ; CHECK:       block
 ; CHECK:         br_if     0                                      # 0: down to label[[L2:[0-9]+]]
@@ -124,7 +124,7 @@ try.cont:                                         ; preds = %catch, %catch2, %en
 ; CHECK:                 br        5                              # 5: down to label[[L5:[0-9]+]]
 ; CHECK:               end_try_table
 ; CHECK:             end_block                                    # label[[L4]]:
-; CHECK:             call  _Unwind_CallPersonality
+; CHECK:             call  __gxx_wasm_personality_v0
 ; CHECK:             block
 ; CHECK:               block
 ; CHECK:                 br_if     0                              # 0: down to label[[L6:[0-9]+]]
@@ -1644,7 +1644,7 @@ declare ptr @_ZN7MyClassD2Ev(ptr returned) #0
 ; Function Attrs: nounwind
 declare ptr @_ZN7MyClassC2ERKS_(ptr returned, ptr dereferenceable(4)) #0
 
-declare i32 @__gxx_wasm_personality_v0(...)
+declare void @__gxx_wasm_personality_v0(ptr)
 ; Function Attrs: nounwind
 declare ptr @llvm.wasm.get.exception(token) #0
 ; Function Attrs: nounwind
diff --git a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll
index d5b28b279d419..0c0fa9f840601 100644
--- a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll
+++ b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll
@@ -251,6 +251,6 @@ declare i32 @llvm.wasm.get.ehselector(token) #0
 declare void @__cxa_rethrow()
 declare ptr @__cxa_begin_catch(ptr)
 declare void @__cxa_end_catch()
-declare i32 @__gxx_wasm_personality_v0(...)
+declare void @__gxx_wasm_personality_v0(ptr)
 
 attributes #0 = { nounwind }
diff --git a/llvm/test/CodeGen/WebAssembly/exception-legacy.ll b/llvm/test/CodeGen/WebAssembly/exception-legacy.ll
index 573f69df8b859..217da5c39e27f 100644
--- a/llvm/test/CodeGen/WebAssembly/exception-legacy.ll
+++ b/llvm/test/CodeGen/WebAssembly/exception-legacy.ll
@@ -35,7 +35,7 @@ define void @throw(ptr %p) {
 ; CHECK:     catch     $[[EXN:[0-9]+]]=, __cpp_exception
 ; CHECK:       global.set  __stack_pointer
 ; CHECK:       i32.store __wasm_lpad_context
-; CHECK:       call       $drop=, _Unwind_CallPersonality, $[[EXN]]
+; CHECK:       call       __gxx_wasm_personality_v0, $[[EXN]]
 ; CHECK:       block
 ; CHECK:         br_if     0
 ; CHECK:         call      $drop=, __cxa_begin_catch
@@ -547,7 +547,7 @@ try.cont:                                         ; preds = %entry
 declare void @foo()
 declare void @bar(ptr)
 declare void @take_i32(i32)
-declare i32 @__gxx_wasm_personality_v0(...)
+declare void @__gxx_wasm_personality_v0(ptr)
 ; Function Attrs: noreturn
 declare void @llvm.wasm.throw(i32, ptr) #1
 ; Function Attrs: nounwind
diff --git a/llvm/test/CodeGen/WebAssembly/exception-legacy.mir b/llvm/test/CodeGen/WebAssembly/exception-legacy.mir
index 95929af4f4cea..7b8ba72ddfad9 100644
--- a/llvm/test/CodeGen/WebAssembly/exception-legacy.mir
+++ b/llvm/test/CodeGen/WebAssembly/exception-legacy.mir
@@ -3,7 +3,7 @@
 --- |
   target triple = "wasm32-unknown-unknown"
 
-  declare i32 @__gxx_wasm_personality_v0(...)
+  declare void @__gxx_wasm_personality_v0(ptr)
   declare void @foo()
   define void @eh_label_test() personality ptr @__gxx_wasm_personality_v0 {
     ret void
diff --git a/llvm/test/CodeGen/WebAssembly/exception.ll b/llvm/test/CodeGen/WebAssembly/exception.ll
index f738216d087ec..a972fceba4715 100644
--- a/llvm/test/CodeGen/WebAssembly/exception.ll
+++ b/llvm/test/CodeGen/WebAssembly/exception.ll
@@ -47,7 +47,7 @@ define void @throw(ptr %p) {
 ; CHECK:   local.get  0
 ; CHECK:   global.set  __stack_pointer
 ; CHECK:   i32.store  __wasm_lpad_context
-; CHECK:   call  _Unwind_CallPersonality
+; CHECK:   call  __gxx_wasm_personality_v0
 ; CHECK:   block
 ; CHECK:     br_if     0
 ; CHECK:     call  __cxa_begin_catch
@@ -648,7 +648,7 @@ try.cont:                                         ; preds = %entry
 declare void @foo()
 declare void @bar(ptr)
 declare void @take_i32(i32)
-declare i32 @__gxx_wasm_personality_v0(...)
+declare void @__gxx_wasm_personality_v0(ptr)
 ; Function Attrs: noreturn
 declare void @llvm.wasm.throw(i32, ptr) #1
 ; Function Attrs: nounwind
diff --git a/llvm/test/CodeGen/WebAssembly/function-info.mir b/llvm/test/CodeGen/WebAssembly/function-info.mir
index b81cdffde819f..be87bad9ec72c 100644
--- a/llvm/test/CodeGen/WebAssembly/function-info.mir
+++ b/llvm/test/CodeGen/WebAssembly/function-info.mir
@@ -3,7 +3,7 @@
 --- |
   target triple = "wasm32-unknown-unknown"
 
-  declare i32 @__gxx_wasm_personality_v0(...)
+  declare void @__gxx_wasm_personality_v0(ptr)
   declare void @foo()
   define void @function_property_test() {
     ret void
diff --git a/llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj-phi.ll b/llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj-phi.ll
index 5e8ebcf247000..b4ecb2911c89b 100644
--- a/llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj-phi.ll
+++ b/llvm/test/CodeGen/WebAssembly/lower-...
[truncated]

@QuantumSegfault

Copy link
Copy Markdown
Member Author

cc @hmelder

@github-actions

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp,c,h -- libunwind/include/unwind_wasm.h clang/lib/CodeGen/CGException.cpp libcxxabi/src/cxa_personality.cpp libunwind/include/unwind.h libunwind/src/Unwind-wasm.c llvm/lib/CodeGen/WasmEHPrepare.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/libcxxabi/src/cxa_personality.cpp b/libcxxabi/src/cxa_personality.cpp
index 3fdcd8a0c..c20b01395 100644
--- a/libcxxabi/src/cxa_personality.cpp
+++ b/libcxxabi/src/cxa_personality.cpp
@@ -1011,7 +1011,7 @@ static inline void get_landing_pad(__cxa_catch_temp_type &dest,
 #endif
 }
 
-#if (defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)) || defined(__WASM_EXCEPTIONS__)
+#  if (defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)) || defined(__WASM_EXCEPTIONS__)
 static _Unwind_Reason_Code __gxx_personality_imp
 #else
 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
@@ -1112,7 +1112,7 @@ __gxx_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame,
 }
 #endif
 
-#ifdef __WASM_EXCEPTIONS__
+#  ifdef __WASM_EXCEPTIONS__
 extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code __gxx_wasm_personality_v0(void* exception_ptr) {
   struct _Unwind_Exception* exception_object = (struct _Unwind_Exception*)exception_ptr;
 
@@ -1124,7 +1124,7 @@ extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code __gxx_wasm_personality_v0(voi
   return __gxx_personality_imp(1, _UA_SEARCH_PHASE, exception_object->exception_class, exception_object,
                                (struct _Unwind_Context*)&__wasm_lpad_context);
 }
-#endif
+#  endif
 
 #else
 

@aheejin aheejin left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This generally looks good to me. By the way, how about making our personality function return i32? Even if we don't currently check it from the callsite, having a _Unwind_Reason_Code return value can be useful, and it is the status quo too.

Comment thread llvm/lib/CodeGen/WasmEHPrepare.cpp Resolved
Comment thread llvm/lib/CodeGen/WasmEHPrepare.cpp Resolved
Comment thread llvm/lib/CodeGen/WasmEHPrepare.cpp Resolved
Comment thread llvm/lib/CodeGen/WasmEHPrepare.cpp Resolved
Comment thread libcxxabi/src/cxa_personality.cpp Resolved
Comment thread libcxxabi/src/cxa_personality.cpp Resolved
@llvmorg-github-actions llvmorg-github-actions Bot added PGO Profile Guided Optimizations llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Jul 17, 2026

@aheejin aheejin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the PR description about the signature too (Now the return type is i32)

Also if possible please don't force-push. Github can't track "Changes after you last reviewed" if you force push, so it's hard to see what changed.

Comment thread llvm/lib/CodeGen/WasmEHPrepare.cpp Resolved
Comment thread libcxxabi/src/cxa_personality.cpp Resolved
Comment thread libcxxabi/src/cxa_personality.cpp Resolved
@QuantumSegfault
QuantumSegfault requested a review from aheejin July 28, 2026 18:26

@aheejin aheejin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The formatter is complaining about cxa_personality.cpp but the whole file doesn't conform to the indentation rule for preprocessors anyway...

@QuantumSegfault
QuantumSegfault merged commit 1d4e04f into llvm:main Jul 29, 2026
66 of 67 checks passed
asudarsa-qti pushed a commit to asudarsa-qti/llvm-project that referenced this pull request Jul 29, 2026
…sonality` (llvm#209282)

On Wasm, it's not currently possibly to change which `personality` is
attached to a given IR function. This is primarily because no matter
what `personality` is attached, codegen creates a call to a
`Unwind_CallPersonality` wrapper hard-coded to call c++abi's
personality.

In order to allow alternative personalities without introducing bloat at
each personality call site, this PR adjusts the ABI of the personality
on Wasm. `Unwind_CallPersonality` is moved to `libcxxabi` under the name
`__gxx_wasm_personality_v0`, with the "real" personality code renamed
`__gxx_personality_imp` and called by `__gxx_wasm_personality_v0` (after
resetting the landing pad context).

The signature of EH personalities on Wasm is now `int personality(void*
exception_object)` (where the int return is `_Unwind_Reason_Code` and
the `void*` is `_Unwind_Exception*`), and personalities are expected to
`__wasm_lpad_context.selector = 0;`.

Based on llvm#175202
dicej added a commit to dicej/wasi-sdk that referenced this pull request Jul 30, 2026
This backports llvm/llvm-project#209282 to resolve the
remaining LLVM issues with Wasm exceptions and shared libraries.

I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

Currently a few of the p3 tests are failing in shared library mode, apparently
due to TLS-related issues.  I'll work on debugging those.
dicej added a commit to dicej/wasi-sdk that referenced this pull request Jul 30, 2026
This backports llvm/llvm-project#209282 to resolve the
remaining LLVM issues with Wasm exceptions and shared libraries.

I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

Currently a few of the p3 tests are failing in shared library mode, apparently
due to TLS-related issues.  I'll work on debugging those.
aheejin added a commit to emscripten-core/emscripten that referenced this pull request Jul 30, 2026
After llvm/llvm-project#209282, the user code
calls a personality function instead of `Unwind_CallPersonality`. It
also changed the signature of Wasm personality function. It had both
code generation and library changes, but now our waterfall picks up the
codegen changes but our libraries are still from the last release.

This applies llvm/llvm-project#209282 library
side patch here as well. This kind of change is rare enough so I don't
think we necessary should follow LLVM tot for libunwind, unless we
decide to do that broadly for all LLVM libraries.

Also this adds `system/lib/libunwind/include` to libcxx's `includes`
directory. Turns out so far we have been including Clang's default
`unwind.h` and it was OK but now we need to include the correct file.

This temporarily skips all Wasm EH tests to make the LLVM roll land.
They will be reenabled once the roll lands.

Fixes #27442.
jgreenbaum pushed a commit to jgreenbaum/llvm-project that referenced this pull request Aug 3, 2026
…sonality` (llvm#209282)

On Wasm, it's not currently possibly to change which `personality` is
attached to a given IR function. This is primarily because no matter
what `personality` is attached, codegen creates a call to a
`Unwind_CallPersonality` wrapper hard-coded to call c++abi's
personality.

In order to allow alternative personalities without introducing bloat at
each personality call site, this PR adjusts the ABI of the personality
on Wasm. `Unwind_CallPersonality` is moved to `libcxxabi` under the name
`__gxx_wasm_personality_v0`, with the "real" personality code renamed
`__gxx_personality_imp` and called by `__gxx_wasm_personality_v0` (after
resetting the landing pad context).

The signature of EH personalities on Wasm is now `int personality(void*
exception_object)` (where the int return is `_Unwind_Reason_Code` and
the `void*` is `_Unwind_Exception*`), and personalities are expected to
`__wasm_lpad_context.selector = 0;`.

Based on llvm#175202
dicej added a commit to dicej/wasi-sdk that referenced this pull request Sep 30, 2026
I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

This includes patches for the required LLVM PR backports:

- llvm/llvm-project#209282
- llvm/llvm-project#222747
dicej added a commit to dicej/wasi-sdk that referenced this pull request Sep 30, 2026
I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

This includes patches for the required LLVM PR backports:

- llvm/llvm-project#209282
- llvm/llvm-project#222747
QuantumSegfault added a commit that referenced this pull request Sep 30, 2026
)

Now that #209282 is merged, languages/code may choose to create and use
personality functions other than `__gxx_wasm_personality_v0` on
WebAssembly. However said personality must still be
`isScopedEHPersonality` and recognized as a Wasm personality for
everything to function correctly.

This reserves a `EHPersonality::Wasm_D` (symbol
`_d_eh_personality_wasm`) for future use by the D programming language.
dicej added a commit to dicej/wasi-sdk that referenced this pull request Sep 30, 2026
I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

This includes patches for the required LLVM PR backports:

- llvm/llvm-project#209282
- llvm/llvm-project#222747
dicej added a commit to dicej/wasi-sdk that referenced this pull request Sep 30, 2026
I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

This includes patches for the required LLVM PR backports:

- llvm/llvm-project#209282
- llvm/llvm-project#222747
dicej added a commit to dicej/wasi-sdk that referenced this pull request Sep 30, 2026
I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

This includes patches for the required LLVM PR backports:

- llvm/llvm-project#209282
- llvm/llvm-project#222747
dicej added a commit to dicej/wasi-sdk that referenced this pull request Sep 30, 2026
I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables.  As usual my CMake skills are
underwhelming; very open to feedback.

This includes patches for the required LLVM PR backports:

- llvm/llvm-project#209282
- llvm/llvm-project#222747
alexcrichton pushed a commit to WebAssembly/wasi-sdk that referenced this pull request Oct 1, 2026
I've updated `tests/CMakeLists.txt` to run all p2 and p3 tests as shared
libraries as well as normal executables. As usual my CMake skills are
underwhelming; very open to feedback.

This includes patches for the required LLVM PR backports:

- llvm/llvm-project#209282
- llvm/llvm-project#222747
Expertcoderz pushed a commit to Expertcoderz/llvm-project that referenced this pull request Oct 3, 2026
…#224883)

Now that llvm#209282 is merged, languages/code may choose to create and use
personality functions other than `__gxx_wasm_personality_v0` on
WebAssembly. However said personality must still be
`isScopedEHPersonality` and recognized as a Wasm personality for
everything to function correctly.

This reserves a `EHPersonality::Wasm_D` (symbol
`_d_eh_personality_wasm`) for future use by the D programming language.
PetarKirov added a commit to PetarKirov/ldc that referenced this pull request Oct 30, 2026
Since llvm/llvm-project#209282 (LLVM 24), WasmEHPrepare no longer calls
`_Unwind_CallPersonality`; a landing pad calls the function's own
personality, as `int personality(void* exception_object)`. LDC declared
`__gxx_wasm_personality_v0` with the five-parameter Itanium prototype and
druntime never defined it, so every executable with a `catch` failed to
link (undefined symbol), and the call had the wrong Wasm signature.

Declare it with the new prototype (on older LLVM it only names the
personality, so the prototype is never used) and define it in druntime,
keeping `_Unwind_CallPersonality` for LLVM 23 and older.

Claude-Session: https://claude.ai/code/session_01RNXL2XC6HKXKYPyX5JhfTg
@anutosh491

Copy link
Copy Markdown
Member

Hey @aheejin,

Could we consider backporting this to release/23.x for 23.1.6? With LLVM/Clang 23 and the emsdk 6.0.8 sysroot, a small std::runtime_error/catch (const std::exception&) example compiles but its Wasm module fails to load: _Unwind_CallPersonality cannot be resolved.

I experienced this on multiple projectd downstream. The 23.x branch still emits that symbol, while this fix uses the personality supplied by the newer runtime. I have a downstream backport and can help test wasm32 and wasm64.

Could a change be made for the release branch ?

alexcrichton pushed a commit to WebAssembly/wasi-sdk that referenced this pull request Dec 1, 2026
- Update the LLVM submodule from 23.1.5 to 23.1.6. All
`src/llvm-*.patch` files still apply.
- Build-only-sysroot CI job:
- Move the stock-clang entries (`default`, `static`) from LLVM 22 to
LLVM 23 from apt.llvm.org.
- Add a `wasi-sdk-34` entry that builds the sysroot with the latest
release.
- Install the `wasm-component-ld` version from
`cmake/wasi-sdk-toolchain.cmake` (0.5.31) instead of a hard-coded
0.5.21.
- Pass `-DWASI_SDK_EXCEPTIONS=OFF`: with clang 23+ the sysroot defaults
to `DUAL`, which needs the toolchain patches from
llvm/llvm-project#209282 and #222747 that neither compiler has.
- Set `fail-fast: false` so one entry failing doesn't cancel the others.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:WebAssembly clang:codegen IR generation bugs: mangling, exceptions, etc. libc++abi libc++abi C++ Runtime Library. Not libc++. libunwind llvm:codegen llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:ir llvm:transforms PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants