Garbage collector examples wanted (original) (raw)
November 13, 2025, 10:52am 1
I am writing a runtime for a garbage collected language. Obviously this is quite a complex topic, and I am definitely at the novice end of the spectrum. I think my best approach might be to:
- Read the docs ( Garbage Collection with LLVM — LLVM 22.0.0git documentation ).
- Understand a bit about how more advanced GCs work so I at least have an idea of the direction in which I am aiming longer term.
- Implement the simplest stop-the-world mark-and-sweep collector possible for the first pass.
It would be really helpful to find an exising project or two that implement garbage collectors and make use of LLVM GCStrategy and associated LLVM features that can assist with GC.
If anyone has any links they would care to share that might be helpful in this regard, do please share them!
zero9178 November 13, 2025, 11:19am 2
Hello!
A key thing to realize when using LLVMs GC facilities is to know/realize what it provides and what it doesn’t. LLVM only provides the bare minimum of metadata and code generation that is required for one to write a GC which mainly comes down to the stack map.
The stack map enables you to find all references (pointers in address space 1) that are alive at a specific program counter in a given frame and tells you how to retrieve them (whether they are in a register or in stack memory).
When implementing a GC you can then:
- Walk the stack
- For every frame in the stack, find the stack map entry
- Read all references in the frame and add it to the root set
- Perform garbage collection
- Optionally, perform relocation and write the new addresses of the root objects back into the frame
As far as implementation steps are concerned, you should:
- Use address space 1 for all pointers in IR that are “references” that need to be recorded in the stackmap.
- Schedule and run the
RewriteStatepointsForGCpass to turn all call instructions into statepoint intrinsics that record all alive references in the stack map - At runtime: Find a way to read the stackmap section.
We have previously implemented a very simple relocating GC in a JIT LLVM envrionment in a JVM implementation:
Some useful pointers might be: GitHub - JLLVM/JLLVM: JVM implementation using LLVM as a JIT
JLLVM/src/jllvm/vm/StackMapRegistrationPlugin.cpp at main · JLLVM/JLLVM · GitHub
JLLVM/src/jllvm/vm/Runtime.cpp at main · JLLVM/JLLVM · GitHub
JLLVM/src/jllvm/gc/GarbageCollector.cpp at main · JLLVM/JLLVM · GitHub
https://www.youtube.com/watch?v=g9b_G4ao3PY&pp=ygUSSkxMVk0gbWFya3VzIGJvZWNr
Thank you - in terms of complexity, code size and educational value, I don’t think I could have wished for a better example than your JLLVM code, that will certainly help me a lot.
avadeaux December 17, 2025, 5:23am 4
Hi, I’m working on a programming language project, and last summer I did the first version of the design and implementation of the garbage collector for my runtime. I’m not ready to make my actual garbage collector code public (I haven’t got to the point of testing it yet), but in order to understand how to get started I experimented with the statepoint mechanism in LLVM, and created a small demo to make sure I could get it to work. Perhaps you are already past that level, but in case you find it useful, you can find my demo and a description of it in this blog post: