Skip to content

WASI threads, implementation of wasm32-wasi-preview1-threads target - #112922

Merged
bors merged 1 commit into
rust-lang:masterfrom
g0djan:godjan/wasi-threads
Aug 2, 2023
Merged

WASI threads, implementation of wasm32-wasi-preview1-threads target#112922
bors merged 1 commit into
rust-lang:masterfrom
g0djan:godjan/wasi-threads

Conversation

@g0djan

@g0djan g0djan commented Jun 22, 2023

Copy link
Copy Markdown
Contributor

This PR adds a target proposed in rust-lang/compiler-team#574 by @abrown and implementation of std::thread::spawn for the target wasm32-wasi-preview1-threads

Tier 3 Target Policy

As tier 3 targets, the new targets are required to adhere to the tier 3 target policy requirements. This section quotes each requirement in entirety and describes how they are met.

  • A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)

See src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md.

  • Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
  • Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
    If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.

The target is using the same name for $ARCH=wasm32 and $OS=wasi as existing Rust targets. The suffix preview1 introduced to accurately set expectations because eventually this target will be deprecated and follows MCP 607. The suffix threads indicates that it’s an extension that enables threads to the existing target and it follows MCP 574 which describes the rationale behind introducing a separate target.

  • Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
  • The target must not introduce license incompatibilities.
  • Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
  • The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
  • Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
  • "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.

This PR does not introduce any new dependency.
The new target doesn’t support building host tools.

Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.

The full standard library is available for this target as it’s an extension to an existing target that has already supported it.

The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.

Only manual test running is supported at the moment with some tweaks in the test runner codebase. For build and running tests see src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md.

  • Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
  • This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
  • Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
  • Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
  • Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
  • In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.

I acknowledge these requirements and intend to ensure they are met.

@rustbot

