Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does LLVM provide any facilities for implementing "green threads"/lightweight processes?

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?

like image 799
Ricky Stewart Avatar asked Jan 03 '15 04:01

Ricky Stewart


1 Answers

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.

like image 115
Chandler Carruth Avatar answered Jan 02 '23 09:01

Chandler Carruth