Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 1 | ===================================== |
| 2 | Garbage Collection Safepoints in LLVM |
| 3 | ===================================== |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | :depth: 2 |
| 8 | |
| 9 | Status |
| 10 | ======= |
| 11 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 12 | This document describes a set of experimental extensions to LLVM. Use |
| 13 | with caution. Because the intrinsics have experimental status, |
| 14 | compatibility across LLVM releases is not guaranteed. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 15 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 16 | LLVM currently supports an alternate mechanism for conservative |
Philip Reames | e0dd0f2 | 2015-02-25 00:18:04 +0000 | [diff] [blame^] | 17 | garbage collection support using the ``gcroot`` intrinsic. The mechanism |
| 18 | described here shares little in common with the alternate ``gcroot`` |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 19 | implementation and it is hoped that this mechanism will eventually |
| 20 | replace the gc_root mechanism. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 21 | |
| 22 | Overview |
| 23 | ======== |
| 24 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 25 | To collect dead objects, garbage collectors must be able to identify |
| 26 | any references to objects contained within executing code, and, |
| 27 | depending on the collector, potentially update them. The collector |
| 28 | does not need this information at all points in code - that would make |
| 29 | the problem much harder - but only at well-defined points in the |
| 30 | execution known as 'safepoints' For most collectors, it is sufficient |
| 31 | to track at least one copy of each unique pointer value. However, for |
| 32 | a collector which wishes to relocate objects directly reachable from |
| 33 | running code, a higher standard is required. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 34 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 35 | One additional challenge is that the compiler may compute intermediate |
| 36 | results ("derived pointers") which point outside of the allocation or |
| 37 | even into the middle of another allocation. The eventual use of this |
| 38 | intermediate value must yield an address within the bounds of the |
| 39 | allocation, but such "exterior derived pointers" may be visible to the |
| 40 | collector. Given this, a garbage collector can not safely rely on the |
| 41 | runtime value of an address to indicate the object it is associated |
| 42 | with. If the garbage collector wishes to move any object, the |
| 43 | compiler must provide a mapping, for each pointer, to an indication of |
| 44 | its allocation. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 45 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 46 | To simplify the interaction between a collector and the compiled code, |
| 47 | most garbage collectors are organized in terms of three abstractions: |
| 48 | load barriers, store barriers, and safepoints. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 49 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 50 | #. A load barrier is a bit of code executed immediately after the |
| 51 | machine load instruction, but before any use of the value loaded. |
| 52 | Depending on the collector, such a barrier may be needed for all |
| 53 | loads, merely loads of a particular type (in the original source |
| 54 | language), or none at all. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 55 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 56 | #. Analogously, a store barrier is a code fragement that runs |
| 57 | immediately before the machine store instruction, but after the |
| 58 | computation of the value stored. The most common use of a store |
| 59 | barrier is to update a 'card table' in a generational garbage |
| 60 | collector. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 61 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 62 | #. A safepoint is a location at which pointers visible to the compiled |
| 63 | code (i.e. currently in registers or on the stack) are allowed to |
| 64 | change. After the safepoint completes, the actual pointer value |
| 65 | may differ, but the 'object' (as seen by the source language) |
| 66 | pointed to will not. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 67 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 68 | Note that the term 'safepoint' is somewhat overloaded. It refers to |
| 69 | both the location at which the machine state is parsable and the |
| 70 | coordination protocol involved in bring application threads to a |
| 71 | point at which the collector can safely use that information. The |
| 72 | term "statepoint" as used in this document refers exclusively to the |
| 73 | former. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 74 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 75 | This document focuses on the last item - compiler support for |
| 76 | safepoints in generated code. We will assume that an outside |
| 77 | mechanism has decided where to place safepoints. From our |
| 78 | perspective, all safepoints will be function calls. To support |
| 79 | relocation of objects directly reachable from values in compiled code, |
| 80 | the collector must be able to: |
| 81 | |
| 82 | #. identify every copy of a pointer (including copies introduced by |
| 83 | the compiler itself) at the safepoint, |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 84 | #. identify which object each pointer relates to, and |
| 85 | #. potentially update each of those copies. |
| 86 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 87 | This document describes the mechanism by which an LLVM based compiler |
| 88 | can provide this information to a language runtime/collector, and |
| 89 | ensure that all pointers can be read and updated if desired. The |
| 90 | heart of the approach is to construct (or rewrite) the IR in a manner |
| 91 | where the possible updates performed by the garbage collector are |
| 92 | explicitly visible in the IR. Doing so requires that we: |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 93 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 94 | #. create a new SSA value for each potentially relocated pointer, and |
| 95 | ensure that no uses of the original (non relocated) value is |
| 96 | reachable after the safepoint, |
| 97 | #. specify the relocation in a way which is opaque to the compiler to |
| 98 | ensure that the optimizer can not introduce new uses of an |
| 99 | unrelocated value after a statepoint. This prevents the optimizer |
| 100 | from performing unsound optimizations. |
| 101 | #. recording a mapping of live pointers (and the allocation they're |
| 102 | associated with) for each statepoint. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 103 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 104 | At the most abstract level, inserting a safepoint can be thought of as |
| 105 | replacing a call instruction with a call to a multiple return value |
| 106 | function which both calls the original target of the call, returns |
| 107 | it's result, and returns updated values for any live pointers to |
| 108 | garbage collected objects. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 109 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 110 | Note that the task of identifying all live pointers to garbage |
| 111 | collected values, transforming the IR to expose a pointer giving the |
| 112 | base object for every such live pointer, and inserting all the |
| 113 | intrinsics correctly is explicitly out of scope for this document. |
| 114 | The recommended approach is described in the section of Late |
| 115 | Safepoint Placement below. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 116 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 117 | This abstract function call is concretely represented by a sequence of |
| 118 | intrinsic calls known as a 'statepoint sequence'. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | Let's consider a simple call in LLVM IR: |
| 122 | todo |
| 123 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 124 | Depending on our language we may need to allow a safepoint during the |
| 125 | execution of the function called from this site. If so, we need to |
| 126 | let the collector update local values in the current frame. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 127 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 128 | Let's say we need to relocate SSA values 'a', 'b', and 'c' at this |
| 129 | safepoint. To represent this, we would generate the statepoint |
| 130 | sequence: |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 131 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 132 | todo |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 133 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 134 | Ideally, this sequence would have been represented as a M argument, N |
| 135 | return value function (where M is the number of values being |
| 136 | relocated + the original call arguments and N is the original return |
| 137 | value + each relocated value), but LLVM does not easily support such a |
| 138 | representation. |
| 139 | |
| 140 | Instead, the statepoint intrinsic marks the actual site of the |
| 141 | safepoint or statepoint. The statepoint returns a token value (which |
| 142 | exists only at compile time). To get back the original return value |
| 143 | of the call, we use the 'gc.result' intrinsic. To get the relocation |
| 144 | of each pointer in turn, we use the 'gc.relocate' intrinsic with the |
| 145 | appropriate index. Note that both the gc.relocate and gc.result are |
| 146 | tied to the statepoint. The combination forms a "statepoint sequence" |
| 147 | and represents the entitety of a parseable call or 'statepoint'. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 148 | |
| 149 | When lowered, this example would generate the following x86 assembly:: |
| 150 | put assembly here |
| 151 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 152 | Each of the potentially relocated values has been spilled to the |
| 153 | stack, and a record of that location has been recorded to the |
| 154 | :ref:`Stack Map section <stackmap-section>`. If the garbage collector |
| 155 | needs to update any of these pointers during the call, it knows |
| 156 | exactly what to change. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 157 | |
| 158 | Intrinsics |
| 159 | =========== |
| 160 | |
Philip Reames | c012728 | 2015-02-24 23:57:26 +0000 | [diff] [blame] | 161 | 'llvm.experimental.gc.statepoint' Intrinsic |
| 162 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 163 | |
| 164 | Syntax: |
| 165 | """"""" |
| 166 | |
| 167 | :: |
| 168 | |
| 169 | declare i32 |
Philip Reames | c012728 | 2015-02-24 23:57:26 +0000 | [diff] [blame] | 170 | @llvm.experimental.gc.statepoint(func_type <target>, |
| 171 | i64 <#call args>. i64 <unused>, |
| 172 | ... (call parameters), |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 173 | i64 <# deopt args>, ... (deopt parameters), |
| 174 | ... (gc parameters)) |
| 175 | |
| 176 | Overview: |
| 177 | """"""""" |
| 178 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 179 | The statepoint intrinsic represents a call which is parse-able by the |
| 180 | runtime. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 181 | |
| 182 | Operands: |
| 183 | """"""""" |
| 184 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 185 | The 'target' operand is the function actually being called. The |
| 186 | target can be specified as either a symbolic LLVM function, or as an |
| 187 | arbitrary Value of appropriate function type. Note that the function |
| 188 | type must match the signature of the callee and the types of the 'call |
| 189 | parameters' arguments. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 190 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 191 | The '#call args' operand is the number of arguments to the actual |
| 192 | call. It must exactly match the number of arguments passed in the |
| 193 | 'call parameters' variable length section. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 194 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 195 | The 'unused' operand is unused and likely to be removed. Please do |
| 196 | not use. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 197 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 198 | The 'call parameters' arguments are simply the arguments which need to |
| 199 | be passed to the call target. They will be lowered according to the |
| 200 | specified calling convention and otherwise handled like a normal call |
| 201 | instruction. The number of arguments must exactly match what is |
| 202 | specified in '# call args'. The types must match the signature of |
| 203 | 'target'. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 204 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 205 | The 'deopt parameters' arguments contain an arbitrary list of Values |
| 206 | which is meaningful to the runtime. The runtime may read any of these |
| 207 | values, but is assumed not to modify them. If the garbage collector |
| 208 | might need to modify one of these values, it must also be listed in |
| 209 | the 'gc pointer' argument list. The '# deopt args' field indicates |
| 210 | how many operands are to be interpreted as 'deopt parameters'. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 211 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 212 | The 'gc parameters' arguments contain every pointer to a garbage |
| 213 | collector object which potentially needs to be updated by the garbage |
| 214 | collector. Note that the argument list must explicitly contain a base |
| 215 | pointer for every derived pointer listed. The order of arguments is |
| 216 | unimportant. Unlike the other variable length parameter sets, this |
| 217 | list is not length prefixed. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 218 | |
| 219 | Semantics: |
| 220 | """""""""" |
| 221 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 222 | A statepoint is assumed to read and write all memory. As a result, |
| 223 | memory operations can not be reordered past a statepoint. It is |
| 224 | illegal to mark a statepoint as being either 'readonly' or 'readnone'. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 225 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 226 | Note that legal IR can not perform any memory operation on a 'gc |
| 227 | pointer' argument of the statepoint in a location statically reachable |
| 228 | from the statepoint. Instead, the explicitly relocated value (from a |
| 229 | ''gc.relocate'') must be used. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 230 | |
Philip Reames | c012728 | 2015-02-24 23:57:26 +0000 | [diff] [blame] | 231 | 'llvm.experimental.gc.result' Intrinsic |
| 232 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 233 | |
| 234 | Syntax: |
| 235 | """"""" |
| 236 | |
| 237 | :: |
| 238 | |
| 239 | declare type* |
Philip Reames | c012728 | 2015-02-24 23:57:26 +0000 | [diff] [blame] | 240 | @llvm.experimental.gc.result(i32 %statepoint_token) |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 241 | |
| 242 | Overview: |
| 243 | """"""""" |
| 244 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 245 | '''gc.result''' extracts the result of the original call instruction |
| 246 | which was replaced by the '''gc.statepoint'''. The '''gc.result''' |
| 247 | intrinsic is actually a family of three intrinsics due to an |
| 248 | implementation limitation. Other than the type of the return value, |
| 249 | the semantics are the same. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 250 | |
| 251 | Operands: |
| 252 | """"""""" |
| 253 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 254 | The first and only argument is the '''gc.statepoint''' which starts |
| 255 | the safepoint sequence of which this '''gc.result'' is a part. |
| 256 | Despite the typing of this as a generic i32, *only* the value defined |
| 257 | by a '''gc.statepoint''' is legal here. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 258 | |
| 259 | Semantics: |
| 260 | """""""""" |
| 261 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 262 | The ''gc.result'' represents the return value of the call target of |
| 263 | the ''statepoint''. The type of the ''gc.result'' must exactly match |
| 264 | the type of the target. If the call target returns void, there will |
| 265 | be no ''gc.result''. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 266 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 267 | A ''gc.result'' is modeled as a 'readnone' pure function. It has no |
| 268 | side effects since it is just a projection of the return value of the |
| 269 | previous call represented by the ''gc.statepoint''. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 270 | |
Philip Reames | c012728 | 2015-02-24 23:57:26 +0000 | [diff] [blame] | 271 | 'llvm.experimental.gc.relocate' Intrinsic |
| 272 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 273 | |
| 274 | Syntax: |
| 275 | """"""" |
| 276 | |
| 277 | :: |
| 278 | |
Philip Reames | c012728 | 2015-02-24 23:57:26 +0000 | [diff] [blame] | 279 | declare <pointer type> |
| 280 | @llvm.experimental.gc.relocate(i32 %statepoint_token, |
| 281 | i32 %base_offset, |
| 282 | i32 %pointer_offset) |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 283 | |
| 284 | Overview: |
| 285 | """"""""" |
| 286 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 287 | A ''gc.relocate'' returns the potentially relocated value of a pointer |
| 288 | at the safepoint. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 289 | |
| 290 | Operands: |
| 291 | """"""""" |
| 292 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 293 | The first argument is the '''gc.statepoint''' which starts the |
| 294 | safepoint sequence of which this '''gc.relocation'' is a part. |
| 295 | Despite the typing of this as a generic i32, *only* the value defined |
| 296 | by a '''gc.statepoint''' is legal here. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 297 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 298 | The second argument is an index into the statepoints list of arguments |
| 299 | which specifies the base pointer for the pointer being relocated. |
| 300 | This index must land within the 'gc parameter' section of the |
| 301 | statepoint's argument list. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 302 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 303 | The third argument is an index into the statepoint's list of arguments |
| 304 | which specify the (potentially) derived pointer being relocated. It |
| 305 | is legal for this index to be the same as the second argument |
| 306 | if-and-only-if a base pointer is being relocated. This index must land |
| 307 | within the 'gc parameter' section of the statepoint's argument list. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 308 | |
| 309 | Semantics: |
| 310 | """""""""" |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 311 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 312 | The return value of ''gc.relocate'' is the potentially relocated value |
| 313 | of the pointer specified by it's arguments. It is unspecified how the |
| 314 | value of the returned pointer relates to the argument to the |
| 315 | ''gc.statepoint'' other than that a) it points to the same source |
| 316 | language object with the same offset, and b) the 'based-on' |
| 317 | relationship of the newly relocated pointers is a projection of the |
| 318 | unrelocated pointers. In particular, the integer value of the pointer |
| 319 | returned is unspecified. |
| 320 | |
| 321 | A ''gc.relocate'' is modeled as a 'readnone' pure function. It has no |
| 322 | side effects since it is just a way to extract information about work |
| 323 | done during the actual call modeled by the ''gc.statepoint''. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 324 | |
| 325 | |
Philip Reames | ce5ff37 | 2014-12-04 00:45:23 +0000 | [diff] [blame] | 326 | Stack Map Format |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 327 | ================ |
| 328 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 329 | Locations for each pointer value which may need read and/or updated by |
| 330 | the runtime or collector are provided via the :ref:`Stack Map format |
| 331 | <stackmap-format>` specified in the PatchPoint documentation. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 332 | |
| 333 | Each statepoint generates the following Locations: |
| 334 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 335 | * Constant which describes number of following deopt *Locations* (not |
| 336 | operands) |
| 337 | * Variable number of Locations, one for each deopt parameter listed in |
| 338 | the IR statepoint (same number as described by previous Constant) |
| 339 | * Variable number of Locations pairs, one pair for each unique pointer |
| 340 | which needs relocated. The first Location in each pair describes |
| 341 | the base pointer for the object. The second is the derived pointer |
| 342 | actually being relocated. It is guaranteed that the base pointer |
| 343 | must also appear explicitly as a relocation pair if used after the |
| 344 | statepoint. There may be fewer pairs then gc parameters in the IR |
| 345 | statepoint. Each *unique* pair will occur at least once; duplicates |
| 346 | are possible. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 347 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 348 | Note that the Locations used in each section may describe the same |
| 349 | physical location. e.g. A stack slot may appear as a deopt location, |
| 350 | a gc base pointer, and a gc derived pointer. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 351 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 352 | The ID field of the 'StkMapRecord' for a statepoint is meaningless and |
| 353 | it's value is explicitly unspecified. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 354 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 355 | The LiveOut section of the StkMapRecord will be empty for a statepoint |
| 356 | record. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 357 | |
| 358 | Safepoint Semantics & Verification |
| 359 | ================================== |
| 360 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 361 | The fundamental correctness property for the compiled code's |
| 362 | correctness w.r.t. the garbage collector is a dynamic one. It must be |
| 363 | the case that there is no dynamic trace such that a operation |
| 364 | involving a potentially relocated pointer is observably-after a |
| 365 | safepoint which could relocate it. 'observably-after' is this usage |
| 366 | means that an outside observer could observe this sequence of events |
| 367 | in a way which precludes the operation being performed before the |
| 368 | safepoint. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 369 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 370 | To understand why this 'observable-after' property is required, |
| 371 | consider a null comparison performed on the original copy of a |
| 372 | relocated pointer. Assuming that control flow follows the safepoint, |
| 373 | there is no way to observe externally whether the null comparison is |
| 374 | performed before or after the safepoint. (Remember, the original |
| 375 | Value is unmodified by the safepoint.) The compiler is free to make |
| 376 | either scheduling choice. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 377 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 378 | The actual correctness property implemented is slightly stronger than |
| 379 | this. We require that there be no *static path* on which a |
| 380 | potentially relocated pointer is 'observably-after' it may have been |
| 381 | relocated. This is slightly stronger than is strictly necessary (and |
| 382 | thus may disallow some otherwise valid programs), but greatly |
| 383 | simplifies reasoning about correctness of the compiled code. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 384 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 385 | By construction, this property will be upheld by the optimizer if |
| 386 | correctly established in the source IR. This is a key invariant of |
| 387 | the design. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 388 | |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 389 | The existing IR Verifier pass has been extended to check most of the |
| 390 | local restrictions on the intrinsics mentioned in their respective |
| 391 | documentation. The current implementation in LLVM does not check the |
| 392 | key relocation invariant, but this is ongoing work on developing such |
| 393 | a verifier. Please ask on llvmdev if you're interested in |
| 394 | experimenting with the current version. |
Philip Reames | f612322 | 2014-12-02 19:37:00 +0000 | [diff] [blame] | 395 | |
Philip Reames | 8333152 | 2014-12-04 18:33:28 +0000 | [diff] [blame] | 396 | Bugs and Enhancements |
| 397 | ===================== |
Philip Reames | dfc238b | 2015-01-02 19:46:49 +0000 | [diff] [blame] | 398 | |
| 399 | Currently known bugs and enhancements under consideration can be |
| 400 | tracked by performing a `bugzilla search |
| 401 | <http://llvm.org/bugs/buglist.cgi?cmdtype=runnamed&namedcmd=Statepoint%20Bugs&list_id=64342>`_ |
| 402 | for [Statepoint] in the summary field. When filing new bugs, please |
| 403 | use this tag so that interested parties see the newly filed bug. As |
| 404 | with most LLVM features, design discussions take place on `llvmdev |
| 405 | <http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>`_, and patches |
| 406 | should be sent to `llvm-commits |
| 407 | <http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits>`_ for review. |
Philip Reames | 8333152 | 2014-12-04 18:33:28 +0000 | [diff] [blame] | 408 | |