Debian Codegraph @

doc/guide/src/RefFlatten.adoc

raw
1 = RefFlatten
2
3 <<RefFlatten#>> is an optimization pass for the <<SSA2#>>
4 <<IntermediateLanguage#>>, invoked from <<SSA2Simplify#>>.
5
6 == Description
7
8 This pass flattens a `ref` cell into its containing object.
9 The idea is to replace, where possible, a type like
10 ----
11 (int ref * real)
12 ----
13
14 with a type like
15 ----
16 (int[m] * real)
17 ----
18
19 where the `[m]` indicates a mutable field of a tuple.
20
21 == Implementation
22
23 * https://github.com/MLton/mlton/blob/master/mlton/ssa/ref-flatten.fun[`ref-flatten.fun`]
24
25 == Details and Notes
26
27 The savings is obvious, I hope. We avoid an extra heap-allocated
28 object for the `ref`, which in the above case saves two words. We
29 also save the time and code for the extra indirection at each get and
30 set. There are lots of useful data structures (singly-linked and
31 doubly-linked lists, union-find, Fibonacci heaps, ...) that I believe
32 we are paying through the nose right now because of the absence of ref
33 flattening.
34
35 The idea is to compute for each occurrence of a `ref` type in the
36 program whether or not that `ref` can be represented as an offset of
37 some object (constructor or tuple). As before, a unification-based
38 whole-program with deep abstract values makes sure the analysis is
39 consistent.
40
41 The only syntactic part of the analysis that remains is the part that
42 checks that for a variable bound to a value constructed by `Ref_ref`:
43
44 * the object allocation is in the same block. This is pretty
45 draconian, and it would be nice to generalize it some day to allow
46 flattening as long as the `ref` allocation and object allocation "line
47 up one-to-one" in the same loop-free chunk of code.
48
49 * updates occur in the same block (and hence it is safe-for-space
50 because the containing object is still alive). It would be nice to
51 relax this to allow updates as long as it can be provedthat the
52 container is live.
53
54 Prevent flattening of `unit ref`-s.
55
56 <<RefFlatten#>> is safe for space. The idea is to prevent a `ref`
57 being flattened into an object that has a component of unbounded size
58 (other than possibly the `ref` itself) unless we can prove that at
59 each point the `ref` is live, then the containing object is live too.
60 I used a pretty simple approximation to liveness.