Add weak references to the runtime.
- Dominant language
- C++
- Stars
- 3.9k
- Forks
- 1k
- Avg merge
- 4d 16h
- Merged PRs (30d)
- 47
Description
Not yet sure how I want to do this but it's definitely not by adding a GC :)
One idea is to have back references on all ref objects to an atomic slist of weak references. This has the cost of one (two today) pointer per ref object but would allow thread-safe weak references to any object with no centralized management. The annoying part is that slists are bad at random removal. Another idea would be to have a dedicated weak ref table that gets allocated on the first weak reference to a ref object and stashed on the object. This table would then be live so long as there are any weak refs outstanding to the object and could have whatever synchronization it wanted.
In the compiler we can have a dedicated `!vm.weak<>` type such that `!vm.weak>` denotes a weak reference that needs a special `vm.weak.resolve` (or w/e) op to get the inner strong ref. In this way all VM ops and the runtime operations are still defined on strong refs and the only support needed is for the access op and a wrapper ref object as the weak reference itself is a ref object. This allows things like weak references inside of lists and such.
So long as it's only a cost of one pointer per ref object I'm not sure it's worth much more hackery (lookaside tables/global state/etc) to avoid. The more annoying part is that it requires a new bit of offsetof trickery. Today all ref objects in the system need only have a single atomic count and that's used to decouple parts of the system (hal doesn't depend on vm but has compatible types, etc). I think it means it's finally time to move the core ref type into iree/base/. We can also add an ioctl-like interface to types so that we can destroy, print, debug, etc as well as add/remove weak refs. Part of this cleanup would be to move the type registry to be dynamic such that we can attach it to contexts vs global today.
Contributor guide
Assessment
This issue has not been assessed yet.