ArgosOfIthica/scalu

[DEV] Add peephole optimization for computations with balanced head/tail

ArgosOfIthica opened this issue · 1 comments

Is your feature request related to a problem? Please describe.
In generated configurations, a computation head can compile to an alias with a tail of size 1, where the only tail element is another computation call. An example:

alias %ex1 "%ex2"

Suppose there is a computation c and a head h and a tail t, where t is a single computation call. By convention, all prefixed heads correspond to a computation and not a state. All callsites containing h, therefore, call h or assign a state s to h. Calling h is trivially equal to calling t; calling h always calls t, as h is a computation, which is immutable.

Assigning a state s to h is also equal to assigning s to t. For there to be an inequality, there must be a way of telling the difference between an assignment between s->h and s->t, but this cannot be done. h and t are immutable. If we call s where s->h, we call h as well, which is equal to calling t, which the exact output of calling s where s->t.

Describe the solution you'd like
Let the optimizer look for the above pattern in the output config. Upon finding a match, delete the assignment of h->t and change all instances of h in the config to t. Do this for all c until matches are exhausted. In theory, this opens up the possibility of having the deduplication algorithm run again, but it's unclear that t's propagation will actually create a non-zero number of effective duplicates with the current architecture.

b0126ac

In example a->b, b->c, we now replace all instances of a with b. This is a slight adjustment from the plan, where we originally wanted to replace all valid instances of b with a. This means that call sites, which could be a, cannot be optimized away, still need a layer of indirection through b (which cannot be a call site). This is fine, as a single layer of indirection is not extremely costly, computationally.