Hi! I’m new to LLVM and C++ and I’m trying to implement a garbage collector that uses the statepoint API. I have been reading all the documentation and asking ChatGPT millions of questions. I think I have an idea of the path ahead of me. However, I don’t feel confident about this being the right path.
I have not been able to find a runtime implementation that uses the that uses the statepoint-example strategy online. The LLVM docs mention examples in the repo under /llvm/runtime/, but they seem to be gone.
The following is my understanding of what I need to do:
I want to use the statepoint API over the gc_root because the documents recommend using it for new projects. I understand that I need to do the following:
-
Declare a custom GC strategy with
UseStatepoints = true;andUseRS4GC = trueor use thestatepoint-examplebuilt-in strategy. I plan to usestatepoint-exampleunless I hit a technical issue that prevents me from using it. -
Annotate all my functions to use this strategy with
setGC:
#include <llvm/IR/IRBuilder.h>
// ...
Function *fn = Function::Create(prototype, Function::ExternalLinkage, function_name, module.get());
fn.setGC("statepoint-example");
-
Use the
RewriteStatepointsForGCtransformation pass will automatically introduce annotations like@llvm.experimental.gc.statepointand@llvm.experimental.gc.relocate. -
Use the
PlaceSafepointstransformation pas to automatically add calls tosafepoint_pollat safe points. -
My GC should have an
initializephase that is called before the application starts and usesGCFunctionMetadata::roots_begin()andend()fromGCMetadataPrinterto access the stack maps. -
The runtime should implement a function
safepoint_pollthat is called atsafepointsand uses the data collected in step 5 to check if a collection is required.
My runtime will implement something similar to the event loop in JS runtimes. I would like to be able to notify the event loop when the call stack is empty. Will this be possible using the stack map data?
Can someone more experienced confirm if I’m on the right path or not? If you know where can I find an example implementation that would be awesome.
Thanks!