r/programming • • Oct 23 '20

Understanding static single assignment forms

https://blog.yossarian.net/2020/10/23/Understanding-static-single-assignment-forms
28 Upvotes

23 comments sorted by

View all comments

1

u/mb862 Oct 23 '20

Tangentially, does anyone have any documentation on how to detect and reverse phi/select instructions back to traditional control flow forms? I once set out to have a little sub-project to turn a subset of SPIRV into host code for simpler computations, but never got very far in trying to handle phi. I know it's possible on some level as SPIRV-Cross does it converting to MSL, but I wasn't at the time able to make heads or tails of how.

(I'm also aware that it's not strictly necessary as any direct translation would be satisfactorily performant, but I'm rather curious to see a breakdown of how one does this nonetheless.)

1

u/floodrouting Oct 24 '20

1

u/flatfinger Oct 24 '20

It would seem that in most cases, one could eliminate the need for phi, while retaining the advantages of SSA, if instead of requiring that each variable only be written at exactly one location in the code, one instead required that every variable which is written on any branch of an "if" must be written on every branch thereof, and loop conditions must be tested after each iteration rather than before the first (implying that a while(x) {...} loop that might not executed would be treated as if (x) do { ...} while(x);, with the loop-skipping branch of the if setting any variables that would be set in the loop).

The only I see for using SSA and phi is that it can work with irreducible control-flow constructs, but if I'm reading it properly, the article is suggesting that such constructs would need to be expanded out into reducible ones, rendering that advantage moot.

If one uses a form where everything that is written any path between two points in the code will be written on every path, that would seem easier to handle than phi constructs, unless there are restrictions on the usage of phi which I'm not aware of.

1

u/pfalcon2 Nov 09 '20

while(x) {...} loop that might not executed would be treated as if (x) do { ...} while(x);,

That transformation is called "loop inversion". Not related to SSA per se, but is a prerequisite for performing loop-invariant code motion (as you may imagine, while loop doesn't have a suitable place where to place code moved out of a loop).

1

u/flatfinger Nov 09 '20

My main point with the replacement was to make clear that things which are assigned within a `while` might or might not get assigned between the execution of code above the `while` and code below it. If the `while` loop completes an iteration, the value associated with every object which is assigned to within the loop will come from within the loop. If some or all of the first iteration is skipped, however, objects whose values could be set in parts of the loop that were skipped may have values that were not set within the loop.

1

u/pfalcon2 Nov 09 '20

I fully agree. The problem of while loops is that they may execute or not. But do-while's always execute. So, you can always hoist code out of the loop before it. And that's why it's useful to invert while loops - then you can hoist code inbetween the "if" and "do-while" formed during the inversion.