rustbot commented Jun 22, 2023

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cuviper (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 22, 2023
@rustbot

rustbot commented Jun 22, 2023

Copy link
Copy Markdown
Collaborator

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

@rust-log-analyzer

This comment has been minimized.

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IIRC this won't get much testing on Rust CI (unsure if things have changed since I last looked), so it might be worth running tests locally not only for this target but also for the wasm32-wasi target if you have some time (not perhaps the ui test suite but various programs locally)

Comment thread library/std/src/sys/wasi/thread.rs Outdated
Comment thread library/std/src/sys/wasi/tests/atomic_test.rs Outdated
Comment thread library/std/src/sys/wasi/os.rs
Comment thread library/std/src/sys/wasi/mod.rs Outdated
Comment thread library/std/Cargo.toml Outdated
Comment thread compiler/rustc_target/src/spec/wasm32_wasi_threads.rs Outdated
@g0djan

g0djan commented Jun 24, 2023

Copy link
Copy Markdown
Contributor Author

(not perhaps the ui test suite but various programs locally)

Is it too big or why not?

Just checked that threads pass 51/65 from tests/ui/threads-sendsync, 5 tests doesn't compile and 9 fails.

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it too big or why not?

Mostly in that I don't think it's regularly run with the WASI targets right now so it's not as simple as "run this and make sure it passes", you'd have to do the legwork of running it and figuring out if failures are relevant or not.

Comment thread library/std/src/sys/wasi/thread.rs Outdated
Comment thread library/std/src/sys/wasi/mod.rs Outdated

@abrown abrown left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excited to try this out!

Comment thread library/std/src/sys/wasi/thread.rs Outdated
@g0djan

g0djan commented Jun 29, 2023

Copy link
Copy Markdown
Contributor Author

IIRC this won't get much testing on Rust CI (unsure if things have changed since I last looked), so it might be worth running tests locally not only for this target but also for the wasm32-wasi target if you have some time (not perhaps the ui test suite but various programs locally)

@alexcrichton
Tested with an app and it's 2 testuites.
For both targets wasm32-wasi and wasm32-wasi-threads an app worked normally and both testuites passed. It's 1133 tests + 214 tests

Would it be enough of testing or do you know any open-source rust projects that are already getting compiled to wasm32-wasi?

@deltragon

Copy link
Copy Markdown
Contributor

Should this be blocked on #110596? (See rust-lang/compiler-team#607 for context)
It seems impractical to add the wasm32-wasi-threads target, only to rename it to (presumably?) wasm32-wasi-threads-preview1 soon after.
Alternatively, it could pick a new name for the target immediately, and not wait for the renaming of the existing target, I think.

Comment thread library/std/src/sys/wasi/thread.rs

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good job!

Comment thread library/std/src/sys/wasi/thread.rs Outdated
@mirkootter

ghost commented Jun 30, 2023

Copy link
Copy Markdown
Contributor

Just a note regarding CI testing. Note sure if you have already found out by yourself, but a few details I found out for my own tests:

  • only emscripten and wasm32-unknown-unknown is currently tested in CI (not sure though, but I think so)
  • the testing happens in the Docker files in src/ci/docker/. For example, the "test-various" system runs tests for the wasm32-unknown-unknown target.
  • There a few tests which "run" wasm code, using nodejs. For example tests/run-make/wasm-abi
  • If you need a special runtime (i.e. WAMR) to run code for your tests, the test-various Dockerfile file might be a good place to add it.

I am in a similar situation: I just added native wasm exception handling support to the rust compiler; I'd like to add a run-make test for it, but the nodejs version is too old and does not support wasm exceptions yet. I will add a PR for this, though. Maybe it goes through :)

@alexcrichton

ghost commented Jun 30, 2023

Copy link
Copy Markdown
Member

Would it be enough of testing or do you know any open-source rust projects that are already getting compiled to wasm32-wasi?

Seems reasonable to me, yeah. I originally commented to this effect to recommend testing the non-threads wasm32-wasi target since the original version of this PR I think would have broken that. I'm more confident (but not 100%) that the current PR won't break existing code, but it might be good to spot-check a few things as well.

@g0djan

ghost commented Jul 4, 2023

Copy link
Copy Markdown
Contributor Author

the test-various Dockerfile file might be a good place to add it.

@mirkootter thanks

@g0djan

ghost commented Jul 4, 2023

Copy link
Copy Markdown
Contributor Author

Seems reasonable to me, yeah

might be good to spot-check a few things as well

@alexcrichton
😄 I think I didn't get whether I need to do more testing

@g0djan

ghost commented Jul 4, 2023

Copy link
Copy Markdown
Contributor Author

Should this be blocked on #110596? (See rust-lang/compiler-team#607 for context) It seems impractical to add the wasm32-wasi-threads target, only to rename it to (presumably?) wasm32-wasi-threads-preview1 soon after. Alternatively, it could pick a new name for the target immediately, and not wait for the renaming of the existing target, I think.

@abrown @bjorn3 @alexcrichton is there any concern if I just rename it to -preview1 and is it a blocker at all?

@alexcrichton

ghost commented Jul 5, 2023

Copy link
Copy Markdown
Member

As for testing, I think that's now up to whomever approves this PR. It sounds like not a lot of testing has been done, and AFAIK this won't be tested on CI.

As for naming, I don't have a preference myself. I believe the name wasm32-wasi-threads was selected independently of the renaming of wasm32-wasi-preview1. The name might instead wish to become wasm32-wasi-preview1-threads to keep -threads as a "suffix", but that's a bikeshed I'd rather not jump in to painting other than commenting this here.

@abrown

ghost commented Jul 5, 2023

Copy link
Copy Markdown
Contributor

wasm32-wasi-preview1-threads makes sense to me as well.

@g0djan

ghost commented Jul 11, 2023

Copy link
Copy Markdown
Contributor Author

As for testing, I think that's now up to whomever approves this PR

Ah okay, I thought it's gonna be you @alexcrichton, @bjorn3 or @abrown.
Should I invite somebody else to review it, can you recommend someone?

It sounds like not a lot of testing has been done

Sort of, I've done some testing but I haven't found a good open source Rust project compiled to wasm32-wasi to test it. I tried https://github.com/bytecodealliance/wizer/tree/main today and it was fine, but there wasn't much to test.

I have an idea to compare compilation and run results for wasm32-wasi without this change, for wasm32-wasi with this change and for wasm32-wasi-threads on ui testsuite

AFAIK this won't be tested on CI.

Is wasm32-wasi tested in stdarch ci? I found this Dockerfile https://github.com/rust-lang/stdarch/blame/d77878b7299dd7e286799a6e8447048b65d2a861/ci/docker/wasm32-wasi/Dockerfile

@codefromthecrypt

ghost commented Jul 11, 2023

Copy link
Copy Markdown

unsolicited and probably undesired 2p, but adding it to prevent this being merged without outside perspective

This should not be named preview1 because wasi-threads is not a part of preview1. It isn't even phase 3. If anything, this should be clear that this is not a finished feature and not about preview1. Calling this wasm32-wasi-preview1-threads is good marketing but doesn't reflect that that it is experimental and not about preview1

@john-sharratt

ghost commented Jul 12, 2023

Copy link
Copy Markdown

Hey guys, so I got this fully working here already using the WASI threads ABI (not the WASIX ABI)
https://github.com/wasix-org/rust

It's quite well tested now (many users for over a year) and its been running in production - namely the big drama being how to pass through the stacks so they can unwind. Not directly needed for threading but important for other patterns (such as asyncify when used with threading)

Note: that it uses the triple wasm32-wasmer-wasi rather than wasm32-unknown-wasi.
P.s. using threads in target-os doesn't seem to make much sense to me.

I think that we should work a bit more closely together on this, one thing that definitely makes sense is that we align the triplets as otherwise this will get messy quite quick.

Also while not directly for this PR... this might be a good time to start thinking about how we can make the other POSIX WASM capabilities eventually land upstream behind appropriate target-vendor toggles- willing to put the time in on this one.

Cheers

@yoshuawuyts

ghost commented Jul 12, 2023

Copy link
Copy Markdown
Member

This should not be named preview1 because wasi-threads is not a part of preview1. This should not be named preview1 because [it] is not a part of preview1. [It] should be clear that this is not a finished feature and not about preview1.

That's not right, wasi-threads is an extension to the Wasi Preview 1 specification which adds support for threads. That rationale was been clearly described in the accepted MCP. The only thing that's changed since is that MCP 607 has been accepted, but that doesn't change the relationship between the base target and the threads extension. Since the compiler team has already accepted that rationale, it doesn't seem particularly productive to relitigate that in this PR 1.

Footnotes

  1. I believe that the bar to deviating from previously-accepted rationale should at least clear the same bar as what it took to accept the rationale in the first place. That is to say: if you feel strongly enough that the relationship between the preview 1 base target and the threads extension is incorrect and should be changed, the way to go in my opinion would be to argue your case in an MCP and have it accepted by the compiler team.

@rustbot rustbot added the A-testsuite Area: The testsuite used to check the correctness of rustc label Jul 12, 2023
@wesleywiser

ghost commented Jul 31, 2023

Copy link
Copy Markdown
Member

@bors r+

@bors

ghost commented Jul 31, 2023

Copy link
Copy Markdown
Collaborator

📌 Commit 5697f16 has been approved by wesleywiser

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 31, 2023
@bors

ghost commented Aug 2, 2023

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 5697f16 with merge aa8462b...

@bors

ghost commented Aug 2, 2023

Copy link
Copy Markdown
Collaborator

☀️ Test successful - checks-actions
Approved by: wesleywiser
Pushing aa8462b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 2, 2023
@bors
bors merged commit aa8462b into rust-lang:master Aug 2, 2023
@rustbot rustbot added this to the 1.73.0 milestone Aug 2, 2023
@rust-timer

ghost commented Aug 2, 2023

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (aa8462b): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.5% [0.5%, 0.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.3% [-0.3%, -0.3%] 1
Improvements ✅
(secondary)
-0.4% [-0.5%, -0.3%] 3
All ❌✅ (primary) 0.1% [-0.3%, 0.5%] 2

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: missing data

@Mark-Simulacrum

ghost commented Aug 8, 2023

Copy link
Copy Markdown
Member

@rustbot label +perf-regression-triaged

This appears to be noise rather than genuine delta.

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

Labels

A-testsuite Area: The testsuite used to check the correctness of rustc merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.