Skip to content

Emscripten: link with -sWASM_BIGINT - #131736

Merged
bors merged 1 commit into
rust-lang:masterfrom
hoodmane:emscripten-wasm-bigint
Nov 21, 2024
Merged

bors merged 1 commit into
rust-lang:masterfrom
hoodmane:emscripten-wasm-bigint

Conversation

@hoodmane

@hoodmane hoodmane commented Oct 15, 2024 •

Copy link
Copy Markdown
Contributor

When linking an executable without dynamic linking, this is a pure improvement. It significantly reduces code size and avoids a lot of buggy behaviors. It is supported in all browsers for many years and in all maintained versions of Node.

It does change the ABI, so people who are dynamically linking with a library or executable that uses the old ABI may need to turn it off. It can be disabled if needed by passing -Clink-arg -sWASM_BIGINT=0 to rustc. But few people will want to turn it off.

Note this includes a libc bump to 0.2.162!

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 15, 2024
@jieyouxu

ghost commented Oct 15, 2024

Copy link
Copy Markdown
Member

I'm going to ask Alex to review this because I don't know much about this. r? @alexcrichton

If Alex thinks this is a good idea, then we can go ahead and also mark this with relnotes.

@rustbot rustbot assigned alexcrichton and unassigned jieyouxu Oct 15, 2024
@hoodmane

ghost commented Oct 15, 2024

Copy link
Copy Markdown
Contributor Author

@sbc100 have you thought about making -sWASM_BIGINT the default in general? I guess the fact that it requires Safari 15 is the most onerous requirement but Safari 15 came out on September 20, 2021 a bit over 3 years ago. All versions of node that don't support it are EOL and the others browsers have supported it since 2019.

@juntyr

ghost commented Oct 15, 2024

Copy link
Copy Markdown
Contributor

If #131582 lands first, this PR should probably update the enabled WASM features section

@sbc100

ghost commented Oct 15, 2024

Copy link
Copy Markdown

@sbc100 have you thought about making -sWASM_BIGINT the default in general? I guess the fact that it requires Safari 15 is the most onerous requirement but Safari 15 came out on September 20, 2021 a bit over 3 years ago. All versions of node that don't support it are EOL and the others browsers have supported it since 2019.

Yes, -sWASM_BIGINT should be the default pretty soon. And yes, that involves bumping the default min safari version, which we didn't want to do last time I looked into it. I think the time might be right. There are couple of other blockers for that too, such as wasm2js support.

@sbc100

ghost commented Oct 15, 2024

Copy link
Copy Markdown

@sbc100 have you thought about making -sWASM_BIGINT the default in general? I guess the fact that it requires Safari 15 is the most onerous requirement but Safari 15 came out on September 20, 2021 a bit over 3 years ago. All versions of node that don't support it are EOL and the others browsers have supported it since 2019.

Yes, -sWASM_BIGINT should be the default pretty soon. And yes, that involves bumping the default min safari version, which we didn't want to do last time I looked into it. I think the time might be right. There are couple of other blockers for that too, such as wasm2js support.

See emscripten-core/emscripten#19156 and

@alexcrichton

ghost commented Oct 16, 2024

Copy link
Copy Markdown
Member

I'm not an expert on the Emscripten side of things so in terms of "is this the right time?" I'd defer to the folks who know more, so sounds like this is a green-light there and good to go.

One question I would have though is that you mention that this changes the ABI. Does that mean that this would impact C/C++/Rust code being linked together (statically, not dynamically)? For example does the cc crate need to update to pass this flag by default as well to have Rust/C/C++ all interoperate after this PR lands?

@hoodmane

ghost commented Oct 16, 2024

Copy link
Copy Markdown
Contributor Author

It's only a link flag so no action is needed for static libraries. Any dynamic libraries also need to be linked with this flag.

@alexcrichton

ghost commented Oct 16, 2024

Copy link
Copy Markdown
Member

Ok great! If this is going to be the default in Emscripten soon though should rustc follow Emscripten's defaults rather than having its own configuration? If Emscripten switches by default then users would only "just" need to upgrade their Emscripten toolchain, right?

If we do go the rustc route though mind modifying the target definition instead of the linker definition?

@hoodmane
hoodmane force-pushed the emscripten-wasm-bigint branch from 306e7b4 to 7f4f3d8 Compare October 16, 2024 07:23
@rustbot

ghost commented Oct 16, 2024

Copy link
Copy Markdown
Collaborator

These commits modify compiler targets.
(See the Target Tier Policy.)

@hoodmane

ghost commented Oct 16, 2024

Copy link
Copy Markdown
Contributor Author

This flag does make a significant in the quality of the linked executable so people should be using it unless they have a reason not to. But we could indeed wait and stay with the Emscripten defaults if they are switching it soon.

@hoodmane

ghost commented Oct 16, 2024 •

Copy link
Copy Markdown
Contributor Author

@alexcrichton Now I remember Rust dynamic libraries linked without -sWASM_BIGINT don't work at all (unless they are compiled with unwind=abort) and the recommended solution was to link with -sWASM_BIGINT. So Rust is particularly seriously affected by this.
emscripten-core/emscripten#16693

On this basis, I'd say we should move forward with merging this.

@workingjubilee

ghost commented Oct 16, 2024 •

Copy link
Copy Markdown
Member

@hoodmane Can you confirm whether we might want updates to the cc crate as well? I'm not sure I understand the matrix of outcomes from the previous statements about static vs. dynamic linkage.

