[LLVMdev] Boehm GC + static variables?

Jeffrey Yasskin jyasskin at google.com
Sun Jan 31 09:08:00 PST 2010

You should look at
and see if inheriting from that and overriding allocateGlobal() will
do what you want.

I'm a little surprised the boehm gc doesn't already see the globals,
since there's a reference to their memory from the JMM, but maybe it
doesn't scan mmap regions by default.

On Sun, Jan 31, 2010 at 6:12 AM, James Williams <junk at giantblob.com> wrote:
> I've implemented this by adding calls to GC_add_roots(<first global in
> module>,<last global in module>+1) to the llvm.global_ctors before any other
> static initialization code for the module.
> This should be safe assuming that:
> - global variables are laid out in memory in the order they appear in their
> module (and ideally contiguously without being interleaved with any other
> values)

This isn't necessarily the case. The default JMM allocates slabs with
mmap, which doesn't guarantee that they're in order.

> - llvm.global_ctors for a given module is run before any other code can
> reference static variables belonging to that module.

llvm.global_ctors runs when you call
runStaticConstructorsDestructors(false), which lli does before running
any other user code. There could be other global constructors that
access the globals, though, before your constructor runs.

> Can anyone confirm if I can rely on these assumptions?
> -- James
> On 31 January 2010 12:53, James Williams <junk at giantblob.com> wrote:
>> Hi,
>> I'm running LLVM bitcode generated by my compiler under lli. The bitcode
>> is linked against Boehm GC (lli -load=/usr/lib/libgc.so).
>> It looks like Boehm GC isn't scanning global variables and as a result
>> objects referenced only through globals are being prematurely collected. I
>> understand that Boehm GC needs to see the data segment containing my global
>> variables as a root. For native executables It's smart enough to pick this
>> up from the sections in the ELF executable but this doesn't apply when LLVM
>> bitcode is running under lli.
>> Is there some way I can hook into the code generator's placement of new
>> data segments as bitcode is compiled to native code so I can add roots for
>> the segments as required by Boehm GC?
>> Thanks in advance,
>> -- James Williams
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

More information about the llvm-dev mailing list