Debian Codegraph @

doc/guide/src/ShareZeroVec.adoc

raw
1 = ShareZeroVec
2
3 <<ShareZeroVec#>> is an optimization pass for the <<SSA#>>
4 <<IntermediateLanguage#>>, invoked from <<SSASimplify#>>.
5
6 == Description
7
8 An SSA optimization to share zero-length vectors.
9
10 From https://github.com/MLton/mlton/commit/be8c5f576[`be8c5f576`], which replaced the use of the
11 `Array_array0Const` primitive in the Basis Library implementation with a
12 (nullary) `Vector_vector` primitive:
13
14 ________
15
16 The original motivation for the `Array_array0Const` primitive was to share the
17 heap space required for zero-length vectors among all vectors (of a given type).
18 It was claimed that this optimization is important, e.g., in a self-compile,
19 where vectors are used for lots of syntax tree elements and many of those
20 vectors are empty. See:
21 http://www.mlton.org/pipermail/mlton-devel/2002-February/021523.html
22
23 Curiously, the full effect of this optimization has been missing for quite some
24 time (perhaps since the port of <<ConstantPropagation#>> to the SSA IL). While
25 <<ConstantPropagation#>> has "globalized" the nullary application of the
26 `Array_array0Const` primitive, it also simultaneously transformed it to an
27 application of the `Array_uninit` (previously, the `Array_array`) primitive to
28 the zero constant. The hash-consing of globals, meant to create exactly one
29 global for each distinct constant, treats `Array_uninit` primitives as unequal
30 (appropriately, since `Array_uninit` allocates an array with identity (though
31 the identity may be supressed by a subsequent `Array_toVector`)), hence each
32 distinct `Array_array0Const` primitive in the program remained as distinct
33 globals. The limited amount of inlining prior to <<ConstantPropagation#>> meant
34 that there were typically fewer than a dozen "copies" of the same empty vector
35 in a program for a given type.
36
37 As a "functional" primitive, a nullary `Vector_vector` is globalized by
38 ClosureConvert, but is further recognized by ConstantPropagation and hash-consed
39 into a unique instance for each type.
40 ________
41
42 However, a single, shared, global `Vector_vector ()` inhibits the
43 coercion-based optimizations of `Useless`. For example, consider the
44 following program:
45
46 [source,sml]
47 ----
48 val n = valOf (Int.fromString (hd (CommandLine.arguments ())))
49
50 val v1 = Vector.tabulate (n, fn i =>
51 let val w = Word16.fromInt i
52 in (w - 0wx1, w, w + 0wx1 + w)
53 end)
54 val v2 = Vector.map (fn (w1, w2, w3) => (w1, 0wx2 * w2, 0wx3 * w3)) v1
55 val v3 = VectorSlice.vector (VectorSlice.slice (v1, 1, SOME (n - 2)))
56 val ans1 = Vector.foldl (fn ((w1,w2,w3),w) => w + w1 + w2 + w3) 0wx0 v1
57 val ans2 = Vector.foldl (fn ((_,w2,_),w) => w + w2) 0wx0 v2
58 val ans3 = Vector.foldl (fn ((_,w2,_),w) => w + w2) 0wx0 v3
59
60 val _ = print (concat ["ans1 = ", Word16.toString ans1, " ",
61 "ans2 = ", Word16.toString ans2, " ",
62 "ans3 = ", Word16.toString ans3, "\n"])
63 ----
64
65 We would like `v2` and `v3` to be optimized from
66 `(word16 * word16 * word16) vector` to `word16 vector` because only
67 the 2nd component of the elements is needed to compute the answer.
68
69 With `Array_array0Const`, each distinct occurrence of
70 `Array_array0Const((word16 * word16 * word16))` arising from
71 polyvariance and inlining remained a distinct
72 `Array_uninit((word16 * word16 * word16)) (0x0)` global, which
73 resulted in distinct occurrences for the
74 `val v1 = Vector.tabulate ...` and for the
75 `val v2 = Vector.map ...`. The latter could be optimized to
76 `Array_uninit(word16) (0x0)` by `Useless`, because its result only
77 flows to places requiring the 2nd component of the elements.
78
79 With `Vector_vector ()`, the distinct occurrences of
80 `Vector_vector((word16 * word16 * word16)) ()` arising from
81 polyvariance are globalized during `ClosureConvert`, those global
82 references may be further duplicated by inlining, but the distinct
83 occurrences of `Vector_vector((word16 * word16 * word16)) ()` are
84 merged to a single occurrence. Because this result flows to places
85 requiring all three components of the elements, it remains
86 `Vector_vector((word16 * word16 * word16)) ()` after
87 `Useless`. Furthermore, because one cannot (in constant time) coerce a
88 `(word16 * word16 * word16) vector` to a `word16 vector`, the `v2`
89 value remains of type `(word16 * word16 * word16) vector`.
90
91 One option would be to drop the 0-element vector "optimization"
92 entirely. This costs some space (no sharing of empty vectors) and
93 some time (allocation and garbage collection of empty vectors).
94
95 Another option would be to reinstate the `Array_array0Const` primitive
96 and associated `ConstantPropagation` treatment. But, the semantics
97 and purpose of `Array_array0Const` was poorly understood, resulting in
98 this break.
99
100 The <<ShareZeroVec#>> pass pursues a different approach: perform the 0-element
101 vector "optimization" as a separate optimization, after
102 `ConstantPropagation` and `Useless`. A trivial static analysis is
103 used to match `val v: t vector = Array_toVector(t) (a)` with
104 corresponding `val a: array = Array_uninit(t) (l)` and the later are
105 expanded to
106 `val a: t array = if 0 = l then zeroArr_[t] else Array_uninit(t) (l)`
107 with a single global `val zeroArr_[t] = Array_uninit(t) (0)` created
108 for each distinct type (after coercion-based optimizations).
109
110 One disadvantage of this approach, compared to the `Vector_vector(t) ()`
111 approach, is that `Array_toVector` is applied each time a vector
112 is created, even if it is being applied to the `zeroArr_[t]`
113 zero-length array. (Although, this was the behavior of the
114 `Array_array0Const` approach.) This updates the object header each
115 time, whereas the `Vector_vector(t) ()` approach would have updated
116 the object header once, when the global was created, and the
117 `zeroVec_[t]` global and the `Array_toVector` result would flow to the
118 join point.
119
120 It would be possible to properly share zero-length vectors, but doing
121 so is a more sophisticated analysis and transformation, because there
122 can be arbitrary code between the
123 `val a: t array = Array_uninit(t) (l)` and the corresponding
124 `val v: v vector = Array_toVector(t) (a)`, although, in practice,
125 nothing happens when a zero-length vector is created. It may be best
126 to pursue a more general "array to vector" optimization that
127 transforms creations of static-length vectors (e.g., all the
128 `Vector.new<N>` functions) into `Vector_vector` primitives (some of
129 which could be globalized).
130
131 == Implementation
132
133 * https://github.com/MLton/mlton/blob/master/mlton/ssa/share-zero-vec.fun[`share-zero-vec.fun`]
134
135 == Details and Notes
136
137 {empty}