Repository navigation
[WebAssembly] Move __cpp_exception to libunwind - #185770
Conversation
|
@llvm/pr-subscribers-libunwind Author: Alex Crichton (alexcrichton) ChangesThis commit is an extension of some work in [wasi-sdk] to get C++ exceptions working on WASI targets. This is intended to complement the work in #168449 and together is enough to get C++ exceptions working within wasi-sdk. The changes here are:
5 Files Affected:
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 6c27f6d4d529e..f0570a9092f40 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -891,7 +891,6 @@ set(s390x_SOURCES
set(wasm_SOURCES
wasm/__c_longjmp.S
- wasm/__cpp_exception.S
${GENERIC_TF_SOURCES}
${GENERIC_SOURCES}
)
diff --git a/compiler-rt/lib/builtins/wasm/__cpp_exception.S b/compiler-rt/lib/builtins/wasm/__cpp_exception.S
deleted file mode 100644
index 0496e1dbf6158..0000000000000
--- a/compiler-rt/lib/builtins/wasm/__cpp_exception.S
+++ /dev/null
@@ -1,26 +0,0 @@
-//===-- __cpp_exception.S - Implement __cpp_exception ---------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements __cpp_exception which LLVM uses to implement exception
-// handling when Wasm EH is enabled.
-//
-//===----------------------------------------------------------------------===//
-
-#ifdef __wasm_exception_handling__
-
-#ifdef __wasm64__
-#define PTR i64
-#else
-#define PTR i32
-#endif
-
-.globl __cpp_exception
-.tagtype __cpp_exception PTR
-__cpp_exception:
-
-#endif // !__wasm_exception_handling__
diff --git a/libunwind/src/Unwind-wasm.c b/libunwind/src/Unwind-wasm.c
index 2f4498c3f3989..44d284ba85b8f 100644
--- a/libunwind/src/Unwind-wasm.c
+++ b/libunwind/src/Unwind-wasm.c
@@ -69,6 +69,23 @@ _Unwind_RaiseException(_Unwind_Exception *exception_object) {
__builtin_wasm_throw(0, exception_object);
}
+// Define the `__cpp_exception` symbol which `__builtin_wasm_throw` above will
+// reference. This is defined here in `libunwind` as the single canonical
+// definition for this API and it's required for users to ensure that there's
+// only one copy of `libunwind` within a wasm module to ensure this is only
+// defined once and exactly once.
+__asm__(
+".globl __cpp_exception\n"
+#if defined(__wasm32__)
+".tagtype __cpp_exception i32\n"
+#elif defined(__wasm64__)
+".tagtype __cpp_exception i64\n"
+#else
+# error "Unsupported Wasm architecture"
+#endif
+"__cpp_exception:\n"
+);
+
/// Called by __cxa_end_catch.
_LIBUNWIND_EXPORT void
_Unwind_DeleteException(_Unwind_Exception *exception_object) {
diff --git a/libunwind/src/assembly.h b/libunwind/src/assembly.h
index 84c9d526f1d75..2167326605b8a 100644
--- a/libunwind/src/assembly.h
+++ b/libunwind/src/assembly.h
@@ -253,6 +253,9 @@ aliasname: \
#define WEAK_ALIAS(name, aliasname)
#define NO_EXEC_STACK_DIRECTIVE
+#elif defined(__wasm__)
+#define NO_EXEC_STACK_DIRECTIVE
+
// clang-format on
#else
diff --git a/llvm/utils/gn/secondary/compiler-rt/lib/builtins/sources.gni b/llvm/utils/gn/secondary/compiler-rt/lib/builtins/sources.gni
index 2ac71aa8e8367..c9eeede16e3eb 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/builtins/sources.gni
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/builtins/sources.gni
@@ -539,7 +539,6 @@ if (current_cpu == "ve") {
if (current_cpu == "wasm") {
builtins_sources += [
"wasm/__c_longjmp.S",
- "wasm/__cpp_exception.S",
]
}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
2540fb8 to
e4ba5cd
Compare
|
Regarding the assembly.h change, do you know why we haven't need that in emscripten? @aheejin have you run into issues with assembly.h before? |
|
Oh I see, I wonder if we should instead widen the |
e4ba5cd to
07b8ce3
Compare
|
Oh good catch, turns out that #162581 already made these files "conditional enough" to the point that there's no need for that change any more. I've updated the commit message and backed out the |
|
Do we still need the first paragraph of the PR description? Paragraph 2 seems like good description of the change on its own? |
The `__cpp_exception` symbol is now defined in libunwind instead of compiler-rt. This is moved for a few reasons, but the primary reason is that compiler-rt is linked duplicate-ly into all shared objects meaning that it's not suitable for define-once symbols such as `__cpp_exception`. By moving the definition to the user of the symbol, libunwind itself, that guarantees that the symbol should be defined exactly once and only when appropriate. A secondary reason for this movement is that it avoids the need to compile compiler-rt twice: once with exception and once without, and instead the same build can be used for both exceptions-and-not.
07b8ce3 to
d702761
Compare
|
Good point yeah, and sounds reasonable to me, removed from both the PR and the commit |
|
We should probably link to #160959 where add these symbols to compiler-rt (which it turns out was the wrong place for them). |
|
I wonder if we should also move __c_longjmp out of compiler-rt? |
|
Agreed yeah, I suspect that longjmp/setjmp in wasi-libc are probably broken since the change awhile back to avoid adding a weak definition of |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/35499 Here is the relevant piece of the build log for the reference |
This is similar to #185770 where it removes an exception-handling-related symbol from `compiler-rt` in favor of having definitions elsewhere. The compiler-rt library is linked into all shared objects, for example, which can result in duplicate definitions of a symbol where this tag wants to have one unique definition. The intention behind this commit is to defer the definition of this symbol to downstream libraries, such as the definition of `longjmp` itself. An example of this is WebAssembly/wasi-libc#772 where the responsibility of defining this symbol now lies with wasi-libc.
This commit collects together some LLVM PRs, some changes in the build configuration here, and some thoughts from WebAssembly#565 and related issues. Specifically the changes here are: * The patch for llvm/llvm-project#168449 is updated to its upstream (unlanded) form. * Patches for the (landed) llvm/llvm-project#185770 and llvm/llvm-project#185775 are added. * The `WASI_SDK_EXCEPTIONS` configuration is now either `ON`, `OFF`, or `DUAL`. The default depends on the version of Clang in use, where 23.0.0+ (which isn't released officially yet) will be `DUAL` and otherwise it's `OFF`. CI for our custom-built patched toolchain defaults to `DUAL`. * In `DUAL` mode libcxx is built twice into two different directories, once with exceptions and once without. This is supported by LLVM patches and means that Clang will select the right set of libraries based on compiler flags. The end result here is that the produced toolchain from this repository, by default, supports C++ exceptions. Additionally if exceptions-related flags are not passed then the final binary will not use C++ exceptions nor require the wasm exception-handling proposal. There's still follow-up work from WebAssembly#565, such as: * Subjectively it feels wordy to pass `-fwasm-exceptions` vs `-fexceptions`. * Personally I think `-mllvm -wasm-use-legacy-eh=false` should become the default upstream. * Subjectively I don't think that `-lunwind` should be necessary and it should be injected automatically with `-fwasm-exceptions` (or `-fexceptions`). * Shared libraries for exceptions remain disabled due to build errors I do not personally know how to resolve. I'll file follow-up issues for these once this has landed since they're more minor compared to the main body of "anything works". Closes WebAssembly#334 Closes WebAssembly#565
This commit collects together some LLVM PRs, some changes in the build configuration here, and some thoughts from WebAssembly#565 and related issues. Specifically the changes here are: * The patch for llvm/llvm-project#168449 is updated to its upstream (unlanded) form. * Patches for the (landed) llvm/llvm-project#185770 and llvm/llvm-project#185775 are added. * The `WASI_SDK_EXCEPTIONS` configuration is now either `ON`, `OFF`, or `DUAL`. The default depends on the version of Clang in use, where 23.0.0+ (which isn't released officially yet) will be `DUAL` and otherwise it's `OFF`. CI for our custom-built patched toolchain defaults to `DUAL`. * In `DUAL` mode libcxx is built twice into two different directories, once with exceptions and once without. This is supported by LLVM patches and means that Clang will select the right set of libraries based on compiler flags. The end result here is that the produced toolchain from this repository, by default, supports C++ exceptions. Additionally if exceptions-related flags are not passed then the final binary will not use C++ exceptions nor require the wasm exception-handling proposal. There's still follow-up work from WebAssembly#565, such as: * Subjectively it feels wordy to pass `-fwasm-exceptions` vs `-fexceptions`. * Personally I think `-mllvm -wasm-use-legacy-eh=false` should become the default upstream. * Subjectively I don't think that `-lunwind` should be necessary and it should be injected automatically with `-fwasm-exceptions` (or `-fexceptions`). * Shared libraries for exceptions remain disabled due to build errors I do not personally know how to resolve. I'll file follow-up issues for these once this has landed since they're more minor compared to the main body of "anything works". Closes WebAssembly#334 Closes WebAssembly#565
This commit collects together some LLVM PRs, some changes in the build configuration here, and some thoughts from #565 and related issues. Specifically the changes here are: * The patch for llvm/llvm-project#168449 is updated to its upstream (unlanded) form. * Patches for the (landed) llvm/llvm-project#185770 and llvm/llvm-project#185775 are added. * The `WASI_SDK_EXCEPTIONS` configuration is now either `ON`, `OFF`, or `DUAL`. The default depends on the version of Clang in use, where 23.0.0+ (which isn't released officially yet) will be `DUAL` and otherwise it's `OFF`. CI for our custom-built patched toolchain defaults to `DUAL`. * In `DUAL` mode libcxx is built twice into two different directories, once with exceptions and once without. This is supported by LLVM patches and means that Clang will select the right set of libraries based on compiler flags. The end result here is that the produced toolchain from this repository, by default, supports C++ exceptions. Additionally if exceptions-related flags are not passed then the final binary will not use C++ exceptions nor require the wasm exception-handling proposal. There's still follow-up work from #565, such as: * Subjectively it feels wordy to pass `-fwasm-exceptions` vs `-fexceptions`. * Personally I think `-mllvm -wasm-use-legacy-eh=false` should become the default upstream. * Subjectively I don't think that `-lunwind` should be necessary and it should be injected automatically with `-fwasm-exceptions` (or `-fexceptions`). * Shared libraries for exceptions remain disabled due to build errors I do not personally know how to resolve. I'll file follow-up issues for these once this has landed since they're more minor compared to the main body of "anything works". Closes #334 Closes #565 --------- Co-authored-by: Joel Dice <[email protected]>
#27496) This makes `-fwasm-exceptions` link the Wasm EH runtime independently of C++ linking. Wasm EH objects can be produced by non-C++ frontends - notably rustc, whose precompiled std uses Wasm exceptions for unwinding and which invokes `emcc` (not `em++`) as its linker. Since `DEFAULT_TO_CXX` was disabled by default in #27469, any rust-driven emcc link now fails with: ``` wasm-ld: error: libstd-*.rlib: undefined symbol: __cpp_exception wasm-ld: error: libpanic_unwind-*.rlib: undefined symbol: _Unwind_RaiseException ``` because both libunwind and the `__cpp_exception` tag (previously defined in libc++abi) were only linked under `LINK_AS_CXX`. The tag and the unwinding runtime are language-agnostic runtime support for the Wasm EH LLVM lowering, not part of the C++ ABI surface. This PR now includes #27498 as its base, which provides the same `_Unwind_*` linkability for the non-Wasm-EH modes on the JS side. What's implemented: * The `__cpp_exception` tag definition moves from libc++abi (`__cpp_exception.S`) into libunwind's `Unwind-wasm.c` as inline asm, matching upstream llvm/llvm-project#185770. * libunwind is linked whenever `WASM_EXCEPTIONS` is enabled, rather than only when linking as C++. Test coverage extends `test_libunwind` from #27498 with `wasm_eh` and `wasm_legacy_eh` variants (completing its TODO), and adds `test_cpp_exception_tag` linking an asm object that throws the tag directly from a C caller - the same reference pattern as rustc-generated catch/throw sites - which fails to link without this change. The now-unneeded `DEFAULT_TO_CXX` workaround is dropped from `test_wasi_with_sjlj`. Existing C++ Wasm EH coverage (`core0.test_exceptions_*`) passes unchanged. _Made with AI assistance under my review_
The
__cpp_exceptionsymbol is now defined in libunwind instead ofcompiler-rt. This is moved for a few reasons, but the primary reason is
that compiler-rt is linked duplicate-ly into all shared objects meaning
that it's not suitable for define-once symbols such as
__cpp_exception. By moving the definition to the user of the symbol,libunwind itself, that guarantees that the symbol should be defined
exactly once and only when appropriate. A secondary reason for this
movement is that it avoids the need to compile compiler-rt twice: once
with exception and once without, and instead the same build can be used
for both exceptions-and-not.