Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" Content="text/html; charset=UTF-8" > |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 6 | <title>Accurate Garbage Collection with LLVM</title> |
| 7 | <link rel="stylesheet" href="llvm.css" type="text/css"> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 8 | <style type="text/css"> |
| 9 | .rowhead { text-align: left; background: inherit; } |
| 10 | .indent { padding-left: 1em; } |
| 11 | .optl { color: #BFBFBF; } |
| 12 | </style> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 13 | </head> |
| 14 | <body> |
| 15 | |
| 16 | <div class="doc_title"> |
| 17 | Accurate Garbage Collection with LLVM |
| 18 | </div> |
| 19 | |
| 20 | <ol> |
| 21 | <li><a href="#introduction">Introduction</a> |
| 22 | <ul> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 23 | <li><a href="#feature">Goals and non-goals</a></li> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 24 | </ul> |
| 25 | </li> |
| 26 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 27 | <li><a href="#quickstart">Getting started</a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 28 | <ul> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 29 | <li><a href="quickstart-compiler">In your compiler</a></li> |
| 30 | <li><a href="quickstart-runtime">In your runtime library</a></li> |
| 31 | <li><a href="shadow-stack">About the shadow stack</a></li> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 32 | </ul> |
| 33 | </li> |
| 34 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 35 | <li><a href="#core">Core support</a> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 36 | <ul> |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 37 | <li><a href="#gcattr">Specifying GC code generation: |
| 38 | <tt>gc "..."</tt></a></li> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 39 | <li><a href="#gcroot">Identifying GC roots on the stack: |
| 40 | <tt>llvm.gcroot</tt></a></li> |
| 41 | <li><a href="#barriers">Reading and writing references in the heap</a> |
| 42 | <ul> |
| 43 | <li><a href="#gcwrite">Write barrier: <tt>llvm.gcwrite</tt></a></li> |
| 44 | <li><a href="#gcread">Read barrier: <tt>llvm.gcread</tt></a></li> |
| 45 | </ul> |
| 46 | </li> |
| 47 | </ul> |
| 48 | </li> |
| 49 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 50 | <li><a href="#plugin">Compiler plugin interface</a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 51 | <ul> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 52 | <li><a href="#collector-algos">Overview of available features</a></li> |
| 53 | <li><a href="#stack-map">Computing stack maps</a></li> |
| 54 | <li><a href="#init-roots">Initializing roots to null: |
| 55 | <tt>InitRoots</tt></a></li> |
| 56 | <li><a href="#custom">Custom lowering of intrinsics: <tt>CustomRoots</tt>, |
| 57 | <tt>CustomReadBarriers</tt>, and <tt>CustomWriteBarriers</tt></a></li> |
| 58 | <li><a href="#safe-points">Generating safe points: |
| 59 | <tt>NeededSafePoints</tt></a></li> |
| 60 | <li><a href="#assembly">Emitting assembly code: |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 61 | <tt>GCMetadataPrinter</tt></a></li> |
Chris Lattner | 0b02dbc | 2004-07-09 05:03:54 +0000 | [diff] [blame] | 62 | </ul> |
Chris Lattner | 9b2a184 | 2004-05-27 05:52:10 +0000 | [diff] [blame] | 63 | </li> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 64 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 65 | <li><a href="#runtime-impl">Implementing a collector runtime</a> |
| 66 | <ul> |
| 67 | <li><a href="#gcdescriptors">Tracing GC pointers from heap |
| 68 | objects</a></li> |
| 69 | </ul> |
| 70 | </li> |
| 71 | |
| 72 | <li><a href="#references">References</a></li> |
| 73 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 74 | </ol> |
| 75 | |
| 76 | <div class="doc_author"> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 77 | <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a> and |
| 78 | Gordon Henriksen</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 79 | </div> |
| 80 | |
| 81 | <!-- *********************************************************************** --> |
| 82 | <div class="doc_section"> |
| 83 | <a name="introduction">Introduction</a> |
| 84 | </div> |
| 85 | <!-- *********************************************************************** --> |
| 86 | |
| 87 | <div class="doc_text"> |
| 88 | |
| 89 | <p>Garbage collection is a widely used technique that frees the programmer from |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 90 | having to know the lifetimes of heap objects, making software easier to produce |
| 91 | and maintain. Many programming languages rely on garbage collection for |
| 92 | automatic memory management. There are two primary forms of garbage collection: |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 93 | conservative and accurate.</p> |
| 94 | |
| 95 | <p>Conservative garbage collection often does not require any special support |
| 96 | from either the language or the compiler: it can handle non-type-safe |
| 97 | programming languages (such as C/C++) and does not require any special |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 98 | information from the compiler. The |
Jeff Cohen | 65fc36b | 2007-04-18 17:26:14 +0000 | [diff] [blame] | 99 | <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/">Boehm collector</a> is |
| 100 | an example of a state-of-the-art conservative collector.</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 101 | |
| 102 | <p>Accurate garbage collection requires the ability to identify all pointers in |
| 103 | the program at run-time (which requires that the source-language be type-safe in |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 104 | most cases). Identifying pointers at run-time requires compiler support to |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 105 | locate all places that hold live pointer variables at run-time, including the |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 106 | <a href="#gcroot">processor stack and registers</a>.</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 107 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 108 | <p>Conservative garbage collection is attractive because it does not require any |
| 109 | special compiler support, but it does have problems. In particular, because the |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 110 | conservative garbage collector cannot <i>know</i> that a particular word in the |
| 111 | machine is a pointer, it cannot move live objects in the heap (preventing the |
| 112 | use of compacting and generational GC algorithms) and it can occasionally suffer |
| 113 | from memory leaks due to integer values that happen to point to objects in the |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 114 | program. In addition, some aggressive compiler transformations can break |
| 115 | conservative garbage collectors (though these seem rare in practice).</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 116 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 117 | <p>Accurate garbage collectors do not suffer from any of these problems, but |
| 118 | they can suffer from degraded scalar optimization of the program. In particular, |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 119 | because the runtime must be able to identify and update all pointers active in |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 120 | the program, some optimizations are less effective. In practice, however, the |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 121 | locality and performance benefits of using aggressive garbage allocation |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 122 | techniques dominates any low-level losses.</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 123 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 124 | <p>This document describes the mechanisms and interfaces provided by LLVM to |
| 125 | support accurate garbage collection.</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 126 | |
| 127 | </div> |
| 128 | |
| 129 | <!-- ======================================================================= --> |
| 130 | <div class="doc_subsection"> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 131 | <a name="feature">Goals and non-goals</a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 132 | </div> |
| 133 | |
| 134 | <div class="doc_text"> |
| 135 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 136 | <p>LLVM's intermediate representation provides <a href="#intrinsics">garbage |
Chris Lattner | 05d6709 | 2008-04-24 05:59:56 +0000 | [diff] [blame] | 137 | collection intrinsics</a> that offer support for a broad class of |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 138 | collector models. For instance, the intrinsics permit:</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 139 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 140 | <ul> |
| 141 | <li>semi-space collectors</li> |
| 142 | <li>mark-sweep collectors</li> |
| 143 | <li>generational collectors</li> |
| 144 | <li>reference counting</li> |
| 145 | <li>incremental collectors</li> |
| 146 | <li>concurrent collectors</li> |
| 147 | <li>cooperative collectors</li> |
| 148 | </ul> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 149 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 150 | <p>We hope that the primitive support built into the LLVM IR is sufficient to |
| 151 | support a broad class of garbage collected languages including Scheme, ML, Java, |
| 152 | C#, Perl, Python, Lua, Ruby, other scripting languages, and more.</p> |
| 153 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 154 | <p>However, LLVM does not itself provide a garbage collector—this should |
| 155 | be part of your language's runtime library. LLVM provides a framework for |
| 156 | compile time <a href="#plugin">code generation plugins</a>. The role of these |
| 157 | plugins is to generate code and data structures which conforms to the <em>binary |
| 158 | interface</em> specified by the <em>runtime library</em>. This is similar to the |
| 159 | relationship between LLVM and DWARF debugging info, for example. The |
| 160 | difference primarily lies in the lack of an established standard in the domain |
| 161 | of garbage collection—thus the plugins.</p> |
| 162 | |
| 163 | <p>The aspects of the binary interface with which LLVM's GC support is |
| 164 | concerned are:</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 165 | |
| 166 | <ul> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 167 | <li>Creation of GC-safe points within code where collection is allowed to |
| 168 | execute safely.</li> |
| 169 | <li>Definition of a stack frame descriptor. For each safe point in the code, |
| 170 | a frame descriptor maps where object references are located within the |
| 171 | frame so that the GC may traverse and perhaps update them.</li> |
| 172 | <li>Write barriers when storing object references within the heap. These |
| 173 | are commonly used to optimize incremental scans.</li> |
| 174 | <li>Emission of read barriers when loading object references. These are |
| 175 | useful for interoperating with concurrent collectors.</li> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 176 | </ul> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 177 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 178 | <p>There are additional areas that LLVM does not directly address:</p> |
| 179 | |
| 180 | <ul> |
| 181 | <li>Registration of global roots.</li> |
| 182 | <li>Discovery or registration of stack frame descriptors.</li> |
| 183 | <li>The functions used by the program to allocate memory, trigger a |
| 184 | collection, etc.</li> |
| 185 | </ul> |
| 186 | |
| 187 | <p>In general, LLVM's support for GC does not include features which can be |
| 188 | adequately addressed with other features of the IR and does not specify a |
| 189 | particular binary interface. On the plus side, this means that you should be |
| 190 | able to integrate LLVM with an existing runtime. On the other hand, it leaves |
| 191 | a lot of work for the developer of a novel language. However, it's easy to get |
| 192 | started quickly and scale up to a more sophisticated implementation as your |
| 193 | compiler matures.</p> |
| 194 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 195 | </div> |
| 196 | |
| 197 | <!-- *********************************************************************** --> |
| 198 | <div class="doc_section"> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 199 | <a name="quickstart">Getting started</a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 200 | </div> |
| 201 | <!-- *********************************************************************** --> |
| 202 | |
| 203 | <div class="doc_text"> |
| 204 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 205 | <p>Using a GC with LLVM implies many things, for example:</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 206 | |
| 207 | <ul> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 208 | <li>Write a runtime library or find an existing one which implements a GC |
| 209 | heap.<ol> |
| 210 | <li>Implement a memory allocator.</li> |
| 211 | <li>Design a binary interface for frame descriptors, used to identify |
| 212 | references within a stack frame.*</li> |
| 213 | <li>Implement a stack crawler to discover functions on the call stack.*</li> |
| 214 | <li>Implement a registry for global roots.</li> |
| 215 | <li>Design a binary interface for type descriptors, used to map references |
| 216 | within heap objects.</li> |
| 217 | <li>Implement a collection routine bringing together all of the above.</li> |
| 218 | </ol></li> |
| 219 | <li>Emit compatible code from your compiler.<ul> |
| 220 | <li>Initialization in the main function.</li> |
| 221 | <li>Use the <tt>gc "..."</tt> attribute to enable GC code generation |
| 222 | (or <tt>F.setGC("...")</tt>).</li> |
| 223 | <li>Use <tt>@llvm.gcroot</tt> to mark stack roots.</li> |
| 224 | <li>Use <tt>@llvm.gcread</tt> and/or <tt>@llvm.gcwrite</tt> to |
| 225 | manipulate GC references, if necessary.</li> |
| 226 | <li>Allocate memory using the GC allocation routine provided by the |
| 227 | runtime library.</li> |
| 228 | <li>Generate type descriptors according to your runtime's binary interface.</li> |
| 229 | </ul></li> |
| 230 | <li>Write a compiler plugin to interface LLVM with the runtime library.*<ul> |
| 231 | <li>Lower <tt>@llvm.gcread</tt> and <tt>@llvm.gcwrite</tt> to appropriate |
| 232 | code sequences.*</li> |
| 233 | <li>Generate stack maps according to the runtime's binary interface.*</li> |
| 234 | </ul></li> |
| 235 | <li>Load the plugin into the compiler. Use <tt>llc -load</tt> or link the |
| 236 | plugin statically with your language's compiler.*</li> |
| 237 | <li>Link program executables with the runtime.</li> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 238 | </ul> |
| 239 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 240 | <p>To help with several of these tasks (those indicated with a *), LLVM |
| 241 | includes a highly portable, built-in ShadowStack code generator. It is compiled |
| 242 | into <tt>llc</tt> and works even with the interpreter and C backends.</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 243 | |
| 244 | </div> |
| 245 | |
| 246 | <!-- ======================================================================= --> |
| 247 | <div class="doc_subsection"> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 248 | <a name="quickstart-compiler">In your compiler</a> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 249 | </div> |
| 250 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 251 | <div class="doc_text"> |
| 252 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 253 | <p>To turn the shadow stack on for your functions, first call:</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 254 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 255 | <div class="doc_code"><pre |
| 256 | >F.setGC("shadow-stack");</pre></div> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 257 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 258 | <p>for each function your compiler emits. Since the shadow stack is built into |
| 259 | LLVM, you do not need to load a plugin.</p> |
| 260 | |
| 261 | <p>Your compiler must also use <tt>@llvm.gcroot</tt> as documented. |
| 262 | Don't forget to create a root for each intermediate value that is generated |
| 263 | when evaluating an expression. In <tt>h(f(), g())</tt>, the result of |
| 264 | <tt>f()</tt> could easily be collected if evaluating <tt>g()</tt> triggers a |
| 265 | collection.</p> |
| 266 | |
| 267 | <p>There's no need to use <tt>@llvm.gcread</tt> and <tt>@llvm.gcwrite</tt> over |
| 268 | plain <tt>load</tt> and <tt>store</tt> for now. You will need them when |
| 269 | switching to a more advanced GC.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 270 | |
| 271 | </div> |
| 272 | |
| 273 | <!-- ======================================================================= --> |
| 274 | <div class="doc_subsection"> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 275 | <a name="quickstart-runtime">In your runtime</a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 276 | </div> |
| 277 | |
| 278 | <div class="doc_text"> |
| 279 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 280 | <p>The shadow stack doesn't imply a memory allocation algorithm. A semispace |
| 281 | collector or building atop <tt>malloc</tt> are great places to start, and can |
| 282 | be implemented with very little code.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 283 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 284 | <p>When it comes time to collect, however, your runtime needs to traverse the |
| 285 | stack roots, and for this it needs to integrate with the shadow stack. Luckily, |
| 286 | doing so is very simple. (This code is heavily commented to help you |
| 287 | understand the data structure, but there are only 20 lines of meaningful |
| 288 | code.)</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 289 | |
| 290 | </div> |
| 291 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 292 | <div class="doc_code"><pre |
| 293 | >/// @brief A constant shadow stack frame descriptor. The compiler emits one of |
| 294 | /// these for each function. |
| 295 | /// |
| 296 | /// Storage of metadata values is elided if the %meta parameter to @llvm.gcroot |
| 297 | /// is null. |
| 298 | struct FrameMap { |
| 299 | int32_t NumRoots; //< Number of roots in stack frame. |
| 300 | int32_t NumMeta; //< Number of metadata descriptors. May be < NumRoots. |
| 301 | const void *Meta[0]; //< Metadata for each root. |
| 302 | }; |
| 303 | |
| 304 | /// @brief A link in the dynamic shadow stack. One of these is embedded in the |
| 305 | /// stack frame of each function on the call stack. |
| 306 | struct StackEntry { |
| 307 | StackEntry *Next; //< Link to next stack entry (the caller's). |
| 308 | const FrameMap *Map; //< Pointer to constant FrameMap. |
| 309 | void *Roots[0]; //< Stack roots (in-place array). |
| 310 | }; |
| 311 | |
| 312 | /// @brief The head of the singly-linked list of StackEntries. Functions push |
| 313 | /// and pop onto this in their prologue and epilogue. |
| 314 | /// |
| 315 | /// Since there is only a global list, this technique is not threadsafe. |
| 316 | StackEntry *llvm_gc_root_chain; |
| 317 | |
| 318 | /// @brief Calls Visitor(root, meta) for each GC root on the stack. |
| 319 | /// root and meta are exactly the values passed to |
| 320 | /// <tt>@llvm.gcroot</tt>. |
| 321 | /// |
| 322 | /// Visitor could be a function to recursively mark live objects. Or it |
| 323 | /// might copy them to another heap or generation. |
| 324 | /// |
| 325 | /// @param Visitor A function to invoke for every GC root on the stack. |
| 326 | void visitGCRoots(void (*Visitor)(void **Root, const void *Meta)) { |
| 327 | for (StackEntry *R = llvm_gc_root_chain; R; R = R->Next) { |
| 328 | unsigned i = 0; |
| 329 | |
| 330 | // For roots [0, NumMeta), the metadata pointer is in the FrameMap. |
| 331 | for (unsigned e = R->Map->NumMeta; i != e; ++i) |
| 332 | Visitor(&R->Roots[i], R->Map->Meta[i]); |
| 333 | |
| 334 | // For roots [NumMeta, NumRoots), the metadata pointer is null. |
| 335 | for (unsigned e = R->Map->NumRoots; i != e; ++i) |
| 336 | Visitor(&R->Roots[i], NULL); |
| 337 | } |
| 338 | }</pre></div> |
| 339 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 340 | <!-- ======================================================================= --> |
| 341 | <div class="doc_subsection"> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 342 | <a name="shadow-stack">About the shadow stack</a> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 343 | </div> |
| 344 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 345 | <div class="doc_text"> |
| 346 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 347 | <p>Unlike many GC algorithms which rely on a cooperative code generator to |
| 348 | generate stack maps, this algorithm carefully maintains a linked list of stack |
| 349 | root descriptors [<a href="#henderson02">Henderson2002</a>]. This so-called |
| 350 | "shadow stack" mirrors the machine stack. Maintaining this data structure is |
| 351 | slower than using stack maps, but has a significant portability advantage |
| 352 | because it requires no special support from the target code generator.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 353 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 354 | <p>The tradeoff for this simplicity and portability is:</p> |
| 355 | |
| 356 | <ul> |
| 357 | <li>High overhead per function call.</li> |
| 358 | <li>Not thread-safe.</li> |
| 359 | </ul> |
| 360 | |
| 361 | <p>Still, it's an easy way to get started.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 362 | |
| 363 | </div> |
| 364 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 365 | <!-- *********************************************************************** --> |
| 366 | <div class="doc_section"> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 367 | <a name="core">IR features</a><a name="intrinsics"></a> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 368 | </div> |
| 369 | <!-- *********************************************************************** --> |
| 370 | |
| 371 | <div class="doc_text"> |
| 372 | |
| 373 | <p>This section describes the garbage collection facilities provided by the |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 374 | <a href="LangRef.html">LLVM intermediate representation</a>. The exact behavior |
| 375 | of these IR features is specified by the binary interface implemented by a |
| 376 | <a href="#plugin">code generation plugin</a>, not by this document.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 377 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 378 | <p>These facilities are limited to those strictly necessary; they are not |
| 379 | intended to be a complete interface to any garbage collector. A program will |
| 380 | need to interface with the GC library using the facilities provided by that |
| 381 | program.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 382 | |
| 383 | </div> |
| 384 | |
| 385 | <!-- ======================================================================= --> |
| 386 | <div class="doc_subsection"> |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 387 | <a name="gcattr">Specifying GC code generation: <tt>gc "..."</tt></a> |
| 388 | </div> |
| 389 | |
| 390 | <div class="doc_code"><tt> |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 391 | define <i>ty</i> @<i>name</i>(...) <u>gc "<i>name</i>"</u> { ... |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 392 | </tt></div> |
| 393 | |
| 394 | <div class="doc_text"> |
| 395 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 396 | <p>The <tt>gc</tt> function attribute is used to specify the desired GC style |
| 397 | to the compiler. Its programmatic equivalent is the <tt>setGC</tt> method of |
| 398 | <tt>Function</tt>.</p> |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 399 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 400 | <p>Setting <tt>gc "<i>name</i>"</tt> on a function triggers a search for a |
| 401 | matching code generation plugin "<i>name</i>"; it is that plugin which defines |
| 402 | the exact nature of the code generated to support GC. If none is found, the |
| 403 | compiler will raise an error.</p> |
| 404 | |
| 405 | <p>Specifying the GC style on a per-function basis allows LLVM to link together |
| 406 | programs that use different garbage collection algorithms (or none at all).</p> |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 407 | |
| 408 | </div> |
| 409 | |
| 410 | <!-- ======================================================================= --> |
| 411 | <div class="doc_subsection"> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 412 | <a name="gcroot">Identifying GC roots on the stack: <tt>llvm.gcroot</tt></a> |
| 413 | </div> |
| 414 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 415 | <div class="doc_code"><tt> |
Chris Lattner | 1734055 | 2008-04-24 06:00:30 +0000 | [diff] [blame] | 416 | void @llvm.gcroot(i8** %ptrloc, i8* %metadata) |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 417 | </tt></div> |
| 418 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 419 | <div class="doc_text"> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 420 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 421 | <p>The <tt>llvm.gcroot</tt> intrinsic is used to inform LLVM that a stack |
| 422 | variable references an object on the heap and is to be tracked for garbage |
| 423 | collection. The exact impact on generated code is specified by a <a |
| 424 | href="#plugin">compiler plugin</a>.</p> |
| 425 | |
| 426 | <p>A compiler which uses mem2reg to raise imperative code using <tt>alloca</tt> |
| 427 | into SSA form need only add a call to <tt>@llvm.gcroot</tt> for those variables |
| 428 | which a pointers into the GC heap.</p> |
| 429 | |
| 430 | <p>It is also important to mark intermediate values with <tt>llvm.gcroot</tt>. |
| 431 | For example, consider <tt>h(f(), g())</tt>. Beware leaking the result of |
| 432 | <tt>f()</tt> in the case that <tt>g()</tt> triggers a collection.</p> |
| 433 | |
| 434 | <p>The first argument <b>must</b> be a value referring to an alloca instruction |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 435 | or a bitcast of an alloca. The second contains a pointer to metadata that |
| 436 | should be associated with the pointer, and <b>must</b> be a constant or global |
| 437 | value address. If your target collector uses tags, use a null pointer for |
| 438 | metadata.</p> |
| 439 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 440 | <p>The <tt>%metadata</tt> argument can be used to avoid requiring heap objects |
| 441 | to have 'isa' pointers or tag bits. [<a href="#appel89">Appel89</a>, <a |
| 442 | href="#goldberg91">Goldberg91</a>, <a href="#tolmach94">Tolmach94</a>] If |
| 443 | specified, its value will be tracked along with the location of the pointer in |
| 444 | the stack frame.</p> |
| 445 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 446 | <p>Consider the following fragment of Java code:</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 447 | |
| 448 | <pre> |
| 449 | { |
| 450 | Object X; // A null-initialized reference to an object |
| 451 | ... |
| 452 | } |
| 453 | </pre> |
| 454 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 455 | <p>This block (which may be located in the middle of a function or in a loop |
| 456 | nest), could be compiled to this LLVM code:</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 457 | |
| 458 | <pre> |
| 459 | Entry: |
| 460 | ;; In the entry block for the function, allocate the |
| 461 | ;; stack space for X, which is an LLVM pointer. |
| 462 | %X = alloca %Object* |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 463 | |
| 464 | ;; Tell LLVM that the stack space is a stack root. |
| 465 | ;; Java has type-tags on objects, so we pass null as metadata. |
| 466 | %tmp = bitcast %Object** %X to i8** |
Chris Lattner | 1734055 | 2008-04-24 06:00:30 +0000 | [diff] [blame] | 467 | call void @llvm.gcroot(i8** %X, i8* null) |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 468 | ... |
| 469 | |
| 470 | ;; "CodeBlock" is the block corresponding to the start |
Reid Spencer | 03d186a | 2004-05-25 08:45:31 +0000 | [diff] [blame] | 471 | ;; of the scope above. |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 472 | CodeBlock: |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 473 | ;; Java null-initializes pointers. |
| 474 | store %Object* null, %Object** %X |
| 475 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 476 | ... |
| 477 | |
| 478 | ;; As the pointer goes out of scope, store a null value into |
| 479 | ;; it, to indicate that the value is no longer live. |
| 480 | store %Object* null, %Object** %X |
| 481 | ... |
| 482 | </pre> |
| 483 | |
| 484 | </div> |
| 485 | |
| 486 | <!-- ======================================================================= --> |
| 487 | <div class="doc_subsection"> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 488 | <a name="barriers">Reading and writing references in the heap</a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 489 | </div> |
| 490 | |
| 491 | <div class="doc_text"> |
| 492 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 493 | <p>Some collectors need to be informed when the mutator (the program that needs |
| 494 | garbage collection) either reads a pointer from or writes a pointer to a field |
| 495 | of a heap object. The code fragments inserted at these points are called |
| 496 | <em>read barriers</em> and <em>write barriers</em>, respectively. The amount of |
| 497 | code that needs to be executed is usually quite small and not on the critical |
| 498 | path of any computation, so the overall performance impact of the barrier is |
| 499 | tolerable.</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 500 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 501 | <p>Barriers often require access to the <em>object pointer</em> rather than the |
| 502 | <em>derived pointer</em> (which is a pointer to the field within the |
| 503 | object). Accordingly, these intrinsics take both pointers as separate arguments |
| 504 | for completeness. In this snippet, <tt>%object</tt> is the object pointer, and |
| 505 | <tt>%derived</tt> is the derived pointer:</p> |
| 506 | |
Chris Lattner | 05d6709 | 2008-04-24 05:59:56 +0000 | [diff] [blame] | 507 | <blockquote><pre> |
| 508 | ;; An array type. |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 509 | %class.Array = type { %class.Object, i32, [0 x %class.Object*] } |
Chris Lattner | 05d6709 | 2008-04-24 05:59:56 +0000 | [diff] [blame] | 510 | ... |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 511 | |
| 512 | ;; Load the object pointer from a gcroot. |
| 513 | %object = load %class.Array** %object_addr |
| 514 | |
| 515 | ;; Compute the derived pointer. |
Chris Lattner | 05d6709 | 2008-04-24 05:59:56 +0000 | [diff] [blame] | 516 | %derived = getelementptr %object, i32 0, i32 2, i32 %n</pre></blockquote> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 517 | |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 518 | <p>The use of these intrinsics is naturally optional if the target GC does |
| 519 | require the corresponding barrier. If so, the GC plugin will replace the |
| 520 | intrinsic calls with the corresponding <tt>load</tt> or <tt>store</tt> |
| 521 | instruction if they are used.</p> |
| 522 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 523 | </div> |
| 524 | |
| 525 | <!-- ======================================================================= --> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 526 | <div class="doc_subsubsection"> |
| 527 | <a name="gcwrite">Write barrier: <tt>llvm.gcwrite</tt></a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 528 | </div> |
| 529 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 530 | <div class="doc_code"><tt> |
| 531 | void @llvm.gcwrite(i8* %value, i8* %object, i8** %derived) |
| 532 | </tt></div> |
| 533 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 534 | <div class="doc_text"> |
| 535 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 536 | <p>For write barriers, LLVM provides the <tt>llvm.gcwrite</tt> intrinsic |
| 537 | function. It has exactly the same semantics as a non-volatile <tt>store</tt> to |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 538 | the derived pointer (the third argument). The exact code generated is specified |
| 539 | by a <a href="#plugin">compiler plugin</a>.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 540 | |
| 541 | <p>Many important algorithms require write barriers, including generational |
| 542 | and concurrent collectors. Additionally, write barriers could be used to |
| 543 | implement reference counting.</p> |
| 544 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 545 | </div> |
| 546 | |
| 547 | <!-- ======================================================================= --> |
| 548 | <div class="doc_subsubsection"> |
| 549 | <a name="gcread">Read barrier: <tt>llvm.gcread</tt></a> |
| 550 | </div> |
| 551 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 552 | <div class="doc_code"><tt> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 553 | i8* @llvm.gcread(i8* %object, i8** %derived)<br> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 554 | </tt></div> |
| 555 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 556 | <div class="doc_text"> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 557 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 558 | <p>For read barriers, LLVM provides the <tt>llvm.gcread</tt> intrinsic function. |
| 559 | It has exactly the same semantics as a non-volatile <tt>load</tt> from the |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 560 | derived pointer (the second argument). The exact code generated is specified by |
| 561 | a <a href="#plugin">compiler plugin</a>.</p> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 562 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 563 | <p>Read barriers are needed by fewer algorithms than write barriers, and may |
| 564 | have a greater performance impact since pointer reads are more frequent than |
| 565 | writes.</p> |
| 566 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 567 | </div> |
| 568 | |
| 569 | <!-- *********************************************************************** --> |
| 570 | <div class="doc_section"> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 571 | <a name="plugin">Implementing a collector plugin</a> |
| 572 | </div> |
| 573 | <!-- *********************************************************************** --> |
| 574 | |
| 575 | <div class="doc_text"> |
| 576 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 577 | <p>User code specifies which GC code generation to use with the <tt>gc</tt> |
| 578 | function attribute or, equivalently, with the <tt>setGC</tt> method of |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 579 | <tt>Function</tt>.</p> |
| 580 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 581 | <p>To implement a GC plugin, it is necessary to subclass |
| 582 | <tt>llvm::GCStrategy</tt>, which can be accomplished in a few lines of |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 583 | boilerplate code. LLVM's infrastructure provides access to several important |
| 584 | algorithms. For an uncontroversial collector, all that remains may be to emit |
| 585 | the assembly code for the collector's unique stack map data structure, which |
| 586 | might be accomplished in as few as 100 LOC.</p> |
| 587 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 588 | <p>This is not the appropriate place to implement a garbage collected heap or a |
| 589 | garbage collector itself. That code should exist in the language's runtime |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 590 | library. The compiler plugin is responsible for generating code which |
| 591 | conforms to the binary interface defined by library, most essentially the |
| 592 | <a href="stack-map">stack map</a>.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 593 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 594 | <p>To subclass <tt>llvm::GCStrategy</tt> and register it with the compiler:</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 595 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 596 | <blockquote><pre>// lib/MyGC/MyGC.cpp - Example LLVM GC plugin |
| 597 | |
| 598 | #include "llvm/CodeGen/GCStrategy.h" |
| 599 | #include "llvm/CodeGen/GCMetadata.h" |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 600 | #include "llvm/Support/Compiler.h" |
| 601 | |
| 602 | using namespace llvm; |
| 603 | |
| 604 | namespace { |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 605 | class VISIBILITY_HIDDEN MyGC : public GCStrategy { |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 606 | public: |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 607 | MyGC() {} |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 608 | }; |
| 609 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 610 | GCRegistry::Add<MyGC> |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 611 | X("mygc", "My bespoke garbage collector."); |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 612 | }</pre></blockquote> |
| 613 | |
| 614 | <p>Using the LLVM makefiles (like the <a |
| 615 | href="http://llvm.org/viewvc/llvm-project/llvm/trunk/projects/sample/">sample |
| 616 | project</a>), this can be built into a plugin using a simple makefile:</p> |
| 617 | |
| 618 | <blockquote><pre |
| 619 | ># lib/MyGC/Makefile |
| 620 | |
| 621 | LEVEL := ../.. |
| 622 | LIBRARYNAME = <var>MyGC</var> |
| 623 | LOADABLE_MODULE = 1 |
| 624 | |
| 625 | include $(LEVEL)/Makefile.common</pre></blockquote> |
| 626 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 627 | <p>Once the plugin is compiled, code using it may be compiled using <tt>llc |
| 628 | -load=<var>MyGC.so</var></tt> (though <var>MyGC.so</var> may have some other |
| 629 | platform-specific extension):</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 630 | |
| 631 | <blockquote><pre |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 632 | >$ cat sample.ll |
| 633 | define void @f() gc "mygc" { |
| 634 | entry: |
| 635 | ret void |
| 636 | } |
| 637 | $ llvm-as < sample.ll | llc -load=MyGC.so</pre></blockquote> |
| 638 | |
| 639 | <p>It is also possible to statically link the collector plugin into tools, such |
| 640 | as a language-specific compiler front-end.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 641 | |
| 642 | </div> |
| 643 | |
| 644 | <!-- ======================================================================= --> |
| 645 | <div class="doc_subsection"> |
| 646 | <a name="collector-algos">Overview of available features</a> |
| 647 | </div> |
| 648 | |
| 649 | <div class="doc_text"> |
| 650 | |
| 651 | <p>The boilerplate collector above does nothing. More specifically:</p> |
| 652 | |
| 653 | <ul> |
| 654 | <li><tt>llvm.gcread</tt> calls are replaced with the corresponding |
| 655 | <tt>load</tt> instruction.</li> |
| 656 | <li><tt>llvm.gcwrite</tt> calls are replaced with the corresponding |
| 657 | <tt>store</tt> instruction.</li> |
| 658 | <li>No stack map is emitted, and no safe points are added.</li> |
| 659 | </ul> |
| 660 | |
| 661 | <p><tt>Collector</tt> provides a range of features through which a plugin |
| 662 | collector may do useful work. This matrix summarizes the supported (and planned) |
| 663 | features and correlates them with the collection techniques which typically |
| 664 | require them.</p> |
| 665 | |
| 666 | <table> |
| 667 | <tr> |
| 668 | <th>Algorithm</th> |
| 669 | <th>Done</th> |
| 670 | <th>shadow stack</th> |
| 671 | <th>refcount</th> |
| 672 | <th>mark-sweep</th> |
| 673 | <th>copying</th> |
| 674 | <th>incremental</th> |
| 675 | <th>threaded</th> |
| 676 | <th>concurrent</th> |
| 677 | </tr> |
| 678 | <tr> |
| 679 | <th class="rowhead"><a href="#stack-map">stack map</a></th> |
| 680 | <td>✔</td> |
| 681 | <td></td> |
| 682 | <td></td> |
| 683 | <td>✘</td> |
| 684 | <td>✘</td> |
| 685 | <td>✘</td> |
| 686 | <td>✘</td> |
| 687 | <td>✘</td> |
| 688 | </tr> |
| 689 | <tr> |
| 690 | <th class="rowhead"><a href="#init-roots">initialize roots</a></th> |
| 691 | <td>✔</td> |
| 692 | <td>✘</td> |
| 693 | <td>✘</td> |
| 694 | <td>✘</td> |
| 695 | <td>✘</td> |
| 696 | <td>✘</td> |
| 697 | <td>✘</td> |
| 698 | <td>✘</td> |
| 699 | </tr> |
| 700 | <tr class="doc_warning"> |
| 701 | <th class="rowhead">derived pointers</th> |
| 702 | <td>NO</td> |
| 703 | <td></td> |
| 704 | <td></td> |
| 705 | <td></td> |
| 706 | <td></td> |
| 707 | <td></td> |
| 708 | <td>✘*</td> |
| 709 | <td>✘*</td> |
| 710 | </tr> |
| 711 | <tr> |
| 712 | <th class="rowhead"><em><a href="#custom">custom lowering</a></em></th> |
| 713 | <td>✔</td> |
| 714 | <th></th> |
| 715 | <th></th> |
| 716 | <th></th> |
| 717 | <th></th> |
| 718 | <th></th> |
| 719 | <th></th> |
| 720 | <th></th> |
| 721 | </tr> |
| 722 | <tr> |
| 723 | <th class="rowhead indent">gcroot</th> |
| 724 | <td>✔</td> |
| 725 | <td>✘</td> |
| 726 | <td>✘</td> |
| 727 | <td></td> |
| 728 | <td></td> |
| 729 | <td></td> |
| 730 | <td></td> |
| 731 | <td></td> |
| 732 | </tr> |
| 733 | <tr> |
| 734 | <th class="rowhead indent">gcwrite</th> |
| 735 | <td>✔</td> |
| 736 | <td></td> |
| 737 | <td>✘</td> |
| 738 | <td></td> |
| 739 | <td></td> |
| 740 | <td>✘</td> |
| 741 | <td></td> |
| 742 | <td>✘</td> |
| 743 | </tr> |
| 744 | <tr> |
| 745 | <th class="rowhead indent">gcread</th> |
| 746 | <td>✔</td> |
| 747 | <td></td> |
| 748 | <td></td> |
| 749 | <td></td> |
| 750 | <td></td> |
| 751 | <td></td> |
| 752 | <td></td> |
| 753 | <td>✘</td> |
| 754 | </tr> |
| 755 | <tr> |
| 756 | <th class="rowhead"><em><a href="#safe-points">safe points</a></em></th> |
| 757 | <td></td> |
| 758 | <th></th> |
| 759 | <th></th> |
| 760 | <th></th> |
| 761 | <th></th> |
| 762 | <th></th> |
| 763 | <th></th> |
| 764 | <th></th> |
| 765 | </tr> |
| 766 | <tr> |
| 767 | <th class="rowhead indent">in calls</th> |
| 768 | <td>✔</td> |
| 769 | <td></td> |
| 770 | <td></td> |
| 771 | <td>✘</td> |
| 772 | <td>✘</td> |
| 773 | <td>✘</td> |
| 774 | <td>✘</td> |
| 775 | <td>✘</td> |
| 776 | </tr> |
| 777 | <tr> |
| 778 | <th class="rowhead indent">before calls</th> |
| 779 | <td>✔</td> |
| 780 | <td></td> |
| 781 | <td></td> |
| 782 | <td></td> |
| 783 | <td></td> |
| 784 | <td></td> |
| 785 | <td>✘</td> |
| 786 | <td>✘</td> |
| 787 | </tr> |
| 788 | <tr class="doc_warning"> |
| 789 | <th class="rowhead indent">for loops</th> |
| 790 | <td>NO</td> |
| 791 | <td></td> |
| 792 | <td></td> |
| 793 | <td></td> |
| 794 | <td></td> |
| 795 | <td></td> |
| 796 | <td>✘</td> |
| 797 | <td>✘</td> |
| 798 | </tr> |
| 799 | <tr> |
| 800 | <th class="rowhead indent">before escape</th> |
| 801 | <td>✔</td> |
| 802 | <td></td> |
| 803 | <td></td> |
| 804 | <td></td> |
| 805 | <td></td> |
| 806 | <td></td> |
| 807 | <td>✘</td> |
| 808 | <td>✘</td> |
| 809 | </tr> |
| 810 | <tr class="doc_warning"> |
| 811 | <th class="rowhead">emit code at safe points</th> |
| 812 | <td>NO</td> |
| 813 | <td></td> |
| 814 | <td></td> |
| 815 | <td></td> |
| 816 | <td></td> |
| 817 | <td></td> |
| 818 | <td>✘</td> |
| 819 | <td>✘</td> |
| 820 | </tr> |
| 821 | <tr> |
| 822 | <th class="rowhead"><em>output</em></th> |
| 823 | <td></td> |
| 824 | <th></th> |
| 825 | <th></th> |
| 826 | <th></th> |
| 827 | <th></th> |
| 828 | <th></th> |
| 829 | <th></th> |
| 830 | <th></th> |
| 831 | </tr> |
| 832 | <tr> |
| 833 | <th class="rowhead indent"><a href="#assembly">assembly</a></th> |
| 834 | <td>✔</td> |
| 835 | <td></td> |
| 836 | <td></td> |
| 837 | <td>✘</td> |
| 838 | <td>✘</td> |
| 839 | <td>✘</td> |
| 840 | <td>✘</td> |
| 841 | <td>✘</td> |
| 842 | </tr> |
| 843 | <tr class="doc_warning"> |
| 844 | <th class="rowhead indent">JIT</th> |
| 845 | <td>NO</td> |
| 846 | <td></td> |
| 847 | <td></td> |
| 848 | <td class="optl">✘</td> |
| 849 | <td class="optl">✘</td> |
| 850 | <td class="optl">✘</td> |
| 851 | <td class="optl">✘</td> |
| 852 | <td class="optl">✘</td> |
| 853 | </tr> |
| 854 | <tr class="doc_warning"> |
| 855 | <th class="rowhead indent">obj</th> |
| 856 | <td>NO</td> |
| 857 | <td></td> |
| 858 | <td></td> |
| 859 | <td class="optl">✘</td> |
| 860 | <td class="optl">✘</td> |
| 861 | <td class="optl">✘</td> |
| 862 | <td class="optl">✘</td> |
| 863 | <td class="optl">✘</td> |
| 864 | </tr> |
| 865 | <tr class="doc_warning"> |
| 866 | <th class="rowhead">live analysis</th> |
| 867 | <td>NO</td> |
| 868 | <td></td> |
| 869 | <td></td> |
| 870 | <td class="optl">✘</td> |
| 871 | <td class="optl">✘</td> |
| 872 | <td class="optl">✘</td> |
| 873 | <td class="optl">✘</td> |
| 874 | <td class="optl">✘</td> |
| 875 | </tr> |
| 876 | <tr class="doc_warning"> |
| 877 | <th class="rowhead">register map</th> |
| 878 | <td>NO</td> |
| 879 | <td></td> |
| 880 | <td></td> |
| 881 | <td class="optl">✘</td> |
| 882 | <td class="optl">✘</td> |
| 883 | <td class="optl">✘</td> |
| 884 | <td class="optl">✘</td> |
| 885 | <td class="optl">✘</td> |
| 886 | </tr> |
| 887 | <tr> |
| 888 | <td colspan="10"> |
| 889 | <div><span class="doc_warning">*</span> Derived pointers only pose a |
| 890 | hazard to copying collectors.</div> |
| 891 | <div><span class="optl">✘</span> in gray denotes a feature which |
| 892 | could be utilized if available.</div> |
| 893 | </td> |
| 894 | </tr> |
| 895 | </table> |
| 896 | |
| 897 | <p>To be clear, the collection techniques above are defined as:</p> |
| 898 | |
| 899 | <dl> |
| 900 | <dt>Shadow Stack</dt> |
| 901 | <dd>The mutator carefully maintains a linked list of stack root |
| 902 | descriptors.</dd> |
| 903 | <dt>Reference Counting</dt> |
| 904 | <dd>The mutator maintains a reference count for each object and frees an |
| 905 | object when its count falls to zero.</dd> |
| 906 | <dt>Mark-Sweep</dt> |
| 907 | <dd>When the heap is exhausted, the collector marks reachable objects starting |
| 908 | from the roots, then deallocates unreachable objects in a sweep |
| 909 | phase.</dd> |
| 910 | <dt>Copying</dt> |
| 911 | <dd>As reachability analysis proceeds, the collector copies objects from one |
| 912 | heap area to another, compacting them in the process. Copying collectors |
| 913 | enable highly efficient "bump pointer" allocation and can improve locality |
| 914 | of reference.</dd> |
| 915 | <dt>Incremental</dt> |
| 916 | <dd>(Including generational collectors.) Incremental collectors generally have |
| 917 | all the properties of a copying collector (regardless of whether the |
| 918 | mature heap is compacting), but bring the added complexity of requiring |
| 919 | write barriers.</dd> |
| 920 | <dt>Threaded</dt> |
| 921 | <dd>Denotes a multithreaded mutator; the collector must still stop the mutator |
| 922 | ("stop the world") before beginning reachability analysis. Stopping a |
| 923 | multithreaded mutator is a complicated problem. It generally requires |
| 924 | highly platform specific code in the runtime, and the production of |
| 925 | carefully designed machine code at safe points.</dd> |
| 926 | <dt>Concurrent</dt> |
| 927 | <dd>In this technique, the mutator and the collector run concurrently, with |
| 928 | the goal of eliminating pause times. In a <em>cooperative</em> collector, |
| 929 | the mutator further aids with collection should a pause occur, allowing |
| 930 | collection to take advantage of multiprocessor hosts. The "stop the world" |
| 931 | problem of threaded collectors is generally still present to a limited |
| 932 | extent. Sophisticated marking algorithms are necessary. Read barriers may |
| 933 | be necessary.</dd> |
| 934 | </dl> |
| 935 | |
| 936 | <p>As the matrix indicates, LLVM's garbage collection infrastructure is already |
| 937 | suitable for a wide variety of collectors, but does not currently extend to |
| 938 | multithreaded programs. This will be added in the future as there is |
| 939 | interest.</p> |
| 940 | |
| 941 | </div> |
| 942 | |
| 943 | <!-- ======================================================================= --> |
| 944 | <div class="doc_subsection"> |
| 945 | <a name="stack-map">Computing stack maps</a> |
| 946 | </div> |
| 947 | |
| 948 | <div class="doc_text"> |
| 949 | |
| 950 | <blockquote><pre |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 951 | >for (iterator I = begin(), E = end(); I != E; ++I) { |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 952 | GCFunctionInfo *FI = *I; |
| 953 | unsigned FrameSize = FI->getFrameSize(); |
| 954 | size_t RootCount = FI->roots_size(); |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 955 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 956 | for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(), |
| 957 | RE = FI->roots_end(); |
| 958 | RI != RE; ++RI) { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 959 | int RootNum = RI->Num; |
| 960 | int RootStackOffset = RI->StackOffset; |
| 961 | Constant *RootMetadata = RI->Metadata; |
| 962 | } |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 963 | }</pre></blockquote> |
| 964 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 965 | <p>LLVM automatically computes a stack map. All a <tt>GCStrategy</tt> needs to do |
| 966 | is access it using <tt>GCFunctionMetadata::roots_begin()</tt> and |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 967 | -<tt>end()</tt>. If the <tt>llvm.gcroot</tt> intrinsic is eliminated before code |
| 968 | generation by a custom lowering pass, LLVM's stack map will be empty.</p> |
| 969 | |
| 970 | </div> |
| 971 | |
| 972 | |
| 973 | <!-- ======================================================================= --> |
| 974 | <div class="doc_subsection"> |
| 975 | <a name="init-roots">Initializing roots to null: <tt>InitRoots</tt></a> |
| 976 | </div> |
| 977 | |
| 978 | <div class="doc_text"> |
| 979 | |
| 980 | <blockquote><pre |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 981 | >MyGC::MyGC() { |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 982 | InitRoots = true; |
| 983 | }</pre></blockquote> |
| 984 | |
| 985 | <p>When set, LLVM will automatically initialize each root to <tt>null</tt> upon |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 986 | entry to the function. This prevents the GC's sweep phase from visiting |
| 987 | uninitialized pointers, which will almost certainly cause it to crash. This |
| 988 | initialization occurs before custom lowering, so the two may be used |
| 989 | together.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 990 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 991 | <p>Since LLVM does not yet compute liveness information, there is no means of |
| 992 | distinguishing an uninitialized stack root from an initialized one. Therefore, |
| 993 | this feature should be used by all GC plugins. It is enabled by default.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 994 | |
| 995 | </div> |
| 996 | |
| 997 | |
| 998 | <!-- ======================================================================= --> |
| 999 | <div class="doc_subsection"> |
| 1000 | <a name="custom">Custom lowering of intrinsics: <tt>CustomRoots</tt>, |
| 1001 | <tt>CustomReadBarriers</tt>, and <tt>CustomWriteBarriers</tt></a> |
| 1002 | </div> |
| 1003 | |
| 1004 | <div class="doc_text"> |
| 1005 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1006 | <p>For GCs which use barriers or unusual treatment of stack roots, these |
| 1007 | flags allow the collector to perform arbitrary transformations of the LLVM |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1008 | IR:</p> |
| 1009 | |
| 1010 | <blockquote><pre |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1011 | >class MyGC : public GCStrategy { |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1012 | public: |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1013 | MyGC() { |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1014 | CustomRoots = true; |
| 1015 | CustomReadBarriers = true; |
| 1016 | CustomWriteBarriers = true; |
| 1017 | } |
| 1018 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1019 | virtual bool initializeCustomLowering(Module &M); |
| 1020 | virtual bool performCustomLowering(Function &F); |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1021 | };</pre></blockquote> |
| 1022 | |
| 1023 | <p>If any of these flags are set, then LLVM suppresses its default lowering for |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1024 | the corresponding intrinsics and instead calls |
| 1025 | <tt>performCustomLowering</tt>.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1026 | |
| 1027 | <p>LLVM's default action for each intrinsic is as follows:</p> |
| 1028 | |
| 1029 | <ul> |
| 1030 | <li><tt>llvm.gcroot</tt>: Pass through to the code generator to generate a |
| 1031 | stack map.</li> |
| 1032 | <li><tt>llvm.gcread</tt>: Substitute a <tt>load</tt> instruction.</li> |
| 1033 | <li><tt>llvm.gcwrite</tt>: Substitute a <tt>store</tt> instruction.</li> |
| 1034 | </ul> |
| 1035 | |
| 1036 | <p>If <tt>CustomReadBarriers</tt> or <tt>CustomWriteBarriers</tt> are specified, |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1037 | then <tt>performCustomLowering</tt> <strong>must</strong> eliminate the |
| 1038 | corresponding barriers.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1039 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1040 | <p><tt>performCustomLowering</tt> must comply with the same restrictions as <a |
| 1041 | href="WritingAnLLVMPass.html#runOnFunction"><tt |
| 1042 | >FunctionPass::runOnFunction</tt></a>. |
| 1043 | Likewise, <tt>initializeCustomLowering</tt> has the same semantics as <a |
| 1044 | href="WritingAnLLVMPass.html#doInitialization_mod"><tt |
| 1045 | >Pass::doInitialization(Module&)</tt></a>.</p> |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1046 | |
| 1047 | <p>The following can be used as a template:</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1048 | |
| 1049 | <blockquote><pre |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1050 | >#include "llvm/Module.h" |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1051 | #include "llvm/IntrinsicInst.h" |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1052 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1053 | bool MyGC::initializeCustomLowering(Module &M) { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1056 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1057 | bool MyGC::performCustomLowering(Function &F) { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1058 | bool MadeChange = false; |
| 1059 | |
| 1060 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
Gordon Henriksen | 74f4ded | 2007-12-22 23:34:26 +0000 | [diff] [blame] | 1061 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) |
| 1062 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1063 | if (Function *F = CI->getCalledFunction()) |
| 1064 | switch (F->getIntrinsicID()) { |
| 1065 | case Intrinsic::gcwrite: |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1066 | // Handle llvm.gcwrite. |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1067 | CI->eraseFromParent(); |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1068 | MadeChange = true; |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | case Intrinsic::gcread: |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1071 | // Handle llvm.gcread. |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1072 | CI->eraseFromParent(); |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1073 | MadeChange = true; |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1074 | break; |
| 1075 | case Intrinsic::gcroot: |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1076 | // Handle llvm.gcroot. |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1077 | CI->eraseFromParent(); |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1078 | MadeChange = true; |
Gordon Henriksen | 0adede0 | 2007-12-22 23:32:32 +0000 | [diff] [blame] | 1079 | break; |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | return MadeChange; |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1083 | }</pre></blockquote> |
| 1084 | |
| 1085 | </div> |
| 1086 | |
| 1087 | |
| 1088 | <!-- ======================================================================= --> |
| 1089 | <div class="doc_subsection"> |
| 1090 | <a name="safe-points">Generating safe points: <tt>NeededSafePoints</tt></a> |
| 1091 | </div> |
| 1092 | |
| 1093 | <div class="doc_text"> |
| 1094 | |
| 1095 | <p>LLVM can compute four kinds of safe points:</p> |
| 1096 | |
| 1097 | <blockquote><pre |
| 1098 | >namespace GC { |
| 1099 | /// PointKind - The type of a collector-safe point. |
| 1100 | /// |
| 1101 | enum PointKind { |
| 1102 | Loop, //< Instr is a loop (backwards branch). |
| 1103 | Return, //< Instr is a return instruction. |
| 1104 | PreCall, //< Instr is a call instruction. |
| 1105 | PostCall //< Instr is the return address of a call. |
| 1106 | }; |
| 1107 | }</pre></blockquote> |
| 1108 | |
| 1109 | <p>A collector can request any combination of the four by setting the |
| 1110 | <tt>NeededSafePoints</tt> mask:</p> |
| 1111 | |
| 1112 | <blockquote><pre |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1113 | >MyGC::MyGC() { |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1114 | NeededSafePoints = 1 << GC::Loop |
| 1115 | | 1 << GC::Return |
| 1116 | | 1 << GC::PreCall |
| 1117 | | 1 << GC::PostCall; |
| 1118 | }</pre></blockquote> |
| 1119 | |
| 1120 | <p>It can then use the following routines to access safe points.</p> |
| 1121 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1122 | <blockquote><pre |
| 1123 | >for (iterator I = begin(), E = end(); I != E; ++I) { |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1124 | GCFunctionInfo *MD = *I; |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1125 | size_t PointCount = MD->size(); |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1126 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1127 | for (GCFunctionInfo::iterator PI = MD->begin(), |
| 1128 | PE = MD->end(); PI != PE; ++PI) { |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1129 | GC::PointKind PointKind = PI->Kind; |
| 1130 | unsigned PointNum = PI->Num; |
| 1131 | } |
| 1132 | } |
| 1133 | </pre></blockquote> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1134 | |
| 1135 | <p>Almost every collector requires <tt>PostCall</tt> safe points, since these |
| 1136 | correspond to the moments when the function is suspended during a call to a |
| 1137 | subroutine.</p> |
| 1138 | |
| 1139 | <p>Threaded programs generally require <tt>Loop</tt> safe points to guarantee |
| 1140 | that the application will reach a safe point within a bounded amount of time, |
| 1141 | even if it is executing a long-running loop which contains no function |
| 1142 | calls.</p> |
| 1143 | |
| 1144 | <p>Threaded collectors may also require <tt>Return</tt> and <tt>PreCall</tt> |
| 1145 | safe points to implement "stop the world" techniques using self-modifying code, |
| 1146 | where it is important that the program not exit the function without reaching a |
| 1147 | safe point (because only the topmost function has been patched).</p> |
| 1148 | |
| 1149 | </div> |
| 1150 | |
| 1151 | |
| 1152 | <!-- ======================================================================= --> |
| 1153 | <div class="doc_subsection"> |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1154 | <a name="assembly">Emitting assembly code: <tt>GCMetadataPrinter</tt></a> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1155 | </div> |
| 1156 | |
| 1157 | <div class="doc_text"> |
| 1158 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1159 | <p>LLVM allows a GC to print arbitrary assembly code before and after the rest |
| 1160 | of a module's assembly code. At the end of the module, the GC can print stack |
| 1161 | maps built by the code generator. (At the beginning, this information is not |
| 1162 | yet computed.)</p> |
| 1163 | |
| 1164 | <p>Since AsmWriter and CodeGen are separate components of LLVM, a separate |
| 1165 | abstract base class and registry is provided for printing assembly code, the |
Gordon Henriksen | c9c0b59 | 2009-03-02 03:47:20 +0000 | [diff] [blame] | 1166 | <tt>GCMetadaPrinter</tt> and <tt>GCMetadataPrinterRegistry</tt>. The AsmWriter |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1167 | will look for such a subclass if the <tt>GCStrategy</tt> sets |
| 1168 | <tt>UsesMetadata</tt>:</p> |
| 1169 | |
| 1170 | <blockquote><pre |
| 1171 | >MyGC::MyGC() { |
| 1172 | UsesMetadata = true; |
| 1173 | }</pre></blockquote> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1174 | |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1175 | <p>Note that LLVM does not currently have analogous APIs to support code |
| 1176 | generation in the JIT, nor using the object writers.</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1177 | |
| 1178 | <blockquote><pre |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1179 | >// lib/MyGC/MyGCPrinter.cpp - Example LLVM GC printer |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1180 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1181 | #include "llvm/CodeGen/GCMetadataPrinter.h" |
| 1182 | #include "llvm/Support/Compiler.h" |
| 1183 | |
| 1184 | using namespace llvm; |
| 1185 | |
| 1186 | namespace { |
| 1187 | class VISIBILITY_HIDDEN MyGCPrinter : public GCMetadataPrinter { |
| 1188 | public: |
| 1189 | virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP, |
| 1190 | const TargetAsmInfo &TAI); |
| 1191 | |
| 1192 | virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP, |
| 1193 | const TargetAsmInfo &TAI); |
| 1194 | }; |
| 1195 | |
| 1196 | GCMetadataPrinterRegistry::Add<MyGCPrinter> |
| 1197 | X("mygc", "My bespoke garbage collector."); |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1198 | }</pre></blockquote> |
| 1199 | |
| 1200 | <p>The collector should use <tt>AsmPrinter</tt> and <tt>TargetAsmInfo</tt> to |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1201 | print portable assembly code to the <tt>std::ostream</tt>. The collector itself |
| 1202 | contains the stack map for the entire module, and may access the |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1203 | <tt>GCFunctionInfo</tt> using its own <tt>begin()</tt> and <tt>end()</tt> |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1204 | methods. Here's a realistic example:</p> |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1205 | |
| 1206 | <blockquote><pre |
| 1207 | >#include "llvm/CodeGen/AsmPrinter.h" |
| 1208 | #include "llvm/Function.h" |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1209 | #include "llvm/Target/TargetMachine.h" |
| 1210 | #include "llvm/Target/TargetData.h" |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1211 | #include "llvm/Target/TargetAsmInfo.h" |
| 1212 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1213 | void MyGCPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP, |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1214 | const TargetAsmInfo &TAI) { |
| 1215 | // Nothing to do. |
| 1216 | } |
| 1217 | |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1218 | void MyGCPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP, |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1219 | const TargetAsmInfo &TAI) { |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1220 | // Set up for emitting addresses. |
| 1221 | const char *AddressDirective; |
| 1222 | int AddressAlignLog; |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1223 | if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) { |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1224 | AddressDirective = TAI.getData32bitsDirective(); |
| 1225 | AddressAlignLog = 2; |
| 1226 | } else { |
| 1227 | AddressDirective = TAI.getData64bitsDirective(); |
| 1228 | AddressAlignLog = 3; |
| 1229 | } |
| 1230 | |
| 1231 | // Put this in the data section. |
| 1232 | AP.SwitchToDataSection(TAI.getDataSection()); |
| 1233 | |
| 1234 | // For each function... |
Gordon Henriksen | ad93c4f | 2007-12-11 00:30:17 +0000 | [diff] [blame] | 1235 | for (iterator FI = begin(), FE = end(); FI != FE; ++FI) { |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1236 | GCFunctionInfo &MD = **FI; |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Emit this data structure: |
| 1239 | // |
| 1240 | // struct { |
| 1241 | // int32_t PointCount; |
| 1242 | // struct { |
| 1243 | // void *SafePointAddress; |
| 1244 | // int32_t LiveCount; |
| 1245 | // int32_t LiveOffsets[LiveCount]; |
| 1246 | // } Points[PointCount]; |
| 1247 | // } __gcmap_<FUNCTIONNAME>; |
| 1248 | |
| 1249 | // Align to address width. |
| 1250 | AP.EmitAlignment(AddressAlignLog); |
| 1251 | |
| 1252 | // Emit the symbol by which the stack map can be found. |
| 1253 | std::string Symbol; |
| 1254 | Symbol += TAI.getGlobalPrefix(); |
| 1255 | Symbol += "__gcmap_"; |
| 1256 | Symbol += MD.getFunction().getName(); |
| 1257 | if (const char *GlobalDirective = TAI.getGlobalDirective()) |
| 1258 | OS << GlobalDirective << Symbol << "\n"; |
| 1259 | OS << TAI.getGlobalPrefix() << Symbol << ":\n"; |
| 1260 | |
| 1261 | // Emit PointCount. |
| 1262 | AP.EmitInt32(MD.size()); |
| 1263 | AP.EOL("safe point count"); |
| 1264 | |
| 1265 | // And each safe point... |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1266 | for (GCFunctionInfo::iterator PI = MD.begin(), |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1267 | PE = MD.end(); PI != PE; ++PI) { |
| 1268 | // Align to address width. |
| 1269 | AP.EmitAlignment(AddressAlignLog); |
| 1270 | |
| 1271 | // Emit the address of the safe point. |
| 1272 | OS << AddressDirective |
| 1273 | << TAI.getPrivateGlobalPrefix() << "label" << PI->Num; |
| 1274 | AP.EOL("safe point address"); |
| 1275 | |
| 1276 | // Emit the stack frame size. |
| 1277 | AP.EmitInt32(MD.getFrameSize()); |
| 1278 | AP.EOL("stack frame size"); |
| 1279 | |
| 1280 | // Emit the number of live roots in the function. |
| 1281 | AP.EmitInt32(MD.live_size(PI)); |
| 1282 | AP.EOL("live root count"); |
| 1283 | |
| 1284 | // And for each live root... |
Gordon Henriksen | 01571ef | 2008-08-24 03:18:23 +0000 | [diff] [blame] | 1285 | for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI), |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1286 | LE = MD.live_end(PI); |
| 1287 | LI != LE; ++LI) { |
| 1288 | // Print its offset within the stack frame. |
| 1289 | AP.EmitInt32(LI->StackOffset); |
| 1290 | AP.EOL("stack offset"); |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | </pre></blockquote> |
| 1296 | |
| 1297 | </div> |
| 1298 | |
| 1299 | |
| 1300 | <!-- *********************************************************************** --> |
| 1301 | <div class="doc_section"> |
Chris Lattner | 9b2a184 | 2004-05-27 05:52:10 +0000 | [diff] [blame] | 1302 | <a name="references">References</a> |
| 1303 | </div> |
| 1304 | <!-- *********************************************************************** --> |
| 1305 | |
| 1306 | <div class="doc_text"> |
| 1307 | |
| 1308 | <p><a name="appel89">[Appel89]</a> Runtime Tags Aren't Necessary. Andrew |
| 1309 | W. Appel. Lisp and Symbolic Computation 19(7):703-705, July 1989.</p> |
| 1310 | |
| 1311 | <p><a name="goldberg91">[Goldberg91]</a> Tag-free garbage collection for |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1312 | strongly typed programming languages. Benjamin Goldberg. ACM SIGPLAN |
Chris Lattner | 9b2a184 | 2004-05-27 05:52:10 +0000 | [diff] [blame] | 1313 | PLDI'91.</p> |
| 1314 | |
| 1315 | <p><a name="tolmach94">[Tolmach94]</a> Tag-free garbage collection using |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1316 | explicit type parameters. Andrew Tolmach. Proceedings of the 1994 ACM |
Chris Lattner | 9b2a184 | 2004-05-27 05:52:10 +0000 | [diff] [blame] | 1317 | conference on LISP and functional programming.</p> |
| 1318 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1319 | <p><a name="henderson02">[Henderson2002]</a> <a |
| 1320 | href="http://citeseer.ist.psu.edu/henderson02accurate.html"> |
| 1321 | Accurate Garbage Collection in an Uncooperative Environment</a>. |
| 1322 | Fergus Henderson. International Symposium on Memory Management 2002.</p> |
| 1323 | |
Chris Lattner | 9b2a184 | 2004-05-27 05:52:10 +0000 | [diff] [blame] | 1324 | </div> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 1325 | |
Gordon Henriksen | 326e24f | 2007-09-27 19:31:36 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 1327 | <!-- *********************************************************************** --> |
| 1328 | |
| 1329 | <hr> |
| 1330 | <address> |
| 1331 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img |
Misha Brukman | 4440870 | 2008-12-11 17:34:48 +0000 | [diff] [blame] | 1332 | src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 1333 | <a href="http://validator.w3.org/check/referer"><img |
Misha Brukman | 4440870 | 2008-12-11 17:34:48 +0000 | [diff] [blame] | 1334 | src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 1335 | |
| 1336 | <a href="mailto:sabre@nondot.org">Chris Lattner</a><br> |
Reid Spencer | 05fe4b0 | 2006-03-14 05:39:39 +0000 | [diff] [blame] | 1337 | <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br> |
Chris Lattner | 0d8c2db | 2004-05-23 21:02:20 +0000 | [diff] [blame] | 1338 | Last modified: $Date$ |
| 1339 | </address> |
| 1340 | |
| 1341 | </body> |
| 1342 | </html> |