Debian Codegraph @

lib/mlrisc-lib/MLRISC/Doc/latex/gc.tex

raw
1 \section{Garbage Collection Safety}
2 \subsection{Motivation}
3 High level languages such as SML make use of garbage collectors
4 to reclaim unused storage at runtime. For generality, I assume that
5 a precise, compacting garbage collector is used. In general,
6 low-level optimizations that reorder instructions
7 pass \newdef{gc safepoints}, when applied naively,
8 are not safe. In general, two general classes of safety issues can be identified:
9 \begin{description}
10 \item[derived values]
11 A derived value $x$ is a value that are
12 dependent on the addresses of one of more heap allocated objects
13 $a1,a2,a3,\ldots$ and/or the recent branch history.
14 When these allocated objects $a1,a2,a3,\ldots$
15 are moved by the garbage collector, $x$
16 has to be adjusted accordingly.
17
18 For example, inductive variable elimination may transformed an array
19 indexing into a running pointer to the middle of an array object.
20 Such running pointer is a derived value and is dependent on the
21 starting address of the array.
22
23 The main difficulty in handling a derived value $x$
24 during garbage collection is that sometimes it is impossible or
25 counter-productive to recompute from $a1,a2,a3,\ldots$.
26 For example, when the recent branch history is unknown, or when the
27 precise relationship between $x$ and $a1,a2,a3,\ldots$ cannot
28 be inferred from context.
29 We call these \newdef{unrecoverable} derived values.
30 \item[incomplete allocation]
31 If heap allocation is performed inlined, then code motion may
32 render some allocation incomplete at a gc safepoint. In general, incomplete
33 allocation has to be completed, or rolled backed and then reexecuted
34 after garbage collection, when the source language semantics allow it.
35 \end{description}
36
37 Typically, two gc safepoints cannot be separated by an unbounded
38 number of allocations, which implies that in general, optimizations that move
39 instructions between basic blocks are unsafe when naively applied,
40 which greatly limits the class of optimizations in such an environment
41 to trivial basic block level optimizations.
42 framework is a necessity.
43
44
45 \subsection{Safety Framework}
46 MLRISC contains a gc-safety framework
47 for performing aggressive machine level optimizations, including SSA-based
48 scalar optimizations, global instruction scheduling, and global
49 register allocation. Unlike previous work in this area, phases that
50 perform optimizations and phases that maintain and update
51 garbage collection information are completely separate, and the optimizer
52 is constructed in a fully modular manner. In particular,
53 gc-types and safety constraints
54 are \emph{parameterizable}
55 by the source language semantics, the object representation,
56 and the target architectures.
57
58 This framework has the following overall structure:
59 \begin{description}
60 \item[Garbage collection invariants annotation]
61 The front-end client is responsible for annotating each
62 value in the program with a \newdef{gc type}, which is
63 used to specify the abstract object representation,
64 and the constraints on code motion that may be applied to such a value.
65 The front-end uses an architecture independent \href{mltree.html}{RTL}
66 language for representing the program, thus this annotation
67 phase is portable between target architectures.
68 \item[GC constraints propagation]
69 After instruction selection, gc constraint are propagated throughout
70 the machine level program representation. Again, for portability, gc typing
71 rules are specified in terms of the \href{mltree.html}{ RTL } of
72 the machine instructions. In this phase, unsafe code motions which
73 exposes unrecoverable derived values to gc safepoints are automatically
74 identified. (Pseudo) control dependence and anti-control dependence
75 constraints are then added the program representation to prohibit all
76 gc-unsafe code motions.
77 \item[Machine level optimizations]
78 After constraints propagation, traditional
79 machine level optimizations such as
80 SSA optimizations and/or global scheduling are applied, without regard
81 to gc information. This is safe because
82 all gc safety constraints have been translated into the appropriate
83 code motion constraints.
84 \item[GC type propagation and gc code generation]
85 GC type inference is performed when all optimizations
86 have been performed. GC safepoints are then
87 identified and the root sets are determined. In addition, compensation
88 code are inserted at gc points for repairing recoverable derived values.
89 \end{description}
90 \subsection{Concurrency Safety}
91 In the presence of \newdef{concurrency}, i.e. multiple threads
92 of control that communicate via a shared heap, the above framework
93 will have to slightly extended. As in before, we assume that
94 context switching can only occur at well-defined
95 \emph{safepoints}.
96 The crucial aspect is that values that are live at safepoints must be
97 classified as \newdef{local} or \newdef{global}.
98 Local values are only observable from
99 the local thread, while global values are potentially observable and mutable
100 from other threads. The invariants to maintain are as follows:
101 \begin{itemize}
102 \item Only local and recoverable derived values may be live at a safepoint,
103 \item Only local and recoverable allocation may be incomplete at a safepoint
104 \end{itemize}