blob: 9d19d03414d7eae2687951c364ce82b08ed6090b [file] [log] [blame]
Philip Reamesf6123222014-12-02 19:37:00 +00001=====================================
2Garbage Collection Safepoints in LLVM
3=====================================
4
5.. contents::
6 :local:
7 :depth: 2
8
9Status
10=======
11
Philip Reamesdfc238b2015-01-02 19:46:49 +000012This document describes a set of experimental extensions to LLVM. Use
13with caution. Because the intrinsics have experimental status,
14compatibility across LLVM releases is not guaranteed.
Philip Reamesf6123222014-12-02 19:37:00 +000015
Philip Reamesdfc238b2015-01-02 19:46:49 +000016LLVM currently supports an alternate mechanism for conservative
Philip Reamese0dd0f22015-02-25 00:18:04 +000017garbage collection support using the ``gcroot`` intrinsic. The mechanism
18described here shares little in common with the alternate ``gcroot``
Philip Reamesdfc238b2015-01-02 19:46:49 +000019implementation and it is hoped that this mechanism will eventually
20replace the gc_root mechanism.
Philip Reamesf6123222014-12-02 19:37:00 +000021
22Overview
23========
24
Philip Reamesdfc238b2015-01-02 19:46:49 +000025To collect dead objects, garbage collectors must be able to identify
26any references to objects contained within executing code, and,
27depending on the collector, potentially update them. The collector
28does not need this information at all points in code - that would make
29the problem much harder - but only at well-defined points in the
30execution known as 'safepoints' For most collectors, it is sufficient
31to track at least one copy of each unique pointer value. However, for
32a collector which wishes to relocate objects directly reachable from
33running code, a higher standard is required.
Philip Reamesf6123222014-12-02 19:37:00 +000034
Philip Reamesdfc238b2015-01-02 19:46:49 +000035One additional challenge is that the compiler may compute intermediate
36results ("derived pointers") which point outside of the allocation or
37even into the middle of another allocation. The eventual use of this
38intermediate value must yield an address within the bounds of the
39allocation, but such "exterior derived pointers" may be visible to the
40collector. Given this, a garbage collector can not safely rely on the
41runtime value of an address to indicate the object it is associated
42with. If the garbage collector wishes to move any object, the
43compiler must provide a mapping, for each pointer, to an indication of
44its allocation.
Philip Reamesf6123222014-12-02 19:37:00 +000045
Philip Reamesdfc238b2015-01-02 19:46:49 +000046To simplify the interaction between a collector and the compiled code,
47most garbage collectors are organized in terms of three abstractions:
48load barriers, store barriers, and safepoints.
Philip Reamesf6123222014-12-02 19:37:00 +000049
Philip Reamesdfc238b2015-01-02 19:46:49 +000050#. 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 Reamesf6123222014-12-02 19:37:00 +000055
Philip Reamesdfc238b2015-01-02 19:46:49 +000056#. 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 Reamesf6123222014-12-02 19:37:00 +000061
Philip Reamesdfc238b2015-01-02 19:46:49 +000062#. 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 Reamesf6123222014-12-02 19:37:00 +000067
Philip Reamesdfc238b2015-01-02 19:46:49 +000068 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 Reamesf6123222014-12-02 19:37:00 +000074
Philip Reamesdfc238b2015-01-02 19:46:49 +000075This document focuses on the last item - compiler support for
76safepoints in generated code. We will assume that an outside
77mechanism has decided where to place safepoints. From our
78perspective, all safepoints will be function calls. To support
79relocation of objects directly reachable from values in compiled code,
80the 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 Reamesf6123222014-12-02 19:37:00 +000084#. identify which object each pointer relates to, and
85#. potentially update each of those copies.
86
Philip Reamesdfc238b2015-01-02 19:46:49 +000087This document describes the mechanism by which an LLVM based compiler
88can provide this information to a language runtime/collector, and
89ensure that all pointers can be read and updated if desired. The
90heart of the approach is to construct (or rewrite) the IR in a manner
91where the possible updates performed by the garbage collector are
92explicitly visible in the IR. Doing so requires that we:
Philip Reamesf6123222014-12-02 19:37:00 +000093
Philip Reamesdfc238b2015-01-02 19:46:49 +000094#. 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 Reamesf6123222014-12-02 19:37:00 +0000103
Philip Reamesdfc238b2015-01-02 19:46:49 +0000104At the most abstract level, inserting a safepoint can be thought of as
105replacing a call instruction with a call to a multiple return value
106function which both calls the original target of the call, returns
107it's result, and returns updated values for any live pointers to
108garbage collected objects.
Philip Reamesf6123222014-12-02 19:37:00 +0000109
Philip Reamesdfc238b2015-01-02 19:46:49 +0000110 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 Reamesf6123222014-12-02 19:37:00 +0000116
Philip Reamesdfc238b2015-01-02 19:46:49 +0000117This abstract function call is concretely represented by a sequence of
118intrinsic calls known as a 'statepoint sequence'.
Philip Reamesf6123222014-12-02 19:37:00 +0000119
120
121Let's consider a simple call in LLVM IR:
122 todo
123
Philip Reamesdfc238b2015-01-02 19:46:49 +0000124Depending on our language we may need to allow a safepoint during the
125execution of the function called from this site. If so, we need to
126let the collector update local values in the current frame.
Philip Reamesf6123222014-12-02 19:37:00 +0000127
Philip Reamesdfc238b2015-01-02 19:46:49 +0000128Let's say we need to relocate SSA values 'a', 'b', and 'c' at this
129safepoint. To represent this, we would generate the statepoint
130sequence:
Philip Reamesf6123222014-12-02 19:37:00 +0000131
Philip Reamesdfc238b2015-01-02 19:46:49 +0000132 todo
Philip Reamesf6123222014-12-02 19:37:00 +0000133
Philip Reamesdfc238b2015-01-02 19:46:49 +0000134Ideally, this sequence would have been represented as a M argument, N
135return value function (where M is the number of values being
136relocated + the original call arguments and N is the original return
137value + each relocated value), but LLVM does not easily support such a
138representation.
139
140Instead, the statepoint intrinsic marks the actual site of the
141safepoint or statepoint. The statepoint returns a token value (which
142exists only at compile time). To get back the original return value
143of the call, we use the 'gc.result' intrinsic. To get the relocation
144of each pointer in turn, we use the 'gc.relocate' intrinsic with the
145appropriate index. Note that both the gc.relocate and gc.result are
146tied to the statepoint. The combination forms a "statepoint sequence"
147and represents the entitety of a parseable call or 'statepoint'.
Philip Reamesf6123222014-12-02 19:37:00 +0000148
149When lowered, this example would generate the following x86 assembly::
150 put assembly here
151
Philip Reamesdfc238b2015-01-02 19:46:49 +0000152Each of the potentially relocated values has been spilled to the
153stack, and a record of that location has been recorded to the
154:ref:`Stack Map section <stackmap-section>`. If the garbage collector
155needs to update any of these pointers during the call, it knows
156exactly what to change.
Philip Reamesf6123222014-12-02 19:37:00 +0000157
158Intrinsics
159===========
160
Philip Reamesc0127282015-02-24 23:57:26 +0000161'llvm.experimental.gc.statepoint' Intrinsic
162^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000163
164Syntax:
165"""""""
166
167::
168
169 declare i32
Philip Reamesc0127282015-02-24 23:57:26 +0000170 @llvm.experimental.gc.statepoint(func_type <target>,
171 i64 <#call args>. i64 <unused>,
172 ... (call parameters),
Philip Reamesf6123222014-12-02 19:37:00 +0000173 i64 <# deopt args>, ... (deopt parameters),
174 ... (gc parameters))
175
176Overview:
177"""""""""
178
Philip Reamesdfc238b2015-01-02 19:46:49 +0000179The statepoint intrinsic represents a call which is parse-able by the
180runtime.
Philip Reamesf6123222014-12-02 19:37:00 +0000181
182Operands:
183"""""""""
184
Philip Reamesdfc238b2015-01-02 19:46:49 +0000185The 'target' operand is the function actually being called. The
186target can be specified as either a symbolic LLVM function, or as an
187arbitrary Value of appropriate function type. Note that the function
188type must match the signature of the callee and the types of the 'call
189parameters' arguments.
Philip Reamesf6123222014-12-02 19:37:00 +0000190
Philip Reamesdfc238b2015-01-02 19:46:49 +0000191The '#call args' operand is the number of arguments to the actual
192call. It must exactly match the number of arguments passed in the
193'call parameters' variable length section.
Philip Reamesf6123222014-12-02 19:37:00 +0000194
Philip Reamesdfc238b2015-01-02 19:46:49 +0000195The 'unused' operand is unused and likely to be removed. Please do
196not use.
Philip Reamesf6123222014-12-02 19:37:00 +0000197
Philip Reamesdfc238b2015-01-02 19:46:49 +0000198The 'call parameters' arguments are simply the arguments which need to
199be passed to the call target. They will be lowered according to the
200specified calling convention and otherwise handled like a normal call
201instruction. The number of arguments must exactly match what is
202specified in '# call args'. The types must match the signature of
203'target'.
Philip Reamesf6123222014-12-02 19:37:00 +0000204
Philip Reamesdfc238b2015-01-02 19:46:49 +0000205The 'deopt parameters' arguments contain an arbitrary list of Values
206which is meaningful to the runtime. The runtime may read any of these
207values, but is assumed not to modify them. If the garbage collector
208might need to modify one of these values, it must also be listed in
209the 'gc pointer' argument list. The '# deopt args' field indicates
210how many operands are to be interpreted as 'deopt parameters'.
Philip Reamesf6123222014-12-02 19:37:00 +0000211
Philip Reamesdfc238b2015-01-02 19:46:49 +0000212The 'gc parameters' arguments contain every pointer to a garbage
213collector object which potentially needs to be updated by the garbage
214collector. Note that the argument list must explicitly contain a base
215pointer for every derived pointer listed. The order of arguments is
216unimportant. Unlike the other variable length parameter sets, this
217list is not length prefixed.
Philip Reamesf6123222014-12-02 19:37:00 +0000218
219Semantics:
220""""""""""
221
Philip Reamesdfc238b2015-01-02 19:46:49 +0000222A statepoint is assumed to read and write all memory. As a result,
223memory operations can not be reordered past a statepoint. It is
224illegal to mark a statepoint as being either 'readonly' or 'readnone'.
Philip Reamesf6123222014-12-02 19:37:00 +0000225
Philip Reamesdfc238b2015-01-02 19:46:49 +0000226Note that legal IR can not perform any memory operation on a 'gc
227pointer' argument of the statepoint in a location statically reachable
228from the statepoint. Instead, the explicitly relocated value (from a
229''gc.relocate'') must be used.
Philip Reamesf6123222014-12-02 19:37:00 +0000230
Philip Reamesc0127282015-02-24 23:57:26 +0000231'llvm.experimental.gc.result' Intrinsic
232^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000233
234Syntax:
235"""""""
236
237::
238
239 declare type*
Philip Reamesc0127282015-02-24 23:57:26 +0000240 @llvm.experimental.gc.result(i32 %statepoint_token)
Philip Reamesf6123222014-12-02 19:37:00 +0000241
242Overview:
243"""""""""
244
Philip Reamesdfc238b2015-01-02 19:46:49 +0000245'''gc.result''' extracts the result of the original call instruction
246which was replaced by the '''gc.statepoint'''. The '''gc.result'''
247intrinsic is actually a family of three intrinsics due to an
248implementation limitation. Other than the type of the return value,
249the semantics are the same.
Philip Reamesf6123222014-12-02 19:37:00 +0000250
251Operands:
252"""""""""
253
Philip Reamesdfc238b2015-01-02 19:46:49 +0000254The first and only argument is the '''gc.statepoint''' which starts
255the safepoint sequence of which this '''gc.result'' is a part.
256Despite the typing of this as a generic i32, *only* the value defined
257by a '''gc.statepoint''' is legal here.
Philip Reamesf6123222014-12-02 19:37:00 +0000258
259Semantics:
260""""""""""
261
Philip Reamesdfc238b2015-01-02 19:46:49 +0000262The ''gc.result'' represents the return value of the call target of
263the ''statepoint''. The type of the ''gc.result'' must exactly match
264the type of the target. If the call target returns void, there will
265be no ''gc.result''.
Philip Reamesf6123222014-12-02 19:37:00 +0000266
Philip Reamesdfc238b2015-01-02 19:46:49 +0000267A ''gc.result'' is modeled as a 'readnone' pure function. It has no
268side effects since it is just a projection of the return value of the
269previous call represented by the ''gc.statepoint''.
Philip Reamesf6123222014-12-02 19:37:00 +0000270
Philip Reamesc0127282015-02-24 23:57:26 +0000271'llvm.experimental.gc.relocate' Intrinsic
272^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000273
274Syntax:
275"""""""
276
277::
278
Philip Reamesc0127282015-02-24 23:57:26 +0000279 declare <pointer type>
280 @llvm.experimental.gc.relocate(i32 %statepoint_token,
281 i32 %base_offset,
282 i32 %pointer_offset)
Philip Reamesf6123222014-12-02 19:37:00 +0000283
284Overview:
285"""""""""
286
Philip Reamesdfc238b2015-01-02 19:46:49 +0000287A ''gc.relocate'' returns the potentially relocated value of a pointer
288at the safepoint.
Philip Reamesf6123222014-12-02 19:37:00 +0000289
290Operands:
291"""""""""
292
Philip Reamesdfc238b2015-01-02 19:46:49 +0000293The first argument is the '''gc.statepoint''' which starts the
294safepoint sequence of which this '''gc.relocation'' is a part.
295Despite the typing of this as a generic i32, *only* the value defined
296by a '''gc.statepoint''' is legal here.
Philip Reamesf6123222014-12-02 19:37:00 +0000297
Philip Reamesdfc238b2015-01-02 19:46:49 +0000298The second argument is an index into the statepoints list of arguments
299which specifies the base pointer for the pointer being relocated.
300This index must land within the 'gc parameter' section of the
301statepoint's argument list.
Philip Reamesf6123222014-12-02 19:37:00 +0000302
Philip Reamesdfc238b2015-01-02 19:46:49 +0000303The third argument is an index into the statepoint's list of arguments
304which specify the (potentially) derived pointer being relocated. It
305is legal for this index to be the same as the second argument
306if-and-only-if a base pointer is being relocated. This index must land
307within the 'gc parameter' section of the statepoint's argument list.
Philip Reamesf6123222014-12-02 19:37:00 +0000308
309Semantics:
310""""""""""
Philip Reamesf6123222014-12-02 19:37:00 +0000311
Philip Reamesdfc238b2015-01-02 19:46:49 +0000312The return value of ''gc.relocate'' is the potentially relocated value
313of the pointer specified by it's arguments. It is unspecified how the
314value of the returned pointer relates to the argument to the
315''gc.statepoint'' other than that a) it points to the same source
316language object with the same offset, and b) the 'based-on'
317relationship of the newly relocated pointers is a projection of the
318unrelocated pointers. In particular, the integer value of the pointer
319returned is unspecified.
320
321A ''gc.relocate'' is modeled as a 'readnone' pure function. It has no
322side effects since it is just a way to extract information about work
323done during the actual call modeled by the ''gc.statepoint''.
Philip Reamesf6123222014-12-02 19:37:00 +0000324
325
Philip Reamesce5ff372014-12-04 00:45:23 +0000326Stack Map Format
Philip Reamesf6123222014-12-02 19:37:00 +0000327================
328
Philip Reamesdfc238b2015-01-02 19:46:49 +0000329Locations for each pointer value which may need read and/or updated by
330the runtime or collector are provided via the :ref:`Stack Map format
331<stackmap-format>` specified in the PatchPoint documentation.
Philip Reamesf6123222014-12-02 19:37:00 +0000332
333Each statepoint generates the following Locations:
334
Philip Reamesdfc238b2015-01-02 19:46:49 +0000335* 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 Reamesf6123222014-12-02 19:37:00 +0000347
Philip Reamesdfc238b2015-01-02 19:46:49 +0000348Note that the Locations used in each section may describe the same
349physical location. e.g. A stack slot may appear as a deopt location,
350a gc base pointer, and a gc derived pointer.
Philip Reamesf6123222014-12-02 19:37:00 +0000351
Philip Reamesdfc238b2015-01-02 19:46:49 +0000352The ID field of the 'StkMapRecord' for a statepoint is meaningless and
353it's value is explicitly unspecified.
Philip Reamesf6123222014-12-02 19:37:00 +0000354
Philip Reamesdfc238b2015-01-02 19:46:49 +0000355The LiveOut section of the StkMapRecord will be empty for a statepoint
356record.
Philip Reamesf6123222014-12-02 19:37:00 +0000357
358Safepoint Semantics & Verification
359==================================
360
Philip Reamesdfc238b2015-01-02 19:46:49 +0000361The fundamental correctness property for the compiled code's
362correctness w.r.t. the garbage collector is a dynamic one. It must be
363the case that there is no dynamic trace such that a operation
364involving a potentially relocated pointer is observably-after a
365safepoint which could relocate it. 'observably-after' is this usage
366means that an outside observer could observe this sequence of events
367in a way which precludes the operation being performed before the
368safepoint.
Philip Reamesf6123222014-12-02 19:37:00 +0000369
Philip Reamesdfc238b2015-01-02 19:46:49 +0000370To understand why this 'observable-after' property is required,
371consider a null comparison performed on the original copy of a
372relocated pointer. Assuming that control flow follows the safepoint,
373there is no way to observe externally whether the null comparison is
374performed before or after the safepoint. (Remember, the original
375Value is unmodified by the safepoint.) The compiler is free to make
376either scheduling choice.
Philip Reamesf6123222014-12-02 19:37:00 +0000377
Philip Reamesdfc238b2015-01-02 19:46:49 +0000378The actual correctness property implemented is slightly stronger than
379this. We require that there be no *static path* on which a
380potentially relocated pointer is 'observably-after' it may have been
381relocated. This is slightly stronger than is strictly necessary (and
382thus may disallow some otherwise valid programs), but greatly
383simplifies reasoning about correctness of the compiled code.
Philip Reamesf6123222014-12-02 19:37:00 +0000384
Philip Reamesdfc238b2015-01-02 19:46:49 +0000385By construction, this property will be upheld by the optimizer if
386correctly established in the source IR. This is a key invariant of
387the design.
Philip Reamesf6123222014-12-02 19:37:00 +0000388
Philip Reamesdfc238b2015-01-02 19:46:49 +0000389The existing IR Verifier pass has been extended to check most of the
390local restrictions on the intrinsics mentioned in their respective
391documentation. The current implementation in LLVM does not check the
392key relocation invariant, but this is ongoing work on developing such
393a verifier. Please ask on llvmdev if you're interested in
394experimenting with the current version.
Philip Reamesf6123222014-12-02 19:37:00 +0000395
Philip Reames83331522014-12-04 18:33:28 +0000396Bugs and Enhancements
397=====================
Philip Reamesdfc238b2015-01-02 19:46:49 +0000398
399Currently known bugs and enhancements under consideration can be
400tracked by performing a `bugzilla search
401<http://llvm.org/bugs/buglist.cgi?cmdtype=runnamed&namedcmd=Statepoint%20Bugs&list_id=64342>`_
402for [Statepoint] in the summary field. When filing new bugs, please
403use this tag so that interested parties see the newly filed bug. As
404with most LLVM features, design discussions take place on `llvmdev
405<http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>`_, and patches
406should be sent to `llvm-commits
407<http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits>`_ for review.
Philip Reames83331522014-12-04 18:33:28 +0000408