AnyDSL / thorin

The Higher-Order Intermediate Representation
https://anydsl.github.io
GNU Lesser General Public License v3.0
151 stars 15 forks source link

Closure conversion #88

Closed madmann91 closed 6 years ago

madmann91 commented 6 years ago

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 for ClosureType and implementing calls.