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

Wasmtime: Finish support for the typed function references proposal #7943

Merged

Commits on Feb 16, 2024

  1. Wasmtime: Finish support for the typed function references proposal

    While we supported the function references proposal inside Wasm, we didn't
    support it on the "outside" in the Wasmtime embedder APIs. So much of the work
    here is exposing typed function references, and their type system updates, in
    the embedder API. These changes include:
    
    * `ValType::FuncRef` and `ValType::ExternRef` are gone, replaced with the
      introduction of the `RefType` and `HeapType` types and a
      `ValType::Ref(RefType)` variant.
    
    * `ValType` and `FuncType` no longer implement `Eq` and `PartialEq`. Instead
      there are `ValType::matches` and `FuncType::matches` methods which check
      directional subtyping. I also added `ValType::eq` and `FuncType::eq` static
      methods for the rare case where someone needs to check precise equality, but
      that is almost never actually the case, 99.99% of the time you want to check
      subtyping.
    
    * There are also public `Val::matches_ty` predicates for checking if a value is
      an instance of a type, as well as internal helpers like
      `Val::ensure_matches_ty` that return a formatted error if the value does not
      match the given type. These helpers are used throughout Wasmtime internals
      now.
    
    * There is now a dedicated `wasmtime::Ref` type that represents reference
      values. Table operations have been updated to take and return `Ref`s rather
      than `Val`s.
    
    Furthermore, this commit also includes type registry changes to correctly manage
    lifetimes of types that reference other types. This wasn't previously an issue
    because the only thing that could reference types that reference other types was
    a Wasm module that added all the types that could reference each other at the
    same time and removed them all at the same time. But now that the previously
    discussed work to expose these things in the embedder API is done, type lifetime
    management in the registry becomes a little trickier because the embedder might
    grab a reference to a type that references another type, and then unload the
    Wasm module that originally defined that type, but then the user should still be
    able use that type and the other types it transtively references. Before, we
    were refcounting individual registry entries. Now, we still are refcounting
    individual entries, but now we are also accounting for type-to-type references
    and adding a new type to the registry will increment the refcounts of each of
    the types that it references, and removing a type from the registry will
    decrement the refcounts of each of the types it references, and then recursively
    (logically, not literally) remove any types whose refcount has now reached zero.
    
    Additionally, this PR adds support for subtyping to `Func::typed`- and
    `Func::wrap`-style APIs. For result types, you can always use a supertype of the
    WebAssembly function's actual declared return type in `Func::typed`. And for
    param types, you can always use a subtype of the Wasm function's actual declared
    param type. Doing these things essentially erases information but is always
    correct. But additionally, for functions which take a reference to a concrete
    type as a parameter, you can also use the concrete type's supertype. Consider a
    WebAssembly function that takes a reference to a function with a concrete type:
    `(ref null <func type index>)`. In this scenario, there is no static
    `wasmtime::Foo` Rust type that corresponds to that particular Wasm-defined
    concrete reference type because Wasm modules are loaded dynamically at
    runtime. You *could* do `f.typed::<Option<NoFunc>, ()>()`, and while that is
    correctly typed and valid, it is often overly restrictive. The only value you
    could call the resulting typed function with is the null function reference, but
    we'd like to call it with non-null function references that happen to be of the
    correct type. Therefore, `f.typed<Option<Func>, ()>()` is also allowed in this
    case, even though `Option<Func>` represents `(ref null func)` which is the
    supertype, not subtype, of `(ref null <func type index>)`. This does imply some
    minimal dynamic type checks in this case, but it is supported for better
    ergonomics, to enable passing non-null references into the function.
    
    We can investigate whether it is possible to use generic type parameters and
    combinators to define Rust types that precisely match concrete reference types
    in future, follow-up pull requests. But for now, we've made things usable, at
    least.
    
    Finally, this also takes the first baby step towards adding support for the Wasm
    GC proposal. Right now the only thing that is supported is `nofunc` references,
    and this was mainly to make testing function reference subtyping easier. But
    that does mean that supporting `nofunc` references entailed also adding a
    `wasmtime::NoFunc` type as well as the `Config::wasm_gc(enabled)` knob. So we
    officially have an in-progress implementation of Wasm GC in Wasmtime after this
    PR lands!
    
    Fixes bytecodealliance#6455
    fitzgen committed Feb 16, 2024
    Configuration menu
    Copy the full SHA
    ad731d3 View commit details
    Browse the repository at this point in the history
  2. Fix WAT in test to be valid

    fitzgen committed Feb 16, 2024
    Configuration menu
    Copy the full SHA
    6b6d124 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    289bdd4 View commit details
    Browse the repository at this point in the history
  4. Remove unnecessary engine parameters from a few methods

    Ever since `FuncType`'s internal `RegisteredType` holds onto its own `Engine`,
    we don't need these anymore.
    
    Still useful to keep the `Engine` parameter around for the `ensure_matches`
    methods because that can be used to check correct store/engine usage for
    embedders.
    fitzgen committed Feb 16, 2024
    Configuration menu
    Copy the full SHA
    680ac2e View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    424787b View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    d7533d6 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    c50d546 View commit details
    Browse the repository at this point in the history
  8. Add a missing is_v128 method

    fitzgen committed Feb 16, 2024
    Configuration menu
    Copy the full SHA
    1ef1db8 View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    d9245af View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    b03beb1 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    2380b23 View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    539d7b6 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    fdf1889 View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    ce29e58 View commit details
    Browse the repository at this point in the history
  15. Configuration menu
    Copy the full SHA
    3942b81 View commit details
    Browse the repository at this point in the history
  16. Configuration menu
    Copy the full SHA
    3aa04da View commit details
    Browse the repository at this point in the history
  17. Configuration menu
    Copy the full SHA
    027151b View commit details
    Browse the repository at this point in the history

Commits on Feb 20, 2024

  1. Fix doc links

    prtest:full
    fitzgen committed Feb 20, 2024
    Configuration menu
    Copy the full SHA
    62a8944 View commit details
    Browse the repository at this point in the history
  2. Fix size assertion on s390x

    fitzgen committed Feb 20, 2024
    Configuration menu
    Copy the full SHA
    8aced9e View commit details
    Browse the repository at this point in the history