alexcrichton opened PR #6691 from alexcrichton:resources
to bytecodealliance:main
:
This PR is a work-in-progress of implementing the resource type of the component model in Wasmtime. My goal at this time is to implement everything necessary to get
*.wast
and embedder API tests working. This has notable omissions which are deferred to a future PR:
- No support in
component::bindgen!
- No support for component-to-component resources (fact compiler)
Remaining items:
- [ ] destructors
- [ ]
(borrow $T)
- [ ] probably more tests
- [ ] rewrite this OP with more words
- [ ] fill out all
/// TODO
documentation- [ ] Land and publish
wasm-tools
changes
sunfishcode submitted PR review.
sunfishcode submitted PR review.
sunfishcode created PR review comment:
This is probably not a huge optimization, but these two instructions to compute the new
rep
value can be emitted intocall_func_ref_block
instead so that they're not executed if there is no destructor.
sunfishcode created PR review comment:
I'm curious why this code is checking for the function pointer being null, instead of testing the least significant bit of the value returned by
resource_drop
. Do we ever have aSome
value with a null destructor pointer?
alexcrichton updated PR #6691.
alexcrichton created PR review comment:
Ah they end up being two different conditions. First is that
resource.drop
can be used to drop either own or borrow values, so the first check is basically "which did I drop?". The second check is "ok is there actually a destructor?". So a return value of 0 from the intrinsic means "you dropped a borrow do nothing else". Otherwise the return value is(rep << 1) | 1
where the destructor, if specified, is invoked withrep
.As I type this out though I realize that whether or not the destructor is specified is actually static so there's actually no need for a runtime check of the funcref, all that needs to be checked is the return value from the drop intrinsic to see if it's an own/borrow.
alexcrichton updated PR #6691.
alexcrichton edited PR #6691:
This PR is a work-in-progress of implementing the resource type of the component model in Wasmtime. My goal at this time is to implement everything necessary to get
*.wast
and embedder API tests working. This has notable omissions which are deferred to a future PR:
- No support in
component::bindgen!
- No support for component-to-component resources (fact compiler)
Remaining items:
- [x] destructors
- [ ]
(borrow $T)
- [ ] probably more tests
- [ ] rewrite this OP with more words
- [ ] fill out all
/// TODO
documentation- [ ] Land and publish
wasm-tools
changes
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
sunfishcode submitted PR review.
sunfishcode submitted PR review.
sunfishcode created PR review comment:
Can this use
NonNull::cast
?
sunfishcode created PR review comment:
This comment should be updated to reflect its meaning.
sunfishcode created PR review comment:
This comment about the null check is now out of date; it'd be good to mention what you said earlier about the type being known statically.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton edited PR #6691:
This PR is a work-in-progress of implementing the resource type of the component model in Wasmtime. My goal at this time is to implement everything necessary to get
*.wast
and embedder API tests working. This has notable omissions which are deferred to a future PR:
- No support in
component::bindgen!
- No support for component-to-component resources (fact compiler)
Remaining items:
- [x] destructors
- [x]
(borrow $T)
- [ ] probably more tests
- [ ] rewrite this OP with more words
- [ ] fill out all
/// TODO
documentation- [ ] Land and publish
wasm-tools
changes
alexcrichton edited PR #6691:
This PR is a work-in-progress of implementing the resource type of the component model in Wasmtime. My goal at this time is to implement everything necessary to get
*.wast
and embedder API tests working. This has notable omissions which are deferred to a future PR:
- No support in
component::bindgen!
- No support for component-to-component resources (fact compiler)
Remaining items:
- [x] destructors
- [x]
(borrow $T)
- [x] probably more tests
- [ ] rewrite this OP with more words
- [ ] fill out all
/// TODO
documentation- [ ] Land and publish
wasm-tools
changes
alexcrichton edited PR #6691:
This PR is a work-in-progress of implementing the resource type of the component model in Wasmtime. My goal at this time is to implement everything necessary to get
*.wast
and embedder API tests working. This has notable omissions which are deferred to a future PR:
- No support in
component::bindgen!
- No support for component-to-component resources (fact compiler)
Remaining items:
- [x] destructors
- [x]
(borrow $T)
- [x] probably more tests
- [ ] rewrite this OP with more words
- [ ] fill out all
/// TODO
documentation- [x] Land and publish
wasm-tools
changes
alexcrichton edited PR #6691:
This PR is a work-in-progress of implementing the resource type of the component model in Wasmtime. My goal at this time is to implement everything necessary to get
*.wast
and embedder API tests working. This has notable omissions which are deferred to a future PR:
- No support in
component::bindgen!
- No support for component-to-component resources (fact compiler)
Remaining items:
- [x] destructors
- [x]
(borrow $T)
- [x] probably more tests
- [ ] rewrite this OP with more words
- [ ] fill out all
/// TODO
documentation- [x] Land and publish
wasm-tools
changesCloses #6583
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton updated PR #6691.
alexcrichton edited PR #6691:
This PR is a work-in-progress of implementing the resource type of the component model in Wasmtime. My goal at this time is to implement everything necessary to get
*.wast
and embedder API tests working. This has notable omissions which are deferred to a future PR:
- No support in
component::bindgen!
- No support for component-to-component resources (fact compiler)
Remaining items:
- [x] destructors
- [x]
(borrow $T)
- [x] probably more tests
- [ ] rewrite this OP with more words
- [x] fill out all
/// TODO
documentation- [x] Land and publish
wasm-tools
changesCloses #6583
alexcrichton edited PR #6691:
This PR is an implementation of the resource datatype in the component model as specified upstream. The goal of this PR is to get lots of the low-level infrastructure for resources sorted out, but not 100% of the story as there's still remaining work. Features implemented in this PR are:
- Validation, translation, and compilation of resources now works
- Implementation of all resource-related intrinsics such as
resource.{rep,new,drop}
- Updates to the compiled image of components as necessary for intrinsics
- Implementation of runtime state of resources, aka tables, in component instances
- Updates to type-checking and type-matching to take resources into account
- Implementation of an embedding API for host-defined resources
- Implementation of an embedding API for guest-defined resources
- An initial suite of test which showcase basic behavior of resources and the capabilities of the embedding API
This is intended to be a solid implementation for all the "internals" of resources throughout Wasmtime. It's expected that all further work will be much easier, less invasive, and not so large a scale. Or at least that's the hope. Note though that this is not a 100% complete story for resources in Wasmtime. For example it's still not possible to take a WIT off the shelf with resources and use that with Wasmtime. Key missing features in Wasmtime related to resources are:
- https://github.com/bytecodealliance/wasmtime/issues/6722
- https://github.com/bytecodealliance/wasmtime/issues/6696
- https://github.com/bytecodealliance/wasmtime/issues/6724
This work is a prerequisite for the above, though, and the hope additionally is that the above can all start to progress in parallel with this work as a basis. Basically getting enough in that everything is no longer bottlenecked on me but it's possible to start building out from here.
Closes #6583
alexcrichton requested fitzgen for a review on PR #6691.
alexcrichton has marked PR #6691 as ready for review.
alexcrichton requested wasmtime-core-reviewers for a review on PR #6691.
fitzgen submitted PR review:
r=me with nitpicks below
fitzgen created PR review comment:
This isn't just for transcodes, right? Outdated comment?
fitzgen created PR review comment:
It would be nice to have an index newtype for libcalls.
fitzgen submitted PR review:
r=me with nitpicks below
fitzgen created PR review comment:
Can this be read only?
fitzgen created PR review comment:
Copy paste comment needs to be inverted.
fitzgen created PR review comment:
This as well?
fitzgen created PR review comment:
Can we emit an assertion in the generated code for this when
cfg(debug_assertions)
?
fitzgen created PR review comment:
Can we make this branch
unreachable!()
?
fitzgen created PR review comment:
/// definition which should refer to a resource itself.
fitzgen created PR review comment:
/// Unlike `resource_id_to_table_index` this is required to be eagerly
fitzgen created PR review comment:
Sometimes "wasmparser" has backticks and sometimes it doesn't in this big doc comment. We should be consistent. I personally prefer backticks but this is obviously not a big deal.
(Thanks, btw, for writing this nice big doc comment!)
fitzgen created PR review comment:
//! and then from then on purely talk about table indices. Each component
fitzgen created PR review comment:
Copy-paste doc comment is misleading
fitzgen created PR review comment:
Non-resource types are structural, not nominal, right?
//! such as `list`, `record`, and `string` are considered "structural" where two //! types are considered equal if their components are equal. For example `(list //! $a)` and `(list $b)` are the same if `$a` and `$b` are the same. Resources,
fitzgen created PR review comment:
//! Given the subtlety here the goal is to lean on `wasmparser` as much as
fitzgen created PR review comment:
// signature for `resource.drop` is mentioned somewhere, and if the // wasm-to-native trampoline for `resource.drop` hasn't been created yet // then insert that here. This is possibly required by destruction of
fitzgen created PR review comment:
Not sure I am 100% following so take this with a grain of salt, but can this just check whether
ret.resource_drop_wasm_to_native_trampoline.is_some()
instead of maintaining a hash set if all compile keys?
fitzgen created PR review comment:
If a component takes two resource type imports, and is instantiated from the host with the same resource for both imports, and we get the
ResourceType
for each of those imports from that component, will both of those result in the sameResourceType
? That is, even though the compiled component cannot know they are the same, at the Wasmtime API level when we do reflection on the instantiated component, do we see through everything and determine they are the same? I think we should, and I think we should test this if we don't already.
fitzgen created PR review comment:
Kind of annoying we have to thread this through everything specially, when we worked so hard to avoid that elsewhere. Can we perhaps give it it a known compile key or something, so we can just look it up in the various output maps or whatever? Rather than add a special field to all of our compiler's structs. Not sure this is feasible but I'd like to just push in that direction if possible.
fitzgen created PR review comment:
Nice.
fitzgen created PR review comment:
/// component model provides is that the `u32` is not forgeable by guests and
fitzgen created PR review comment:
The parenthetical could be clarified a bit:
/// doesn't go further and test for equality in the guest itself (for example /// two different heap allocations of `Box<u32>` can be equal in normal Rust if they contain /// the same value, but will never be considered equal when compared as `Val::Resource`s).
alexcrichton submitted PR review.
alexcrichton created PR review comment:
This branch is hit at runtime so it can't be
unreachable!
, but I'll update the comment to clarify that.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Oops heh yes I mean the exact opposite of what I say
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Correct! Added a test as well :+1:
alexcrichton edited PR review comment.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
It's a bit subtle and worth a comment that's not currently present, but this is doing 2 things where first it's determining if a trampoline is required and if so creating a
CompileKey
for it. Next it's detecting if that key was already compiled for some previous trampoline, and if not it adds it.In that sense this is the "root" where
resource_drop_wasm_to_native_trampoline
is first set toSome
, so it can't piggy-back on anything else.That being said coupled with your below concern I'll see if I can clean this up a bit.
alexcrichton updated PR #6691.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
True! There's a newtype for core wasm libcalls but I was a bit hesitant to duplicate all the macro machinery for that as well since the libcalls already duplicate other macro machinery.
alexcrichton has enabled auto merge for PR #6691.
alexcrichton updated PR #6691.
alexcrichton has enabled auto merge for PR #6691.
alexcrichton updated PR #6691.
alexcrichton has enabled auto merge for PR #6691.
alexcrichton updated PR #6691.
alexcrichton has enabled auto merge for PR #6691.
alexcrichton merged PR #6691.
Last updated: Dec 23 2024 at 13:07 UTC