Open sampsyo opened 4 months ago
In case it is of use, I think I was able to slightly modify the interpreter to handle the swap problem, at least in the specific example in the issue. My modified interpreter is at https://github.com/0adb/bril. Instructions for compiling it are in the README, but the main changes are in bril2i.ts. The main change was to add a second environment variable to the state maintained during function execution, which would be updated to the most recent environment only when changing labels, and which would be what a phi instruction would check when assigning to a variable.
I uploaded the example given in the issue as test/interp/ssa/ssa-swap.bril
in the fork, and was able to obtain the following results (behavior from the original bril interpreter, and then from the new one):
arthur@myMachineName:~/Documents/bril$ bril2json < test/interp/ssa/ssa-swap.bril | brili
0 1
1 1
1 1
1 1
1 1
1 1
arthur@myMachineName:~/Documents/bril$ bril2json < test/interp/ssa/ssa-swap.bril | bril2i
0 1
1 0
0 1
1 0
0 1
1 0
I also checked that the other SSA test cases would behave the same between brili.ts and bril2i.ts and they seem to be good. Have not tested the other interpreter tests though.
@pervognsen pointed out on Mastodon that
phi
instructions behave in a weird and probably bad way. Because they are "just normal instructions," they take effect immediately, i.e., all subsequent instructions immediately see the update to the variable they write. That includes otherphi
instructions that use the same variable on their right-hand sides.Here's a self-contained example showing this behavior:
Under Bril's current semantics (as implemented by the reference interpreter and my
from_ssa.py
example), the twophi
s execute in order, so the first one clobbersx1
before the second one reads it.This behavior is nonstandard and problematic. Under a more normal SSA semantics, the
phi
s would execute "simultaneously," i.e., they would all read the RHS values from the start of the basic block, never from earlierphi
s in the same basic block. This program would then swap the values ofx1
andy1
on every trip around the loop.In the context of out-of-SSA transformations, this issue is called the "swap problem" in this classic Preston Briggs banger.
Anyway, my take here is that this (along with #108) is yet another consequence of my trying too hard to treat φ-nodes as "just normal Bril instructions." I was enamored with the idea that Bril's SSA form could be a tiny extension on top of the baseline non-SSA language, that interpreters wouldn't have to work too hard to tack on this one additional feature, and that we wouldn't need awkward restrictions like having all the φ-nodes appear at the beginnings of basic blocks. I think this has worked out poorly, and an SSA variant needs deeper changes to the language. At the very least, we cannot treat φ-nodes as normal instructions that read their arguments and write their results in order, like any other instruction.
If we do a more holistic redesign, I wonder whether something closer to MLIR-style basic block arguments might be a better choice. If nothing else, they make it more explicit that φ-nodes are semantically distinct from other instructions.