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

Implement support for the standard mangling scheme in the component model #1828

Merged

Conversation

alexcrichton
Copy link
Member

This PR is a series of commits leading up to the conclusion of supporting the name mangling scheme described in WebAssembly/component-model#378. This is intended to be "the future" for how core wasm modules are shaped in terms of their imports and exports. The highlights of this support are:

  • Both the old and the new schemes are supported simultaneously. They've got no overlap and this shouldn't cause issues. This also means that this PR should break no one.
  • Utilities and some helpers were added for bindings generators. It's expected that generators will "simply switch" to the new scheme and will not retain support for the "legacy" naming scheme.
  • Lots of refactoring was needed internally in wit-component to further decouple baked-in assumptions about what names correspond to what.
  • The wasm-tools component embed --dummy flag now uses the new naming scheme by default
  • A new --dummy-names legacy flag can be used to enable the old names.
  • A new wasm-tools component new --reject-legacy-names flag was added to help test code generators and make sure they're entirely using the "new scheme".

The current idea I have for a rollout plan of this is to get this into wasm-tools and places like wasm-component-ld and others. Once that's propagated for a bit start changing guest bindings generators to using the new mangling scheme. Once that's done, and quite some time has passed, consider removing support for the old mangling scheme or enabling --reject-legacy-names by default.

Use the map's metadata to determine what the core wasm name is for each
export instead of recalculating it in the encoder which would duplicate
work done in validation.
This commit decouples the string encodings listed for imports/exports
from their core wasm names to instead being registered with WIT-level
constructs instead. Previously the parsing phase of a module would
register a string encoding for core wasm import/export names but this
subverted the logic of validation where detection of how exactly an
import lines up with WIT-level items is determined. The goal of this
commit is to decouple this relation.

Worlds are encoding into custom sections with a known string encoding
for all imports/exports of that world. This can possibly differ for
different parts of an application to theoretically enable one interface
to be imported with UTF-8 and another with UTF-16. This means that
encodings are tracked per-import/export rather than per-world.
Previously this process would assume that there is a single name for an
import's/export's encoding but with new detection and names coming down
the line this is no longer going to be the case. For example with the
new names in WebAssembly/component-model#378 there are new names to be
supported meaning that there's not one single name to register encodings
with.

To help bridge this gap the abstraction here is changed to where
metadata for a module records string encodings on a WIT level, for
example per WIT import/export, instead of per core wasm import/export.
Then during encoding of a component the WIT level constructs are matched
up instead of the core names to determine the string encoding in the
lift/lower operation.

The end goal is that the connection between core wasm names and WIT
names continues to be decoupled where validation is the only location
concerned about this.
This commit removes the need for the GC pass on the adapter module to
guess what core wasm export names are needed for WIT. Previously it was
assumed that certain exports would have exact core wasm names but that's
going to change soon so this refactoring is empowering these future changes.

The GC pass for adapters is restructured to run validation over the
non-GC'd adapter first. This validation pass will identify WIT export
functions and such and then this information is used to determine the
set of live exports. These live exports are then used to perform a GC
pass, and then afterwards the validation pass is run a second time to
recalculate information with possibly-removed imports.
This commit adds support for WebAssembly/component-model#378 to
`wit-component`. Notably a new set of alternative names are registered
and recognized during the module-to-component translation process.
Support for the previous set of names are all preserved and will
continue to be supported for some time. The new names are, for now,
recognized in parallel to the old names.

This involved some refactoring to the validation part of `wit-component`
and further encapsulation of various names to one small location instead
of a shared location for everywhere else to use as well.
This commit updates the `wasm-tools component embed` subcommand,
specifically the `--dummy` flag. This flag now uses the new "standard32"
names for the core module that is generated. Additionally a new
`--dummy-names $FOO` option has been added to enable generating the old
names as well as the new names.

Utilities have also been added to `Resolve` for bindings generators to
avoid hardcoding ABI names and instead use the add categories of
imports/exports to name items.
This commit adds a new `--reject-legacy-names` flag to the `wasm-tools
component new` subcommand which can be used to disable support for the
legacy naming scheme. This is intended to help with testing out the new
naming scheme for tools and to help evaluate in the future if it's
theoretically possible to remove support for the old naming scheme.
@alexcrichton
Copy link
Member Author

Also I'll preemptively apologize @dicej because this will further conflict with your async work rebase. I'm in no rush to merge this though so happy to rebase around you too.

Copy link
Collaborator

@dicej dicej left a comment

Choose a reason for hiding this comment

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

LGTM.

BTW, I don't think BuildTargets.md says anything yet about name mangling for async imports, exports, callback functions, stream/future built-ins, etc. When I rebase my async code onto this, should I just make something (plausible) up? Or else just use todo! in the async parts of impl NameMangling for Standard, meaning you have to use Legacy if you want to experiment with async?

@alexcrichton
Copy link
Member Author

I think that's reasonable yeah, maybe not todo!() but None-returning things yeah. I think it's probably best to sketch out with "legacy" and get "standard" working later on once it's more official in the component model itself.

@alexcrichton alexcrichton added this pull request to the merge queue Oct 2, 2024
Merged via the queue into bytecodealliance:main with commit 42e1c77 Oct 2, 2024
30 checks passed
@alexcrichton alexcrichton deleted the build-target-new-names branch October 2, 2024 15:02
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