[LLVMdev] Garbage collection implementation

Jon Harrop jon at ffconsultancy.com
Tue Jun 23 14:35:38 PDT 2009


On Tuesday 23 June 2009 14:29:22 Angelos Manousaridis wrote:
> I am using LLVM as the last stage of a compiler in order to easily produce
> a binary in native code. My compiler is implemented in Ocaml and has
> various layers of languages. In the last layer prior to LLVM, I have a
> value which has been converted to CPS, closure and hoisting (of functions).
>
> I am now trying to write a garbage collector for this language. The shadow
> stack is not suitable for me, because essentially there is no stack in my
> case!

I don't follow. Shadow stack is overkill because you only ever have one stack 
frame but it should work perfectly.

> All the function calls are tail calls and without tail recursive 
> optimization and stack re-use, the binary is useless.
>
> My goal for now is to write a simple stop-the-world, semispace collector.
> To cut a long story sort, I need to implement an allocation function like
> this:
>
> if ( there is enough space ) {
>   allocate space;
> } else {
>   dump all live physical registers on the stack;
>   garbage collect;
>   restore from stack, using the new locations;
> }
>
> The tricky part is the "dump all live registers".

The solution I used in HLVM is essentially to always keep all 
registers "dumped". Specifically, they are dumped as soon as they go live and 
reclaimed at the end of each function. That is grossly inefficient in theory 
but it is easy and, in fact, performance can still be very good indeed (e.g. 
several times faster than OCaml).

> I spent quite some time 
> reading the documentation and have come up to two alternatives. Either
> implement a new pass which reads the live variable analysis and spills all
> registers to the stack at this point, or implement a new intrinsic which
> spills all registers to the stack (after gc, I have to reload registers
> manually).
>
> Neither of these ideas appears easy, and I am wondering if there is a
> simpler way around this.

The simplest way is surely to reuse HLVM because it provides everything you 
need and is even written in the right language! ;-)

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e



More information about the llvm-dev mailing list