| 1 |
= CommonArg |
| 2 |
|
| 3 |
<<CommonArg#>> is an optimization pass for the <<SSA#>> |
| 4 |
<<IntermediateLanguage#>>, invoked from <<SSASimplify#>>. |
| 5 |
|
| 6 |
== Description |
| 7 |
|
| 8 |
It optimizes instances of `Goto` transfers that pass the same |
| 9 |
arguments to the same label; e.g. |
| 10 |
---- |
| 11 |
L_1 () |
| 12 |
... |
| 13 |
z1 = ? |
| 14 |
... |
| 15 |
L_3 (x, y, z1) |
| 16 |
L_2 () |
| 17 |
... |
| 18 |
z2 = ? |
| 19 |
... |
| 20 |
L_3 (x, y, z2) |
| 21 |
L_3 (a, b, c) |
| 22 |
... |
| 23 |
---- |
| 24 |
|
| 25 |
This code can be simplified to: |
| 26 |
---- |
| 27 |
L_1 () |
| 28 |
... |
| 29 |
z1 = ? |
| 30 |
... |
| 31 |
L_3 (z1) |
| 32 |
L_2 () |
| 33 |
... |
| 34 |
z2 = ? |
| 35 |
... |
| 36 |
L_3 (z2) |
| 37 |
L_3 (c) |
| 38 |
a = x |
| 39 |
b = y |
| 40 |
---- |
| 41 |
which saves a number of resources: time of setting up the arguments |
| 42 |
for the jump to `L_3`, space (either stack slots or temporaries) for |
| 43 |
the arguments of `L_3`, etc. It may also expose some other |
| 44 |
optimizations, if more information is known about `x` or `y`. |
| 45 |
|
| 46 |
== Implementation |
| 47 |
|
| 48 |
* https://github.com/MLton/mlton/blob/master/mlton/ssa/common-arg.fun[`common-arg.fun`] |
| 49 |
|
| 50 |
== Details and Notes |
| 51 |
|
| 52 |
Three analyses were originally proposed to drive the optimization |
| 53 |
transformation. Only the _Dominator Analysis_ is currently |
| 54 |
implemented. (Implementations of the other analyses are available in |
| 55 |
the <<Sources#,repository history>>.) |
| 56 |
|
| 57 |
=== Syntactic Analysis |
| 58 |
|
| 59 |
The simplest analysis I could think of maintains |
| 60 |
---- |
| 61 |
varInfo: Var.t -> Var.t option list ref |
| 62 |
---- |
| 63 |
initialized to `[]`. |
| 64 |
|
| 65 |
* For each variable `v` bound in a `Statement.t` or in the |
| 66 |
`Function.t` args, then `List.push(varInfo v, NONE)`. |
| 67 |
* For each `L (x1, ..., xn)` transfer where `(a1, ..., an)` are the |
| 68 |
formals of `L`, then `List.push(varInfo ai, SOME xi)`. |
| 69 |
* For each block argument a used in an unknown context (e.g., |
| 70 |
arguments of blocks used as continuations, handlers, arith success, |
| 71 |
runtime return, or case switch labels), then |
| 72 |
`List.push(varInfo a, NONE)`. |
| 73 |
|
| 74 |
Now, any block argument `a` such that `varInfo a = xs`, where all of |
| 75 |
the elements of `xs` are equal to `SOME x`, can be optimized by |
| 76 |
setting `a = x` at the beginning of the block and dropping the |
| 77 |
argument from `Goto` transfers. |
| 78 |
|
| 79 |
That takes care of the example above. We can clearly do slightly |
| 80 |
better, by changing the transformation criteria to the following: any |
| 81 |
block argument a such that `varInfo a = xs`, where all of the elements |
| 82 |
of `xs` are equal to `SOME x` _or_ are equal to `SOME a`, can be |
| 83 |
optimized by setting `a = x` at the beginning of the block and |
| 84 |
dropping the argument from `Goto` transfers. This optimizes a case |
| 85 |
like: |
| 86 |
---- |
| 87 |
L_1 () |
| 88 |
... z1 = ? ... |
| 89 |
L_3 (x, y, z1) |
| 90 |
L_2 () |
| 91 |
... z2 = ? ... |
| 92 |
L_3(x, y, z2) |
| 93 |
L_3 (a, b, c) |
| 94 |
... w = ? ... |
| 95 |
case w of |
| 96 |
true => L_4 | false => L_5 |
| 97 |
L_4 () |
| 98 |
... |
| 99 |
L_3 (a, b, w) |
| 100 |
L_5 () |
| 101 |
... |
| 102 |
---- |
| 103 |
where a common argument is passed to a loop (and is invariant through |
| 104 |
the loop). Of course, the <<LoopInvariant#>> optimization pass would |
| 105 |
normally introduce a local loop and essentially reduce this to the |
| 106 |
first example, but I have seen this in practice, which suggests that |
| 107 |
some optimizations after <<LoopInvariant#>> do enough simplifications |
| 108 |
to introduce (new) loop invariant arguments. |
| 109 |
|
| 110 |
=== Fixpoint Analysis |
| 111 |
|
| 112 |
However, the above analysis and transformation doesn't cover the cases |
| 113 |
where eliminating one common argument exposes the opportunity to |
| 114 |
eliminate other common arguments. For example: |
| 115 |
---- |
| 116 |
L_1 () |
| 117 |
... |
| 118 |
L_3 (x) |
| 119 |
L_2 () |
| 120 |
... |
| 121 |
L_3 (x) |
| 122 |
L_3 (a) |
| 123 |
... |
| 124 |
L_5 (a) |
| 125 |
L_4 () |
| 126 |
... |
| 127 |
L_5 (x) |
| 128 |
L_5 (b) |
| 129 |
... |
| 130 |
---- |
| 131 |
|
| 132 |
One pass of analysis and transformation would eliminate the argument |
| 133 |
to `L_3` and rewrite the `L_5(a)` transfer to `L_5 (x)`, thereby |
| 134 |
exposing the opportunity to eliminate the common argument to `L_5`. |
| 135 |
|
| 136 |
The interdependency the arguments to `L_3` and `L_5` suggest |
| 137 |
performing some sort of fixed-point analysis. This analysis is |
| 138 |
relatively simple; maintain |
| 139 |
---- |
| 140 |
varInfo: Var.t -> VarLattice.t |
| 141 |
---- |
| 142 |
{empty}where |
| 143 |
---- |
| 144 |
VarLattice.t ~=~ Bot | Point of Var.t | Top |
| 145 |
---- |
| 146 |
(but is implemented by the <<FlatLattice#>> functor with a `lessThan` |
| 147 |
list and `value ref` under the hood), initialized to `Bot`. |
| 148 |
|
| 149 |
* For each variable `v` bound in a `Statement.t` or in the |
| 150 |
`Function.t` args, then `VarLattice.<= (Point v, varInfo v)` |
| 151 |
* For each `L (x1, ..., xn)` transfer where `(a1, ..., an)` are the |
| 152 |
formals of `L`}, then `VarLattice.<= (varInfo xi, varInfo ai)`. |
| 153 |
* For each block argument a used in an unknown context, then |
| 154 |
`VarLattice.<= (Point a, varInfo a)`. |
| 155 |
|
| 156 |
Now, any block argument a such that `varInfo a = Point x` can be |
| 157 |
optimized by setting `a = x` at the beginning of the block and |
| 158 |
dropping the argument from `Goto` transfers. |
| 159 |
|
| 160 |
Now, with the last example, we introduce the ordering constraints: |
| 161 |
---- |
| 162 |
varInfo x <= varInfo a |
| 163 |
varInfo a <= varInfo b |
| 164 |
varInfo x <= varInfo b |
| 165 |
---- |
| 166 |
|
| 167 |
Assuming that `varInfo x = Point x`, then we get `varInfo a = Point x` |
| 168 |
and `varInfo b = Point x`, and we optimize the example as desired. |
| 169 |
|
| 170 |
But, that is a rather weak assumption. It's quite possible for |
| 171 |
`varInfo x = Top`. For example, consider: |
| 172 |
---- |
| 173 |
G_1 () |
| 174 |
... n = 1 ... |
| 175 |
L_0 (n) |
| 176 |
G_2 () |
| 177 |
... m = 2 ... |
| 178 |
L_0 (m) |
| 179 |
L_0 (x) |
| 180 |
... |
| 181 |
L_1 () |
| 182 |
... |
| 183 |
L_3 (x) |
| 184 |
L_2 () |
| 185 |
... |
| 186 |
L_3 (x) |
| 187 |
L_3 (a) |
| 188 |
... |
| 189 |
L_5(a) |
| 190 |
L_4 () |
| 191 |
... |
| 192 |
L_5(x) |
| 193 |
L_5 (b) |
| 194 |
... |
| 195 |
---- |
| 196 |
|
| 197 |
Now `varInfo x = varInfo a = varInfo b = Top`. What went wrong here? |
| 198 |
When `varInfo x` went to `Top`, it got propagated all the way through |
| 199 |
to `a` and `b`, and prevented the elimination of any common arguments. |
| 200 |
What we'd like to do instead is when `varInfo x` goes to `Top`, |
| 201 |
propagate on `Point x` -- we have no hope of eliminating `x`, but if |
| 202 |
we hold `x` constant, then we have a chance of eliminating arguments |
| 203 |
for which `x` is passed as an actual. |
| 204 |
|
| 205 |
=== Dominator Analysis |
| 206 |
|
| 207 |
Does anyone see where this is going yet? Pausing for a little |
| 208 |
thought, <<MatthewFluet#>> realized that he had once before tried |
| 209 |
proposing this kind of "fix" to a fixed-point analysis -- when we were |
| 210 |
first investigating the <<Contify#>> optimization in light of John |
| 211 |
Reppy's CWS paper. Of course, that "fix" failed because it defined a |
| 212 |
non-monotonic function and one couldn't take the fixed point. But, |
| 213 |
<<StephenWeeks#>> suggested a dominator based approach, and we were |
| 214 |
able to show that, indeed, the dominator analysis subsumed both the |
| 215 |
previous call based analysis and the cont based analysis. And, a |
| 216 |
moment's reflection reveals further parallels: when |
| 217 |
`varInfo: Var.t \-> Var.t option list ref`, we have something analogous |
| 218 |
to the call analysis, and when `varInfo: Var.t \-> VarLattice.t`, we |
| 219 |
have something analogous to the cont analysis. Maybe there is |
| 220 |
something analogous to the dominator approach (and therefore superior |
| 221 |
to the previous analyses). |
| 222 |
|
| 223 |
And this turns out to be the case. Construct the graph `G` as follows: |
| 224 |
---- |
| 225 |
nodes(G) = {Root} U Var.t |
| 226 |
edges(G) = {Root -> v | v bound in a Statement.t or |
| 227 |
in the Function.t args} U |
| 228 |
{xi -> ai | L(x1, ..., xn) transfer where (a1, ..., an) |
| 229 |
are the formals of L} U |
| 230 |
{Root -> a | a is a block argument used in an unknown context} |
| 231 |
---- |
| 232 |
|
| 233 |
Let `idom(x)` be the immediate dominator of `x` in `G` with root |
| 234 |
`Root`. Now, any block argument a such that `idom(a) = x <> Root` can |
| 235 |
be optimized by setting `a = x` at the beginning of the block and |
| 236 |
dropping the argument from `Goto` transfers. |
| 237 |
|
| 238 |
Furthermore, experimental evidence suggests (and we are confident that |
| 239 |
a formal presentation could prove) that the dominator analysis |
| 240 |
subsumes the "syntactic" and "fixpoint" based analyses in this context |
| 241 |
as well and that the dominator analysis gets "everything" in one go. |
| 242 |
|
| 243 |
=== Final Thoughts |
| 244 |
|
| 245 |
I must admit, I was rather surprised at this progression and final |
| 246 |
result. At the outset, I never would have thought of a connection |
| 247 |
between <<Contify#>> and <<CommonArg#>> optimizations. They would seem |
| 248 |
to be two completely different optimizations. Although, this may not |
| 249 |
really be the case. As one of the reviewers of the ICFP paper said: |
| 250 |
____ |
| 251 |
I understand that such a form of CPS might be convenient in some |
| 252 |
cases, but when we're talking about analyzing code to detect that some |
| 253 |
continuation is constant, I think it makes a lot more sense to make |
| 254 |
all the continuation arguments completely explicit. |
| 255 |
|
| 256 |
I believe that making all the continuation arguments explicit will |
| 257 |
show that the optimization can be generalized to eliminating constant |
| 258 |
arguments, whether continuations or not. |
| 259 |
____ |
| 260 |
|
| 261 |
What I think the common argument optimization shows is that the |
| 262 |
dominator analysis does slightly better than the reviewer puts it: we |
| 263 |
find more than just constant continuations, we find common |
| 264 |
continuations. And I think this is further justified by the fact that |
| 265 |
I have observed common argument eliminate some `env_X` arguments which |
| 266 |
would appear to correspond to determining that while the closure being |
| 267 |
executed isn't constant it is at least the same as the closure being |
| 268 |
passed elsewhere. |
| 269 |
|
| 270 |
At first, I was curious whether or not we had missed a bigger picture |
| 271 |
with the dominator analysis. When we wrote the contification paper, I |
| 272 |
assumed that the dominator analysis was a specialized solution to a |
| 273 |
specialized problem; we never suggested that it was a technique suited |
| 274 |
to a larger class of analyses. After initially finding a connection |
| 275 |
between <<Contify#>> and <<CommonArg#>> (and thinking that the only |
| 276 |
connection was the technique), I wondered if the dominator technique |
| 277 |
really was applicable to a larger class of analyses. That is still a |
| 278 |
question, but after writing up the above, I'm suspecting that the |
| 279 |
"real story" is that the dominator analysis is a solution to the |
| 280 |
common argument optimization, and that the <<Contify#>> optimization is |
| 281 |
specializing <<CommonArg#>> to the case of continuation arguments (with |
| 282 |
a different transformation at the end). (Note, a whole-program, |
| 283 |
inter-procedural common argument analysis doesn't really make sense |
| 284 |
(in our <<SSA#>> <<IntermediateLanguage#>>), because the only way of |
| 285 |
passing values between functions is as arguments. (Unless of course |
| 286 |
in the case that the common argument is also a constant argument, in |
| 287 |
which case <<ConstantPropagation#>> could lift it to a global.) The |
| 288 |
inter-procedural <<Contify#>> optimization works out because there we |
| 289 |
move the function to the argument.) |
| 290 |
|
| 291 |
Anyways, it's still unclear to me whether or not the dominator based |
| 292 |
approach solves other kinds of problems. |
| 293 |
|
| 294 |
=== Phase Ordering |
| 295 |
|
| 296 |
On the downside, the optimization doesn't have a huge impact on |
| 297 |
runtime, although it does predictably saved some code size. I stuck |
| 298 |
it in the optimization sequence after <<Flatten#>> and (the third round |
| 299 |
of) <<LocalFlatten#>>, since it seems to me that we could have cases |
| 300 |
where some components of a tuple used as an argument are common, but |
| 301 |
the whole tuple isn't. I think it makes sense to add it after |
| 302 |
<<IntroduceLoops#>> and <<LoopInvariant#>> (even though <<CommonArg#>> |
| 303 |
get some things that <<LoopInvariant#>> gets, it doesn't get all of |
| 304 |
them). I also think that it makes sense to add it before |
| 305 |
<<CommonSubexp#>>, since identifying variables could expose more common |
| 306 |
subexpressions. I would think a similar thought applies to |
| 307 |
<<RedundantTests#>>. |