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

wasm-encoder: add method to allow fetching raw Function bytes. #1630

Merged
merged 1 commit into from
Jun 23, 2024

Conversation

cfallin
Copy link
Member

@cfallin cfallin commented Jun 22, 2024

When generating function bodies with wasm-encoder, it is sometimes useful to take the function bytecode, cache it, then reuse it later. (For my specific use-case, in weval, I would like to cache weval-specialized function bodies and reuse them when creating new modules.)

Unfortunately the existing API of wasm-encoder makes this almost but not quite easy: Function implements Encode::encode, but this will produce a function body with the length prefixed. However there's no way to use this unmodified with the next level up the entity hierarchy: CodeSection::raw wants the function bytes without the length prefixed.

This is a fairly annoying if small API gap, and otherwise requires manually stripping the length prefix, a leb128-encoded integer. It's also a small footgun: I naively did not realize this mismatch and tried to do the above, only to get perplexing type errors with locals.

This PR adds one method on wasm_encoder::Function to return the inner bytes directly, and the doc-comment contains an example showing the intended use-case.

When generating function bodies with `wasm-encoder`, it is sometimes
useful to take the function bytecode, cache it, then reuse it later.
(For my specific use-case, in weval, I would like to cache
weval-specialized function bodies and reuse them when creating new
modules.)

Unfortunately the existing API of `wasm-encoder` makes this *almost* but
*not quite* easy: `Function` implements `Encode::encode`, but this will
produce a function body *with* the length prefixed. However there's no
way to use this unmodified with the next level up the entity hierarchy:
`CodeSection::raw` wants the function bytes *without* the length
prefixed.

This is a fairly annoying if small API gap, and otherwise requires
manually stripping the length prefix, a leb128-encoded integer. It's
also a small footgun: I naively did not realize this mismatch and tried
to do the above, only to get perplexing type errors with locals.

This PR adds one method on `wasm_encoder::Function` to return the inner
bytes directly, and the doc-comment contains an example showing the
intended use-case.
@alexcrichton alexcrichton added this pull request to the merge queue Jun 23, 2024
@alexcrichton
Copy link
Member

Happy to add more helpers and/or edit existing helpers as well if needed!

@cfallin
Copy link
Member Author

cfallin commented Jun 23, 2024

Thanks! I think I have what I need for now (I have caching working in weval).

It looks like the release schedule is somewhat ad-hoc here but that we have the same automation as in Wasmtime -- if it's not too much trouble, would it be possible to do a point release with this? No particular rush if another release is coming soon anyway.

Merged via the queue into bytecodealliance:main with commit 62cf274 Jun 23, 2024
27 checks passed
@cfallin cfallin deleted the func-into-raw-body branch June 23, 2024 02:52
@alexcrichton
Copy link
Member

It's true yeah there's no release cadence here, but one difference from Wasmtime is that there's not great support for backports and/or release branches. The current patch release process only works if there aren't otherwise breaking changes on main relative to the last release, which there currently are. Would it be best to get this out pretty soon? If so I might manually make a release branch for this and backport it

@cfallin
Copy link
Member Author

cfallin commented Jun 24, 2024

Ah, no worries then, happy to wait for the next major release. To be concrete I don't have any reason to ask for it to be expedited this week in particular, then I'm on PTO for two weeks, so any time in the next month is probably fine :-)

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.

None yet

2 participants