bytecodealliance / bytecodealliance/wasmtime
Support lifetimes in context in ISLE
- Dominant language
- Rust
- Stars
- 18.6k
- Forks
- 1.8k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 121
Description
#### Feature
Add lifetime parameters in ISLE-generated `Context`, so that it can return values with lifetime parameters (not `'static` lifetime).
```rust
// ISLE generated Context
trait Context<'a> {
fn get_foo(&mut self) -> FooRef<'a>;
}
```
In this way, the context can be implemented like:
```rust
type FooRef<'a> = &'a Foo;
#[derive(Debug)]
struct Foo(i32);
struct FooCtx<'a> {
foo: &'a Foo
}
impl<'a> Context<'a> for FooCtx<'a> {
fn get_foo(&mut self) -> FooRef<'a> {
self.foo
}
}
```
(previously discussed in Zulip: https://bytecodealliance.zulipchat.com/#narrow/stream/217117-cranelift/topic/ISLE.20support.20for.20references.20with.20context.20lifetime)
#### Benefit
I'm currently using `cranelift-isle` for a project of my own. However, unlike Cranelift, the types that I use with ISLE cannot be cheaply cloned like those used in Cranelift. If a `Context` can return values with non-`'static` lifetime, I can efficiently reference into readonly input data structures.
#### Implementation
I am implementing it in my fork: https://github.com/lynzrand/wasmtime/tree/isle-patch
Additions to the language:
- Lifetime identifiers: `'a` is a valid lifetime identifier.
- A pragma statement: `(pragma context_lifetime 'a 'b ...)`.
- A new kind of primitive type definition with lifetime: `(type Node (primitive (NodeRef 'a)))`.
Modifications:
By adding a pragma statement:
```lisp
(pragma context_lifetime 'a 'b)
```
... the codegen will now generate the context trait with the specified lifetimes:
```rust
trait Context<'a, 'b> {
// ...
}
```
These lifetimes can then be used in primitive type definitions:
```lisp
(type Node (primitive (NodeRef 'a)))
(decl lower (Node) ...)
(rule (lower (Node ...)) ...)
```
... and reflected in generated code:
```rust
pub fn constructor_lower<'a, 'b, C: Context<'a, 'b>>(
ctx: &mut C,
arg0: NodeRef<'a>,
) -> ...
```
(I have yet to look into enums.)
My implementation is currently pretty naive and lacks polishing. I plan to continue testing and polishing it as I continue on my project.
#### Alternatives
I can choose to use `impl Copy` indices or other kinds of keys to index structures instead of fancy references, like Cranelift does.
Contributor guide
Assessment
This issue has not been assessed yet.