1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>Accurate Garbage Collection with LLVM
</title>
6 <link rel=
"stylesheet" href=
"llvm.css" type=
"text/css">
10 <div class=
"doc_title">
11 Accurate Garbage Collection with LLVM
15 <li><a href=
"#introduction">Introduction
</a>
17 <li><a href=
"#feature">GC features provided and algorithms supported
</a></li>
21 <li><a href=
"#interfaces">Interfaces for user programs
</a>
23 <li><a href=
"#roots">Identifying GC roots on the stack:
<tt>llvm.gcroot
</tt></a></li>
24 <li><a href=
"#allocate">Allocating memory from the GC
</a></li>
25 <li><a href=
"#barriers">Reading and writing references to the heap
</a></li>
26 <li><a href=
"#explicit">Explicit invocation of the garbage collector
</a></li>
30 <li><a href=
"#gcimpl">Implementing a garbage collector
</a>
32 <li><a href=
"#llvm_gc_readwrite">Implementing
<tt>llvm_gc_read
</tt> and
<tt>llvm_gc_write
</tt></a></li>
33 <li><a href=
"#callbacks">Callback functions used to implement the garbage collector
</a></li>
36 <li><a href=
"#gcimpls">GC implementations available
</a>
38 <li><a href=
"#semispace">SemiSpace - A simple copying garbage collector
</a></li>
43 <li><a href="#codegen">Implementing GC support in a code generator</a></li>
47 <div class=
"doc_author">
48 <p>Written by
<a href=
"mailto:sabre@nondot.org">Chris Lattner
</a></p>
51 <!-- *********************************************************************** -->
52 <div class=
"doc_section">
53 <a name=
"introduction">Introduction
</a>
55 <!-- *********************************************************************** -->
57 <div class=
"doc_text">
59 <p>Garbage collection is a widely used technique that frees the programmer from
60 having to know the life-times of heap objects, making software easier to produce
61 and maintain. Many programming languages rely on garbage collection for
62 automatic memory management. There are two primary forms of garbage collection:
63 conservative and accurate.
</p>
65 <p>Conservative garbage collection often does not require any special support
66 from either the language or the compiler: it can handle non-type-safe
67 programming languages (such as C/C++) and does not require any special
68 information from the compiler. The [LINK] Boehm collector is an example of a
69 state-of-the-art conservative collector.
</p>
71 <p>Accurate garbage collection requires the ability to identify all pointers in
72 the program at run-time (which requires that the source-language be type-safe in
73 most cases). Identifying pointers at run-time requires compiler support to
74 locate all places that hold live pointer variables at run-time, including the
75 <a href=
"#roots">processor stack and registers
</a>.
</p>
78 Conservative garbage collection is attractive because it does not require any
79 special compiler support, but it does have problems. In particular, because the
80 conservative garbage collector cannot
<i>know
</i> that a particular word in the
81 machine is a pointer, it cannot move live objects in the heap (preventing the
82 use of compacting and generational GC algorithms) and it can occasionally suffer
83 from memory leaks due to integer values that happen to point to objects in the
84 program. In addition, some aggressive compiler transformations can break
85 conservative garbage collectors (though these seem rare in practice).
89 Accurate garbage collectors do not suffer from any of these problems, but they
90 can suffer from degraded scalar optimization of the program. In particular,
91 because the runtime must be able to identify and update all pointers active in
92 the program, some optimizations are less effective. In practice, however, the
93 locality and performance benefits of using aggressive garbage allocation
94 techniques dominates any low-level losses.
98 This document describes the mechanisms and interfaces provided by LLVM to
99 support accurate garbage collection.
104 <!-- ======================================================================= -->
105 <div class=
"doc_subsection">
106 <a name=
"feature">GC features provided and algorithms supported
</a>
109 <div class=
"doc_text">
112 LLVM provides support for a broad class of garbage collection algorithms,
113 including compacting semi-space collectors, mark-sweep collectors, generational
114 collectors, and even reference counting implementations. It includes support
115 for
<a href=
"#barriers">read and write barriers
</a>, and associating
<a
116 href=
"#roots">meta-data with stack objects
</a> (used for tagless garbage
117 collection). All LLVM code generators support garbage collection, including the
122 We hope that the primitive support built into LLVM is sufficient to support a
123 broad class of garbage collected languages, including Scheme, ML, scripting
124 languages, Java, C#, etc. That said, the implemented garbage collectors may
125 need to be extended to support language-specific features such as finalization,
126 weak references, or other features. As these needs are identified and
127 implemented, they should be added to this specification.
131 LLVM does not currently support garbage collection of multi-threaded programs or
132 GC-safe points other than function calls, but these will be added in the future
133 as there is interest.
138 <!-- *********************************************************************** -->
139 <div class=
"doc_section">
140 <a name=
"interfaces">Interfaces for user programs
</a>
142 <!-- *********************************************************************** -->
144 <div class=
"doc_text">
146 <p>This section describes the interfaces provided by LLVM and by the garbage
147 collector run-time that should be used by user programs. As such, this is the
148 interface that front-end authors should generate code for.
153 <!-- ======================================================================= -->
154 <div class=
"doc_subsection">
155 <a name=
"roots">Identifying GC roots on the stack:
<tt>llvm.gcroot
</tt></a>
158 <div class=
"doc_text">
160 <div class=
"doc_code"><tt>
161 void %llvm.gcroot(
<ty
>** %ptrloc,
<ty2
>* %metadata)
165 The
<tt>llvm.gcroot
</tt> intrinsic is used to inform LLVM of a pointer variable
166 on the stack. The first argument contains the address of the variable on the
167 stack, and the second contains a pointer to metadata that should be associated
168 with the pointer (which
<b>must
</b> be a constant or global value address). At
169 runtime, the
<tt>llvm.gcroot
</tt> intrinsic stores a null pointer into the
170 specified location to initialize the pointer.
</p>
173 Consider the following fragment of Java code:
178 Object X; // A null-initialized reference to an object
184 This block (which may be located in the middle of a function or in a loop nest),
185 could be compiled to this LLVM code:
190 ;; In the entry block for the function, allocate the
191 ;; stack space for X, which is an LLVM pointer.
195 ;;
"CodeBlock" is the block corresponding to the start
196 ;; of the scope above.
198 ;; Initialize the object, telling LLVM that it is now live.
199 ;; Java has type-tags on objects, so it doesn't need any
201 call void %llvm.gcroot(%Object** %X, sbyte* null)
204 ;; As the pointer goes out of scope, store a null value into
205 ;; it, to indicate that the value is no longer live.
206 store %Object* null, %Object** %X
212 <!-- ======================================================================= -->
213 <div class=
"doc_subsection">
214 <a name=
"allocate">Allocating memory from the GC
</a>
217 <div class=
"doc_text">
219 <div class=
"doc_code"><tt>
220 sbyte *%llvm_gc_allocate(unsigned %Size)
223 <p>The
<tt>llvm_gc_allocate
</tt> function is a global function defined by the
224 garbage collector implementation to allocate memory. It returns a
225 zeroed-out block of memory of the appropriate size.
</p>
229 <!-- ======================================================================= -->
230 <div class=
"doc_subsection">
231 <a name=
"barriers">Reading and writing references to the heap
</a>
234 <div class=
"doc_text">
236 <div class=
"doc_code"><tt>
237 sbyte *%llvm.gcread(sbyte *, sbyte **)
<br>
238 void %llvm.gcwrite(sbyte*, sbyte*, sbyte**)
241 <p>Several of the more interesting garbage collectors (e.g., generational
242 collectors) need to be informed when the mutator (the program that needs garbage
243 collection) reads or writes object references into the heap. In the case of a
244 generational collector, it needs to keep track of which
"old" generation objects
245 have references stored into them. The amount of code that typically needs to be
246 executed is usually quite small (and not on the critical path of any
247 computation), so the overall performance impact of the inserted code is
250 <p>To support garbage collectors that use read or write barriers, LLVM provides
251 the
<tt>llvm.gcread
</tt> and
<tt>llvm.gcwrite
</tt> intrinsics. The first
252 intrinsic has exactly the same semantics as a non-volatile LLVM load and the
253 second has the same semantics as a non-volatile LLVM store, with the
254 additions that they also take a pointer to the start of the memory
255 object as an argument. At code generation
256 time, these intrinsics are replaced with calls into the garbage collector
257 (
<tt><a href=
"#llvm_gc_readwrite">llvm_gc_read
</a></tt> and
<tt><a
258 href=
"#llvm_gc_readwrite">llvm_gc_write
</a></tt> respectively), which are then
259 inlined into the code.
263 If you are writing a front-end for a garbage collected language, every load or
264 store of a reference from or to the heap should use these intrinsics instead of
265 normal LLVM loads/stores.
</p>
269 <!-- ======================================================================= -->
270 <div class=
"doc_subsection">
271 <a name=
"initialize">Garbage collector startup and initialization
</a>
274 <div class=
"doc_text">
276 <div class=
"doc_code"><tt>
277 void %llvm_gc_initialize(unsigned %InitialHeapSize)
281 The
<tt>llvm_gc_initialize
</tt> function should be called once before any other
282 garbage collection functions are called. This gives the garbage collector the
283 chance to initialize itself and allocate the heap spaces. The initial heap size
284 to allocate should be specified as an argument.
289 <!-- ======================================================================= -->
290 <div class=
"doc_subsection">
291 <a name=
"explicit">Explicit invocation of the garbage collector
</a>
294 <div class=
"doc_text">
296 <div class=
"doc_code"><tt>
297 void %llvm_gc_collect()
301 The
<tt>llvm_gc_collect
</tt> function is exported by the garbage collector
302 implementations to provide a full collection, even when the heap is not
303 exhausted. This can be used by end-user code as a hint, and may be ignored by
304 the garbage collector.
310 <!-- *********************************************************************** -->
311 <div class=
"doc_section">
312 <a name=
"gcimpl">Implementing a garbage collector
</a>
314 <!-- *********************************************************************** -->
316 <div class=
"doc_text">
319 Implementing a garbage collector for LLVM is fairly straight-forward. The LLVM
320 garbage collectors are provided in a form that makes them easy to link into the
321 language-specific runtime that a language front-end would use. They require
322 functionality from the language-specific runtime to get information about
<a
323 href=
"#gcdescriptors">where pointers are located in heap objects
</a>.
327 implementation must include the
<a
328 href=
"#allocate"><tt>llvm_gc_allocate
</tt></a> and
<a
329 href=
"#explicit"><tt>llvm_gc_collect
</tt></a> functions, and it must implement
330 the
<a href=
"#llvm_gc_readwrite">read/write barrier
</a> functions as well. To
331 do this, it will probably have to
<a href=
"#traceroots">trace through the roots
332 from the stack
</a> and understand the
<a href=
"#gcdescriptors">GC descriptors
333 for heap objects
</a>. Luckily, there are some
<a href=
"#gcimpls">example
334 implementations
</a> available.
339 <!-- ======================================================================= -->
340 <div class=
"doc_subsection">
341 <a name=
"llvm_gc_readwrite">Implementing
<tt>llvm_gc_read
</tt> and
<tt>llvm_gc_write
</tt></a>
344 <div class=
"doc_text">
345 <div class=
"doc_code"><tt>
346 void *llvm_gc_read(void*, void **)
<br>
347 void llvm_gc_write(void*, void *, void**)
351 These functions
<i>must
</i> be implemented in every garbage collector, even if
352 they do not need read/write barriers. In this case, just load or store the
353 pointer, then return.
357 If an actual read or write barrier is needed, it should be straight-forward to
363 <!-- ======================================================================= -->
364 <div class=
"doc_subsection">
365 <a name=
"callbacks">Callback functions used to implement the garbage collector
</a>
368 <div class=
"doc_text">
370 Garbage collector implementations make use of call-back functions that are
371 implemented by other parts of the LLVM system.
375 <!--_________________________________________________________________________-->
376 <div class=
"doc_subsubsection">
377 <a name=
"traceroots">Tracing GC pointers from the program stack
</a>
380 <div class=
"doc_text">
381 <div class=
"doc_code"><tt>
382 void llvm_cg_walk_gcroots(void (*FP)(void **Root, void *Meta));
386 The
<tt>llvm_cg_walk_gcroots
</tt> function is a function provided by the code
387 generator that iterates through all of the GC roots on the stack, calling the
388 specified function pointer with each record. For each GC root, the address of
389 the pointer and the meta-data (from the
<a
390 href=
"#gcroot"><tt>llvm.gcroot
</tt></a> intrinsic) are provided.
394 <!--_________________________________________________________________________-->
395 <div class=
"doc_subsubsection">
396 <a name=
"staticroots">Tracing GC pointers from static roots
</a>
399 <div class=
"doc_text">
404 <!--_________________________________________________________________________-->
405 <div class=
"doc_subsubsection">
406 <a name=
"gcdescriptors">Tracing GC pointers from heap objects
</a>
409 <div class=
"doc_text">
411 The three most common ways to keep track of where pointers live in heap objects
412 are (listed in order of space overhead required):
</p>
415 <li>In languages with polymorphic objects, pointers from an object header are
416 usually used to identify the GC pointers in the heap object. This is common for
417 object-oriented languages like Self, Smalltalk, Java, or C#.
</li>
419 <li>If heap objects are not polymorphic, often the
"shape" of the heap can be
420 determined from the roots of the heap or from some other meta-data [
<a
421 href=
"#appel89">Appel89
</a>,
<a href=
"#goldberg91">Goldberg91
</a>,
<a
422 href=
"#tolmach94">Tolmach94
</a>]. In this case, the garbage collector can
423 propagate the information around from meta data stored with the roots. This
424 often eliminates the need to have a header on objects in the heap. This is
425 common in the ML family.
</li>
427 <li>If all heap objects have pointers in the same locations, or pointers can be
428 distinguished just by looking at them (e.g., the low order bit is clear), no
429 book-keeping is needed at all. This is common for Lisp-like languages.
</li>
432 <p>The LLVM garbage collectors are capable of supporting all of these styles of
433 language, including ones that mix various implementations. To do this, it
434 allows the source-language to associate meta-data with the
<a
435 href=
"#roots">stack roots
</a>, and the heap tracing routines can propagate the
436 information. In addition, LLVM allows the front-end to extract GC information
437 from in any form from a specific object pointer (this supports situations #
1 and
441 <p><b>Making this efficient
</b></p>
449 <!-- *********************************************************************** -->
450 <div class=
"doc_section">
451 <a name=
"gcimpls">GC implementations available
</a>
453 <!-- *********************************************************************** -->
455 <div class=
"doc_text">
458 To make this more concrete, the currently implemented LLVM garbage collectors
459 all live in the
<tt>llvm/runtime/GC/*
</tt> directories in the LLVM source-base.
460 If you are interested in implementing an algorithm, there are many interesting
461 possibilities (mark/sweep, a generational collector, a reference counting
462 collector, etc), or you could choose to improve one of the existing algorithms.
467 <!-- ======================================================================= -->
468 <div class=
"doc_subsection">
469 <a name=
"semispace">SemiSpace - A simple copying garbage collector
</a>
472 <div class=
"doc_text">
474 SemiSpace is a very simple copying collector. When it starts up, it allocates
475 two blocks of memory for the heap. It uses a simple bump-pointer allocator to
476 allocate memory from the first block until it runs out of space. When it runs
477 out of space, it traces through all of the roots of the program, copying blocks
478 to the other half of the memory space.
483 <!--_________________________________________________________________________-->
484 <div class=
"doc_subsubsection">
485 Possible Improvements
488 <div class=
"doc_text">
491 If a collection cycle happens and the heap is not compacted very much (say less
492 than
25% of the allocated memory was freed), the memory regions should be
497 <!-- *********************************************************************** -->
498 <div class=
"doc_section">
499 <a name=
"references">References
</a>
501 <!-- *********************************************************************** -->
503 <div class=
"doc_text">
505 <p><a name=
"appel89">[Appel89]
</a> Runtime Tags Aren't Necessary. Andrew
506 W. Appel. Lisp and Symbolic Computation
19(
7):
703-
705, July
1989.
</p>
508 <p><a name=
"goldberg91">[Goldberg91]
</a> Tag-free garbage collection for
509 strongly typed programming languages. Benjamin Goldberg. ACM SIGPLAN
512 <p><a name=
"tolmach94">[Tolmach94]
</a> Tag-free garbage collection using
513 explicit type parameters. Andrew Tolmach. Proceedings of the
1994 ACM
514 conference on LISP and functional programming.
</p>
518 <!-- *********************************************************************** -->
522 <a href=
"http://jigsaw.w3.org/css-validator/check/referer"><img
523 src=
"http://jigsaw.w3.org/css-validator/images/vcss" alt=
"Valid CSS!"></a>
524 <a href=
"http://validator.w3.org/check/referer"><img
525 src=
"http://www.w3.org/Icons/valid-html401" alt=
"Valid HTML 4.01!"></a>
527 <a href=
"mailto:sabre@nondot.org">Chris Lattner
</a><br>
528 <a href=
"http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure
</a><br>
529 Last modified: $Date$