Skip to content

Conversation

daxpedda
Copy link
Contributor

@daxpedda daxpedda commented Oct 1, 2025

This prepares us for a future where LLVM eventually stabilizes the atomics target feature, in which case we don't want to inflate atomics with threads. Otherwise users would be stuck with shared memory even when they don't want it/need it.

Context

Currently the atomics target features is unstable and can't be used without re-building Std with it (-Zbuild-std).
Enabling the atomics target feature automatically enables shared memory.
Shared memory is required to actually allow multi-threading.
However, shared memory comes with a performance overhead when atomic instructions aren't able to be lowered to regular memory access instructions or when interacting with certain Web APIs.
So it is very undesirable to enable shared memory by default for the majority of users.

While it is possible to use atomics without shared memory, the question remains what use-case this scenario has.
The only one I can think of would involve multiple memories, where the main memory remains un-shared but a second shared memory exists. While Rust doesn't support multiple memories, it might be possible with inline assembly (#136382).

So alternatively, we might consider not enabling atomics by default even when LLVM does. In which case everything would remain the same.


This will break current Web multi-threading users. To address this they can add the following RUSTFLAGS:

-Clink-args=--shared-memory,--max-memory=1073741824,--import-memory,--export=__wasm_init_tls,--export=__tls_size,--export=__tls_align,--export=__tls_base

We could add a new experimental flag that enables the right linker arguments for users, but I feel that's not in Rusts scope. Or like suggested before: a Rust-only threads target feature.

Addresses #77839.
r? @alexcrichton

@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@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 1, 2025
@rust-log-analyzer

This comment has been minimized.

@daxpedda daxpedda force-pushed the wasm-u-u-atomics-threads branch from 6f67cc5 to 5b809b3 Compare October 1, 2025 13:36
@alexcrichton
Copy link
Member

Should the wasm32-wasip1-threads target specification perhaps get adjusted to pass these arguments by default? Otherwise that target won't actually produce a shared memory by default if I'm understanding this right. Otherwise though this looks reasonable to me, thanks!

@daxpedda
Copy link
Contributor Author

daxpedda commented Oct 1, 2025

AFAIK it already does:

options.add_pre_link_args(
LinkerFlavor::WasmLld(Cc::No),
&["--import-memory", "--export-memory", "--shared-memory"],
);
options.add_pre_link_args(
LinkerFlavor::WasmLld(Cc::Yes),
&[
"--target=wasm32-wasip1-threads",
"-Wl,--import-memory",
"-Wl,--export-memory,",
"-Wl,--shared-memory",
],
);

@bjorn3
Copy link
Member

bjorn3 commented Oct 1, 2025

That is missing the --max-memory, which is mandatory for shared memories.

@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2025

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

@daxpedda
Copy link
Contributor Author

daxpedda commented Oct 1, 2025

That is missing the --max-memory, which is mandatory for shared memories.

Its interesting that so many flags were overlapping then.
I added --max-memory to the affected targets.

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Collaborator

bors commented Oct 1, 2025

📌 Commit 3e8ce2b has been approved by alexcrichton

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 Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

6 participants