about summary refs log tree commit diff stats
path: root/058stream-equal.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-03-06 00:06:42 -0800
committerKartik Agaram <vc@akkartik.com>2020-03-06 00:06:42 -0800
commit4032286f9bb231139b733e5444b862006d3b9119 (patch)
tree04f3c32508c62ff28f99f8355459df3529a99b02 /058stream-equal.subx
parent0657fc16ab8d8f86d84b52a8998715c32bc78eb9 (diff)
downloadmu-4032286f9bb231139b733e5444b862006d3b9119.tar.gz
6082 - bugfix in spilling register vars
In the process I'm starting to realize that my approach to avoiding spills
isn't ideal. It works for local variables but not to avoid spilling outputs.

To correctly decide whether to spill to an output register or not, we really
need to analyze when a variable is live. If we don't do that, we'll end
up in one of two bad situations:

a) Don't spill the outermost use of an output register (or just the outermost
scope in a function). This is weird because it's hard to explain to the
programmer why they can overwrite a local with an output above a '{' but
not below.

b) Disallow overwriting entirely. This is easier to communicate but quite
inconvenient. It's nice to be able to use eax for some temporary purpose
before overwriting it with the final result of a function.

If we instead track liveness, things are convenient and also easier to
explain. If a temporary is used after the output has been written that's
an obvious problem: "you clobbered the output". (It seems more reasonable
to disallow multiple live ranges for the output. Once an output is written
it can only be shadowed in a nested block.)

That's the bad news. Now for some good news:

One lovely property Mu the language has at the moment is that live ranges
are guaranteed to be linear segments of code. We don't need to analyze
loop-carried dependences. This means that we can decide whether a variable
is live purely by scanning later statements for its use. (Defining 'register
use' is slightly non-trivial; primitives must somehow specify when they
read their output register.)

So we don't actually need to worry about a loop reading a register with
one type and writing to another type at the end of an iteration. The only
way that can happen is if the write at the end was to a local variable,
and we're guaranteeing that local variables will be reclaimed at the end
of the iteration.

So, the sequence of tasks:
  a) compute register liveness
  b1) verify that all register variables used at any point in a program
  are always the topmost use of that register.
  b2) decide whether to spill/shadow, clobber or flag an error.

There's still the open question of where to attach liveness state. It can't
be on a var, because liveness varies by use of the var. It can't be on a
statement because we may want to know the liveness of variables not referenced
in a given statement. Conceptually we want a matrix of locals x stmts (flattened).
But I think it's simpler than that. We just want to know liveness at the
time of variable declarations. A new register variable can be in one of
three states w.r.t. its previous definition: either it's shadowing it,
or it can clobber it, or there's a conflict and we need to raise an error.

I think we can compute this information for each variable definition by
an analysis similar to existing ones, maintaining a stack of variable definitions.
The major difference is that we don't pop variables when a block ends.
Details to be worked out. But when we do I hope to get these pending tests
passing.
Diffstat (limited to '058stream-equal.subx')
0 files changed, 0 insertions, 0 deletions