xref: /aosp_15_r20/external/llvm/docs/GarbageCollection.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker=====================================
2*9880d681SAndroid Build Coastguard WorkerGarbage Collection with LLVM
3*9880d681SAndroid Build Coastguard Worker=====================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerAbstract
9*9880d681SAndroid Build Coastguard Worker========
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerThis document covers how to integrate LLVM into a compiler for a language which
12*9880d681SAndroid Build Coastguard Workersupports garbage collection.  **Note that LLVM itself does not provide a
13*9880d681SAndroid Build Coastguard Workergarbage collector.**  You must provide your own.
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard WorkerQuick Start
16*9880d681SAndroid Build Coastguard Worker============
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard WorkerFirst, you should pick a collector strategy.  LLVM includes a number of built
19*9880d681SAndroid Build Coastguard Workerin ones, but you can also implement a loadable plugin with a custom definition.
20*9880d681SAndroid Build Coastguard WorkerNote that the collector strategy is a description of how LLVM should generate
21*9880d681SAndroid Build Coastguard Workercode such that it interacts with your collector and runtime, not a description
22*9880d681SAndroid Build Coastguard Workerof the collector itself.
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard WorkerNext, mark your generated functions as using your chosen collector strategy.
25*9880d681SAndroid Build Coastguard WorkerFrom c++, you can call:
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker  F.setGC(<collector description name>);
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard WorkerThis will produce IR like the following fragment:
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker  define void @foo() gc "<collector description name>" { ... }
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard WorkerWhen generating LLVM IR for your functions, you will need to:
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker* Use ``@llvm.gcread`` and/or ``@llvm.gcwrite`` in place of standard load and
42*9880d681SAndroid Build Coastguard Worker  store instructions.  These intrinsics are used to represent load and store
43*9880d681SAndroid Build Coastguard Worker  barriers.  If you collector does not require such barriers, you can skip
44*9880d681SAndroid Build Coastguard Worker  this step.
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker* Use the memory allocation routines provided by your garbage collector's
47*9880d681SAndroid Build Coastguard Worker  runtime library.
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker* If your collector requires them, generate type maps according to your
50*9880d681SAndroid Build Coastguard Worker  runtime's binary interface.  LLVM is not involved in the process.  In
51*9880d681SAndroid Build Coastguard Worker  particular, the LLVM type system is not suitable for conveying such
52*9880d681SAndroid Build Coastguard Worker  information though the compiler.
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker* Insert any coordination code required for interacting with your collector.
55*9880d681SAndroid Build Coastguard Worker  Many collectors require running application code to periodically check a
56*9880d681SAndroid Build Coastguard Worker  flag and conditionally call a runtime function.  This is often referred to
57*9880d681SAndroid Build Coastguard Worker  as a safepoint poll.
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard WorkerYou will need to identify roots (i.e. references to heap objects your collector
60*9880d681SAndroid Build Coastguard Workerneeds to know about) in your generated IR, so that LLVM can encode them into
61*9880d681SAndroid Build Coastguard Workeryour final stack maps.  Depending on the collector strategy chosen, this is
62*9880d681SAndroid Build Coastguard Workeraccomplished by using either the ``@llvm.gcroot`` intrinsics or an
63*9880d681SAndroid Build Coastguard Worker``gc.statepoint`` relocation sequence.
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard WorkerDon't forget to create a root for each intermediate value that is generated when
66*9880d681SAndroid Build Coastguard Workerevaluating an expression.  In ``h(f(), g())``, the result of ``f()`` could
67*9880d681SAndroid Build Coastguard Workereasily be collected if evaluating ``g()`` triggers a collection.
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard WorkerFinally, you need to link your runtime library with the generated program
70*9880d681SAndroid Build Coastguard Workerexecutable (for a static compiler) or ensure the appropriate symbols are
71*9880d681SAndroid Build Coastguard Workeravailable for the runtime linker (for a JIT compiler).
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard WorkerIntroduction
75*9880d681SAndroid Build Coastguard Worker============
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard WorkerWhat is Garbage Collection?
78*9880d681SAndroid Build Coastguard Worker---------------------------
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard WorkerGarbage collection is a widely used technique that frees the programmer from
81*9880d681SAndroid Build Coastguard Workerhaving to know the lifetimes of heap objects, making software easier to produce
82*9880d681SAndroid Build Coastguard Workerand maintain.  Many programming languages rely on garbage collection for
83*9880d681SAndroid Build Coastguard Workerautomatic memory management.  There are two primary forms of garbage collection:
84*9880d681SAndroid Build Coastguard Workerconservative and accurate.
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard WorkerConservative garbage collection often does not require any special support from
87*9880d681SAndroid Build Coastguard Workereither the language or the compiler: it can handle non-type-safe programming
88*9880d681SAndroid Build Coastguard Workerlanguages (such as C/C++) and does not require any special information from the
89*9880d681SAndroid Build Coastguard Workercompiler.  The `Boehm collector
90*9880d681SAndroid Build Coastguard Worker<http://www.hpl.hp.com/personal/Hans_Boehm/gc/>`__ is an example of a
91*9880d681SAndroid Build Coastguard Workerstate-of-the-art conservative collector.
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard WorkerAccurate garbage collection requires the ability to identify all pointers in the
94*9880d681SAndroid Build Coastguard Workerprogram at run-time (which requires that the source-language be type-safe in
95*9880d681SAndroid Build Coastguard Workermost cases).  Identifying pointers at run-time requires compiler support to
96*9880d681SAndroid Build Coastguard Workerlocate all places that hold live pointer variables at run-time, including the
97*9880d681SAndroid Build Coastguard Worker:ref:`processor stack and registers <gcroot>`.
98*9880d681SAndroid Build Coastguard Worker
99*9880d681SAndroid Build Coastguard WorkerConservative garbage collection is attractive because it does not require any
100*9880d681SAndroid Build Coastguard Workerspecial compiler support, but it does have problems.  In particular, because the
101*9880d681SAndroid Build Coastguard Workerconservative garbage collector cannot *know* that a particular word in the
102*9880d681SAndroid Build Coastguard Workermachine is a pointer, it cannot move live objects in the heap (preventing the
103*9880d681SAndroid Build Coastguard Workeruse of compacting and generational GC algorithms) and it can occasionally suffer
104*9880d681SAndroid Build Coastguard Workerfrom memory leaks due to integer values that happen to point to objects in the
105*9880d681SAndroid Build Coastguard Workerprogram.  In addition, some aggressive compiler transformations can break
106*9880d681SAndroid Build Coastguard Workerconservative garbage collectors (though these seem rare in practice).
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard WorkerAccurate garbage collectors do not suffer from any of these problems, but they
109*9880d681SAndroid Build Coastguard Workercan suffer from degraded scalar optimization of the program.  In particular,
110*9880d681SAndroid Build Coastguard Workerbecause the runtime must be able to identify and update all pointers active in
111*9880d681SAndroid Build Coastguard Workerthe program, some optimizations are less effective.  In practice, however, the
112*9880d681SAndroid Build Coastguard Workerlocality and performance benefits of using aggressive garbage collection
113*9880d681SAndroid Build Coastguard Workertechniques dominates any low-level losses.
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard WorkerThis document describes the mechanisms and interfaces provided by LLVM to
116*9880d681SAndroid Build Coastguard Workersupport accurate garbage collection.
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard WorkerGoals and non-goals
119*9880d681SAndroid Build Coastguard Worker-------------------
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard WorkerLLVM's intermediate representation provides :ref:`garbage collection intrinsics
122*9880d681SAndroid Build Coastguard Worker<gc_intrinsics>` that offer support for a broad class of collector models.  For
123*9880d681SAndroid Build Coastguard Workerinstance, the intrinsics permit:
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker* semi-space collectors
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker* mark-sweep collectors
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker* generational collectors
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker* incremental collectors
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker* concurrent collectors
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker* cooperative collectors
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker* reference counting
138*9880d681SAndroid Build Coastguard Worker
139*9880d681SAndroid Build Coastguard WorkerWe hope that the support built into the LLVM IR is sufficient to support a
140*9880d681SAndroid Build Coastguard Workerbroad class of garbage collected languages including Scheme, ML, Java, C#,
141*9880d681SAndroid Build Coastguard WorkerPerl, Python, Lua, Ruby, other scripting languages, and more.
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard WorkerNote that LLVM **does not itself provide a garbage collector** --- this should
144*9880d681SAndroid Build Coastguard Workerbe part of your language's runtime library.  LLVM provides a framework for
145*9880d681SAndroid Build Coastguard Workerdescribing the garbage collectors requirements to the compiler.  In particular,
146*9880d681SAndroid Build Coastguard WorkerLLVM provides support for generating stack maps at call sites, polling for a
147*9880d681SAndroid Build Coastguard Workersafepoint, and emitting load and store barriers.  You can also extend LLVM -
148*9880d681SAndroid Build Coastguard Workerpossibly through a loadable :ref:`code generation plugins <plugin>` - to
149*9880d681SAndroid Build Coastguard Workergenerate code and data structures which conforms to the *binary interface*
150*9880d681SAndroid Build Coastguard Workerspecified by the *runtime library*.  This is similar to the relationship between
151*9880d681SAndroid Build Coastguard WorkerLLVM and DWARF debugging info, for example.  The difference primarily lies in
152*9880d681SAndroid Build Coastguard Workerthe lack of an established standard in the domain of garbage collection --- thus
153*9880d681SAndroid Build Coastguard Workerthe need for a flexible extension mechanism.
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard WorkerThe aspects of the binary interface with which LLVM's GC support is
156*9880d681SAndroid Build Coastguard Workerconcerned are:
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker* Creation of GC safepoints within code where collection is allowed to execute
159*9880d681SAndroid Build Coastguard Worker  safely.
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker* Computation of the stack map.  For each safe point in the code, object
162*9880d681SAndroid Build Coastguard Worker  references within the stack frame must be identified so that the collector may
163*9880d681SAndroid Build Coastguard Worker  traverse and perhaps update them.
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker* Write barriers when storing object references to the heap.  These are commonly
166*9880d681SAndroid Build Coastguard Worker  used to optimize incremental scans in generational collectors.
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker* Emission of read barriers when loading object references.  These are useful
169*9880d681SAndroid Build Coastguard Worker  for interoperating with concurrent collectors.
170*9880d681SAndroid Build Coastguard Worker
171*9880d681SAndroid Build Coastguard WorkerThere are additional areas that LLVM does not directly address:
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker* Registration of global roots with the runtime.
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker* Registration of stack map entries with the runtime.
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker* The functions used by the program to allocate memory, trigger a collection,
178*9880d681SAndroid Build Coastguard Worker  etc.
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker* Computation or compilation of type maps, or registration of them with the
181*9880d681SAndroid Build Coastguard Worker  runtime.  These are used to crawl the heap for object references.
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard WorkerIn general, LLVM's support for GC does not include features which can be
184*9880d681SAndroid Build Coastguard Workeradequately addressed with other features of the IR and does not specify a
185*9880d681SAndroid Build Coastguard Workerparticular binary interface.  On the plus side, this means that you should be
186*9880d681SAndroid Build Coastguard Workerable to integrate LLVM with an existing runtime.  On the other hand, it can
187*9880d681SAndroid Build Coastguard Workerhave the effect of leaving a lot of work for the developer of a novel
188*9880d681SAndroid Build Coastguard Workerlanguage.  We try to mitigate this by providing built in collector strategy
189*9880d681SAndroid Build Coastguard Workerdescriptions that can work with many common collector designs and easy
190*9880d681SAndroid Build Coastguard Workerextension points.  If you don't already have a specific binary interface
191*9880d681SAndroid Build Coastguard Workeryou need to support, we recommend trying to use one of these built in collector
192*9880d681SAndroid Build Coastguard Workerstrategies.
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker.. _gc_intrinsics:
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard WorkerLLVM IR Features
197*9880d681SAndroid Build Coastguard Worker================
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard WorkerThis section describes the garbage collection facilities provided by the
200*9880d681SAndroid Build Coastguard Worker:doc:`LLVM intermediate representation <LangRef>`.  The exact behavior of these
201*9880d681SAndroid Build Coastguard WorkerIR features is specified by the selected :ref:`GC strategy description
202*9880d681SAndroid Build Coastguard Worker<plugin>`.
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard WorkerSpecifying GC code generation: ``gc "..."``
205*9880d681SAndroid Build Coastguard Worker-------------------------------------------
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker  define <returntype> @name(...) gc "name" { ... }
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard WorkerThe ``gc`` function attribute is used to specify the desired GC strategy to the
212*9880d681SAndroid Build Coastguard Workercompiler.  Its programmatic equivalent is the ``setGC`` method of ``Function``.
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard WorkerSetting ``gc "name"`` on a function triggers a search for a matching subclass
215*9880d681SAndroid Build Coastguard Workerof GCStrategy.  Some collector strategies are built in.  You can add others
216*9880d681SAndroid Build Coastguard Workerusing either the loadable plugin mechanism, or by patching your copy of LLVM.
217*9880d681SAndroid Build Coastguard WorkerIt is the selected GC strategy which defines the exact nature of the code
218*9880d681SAndroid Build Coastguard Workergenerated to support GC.  If none is found, the compiler will raise an error.
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard WorkerSpecifying the GC style on a per-function basis allows LLVM to link together
221*9880d681SAndroid Build Coastguard Workerprograms that use different garbage collection algorithms (or none at all).
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard Worker.. _gcroot:
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard WorkerIdentifying GC roots on the stack
226*9880d681SAndroid Build Coastguard Worker----------------------------------
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard WorkerLLVM currently supports two different mechanisms for describing references in
229*9880d681SAndroid Build Coastguard Workercompiled code at safepoints.  ``llvm.gcroot`` is the older mechanism;
230*9880d681SAndroid Build Coastguard Worker``gc.statepoint`` has been added more recently.  At the moment, you can choose
231*9880d681SAndroid Build Coastguard Workereither implementation (on a per :ref:`GC strategy <plugin>` basis).  Longer
232*9880d681SAndroid Build Coastguard Workerterm, we will probably either migrate away from ``llvm.gcroot`` entirely, or
233*9880d681SAndroid Build Coastguard Workersubstantially merge their implementations. Note that most new development
234*9880d681SAndroid Build Coastguard Workerwork is focused on ``gc.statepoint``.
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard WorkerUsing ``gc.statepoint``
237*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^
238*9880d681SAndroid Build Coastguard Worker:doc:`This page <Statepoints>` contains detailed documentation for
239*9880d681SAndroid Build Coastguard Worker``gc.statepoint``.
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard WorkerUsing ``llvm.gcwrite``
242*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker  void @llvm.gcroot(i8** %ptrloc, i8* %metadata)
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard WorkerThe ``llvm.gcroot`` intrinsic is used to inform LLVM that a stack variable
249*9880d681SAndroid Build Coastguard Workerreferences an object on the heap and is to be tracked for garbage collection.
250*9880d681SAndroid Build Coastguard WorkerThe exact impact on generated code is specified by the Function's selected
251*9880d681SAndroid Build Coastguard Worker:ref:`GC strategy <plugin>`.  All calls to ``llvm.gcroot`` **must** reside
252*9880d681SAndroid Build Coastguard Workerinside the first basic block.
253*9880d681SAndroid Build Coastguard Worker
254*9880d681SAndroid Build Coastguard WorkerThe first argument **must** be a value referring to an alloca instruction or a
255*9880d681SAndroid Build Coastguard Workerbitcast of an alloca.  The second contains a pointer to metadata that should be
256*9880d681SAndroid Build Coastguard Workerassociated with the pointer, and **must** be a constant or global value
257*9880d681SAndroid Build Coastguard Workeraddress.  If your target collector uses tags, use a null pointer for metadata.
258*9880d681SAndroid Build Coastguard Worker
259*9880d681SAndroid Build Coastguard WorkerA compiler which performs manual SSA construction **must** ensure that SSA
260*9880d681SAndroid Build Coastguard Workervalues representing GC references are stored in to the alloca passed to the
261*9880d681SAndroid Build Coastguard Workerrespective ``gcroot`` before every call site and reloaded after every call.
262*9880d681SAndroid Build Coastguard WorkerA compiler which uses mem2reg to raise imperative code using ``alloca`` into
263*9880d681SAndroid Build Coastguard WorkerSSA form need only add a call to ``@llvm.gcroot`` for those variables which
264*9880d681SAndroid Build Coastguard Workerare pointers into the GC heap.
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard WorkerIt is also important to mark intermediate values with ``llvm.gcroot``.  For
267*9880d681SAndroid Build Coastguard Workerexample, consider ``h(f(), g())``.  Beware leaking the result of ``f()`` in the
268*9880d681SAndroid Build Coastguard Workercase that ``g()`` triggers a collection.  Note, that stack variables must be
269*9880d681SAndroid Build Coastguard Workerinitialized and marked with ``llvm.gcroot`` in function's prologue.
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard WorkerThe ``%metadata`` argument can be used to avoid requiring heap objects to have
272*9880d681SAndroid Build Coastguard Worker'isa' pointers or tag bits. [Appel89_, Goldberg91_, Tolmach94_] If specified,
273*9880d681SAndroid Build Coastguard Workerits value will be tracked along with the location of the pointer in the stack
274*9880d681SAndroid Build Coastguard Workerframe.
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard WorkerConsider the following fragment of Java code:
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard Worker.. code-block:: java
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker   {
281*9880d681SAndroid Build Coastguard Worker     Object X;   // A null-initialized reference to an object
282*9880d681SAndroid Build Coastguard Worker     ...
283*9880d681SAndroid Build Coastguard Worker   }
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard WorkerThis block (which may be located in the middle of a function or in a loop nest),
286*9880d681SAndroid Build Coastguard Workercould be compiled to this LLVM code:
287*9880d681SAndroid Build Coastguard Worker
288*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
289*9880d681SAndroid Build Coastguard Worker
290*9880d681SAndroid Build Coastguard Worker  Entry:
291*9880d681SAndroid Build Coastguard Worker     ;; In the entry block for the function, allocate the
292*9880d681SAndroid Build Coastguard Worker     ;; stack space for X, which is an LLVM pointer.
293*9880d681SAndroid Build Coastguard Worker     %X = alloca %Object*
294*9880d681SAndroid Build Coastguard Worker
295*9880d681SAndroid Build Coastguard Worker     ;; Tell LLVM that the stack space is a stack root.
296*9880d681SAndroid Build Coastguard Worker     ;; Java has type-tags on objects, so we pass null as metadata.
297*9880d681SAndroid Build Coastguard Worker     %tmp = bitcast %Object** %X to i8**
298*9880d681SAndroid Build Coastguard Worker     call void @llvm.gcroot(i8** %tmp, i8* null)
299*9880d681SAndroid Build Coastguard Worker     ...
300*9880d681SAndroid Build Coastguard Worker
301*9880d681SAndroid Build Coastguard Worker     ;; "CodeBlock" is the block corresponding to the start
302*9880d681SAndroid Build Coastguard Worker     ;;  of the scope above.
303*9880d681SAndroid Build Coastguard Worker  CodeBlock:
304*9880d681SAndroid Build Coastguard Worker     ;; Java null-initializes pointers.
305*9880d681SAndroid Build Coastguard Worker     store %Object* null, %Object** %X
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker     ...
308*9880d681SAndroid Build Coastguard Worker
309*9880d681SAndroid Build Coastguard Worker     ;; As the pointer goes out of scope, store a null value into
310*9880d681SAndroid Build Coastguard Worker     ;; it, to indicate that the value is no longer live.
311*9880d681SAndroid Build Coastguard Worker     store %Object* null, %Object** %X
312*9880d681SAndroid Build Coastguard Worker     ...
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard WorkerReading and writing references in the heap
315*9880d681SAndroid Build Coastguard Worker------------------------------------------
316*9880d681SAndroid Build Coastguard Worker
317*9880d681SAndroid Build Coastguard WorkerSome collectors need to be informed when the mutator (the program that needs
318*9880d681SAndroid Build Coastguard Workergarbage collection) either reads a pointer from or writes a pointer to a field
319*9880d681SAndroid Build Coastguard Workerof a heap object.  The code fragments inserted at these points are called *read
320*9880d681SAndroid Build Coastguard Workerbarriers* and *write barriers*, respectively.  The amount of code that needs to
321*9880d681SAndroid Build Coastguard Workerbe executed is usually quite small and not on the critical path of any
322*9880d681SAndroid Build Coastguard Workercomputation, so the overall performance impact of the barrier is tolerable.
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard WorkerBarriers often require access to the *object pointer* rather than the *derived
325*9880d681SAndroid Build Coastguard Workerpointer* (which is a pointer to the field within the object).  Accordingly,
326*9880d681SAndroid Build Coastguard Workerthese intrinsics take both pointers as separate arguments for completeness.  In
327*9880d681SAndroid Build Coastguard Workerthis snippet, ``%object`` is the object pointer, and ``%derived`` is the derived
328*9880d681SAndroid Build Coastguard Workerpointer:
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
331*9880d681SAndroid Build Coastguard Worker
332*9880d681SAndroid Build Coastguard Worker  ;; An array type.
333*9880d681SAndroid Build Coastguard Worker  %class.Array = type { %class.Object, i32, [0 x %class.Object*] }
334*9880d681SAndroid Build Coastguard Worker  ...
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker  ;; Load the object pointer from a gcroot.
337*9880d681SAndroid Build Coastguard Worker  %object = load %class.Array** %object_addr
338*9880d681SAndroid Build Coastguard Worker
339*9880d681SAndroid Build Coastguard Worker  ;; Compute the derived pointer.
340*9880d681SAndroid Build Coastguard Worker  %derived = getelementptr %object, i32 0, i32 2, i32 %n
341*9880d681SAndroid Build Coastguard Worker
342*9880d681SAndroid Build Coastguard WorkerLLVM does not enforce this relationship between the object and derived pointer
343*9880d681SAndroid Build Coastguard Worker(although a particular :ref:`collector strategy <plugin>` might).  However, it
344*9880d681SAndroid Build Coastguard Workerwould be an unusual collector that violated it.
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard WorkerThe use of these intrinsics is naturally optional if the target GC does not
347*9880d681SAndroid Build Coastguard Workerrequire the corresponding barrier.  The GC strategy used with such a collector
348*9880d681SAndroid Build Coastguard Workershould replace the intrinsic calls with the corresponding ``load`` or
349*9880d681SAndroid Build Coastguard Worker``store`` instruction if they are used.
350*9880d681SAndroid Build Coastguard Worker
351*9880d681SAndroid Build Coastguard WorkerOne known deficiency with the current design is that the barrier intrinsics do
352*9880d681SAndroid Build Coastguard Workernot include the size or alignment of the underlying operation performed.  It is
353*9880d681SAndroid Build Coastguard Workercurrently assumed that the operation is of pointer size and the alignment is
354*9880d681SAndroid Build Coastguard Workerassumed to be the target machine's default alignment.
355*9880d681SAndroid Build Coastguard Worker
356*9880d681SAndroid Build Coastguard WorkerWrite barrier: ``llvm.gcwrite``
357*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
358*9880d681SAndroid Build Coastguard Worker
359*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard Worker  void @llvm.gcwrite(i8* %value, i8* %object, i8** %derived)
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard WorkerFor write barriers, LLVM provides the ``llvm.gcwrite`` intrinsic function.  It
364*9880d681SAndroid Build Coastguard Workerhas exactly the same semantics as a non-volatile ``store`` to the derived
365*9880d681SAndroid Build Coastguard Workerpointer (the third argument).  The exact code generated is specified by the
366*9880d681SAndroid Build Coastguard WorkerFunction's selected :ref:`GC strategy <plugin>`.
367*9880d681SAndroid Build Coastguard Worker
368*9880d681SAndroid Build Coastguard WorkerMany important algorithms require write barriers, including generational and
369*9880d681SAndroid Build Coastguard Workerconcurrent collectors.  Additionally, write barriers could be used to implement
370*9880d681SAndroid Build Coastguard Workerreference counting.
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard WorkerRead barrier: ``llvm.gcread``
373*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
374*9880d681SAndroid Build Coastguard Worker
375*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
376*9880d681SAndroid Build Coastguard Worker
377*9880d681SAndroid Build Coastguard Worker  i8* @llvm.gcread(i8* %object, i8** %derived)
378*9880d681SAndroid Build Coastguard Worker
379*9880d681SAndroid Build Coastguard WorkerFor read barriers, LLVM provides the ``llvm.gcread`` intrinsic function.  It has
380*9880d681SAndroid Build Coastguard Workerexactly the same semantics as a non-volatile ``load`` from the derived pointer
381*9880d681SAndroid Build Coastguard Worker(the second argument).  The exact code generated is specified by the Function's
382*9880d681SAndroid Build Coastguard Workerselected :ref:`GC strategy <plugin>`.
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard WorkerRead barriers are needed by fewer algorithms than write barriers, and may have a
385*9880d681SAndroid Build Coastguard Workergreater performance impact since pointer reads are more frequent than writes.
386*9880d681SAndroid Build Coastguard Worker
387*9880d681SAndroid Build Coastguard Worker.. _plugin:
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard Worker.. _builtin-gc-strategies:
390*9880d681SAndroid Build Coastguard Worker
391*9880d681SAndroid Build Coastguard WorkerBuilt In GC Strategies
392*9880d681SAndroid Build Coastguard Worker======================
393*9880d681SAndroid Build Coastguard Worker
394*9880d681SAndroid Build Coastguard WorkerLLVM includes built in support for several varieties of garbage collectors.
395*9880d681SAndroid Build Coastguard Worker
396*9880d681SAndroid Build Coastguard WorkerThe Shadow Stack GC
397*9880d681SAndroid Build Coastguard Worker----------------------
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard WorkerTo use this collector strategy, mark your functions with:
400*9880d681SAndroid Build Coastguard Worker
401*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
402*9880d681SAndroid Build Coastguard Worker
403*9880d681SAndroid Build Coastguard Worker  F.setGC("shadow-stack");
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard WorkerUnlike many GC algorithms which rely on a cooperative code generator to compile
406*9880d681SAndroid Build Coastguard Workerstack maps, this algorithm carefully maintains a linked list of stack roots
407*9880d681SAndroid Build Coastguard Worker[:ref:`Henderson2002 <henderson02>`].  This so-called "shadow stack" mirrors the
408*9880d681SAndroid Build Coastguard Workermachine stack.  Maintaining this data structure is slower than using a stack map
409*9880d681SAndroid Build Coastguard Workercompiled into the executable as constant data, but has a significant portability
410*9880d681SAndroid Build Coastguard Workeradvantage because it requires no special support from the target code generator,
411*9880d681SAndroid Build Coastguard Workerand does not require tricky platform-specific code to crawl the machine stack.
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard WorkerThe tradeoff for this simplicity and portability is:
414*9880d681SAndroid Build Coastguard Worker
415*9880d681SAndroid Build Coastguard Worker* High overhead per function call.
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker* Not thread-safe.
418*9880d681SAndroid Build Coastguard Worker
419*9880d681SAndroid Build Coastguard WorkerStill, it's an easy way to get started.  After your compiler and runtime are up
420*9880d681SAndroid Build Coastguard Workerand running, writing a :ref:`plugin <plugin>` will allow you to take advantage
421*9880d681SAndroid Build Coastguard Workerof :ref:`more advanced GC features <collector-algos>` of LLVM in order to
422*9880d681SAndroid Build Coastguard Workerimprove performance.
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard Worker
425*9880d681SAndroid Build Coastguard WorkerThe shadow stack doesn't imply a memory allocation algorithm.  A semispace
426*9880d681SAndroid Build Coastguard Workercollector or building atop ``malloc`` are great places to start, and can be
427*9880d681SAndroid Build Coastguard Workerimplemented with very little code.
428*9880d681SAndroid Build Coastguard Worker
429*9880d681SAndroid Build Coastguard WorkerWhen it comes time to collect, however, your runtime needs to traverse the stack
430*9880d681SAndroid Build Coastguard Workerroots, and for this it needs to integrate with the shadow stack.  Luckily, doing
431*9880d681SAndroid Build Coastguard Workerso is very simple. (This code is heavily commented to help you understand the
432*9880d681SAndroid Build Coastguard Workerdata structure, but there are only 20 lines of meaningful code.)
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard Worker  /// @brief The map for a single function's stack frame.  One of these is
437*9880d681SAndroid Build Coastguard Worker  ///        compiled as constant data into the executable for each function.
438*9880d681SAndroid Build Coastguard Worker  ///
439*9880d681SAndroid Build Coastguard Worker  /// Storage of metadata values is elided if the %metadata parameter to
440*9880d681SAndroid Build Coastguard Worker  /// @llvm.gcroot is null.
441*9880d681SAndroid Build Coastguard Worker  struct FrameMap {
442*9880d681SAndroid Build Coastguard Worker    int32_t NumRoots;    //< Number of roots in stack frame.
443*9880d681SAndroid Build Coastguard Worker    int32_t NumMeta;     //< Number of metadata entries.  May be < NumRoots.
444*9880d681SAndroid Build Coastguard Worker    const void *Meta[0]; //< Metadata for each root.
445*9880d681SAndroid Build Coastguard Worker  };
446*9880d681SAndroid Build Coastguard Worker
447*9880d681SAndroid Build Coastguard Worker  /// @brief A link in the dynamic shadow stack.  One of these is embedded in
448*9880d681SAndroid Build Coastguard Worker  ///        the stack frame of each function on the call stack.
449*9880d681SAndroid Build Coastguard Worker  struct StackEntry {
450*9880d681SAndroid Build Coastguard Worker    StackEntry *Next;    //< Link to next stack entry (the caller's).
451*9880d681SAndroid Build Coastguard Worker    const FrameMap *Map; //< Pointer to constant FrameMap.
452*9880d681SAndroid Build Coastguard Worker    void *Roots[0];      //< Stack roots (in-place array).
453*9880d681SAndroid Build Coastguard Worker  };
454*9880d681SAndroid Build Coastguard Worker
455*9880d681SAndroid Build Coastguard Worker  /// @brief The head of the singly-linked list of StackEntries.  Functions push
456*9880d681SAndroid Build Coastguard Worker  ///        and pop onto this in their prologue and epilogue.
457*9880d681SAndroid Build Coastguard Worker  ///
458*9880d681SAndroid Build Coastguard Worker  /// Since there is only a global list, this technique is not threadsafe.
459*9880d681SAndroid Build Coastguard Worker  StackEntry *llvm_gc_root_chain;
460*9880d681SAndroid Build Coastguard Worker
461*9880d681SAndroid Build Coastguard Worker  /// @brief Calls Visitor(root, meta) for each GC root on the stack.
462*9880d681SAndroid Build Coastguard Worker  ///        root and meta are exactly the values passed to
463*9880d681SAndroid Build Coastguard Worker  ///        @llvm.gcroot.
464*9880d681SAndroid Build Coastguard Worker  ///
465*9880d681SAndroid Build Coastguard Worker  /// Visitor could be a function to recursively mark live objects.  Or it
466*9880d681SAndroid Build Coastguard Worker  /// might copy them to another heap or generation.
467*9880d681SAndroid Build Coastguard Worker  ///
468*9880d681SAndroid Build Coastguard Worker  /// @param Visitor A function to invoke for every GC root on the stack.
469*9880d681SAndroid Build Coastguard Worker  void visitGCRoots(void (*Visitor)(void **Root, const void *Meta)) {
470*9880d681SAndroid Build Coastguard Worker    for (StackEntry *R = llvm_gc_root_chain; R; R = R->Next) {
471*9880d681SAndroid Build Coastguard Worker      unsigned i = 0;
472*9880d681SAndroid Build Coastguard Worker
473*9880d681SAndroid Build Coastguard Worker      // For roots [0, NumMeta), the metadata pointer is in the FrameMap.
474*9880d681SAndroid Build Coastguard Worker      for (unsigned e = R->Map->NumMeta; i != e; ++i)
475*9880d681SAndroid Build Coastguard Worker        Visitor(&R->Roots[i], R->Map->Meta[i]);
476*9880d681SAndroid Build Coastguard Worker
477*9880d681SAndroid Build Coastguard Worker      // For roots [NumMeta, NumRoots), the metadata pointer is null.
478*9880d681SAndroid Build Coastguard Worker      for (unsigned e = R->Map->NumRoots; i != e; ++i)
479*9880d681SAndroid Build Coastguard Worker        Visitor(&R->Roots[i], NULL);
480*9880d681SAndroid Build Coastguard Worker    }
481*9880d681SAndroid Build Coastguard Worker  }
482*9880d681SAndroid Build Coastguard Worker
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard WorkerThe 'Erlang' and 'Ocaml' GCs
485*9880d681SAndroid Build Coastguard Worker-----------------------------
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard WorkerLLVM ships with two example collectors which leverage the ``gcroot``
488*9880d681SAndroid Build Coastguard Workermechanisms.  To our knowledge, these are not actually used by any language
489*9880d681SAndroid Build Coastguard Workerruntime, but they do provide a reasonable starting point for someone interested
490*9880d681SAndroid Build Coastguard Workerin writing an ``gcroot`` compatible GC plugin.  In particular, these are the
491*9880d681SAndroid Build Coastguard Workeronly in tree examples of how to produce a custom binary stack map format using
492*9880d681SAndroid Build Coastguard Workera ``gcroot`` strategy.
493*9880d681SAndroid Build Coastguard Worker
494*9880d681SAndroid Build Coastguard WorkerAs there names imply, the binary format produced is intended to model that
495*9880d681SAndroid Build Coastguard Workerused by the Erlang and OCaml compilers respectively.
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard Worker.. _statepoint_example_gc:
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard WorkerThe Statepoint Example GC
500*9880d681SAndroid Build Coastguard Worker-------------------------
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
503*9880d681SAndroid Build Coastguard Worker
504*9880d681SAndroid Build Coastguard Worker  F.setGC("statepoint-example");
505*9880d681SAndroid Build Coastguard Worker
506*9880d681SAndroid Build Coastguard WorkerThis GC provides an example of how one might use the infrastructure provided
507*9880d681SAndroid Build Coastguard Workerby ``gc.statepoint``. This example GC is compatible with the
508*9880d681SAndroid Build Coastguard Worker:ref:`PlaceSafepoints` and :ref:`RewriteStatepointsForGC` utility passes
509*9880d681SAndroid Build Coastguard Workerwhich simplify ``gc.statepoint`` sequence insertion. If you need to build a
510*9880d681SAndroid Build Coastguard Workercustom GC strategy around the ``gc.statepoints`` mechanisms, it is recommended
511*9880d681SAndroid Build Coastguard Workerthat you use this one as a starting point.
512*9880d681SAndroid Build Coastguard Worker
513*9880d681SAndroid Build Coastguard WorkerThis GC strategy does not support read or write barriers.  As a result, these
514*9880d681SAndroid Build Coastguard Workerintrinsics are lowered to normal loads and stores.
515*9880d681SAndroid Build Coastguard Worker
516*9880d681SAndroid Build Coastguard WorkerThe stack map format generated by this GC strategy can be found in the
517*9880d681SAndroid Build Coastguard Worker:ref:`stackmap-section` using a format documented :ref:`here
518*9880d681SAndroid Build Coastguard Worker<statepoint-stackmap-format>`. This format is intended to be the standard
519*9880d681SAndroid Build Coastguard Workerformat supported by LLVM going forward.
520*9880d681SAndroid Build Coastguard Worker
521*9880d681SAndroid Build Coastguard WorkerThe CoreCLR GC
522*9880d681SAndroid Build Coastguard Worker-------------------------
523*9880d681SAndroid Build Coastguard Worker
524*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
525*9880d681SAndroid Build Coastguard Worker
526*9880d681SAndroid Build Coastguard Worker  F.setGC("coreclr");
527*9880d681SAndroid Build Coastguard Worker
528*9880d681SAndroid Build Coastguard WorkerThis GC leverages the ``gc.statepoint`` mechanism to support the
529*9880d681SAndroid Build Coastguard Worker`CoreCLR <https://github.com/dotnet/coreclr>`__ runtime.
530*9880d681SAndroid Build Coastguard Worker
531*9880d681SAndroid Build Coastguard WorkerSupport for this GC strategy is a work in progress. This strategy will
532*9880d681SAndroid Build Coastguard Workerdiffer from
533*9880d681SAndroid Build Coastguard Worker:ref:`statepoint-example GC<statepoint_example_gc>` strategy in
534*9880d681SAndroid Build Coastguard Workercertain aspects like:
535*9880d681SAndroid Build Coastguard Worker
536*9880d681SAndroid Build Coastguard Worker* Base-pointers of interior pointers are not explicitly
537*9880d681SAndroid Build Coastguard Worker  tracked and reported.
538*9880d681SAndroid Build Coastguard Worker
539*9880d681SAndroid Build Coastguard Worker* A different format is used for encoding stack maps.
540*9880d681SAndroid Build Coastguard Worker
541*9880d681SAndroid Build Coastguard Worker* Safe-point polls are only needed before loop-back edges
542*9880d681SAndroid Build Coastguard Worker  and before tail-calls (not needed at function-entry).
543*9880d681SAndroid Build Coastguard Worker
544*9880d681SAndroid Build Coastguard WorkerCustom GC Strategies
545*9880d681SAndroid Build Coastguard Worker====================
546*9880d681SAndroid Build Coastguard Worker
547*9880d681SAndroid Build Coastguard WorkerIf none of the built in GC strategy descriptions met your needs above, you will
548*9880d681SAndroid Build Coastguard Workerneed to define a custom GCStrategy and possibly, a custom LLVM pass to perform
549*9880d681SAndroid Build Coastguard Workerlowering.  Your best example of where to start defining a custom GCStrategy
550*9880d681SAndroid Build Coastguard Workerwould be to look at one of the built in strategies.
551*9880d681SAndroid Build Coastguard Worker
552*9880d681SAndroid Build Coastguard WorkerYou may be able to structure this additional code as a loadable plugin library.
553*9880d681SAndroid Build Coastguard WorkerLoadable plugins are sufficient if all you need is to enable a different
554*9880d681SAndroid Build Coastguard Workercombination of built in functionality, but if you need to provide a custom
555*9880d681SAndroid Build Coastguard Workerlowering pass, you will need to build a patched version of LLVM.  If you think
556*9880d681SAndroid Build Coastguard Workeryou need a patched build, please ask for advice on llvm-dev.  There may be an
557*9880d681SAndroid Build Coastguard Workereasy way we can extend the support to make it work for your use case without
558*9880d681SAndroid Build Coastguard Workerrequiring a custom build.
559*9880d681SAndroid Build Coastguard Worker
560*9880d681SAndroid Build Coastguard WorkerCollector Requirements
561*9880d681SAndroid Build Coastguard Worker----------------------
562*9880d681SAndroid Build Coastguard Worker
563*9880d681SAndroid Build Coastguard WorkerYou should be able to leverage any existing collector library that includes the following elements:
564*9880d681SAndroid Build Coastguard Worker
565*9880d681SAndroid Build Coastguard Worker#. A memory allocator which exposes an allocation function your compiled
566*9880d681SAndroid Build Coastguard Worker   code can call.
567*9880d681SAndroid Build Coastguard Worker
568*9880d681SAndroid Build Coastguard Worker#. A binary format for the stack map.  A stack map describes the location
569*9880d681SAndroid Build Coastguard Worker   of references at a safepoint and is used by precise collectors to identify
570*9880d681SAndroid Build Coastguard Worker   references within a stack frame on the machine stack. Note that collectors
571*9880d681SAndroid Build Coastguard Worker   which conservatively scan the stack don't require such a structure.
572*9880d681SAndroid Build Coastguard Worker
573*9880d681SAndroid Build Coastguard Worker#. A stack crawler to discover functions on the call stack, and enumerate the
574*9880d681SAndroid Build Coastguard Worker   references listed in the stack map for each call site.
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker#. A mechanism for identifying references in global locations (e.g. global
577*9880d681SAndroid Build Coastguard Worker   variables).
578*9880d681SAndroid Build Coastguard Worker
579*9880d681SAndroid Build Coastguard Worker#. If you collector requires them, an LLVM IR implementation of your collectors
580*9880d681SAndroid Build Coastguard Worker   load and store barriers.  Note that since many collectors don't require
581*9880d681SAndroid Build Coastguard Worker   barriers at all, LLVM defaults to lowering such barriers to normal loads
582*9880d681SAndroid Build Coastguard Worker   and stores unless you arrange otherwise.
583*9880d681SAndroid Build Coastguard Worker
584*9880d681SAndroid Build Coastguard Worker
585*9880d681SAndroid Build Coastguard WorkerImplementing a collector plugin
586*9880d681SAndroid Build Coastguard Worker-------------------------------
587*9880d681SAndroid Build Coastguard Worker
588*9880d681SAndroid Build Coastguard WorkerUser code specifies which GC code generation to use with the ``gc`` function
589*9880d681SAndroid Build Coastguard Workerattribute or, equivalently, with the ``setGC`` method of ``Function``.
590*9880d681SAndroid Build Coastguard Worker
591*9880d681SAndroid Build Coastguard WorkerTo implement a GC plugin, it is necessary to subclass ``llvm::GCStrategy``,
592*9880d681SAndroid Build Coastguard Workerwhich can be accomplished in a few lines of boilerplate code.  LLVM's
593*9880d681SAndroid Build Coastguard Workerinfrastructure provides access to several important algorithms.  For an
594*9880d681SAndroid Build Coastguard Workeruncontroversial collector, all that remains may be to compile LLVM's computed
595*9880d681SAndroid Build Coastguard Workerstack map to assembly code (using the binary representation expected by the
596*9880d681SAndroid Build Coastguard Workerruntime library).  This can be accomplished in about 100 lines of code.
597*9880d681SAndroid Build Coastguard Worker
598*9880d681SAndroid Build Coastguard WorkerThis is not the appropriate place to implement a garbage collected heap or a
599*9880d681SAndroid Build Coastguard Workergarbage collector itself.  That code should exist in the language's runtime
600*9880d681SAndroid Build Coastguard Workerlibrary.  The compiler plugin is responsible for generating code which conforms
601*9880d681SAndroid Build Coastguard Workerto the binary interface defined by library, most essentially the :ref:`stack map
602*9880d681SAndroid Build Coastguard Worker<stack-map>`.
603*9880d681SAndroid Build Coastguard Worker
604*9880d681SAndroid Build Coastguard WorkerTo subclass ``llvm::GCStrategy`` and register it with the compiler:
605*9880d681SAndroid Build Coastguard Worker
606*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
607*9880d681SAndroid Build Coastguard Worker
608*9880d681SAndroid Build Coastguard Worker  // lib/MyGC/MyGC.cpp - Example LLVM GC plugin
609*9880d681SAndroid Build Coastguard Worker
610*9880d681SAndroid Build Coastguard Worker  #include "llvm/CodeGen/GCStrategy.h"
611*9880d681SAndroid Build Coastguard Worker  #include "llvm/CodeGen/GCMetadata.h"
612*9880d681SAndroid Build Coastguard Worker  #include "llvm/Support/Compiler.h"
613*9880d681SAndroid Build Coastguard Worker
614*9880d681SAndroid Build Coastguard Worker  using namespace llvm;
615*9880d681SAndroid Build Coastguard Worker
616*9880d681SAndroid Build Coastguard Worker  namespace {
617*9880d681SAndroid Build Coastguard Worker    class LLVM_LIBRARY_VISIBILITY MyGC : public GCStrategy {
618*9880d681SAndroid Build Coastguard Worker    public:
619*9880d681SAndroid Build Coastguard Worker      MyGC() {}
620*9880d681SAndroid Build Coastguard Worker    };
621*9880d681SAndroid Build Coastguard Worker
622*9880d681SAndroid Build Coastguard Worker    GCRegistry::Add<MyGC>
623*9880d681SAndroid Build Coastguard Worker    X("mygc", "My bespoke garbage collector.");
624*9880d681SAndroid Build Coastguard Worker  }
625*9880d681SAndroid Build Coastguard Worker
626*9880d681SAndroid Build Coastguard WorkerThis boilerplate collector does nothing.  More specifically:
627*9880d681SAndroid Build Coastguard Worker
628*9880d681SAndroid Build Coastguard Worker* ``llvm.gcread`` calls are replaced with the corresponding ``load``
629*9880d681SAndroid Build Coastguard Worker  instruction.
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker* ``llvm.gcwrite`` calls are replaced with the corresponding ``store``
632*9880d681SAndroid Build Coastguard Worker  instruction.
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker* No safe points are added to the code.
635*9880d681SAndroid Build Coastguard Worker
636*9880d681SAndroid Build Coastguard Worker* The stack map is not compiled into the executable.
637*9880d681SAndroid Build Coastguard Worker
638*9880d681SAndroid Build Coastguard WorkerUsing the LLVM makefiles, this code
639*9880d681SAndroid Build Coastguard Workercan be compiled as a plugin using a simple makefile:
640*9880d681SAndroid Build Coastguard Worker
641*9880d681SAndroid Build Coastguard Worker.. code-block:: make
642*9880d681SAndroid Build Coastguard Worker
643*9880d681SAndroid Build Coastguard Worker  # lib/MyGC/Makefile
644*9880d681SAndroid Build Coastguard Worker
645*9880d681SAndroid Build Coastguard Worker  LEVEL := ../..
646*9880d681SAndroid Build Coastguard Worker  LIBRARYNAME = MyGC
647*9880d681SAndroid Build Coastguard Worker  LOADABLE_MODULE = 1
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard Worker  include $(LEVEL)/Makefile.common
650*9880d681SAndroid Build Coastguard Worker
651*9880d681SAndroid Build Coastguard WorkerOnce the plugin is compiled, code using it may be compiled using ``llc
652*9880d681SAndroid Build Coastguard Worker-load=MyGC.so`` (though MyGC.so may have some other platform-specific
653*9880d681SAndroid Build Coastguard Workerextension):
654*9880d681SAndroid Build Coastguard Worker
655*9880d681SAndroid Build Coastguard Worker::
656*9880d681SAndroid Build Coastguard Worker
657*9880d681SAndroid Build Coastguard Worker  $ cat sample.ll
658*9880d681SAndroid Build Coastguard Worker  define void @f() gc "mygc" {
659*9880d681SAndroid Build Coastguard Worker  entry:
660*9880d681SAndroid Build Coastguard Worker    ret void
661*9880d681SAndroid Build Coastguard Worker  }
662*9880d681SAndroid Build Coastguard Worker  $ llvm-as < sample.ll | llc -load=MyGC.so
663*9880d681SAndroid Build Coastguard Worker
664*9880d681SAndroid Build Coastguard WorkerIt is also possible to statically link the collector plugin into tools, such as
665*9880d681SAndroid Build Coastguard Workera language-specific compiler front-end.
666*9880d681SAndroid Build Coastguard Worker
667*9880d681SAndroid Build Coastguard Worker.. _collector-algos:
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard WorkerOverview of available features
670*9880d681SAndroid Build Coastguard Worker------------------------------
671*9880d681SAndroid Build Coastguard Worker
672*9880d681SAndroid Build Coastguard Worker``GCStrategy`` provides a range of features through which a plugin may do useful
673*9880d681SAndroid Build Coastguard Workerwork.  Some of these are callbacks, some are algorithms that can be enabled,
674*9880d681SAndroid Build Coastguard Workerdisabled, or customized.  This matrix summarizes the supported (and planned)
675*9880d681SAndroid Build Coastguard Workerfeatures and correlates them with the collection techniques which typically
676*9880d681SAndroid Build Coastguard Workerrequire them.
677*9880d681SAndroid Build Coastguard Worker
678*9880d681SAndroid Build Coastguard Worker.. |v| unicode:: 0x2714
679*9880d681SAndroid Build Coastguard Worker   :trim:
680*9880d681SAndroid Build Coastguard Worker
681*9880d681SAndroid Build Coastguard Worker.. |x| unicode:: 0x2718
682*9880d681SAndroid Build Coastguard Worker   :trim:
683*9880d681SAndroid Build Coastguard Worker
684*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
685*9880d681SAndroid Build Coastguard Worker| Algorithm  | Done | Shadow | refcount | mark- | copying | incremental | threaded | concurrent |
686*9880d681SAndroid Build Coastguard Worker|            |      | stack  |          | sweep |         |             |          |            |
687*9880d681SAndroid Build Coastguard Worker+============+======+========+==========+=======+=========+=============+==========+============+
688*9880d681SAndroid Build Coastguard Worker| stack map  | |v|  |        |          | |x|   | |x|     | |x|         | |x|      | |x|        |
689*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
690*9880d681SAndroid Build Coastguard Worker| initialize | |v|  | |x|    | |x|      | |x|   | |x|     | |x|         | |x|      | |x|        |
691*9880d681SAndroid Build Coastguard Worker| roots      |      |        |          |       |         |             |          |            |
692*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
693*9880d681SAndroid Build Coastguard Worker| derived    | NO   |        |          |       |         |             | **N**\*  | **N**\*    |
694*9880d681SAndroid Build Coastguard Worker| pointers   |      |        |          |       |         |             |          |            |
695*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
696*9880d681SAndroid Build Coastguard Worker| **custom   | |v|  |        |          |       |         |             |          |            |
697*9880d681SAndroid Build Coastguard Worker| lowering** |      |        |          |       |         |             |          |            |
698*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
699*9880d681SAndroid Build Coastguard Worker| *gcroot*   | |v|  | |x|    | |x|      |       |         |             |          |            |
700*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
701*9880d681SAndroid Build Coastguard Worker| *gcwrite*  | |v|  |        | |x|      |       |         | |x|         |          | |x|        |
702*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
703*9880d681SAndroid Build Coastguard Worker| *gcread*   | |v|  |        |          |       |         |             |          | |x|        |
704*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
705*9880d681SAndroid Build Coastguard Worker| **safe     |      |        |          |       |         |             |          |            |
706*9880d681SAndroid Build Coastguard Worker| points**   |      |        |          |       |         |             |          |            |
707*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
708*9880d681SAndroid Build Coastguard Worker| *in        | |v|  |        |          | |x|   | |x|     | |x|         | |x|      | |x|        |
709*9880d681SAndroid Build Coastguard Worker| calls*     |      |        |          |       |         |             |          |            |
710*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
711*9880d681SAndroid Build Coastguard Worker| *before    | |v|  |        |          |       |         |             | |x|      | |x|        |
712*9880d681SAndroid Build Coastguard Worker| calls*     |      |        |          |       |         |             |          |            |
713*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
714*9880d681SAndroid Build Coastguard Worker| *for       | NO   |        |          |       |         |             | **N**    | **N**      |
715*9880d681SAndroid Build Coastguard Worker| loops*     |      |        |          |       |         |             |          |            |
716*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
717*9880d681SAndroid Build Coastguard Worker| *before    | |v|  |        |          |       |         |             | |x|      | |x|        |
718*9880d681SAndroid Build Coastguard Worker| escape*    |      |        |          |       |         |             |          |            |
719*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
720*9880d681SAndroid Build Coastguard Worker| emit code  | NO   |        |          |       |         |             | **N**    | **N**      |
721*9880d681SAndroid Build Coastguard Worker| at safe    |      |        |          |       |         |             |          |            |
722*9880d681SAndroid Build Coastguard Worker| points     |      |        |          |       |         |             |          |            |
723*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
724*9880d681SAndroid Build Coastguard Worker| **output** |      |        |          |       |         |             |          |            |
725*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
726*9880d681SAndroid Build Coastguard Worker| *assembly* | |v|  |        |          | |x|   | |x|     | |x|         | |x|      | |x|        |
727*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
728*9880d681SAndroid Build Coastguard Worker| *JIT*      | NO   |        |          | **?** | **?**   | **?**       | **?**    | **?**      |
729*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
730*9880d681SAndroid Build Coastguard Worker| *obj*      | NO   |        |          | **?** | **?**   | **?**       | **?**    | **?**      |
731*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
732*9880d681SAndroid Build Coastguard Worker| live       | NO   |        |          | **?** | **?**   | **?**       | **?**    | **?**      |
733*9880d681SAndroid Build Coastguard Worker| analysis   |      |        |          |       |         |             |          |            |
734*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
735*9880d681SAndroid Build Coastguard Worker| register   | NO   |        |          | **?** | **?**   | **?**       | **?**    | **?**      |
736*9880d681SAndroid Build Coastguard Worker| map        |      |        |          |       |         |             |          |            |
737*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
738*9880d681SAndroid Build Coastguard Worker| \* Derived pointers only pose a hasard to copying collections.                                |
739*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
740*9880d681SAndroid Build Coastguard Worker| **?** denotes a feature which could be utilized if available.                                 |
741*9880d681SAndroid Build Coastguard Worker+------------+------+--------+----------+-------+---------+-------------+----------+------------+
742*9880d681SAndroid Build Coastguard Worker
743*9880d681SAndroid Build Coastguard WorkerTo be clear, the collection techniques above are defined as:
744*9880d681SAndroid Build Coastguard Worker
745*9880d681SAndroid Build Coastguard WorkerShadow Stack
746*9880d681SAndroid Build Coastguard Worker  The mutator carefully maintains a linked list of stack roots.
747*9880d681SAndroid Build Coastguard Worker
748*9880d681SAndroid Build Coastguard WorkerReference Counting
749*9880d681SAndroid Build Coastguard Worker  The mutator maintains a reference count for each object and frees an object
750*9880d681SAndroid Build Coastguard Worker  when its count falls to zero.
751*9880d681SAndroid Build Coastguard Worker
752*9880d681SAndroid Build Coastguard WorkerMark-Sweep
753*9880d681SAndroid Build Coastguard Worker  When the heap is exhausted, the collector marks reachable objects starting
754*9880d681SAndroid Build Coastguard Worker  from the roots, then deallocates unreachable objects in a sweep phase.
755*9880d681SAndroid Build Coastguard Worker
756*9880d681SAndroid Build Coastguard WorkerCopying
757*9880d681SAndroid Build Coastguard Worker  As reachability analysis proceeds, the collector copies objects from one heap
758*9880d681SAndroid Build Coastguard Worker  area to another, compacting them in the process.  Copying collectors enable
759*9880d681SAndroid Build Coastguard Worker  highly efficient "bump pointer" allocation and can improve locality of
760*9880d681SAndroid Build Coastguard Worker  reference.
761*9880d681SAndroid Build Coastguard Worker
762*9880d681SAndroid Build Coastguard WorkerIncremental
763*9880d681SAndroid Build Coastguard Worker  (Including generational collectors.) Incremental collectors generally have all
764*9880d681SAndroid Build Coastguard Worker  the properties of a copying collector (regardless of whether the mature heap
765*9880d681SAndroid Build Coastguard Worker  is compacting), but bring the added complexity of requiring write barriers.
766*9880d681SAndroid Build Coastguard Worker
767*9880d681SAndroid Build Coastguard WorkerThreaded
768*9880d681SAndroid Build Coastguard Worker  Denotes a multithreaded mutator; the collector must still stop the mutator
769*9880d681SAndroid Build Coastguard Worker  ("stop the world") before beginning reachability analysis.  Stopping a
770*9880d681SAndroid Build Coastguard Worker  multithreaded mutator is a complicated problem.  It generally requires highly
771*9880d681SAndroid Build Coastguard Worker  platform-specific code in the runtime, and the production of carefully
772*9880d681SAndroid Build Coastguard Worker  designed machine code at safe points.
773*9880d681SAndroid Build Coastguard Worker
774*9880d681SAndroid Build Coastguard WorkerConcurrent
775*9880d681SAndroid Build Coastguard Worker  In this technique, the mutator and the collector run concurrently, with the
776*9880d681SAndroid Build Coastguard Worker  goal of eliminating pause times.  In a *cooperative* collector, the mutator
777*9880d681SAndroid Build Coastguard Worker  further aids with collection should a pause occur, allowing collection to take
778*9880d681SAndroid Build Coastguard Worker  advantage of multiprocessor hosts.  The "stop the world" problem of threaded
779*9880d681SAndroid Build Coastguard Worker  collectors is generally still present to a limited extent.  Sophisticated
780*9880d681SAndroid Build Coastguard Worker  marking algorithms are necessary.  Read barriers may be necessary.
781*9880d681SAndroid Build Coastguard Worker
782*9880d681SAndroid Build Coastguard WorkerAs the matrix indicates, LLVM's garbage collection infrastructure is already
783*9880d681SAndroid Build Coastguard Workersuitable for a wide variety of collectors, but does not currently extend to
784*9880d681SAndroid Build Coastguard Workermultithreaded programs.  This will be added in the future as there is
785*9880d681SAndroid Build Coastguard Workerinterest.
786*9880d681SAndroid Build Coastguard Worker
787*9880d681SAndroid Build Coastguard Worker.. _stack-map:
788*9880d681SAndroid Build Coastguard Worker
789*9880d681SAndroid Build Coastguard WorkerComputing stack maps
790*9880d681SAndroid Build Coastguard Worker--------------------
791*9880d681SAndroid Build Coastguard Worker
792*9880d681SAndroid Build Coastguard WorkerLLVM automatically computes a stack map.  One of the most important features
793*9880d681SAndroid Build Coastguard Workerof a ``GCStrategy`` is to compile this information into the executable in
794*9880d681SAndroid Build Coastguard Workerthe binary representation expected by the runtime library.
795*9880d681SAndroid Build Coastguard Worker
796*9880d681SAndroid Build Coastguard WorkerThe stack map consists of the location and identity of each GC root in the
797*9880d681SAndroid Build Coastguard Workereach function in the module.  For each root:
798*9880d681SAndroid Build Coastguard Worker
799*9880d681SAndroid Build Coastguard Worker* ``RootNum``: The index of the root.
800*9880d681SAndroid Build Coastguard Worker
801*9880d681SAndroid Build Coastguard Worker* ``StackOffset``: The offset of the object relative to the frame pointer.
802*9880d681SAndroid Build Coastguard Worker
803*9880d681SAndroid Build Coastguard Worker* ``RootMetadata``: The value passed as the ``%metadata`` parameter to the
804*9880d681SAndroid Build Coastguard Worker  ``@llvm.gcroot`` intrinsic.
805*9880d681SAndroid Build Coastguard Worker
806*9880d681SAndroid Build Coastguard WorkerAlso, for the function as a whole:
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker* ``getFrameSize()``: The overall size of the function's initial stack frame,
809*9880d681SAndroid Build Coastguard Worker   not accounting for any dynamic allocation.
810*9880d681SAndroid Build Coastguard Worker
811*9880d681SAndroid Build Coastguard Worker* ``roots_size()``: The count of roots in the function.
812*9880d681SAndroid Build Coastguard Worker
813*9880d681SAndroid Build Coastguard WorkerTo access the stack map, use ``GCFunctionMetadata::roots_begin()`` and
814*9880d681SAndroid Build Coastguard Worker-``end()`` from the :ref:`GCMetadataPrinter <assembly>`:
815*9880d681SAndroid Build Coastguard Worker
816*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
817*9880d681SAndroid Build Coastguard Worker
818*9880d681SAndroid Build Coastguard Worker  for (iterator I = begin(), E = end(); I != E; ++I) {
819*9880d681SAndroid Build Coastguard Worker    GCFunctionInfo *FI = *I;
820*9880d681SAndroid Build Coastguard Worker    unsigned FrameSize = FI->getFrameSize();
821*9880d681SAndroid Build Coastguard Worker    size_t RootCount = FI->roots_size();
822*9880d681SAndroid Build Coastguard Worker
823*9880d681SAndroid Build Coastguard Worker    for (GCFunctionInfo::roots_iterator RI = FI->roots_begin(),
824*9880d681SAndroid Build Coastguard Worker                                        RE = FI->roots_end();
825*9880d681SAndroid Build Coastguard Worker                                        RI != RE; ++RI) {
826*9880d681SAndroid Build Coastguard Worker      int RootNum = RI->Num;
827*9880d681SAndroid Build Coastguard Worker      int RootStackOffset = RI->StackOffset;
828*9880d681SAndroid Build Coastguard Worker      Constant *RootMetadata = RI->Metadata;
829*9880d681SAndroid Build Coastguard Worker    }
830*9880d681SAndroid Build Coastguard Worker  }
831*9880d681SAndroid Build Coastguard Worker
832*9880d681SAndroid Build Coastguard WorkerIf the ``llvm.gcroot`` intrinsic is eliminated before code generation by a
833*9880d681SAndroid Build Coastguard Workercustom lowering pass, LLVM will compute an empty stack map.  This may be useful
834*9880d681SAndroid Build Coastguard Workerfor collector plugins which implement reference counting or a shadow stack.
835*9880d681SAndroid Build Coastguard Worker
836*9880d681SAndroid Build Coastguard Worker.. _init-roots:
837*9880d681SAndroid Build Coastguard Worker
838*9880d681SAndroid Build Coastguard WorkerInitializing roots to null: ``InitRoots``
839*9880d681SAndroid Build Coastguard Worker-----------------------------------------
840*9880d681SAndroid Build Coastguard Worker
841*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
842*9880d681SAndroid Build Coastguard Worker
843*9880d681SAndroid Build Coastguard Worker  MyGC::MyGC() {
844*9880d681SAndroid Build Coastguard Worker    InitRoots = true;
845*9880d681SAndroid Build Coastguard Worker  }
846*9880d681SAndroid Build Coastguard Worker
847*9880d681SAndroid Build Coastguard WorkerWhen set, LLVM will automatically initialize each root to ``null`` upon entry to
848*9880d681SAndroid Build Coastguard Workerthe function.  This prevents the GC's sweep phase from visiting uninitialized
849*9880d681SAndroid Build Coastguard Workerpointers, which will almost certainly cause it to crash.  This initialization
850*9880d681SAndroid Build Coastguard Workeroccurs before custom lowering, so the two may be used together.
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard WorkerSince LLVM does not yet compute liveness information, there is no means of
853*9880d681SAndroid Build Coastguard Workerdistinguishing an uninitialized stack root from an initialized one.  Therefore,
854*9880d681SAndroid Build Coastguard Workerthis feature should be used by all GC plugins.  It is enabled by default.
855*9880d681SAndroid Build Coastguard Worker
856*9880d681SAndroid Build Coastguard WorkerCustom lowering of intrinsics: ``CustomRoots``, ``CustomReadBarriers``, and ``CustomWriteBarriers``
857*9880d681SAndroid Build Coastguard Worker---------------------------------------------------------------------------------------------------
858*9880d681SAndroid Build Coastguard Worker
859*9880d681SAndroid Build Coastguard WorkerFor GCs which use barriers or unusual treatment of stack roots, these
860*9880d681SAndroid Build Coastguard Workerflags allow the collector to perform arbitrary transformations of the
861*9880d681SAndroid Build Coastguard WorkerLLVM IR:
862*9880d681SAndroid Build Coastguard Worker
863*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
864*9880d681SAndroid Build Coastguard Worker
865*9880d681SAndroid Build Coastguard Worker  class MyGC : public GCStrategy {
866*9880d681SAndroid Build Coastguard Worker  public:
867*9880d681SAndroid Build Coastguard Worker    MyGC() {
868*9880d681SAndroid Build Coastguard Worker      CustomRoots = true;
869*9880d681SAndroid Build Coastguard Worker      CustomReadBarriers = true;
870*9880d681SAndroid Build Coastguard Worker      CustomWriteBarriers = true;
871*9880d681SAndroid Build Coastguard Worker    }
872*9880d681SAndroid Build Coastguard Worker  };
873*9880d681SAndroid Build Coastguard Worker
874*9880d681SAndroid Build Coastguard WorkerIf any of these flags are set, LLVM suppresses its default lowering for
875*9880d681SAndroid Build Coastguard Workerthe corresponding intrinsics.  Instead, you must provide a custom Pass
876*9880d681SAndroid Build Coastguard Workerwhich lowers the intrinsics as desired.  If you have opted in to custom
877*9880d681SAndroid Build Coastguard Workerlowering of a particular intrinsic your pass **must** eliminate all
878*9880d681SAndroid Build Coastguard Workerinstances of the corresponding intrinsic in functions which opt in to
879*9880d681SAndroid Build Coastguard Workeryour GC.  The best example of such a pass is the ShadowStackGC and it's
880*9880d681SAndroid Build Coastguard WorkerShadowStackGCLowering pass.
881*9880d681SAndroid Build Coastguard Worker
882*9880d681SAndroid Build Coastguard WorkerThere is currently no way to register such a custom lowering pass
883*9880d681SAndroid Build Coastguard Workerwithout building a custom copy of LLVM.
884*9880d681SAndroid Build Coastguard Worker
885*9880d681SAndroid Build Coastguard Worker.. _safe-points:
886*9880d681SAndroid Build Coastguard Worker
887*9880d681SAndroid Build Coastguard WorkerGenerating safe points: ``NeededSafePoints``
888*9880d681SAndroid Build Coastguard Worker--------------------------------------------
889*9880d681SAndroid Build Coastguard Worker
890*9880d681SAndroid Build Coastguard WorkerLLVM can compute four kinds of safe points:
891*9880d681SAndroid Build Coastguard Worker
892*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
893*9880d681SAndroid Build Coastguard Worker
894*9880d681SAndroid Build Coastguard Worker  namespace GC {
895*9880d681SAndroid Build Coastguard Worker    /// PointKind - The type of a collector-safe point.
896*9880d681SAndroid Build Coastguard Worker    ///
897*9880d681SAndroid Build Coastguard Worker    enum PointKind {
898*9880d681SAndroid Build Coastguard Worker      Loop,    //< Instr is a loop (backwards branch).
899*9880d681SAndroid Build Coastguard Worker      Return,  //< Instr is a return instruction.
900*9880d681SAndroid Build Coastguard Worker      PreCall, //< Instr is a call instruction.
901*9880d681SAndroid Build Coastguard Worker      PostCall //< Instr is the return address of a call.
902*9880d681SAndroid Build Coastguard Worker    };
903*9880d681SAndroid Build Coastguard Worker  }
904*9880d681SAndroid Build Coastguard Worker
905*9880d681SAndroid Build Coastguard WorkerA collector can request any combination of the four by setting the
906*9880d681SAndroid Build Coastguard Worker``NeededSafePoints`` mask:
907*9880d681SAndroid Build Coastguard Worker
908*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
909*9880d681SAndroid Build Coastguard Worker
910*9880d681SAndroid Build Coastguard Worker  MyGC::MyGC()  {
911*9880d681SAndroid Build Coastguard Worker    NeededSafePoints = 1 << GC::Loop
912*9880d681SAndroid Build Coastguard Worker                     | 1 << GC::Return
913*9880d681SAndroid Build Coastguard Worker                     | 1 << GC::PreCall
914*9880d681SAndroid Build Coastguard Worker                     | 1 << GC::PostCall;
915*9880d681SAndroid Build Coastguard Worker  }
916*9880d681SAndroid Build Coastguard Worker
917*9880d681SAndroid Build Coastguard WorkerIt can then use the following routines to access safe points.
918*9880d681SAndroid Build Coastguard Worker
919*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
920*9880d681SAndroid Build Coastguard Worker
921*9880d681SAndroid Build Coastguard Worker  for (iterator I = begin(), E = end(); I != E; ++I) {
922*9880d681SAndroid Build Coastguard Worker    GCFunctionInfo *MD = *I;
923*9880d681SAndroid Build Coastguard Worker    size_t PointCount = MD->size();
924*9880d681SAndroid Build Coastguard Worker
925*9880d681SAndroid Build Coastguard Worker    for (GCFunctionInfo::iterator PI = MD->begin(),
926*9880d681SAndroid Build Coastguard Worker                                  PE = MD->end(); PI != PE; ++PI) {
927*9880d681SAndroid Build Coastguard Worker      GC::PointKind PointKind = PI->Kind;
928*9880d681SAndroid Build Coastguard Worker      unsigned PointNum = PI->Num;
929*9880d681SAndroid Build Coastguard Worker    }
930*9880d681SAndroid Build Coastguard Worker  }
931*9880d681SAndroid Build Coastguard Worker
932*9880d681SAndroid Build Coastguard WorkerAlmost every collector requires ``PostCall`` safe points, since these correspond
933*9880d681SAndroid Build Coastguard Workerto the moments when the function is suspended during a call to a subroutine.
934*9880d681SAndroid Build Coastguard Worker
935*9880d681SAndroid Build Coastguard WorkerThreaded programs generally require ``Loop`` safe points to guarantee that the
936*9880d681SAndroid Build Coastguard Workerapplication will reach a safe point within a bounded amount of time, even if it
937*9880d681SAndroid Build Coastguard Workeris executing a long-running loop which contains no function calls.
938*9880d681SAndroid Build Coastguard Worker
939*9880d681SAndroid Build Coastguard WorkerThreaded collectors may also require ``Return`` and ``PreCall`` safe points to
940*9880d681SAndroid Build Coastguard Workerimplement "stop the world" techniques using self-modifying code, where it is
941*9880d681SAndroid Build Coastguard Workerimportant that the program not exit the function without reaching a safe point
942*9880d681SAndroid Build Coastguard Worker(because only the topmost function has been patched).
943*9880d681SAndroid Build Coastguard Worker
944*9880d681SAndroid Build Coastguard Worker.. _assembly:
945*9880d681SAndroid Build Coastguard Worker
946*9880d681SAndroid Build Coastguard WorkerEmitting assembly code: ``GCMetadataPrinter``
947*9880d681SAndroid Build Coastguard Worker---------------------------------------------
948*9880d681SAndroid Build Coastguard Worker
949*9880d681SAndroid Build Coastguard WorkerLLVM allows a plugin to print arbitrary assembly code before and after the rest
950*9880d681SAndroid Build Coastguard Workerof a module's assembly code.  At the end of the module, the GC can compile the
951*9880d681SAndroid Build Coastguard WorkerLLVM stack map into assembly code. (At the beginning, this information is not
952*9880d681SAndroid Build Coastguard Workeryet computed.)
953*9880d681SAndroid Build Coastguard Worker
954*9880d681SAndroid Build Coastguard WorkerSince AsmWriter and CodeGen are separate components of LLVM, a separate abstract
955*9880d681SAndroid Build Coastguard Workerbase class and registry is provided for printing assembly code, the
956*9880d681SAndroid Build Coastguard Worker``GCMetadaPrinter`` and ``GCMetadataPrinterRegistry``.  The AsmWriter will look
957*9880d681SAndroid Build Coastguard Workerfor such a subclass if the ``GCStrategy`` sets ``UsesMetadata``:
958*9880d681SAndroid Build Coastguard Worker
959*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
960*9880d681SAndroid Build Coastguard Worker
961*9880d681SAndroid Build Coastguard Worker  MyGC::MyGC() {
962*9880d681SAndroid Build Coastguard Worker    UsesMetadata = true;
963*9880d681SAndroid Build Coastguard Worker  }
964*9880d681SAndroid Build Coastguard Worker
965*9880d681SAndroid Build Coastguard WorkerThis separation allows JIT-only clients to be smaller.
966*9880d681SAndroid Build Coastguard Worker
967*9880d681SAndroid Build Coastguard WorkerNote that LLVM does not currently have analogous APIs to support code generation
968*9880d681SAndroid Build Coastguard Workerin the JIT, nor using the object writers.
969*9880d681SAndroid Build Coastguard Worker
970*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
971*9880d681SAndroid Build Coastguard Worker
972*9880d681SAndroid Build Coastguard Worker  // lib/MyGC/MyGCPrinter.cpp - Example LLVM GC printer
973*9880d681SAndroid Build Coastguard Worker
974*9880d681SAndroid Build Coastguard Worker  #include "llvm/CodeGen/GCMetadataPrinter.h"
975*9880d681SAndroid Build Coastguard Worker  #include "llvm/Support/Compiler.h"
976*9880d681SAndroid Build Coastguard Worker
977*9880d681SAndroid Build Coastguard Worker  using namespace llvm;
978*9880d681SAndroid Build Coastguard Worker
979*9880d681SAndroid Build Coastguard Worker  namespace {
980*9880d681SAndroid Build Coastguard Worker    class LLVM_LIBRARY_VISIBILITY MyGCPrinter : public GCMetadataPrinter {
981*9880d681SAndroid Build Coastguard Worker    public:
982*9880d681SAndroid Build Coastguard Worker      virtual void beginAssembly(AsmPrinter &AP);
983*9880d681SAndroid Build Coastguard Worker
984*9880d681SAndroid Build Coastguard Worker      virtual void finishAssembly(AsmPrinter &AP);
985*9880d681SAndroid Build Coastguard Worker    };
986*9880d681SAndroid Build Coastguard Worker
987*9880d681SAndroid Build Coastguard Worker    GCMetadataPrinterRegistry::Add<MyGCPrinter>
988*9880d681SAndroid Build Coastguard Worker    X("mygc", "My bespoke garbage collector.");
989*9880d681SAndroid Build Coastguard Worker  }
990*9880d681SAndroid Build Coastguard Worker
991*9880d681SAndroid Build Coastguard WorkerThe collector should use ``AsmPrinter`` to print portable assembly code.  The
992*9880d681SAndroid Build Coastguard Workercollector itself contains the stack map for the entire module, and may access
993*9880d681SAndroid Build Coastguard Workerthe ``GCFunctionInfo`` using its own ``begin()`` and ``end()`` methods.  Here's
994*9880d681SAndroid Build Coastguard Workera realistic example:
995*9880d681SAndroid Build Coastguard Worker
996*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
997*9880d681SAndroid Build Coastguard Worker
998*9880d681SAndroid Build Coastguard Worker  #include "llvm/CodeGen/AsmPrinter.h"
999*9880d681SAndroid Build Coastguard Worker  #include "llvm/IR/Function.h"
1000*9880d681SAndroid Build Coastguard Worker  #include "llvm/IR/DataLayout.h"
1001*9880d681SAndroid Build Coastguard Worker  #include "llvm/Target/TargetAsmInfo.h"
1002*9880d681SAndroid Build Coastguard Worker  #include "llvm/Target/TargetMachine.h"
1003*9880d681SAndroid Build Coastguard Worker
1004*9880d681SAndroid Build Coastguard Worker  void MyGCPrinter::beginAssembly(AsmPrinter &AP) {
1005*9880d681SAndroid Build Coastguard Worker    // Nothing to do.
1006*9880d681SAndroid Build Coastguard Worker  }
1007*9880d681SAndroid Build Coastguard Worker
1008*9880d681SAndroid Build Coastguard Worker  void MyGCPrinter::finishAssembly(AsmPrinter &AP) {
1009*9880d681SAndroid Build Coastguard Worker    MCStreamer &OS = AP.OutStreamer;
1010*9880d681SAndroid Build Coastguard Worker    unsigned IntPtrSize = AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
1011*9880d681SAndroid Build Coastguard Worker
1012*9880d681SAndroid Build Coastguard Worker    // Put this in the data section.
1013*9880d681SAndroid Build Coastguard Worker    OS.SwitchSection(AP.getObjFileLowering().getDataSection());
1014*9880d681SAndroid Build Coastguard Worker
1015*9880d681SAndroid Build Coastguard Worker    // For each function...
1016*9880d681SAndroid Build Coastguard Worker    for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
1017*9880d681SAndroid Build Coastguard Worker      GCFunctionInfo &MD = **FI;
1018*9880d681SAndroid Build Coastguard Worker
1019*9880d681SAndroid Build Coastguard Worker      // A compact GC layout. Emit this data structure:
1020*9880d681SAndroid Build Coastguard Worker      //
1021*9880d681SAndroid Build Coastguard Worker      // struct {
1022*9880d681SAndroid Build Coastguard Worker      //   int32_t PointCount;
1023*9880d681SAndroid Build Coastguard Worker      //   void *SafePointAddress[PointCount];
1024*9880d681SAndroid Build Coastguard Worker      //   int32_t StackFrameSize; // in words
1025*9880d681SAndroid Build Coastguard Worker      //   int32_t StackArity;
1026*9880d681SAndroid Build Coastguard Worker      //   int32_t LiveCount;
1027*9880d681SAndroid Build Coastguard Worker      //   int32_t LiveOffsets[LiveCount];
1028*9880d681SAndroid Build Coastguard Worker      // } __gcmap_<FUNCTIONNAME>;
1029*9880d681SAndroid Build Coastguard Worker
1030*9880d681SAndroid Build Coastguard Worker      // Align to address width.
1031*9880d681SAndroid Build Coastguard Worker      AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
1032*9880d681SAndroid Build Coastguard Worker
1033*9880d681SAndroid Build Coastguard Worker      // Emit PointCount.
1034*9880d681SAndroid Build Coastguard Worker      OS.AddComment("safe point count");
1035*9880d681SAndroid Build Coastguard Worker      AP.EmitInt32(MD.size());
1036*9880d681SAndroid Build Coastguard Worker
1037*9880d681SAndroid Build Coastguard Worker      // And each safe point...
1038*9880d681SAndroid Build Coastguard Worker      for (GCFunctionInfo::iterator PI = MD.begin(),
1039*9880d681SAndroid Build Coastguard Worker                                    PE = MD.end(); PI != PE; ++PI) {
1040*9880d681SAndroid Build Coastguard Worker        // Emit the address of the safe point.
1041*9880d681SAndroid Build Coastguard Worker        OS.AddComment("safe point address");
1042*9880d681SAndroid Build Coastguard Worker        MCSymbol *Label = PI->Label;
1043*9880d681SAndroid Build Coastguard Worker        AP.EmitLabelPlusOffset(Label/*Hi*/, 0/*Offset*/, 4/*Size*/);
1044*9880d681SAndroid Build Coastguard Worker      }
1045*9880d681SAndroid Build Coastguard Worker
1046*9880d681SAndroid Build Coastguard Worker      // Stack information never change in safe points! Only print info from the
1047*9880d681SAndroid Build Coastguard Worker      // first call-site.
1048*9880d681SAndroid Build Coastguard Worker      GCFunctionInfo::iterator PI = MD.begin();
1049*9880d681SAndroid Build Coastguard Worker
1050*9880d681SAndroid Build Coastguard Worker      // Emit the stack frame size.
1051*9880d681SAndroid Build Coastguard Worker      OS.AddComment("stack frame size (in words)");
1052*9880d681SAndroid Build Coastguard Worker      AP.EmitInt32(MD.getFrameSize() / IntPtrSize);
1053*9880d681SAndroid Build Coastguard Worker
1054*9880d681SAndroid Build Coastguard Worker      // Emit stack arity, i.e. the number of stacked arguments.
1055*9880d681SAndroid Build Coastguard Worker      unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
1056*9880d681SAndroid Build Coastguard Worker      unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs ?
1057*9880d681SAndroid Build Coastguard Worker                            MD.getFunction().arg_size() - RegisteredArgs : 0;
1058*9880d681SAndroid Build Coastguard Worker      OS.AddComment("stack arity");
1059*9880d681SAndroid Build Coastguard Worker      AP.EmitInt32(StackArity);
1060*9880d681SAndroid Build Coastguard Worker
1061*9880d681SAndroid Build Coastguard Worker      // Emit the number of live roots in the function.
1062*9880d681SAndroid Build Coastguard Worker      OS.AddComment("live root count");
1063*9880d681SAndroid Build Coastguard Worker      AP.EmitInt32(MD.live_size(PI));
1064*9880d681SAndroid Build Coastguard Worker
1065*9880d681SAndroid Build Coastguard Worker      // And for each live root...
1066*9880d681SAndroid Build Coastguard Worker      for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
1067*9880d681SAndroid Build Coastguard Worker                                         LE = MD.live_end(PI);
1068*9880d681SAndroid Build Coastguard Worker                                         LI != LE; ++LI) {
1069*9880d681SAndroid Build Coastguard Worker        // Emit live root's offset within the stack frame.
1070*9880d681SAndroid Build Coastguard Worker        OS.AddComment("stack index (offset / wordsize)");
1071*9880d681SAndroid Build Coastguard Worker        AP.EmitInt32(LI->StackOffset);
1072*9880d681SAndroid Build Coastguard Worker      }
1073*9880d681SAndroid Build Coastguard Worker    }
1074*9880d681SAndroid Build Coastguard Worker  }
1075*9880d681SAndroid Build Coastguard Worker
1076*9880d681SAndroid Build Coastguard WorkerReferences
1077*9880d681SAndroid Build Coastguard Worker==========
1078*9880d681SAndroid Build Coastguard Worker
1079*9880d681SAndroid Build Coastguard Worker.. _appel89:
1080*9880d681SAndroid Build Coastguard Worker
1081*9880d681SAndroid Build Coastguard Worker[Appel89] Runtime Tags Aren't Necessary. Andrew W. Appel. Lisp and Symbolic
1082*9880d681SAndroid Build Coastguard WorkerComputation 19(7):703-705, July 1989.
1083*9880d681SAndroid Build Coastguard Worker
1084*9880d681SAndroid Build Coastguard Worker.. _goldberg91:
1085*9880d681SAndroid Build Coastguard Worker
1086*9880d681SAndroid Build Coastguard Worker[Goldberg91] Tag-free garbage collection for strongly typed programming
1087*9880d681SAndroid Build Coastguard Workerlanguages. Benjamin Goldberg. ACM SIGPLAN PLDI'91.
1088*9880d681SAndroid Build Coastguard Worker
1089*9880d681SAndroid Build Coastguard Worker.. _tolmach94:
1090*9880d681SAndroid Build Coastguard Worker
1091*9880d681SAndroid Build Coastguard Worker[Tolmach94] Tag-free garbage collection using explicit type parameters. Andrew
1092*9880d681SAndroid Build Coastguard WorkerTolmach. Proceedings of the 1994 ACM conference on LISP and functional
1093*9880d681SAndroid Build Coastguard Workerprogramming.
1094*9880d681SAndroid Build Coastguard Worker
1095*9880d681SAndroid Build Coastguard Worker.. _henderson02:
1096*9880d681SAndroid Build Coastguard Worker
1097*9880d681SAndroid Build Coastguard Worker[Henderson2002] `Accurate Garbage Collection in an Uncooperative Environment
1098*9880d681SAndroid Build Coastguard Worker<http://citeseer.ist.psu.edu/henderson02accurate.html>`__
1099