blob: 9741c93cc8110a3c0e3b2e174d9f566bcdd8e6dc [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.
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" {
145 %0 = call i32 (void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(void ()* @foo, i32 0, i32 0, i32 5, i32 0, i32 -1, i32 0, i32 0, i32 0, i8 addrspace(1)* %obj)
146 %obj.relocated = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(i32 %0, i32 9, i32 9)
147 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
163sequence" and represents the entitety 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
209
210
211
212
Philip Reamesf6123222014-12-02 19:37:00 +0000213Intrinsics
214===========
215
Philip Reamesc0127282015-02-24 23:57:26 +0000216'llvm.experimental.gc.statepoint' Intrinsic
217^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000218
219Syntax:
220"""""""
221
222::
223
224 declare i32
Philip Reamesc0127282015-02-24 23:57:26 +0000225 @llvm.experimental.gc.statepoint(func_type <target>,
226 i64 <#call args>. i64 <unused>,
227 ... (call parameters),
Philip Reamesf6123222014-12-02 19:37:00 +0000228 i64 <# deopt args>, ... (deopt parameters),
229 ... (gc parameters))
230
231Overview:
232"""""""""
233
Philip Reamesdfc238b2015-01-02 19:46:49 +0000234The statepoint intrinsic represents a call which is parse-able by the
235runtime.
Philip Reamesf6123222014-12-02 19:37:00 +0000236
237Operands:
238"""""""""
239
Philip Reamesdfc238b2015-01-02 19:46:49 +0000240The 'target' operand is the function actually being called. The
241target can be specified as either a symbolic LLVM function, or as an
242arbitrary Value of appropriate function type. Note that the function
243type must match the signature of the callee and the types of the 'call
244parameters' arguments.
Philip Reamesf6123222014-12-02 19:37:00 +0000245
Philip Reamesdfc238b2015-01-02 19:46:49 +0000246The '#call args' operand is the number of arguments to the actual
247call. It must exactly match the number of arguments passed in the
248'call parameters' variable length section.
Philip Reamesf6123222014-12-02 19:37:00 +0000249
Philip Reamesdfc238b2015-01-02 19:46:49 +0000250The 'unused' operand is unused and likely to be removed. Please do
251not use.
Philip Reamesf6123222014-12-02 19:37:00 +0000252
Philip Reamesdfc238b2015-01-02 19:46:49 +0000253The 'call parameters' arguments are simply the arguments which need to
254be passed to the call target. They will be lowered according to the
255specified calling convention and otherwise handled like a normal call
256instruction. The number of arguments must exactly match what is
257specified in '# call args'. The types must match the signature of
258'target'.
Philip Reamesf6123222014-12-02 19:37:00 +0000259
Philip Reamesdfc238b2015-01-02 19:46:49 +0000260The 'deopt parameters' arguments contain an arbitrary list of Values
261which is meaningful to the runtime. The runtime may read any of these
262values, but is assumed not to modify them. If the garbage collector
263might need to modify one of these values, it must also be listed in
264the 'gc pointer' argument list. The '# deopt args' field indicates
265how many operands are to be interpreted as 'deopt parameters'.
Philip Reamesf6123222014-12-02 19:37:00 +0000266
Philip Reamesdfc238b2015-01-02 19:46:49 +0000267The 'gc parameters' arguments contain every pointer to a garbage
268collector object which potentially needs to be updated by the garbage
269collector. Note that the argument list must explicitly contain a base
270pointer for every derived pointer listed. The order of arguments is
271unimportant. Unlike the other variable length parameter sets, this
272list is not length prefixed.
Philip Reamesf6123222014-12-02 19:37:00 +0000273
274Semantics:
275""""""""""
276
Philip Reamesdfc238b2015-01-02 19:46:49 +0000277A statepoint is assumed to read and write all memory. As a result,
278memory operations can not be reordered past a statepoint. It is
279illegal to mark a statepoint as being either 'readonly' or 'readnone'.
Philip Reamesf6123222014-12-02 19:37:00 +0000280
Philip Reamesdfc238b2015-01-02 19:46:49 +0000281Note that legal IR can not perform any memory operation on a 'gc
282pointer' argument of the statepoint in a location statically reachable
283from the statepoint. Instead, the explicitly relocated value (from a
Philip Reamesc609a592015-02-25 00:22:07 +0000284``gc.relocate``) must be used.
Philip Reamesf6123222014-12-02 19:37:00 +0000285
Philip Reamesc0127282015-02-24 23:57:26 +0000286'llvm.experimental.gc.result' Intrinsic
287^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000288
289Syntax:
290"""""""
291
292::
293
294 declare type*
Philip Reamesc0127282015-02-24 23:57:26 +0000295 @llvm.experimental.gc.result(i32 %statepoint_token)
Philip Reamesf6123222014-12-02 19:37:00 +0000296
297Overview:
298"""""""""
299
Philip Reamesc609a592015-02-25 00:22:07 +0000300``gc.result`` extracts the result of the original call instruction
301which was replaced by the ``gc.statepoint``. The ``gc.result``
Philip Reamesdfc238b2015-01-02 19:46:49 +0000302intrinsic is actually a family of three intrinsics due to an
303implementation limitation. Other than the type of the return value,
304the semantics are the same.
Philip Reamesf6123222014-12-02 19:37:00 +0000305
306Operands:
307"""""""""
308
Philip Reamesc609a592015-02-25 00:22:07 +0000309The first and only argument is the ``gc.statepoint`` which starts
310the safepoint sequence of which this ``gc.result`` is a part.
Philip Reamesdfc238b2015-01-02 19:46:49 +0000311Despite the typing of this as a generic i32, *only* the value defined
Philip Reamesc609a592015-02-25 00:22:07 +0000312by a ``gc.statepoint`` is legal here.
Philip Reamesf6123222014-12-02 19:37:00 +0000313
314Semantics:
315""""""""""
316
Philip Reamesc609a592015-02-25 00:22:07 +0000317The ``gc.result`` represents the return value of the call target of
318the ``statepoint``. The type of the ``gc.result`` must exactly match
Philip Reamesdfc238b2015-01-02 19:46:49 +0000319the type of the target. If the call target returns void, there will
Philip Reamesc609a592015-02-25 00:22:07 +0000320be no ``gc.result``.
Philip Reamesf6123222014-12-02 19:37:00 +0000321
Philip Reamesc609a592015-02-25 00:22:07 +0000322A ``gc.result`` is modeled as a 'readnone' pure function. It has no
Philip Reamesdfc238b2015-01-02 19:46:49 +0000323side effects since it is just a projection of the return value of the
Philip Reamesc609a592015-02-25 00:22:07 +0000324previous call represented by the ``gc.statepoint``.
Philip Reamesf6123222014-12-02 19:37:00 +0000325
Philip Reamesc0127282015-02-24 23:57:26 +0000326'llvm.experimental.gc.relocate' Intrinsic
327^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Philip Reamesf6123222014-12-02 19:37:00 +0000328
329Syntax:
330"""""""
331
332::
333
Philip Reamesc0127282015-02-24 23:57:26 +0000334 declare <pointer type>
335 @llvm.experimental.gc.relocate(i32 %statepoint_token,
336 i32 %base_offset,
337 i32 %pointer_offset)
Philip Reamesf6123222014-12-02 19:37:00 +0000338
339Overview:
340"""""""""
341
Philip Reamesc609a592015-02-25 00:22:07 +0000342A ``gc.relocate`` returns the potentially relocated value of a pointer
Philip Reamesdfc238b2015-01-02 19:46:49 +0000343at the safepoint.
Philip Reamesf6123222014-12-02 19:37:00 +0000344
345Operands:
346"""""""""
347
Philip Reamesc609a592015-02-25 00:22:07 +0000348The first argument is the ``gc.statepoint`` which starts the
349safepoint sequence of which this ``gc.relocation`` is a part.
Philip Reamesdfc238b2015-01-02 19:46:49 +0000350Despite the typing of this as a generic i32, *only* the value defined
Philip Reamesc609a592015-02-25 00:22:07 +0000351by a ``gc.statepoint`` is legal here.
Philip Reamesf6123222014-12-02 19:37:00 +0000352
Philip Reamesdfc238b2015-01-02 19:46:49 +0000353The second argument is an index into the statepoints list of arguments
354which specifies the base pointer for the pointer being relocated.
355This index must land within the 'gc parameter' section of the
356statepoint's argument list.
Philip Reamesf6123222014-12-02 19:37:00 +0000357
Philip Reamesdfc238b2015-01-02 19:46:49 +0000358The third argument is an index into the statepoint's list of arguments
359which specify the (potentially) derived pointer being relocated. It
360is legal for this index to be the same as the second argument
361if-and-only-if a base pointer is being relocated. This index must land
362within the 'gc parameter' section of the statepoint's argument list.
Philip Reamesf6123222014-12-02 19:37:00 +0000363
364Semantics:
365""""""""""
Philip Reamesf6123222014-12-02 19:37:00 +0000366
Philip Reamesc609a592015-02-25 00:22:07 +0000367The return value of ``gc.relocate`` is the potentially relocated value
Philip Reamesdfc238b2015-01-02 19:46:49 +0000368of the pointer specified by it's arguments. It is unspecified how the
369value of the returned pointer relates to the argument to the
Philip Reamesc609a592015-02-25 00:22:07 +0000370``gc.statepoint`` other than that a) it points to the same source
Philip Reamesdfc238b2015-01-02 19:46:49 +0000371language object with the same offset, and b) the 'based-on'
372relationship of the newly relocated pointers is a projection of the
373unrelocated pointers. In particular, the integer value of the pointer
374returned is unspecified.
375
Philip Reamesc609a592015-02-25 00:22:07 +0000376A ``gc.relocate`` is modeled as a ``readnone`` pure function. It has no
Philip Reamesdfc238b2015-01-02 19:46:49 +0000377side effects since it is just a way to extract information about work
Philip Reamesc609a592015-02-25 00:22:07 +0000378done during the actual call modeled by the ``gc.statepoint``.
Philip Reamesf6123222014-12-02 19:37:00 +0000379
Philip Reamese6625502015-02-25 23:22:43 +0000380.. _statepoint-stackmap-format:
Philip Reamesf6123222014-12-02 19:37:00 +0000381
Philip Reamesce5ff372014-12-04 00:45:23 +0000382Stack Map Format
Philip Reamesf6123222014-12-02 19:37:00 +0000383================
384
Philip Reamesdfc238b2015-01-02 19:46:49 +0000385Locations for each pointer value which may need read and/or updated by
386the runtime or collector are provided via the :ref:`Stack Map format
387<stackmap-format>` specified in the PatchPoint documentation.
Philip Reamesf6123222014-12-02 19:37:00 +0000388
389Each statepoint generates the following Locations:
390
Philip Reamesdfc238b2015-01-02 19:46:49 +0000391* Constant which describes number of following deopt *Locations* (not
392 operands)
393* Variable number of Locations, one for each deopt parameter listed in
394 the IR statepoint (same number as described by previous Constant)
395* Variable number of Locations pairs, one pair for each unique pointer
396 which needs relocated. The first Location in each pair describes
397 the base pointer for the object. The second is the derived pointer
398 actually being relocated. It is guaranteed that the base pointer
399 must also appear explicitly as a relocation pair if used after the
400 statepoint. There may be fewer pairs then gc parameters in the IR
401 statepoint. Each *unique* pair will occur at least once; duplicates
402 are possible.
Philip Reamesf6123222014-12-02 19:37:00 +0000403
Philip Reamesdfc238b2015-01-02 19:46:49 +0000404Note that the Locations used in each section may describe the same
405physical location. e.g. A stack slot may appear as a deopt location,
406a gc base pointer, and a gc derived pointer.
Philip Reamesf6123222014-12-02 19:37:00 +0000407
Philip Reamesdfc238b2015-01-02 19:46:49 +0000408The ID field of the 'StkMapRecord' for a statepoint is meaningless and
409it's value is explicitly unspecified.
Philip Reamesf6123222014-12-02 19:37:00 +0000410
Philip Reamesdfc238b2015-01-02 19:46:49 +0000411The LiveOut section of the StkMapRecord will be empty for a statepoint
412record.
Philip Reamesf6123222014-12-02 19:37:00 +0000413
414Safepoint Semantics & Verification
415==================================
416
Philip Reamesdfc238b2015-01-02 19:46:49 +0000417The fundamental correctness property for the compiled code's
418correctness w.r.t. the garbage collector is a dynamic one. It must be
419the case that there is no dynamic trace such that a operation
420involving a potentially relocated pointer is observably-after a
421safepoint which could relocate it. 'observably-after' is this usage
422means that an outside observer could observe this sequence of events
423in a way which precludes the operation being performed before the
424safepoint.
Philip Reamesf6123222014-12-02 19:37:00 +0000425
Philip Reamesdfc238b2015-01-02 19:46:49 +0000426To understand why this 'observable-after' property is required,
427consider a null comparison performed on the original copy of a
428relocated pointer. Assuming that control flow follows the safepoint,
429there is no way to observe externally whether the null comparison is
430performed before or after the safepoint. (Remember, the original
431Value is unmodified by the safepoint.) The compiler is free to make
432either scheduling choice.
Philip Reamesf6123222014-12-02 19:37:00 +0000433
Philip Reamesdfc238b2015-01-02 19:46:49 +0000434The actual correctness property implemented is slightly stronger than
435this. We require that there be no *static path* on which a
436potentially relocated pointer is 'observably-after' it may have been
437relocated. This is slightly stronger than is strictly necessary (and
438thus may disallow some otherwise valid programs), but greatly
439simplifies reasoning about correctness of the compiled code.
Philip Reamesf6123222014-12-02 19:37:00 +0000440
Philip Reamesdfc238b2015-01-02 19:46:49 +0000441By construction, this property will be upheld by the optimizer if
442correctly established in the source IR. This is a key invariant of
443the design.
Philip Reamesf6123222014-12-02 19:37:00 +0000444
Philip Reamesdfc238b2015-01-02 19:46:49 +0000445The existing IR Verifier pass has been extended to check most of the
446local restrictions on the intrinsics mentioned in their respective
447documentation. The current implementation in LLVM does not check the
448key relocation invariant, but this is ongoing work on developing such
449a verifier. Please ask on llvmdev if you're interested in
450experimenting with the current version.
Philip Reamesf6123222014-12-02 19:37:00 +0000451
Philip Reamesc88d7322015-02-25 01:23:59 +0000452.. _statepoint-utilities:
453
454Utility Passes for Safepoint Insertion
455======================================
456
457.. _RewriteStatepointsForGC:
458
459RewriteStatepointsForGC
460^^^^^^^^^^^^^^^^^^^^^^^^
461
462The pass RewriteStatepointsForGC transforms a functions IR by replacing a
463``gc.statepoint`` (with an optional ``gc.result``) with a full relocation
464sequence, including all required ``gc.relocates``. To function, the pass
465requires that the GC strategy specified for the function be able to reliably
466distinguish between GC references and non-GC references in IR it is given.
467
468As an example, given this code:
469
470.. code-block:: llvm
471
472 define i8 addrspace(1)* @test1(i8 addrspace(1)* %obj)
473 gc "statepoint-example" {
474 call i32 (void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(void ()* @foo, i32 0, i32 0, i32 5, i32 0, i32 -1, i32 0, i32 0, i32 0)
475 ret i8 addrspace(1)* %obj
476 }
477
478The pass would produce this IR:
479
480.. code-block:: llvm
481
482 define i8 addrspace(1)* @test1(i8 addrspace(1)* %obj)
483 gc "statepoint-example" {
484 %0 = call i32 (void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(void ()* @foo, i32 0, i32 0, i32 5, i32 0, i32 -1, i32 0, i32 0, i32 0, i8 addrspace(1)* %obj)
485 %obj.relocated = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(i32 %0, i32 9, i32 9)
486 ret i8 addrspace(1)* %obj.relocated
487 }
488
489In the above examples, the addrspace(1) marker on the pointers is the mechanism
490that the ``statepoint-example`` GC strategy uses to distinguish references from
491non references. Address space 1 is not globally reserved for this purpose.
492
493This pass can be used an utility function by a language frontend that doesn't
494want to manually reason about liveness, base pointers, or relocation when
495constructing IR. As currently implemented, RewriteStatepointsForGC must be
496run after SSA construction (i.e. mem2ref).
497
498
499In practice, RewriteStatepointsForGC can be run much later in the pass
500pipeline, after most optimization is already done. This helps to improve
501the quality of the generated code when compiled with garbage collection support.
502In the long run, this is the intended usage model. At this time, a few details
503have yet to be worked out about the semantic model required to guarantee this
504is always correct. As such, please use with caution and report bugs.
505
506.. _PlaceSafepoints:
507
508PlaceSafepoints
509^^^^^^^^^^^^^^^^
510
511The pass PlaceSafepoints transforms a function's IR by replacing any call or
512invoke instructions with appropriate ``gc.statepoint`` and ``gc.result`` pairs,
513and inserting safepoint polls sufficient to ensure running code checks for a
514safepoint request on a timely manner. This pass is expected to be run before
515RewriteStatepointsForGC and thus does not produce full relocation sequences.
516
Philip Reames5017ab52015-02-26 01:18:21 +0000517As an example, given input IR of the following:
518
519.. code-block:: llvm
520
521 define void @test() gc "statepoint-example" {
522 call void @foo()
523 ret void
524 }
525
526 declare void @do_safepoint()
527 define void @gc.safepoint_poll() {
528 call void @do_safepoint()
529 ret void
530 }
531
532
533This pass would produce the following IR:
534
535.. code-block:: llvm
536
537 define void @test() gc "statepoint-example" {
538 %safepoint_token = call i32 (void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(void ()* @do_safepoint, i32 0, i32 0, i32 0)
539 %safepoint_token1 = call i32 (void ()*, i32, i32, ...)* @llvm.experimental.gc.statepoint.p0f_isVoidf(void ()* @foo, i32 0, i32 0, i32 0)
540 ret void
541 }
542
543In 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.
544
545
Philip Reamesc88d7322015-02-25 01:23:59 +0000546At the moment, PlaceSafepoints can insert safepoint polls at method entry and
547loop backedges locations. Extending this to work with return polls would be
548straight forward if desired.
549
550PlaceSafepoints includes a number of optimizations to avoid placing safepoint
551polls at particular sites unless needed to ensure timely execution of a poll
552under normal conditions. PlaceSafepoints does not attempt to ensure timely
553execution of a poll under worst case conditions such as heavy system paging.
554
555The implementation of a safepoint poll action is specified by looking up a
556function of the name ``gc.safepoint_poll`` in the containing Module. The body
557of this function is inserted at each poll site desired. While calls or invokes
558inside this method are transformed to a ``gc.statepoints``, recursive poll
559insertion is not performed.
560
561If you are scheduling the RewriteStatepointsForGC pass late in the pass order,
562you should probably schedule this pass immediately before it. The exception
563would be if you need to preserve abstract frame information (e.g. for
564deoptimization or introspection) at safepoints. In that case, ask on the
565llvmdev mailing list for suggestions.
566
567
Philip Reames83331522014-12-04 18:33:28 +0000568Bugs and Enhancements
569=====================
Philip Reamesdfc238b2015-01-02 19:46:49 +0000570
571Currently known bugs and enhancements under consideration can be
572tracked by performing a `bugzilla search
573<http://llvm.org/bugs/buglist.cgi?cmdtype=runnamed&namedcmd=Statepoint%20Bugs&list_id=64342>`_
574for [Statepoint] in the summary field. When filing new bugs, please
575use this tag so that interested parties see the newly filed bug. As
576with most LLVM features, design discussions take place on `llvmdev
577<http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>`_, and patches
578should be sent to `llvm-commits
579<http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits>`_ for review.
Philip Reames83331522014-12-04 18:33:28 +0000580