Implementing garbage collector for statepoint-example strategy

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:

  1. Declare a custom GC strategy with UseStatepoints = true; and UseRS4GC = true or use the statepoint-example built-in strategy. I plan to use statepoint-example unless I hit a technical issue that prevents me from using it.

  2. 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");
  1. Use the RewriteStatepointsForGC transformation pass will automatically introduce annotations like @llvm.experimental.gc.statepoint and @llvm.experimental.gc.relocate.

  2. Use the PlaceSafepoints transformation pas to automatically add calls to safepoint_poll at safe points.

  3. My GC should have an initialize phase that is called before the application starts and uses GCFunctionMetadata::roots_begin() and end() from GCMetadataPrinter to access the stack maps.

  4. The runtime should implement a function safepoint_poll that is called at safepoints and 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!