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
69 <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/">Boehm collector</a> is
70 an example of a state-of-the-art conservative collector.</p>
72 <p>Accurate garbage collection requires the ability to identify all pointers in
73 the program at run-time (which requires that the source-language be type-safe in
74 most cases). Identifying pointers at run-time requires compiler support to
75 locate all places that hold live pointer variables at run-time, including the
76 <a href="#roots">processor stack and registers</a>.</p>
79 Conservative garbage collection is attractive because it does not require any
80 special compiler support, but it does have problems. In particular, because the
81 conservative garbage collector cannot <i>know</i> that a particular word in the
82 machine is a pointer, it cannot move live objects in the heap (preventing the
83 use of compacting and generational GC algorithms) and it can occasionally suffer
84 from memory leaks due to integer values that happen to point to objects in the
85 program. In addition, some aggressive compiler transformations can break
86 conservative garbage collectors (though these seem rare in practice).
90 Accurate garbage collectors do not suffer from any of these problems, but they
91 can suffer from degraded scalar optimization of the program. In particular,
92 because the runtime must be able to identify and update all pointers active in
93 the program, some optimizations are less effective. In practice, however, the
94 locality and performance benefits of using aggressive garbage allocation
95 techniques dominates any low-level losses.
99 This document describes the mechanisms and interfaces provided by LLVM to
100 support accurate garbage collection.
105 <!-- ======================================================================= -->
106 <div class="doc_subsection">
107 <a name="feature">GC features provided and algorithms supported</a>
110 <div class="doc_text">
113 LLVM provides support for a broad class of garbage collection algorithms,
114 including compacting semi-space collectors, mark-sweep collectors, generational
115 collectors, and even reference counting implementations. It includes support
116 for <a href="#barriers">read and write barriers</a>, and associating <a
117 href="#roots">meta-data with stack objects</a> (used for tagless garbage
118 collection). All LLVM code generators support garbage collection, including the
123 We hope that the primitive support built into LLVM is sufficient to support a
124 broad class of garbage collected languages, including Scheme, ML, scripting
125 languages, Java, C#, etc. That said, the implemented garbage collectors may
126 need to be extended to support language-specific features such as finalization,
127 weak references, or other features. As these needs are identified and
128 implemented, they should be added to this specification.
132 LLVM does not currently support garbage collection of multi-threaded programs or
133 GC-safe points other than function calls, but these will be added in the future
134 as there is interest.
139 <!-- *********************************************************************** -->
140 <div class="doc_section">
141 <a name="interfaces">Interfaces for user programs</a>
143 <!-- *********************************************************************** -->
145 <div class="doc_text">
147 <p>This section describes the interfaces provided by LLVM and by the garbage
148 collector run-time that should be used by user programs. As such, this is the
149 interface that front-end authors should generate code for.
154 <!-- ======================================================================= -->
155 <div class="doc_subsection">
156 <a name="roots">Identifying GC roots on the stack: <tt>llvm.gcroot</tt></a>
159 <div class="doc_text">
161 <div class="doc_code"><tt>
162 void %llvm.gcroot(i8** %ptrloc, i8* %metadata)
166 The <tt>llvm.gcroot</tt> intrinsic is used to inform LLVM of a pointer variable
167 on the stack. The first argument contains the address of the variable on the
168 stack, and the second contains a pointer to metadata that should be associated
169 with the pointer (which <b>must</b> be a constant or global value address).</p>
172 Consider the following fragment of Java code:
177 Object X; // A null-initialized reference to an object
183 This block (which may be located in the middle of a function or in a loop nest),
184 could be compiled to this LLVM code:
189 ;; In the entry block for the function, allocate the
190 ;; stack space for X, which is an LLVM pointer.
194 ;; Java null-initializes pointers.
195 store %Object* null, %Object** %X
197 ;; "CodeBlock" is the block corresponding to the start
198 ;; of the scope above.
200 ;; Initialize the object, telling LLVM that it is now live.
201 ;; Java has type-tags on objects, so it doesn't need any
203 %tmp = bitcast %Object** %X to i8**
204 call void %llvm.gcroot(i8** %tmp, i8* null)
207 ;; As the pointer goes out of scope, store a null value into
208 ;; it, to indicate that the value is no longer live.
209 store %Object* null, %Object** %X
215 <!-- ======================================================================= -->
216 <div class="doc_subsection">
217 <a name="allocate">Allocating memory from the GC</a>
220 <div class="doc_text">
222 <div class="doc_code"><tt>
223 void *%llvm_gc_allocate(unsigned %Size)
226 <p>The <tt>llvm_gc_allocate</tt> function is a global function defined by the
227 garbage collector implementation to allocate memory. It returns a
228 zeroed-out block of memory of the appropriate size.</p>
232 <!-- ======================================================================= -->
233 <div class="doc_subsection">
234 <a name="barriers">Reading and writing references to the heap</a>
237 <div class="doc_text">
239 <div class="doc_code"><tt>
240 i8 *%llvm.gcread(i8 *, i8 **)<br>
241 void %llvm.gcwrite(i8*, i8*, i8**)
244 <p>Several of the more interesting garbage collectors (e.g., generational
245 collectors) need to be informed when the mutator (the program that needs garbage
246 collection) reads or writes object references into the heap. In the case of a
247 generational collector, it needs to keep track of which "old" generation objects
248 have references stored into them. The amount of code that typically needs to be
249 executed is usually quite small (and not on the critical path of any
250 computation), so the overall performance impact of the inserted code is
253 <p>To support garbage collectors that use read or write barriers, LLVM provides
254 the <tt>llvm.gcread</tt> and <tt>llvm.gcwrite</tt> intrinsics. The first
255 intrinsic has exactly the same semantics as a non-volatile LLVM load and the
256 second has the same semantics as a non-volatile LLVM store, with the
257 additions that they also take a pointer to the start of the memory
258 object as an argument. At code generation
259 time, these intrinsics are replaced with calls into the garbage collector
260 (<tt><a href="#llvm_gc_readwrite">llvm_gc_read</a></tt> and <tt><a
261 href="#llvm_gc_readwrite">llvm_gc_write</a></tt> respectively), which are then
262 inlined into the code.
266 If you are writing a front-end for a garbage collected language, every load or
267 store of a reference from or to the heap should use these intrinsics instead of
268 normal LLVM loads/stores.</p>
272 <!-- ======================================================================= -->
273 <div class="doc_subsection">
274 <a name="initialize">Garbage collector startup and initialization</a>
277 <div class="doc_text">
279 <div class="doc_code"><tt>
280 void %llvm_gc_initialize(unsigned %InitialHeapSize)
284 The <tt>llvm_gc_initialize</tt> function should be called once before any other
285 garbage collection functions are called. This gives the garbage collector the
286 chance to initialize itself and allocate the heap spaces. The initial heap size
287 to allocate should be specified as an argument.
292 <!-- ======================================================================= -->
293 <div class="doc_subsection">
294 <a name="explicit">Explicit invocation of the garbage collector</a>
297 <div class="doc_text">
299 <div class="doc_code"><tt>
300 void %llvm_gc_collect()
304 The <tt>llvm_gc_collect</tt> function is exported by the garbage collector
305 implementations to provide a full collection, even when the heap is not
306 exhausted. This can be used by end-user code as a hint, and may be ignored by
307 the garbage collector.
313 <!-- *********************************************************************** -->
314 <div class="doc_section">
315 <a name="gcimpl">Implementing a garbage collector</a>
317 <!-- *********************************************************************** -->
319 <div class="doc_text">
322 Implementing a garbage collector for LLVM is fairly straight-forward. The LLVM
323 garbage collectors are provided in a form that makes them easy to link into the
324 language-specific runtime that a language front-end would use. They require
325 functionality from the language-specific runtime to get information about <a
326 href="#gcdescriptors">where pointers are located in heap objects</a>.
330 implementation must include the <a
331 href="#allocate"><tt>llvm_gc_allocate</tt></a> and <a
332 href="#explicit"><tt>llvm_gc_collect</tt></a> functions, and it must implement
333 the <a href="#llvm_gc_readwrite">read/write barrier</a> functions as well. To
334 do this, it will probably have to <a href="#traceroots">trace through the roots
335 from the stack</a> and understand the <a href="#gcdescriptors">GC descriptors
336 for heap objects</a>. Luckily, there are some <a href="#gcimpls">example
337 implementations</a> available.
342 <!-- ======================================================================= -->
343 <div class="doc_subsection">
344 <a name="llvm_gc_readwrite">Implementing <tt>llvm_gc_read</tt> and <tt>llvm_gc_write</tt></a>
347 <div class="doc_text">
348 <div class="doc_code"><tt>
349 void *llvm_gc_read(void*, void **)<br>
350 void llvm_gc_write(void*, void *, void**)
354 These functions <i>must</i> be implemented in every garbage collector, even if
355 they do not need read/write barriers. In this case, just load or store the
356 pointer, then return.
360 If an actual read or write barrier is needed, it should be straight-forward to
366 <!-- ======================================================================= -->
367 <div class="doc_subsection">
368 <a name="callbacks">Callback functions used to implement the garbage collector</a>
371 <div class="doc_text">
373 Garbage collector implementations make use of call-back functions that are
374 implemented by other parts of the LLVM system.
378 <!--_________________________________________________________________________-->
379 <div class="doc_subsubsection">
380 <a name="traceroots">Tracing GC pointers from the program stack</a>
383 <div class="doc_text">
384 <div class="doc_code"><tt>
385 void llvm_cg_walk_gcroots(void (*FP)(void **Root, void *Meta));
389 The <tt>llvm_cg_walk_gcroots</tt> function is a function provided by the code
390 generator that iterates through all of the GC roots on the stack, calling the
391 specified function pointer with each record. For each GC root, the address of
392 the pointer and the meta-data (from the <a
393 href="#roots"><tt>llvm.gcroot</tt></a> intrinsic) are provided.
397 <!--_________________________________________________________________________-->
398 <div class="doc_subsubsection">
399 <a name="staticroots">Tracing GC pointers from static roots</a>
402 <div class="doc_text">
407 <!--_________________________________________________________________________-->
408 <div class="doc_subsubsection">
409 <a name="gcdescriptors">Tracing GC pointers from heap objects</a>
412 <div class="doc_text">
414 The three most common ways to keep track of where pointers live in heap objects
415 are (listed in order of space overhead required):</p>
418 <li>In languages with polymorphic objects, pointers from an object header are
419 usually used to identify the GC pointers in the heap object. This is common for
420 object-oriented languages like Self, Smalltalk, Java, or C#.</li>
422 <li>If heap objects are not polymorphic, often the "shape" of the heap can be
423 determined from the roots of the heap or from some other meta-data [<a
424 href="#appel89">Appel89</a>, <a href="#goldberg91">Goldberg91</a>, <a
425 href="#tolmach94">Tolmach94</a>]. In this case, the garbage collector can
426 propagate the information around from meta data stored with the roots. This
427 often eliminates the need to have a header on objects in the heap. This is
428 common in the ML family.</li>
430 <li>If all heap objects have pointers in the same locations, or pointers can be
431 distinguished just by looking at them (e.g., the low order bit is clear), no
432 book-keeping is needed at all. This is common for Lisp-like languages.</li>
435 <p>The LLVM garbage collectors are capable of supporting all of these styles of
436 language, including ones that mix various implementations. To do this, it
437 allows the source-language to associate meta-data with the <a
438 href="#roots">stack roots</a>, and the heap tracing routines can propagate the
439 information. In addition, LLVM allows the front-end to extract GC information
440 from in any form from a specific object pointer (this supports situations #1 and
444 <p><b>Making this efficient</b></p>
452 <!-- *********************************************************************** -->
453 <div class="doc_section">
454 <a name="gcimpls">GC implementations available</a>
456 <!-- *********************************************************************** -->
458 <div class="doc_text">
461 To make this more concrete, the currently implemented LLVM garbage collectors
462 all live in the <tt>llvm/runtime/GC/*</tt> directories in the LLVM source-base.
463 If you are interested in implementing an algorithm, there are many interesting
464 possibilities (mark/sweep, a generational collector, a reference counting
465 collector, etc), or you could choose to improve one of the existing algorithms.
470 <!-- ======================================================================= -->
471 <div class="doc_subsection">
472 <a name="semispace">SemiSpace - A simple copying garbage collector</a>
475 <div class="doc_text">
477 SemiSpace is a very simple copying collector. When it starts up, it allocates
478 two blocks of memory for the heap. It uses a simple bump-pointer allocator to
479 allocate memory from the first block until it runs out of space. When it runs
480 out of space, it traces through all of the roots of the program, copying blocks
481 to the other half of the memory space.
486 <!--_________________________________________________________________________-->
487 <div class="doc_subsubsection">
488 Possible Improvements
491 <div class="doc_text">
494 If a collection cycle happens and the heap is not compacted very much (say less
495 than 25% of the allocated memory was freed), the memory regions should be
500 <!-- *********************************************************************** -->
501 <div class="doc_section">
502 <a name="references">References</a>
504 <!-- *********************************************************************** -->
506 <div class="doc_text">
508 <p><a name="appel89">[Appel89]</a> Runtime Tags Aren't Necessary. Andrew
509 W. Appel. Lisp and Symbolic Computation 19(7):703-705, July 1989.</p>
511 <p><a name="goldberg91">[Goldberg91]</a> Tag-free garbage collection for
512 strongly typed programming languages. Benjamin Goldberg. ACM SIGPLAN
515 <p><a name="tolmach94">[Tolmach94]</a> Tag-free garbage collection using
516 explicit type parameters. Andrew Tolmach. Proceedings of the 1994 ACM
517 conference on LISP and functional programming.</p>
521 <!-- *********************************************************************** -->
525 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
526 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
527 <a href="http://validator.w3.org/check/referer"><img
528 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
530 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
531 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
532 Last modified: $Date$