Repository navigation
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-libunwind Author: Yerzhan Zhamashev (yerzham) ChangesMost minimal changes to enable libunwind to build for wasm by excluding 2 Files Affected:
diff --git a/libunwind/src/assembly.h b/libunwind/src/assembly.h
index 84c9d526f1d75..9c35b6050a2bf 100644
--- a/libunwind/src/assembly.h
+++ b/libunwind/src/assembly.h
@@ -254,6 +254,8 @@ aliasname: \
#define NO_EXEC_STACK_DIRECTIVE
// clang-format on
+#elif defined(__wasm__)
+
#else
#error Unsupported target
diff --git a/libunwind/src/config.h b/libunwind/src/config.h
index f017403fa2234..73162995f9293 100644
--- a/libunwind/src/config.h
+++ b/libunwind/src/config.h
@@ -75,13 +75,14 @@
#define _LIBUNWIND_EXPORT
#define _LIBUNWIND_HIDDEN
#else
- #if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX)
- #define _LIBUNWIND_EXPORT __declspec(dllexport)
- #define _LIBUNWIND_HIDDEN
- #else
- #define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
- #define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
- #endif
+#if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX) && \
+ !defined(__wasm__)
+#define _LIBUNWIND_EXPORT __declspec(dllexport)
+#define _LIBUNWIND_HIDDEN
+#else
+#define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
+#define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
+#endif
#endif
#define STR(a) #a
|
4b07878 to
8baec64
Compare
|
Compared to yamt's gist why do we not have After fixing that I then ran into the following errors when linking Are there further changes needed to fix this? |
|
I needed to clarify wasi-sdk had to use latest version of src/llvm-project, at least I tested since v21.1.5. Otherwise, usage of NO_EXEC_STACK_DIRECTIVE in UnwindRegistersRestore.S should be gated out since #92192 (then some modification at #162581). I do not think NO_EXEC_STACK_DIRECTIVE is ever used for wasm target after that, so it would be an unused declaration. As for the consequent errors, probably still related to outdated llvm source? |
|
Oh right, I should have directly used your branch instead of locally applying the patch -- thanks! I can report that with this patch (and the one for WASI-SDK) I did get working several large (100K+ LoC) programs from SPEC that use C++ exceptions. I did encounter build issues when using LTO+exceptions ( |
|
Ping |
1 similar comment
|
Ping |
aheejin
left a comment
There was a problem hiding this comment.
Sorry for the delay! My mail filter wasn't set up right..
8baec64 to
0f23156
Compare
0f23156 to
856fee9
Compare
This commit adds initial configuration support for building wasi-sdk with support for C++ exceptions in WebAssembly. A small test is included which is exercised in CI at this time, but otherwise this does not update CI to actually ship sdk builds with C++ exceptions enabled. Instead the intention here is to make it easier to test builds with support for C++ exceptions and centralize planning/development around this. The goal here is to get things compiling to the point that applications can be compiled. I haven't thoroughly tested C++ exceptions and as evident in this PR it still requires changes in LLVM. Some small logic is added here to apply a `*.patch` file to LLVM to avoid needing a new submodule fork or waiting for upstream changes. The intention is that this `*.patch` is short-lived once the changes are officially merged into LLVM itself. The `*.patch` included here contains llvm/llvm-project#168449 as well as another minor edit I found was necessary to get things compiling locally. Given the discussion on that PR it looks like once LLVM is updated with that merged the extra part of the `*.patch` won't be necessary. This PR notably does not enable shared libraries when exceptions are enabled. I don't know enough about how things are supposed to work to be able to fully diagnose the compilation errors I'm seeing if shared libraries are enabled. This is something I'd hope would be fixed before actually shipping exceptions support. This PR then additionally folds in [this gist][gist] for various bits of build logic to this repository itself. Finally, this PR includes some documentation about the current status of exceptions and links to various tracking issues too. [gist]: https://gist.github.com/yerzham/302efcec6a2e82c1e8de4aed576ea29d
> **Note**: this PR is me weaving together the work of many others. For example @yerzham's and @cpetig's work on #565 pretty much shaped this PR. Thank you! This commit adds initial configuration support for building wasi-sdk with support for C++ exceptions in WebAssembly. A small test is included which is exercised in CI at this time, but otherwise this does not update CI to actually ship sdk builds with C++ exceptions enabled. Instead the intention here is to make it easier to test builds with support for C++ exceptions and centralize planning/development around this. The goal here is to get things compiling to the point that applications can be compiled. I haven't thoroughly tested C++ exceptions and as evident in this PR it still requires changes in LLVM. Some small logic is added here to apply a `*.patch` file to LLVM to avoid needing a new submodule fork or waiting for upstream changes. The intention is that this `*.patch` is short-lived once the changes are officially merged into LLVM itself. The `*.patch` included here contains llvm/llvm-project#168449 as well as another minor edit I found was necessary to get things compiling locally. Given the discussion on that PR it looks like once LLVM is updated with that merged the extra part of the `*.patch` won't be necessary. This PR notably does not enable shared libraries when exceptions are enabled. I don't know enough about how things are supposed to work to be able to fully diagnose the compilation errors I'm seeing if shared libraries are enabled. This is something I'd hope would be fixed before actually shipping exceptions support. This PR then additionally folds in [this gist][gist] for various bits of build logic to this repository itself. Finally, this PR includes some documentation about the current status of exceptions and links to various tracking issues too. [gist]: https://gist.github.com/yerzham/302efcec6a2e82c1e8de4aed576ea29d
777761a to
852c8a2
Compare
|
I wanted to ping here and ask -- was there anything remaining for this? I think this is the last piece needed for C++ exceptions in wasi-sdk, so I was curious to ask about and possibly help get this landed. |
|
Same here, I've been using this change as part of packaging WASI support in nixpkgs, but we'd like to see this merged upstream rather than applying a patch. |
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]>
|
What is the status for remaining checks? |
The wasm-jit target of omc links external "C" code as WebAssembly modules, and an omc without a C compiler (the one in a browser) has no other way to run it. The new `build-wasm` command prebuilds them for every version of every indexed library: omc's `getExternalFunctions` describes a library's external functions, and the package manager compiles their `Include` sources with call wrappers from their C signatures, plus the `Library` names a repos.json `wasm` recipe covers (sources, flags, forced headers, sources kept private, files configured as CMake's `configure_file` does). C++ sources compile against libc++ with WebAssembly exceptions. Everything is built against the package manager's own libc, shipped with each bundle as one libc is loaded per simulation, and for a fixed set of wasm features that wasmtime, wasmer and V8 all run. With the toolchain these make a generation: omc keeps generations side by side and runs a model with the newest one all its libraries have, so a new libc or toolchain is a new generation and every bundle rebuilt, with no change to omc. The libc's sysroot is published as the index's `wasmToolchain`, which omc installs to compile a model's own `Include` sources with, and libc++'s headers and `libc++.so` as its optional `cxx` part, for C++ code. System libraries come from scripts under `wasm/`, once each: as modules of their own (libc++, CPython, netCDF-C, ModelicaSDF, ExternalMediaLib) or as archives with hidden symbols linked into each library using them (HDF5, zlib), so two libraries' HDF5 versions never clash. A version's modules are zipped as `<Library>-wasm32-wasip1-<hash>.zip`, the hash of what they were built from, so versions whose C code did not change share one zip-file; a library without external "C" code gets none. wasmdata.json records what was built, including the functions that did not compile, and a version is built again when its source, its recipe or omc's loader interface changes. `genindex` adds a `wasm` entry per version and the top-level `systemLibraries` and `wasmToolchain`. | Library | What it gets | |----------------|--------------------------------------------------------| | Modelica | the MSL's C libraries of the latest release, for each | | Buildings | `ModelicaBuildingsPython_3_8` over CPython 3.14 | | ExternalMedia | ExternalMediaLib 4.1.1 with CoolProp 6.7.0 | | ExternData | the `ED_*` libraries and their parsers, private HDF5 | | NcDataReader2 | `ncDataReader2` over netCDF-C 4.9.3 | | SDF | ModelicaSDF 0.4.5, private HDF5 and matio | | SystemDynamics | prototypes its sources up to 2.1.2 do not include | libc++ is LLVM 21's, with libc++abi and libunwind in one `libc++.so`; libunwind needs llvm/llvm-project#168449 and no register assembly on wasm. Both CMake projects of ExternalMedia assume a native platform, so its sources and CoolProp's are compiled directly, CoolProp told it is on POSIX. Buildings' `pythonWrapper.c` calls `pythonExchangeValuesNoModelica` without a prototype, which does not link in wasm, so its entry forces one in. WASI has no `dup`, `mkdtemp` or `mktemp`, which the MSL, ExternData and matio get from forced headers, and no `clock` without the emulated process clocks, which CoolProp gets privately. Buildings' FFD needs threads and is left out. The Jenkins image moves to Ubuntu resolute with omc from OpenModelica's nightly packages, which has `getExternalFunctions` once the OpenModelica change adding it is in, also serving updateinfo through OMPython, clang/wasm-ld 21 with the wasm32 compiler-rt, CMake and Python 3.14 for the CPython cross-build; the `cache` stage copies the zip-files to libraries.openmodelica.org/precompiled/wasm32-wasip1. Assisted-by: Claude Opus 5.5
The wasm-jit target of omc links external "C" code as WebAssembly modules, and an omc without a C compiler (the one in a browser) has no other way to run it. The new `build-wasm` command prebuilds them for every version of every indexed library: omc's `getExternalFunctions` describes a library's external functions, and the package manager compiles their `Include` sources with call wrappers from their C signatures, plus the `Library` names a repos.json `wasm` recipe covers (sources, flags, forced headers, sources kept private, files configured as CMake's `configure_file` does). C++ sources compile against libc++ with WebAssembly exceptions. Everything is built against the package manager's own libc, shipped with each bundle as one libc is loaded per simulation, and for a fixed set of wasm features that wasmtime, wasmer and V8 all run. With the toolchain these make a generation: omc keeps generations side by side and runs a model with the newest one all its libraries have, so a new libc or toolchain is a new generation and every bundle rebuilt, with no change to omc. The libc's sysroot is published as the index's `wasmToolchain`, which omc installs to compile a model's own `Include` sources with, and libc++'s headers and `libc++.so` as its optional `cxx` part, for C++ code. System libraries come from scripts under `wasm/`, once each: as modules of their own (libc++, CPython, netCDF-C, ModelicaSDF, ExternalMediaLib) or as archives with hidden symbols linked into each library using them (HDF5, zlib), so two libraries' HDF5 versions never clash. A version's modules are zipped as `<Library>-wasm32-wasip1-<hash>.zip`, the hash of what they were built from, so versions whose C code did not change share one zip-file; a library without external "C" code gets none. wasmdata.json records what was built, including the functions that did not compile, and a version is built again when its source, its recipe or omc's loader interface changes. `genindex` adds a `wasm` entry per version and the top-level `systemLibraries` and `wasmToolchain`. | Library | What it gets | |----------------|--------------------------------------------------------| | Modelica | the MSL's C libraries of the latest release, for each | | Buildings | `ModelicaBuildingsPython_3_8` over CPython 3.14 | | ExternalMedia | ExternalMediaLib 4.1.1 with CoolProp 6.7.0 | | ExternData | the `ED_*` libraries and their parsers, private HDF5 | | NcDataReader2 | `ncDataReader2` over netCDF-C 4.9.3 | | SDF | ModelicaSDF 0.4.5, private HDF5 and matio | | SystemDynamics | prototypes its sources up to 2.1.2 do not include | libc++ is LLVM 21's, with libc++abi and libunwind in one `libc++.so`; libunwind needs llvm/llvm-project#168449 and no register assembly on wasm. Both CMake projects of ExternalMedia assume a native platform, so its sources and CoolProp's are compiled directly, CoolProp told it is on POSIX. Buildings' `pythonWrapper.c` calls `pythonExchangeValuesNoModelica` without a prototype, which does not link in wasm, so its entry forces one in. WASI has no `dup`, `mkdtemp` or `mktemp`, which the MSL, ExternData and matio get from forced headers, and no `clock` without the emulated process clocks, which CoolProp gets privately. Buildings' FFD needs threads and is left out. The Jenkins image moves to Ubuntu resolute with omc from OpenModelica's nightly packages, which has `getExternalFunctions` once the OpenModelica change adding it is in, also serving updateinfo through OMPython, clang/wasm-ld 21 with the wasm32 compiler-rt, CMake and Python 3.14 for the CPython cross-build; the `cache` stage copies the zip-files to libraries.openmodelica.org/precompiled/wasm32-wasip1. Assisted-by: Claude Opus 5.5
- Conditionalize the use of POSIX features missing on WASI/WebAssembly, so that LLVM, Clang and LLD build for wasm32-wasip1. From Catherine (whitequark)'s llvm#92677, as carried by YoWASP, rebased onto LLVM 23 and extended to the code that 23 added. - libc++: only use the IBM locale base API on z/OS. Its strtod_l and friends conflict with wasi-libc's declarations of them. This undoes part of llvm#194317, as wasi-sdk does. - libunwind: don't use __declspec on WebAssembly, so that it builds with WebAssembly exceptions (llvm#168449, as wasi-sdk carries). - LLD_WASM_ONLY: a CMake option to link only the WebAssembly linker into lld. - Buffer binary output on WASI. wasi-libc reports a block size of 0, which leaves LLVM's file streams unbuffered, so writing an object file took tens of thousands of host calls. Co-authored-by: Catherine <[email protected]>
…sip1-threads) (#9) * sdk-check build-threads-runtime: the wasm32-unknown-wasip1-threads runtime from upstream's swift-6.4.2-RELEASE and sdk/patches Swift 6.4.2's stdlib, runtime and Concurrency for the threads triple, and its sysroot with libunwind, libc++abi and libc++ built with threads and WebAssembly exceptions (the standard exnref proposal), built by Swift's own build-script with swift.org's release toolchain as the host compiler (no compiler is built). Sources cloned by tag and checked against their commits: swift, llvm-project (sparse) and swift-experimental-string-processing at swift-6.4.2-RELEASE, wasi-libc at wasi-sdk-31. No fork. sdk/patches/swift (git am): - 0001: swiftlang/swift#92018 (multi-threaded wasi-threads Concurrency), backported to 6.4.2's wasistdlib.py; - 0002: the threads sysroot's C++ runtimes with exceptions, assembly compiled for the target, the llvm-project patches applied when missing; - 0003: no embedded stdlib for the threads triple; - 0004: build-script's --build-wasi-threads-stdlib-only (the sysroot and the threads stdlib alone), in place of a Python shim over build-script. sdk/patches/llvm-project (git apply): llvm/llvm-project#168449 and libunwind's assembly empty on wasm. The package: WASI.sdk, the stdlib's static resources with libswiftCxx.a beside them and -lunwind in static-executable-args.lnk, compiler-rt; checked (every object wasm, the EH and thread symbols, __config_site, no legacy EH instruction). A build from the same inputs is kept. Run.swift: a runner whose output streams to the log, and an environment for run(). * sdk/build.sh wasm-threads: a third wasm bundle, wasm32-unknown-wasip1-threads beside wasm32-unknown-wasip1, plain swift-6.4.2-RELEASE_wasm-threads.artifactbundle.tar.gz: swift.org's wasm bundle with the threads runtime (sdk-check build-threads-runtime) as a second target, Isomer's Foundation family built for both, plain only (no ICU for the threads triple). The threads target's swift-sdk.json and toolset.json: atomics, bulk memory, POSIX threads and local-exec TLS for every compile; -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false for C and C++, and -Xllvm -wasm-enable-eh -Xllvm -wasm-use-legacy-eh=false for Swift (SwiftPM hands C options to swiftc as -Xcc, and swift-frontend aborts on that -mllvm option unless its own LLVM has it); the link a shared memory, imported and exported, with a 1 GiB maximum, and -lunwind. rules_swift reads one swift-sdk.json, the threads target's (wasm_sdk threads = True), so the wasip1 target's is wasip1-swift-sdk.json here; SwiftPM takes either by id. The E2 C++ interop maps fix applies to the threads target's identical maps; its Foundation's CWasiThreadIdentity clang module goes in beside it. SwiftPM builds for the threads target run with the toolchain's clang first on PATH (the image's clang 18 knows no -wasm-use-legacy-eh). Its check, under WasmKit 0.5.0 (by sha256) with --wasi-threads for the threads target: the client, the C++ interop client, and sdk-check threads-clients (a TaskGroup of 64 tasks on a task executor over four pthreads with Foundation on each thread; C++ throwing and catching on four std::threads; the C++ object's EH instructions exnref's alone); then undefined, autolink and pairing; swift-testing on the wasip1 target only (the threads runtime has none). sdk-check proof names the threads target's item check-<bundle>-wasm32-threads; draft registers it with `threads = True`. sdk/README.md: the threads SDK. The pack step's times also follow sdk/patches and the runtime's build. * Isomer SDK: wasm-threads in the sdk matrix * The download check runs as the target's programs do (WasmKit 0.5.0 with --wasi-threads for the threads target) --------- Co-authored-by: Agent <344224964+logan-shire-agent[bot]@users.noreply.github.com> Co-authored-by: Logan Shire <[email protected]>
Most minimal change to enable libunwind to build for wasm by excluding
__wasm__from the Windows-style __declspec path in config.h. This unblocks building an exceptions-enabled wasi-sdk sysroot (see WebAssembly/wasi-sdk#565) and builds on prior attempts including yamt’s gist, #79667, and #140365.