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 thread.spawn_indirect #2042

Merged
merged 5 commits into from
Mar 17, 2025

Conversation

abrown
Copy link
Collaborator

@abrown abrown commented Feb 7, 2025

This propagates the upstream spec changes to the component model in #447 here. See commit messages for more details.

@abrown abrown force-pushed the set-spawn-indirect branch from cfc0969 to 7d7f530 Compare February 7, 2025 19:16
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! Mind adding a test as well for using a table of the wrong type? (e.g. a non-shared funcref or a shared-externref)

@abrown abrown force-pushed the set-spawn-indirect branch from 7d7f530 to c50c815 Compare March 17, 2025 19:39
abrown added 4 commits March 17, 2025 14:05
As discussed in [bytecodealliance#89], this adds support for a new intrinsic,
`thread.spawn_indirect`. This new operation would allow spawning a
shared function stored in a table via a table index.

This leaves some future work undone:
- `thread.spawn` could/should be renamed to `thread.spawn_ref`
- `thread.spawn_indirect` could/should take the encoding byte from
  `thread.hw_concurrency`--swap `0x07` for `0x06`
- importantly, `thread.spawn_indirect` should gain a field indicating
  which type to expect in the indirect table; due to current limitations
  in `wasm-tools`, the locations to check once this is possible are
  marked with `TODO: spawn indirect types`.

[bytecodealliance#89]: WebAssembly/shared-everything-threads#89
[bytecodealliance#447] tries to make the built-in naming a bit more consistent; this
change propagates that here.

[bytecodealliance#447]: WebAssembly/component-model#447
This encoding change is necessary due to recent additions to the
component model; see [bytecodealliance#447].

[bytecodealliance#447]: WebAssembly/component-model#447
Initially I implemented `thread.spawn_indirect` without the ability to
check the type of the function to spawn out of an abundance of caution
(see the implementation issues described in [bytecodealliance#89]). In the process of
writing out the specification, we convinced ourselves that these
problems should not apply to `thread.spawn_indirect`.

This change adds the function type index necessary for doing some extra
validation of `thread.spawn_indirect` and adds some tests related to
this. One unimplemented TODO is what to do about shared tables:
technically, the table used by a `thread.spawn_indirect` should be
shared but we have so far prevented this in the component model; this
can be resolved later, though.

[bytecodealliance#89]: WebAssembly/shared-everything-threads#89
@abrown abrown force-pushed the set-spawn-indirect branch from c50c815 to b5b9267 Compare March 17, 2025 21:09
@abrown
Copy link
Collaborator Author

abrown commented Mar 17, 2025

@alexcrichton, I rebased this on top of main so a lot has changed. This also incorporates the conclusions we reached in WebAssembly/component-model#447 so there's new encoding-related implementation related to that.

@abrown abrown marked this pull request as ready for review March 17, 2025 21:12
@abrown abrown requested a review from alexcrichton March 17, 2025 21:12
Comment on lines +1981 to +1982
// TODO: check that the table is shared once components allow shared
// tables.
Copy link
Member

Choose a reason for hiding this comment

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

Is this TODO still applicable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I added it because we still prevent shared tables from being used in components. At some point we will have to relax that but it seems like a separate PR. Speaking of: when can or should we relax that? We have this not-accepted.wast file that gates some of this but I can't fully remember why we added it:

;; RUN: wast --assert default --snapshot tests/snapshots % -f shared-everything-threads
(assert_invalid
(component
(core module $A
(memory (export "m") 1 2 shared))
(core instance $A (instantiate $A))
(alias core export $A "m" (core memory $m))
)
"shared linear memories are not compatible with components yet")
(assert_invalid
(component
(core module $A
(table (export "m") shared 1 2 (ref null (shared func)))
)
(core instance $A (instantiate $A))
(alias core export $A "m" (core table $m))
)
"shared tables are not compatible with components yet")

Copy link
Member

Choose a reason for hiding this comment

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

Oh that was due to #1970. What I added there was a very coarse version of "you can't lower into a 64-bit memory".

What really needs to happen is that every location that uses a memory (or table) needs to perform a subtype check on what the actual table/memory received is. For example all canonical options referring to a memory have to refer to a non-shared 32-bit linear memory.

Basically #1970 was a coarse over-approximation that didn't break anyone "on stable". To remove the logic from #1970 we'll need to audit usage of memory_at and table_at to ensure everything is doing a subtype check as appropriate.

@alexcrichton
Copy link
Member

(sorry about all the interim changes but thanks for rebasing over them!)

@abrown abrown enabled auto-merge March 17, 2025 22:34
@abrown abrown added this pull request to the merge queue Mar 17, 2025
Merged via the queue into bytecodealliance:main with commit ce65be0 Mar 17, 2025
32 checks passed
@abrown abrown deleted the set-spawn-indirect branch March 17, 2025 22:48
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