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

threads: add component model canonical functions #1783

Merged
merged 11 commits into from
Sep 18, 2024

Conversation

abrown
Copy link
Collaborator

@abrown abrown commented Sep 12, 2024

The shared-everything-threads proposal adds two new component model canonical functions, thread.spawn and thread.hw_concurrency. This change adds initial support for these new functions, which should match what is specified in the canonical ABI. All of this support should be gated behind checks that ensure the shared-everything-threads proposal has been enabled.

@abrown abrown requested a review from alexcrichton September 12, 2024 00:12
crates/wasm-encoder/src/reencode/component.rs Outdated Show resolved Hide resolved
crates/wasmparser/src/validator.rs Outdated Show resolved Hide resolved
crates/wasmparser/src/validator/component.rs Show resolved Hide resolved
Comment on lines 1121 to 1124
// Like other component model functions, this is initially added as
// unshared, meaning spawned threads cannot spawn more threads. In the
// future this and other component model intrinsics (e.g., `rep.*`)
// should be marked `shared` to allow access from a shared context.
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit surprised by this in that I would naively expect that it's ok to flag something as shared. It should be safe I think to import a non-shared function when the actual provided value is a shared function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Safe, yes... I think what I was thinking here is that eventually all of these component model canonical functions need to be accessible from both shared and unshared contexts. So eventually they should all be marked shared if this proposal is enabled, I guess. But that seems a bit disruptive at this point... I'm just keeping the status quo at this point (inconsistently, though, because I marked thread.hw_concurrency as shared...)

Copy link
Member

Choose a reason for hiding this comment

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

Hm actually let me phrase what I'm saying a different way. Right now there's no subtyping relationship (at least not in wasm-tools) between shared types and unshared types. That means that you can't import a non-shared function and then satisfy that import with a shared function.

The comment here seems to indicate that you want to work with these intrinsics as-is but you're unable to because the current typing rules require that the core module imports a shared version of the component intrinsic. This version of the intrinsic cannot be imported since core tooling can't emit shared imports yet.

I'd personally like to propose the question to the shared-everything-threads proposal: should shared things be a subtype of non-shared things? Doing so would solve your concern here because the intrinsic would be marked shared and if it were imported as non-shared then that would also work ok because of subtyping. To me at least this feels similar to nullable subtyping.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What I meant by the comment is that I'm trying to avoid binary changes that users might not expect just yet. I don't think we need sub-typing at the moment, though it might be a worthwhile discussion to have (or revisit: I think we've had it before and concluded that there will be no sub-typing between sharedness; the closest I can find is "shared and unshared types are never subtypes of one another" in this section).

I don't think we'll be able to work with these intrinsics as-is: we will have to mark them shared to be available to multiple threads, it's just that I didn't feel comfortable doing that just yet. But it does seem like what I sketched out above is what needs to happen eventually: if shared-everything-threads is enabled, all CM intrinsics get marked shared. Otherwise they are inaccessible from threads!

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok. I think though that this should be marked as shared since that's sort of its purpose for existence? I don't think it's going to harm anything because it's all gated for now. Are you worried about it not being usable by core modules today though?

The shared-everything-threads [proposal] adds two new component model
canonical functions, `thread.spawn` and `thread.hw_concurrency`. This
change adds initial support for these new functions, which should match
what is specified in the [canonical ABI]. All of this support should be
gated behind checks that ensure the shared-everything-threads proposal
has been enabled.

[proposal]: https://github.com/WebAssembly/shared-everything-threads
[canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-threadspawn
@alexcrichton alexcrichton self-assigned this Sep 17, 2024
@abrown abrown marked this pull request as ready for review September 18, 2024 18:20
@alexcrichton alexcrichton added this pull request to the merge queue Sep 18, 2024
Merged via the queue into bytecodealliance:main with commit 5a83828 Sep 18, 2024
30 checks passed
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