This branch implements incomplete support for closure conversion. Since full support would require garbage collection (or at least, some form of tracking to determine the lifetimes of closures), the implementation currently assumes that the environment is either:
A pointer,
A primitive type (i8, i16, i32, i64, ...)
With this assumption, the environment can be passed by value and stored inside the closure object without any allocation (this is similar to what std::function does under the hood). In the IR, a variant type is used to encode that fact and ensure that this optimization is sound (i.e the technique should work on 32bit and 64bit systems).
Backend support is currently only here for LLVM. C-based backend can easily support this feature by implementing emission for Closure primops, conversion for ClosureType and implementing calls.
This branch implements incomplete support for closure conversion. Since full support would require garbage collection (or at least, some form of tracking to determine the lifetimes of closures), the implementation currently assumes that the environment is either:
With this assumption, the environment can be passed by value and stored inside the closure object without any allocation (this is similar to what std::function does under the hood). In the IR, a variant type is used to encode that fact and ensure that this optimization is sound (i.e the technique should work on 32bit and 64bit systems).
Backend support is currently only here for LLVM. C-based backend can easily support this feature by implementing emission for
Closure
primops, conversion forClosureType
and implementing calls.