blob: 084aa62a5cb7fe953c249722449a94237307e171 [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
Bruce Mitchenere9ffb452015-09-12 01:17:08 +000056#. Analogously, a store barrier is a code fragment that runs
Philip Reamesdfc238b2015-01-02 19:46:49 +000057 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.
Philip Reamesc88d7322015-02-25 01:23:59 +0000114 The recommended approach is to use the :ref:`utility passes
115 <statepoint-utilities>` described 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
Philip Reames5017ab52015-02-26 01:18:21 +0000118intrinsic calls known collectively as a "statepoint relocation sequence".
Philip Reamesf6123222014-12-02 19:37:00 +0000119
120Let's consider a simple call in LLVM IR:
Philip Reamesf6123222014-12-02 19:37:00 +0000121
Philip Reames5017ab52015-02-26 01:18:21 +0000122.. code-block:: llvm
Philip Reamesf6123222014-12-02 19:37:00 +0000123
Philip Reames5017ab52015-02-26 01:18:21 +0000124 define i8 addrspace(1)* @test1(i8 addrspace(1)* %obj)
125 gc "statepoint-example" {
126 call void ()* @foo()
127 ret i8 addrspace(1)* %obj
128 }
Philip Reamesf6123222014-12-02 19:37:00 +0000129
Philip Reames5017ab52015-02-26 01:18:21 +0000130Depending on our language we may need to allow a safepoint during the execution
131of ``foo``. If so, we need to let the collector update local values in the
132current frame. If we don't, we'll be accessing a potential invalid reference
133once we eventually return from the call.
134
135In this example, we need to relocate the SSA value ``%obj``. Since we can't
136actually change the value in the SSA value ``%obj``, we need to introduce a new
137SSA value ``%obj.relocated`` which represents the potentially changed value of
138``%obj`` after the safepoint and update any following uses appropriately. The
139resulting relocation sequence is:
140
141.. code-block:: llvm
142
143 define i8 addrspace(1)* @test1(i8 addrspace(1)* %obj)
144 gc "statepoint-example" {
Chen Lid71999e2015-12-26 07:54:32 +0000145 %0 = call token (i64, i32, void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* @foo, i32 0, i32 0, i32 0, i32 0, i8 addrspace(1)* %obj)
146 %obj.relocated = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %0, i32 7, i32 7)
Philip Reames5017ab52015-02-26 01:18:21 +0000147 ret i8 addrspace(1)* %obj.relocated
148 }
Philip Reamesf6123222014-12-02 19:37:00 +0000149
Philip Reamesdfc238b2015-01-02 19:46:49 +0000150Ideally, this sequence would have been represented as a M argument, N
151return value function (where M is the number of values being
152relocated + the original call arguments and N is the original return
153value + each relocated value), but LLVM does not easily support such a
154representation.
155
156Instead, the statepoint intrinsic marks the actual site of the
157safepoint or statepoint. The statepoint returns a token value (which
158exists only at compile time). To get back the original return value
Philip Reames5017ab52015-02-26 01:18:21 +0000159of the call, we use the ``gc.result`` intrinsic. To get the relocation
160of each pointer in turn, we use the ``gc.relocate`` intrinsic with the
161appropriate index. Note that both the ``gc.relocate`` and ``gc.result`` are
162tied to the statepoint. The combination forms a "statepoint relocation
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000163sequence" and represents the entirety of a parseable call or 'statepoint'.
Philip Reamesf6123222014-12-02 19:37:00 +0000164
Philip Reames5017ab52015-02-26 01:18:21 +0000165When lowered, this example would generate the following x86 assembly:
166
167.. code-block:: gas
168
169 .globl test1
170 .align 16, 0x90
171 pushq %rax
172 callq foo
173 .Ltmp1:
174 movq (%rsp), %rax # This load is redundant (oops!)
175 popq %rdx
176 retq
Philip Reamesf6123222014-12-02 19:37:00 +0000177
Philip Reamesdfc238b2015-01-02 19:46:49 +0000178Each of the potentially relocated values has been spilled to the
179stack, and a record of that location has been recorded to the
Philip Reames5017ab52015-02-26 01:18:21 +0000180:ref:`Stack Map section <stackmap-section>`. If the garbage collector
Philip Reamesdfc238b2015-01-02 19:46:49 +0000181needs to update any of these pointers during the call, it knows
182exactly what to change.
Philip Reamesf6123222014-12-02 19:37:00 +0000183
Philip Reames5017ab52015-02-26 01:18:21 +0000184The relevant parts of the StackMap section for our example are:
185
186.. code-block:: gas
187
188 # This describes the call site
189 # Stack Maps: callsite 2882400000
190 .quad 2882400000
191 .long .Ltmp1-test1
192 .short 0
193 # .. 8 entries skipped ..
194 # This entry describes the spill slot which is directly addressable
195 # off RSP with offset 0. Given the value was spilled with a pushq,
196 # that makes sense.
197 # Stack Maps: Loc 8: Direct RSP [encoding: .byte 2, .byte 8, .short 7, .int 0]
198 .byte 2
199 .byte 8
200 .short 7
201 .long 0
202
203This example was taken from the tests for the :ref:`RewriteStatepointsForGC` utility pass. As such, it's full StackMap can be easily examined with the following command.
204
205.. code-block:: bash
206
207 opt -rewrite-statepoints-for-gc test/Transforms/RewriteStatepointsForGC/basics.ll -S | llc -debug-only=stackmaps
208
Philip Reamesc9e54442015-08-26 17:25:36 +0000209Base & Derived Pointers
210^^^^^^^^^^^^^^^^^^^^^^^
211
Philip Reamesca22b862015-08-26 23:13:35 +0000212A "base pointer" is one which points to the starting address of an allocation
213(object). A "derived pointer" is one which is offset from a base pointer by
214some amount. When relocating objects, a garbage collector needs to be able
215to relocate each derived pointer associated with an allocation to the same
216offset from the new address.
Philip Reamesc9e54442015-08-26 17:25:36 +0000217
Philip Reamesca22b862015-08-26 23:13:35 +0000218"Interior derived pointers" remain within the bounds of the allocation
219they're associated with. As a result, the base object can be found at
220runtime provided the bounds of allocations are known to the runtime system.
221
222"Exterior derived pointers" are outside the bounds of the associated object;
223they may even fall within *another* allocations address range. As a result,
224there is no way for a garbage collector to determine which allocation they
225are associated with at runtime and compiler support is needed.
226
227The ``gc.relocate`` intrinsic supports an explicit operand for describing the
228allocation associated with a derived pointer. This operand is frequently
229referred to as the base operand, but does not strictly speaking have to be
230a base pointer, but it does need to lie within the bounds of the associated
231allocation. Some collectors may require that the operand be an actual base
232pointer rather than merely an internal derived pointer. Note that during
233lowering both the base and derived pointer operands are required to be live
234over the associated call safepoint even if the base is otherwise unused
235afterwards.
236
237If we extend our previous example to include a pointless derived pointer,
238we get:
239
240.. code-block:: llvm
241
242 define i8 addrspace(1)* @test1(i8 addrspace(1)* %obj)
243 gc "statepoint-example" {
244 %gep = getelementptr i8, i8 addrspace(1)* %obj, i64 20000
Chen Lid71999e2015-12-26 07:54:32 +0000245 %token = call token (i64, i32, void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* @foo, i32 0, i32 0, i32 0, i32 0, i8 addrspace(1)* %obj, i8 addrspace(1)* %gep)
246 %obj.relocated = call i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %token, i32 7, i32 7)
247 %gep.relocated = call i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %token, i32 7, i32 8)
Philip Reamesca22b862015-08-26 23:13:35 +0000248 %p = getelementptr i8, i8 addrspace(1)* %gep, i64 -20000
249 ret i8 addrspace(1)* %p
250 }
251
252Note that in this example %p and %obj.relocate are the same address and we
253could replace one with the other, potentially removing the derived pointer
254from the live set at the safepoint entirely.
Philip Reames5017ab52015-02-26 01:18:21 +0000255
Pat Gavlincc0431d2015-05-08 18:07:42 +0000256GC Transitions
257^^^^^^^^^^^^^^^^^^
Philip Reames5017ab52015-02-26 01:18:21 +0000258
Pat Gavlincc0431d2015-05-08 18:07:42 +0000259As a practical consideration, many garbage-collected systems allow code that is
260collector-aware ("managed code") to call code that is not collector-aware
261("unmanaged code"). It is common that such calls must also be safepoints, since
262it is desirable to allow the collector to run during the execution of
263unmanaged code. Futhermore, it is common that coordinating the transition from
264managed to unmanaged code requires extra code generation at the call site to
265inform the collector of the transition. In order to support these needs, a
266statepoint may be marked as a GC transition, and data that is necessary to
267perform the transition (if any) may be provided as additional arguments to the
268statepoint.
269
270 Note that although in many cases statepoints may be inferred to be GC
271 transitions based on the function symbols involved (e.g. a call from a
272 function with GC strategy "foo" to a function with GC strategy "bar"),
273 indirect calls that are also GC transitions must also be supported. This
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000274 requirement is the driving force behind the decision to require that GC
Pat Gavlincc0431d2015-05-08 18:07:42 +0000275 transitions are explicitly marked.
276
277Let's revisit the sample given above, this time treating the call to ``@foo``
278as a GC transition. Depending on our target, the transition code may need to
279access some extra state in order to inform the collector of the transition.
280Let's assume a hypothetical GC--somewhat unimaginatively named "hypothetical-gc"
281--that requires that a TLS variable must be written to before and after a call
282to unmanaged code. The resulting relocation sequence is:
283
284.. code-block:: llvm
285
286 @flag = thread_local global i32 0, align 4
287
288 define i8 addrspace(1)* @test1(i8 addrspace(1) *%obj)
289 gc "hypothetical-gc" {
290
Chen Lid71999e2015-12-26 07:54:32 +0000291 %0 = call token (i64, i32, void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* @foo, i32 0, i32 1, i32* @Flag, i32 0, i8 addrspace(1)* %obj)
292 %obj.relocated = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %0, i32 7, i32 7)
Pat Gavlincc0431d2015-05-08 18:07:42 +0000293 ret i8 addrspace(1)* %obj.relocated
294 }
295
296During lowering, this will result in a instruction selection DAG that looks
297something like:
298
Pat Gavlin7afaed22015-05-08 18:37:49 +0000299::
Pat Gavlincc0431d2015-05-08 18:07:42 +0000300
301 CALLSEQ_START
302 ...
303 GC_TRANSITION_START (lowered i32 *@Flag), SRCVALUE i32* Flag
304 STATEPOINT
305 GC_TRANSITION_END (lowered i32 *@Flag), SRCVALUE i32 *Flag
306 ...
307 CALLSEQ_END
308
309In order to generate the necessary transition code, the backend for each target
310supported by "hypothetical-gc" must be modified to lower ``GC_TRANSITION_START``
311and ``GC_TRANSITION_END`` nodes appropriately when the "hypothetical-gc"
312strategy is in use for a particular function. Assuming that such lowering has
313been added for X86, the generated assembly would be:
314
315.. code-block:: gas
316
317 .globl test1
318 .align 16, 0x90
319 pushq %rax
320 movl $1, %fs:Flag@TPOFF
321 callq foo
322 movl $0, %fs:Flag@TPOFF
323 .Ltmp1:
324 movq (%rsp), %rax # This load is redundant (oops!)
325 popq %rdx
326 retq
327
328Note that the design as presented above is not fully implemented: in particular,
329strategy-specific lowering is not present, and all GC transitions are emitted as
330as single no-op before and after the call instruction. These no-ops are often
331removed by the backend during dead machine instruction elimination.
Philip Reames5017ab52015-02-26 01:18:21 +0000332
333
Philip Reamesf6123222014-12-02 19:37:00 +0000334Intrinsics
335===========
336
Philip Reamesc0127282015-02-24 23:57:26 +0000337'llvm.experimental.gc.statepoint' Intrinsic
338^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000339
340Syntax:
341"""""""
342
343::
344
Chen Lid71999e2015-12-26 07:54:32 +0000345 declare token
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000346 @llvm.experimental.gc.statepoint(i64 <id>, i32 <num patch bytes>,
347 func_type <target>,
Sanjoy Dasdc4932f2015-05-13 20:19:51 +0000348 i64 <#call args>, i64 <flags>,
Philip Reamesc0127282015-02-24 23:57:26 +0000349 ... (call parameters),
Pat Gavlincc0431d2015-05-08 18:07:42 +0000350 i64 <# transition args>, ... (transition parameters),
Philip Reamesf6123222014-12-02 19:37:00 +0000351 i64 <# deopt args>, ... (deopt parameters),
352 ... (gc parameters))
353
354Overview:
355"""""""""
356
Philip Reamesdfc238b2015-01-02 19:46:49 +0000357The statepoint intrinsic represents a call which is parse-able by the
358runtime.
Philip Reamesf6123222014-12-02 19:37:00 +0000359
360Operands:
361"""""""""
362
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000363The 'id' operand is a constant integer that is reported as the ID
364field in the generated stackmap. LLVM does not interpret this
365parameter in any way and its meaning is up to the statepoint user to
366decide. Note that LLVM is free to duplicate code containing
367statepoint calls, and this may transform IR that had a unique 'id' per
368lexical call to statepoint to IR that does not.
369
370If 'num patch bytes' is non-zero then the call instruction
371corresponding to the statepoint is not emitted and LLVM emits 'num
372patch bytes' bytes of nops in its place. LLVM will emit code to
373prepare the function arguments and retrieve the function return value
374in accordance to the calling convention; the former before the nop
375sequence and the latter after the nop sequence. It is expected that
376the user will patch over the 'num patch bytes' bytes of nops with a
377calling sequence specific to their runtime before executing the
378generated machine code. There are no guarantees with respect to the
379alignment of the nop sequence. Unlike :doc:`StackMaps` statepoints do
Sanjoy Dascfe41f02015-07-28 23:50:30 +0000380not have a concept of shadow bytes. Note that semantically the
381statepoint still represents a call or invoke to 'target', and the nop
382sequence after patching is expected to represent an operation
383equivalent to a call or invoke to 'target'.
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000384
Philip Reamesdfc238b2015-01-02 19:46:49 +0000385The 'target' operand is the function actually being called. The
386target can be specified as either a symbolic LLVM function, or as an
387arbitrary Value of appropriate function type. Note that the function
388type must match the signature of the callee and the types of the 'call
Sanjoy Dascfe41f02015-07-28 23:50:30 +0000389parameters' arguments.
Philip Reamesf6123222014-12-02 19:37:00 +0000390
Philip Reamesdfc238b2015-01-02 19:46:49 +0000391The '#call args' operand is the number of arguments to the actual
392call. It must exactly match the number of arguments passed in the
393'call parameters' variable length section.
Philip Reamesf6123222014-12-02 19:37:00 +0000394
Pat Gavlincc0431d2015-05-08 18:07:42 +0000395The 'flags' operand is used to specify extra information about the
396statepoint. This is currently only used to mark certain statepoints
397as GC transitions. This operand is a 64-bit integer with the following
398layout, where bit 0 is the least significant bit:
399
400 +-------+---------------------------------------------------+
401 | Bit # | Usage |
402 +=======+===================================================+
403 | 0 | Set if the statepoint is a GC transition, cleared |
404 | | otherwise. |
405 +-------+---------------------------------------------------+
406 | 1-63 | Reserved for future use; must be cleared. |
407 +-------+---------------------------------------------------+
Philip Reamesf6123222014-12-02 19:37:00 +0000408
Philip Reamesdfc238b2015-01-02 19:46:49 +0000409The 'call parameters' arguments are simply the arguments which need to
410be passed to the call target. They will be lowered according to the
411specified calling convention and otherwise handled like a normal call
412instruction. The number of arguments must exactly match what is
413specified in '# call args'. The types must match the signature of
414'target'.
Philip Reamesf6123222014-12-02 19:37:00 +0000415
Pat Gavlincc0431d2015-05-08 18:07:42 +0000416The 'transition parameters' arguments contain an arbitrary list of
417Values which need to be passed to GC transition code. They will be
418lowered and passed as operands to the appropriate GC_TRANSITION nodes
419in the selection DAG. It is assumed that these arguments must be
420available before and after (but not necessarily during) the execution
421of the callee. The '# transition args' field indicates how many operands
422are to be interpreted as 'transition parameters'.
423
Philip Reamesdfc238b2015-01-02 19:46:49 +0000424The 'deopt parameters' arguments contain an arbitrary list of Values
425which is meaningful to the runtime. The runtime may read any of these
426values, but is assumed not to modify them. If the garbage collector
427might need to modify one of these values, it must also be listed in
428the 'gc pointer' argument list. The '# deopt args' field indicates
429how many operands are to be interpreted as 'deopt parameters'.
Philip Reamesf6123222014-12-02 19:37:00 +0000430
Philip Reamesdfc238b2015-01-02 19:46:49 +0000431The 'gc parameters' arguments contain every pointer to a garbage
432collector object which potentially needs to be updated by the garbage
433collector. Note that the argument list must explicitly contain a base
434pointer for every derived pointer listed. The order of arguments is
435unimportant. Unlike the other variable length parameter sets, this
436list is not length prefixed.
Philip Reamesf6123222014-12-02 19:37:00 +0000437
438Semantics:
439""""""""""
440
Philip Reamesdfc238b2015-01-02 19:46:49 +0000441A statepoint is assumed to read and write all memory. As a result,
442memory operations can not be reordered past a statepoint. It is
443illegal to mark a statepoint as being either 'readonly' or 'readnone'.
Philip Reamesf6123222014-12-02 19:37:00 +0000444
Philip Reamesdfc238b2015-01-02 19:46:49 +0000445Note that legal IR can not perform any memory operation on a 'gc
446pointer' argument of the statepoint in a location statically reachable
447from the statepoint. Instead, the explicitly relocated value (from a
Philip Reamesc609a592015-02-25 00:22:07 +0000448``gc.relocate``) must be used.
Philip Reamesf6123222014-12-02 19:37:00 +0000449
Philip Reamesc0127282015-02-24 23:57:26 +0000450'llvm.experimental.gc.result' Intrinsic
451^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000452
453Syntax:
454"""""""
455
456::
457
458 declare type*
Chen Lid71999e2015-12-26 07:54:32 +0000459 @llvm.experimental.gc.result(token %statepoint_token)
Philip Reamesf6123222014-12-02 19:37:00 +0000460
461Overview:
462"""""""""
463
Philip Reamesc609a592015-02-25 00:22:07 +0000464``gc.result`` extracts the result of the original call instruction
465which was replaced by the ``gc.statepoint``. The ``gc.result``
Philip Reamesdfc238b2015-01-02 19:46:49 +0000466intrinsic is actually a family of three intrinsics due to an
467implementation limitation. Other than the type of the return value,
468the semantics are the same.
Philip Reamesf6123222014-12-02 19:37:00 +0000469
470Operands:
471"""""""""
472
Philip Reamesc609a592015-02-25 00:22:07 +0000473The first and only argument is the ``gc.statepoint`` which starts
474the safepoint sequence of which this ``gc.result`` is a part.
Chen Lid71999e2015-12-26 07:54:32 +0000475Despite the typing of this as a generic token, *only* the value defined
Philip Reamesc609a592015-02-25 00:22:07 +0000476by a ``gc.statepoint`` is legal here.
Philip Reamesf6123222014-12-02 19:37:00 +0000477
478Semantics:
479""""""""""
480
Philip Reamesc609a592015-02-25 00:22:07 +0000481The ``gc.result`` represents the return value of the call target of
482the ``statepoint``. The type of the ``gc.result`` must exactly match
Philip Reamesdfc238b2015-01-02 19:46:49 +0000483the type of the target. If the call target returns void, there will
Philip Reamesc609a592015-02-25 00:22:07 +0000484be no ``gc.result``.
Philip Reamesf6123222014-12-02 19:37:00 +0000485
Philip Reamesc609a592015-02-25 00:22:07 +0000486A ``gc.result`` is modeled as a 'readnone' pure function. It has no
Philip Reamesdfc238b2015-01-02 19:46:49 +0000487side effects since it is just a projection of the return value of the
Philip Reamesc609a592015-02-25 00:22:07 +0000488previous call represented by the ``gc.statepoint``.
Philip Reamesf6123222014-12-02 19:37:00 +0000489
Philip Reamesc0127282015-02-24 23:57:26 +0000490'llvm.experimental.gc.relocate' Intrinsic
491^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000492
493Syntax:
494"""""""
495
496::
497
Philip Reamesc0127282015-02-24 23:57:26 +0000498 declare <pointer type>
Chen Lid71999e2015-12-26 07:54:32 +0000499 @llvm.experimental.gc.relocate(token %statepoint_token,
Philip Reamesc0127282015-02-24 23:57:26 +0000500 i32 %base_offset,
501 i32 %pointer_offset)
Philip Reamesf6123222014-12-02 19:37:00 +0000502
503Overview:
504"""""""""
505
Philip Reamesc609a592015-02-25 00:22:07 +0000506A ``gc.relocate`` returns the potentially relocated value of a pointer
Philip Reamesdfc238b2015-01-02 19:46:49 +0000507at the safepoint.
Philip Reamesf6123222014-12-02 19:37:00 +0000508
509Operands:
510"""""""""
511
Philip Reamesc609a592015-02-25 00:22:07 +0000512The first argument is the ``gc.statepoint`` which starts the
513safepoint sequence of which this ``gc.relocation`` is a part.
Chen Lid71999e2015-12-26 07:54:32 +0000514Despite the typing of this as a generic token, *only* the value defined
Philip Reamesc609a592015-02-25 00:22:07 +0000515by a ``gc.statepoint`` is legal here.
Philip Reamesf6123222014-12-02 19:37:00 +0000516
Philip Reamesdfc238b2015-01-02 19:46:49 +0000517The second argument is an index into the statepoints list of arguments
Philip Reamesca22b862015-08-26 23:13:35 +0000518which specifies the allocation for the pointer being relocated.
Philip Reamesdfc238b2015-01-02 19:46:49 +0000519This index must land within the 'gc parameter' section of the
Philip Reamesca22b862015-08-26 23:13:35 +0000520statepoint's argument list. The associated value must be within the
521object with which the pointer being relocated is associated. The optimizer
522is free to change *which* interior derived pointer is reported, provided that
523it does not replace an actual base pointer with another interior derived
524pointer. Collectors are allowed to rely on the base pointer operand
525remaining an actual base pointer if so constructed.
Philip Reamesf6123222014-12-02 19:37:00 +0000526
Philip Reamesdfc238b2015-01-02 19:46:49 +0000527The third argument is an index into the statepoint's list of arguments
528which specify the (potentially) derived pointer being relocated. It
529is legal for this index to be the same as the second argument
530if-and-only-if a base pointer is being relocated. This index must land
531within the 'gc parameter' section of the statepoint's argument list.
Philip Reamesf6123222014-12-02 19:37:00 +0000532
533Semantics:
534""""""""""
Philip Reamesf6123222014-12-02 19:37:00 +0000535
Philip Reamesc609a592015-02-25 00:22:07 +0000536The return value of ``gc.relocate`` is the potentially relocated value
Philip Reamesdfc238b2015-01-02 19:46:49 +0000537of the pointer specified by it's arguments. It is unspecified how the
538value of the returned pointer relates to the argument to the
Philip Reamesc609a592015-02-25 00:22:07 +0000539``gc.statepoint`` other than that a) it points to the same source
Philip Reamesdfc238b2015-01-02 19:46:49 +0000540language object with the same offset, and b) the 'based-on'
541relationship of the newly relocated pointers is a projection of the
542unrelocated pointers. In particular, the integer value of the pointer
543returned is unspecified.
544
Philip Reamesc609a592015-02-25 00:22:07 +0000545A ``gc.relocate`` is modeled as a ``readnone`` pure function. It has no
Philip Reamesdfc238b2015-01-02 19:46:49 +0000546side effects since it is just a way to extract information about work
Philip Reamesc609a592015-02-25 00:22:07 +0000547done during the actual call modeled by the ``gc.statepoint``.
Philip Reamesf6123222014-12-02 19:37:00 +0000548
Philip Reamese6625502015-02-25 23:22:43 +0000549.. _statepoint-stackmap-format:
Philip Reamesf6123222014-12-02 19:37:00 +0000550
Philip Reamesce5ff372014-12-04 00:45:23 +0000551Stack Map Format
Philip Reamesf6123222014-12-02 19:37:00 +0000552================
553
Philip Reamesdfc238b2015-01-02 19:46:49 +0000554Locations for each pointer value which may need read and/or updated by
555the runtime or collector are provided via the :ref:`Stack Map format
556<stackmap-format>` specified in the PatchPoint documentation.
Philip Reamesf6123222014-12-02 19:37:00 +0000557
558Each statepoint generates the following Locations:
559
Pat Gavlinc7dc6d6ee2015-05-12 19:50:19 +0000560* Constant which describes the calling convention of the call target. This
561 constant is a valid :ref:`calling convention identifier <callingconv>` for
562 the version of LLVM used to generate the stackmap. No additional compatibility
563 guarantees are made for this constant over what LLVM provides elsewhere w.r.t.
564 these identifiers.
565* Constant which describes the flags passed to the statepoint intrinsic
Philip Reamesdfc238b2015-01-02 19:46:49 +0000566* Constant which describes number of following deopt *Locations* (not
567 operands)
568* Variable number of Locations, one for each deopt parameter listed in
Philip Reames95e363d2016-01-14 23:58:18 +0000569 the IR statepoint (same number as described by previous Constant). At
570 the moment, only deopt parameters with a bitwidth of 64 bits or less
571 are supported. Values of a type larger than 64 bits can be specified
572 and reported only if a) the value is constant at the call site, and b)
573 the constant can be represented with less than 64 bits (assuming zero
574 extension to the original bitwidth).
Philip Reames35bafee2016-01-15 00:13:39 +0000575* Variable number of relocation records, each of which consists of
576 exactly two Locations. Relocation records are described in detail
577 below.
578
579Each relocation record provides sufficient information for a collector to
580relocate one or more derived pointers. Each record consists of a pair of
581Locations. The second element in the record represents the pointer (or
582pointers) which need updated. The first element in the record provides a
583pointer to the base of the object with which the pointer(s) being relocated is
584associated. This information is required for handling generalized derived
585pointers since a pointer may be outside the bounds of the original allocation,
586but still needs to be relocated with the allocation. Additionally:
587
588* It is guaranteed that the base pointer must also appear explicitly as a
589 relocation pair if used after the statepoint.
590* There may be fewer relocation records then gc parameters in the IR
Philip Reamesdfc238b2015-01-02 19:46:49 +0000591 statepoint. Each *unique* pair will occur at least once; duplicates
Philip Reames35bafee2016-01-15 00:13:39 +0000592 are possible.
593* The Locations within each record may either be of pointer size or a
594 multiple of pointer size. In the later case, the record must be
595 interpreted as describing a sequence of pointers and their corresponding
596 base pointers. If the Location is of size N x sizeof(pointer), then
597 there will be N records of one pointer each contained within the Location.
598 Both Locations in a pair can be assumed to be of the same size.
Philip Reamesf6123222014-12-02 19:37:00 +0000599
Philip Reamesdfc238b2015-01-02 19:46:49 +0000600Note that the Locations used in each section may describe the same
601physical location. e.g. A stack slot may appear as a deopt location,
602a gc base pointer, and a gc derived pointer.
Philip Reamesf6123222014-12-02 19:37:00 +0000603
Philip Reamesdfc238b2015-01-02 19:46:49 +0000604The LiveOut section of the StkMapRecord will be empty for a statepoint
605record.
Philip Reamesf6123222014-12-02 19:37:00 +0000606
607Safepoint Semantics & Verification
608==================================
609
Philip Reamesdfc238b2015-01-02 19:46:49 +0000610The fundamental correctness property for the compiled code's
611correctness w.r.t. the garbage collector is a dynamic one. It must be
612the case that there is no dynamic trace such that a operation
613involving a potentially relocated pointer is observably-after a
614safepoint which could relocate it. 'observably-after' is this usage
615means that an outside observer could observe this sequence of events
616in a way which precludes the operation being performed before the
617safepoint.
Philip Reamesf6123222014-12-02 19:37:00 +0000618
Philip Reamesdfc238b2015-01-02 19:46:49 +0000619To understand why this 'observable-after' property is required,
620consider a null comparison performed on the original copy of a
621relocated pointer. Assuming that control flow follows the safepoint,
622there is no way to observe externally whether the null comparison is
623performed before or after the safepoint. (Remember, the original
624Value is unmodified by the safepoint.) The compiler is free to make
625either scheduling choice.
Philip Reamesf6123222014-12-02 19:37:00 +0000626
Philip Reamesdfc238b2015-01-02 19:46:49 +0000627The actual correctness property implemented is slightly stronger than
628this. We require that there be no *static path* on which a
629potentially relocated pointer is 'observably-after' it may have been
630relocated. This is slightly stronger than is strictly necessary (and
631thus may disallow some otherwise valid programs), but greatly
632simplifies reasoning about correctness of the compiled code.
Philip Reamesf6123222014-12-02 19:37:00 +0000633
Philip Reamesdfc238b2015-01-02 19:46:49 +0000634By construction, this property will be upheld by the optimizer if
635correctly established in the source IR. This is a key invariant of
636the design.
Philip Reamesf6123222014-12-02 19:37:00 +0000637
Philip Reamesdfc238b2015-01-02 19:46:49 +0000638The existing IR Verifier pass has been extended to check most of the
639local restrictions on the intrinsics mentioned in their respective
640documentation. The current implementation in LLVM does not check the
641key relocation invariant, but this is ongoing work on developing such
Tanya Lattner0d28f802015-08-05 03:51:17 +0000642a verifier. Please ask on llvm-dev if you're interested in
Philip Reamesdfc238b2015-01-02 19:46:49 +0000643experimenting with the current version.
Philip Reamesf6123222014-12-02 19:37:00 +0000644
Philip Reamesc88d7322015-02-25 01:23:59 +0000645.. _statepoint-utilities:
646
647Utility Passes for Safepoint Insertion
648======================================
649
650.. _RewriteStatepointsForGC:
651
652RewriteStatepointsForGC
653^^^^^^^^^^^^^^^^^^^^^^^^
654
655The pass RewriteStatepointsForGC transforms a functions IR by replacing a
656``gc.statepoint`` (with an optional ``gc.result``) with a full relocation
657sequence, including all required ``gc.relocates``. To function, the pass
658requires that the GC strategy specified for the function be able to reliably
659distinguish between GC references and non-GC references in IR it is given.
660
661As an example, given this code:
662
663.. code-block:: llvm
664
665 define i8 addrspace(1)* @test1(i8 addrspace(1)* %obj)
666 gc "statepoint-example" {
Chen Lid71999e2015-12-26 07:54:32 +0000667 call token (i64, i32, void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 2882400000, i32 0, void ()* @foo, i32 0, i32 0, i32 0, i32 5, i32 0, i32 -1, i32 0, i32 0, i32 0)
Philip Reamesc88d7322015-02-25 01:23:59 +0000668 ret i8 addrspace(1)* %obj
669 }
670
671The pass would produce this IR:
672
673.. code-block:: llvm
674
675 define i8 addrspace(1)* @test1(i8 addrspace(1)* %obj)
676 gc "statepoint-example" {
Chen Lid71999e2015-12-26 07:54:32 +0000677 %0 = call token (i64, i32, void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 2882400000, i32 0, void ()* @foo, i32 0, i32 0, i32 0, i32 5, i32 0, i32 -1, i32 0, i32 0, i32 0, i8 addrspace(1)* %obj)
678 %obj.relocated = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %0, i32 12, i32 12)
Philip Reamesc88d7322015-02-25 01:23:59 +0000679 ret i8 addrspace(1)* %obj.relocated
680 }
681
682In the above examples, the addrspace(1) marker on the pointers is the mechanism
683that the ``statepoint-example`` GC strategy uses to distinguish references from
684non references. Address space 1 is not globally reserved for this purpose.
685
686This pass can be used an utility function by a language frontend that doesn't
687want to manually reason about liveness, base pointers, or relocation when
688constructing IR. As currently implemented, RewriteStatepointsForGC must be
Philip Reamesca22b862015-08-26 23:13:35 +0000689run after SSA construction (i.e. mem2ref).
Philip Reamesc88d7322015-02-25 01:23:59 +0000690
Philip Reamesca22b862015-08-26 23:13:35 +0000691RewriteStatepointsForGC will ensure that appropriate base pointers are listed
692for every relocation created. It will do so by duplicating code as needed to
693propagate the base pointer associated with each pointer being relocated to
694the appropriate safepoints. The implementation assumes that the following
695IR constructs produce base pointers: loads from the heap, addresses of global
696variables, function arguments, function return values. Constant pointers (such
697as null) are also assumed to be base pointers. In practice, this constraint
698can be relaxed to producing interior derived pointers provided the target
699collector can find the associated allocation from an arbitrary interior
700derived pointer.
Philip Reamesc88d7322015-02-25 01:23:59 +0000701
702In practice, RewriteStatepointsForGC can be run much later in the pass
703pipeline, after most optimization is already done. This helps to improve
704the quality of the generated code when compiled with garbage collection support.
705In the long run, this is the intended usage model. At this time, a few details
706have yet to be worked out about the semantic model required to guarantee this
707is always correct. As such, please use with caution and report bugs.
708
709.. _PlaceSafepoints:
710
711PlaceSafepoints
712^^^^^^^^^^^^^^^^
713
714The pass PlaceSafepoints transforms a function's IR by replacing any call or
715invoke instructions with appropriate ``gc.statepoint`` and ``gc.result`` pairs,
716and inserting safepoint polls sufficient to ensure running code checks for a
717safepoint request on a timely manner. This pass is expected to be run before
718RewriteStatepointsForGC and thus does not produce full relocation sequences.
719
Philip Reames5017ab52015-02-26 01:18:21 +0000720As an example, given input IR of the following:
721
722.. code-block:: llvm
723
724 define void @test() gc "statepoint-example" {
725 call void @foo()
726 ret void
727 }
728
729 declare void @do_safepoint()
730 define void @gc.safepoint_poll() {
731 call void @do_safepoint()
732 ret void
733 }
734
735
736This pass would produce the following IR:
737
738.. code-block:: llvm
739
740 define void @test() gc "statepoint-example" {
Chen Lid71999e2015-12-26 07:54:32 +0000741 %safepoint_token = call token (i64, i32, void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 2882400000, i32 0, void ()* @do_safepoint, i32 0, i32 0, i32 0, i32 0)
742 %safepoint_token1 = call token (i64, i32, void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 2882400000, i32 0, void ()* @foo, i32 0, i32 0, i32 0, i32 0)
Philip Reames5017ab52015-02-26 01:18:21 +0000743 ret void
744 }
745
746In this case, we've added an (unconditional) entry safepoint poll and converted the call into a ``gc.statepoint``. Note that despite appearances, the entry poll is not necessarily redundant. We'd have to know that ``foo`` and ``test`` were not mutually recursive for the poll to be redundant. In practice, you'd probably want to your poll definition to contain a conditional branch of some form.
747
748
Philip Reamesc88d7322015-02-25 01:23:59 +0000749At the moment, PlaceSafepoints can insert safepoint polls at method entry and
750loop backedges locations. Extending this to work with return polls would be
751straight forward if desired.
752
753PlaceSafepoints includes a number of optimizations to avoid placing safepoint
754polls at particular sites unless needed to ensure timely execution of a poll
755under normal conditions. PlaceSafepoints does not attempt to ensure timely
756execution of a poll under worst case conditions such as heavy system paging.
757
758The implementation of a safepoint poll action is specified by looking up a
759function of the name ``gc.safepoint_poll`` in the containing Module. The body
760of this function is inserted at each poll site desired. While calls or invokes
761inside this method are transformed to a ``gc.statepoints``, recursive poll
762insertion is not performed.
763
Sanjoy Dasba74e642015-05-13 20:11:31 +0000764By default PlaceSafepoints passes in ``0xABCDEF00`` as the statepoint
765ID and ``0`` as the number of patchable bytes to the newly constructed
766``gc.statepoint``. These values can be configured on a per-callsite
767basis using the attributes ``"statepoint-id"`` and
768``"statepoint-num-patch-bytes"``. If a call site is marked with a
769``"statepoint-id"`` function attribute and its value is a positive
770integer (represented as a string), then that value is used as the ID
771of the newly constructed ``gc.statepoint``. If a call site is marked
772with a ``"statepoint-num-patch-bytes"`` function attribute and its
773value is a positive integer, then that value is used as the 'num patch
774bytes' parameter of the newly constructed ``gc.statepoint``. The
775``"statepoint-id"`` and ``"statepoint-num-patch-bytes"`` attributes
776are not propagated to the ``gc.statepoint`` call or invoke if they
777could be successfully parsed.
778
Philip Reamesc88d7322015-02-25 01:23:59 +0000779If you are scheduling the RewriteStatepointsForGC pass late in the pass order,
780you should probably schedule this pass immediately before it. The exception
781would be if you need to preserve abstract frame information (e.g. for
782deoptimization or introspection) at safepoints. In that case, ask on the
Tanya Lattner0d28f802015-08-05 03:51:17 +0000783llvm-dev mailing list for suggestions.
Philip Reamesc88d7322015-02-25 01:23:59 +0000784
785
Philip Reamesb7736312015-07-16 21:10:46 +0000786Supported Architectures
787=======================
788
789Support for statepoint generation requires some code for each backend.
790Today, only X86_64 is supported.
791
Philip Reames83331522014-12-04 18:33:28 +0000792Bugs and Enhancements
793=====================
Philip Reamesdfc238b2015-01-02 19:46:49 +0000794
795Currently known bugs and enhancements under consideration can be
796tracked by performing a `bugzilla search
797<http://llvm.org/bugs/buglist.cgi?cmdtype=runnamed&namedcmd=Statepoint%20Bugs&list_id=64342>`_
798for [Statepoint] in the summary field. When filing new bugs, please
799use this tag so that interested parties see the newly filed bug. As
Tanya Lattner0d28f802015-08-05 03:51:17 +0000800with most LLVM features, design discussions take place on `llvm-dev
801<http://lists.llvm.org/mailman/listinfo/llvm-dev>`_, and patches
Philip Reamesdfc238b2015-01-02 19:46:49 +0000802should be sent to `llvm-commits
Tanya Lattner0d28f802015-08-05 03:51:17 +0000803<http://lists.llvm.org/mailman/listinfo/llvm-commits>`_ for review.
Philip Reames83331522014-12-04 18:33:28 +0000804