## Implementing GC in Wasmtime
Ivan Mikushin
---
## Wasmtime
A fast and secure runtime for WebAssembly.
A [Bytecode Alliance](https://bytecodealliance.org/) project.
---
## What is Wasm GC?
Wasm GC (Garbage Collection) is a [proposal](https://github.com/WebAssembly/gc) to enable WebAssembly to natively support garbage collected languages. It introduces:
- **New types:**
New heap types and new ways to define types.
- **New instructions:**
~30 new instructions to manipulate reference types.
----
### Reference Types
WAT syntax: `(ref null? <heap-type>)`
----
### Heap Types
Heap types classify reference types.
- **Abstract:**
`any`, `eq`, `array`, `struct`, `func`, `extern`
- **Unboxed scalars:** `i31`
- **Bottom:** `none`, `noextern`, `nofunc`
- **User-defined (indexed):**
Arrays, Structs, Subtypes
----
### Already existing heap types
- `extern` and `func` -- via [reference-types](https://github.com/WebAssembly/reference-types),
- `(ref null? $t)` -- via [function-references](https://github.com/WebAssembly/function-references)
----
### Type Hierarchy
<div style="background-color: beige; padding: 10px">

</div>
(from Chromium's [Wasm GC proposal:
Spec for Prototype Implementation v.6](https://bit.ly/3cWcm6Q))
----
### Shorthands
- `funcref` == `(ref null func)`
- `externref` == `(ref null extern)`
- `anyref` == `(ref null any)`
- **`nullref`** == `(ref null `**`none`**`)`
- **`nullexternref`** == `(ref null `**`noextern`**`)`
- **`nullfuncref`** == `(ref null `**`nofunc`**`)`
- `eqref` == `(ref null eq)`
- `structref` == `(ref null struct)`
- `arrayref` == `(ref null array)`
- `i31ref` == `(ref null i31)`
----
### Type Definitions
- **Subtype** -- a single type, defined as a subtype of other types.
- **Recursive type group** -- a group of mutually recursive types that can refer to each other.
----
### New Instructions
The Wasm GC proposal introduces approximately 30 new instructions, including:
- **`i31.*`**: For creating and manipulating "unboxed scalars" (31-bit integer values).
- **`array.*`** and **`struct.*`**: For creating and manipulating arrays and structs.
- **`ref.*`**: For casting and testing values of reference types.
- **`br_on_*`**: For conditional branching depending on values of reference types.
- **`extern.*`**: For external reference conversion.
---
## Implementation Plan
1. Wasm GC Support in wasm-tools:
- wasmparser / wasmprinter
- wasm-encoder
- wasm-smith
2. Begin implementation in Wasmtime
- Enable GC support flags
- Incrementally enable GC spec tests
3. Enable the proposal in the fuzz targets.
4. Expose the proposal’s new functionality in Wasmtime API.
[Detailed implementation plan](https://hackmd.io/@imikushin/HyoMh-Lzh)
---
## Current Status
(what's already done)
- Wasm GC Support in wasm-tools:
- **wasmparser, wasmprinter:** Implemented new type support up to user defined structs, and `i31.*` instructions.
- **wasm-encoder:** Implemented only necessary things for now: so that it compiles
---
## Wrap up
- **Wasm GC spec defines:**
- reference types hierarchy
- instructions to work with new types
- **Current focus:**
- enable support in wasm-tools
- **Implementation in Wasmtime:**
- can start after wasmparser support is complete
---
## Thank you! :cat:
-- @imikushin
{"metaMigratedAt":"2023-06-18T04:45:06.774Z","metaMigratedFrom":"YAML","title":"Implementing GC in Wasmtime","breaks":true,"slideOptions":"{\"theme\":\"moon\",\"transition\":\"slide\"}","contributors":"[{\"id\":\"6de392c6-5625-4e5e-8b77-1af2d0b3c5ca\",\"add\":6642,\"del\":5358}]"}