I am looking into designing a concurrent language with support for lightweight processes ("green threads") in the vein of Erlang using LLVM as a native code generator. Lightweight processes are allocated to native OS threads in an M:N fashion, and work-stealing between threads should be possible (i.e. processes should be represented by a data structure that can be passed between threads if necessary). A very large number of processes might exist at once, so processes shouldn't take up much memory and context switching between them should be as quick as possible. Furthermore, it should be rather simple to "pause" a lightweight process during context switches or if garbage collection occurs. I understand Erlang has an LLVM backend, but I can find very little literature on its implementation; can someone describe to me how this might be possible?
LLVM isn't directly relevant to implementing this type of system. There are plenty of frontends for languages with such constructs that lower to LLVM's IR.
LLVM is just compiler technology to generate native code for a single thread of execution. Implementing context switching, setting up the stack appropriately (cactus stacks or other techniques), and other concerns are primarily the responsibility of the runtime and environment.
One exception is supporting the synthesis of runtime calls to grow the stack when necessary, and that potentially splitting the stack into non-contiguous regions. As indicated in comments, LLVM has some support for this, although it is less well tested. However, your frontend can also control the usage of the stack in order to avoid needing any support in LLVM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With