Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move addOnInit and __ATINIT__ into a JS library. #23433

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

brendandahl
Copy link
Collaborator

@brendandahl brendandahl commented Jan 16, 2025

Make it so the addOnInit code is not emitted if it is not needed.

Since preamble.js is not a library I changed the call to
__wasm_call_ctors to be inlined like it is in MINIMAL_RUNTIME. I also
had to add various dependencies on addOnInit and __ATINIT__.

Make it so the `addOnInit` code is not emitted if it is not needed.

Since preamble.js is not a library I changed the call to
`__wasm_call_ctors` to be inlined like it is in MINIMAL_RUNTIME. I also
had to add various dependencies on addOnInit and __ATINIT__.
This is an automatic change generated by tools/maint/rebaseline_tests.py.

The following (97) test expectation files were updated by
running the tests with `--rebaseline`:

```
browser/test_small_js_flags.js.size: 4341 => 4356 [+15 bytes / +0.35%]
other/codesize/test_codesize_cxx_ctors1.gzsize: 8343 => 8335 [-8 bytes / -0.10%]
other/codesize/test_codesize_cxx_ctors1.jssize: 20273 => 20251 [-22 bytes / -0.11%]
other/codesize/test_codesize_cxx_ctors1.size: 129211 => 129218 [+7 bytes / +0.01%]
other/codesize/test_codesize_cxx_ctors2.gzsize: 8327 => 8324 [-3 bytes / -0.04%]
other/codesize/test_codesize_cxx_ctors2.jssize: 20241 => 20229 [-12 bytes / -0.06%]
other/codesize/test_codesize_cxx_ctors2.size: 128623 => 128630 [+7 bytes / +0.01%]
other/codesize/test_codesize_cxx_except.gzsize: 9343 => 9337 [-6 bytes / -0.06%]
other/codesize/test_codesize_cxx_except.jssize: 24041 => 24018 [-23 bytes / -0.10%]
other/codesize/test_codesize_cxx_except.size: 170879 => 170886 [+7 bytes / +0.00%]
other/codesize/test_codesize_cxx_except_wasm.gzsize: 8294 => 8280 [-14 bytes / -0.17%]
other/codesize/test_codesize_cxx_except_wasm.jssize: 20166 => 20143 [-23 bytes / -0.11%]
other/codesize/test_codesize_cxx_except_wasm.size: 142162 => 142169 [+7 bytes / +0.00%]
other/codesize/test_codesize_cxx_except_wasm_exnref.gzsize: 8294 => 8280 [-14 bytes / -0.17%]
other/codesize/test_codesize_cxx_except_wasm_exnref.jssize: 20166 => 20143 [-23 bytes / -0.11%]
other/codesize/test_codesize_cxx_except_wasm_exnref.size: 144708 => 144715 [+7 bytes / +0.00%]
other/codesize/test_codesize_cxx_lto.gzsize: 8357 => 8352 [-5 bytes / -0.06%]
other/codesize/test_codesize_cxx_lto.jssize: 20348 => 20325 [-23 bytes / -0.11%]
other/codesize/test_codesize_cxx_mangle.gzsize: 9349 => 9341 [-8 bytes / -0.09%]
other/codesize/test_codesize_cxx_mangle.jssize: 24041 => 24018 [-23 bytes / -0.10%]
other/codesize/test_codesize_cxx_mangle.size: 232724 => 232731 [+7 bytes / +0.00%]
other/codesize/test_codesize_cxx_noexcept.gzsize: 8343 => 8335 [-8 bytes / -0.10%]
other/codesize/test_codesize_cxx_noexcept.jssize: 20273 => 20251 [-22 bytes / -0.11%]
other/codesize/test_codesize_cxx_noexcept.size: 131774 => 131781 [+7 bytes / +0.01%]
other/codesize/test_codesize_cxx_wasmfs.gzsize: 3564 => 3553 [-11 bytes / -0.31%]
other/codesize/test_codesize_cxx_wasmfs.jssize: 7676 => 7654 [-22 bytes / -0.29%]
other/codesize/test_codesize_cxx_wasmfs.size: 169181 => 169188 [+7 bytes / +0.00%]
other/codesize/test_codesize_files_js_fs.gzsize: 7647 => 7634 [-13 bytes / -0.17%]
other/codesize/test_codesize_files_js_fs.jssize: 18820 => 18798 [-22 bytes / -0.12%]
other/codesize/test_codesize_files_wasmfs.gzsize: 2804 => 2792 [-12 bytes / -0.43%]
other/codesize/test_codesize_files_wasmfs.jssize: 5984 => 5963 [-21 bytes / -0.35%]
other/codesize/test_codesize_hello_O0.gzsize: 7883 => 7882 [-1 bytes / -0.01%]
other/codesize/test_codesize_hello_O0.jssize: 20983 => 20964 [-19 bytes / -0.09%]
other/codesize/test_codesize_hello_O0.size: 15101 => 15110 [+9 bytes / +0.06%]
other/codesize/test_codesize_hello_O1.gzsize: 2658 => 2646 [-12 bytes / -0.45%]
other/codesize/test_codesize_hello_O1.jssize: 6837 => 6808 [-29 bytes / -0.42%]
other/codesize/test_codesize_hello_O2.gzsize: 2320 => 2307 [-13 bytes / -0.56%]
other/codesize/test_codesize_hello_O2.jssize: 4778 => 4757 [-21 bytes / -0.44%]
other/codesize/test_codesize_hello_O3.gzsize: 2278 => 2267 [-11 bytes / -0.48%]
other/codesize/test_codesize_hello_O3.jssize: 4720 => 4699 [-21 bytes / -0.44%]
other/codesize/test_codesize_hello_Os.gzsize: 2278 => 2267 [-11 bytes / -0.48%]
other/codesize/test_codesize_hello_Os.jssize: 4720 => 4699 [-21 bytes / -0.44%]
other/codesize/test_codesize_hello_Oz.gzsize: 2278 => 2267 [-11 bytes / -0.48%]
other/codesize/test_codesize_hello_Oz.jssize: 4720 => 4699 [-21 bytes / -0.44%]
other/codesize/test_codesize_hello_dylink.gzsize: 5992 => 5994 [+2 bytes / +0.03%]
other/codesize/test_codesize_hello_dylink.jssize: 13241 => 13230 [-11 bytes / -0.08%]
other/codesize/test_codesize_hello_export_nothing.gzsize: 1678 => 1665 [-13 bytes / -0.77%]
other/codesize/test_codesize_hello_export_nothing.jssize: 3633 => 3614 [-19 bytes / -0.52%]
other/codesize/test_codesize_hello_wasmfs.gzsize: 2278 => 2267 [-11 bytes / -0.48%]
other/codesize/test_codesize_hello_wasmfs.jssize: 4720 => 4699 [-21 bytes / -0.44%]
other/codesize/test_codesize_libcxxabi_message_O3.gzsize: 1874 => 1862 [-12 bytes / -0.64%]
other/codesize/test_codesize_libcxxabi_message_O3.jssize: 3996 => 3977 [-19 bytes / -0.48%]
other/codesize/test_codesize_libcxxabi_message_O3_standalone.gzsize: 1912 => 1907 [-5 bytes / -0.26%]
other/codesize/test_codesize_libcxxabi_message_O3_standalone.jssize: 4044 => 4034 [-10 bytes / -0.25%]
other/codesize/test_codesize_mem_O3.gzsize: 2326 => 2315 [-11 bytes / -0.47%]
other/codesize/test_codesize_mem_O3.jssize: 4888 => 4867 [-21 bytes / -0.43%]
other/codesize/test_codesize_mem_O3_grow.gzsize: 2474 => 2460 [-14 bytes / -0.57%]
other/codesize/test_codesize_mem_O3_grow.jssize: 5172 => 5151 [-21 bytes / -0.41%]
other/codesize/test_codesize_mem_O3_grow_standalone.gzsize: 2175 => 2169 [-6 bytes / -0.28%]
other/codesize/test_codesize_mem_O3_grow_standalone.jssize: 4581 => 4571 [-10 bytes / -0.22%]
other/codesize/test_codesize_mem_O3_standalone.gzsize: 2141 => 2133 [-8 bytes / -0.37%]
other/codesize/test_codesize_mem_O3_standalone.jssize: 4513 => 4503 [-10 bytes / -0.22%]
other/codesize/test_codesize_mem_O3_standalone_lib.gzsize: 1898 => 1891 [-7 bytes / -0.37%]
other/codesize/test_codesize_mem_O3_standalone_lib.jssize: 4043 => 4033 [-10 bytes / -0.25%]
other/codesize/test_codesize_mem_O3_standalone_narg.gzsize: 1912 => 1907 [-5 bytes / -0.26%]
other/codesize/test_codesize_mem_O3_standalone_narg.jssize: 4044 => 4034 [-10 bytes / -0.25%]
other/codesize/test_codesize_mem_O3_standalone_narg_flto.gzsize: 1912 => 1907 [-5 bytes / -0.26%]
other/codesize/test_codesize_mem_O3_standalone_narg_flto.jssize: 4044 => 4034 [-10 bytes / -0.25%]
other/codesize/test_codesize_minimal_64.gzsize: 1441 => 1425 [-16 bytes / -1.11%]
other/codesize/test_codesize_minimal_64.jssize: 3058 => 3011 [-47 bytes / -1.54%]
other/codesize/test_codesize_minimal_O0.gzsize: 6449 => 6440 [-9 bytes / -0.14%]
other/codesize/test_codesize_minimal_O0.jssize: 17094 => 16995 [-99 bytes / -0.58%]
other/codesize/test_codesize_minimal_O1.gzsize: 1534 => 1518 [-16 bytes / -1.04%]
other/codesize/test_codesize_minimal_O1.jssize: 3702 => 3615 [-87 bytes / -2.35%]
other/codesize/test_codesize_minimal_O2.gzsize: 1380 => 1366 [-14 bytes / -1.01%]
other/codesize/test_codesize_minimal_O2.jssize: 2806 => 2759 [-47 bytes / -1.67%]
other/codesize/test_codesize_minimal_O3.gzsize: 1347 => 1332 [-15 bytes / -1.11%]
other/codesize/test_codesize_minimal_O3.jssize: 2756 => 2709 [-47 bytes / -1.71%]
other/codesize/test_codesize_minimal_Os.gzsize: 1347 => 1332 [-15 bytes / -1.11%]
other/codesize/test_codesize_minimal_Os.jssize: 2756 => 2709 [-47 bytes / -1.71%]
other/codesize/test_codesize_minimal_Oz-ctors.gzsize: 1338 => 1326 [-12 bytes / -0.90%]
other/codesize/test_codesize_minimal_Oz-ctors.jssize: 2741 => 2704 [-37 bytes / -1.35%]
other/codesize/test_codesize_minimal_Oz.gzsize: 1347 => 1332 [-15 bytes / -1.11%]
other/codesize/test_codesize_minimal_Oz.jssize: 2756 => 2709 [-47 bytes / -1.71%]
other/codesize/test_codesize_minimal_esm.gzsize: 1542 => 1526 [-16 bytes / -1.04%]
other/codesize/test_codesize_minimal_esm.jssize: 3210 => 3164 [-46 bytes / -1.43%]
other/codesize/test_codesize_minimal_pthreads.gzsize: 4186 => 4185 [-1 bytes / -0.02%]
other/codesize/test_codesize_minimal_pthreads.jssize: 8728 => 8754 [+26 bytes / +0.30%]
other/codesize/test_codesize_minimal_wasmfs.gzsize: 1347 => 1332 [-15 bytes / -1.11%]
other/codesize/test_codesize_minimal_wasmfs.jssize: 2756 => 2709 [-47 bytes / -1.71%]
other/test_INCOMING_MODULE_JS_API.js.size: 3733 => 3748 [+15 bytes / +0.40%]
other/test_unoptimized_code_size.js.size: 52739 => 52595 [-144 bytes / -0.27%]
other/test_unoptimized_code_size.wasm.size: 15101 => 15110 [+9 bytes / +0.06%]
other/test_unoptimized_code_size_no_asserts.js.size: 28487 => 28327 [-160 bytes / -0.56%]
other/test_unoptimized_code_size_no_asserts.wasm.size: 12182 => 12194 [+12 bytes / +0.10%]
other/test_unoptimized_code_size_strict.js.size: 51522 => 51378 [-144 bytes / -0.28%]
other/test_unoptimized_code_size_strict.wasm.size: 15101 => 15110 [+9 bytes / +0.06%]

Average change: -0.43% (-2.35% - +0.40%)
```
@@ -2296,6 +2296,16 @@ addToLibrary({

$noExitRuntime: "{{{ makeModuleReceiveExpr('noExitRuntime', !EXIT_RUNTIME) }}}",

// functions called during startup
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO: add changelog.

@brendandahl brendandahl marked this pull request as draft January 16, 2025 21:10
@brendandahl
Copy link
Collaborator Author

I've got an issue with pthreads ordering. __wasm_call_ctors needs to happen after some calls, but before other addOnInit code is run. Hmm...

@@ -2296,6 +2296,16 @@ addToLibrary({

$noExitRuntime: "{{{ makeModuleReceiveExpr('noExitRuntime', !EXIT_RUNTIME) }}}",

// functions called during startup
$__ATINIT__: [],
$__ATINIT____postset: () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I keep forgetting that __postset can be a function...

@@ -701,6 +701,7 @@ def phase_linker_setup(options, state): # noqa: C901, PLR0912, PLR0915
exit_with_error('PTHREADS_PROFILING only works with ASSERTIONS enabled')
options.post_js.append(utils.path_from_root('src/threadprofiler.js'))
settings.REQUIRED_EXPORTS.append('emscripten_main_runtime_thread_id')
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$addOnInit']
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this? Maybe worth a comment?

@@ -97,7 +97,7 @@ def getExtendedAttribute(self, _name):
#include <emscripten.h>
#include <stdlib.h>

EM_JS_DEPS(webidl_binder, "$intArrayFromString,$UTF8ToString,$alignMemory");
EM_JS_DEPS(webidl_binder, "$intArrayFromString,$UTF8ToString,$alignMemory,$addOnInit");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this? I guess because the generated code uses addOnInit?

@@ -709,7 +709,7 @@ export const ATMAINS = [];
export const ATINITS = [];

function addAtInit(code) {
ATINITS.push(code);
ATINITS.unshift(code);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this change?

@@ -2296,6 +2296,16 @@ addToLibrary({

$noExitRuntime: "{{{ makeModuleReceiveExpr('noExitRuntime', !EXIT_RUNTIME) }}}",

// functions called during startup
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps this comment could be a little more descriptive? I'm not sure if this is correct but maybe "Function the are called once the runtime is initialized and static constructors have been called".

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

I like the idea in general!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants