Hey, I am a bit confused about the CollectorRegistry. I am attempting to write a garbage collector for LLVM, and the tiny example in the docs at http://llvm.org/releases/2.3/docs/GarbageCollection.html gives this line: CollectorRegistry::Add<MyCollector> X("mygc", "My bespoke garbage collector."); My question is now: Am I supposed to instantiate my collector manually, and tell LLVM about it, or can the Registry instantiate it, and it that case, how to go about that? I haven't been able to deduce a way to get a pointer to my collector from the doxygen docs or the header files, so I hope that you can help me here. :-) Yours, Simon
On 2008-07-23, at 08:58, Simon Ask Ulsnes wrote:> I am attempting to write a garbage collector for LLVM, and the tiny > example in the docs at http://llvm.org/releases/2.3/docs/GarbageCollection.html > gives this line: > > CollectorRegistry::Add<MyCollector> > X("mygc", "My bespoke garbage collector."); > > My question is now: Am I supposed to instantiate my collector > manually, and tell LLVM about it, or can the Registry instantiate > it, and it that case, how to go about that? I haven't been able to > deduce a way to get a pointer to my collector from the doxygen docs > or the header files, so I hope that you can help me here. :-)Hi Simon, The compiler framework will instantiate and call into your Collector subclass automatically as required. The CollectorRegistry::Add<> instance advertises your class to the framework so it can do this. Your Collector class needs to have a default constructor in order to insantiate the Add<> template. The framework decides which Collector to use based upon the 'gc' attribute of a function: define void @f() gc "mygc" { ... } This string must correspond to the first argument to the CollectorRegistry::Add<> constructor. The second argument to the constructor is, I think, "purely informational" for collectors. For the related TargetMachineRegistry, the second argument appears in --help output. Hope that helps, Gordon
Thank you for that clarification.> The framework decides which Collector to use based upon the 'gc' > attribute of a function: > > define void @f() gc "mygc" { > ... > }OK, so for instance if I wanted to be able to use the GC from a C frontend (presumably by using llvm_gc_allocate?), do the C functions need this attribute as well? And if so, can this attribute be applied to functions in bitcode compiled by llvm-gcc, without modifications to the C compiler or the output bitcode? I.e., I use a custom driver program similar to lli that loads in bitcode and my GC, and executes it, and ideally programs would be able to use the GC without needing bitcode-level modifications (it's perfectly ok to have to replace calls to malloc, though).> > This string must correspond to the first argument to the > CollectorRegistry::Add<> constructor. > > The second argument to the constructor is, I think, "purely > informational" for collectors. For the related TargetMachineRegistry, > the second argument appears in --help output. > > Hope that helps, > GordonIt helps a lot. Thank you for your time. :-) Simon