Obviously not when it becomes the default, so I think we're fine just merging this now. I don't even know that we actually want to patch cc here. I just want a yes/no: is this conceivably breaking a build script that uses cc somewhere and then links that with Rust code?

@alexcrichton

ghost commented Oct 16, 2024

Copy link
Copy Markdown
Member

On this basis, I'd say we should move forward with merging this.

Sounds good to me! r=me assuming the answer to @workingjubilee's question is "no breakage" (which give my understanding is the answer, but would be good for @hoodmane you to confirm)

@hoodmane

ghost commented Oct 17, 2024

Copy link
Copy Markdown
Contributor Author

Yeah. cc-rs doesn't know how to link emscripten shared libraries and for static libraries no change is needed. We should probably teach it out to link emscripten shared libraries eventually and then we'll need to add this flag if we want it to work.

@hoodmane

ghost commented Oct 17, 2024

Copy link
Copy Markdown
Contributor Author

Actually, linking with -lc -sWASM_BIGINT also seems to be broken emscripten-core/emscripten#22758. Emscripten really doesn't like it when you pass -lc @sbc100. It seems like that bug is a blocker.

@workingjubilee

ghost commented Oct 17, 2024

Copy link
Copy Markdown
Member

Cool, let's... Oh, we can't actually merge this? Damn.

@sbc100

ghost commented Oct 17, 2024

Copy link
Copy Markdown

Actually, linking with -lc -sWASM_BIGINT also seems to be broken emscripten-core/emscripten#22758. Emscripten really doesn't like it when you pass -lc @sbc100. It seems like that bug is a blocker.

Yes, the explicit -lc is likely the issue. I guess it kind of works but doesn't always select the correct variant of libc based on your settings perhaps? If you can remove the -lc that that would a simple fix on the rust end.

@hoodmane

ghost commented Oct 18, 2024 •

Copy link
Copy Markdown
Contributor Author

Okay I opened an Emscripten PR to hopefully this: emscripten-core/emscripten#22765

In the meantime, can we adjust the EmLinker to specifically drop -lc to unblock this? I tried to make this change a few years ago, but it was rejected as being weird and hacky and generally concerning. Instead, we were passing -Zlink-native-libraries=no but that creates a different set of problems when there are packages other than libc which actually do need to be linked.
#98303

I think if we insert this with a comment saying that it's a hack around emscripten-core/emscripten#22758 and we can remove it when we drop support for Emscripten 3.1.69 it should be okay?
cc @petrochenkov who rejected that change before.

@hoodmane

ghost commented Oct 18, 2024 •

Copy link
Copy Markdown
Contributor Author

Also opened a PR to work around it on Rust's side: #131885

@workingjubilee workingjubilee added the O-emscripten Target: 50% off wasm32-unknown-musl. the savings come out of stdio.h, but hey, you get SDL! label Oct 21, 2024
When linking an executable without dynamic linking, this is a pure improvement.
It significantly reduces code size and avoids a lot of buggy behaviors. It is
supported in all browsers for many years and in all maintained versions of
Node.

It does change the ABI, so people who are dynamically linking with a library
or executable that uses the old ABI may need to turn it off. It can be disabled
if needed by passing `-Clink-arg -sWASM_BIGINT=0` to `rustc`. But few people
will want to turn it off.
@hoodmane
hoodmane force-pushed the emscripten-wasm-bigint branch from 7f4f3d8 to b071f64 Compare November 11, 2024 16:27
@rustbot

ghost commented Nov 11, 2024

Copy link
Copy Markdown
Collaborator

These commits modify the library/Cargo.lock file. Unintentional changes to library/Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@hoodmane

ghost commented Nov 12, 2024

Copy link
Copy Markdown
Contributor Author

Cherry picking #132720 on top of this, I get:

$ ./x.py test --target wasm32-unknown-emscripten ./tests/ui/
...
test result: FAILED. 17199 passed; 4 failed; 735 ignored; 0 measured; 0 filtered out; finished in 517.19s

so one fewer ui test failure than on the main branch. So @alexcrichton @workingjubilee if the bump to libc 0.2.162 is okay then I think this can be merged.

@hoodmane

ghost commented Nov 20, 2024

Copy link
Copy Markdown
Contributor Author

@alexcrichton @workingjubilee ping. Can we merge this?

@workingjubilee

ghost commented Nov 20, 2024

Copy link
Copy Markdown
Member

Oh! Hi.

@workingjubilee

ghost commented Nov 20, 2024

Copy link
Copy Markdown
Member

Yeah, I think we're good here.

@bors r+ rollup

@bors

ghost commented Nov 20, 2024

Copy link
Copy Markdown
Contributor

📌 Commit b071f64 has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 20, 2024
@workingjubilee workingjubilee self-assigned this Nov 20, 2024
@bors
bors merged commit 61878ec into rust-lang:master Nov 21, 2024
@rustbot rustbot added this to the 1.84.0 milestone Nov 21, 2024
@hoodmane
hoodmane deleted the emscripten-wasm-bigint branch November 21, 2024 10:57
@hoodmane

ghost commented Nov 21, 2024

Copy link
Copy Markdown
Contributor Author

Thanks @workingjubilee and @alexcrichton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-emscripten Target: 50% off wasm32-unknown-musl. the savings come out of stdio.h, but hey, you get SDL! S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants