Skip to content

Conversation

@sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Oct 8, 2025

This means we no longer link main modules with -pie. The main
advantage here is that main modules no longer require
runtime relocation code for symbols defined locally.

To show the benefits of this approach I build binaryan_wasm
with -sMAIN_MODULE=1 both before and after this change.

Before:

Wasm Size: 22.6M
__wasm_apply_data_relocs size: 123k

After:

Wasm Size: 16.6M
__wasm_apply_data_relocs size: 0 (no longer exists)

Since this is quite a major change, I've kept the -sRELOCATABLE
option available so folks that use this to get the old behaviour when
linking their main module. In order to tests this path too now run
all the @needs_dylink tests in test_core.py with and without
-sRELOCTABLE. Hopefully we can remove this configuration
completely at some point in the future.

Fixes: #12682

@sbc100 sbc100 force-pushed the main_module_static_v2 branch 2 times, most recently from cd1b8ce to e8f95c7 Compare October 22, 2025 19:23
sbc100 added a commit to sbc100/emscripten that referenced this pull request Oct 22, 2025
Also, add an assertion to dynCall.

Split out from emscripten-core#25522.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Oct 22, 2025
Also, add an assertion to dynCall.

Split out from emscripten-core#25522.
@sbc100 sbc100 force-pushed the main_module_static_v2 branch 2 times, most recently from 8c32591 to d40087a Compare October 22, 2025 20:17
@sbc100 sbc100 requested review from dschuff and kripken October 22, 2025 20:22
@sbc100
Copy link
Collaborator Author

sbc100 commented Oct 22, 2025

I still need to get some stats on the benefits of this, but that tests are all now passing so I think this should be good to review now.

An open question is: What do we do about -sRELOCTABLE. Once this change lands we will have almost not testing on this flag. We do have plan to remove it: #25262. One option is to remove -sRELOCATABLE before this change lands?

sbc100 added a commit to sbc100/emscripten that referenced this pull request Oct 22, 2025
Also, add an assertion to dynCall.

Split out from emscripten-core#25522.
@sbc100 sbc100 force-pushed the main_module_static_v2 branch from d40087a to 6008112 Compare October 22, 2025 20:49
@kripken
Copy link
Member

kripken commented Oct 22, 2025

Yeah, I see what you mean, removing RELOCATABLE first would simplify this a little. I don't have a preference though.

I do see some tests still fail here though?

sbc100 added a commit that referenced this pull request Oct 22, 2025
Also, add an assertion to dynCall.

Split out from #25522.
@sbc100 sbc100 force-pushed the main_module_static_v2 branch 5 times, most recently from 637ed91 to f56f631 Compare October 24, 2025 21:07
@sbc100 sbc100 force-pushed the main_module_static_v2 branch 2 times, most recently from 0294236 to d499627 Compare October 28, 2025 00:15
@sbc100
Copy link
Collaborator Author

sbc100 commented Oct 28, 2025

Ok, aside from the problem of what to do with -sRELOCATABLE setting, this change is not ready to go I think.

The stats look really promising. See the new PR description for the great improvements measured when building a dylink version of binaryen.

@sbc100 sbc100 force-pushed the main_module_static_v2 branch from d499627 to 2c6de78 Compare October 28, 2025 00:59
@sbc100 sbc100 force-pushed the main_module_static_v2 branch 2 times, most recently from 23d619c to a2e97ec Compare October 28, 2025 23:57
@sbc100 sbc100 force-pushed the main_module_static_v2 branch from a2e97ec to 4425035 Compare October 29, 2025 00:27
@sbc100 sbc100 requested a review from kleisauke October 29, 2025 00:29
@sbc100
Copy link
Collaborator Author

sbc100 commented Oct 29, 2025

I solves the -sRELOCTABLE issue by keeping that available as an option and test that configuration too.

@sbc100 sbc100 force-pushed the main_module_static_v2 branch 2 times, most recently from c7635d5 to a2a8c13 Compare October 29, 2025 16:31
@sbc100 sbc100 requested a review from kripken October 29, 2025 18:00
@sbc100 sbc100 force-pushed the main_module_static_v2 branch from a2a8c13 to 79e885d Compare October 29, 2025 18:11
@sbc100
Copy link
Collaborator Author

sbc100 commented Oct 29, 2025

I think these change should be more of less ready to land now.

The old -sRELOCTABLE mode is still available for now.

Once I land this I will likely send a message to mailing list too since its a fairly large change for users of dynamic linking.

if relocatable:
# Since `-sMAIN_MODULE` no longer implies `-sRELOCATABLE` but we want
# to keep that cominbation working we run all the `@needs_dylink` tests
# both with and without the explicit `-sRELOCATABLE`
Copy link
Member

Choose a reason for hiding this comment

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

This is quite a lot of slow tests - is it worth it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is important as long as we want to offer people a fallback using -sRELOCATABLE for some period of time, which I do think is good idea.

Once we remove the fallback option we can then remove all these tests.

Copy link
Member

Choose a reason for hiding this comment

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

How much time does it take? I just vaguely worry dylink tests are most of our core time, and doubling them would almost double the total...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These two tests total 61 out of 1176 total core2 tests.

In terms of runtime of the core2 test suite they comprise 9 seconds of the full runtime of 63 seconds (I skipped the slow tests there too).

Copy link
Member

Choose a reason for hiding this comment

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

Ok, sg, thanks for checking.

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

lgtm % comments now and before

@sbc100 sbc100 force-pushed the main_module_static_v2 branch from 5bbdf80 to 0cdfabc Compare October 29, 2025 18:21
The main advantage here is that main module no longer requires
relocation entries for symbols defined locally.

To show the benefits of this approach I build binaryan_wasm
with `-sMAIN_MODULE=1` both before and after this change.

Before:

Wasm Size: 22.6M
__wasm_apply_data_relocs size: 123k

After:

Wasm Size: 16.6M
__wasm_apply_data_relocs size: 0 (no longer exists)

Fixes: emscripten-core#12682
@sbc100 sbc100 force-pushed the main_module_static_v2 branch from 0cdfabc to 55ea688 Compare October 29, 2025 18:30
@sbc100 sbc100 enabled auto-merge (squash) October 29, 2025 18:36
@sbc100 sbc100 merged commit 4d16f75 into emscripten-core:main Oct 29, 2025
34 checks passed
@sbc100 sbc100 deleted the main_module_static_v2 branch October 29, 2025 19:58
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.

Stop building MAIN_MODULE with RELOCATABLE (-fPIC)

3 participants