Skip to content

Reimplement table.{grow,fill} - #13424

Merged
alexcrichton merged 4 commits into
bytecodealliance:mainfrom
alexcrichton:refactor-table-grow-and-fill
May 21, 2026
Merged

alexcrichton merged 4 commits into
bytecodealliance:mainfrom
alexcrichton:refactor-table-grow-and-fill

Conversation

@alexcrichton

Copy link
Copy Markdown
Member

This commit refactors the implementation of the table.{grow,fill} WebAssembly instructions to perform more work in compiled code rather than within the runtime itself. This shrinks the amount of libcalls needed, for example, simplifies some internals by having them build on each other, and additionally helps address #13387 by routing more bulk operations through a "narrow waist".

This refactors the internals of memory.fill and array.fill, previously refactored before, to share more implementation details. The end result is that the implementation of table.fill is a one-liner to a generic "fill this thing with this value" helper. Additionally the table.grow instruction is reimplemented in terms of this, meaning that the host is responsible for growth, but compiled code is responsible for initializing the table.

This commit refactors the implementation of the `table.{grow,fill}`
WebAssembly instructions to perform more work in compiled code rather
than within the runtime itself. This shrinks the amount of libcalls
needed, for example, simplifies some internals by having them build on
each other, and additionally helps address bytecodealliance#13387 by routing more bulk
operations through a "narrow waist".

This refactors the internals of `memory.fill` and `array.fill`,
previously refactored before, to share more implementation details. The
end result is that the implementation of `table.fill` is a one-liner to
a generic "fill this thing with this value" helper. Additionally the
`table.grow` instruction is reimplemented in terms of this, meaning that
the host is responsible for growth, but compiled code is responsible for
initializing the table.
@alexcrichton
alexcrichton requested review from a team as code owners May 20, 2026 21:25
@alexcrichton
alexcrichton requested review from cfallin and removed request for a team May 20, 2026 21:25

@cfallin cfallin 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.

Looks good!

// this creates a small window of time where the table actually has null
// entries. Given that this table isn't shared, however, this is fine
// because no other wasm instructions (or embedder code) can execute in
// this window.

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.

Add a comment here that we must not have any epoch-yield points within the fill?

(This also seems important in a CM async / cooperative threads context, because otherwise, if an epoch-yield someday becomes run-concurrent-compatible and another async activation runs, it could otherwise observe state halfway through an instruction, right? I don't know if that's a valid concern or if it is statically disallowed but in any case noting the relevant invariants here, and maybe at the bulk-fill loop implementation too, seems like a good idea)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's a good point/question, and definitely related to #11869. For now we're fine insofar as async libcalls like yield points will lock up the entire store making it statically impossible to read or write anything within the store. While we do want to fix that (at least somewhat) in the future it's a bit of an open question how to do so. One issue is that we definitely cannot allow mutations during yield points, but that'd still be a problem here if we allow reads. I'll leave comments on that issue since I think that's probably the best place (as opposed to the code) since it's a pervasive concern and not necessarily isolated to just this part of the code.

It's unfortunate as well because I think we explicitly want epoch yields here in table growth in case it's a massive table growth...

Comment thread crates/cranelift/src/func_environ.rs Resolved
@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label May 20, 2026
@alexcrichton
alexcrichton enabled auto-merge May 21, 2026 05:16
@alexcrichton
alexcrichton added this pull request to the merge queue May 21, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 21, 2026
@alexcrichton
alexcrichton added this pull request to the merge queue May 21, 2026
Merged via the queue into bytecodealliance:main with commit a9a2712 May 21, 2026
51 checks passed
@alexcrichton
alexcrichton deleted the refactor-table-grow-and-fill branch May 21, 2026 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants