Commit 3daf0f0
authored
[EH] Don't export unnecessary functions for EH when not used (emscripten-core#26493)
When we enabled (Wasm / Emscripten) EH and didn't use it, we still
pulled in a lot of JS library functions + libc++abi library functions
that were not DCE'ed. The reason was we mandatorily exported many
functions whenever some EH options were enabled, regardless of whether
they were used or not.
1. We used to enable `EXPORT_EXCEPTION_HANDLING_HELPERS`, which exports
`getExceptionMessage` and `in/decrementExceptionRefcount`, whenever
`EXCEPTION_STACK_TRACES` was true. And `EXCEPTION_STACK_TRACES` is true
whenever `ASSERTIONS` is true, which is the default at `-O0`. And those
exported functions can pull in many libc++abi functions. As a result, at
`-O0`, we pulled in a lot of functions even when we were not using any
exceptions.
This PR removes that `EXCEPTION_STACK_TRACES` ->
`EXPORT_EXCEPTION_HANDLING_HELPERS` link. Without this link, we can
still see stack traces with exception messages with no problem, because
`__cxa_throw` -> `__throw_exception_with_stack_trace` ->
`getExceptionMessage` dependencies:
https://github.com/emscripten-core/emscripten/blob/6ad2f5e03021a39377428e9d476985fc967014d4/system/lib/libcxxabi/src/cxa_exception.cpp#L302-L304
https://github.com/emscripten-core/emscripten/blob/6ad2f5e03021a39377428e9d476985fc967014d4/src/lib/libexceptions.js#L311
2. If we do 1, Emscripten EH's `getExceptionMessage` does not work,
because unlike Wasm EH, its `getExceptionMessage` dependency is within
`runtime_exceptions.js`, where we can't attach `__deps`, so we can't
track it:
https://github.com/emscripten-core/emscripten/blob/6ad2f5e03021a39377428e9d476985fc967014d4/src/runtime_exceptions.js#L20
So, this adds `getExceptionMessage` as a dependency of
`libexception.js`'s `__cxa_throw` directly, when
`EXCEPTION_STACK_TRACES` is enabled.
3. This removes several functions from `REQUIRED_EXPORTS` when
Emscripten EH is used. Previously, the comment said, in LTO mode,
`_cxa_find_matching_catch_*` -> `__cxa_can_catch` dependency was not
tracked. But now, in Emscripten EH, we preemptively add several
`__cxa_find_matching_catch_n`s, and it depends on `findMatchingCatch`,
which depends on `__cxa_end_catch`:
https://github.com/emscripten-core/emscripten/blob/78403050f355085104175499224ad0f6bccb5fb1/src/lib/libexceptions.js#L371-L405
https://github.com/emscripten-core/emscripten/blob/78403050f355085104175499224ad0f6bccb5fb1/src/lib/libexceptions.js#L217
So we don't need to require exporting `__cxa_end_catch` anymore.
Also, other required exports (`__cxa_in/decrement_exception_count` and
`__cxa_free_exceptions`) are dependencies that can be naturally referred
within libc++abi. I think these were added here in emscripten-core#16627 when we were
using `deps_info.py`, which we don't use anymore.
After this commit, when you compile an empty `int main { return 0; }`
with `-O0` and `-fexceptions`/`-fwasm-exceptions`, the size decreases
to:
- `-fexceptions`: 113212 -> 1168
- `-fwasm-exceptions`: 109177 -> 11061 parent 8f9388a commit 3daf0f0
File tree
5 files changed
+32
-32
lines changed- src/lib
- test
- codesize
- tools
5 files changed
+32
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
88 | 94 | | |
89 | 95 | | |
90 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
91 | 90 | | |
92 | 91 | | |
93 | 92 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
92 | 91 | | |
93 | 92 | | |
94 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8692 | 8692 | | |
8693 | 8693 | | |
8694 | 8694 | | |
| 8695 | + | |
| 8696 | + | |
| 8697 | + | |
| 8698 | + | |
| 8699 | + | |
| 8700 | + | |
| 8701 | + | |
| 8702 | + | |
| 8703 | + | |
| 8704 | + | |
| 8705 | + | |
| 8706 | + | |
| 8707 | + | |
| 8708 | + | |
8695 | 8709 | | |
8696 | 8710 | | |
8697 | 8711 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1703 | 1703 | | |
1704 | 1704 | | |
1705 | 1705 | | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
1706 | 1709 | | |
1707 | | - | |
1708 | | - | |
1709 | | - | |
1710 | | - | |
1711 | | - | |
1712 | | - | |
1713 | | - | |
1714 | | - | |
1715 | | - | |
1716 | | - | |
1717 | | - | |
1718 | | - | |
1719 | | - | |
1720 | | - | |
1721 | | - | |
1722 | | - | |
1723 | | - | |
1724 | | - | |
1725 | | - | |
1726 | | - | |
| 1710 | + | |
1727 | 1711 | | |
1728 | 1712 | | |
1729 | 1713 | | |
| |||
1812 | 1796 | | |
1813 | 1797 | | |
1814 | 1798 | | |
1815 | | - | |
1816 | | - | |
1817 | 1799 | | |
1818 | 1800 | | |
1819 | 1801 | | |
| |||
0 commit comments