-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
phi
instructions have the "swap problem"
#330
Comments
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
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. |
This fixes issue 330: sampsyo#330 The approach (copy the environment at the end of the last basic block) was inspired/copied from the comment on the Bril issue: sampsyo#330 (comment)
This fixes issue 330: sampsyo#330 The approach (copy the environment at the end of the last basic block) was inspired/copied from the comment on the Bril issue: sampsyo#330 (comment)
This fixes issue 330: sampsyo#330 The approach (copy the environment at the end of the last basic block) was inspired/copied from the comment on the Bril issue: sampsyo#330 (comment)
This fixes issue 330: sampsyo#330 The approach (copy the environment at the end of the last basic block) was inspired/copied from the comment on the Bril issue: sampsyo#330 (comment)
This fixes issue 330: sampsyo#330 The approach (copy the environment at the end of the last basic block) was inspired/copied from the comment on the Bril issue: sampsyo#330 (comment)
@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.
The text was updated successfully, but these errors were encountered: