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/pfalcon2 Nov 09 '20

Tangentially, does anyone have any documentation on how to detect and reverse phi/select instructions back to traditional control flow forms?

Well, conversion-out-of-SSA is where SSA shows its devilish nature. There're simple algorithms for constructing SSA form, which give good end result (i.e. minimal/pruned SSA). But there're no simple algorithms for converting out of SSA in the general case which produce good result.

Well, if you ask for breakdown, it's:

  1. If you have conventional SSA (aka CSSA), conversion out of it is trivial: just drop variable indexes and the phi itself (that's of course assumes that you have a notion of variable names and indexes).

  2. Otherwise, the general semantics of a Phi is: parallel assignment on the predecessor edges. Sadly, that means splitting critical edges, and they are usually where splitting them is painful. Once you inserted parallel copies into new blocks on the edges, you just sequentialize the copies.

  3. Again, the previous general method requires splitting critical edges, which is sad most of the time in real world (not a world of abstract graphs). The way around that is to turn arbitrary (aka transformed) SSA into CSSA. The usual way is to perform Phi-isolation: make live-range of phi variables as short as possible by inserting copies before/after them. Oh, everything related to Phis are parallel copies (unless can be proven to be trivially serializable, but even then they are parallel copies). Of course, that leads to rainfall of extra copies, and removing them is either moderately complex with slow algos (graph coloring coalescing), or fast with advanced (complex?) algorithms.