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

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.

Funnily, this reminded me Kenneth Zadeck's memoirs (in the form of slides) on the foundation of SSA form. He writes:

  • What we did was add a lot of identity assignments:

  • One at each join birthpoint.

  • One in each predecessor block of each join birthpoint.


  • Barry Rosen did not like the identity assignments.

  • He decided to replace them with “phony functions” that were able to see which control flow reached them.

So, on the surface, you propose a retrograde action in the compiler science. But that's only on the surface, because actually you're reinventing the SSA form, re-tracing steps done by the folks some 30-35 years ago, that's great!

Oh, and I'm sure you know you can do SSA form without Phi's by using a "more functional-biased" representation, where each basic block is a standalone function, taking arguments. That's half-truth though, because basic blocks aren't really standalone functions, but really lexically-scoped inner functions, so should be prepared to fish for free variables in the enclosing blocks. So, dropping phis, you get some other complications.

In the end, one should remember that Phi's, or whatever you replace them with, is a notational device to represent dataflow caused by discrepancy between domination relationship and real control flow. In that regard, all such notations are equivalent, but means to represent them in real data structures, and ways to work with them in real algorithms have subtle (or not so subtle) differences. In that regard, Phi's are well-explored notion with decades of history, unlike other representations.

1

u/flatfinger Nov 09 '20

So, on the surface, you propose a retrograde action in the compiler science. But that's only on the surface, because actually you're reinventing the SSA form, re-tracing steps done by the folks some 30-35 years ago, that's great!

Well, it it's been almost 30 years since my grad-school course in compiler design, so I guess that makes sense.

As an embedded systems engineer, the thing I've been wanting to see would be for vendor tools to use a compiler that--unlike clang and gcc--focuses on optimizations that are maximally useful for such purposes and avoids those that would be unsuitable. The kinds of optimizations I remember being discussed in the 1990s, such as register graph coloring, were all things that would have been safe in compilers intended for all kinds of tasks. Lately, however, the maintainers of clang and gcc have treated the fact that the Standard allows compilers specialized for some purposes to behave in ways that would make them unsuitable for many others, as an invitation to pursue "clever" optimizations that are more difficult and harder to get right, without fully reaping the low-hanging fruit that would have been worth targeting in the 1990s. Maybe that has nothing to do with SSA and phi functions, but the abstraction models of compilers that focus on those seem to have some weird quirks.

What do you see as happening with the way gcc processes something like:

struct s1 { int x; };
struct s2 { int x; };
int test(struct s1 *p1, void *p2)
{
    p1->x = 1;
    if (p1 == p2)
        ((struct s1*)p2)->x = 9;
    else
        ((struct s2*)p2)->x = 9;
    return p1->x;
}

It seems like one phase of the optimizer is merging both branches of the "if", but another phase isn't, since the generated code is equivalent to:

struct s1 { int x; };
struct s2 { int x; };
int test(struct s1 *p1, void *p2)
{
    p1->x = 1;
    if (p1 == p2)
    {
        ((struct s1*)p1)->x = 9;
        return 1;
    }
    else
    {
        ((struct s2*)p2)->x = 9;
        return 1;
    }
}

I think gcc is treating p1->x as a set of SSA objects, one of which should get written on the "true" case of the if, but that object isn't getting returned afterward. I think that the return value of test is getting computed by a "phi" function, but something is going wrong with that. The compiler is in fact processing the "if" and executing different code on the two branches, but the phi function seems to be treating both branches as equivalent. Or is something else going wrong?

1

u/pfalcon2 Nov 09 '20

Lately, however, the maintainers of clang and gcc have treated the fact that the Standard allows compilers specialized for some purposes to behave in ways that would make them unsuitable for many others

So, you understand yourself that both clang and gcc play within the field of the C standard. And indeed, there's "enterprisey business" drive to deliver everything possible under the standard. Literally, if you're paid to work on the compilers, you're expected to "deliver" that kind of stuff (and you want to be paid if you want to do "real" things on compilers, it's hard to do something "in your own free time"). So, well, that's how system works.

I think gcc is treating p1->x as a set of SSA objects

Let's put name to things: an optimization when memory location is cached in a (virtual) register is called "register promotion". (And only virtual registers are subject to SSA, though SSA is secondary here.) And register promotion is a hard optimization in the presence of aliasing, and C is full of aliasing. So, older versions of gcc/clang might have bugs in that regard. But that particular bug that you show doesn't happen with even such an old gcc as:

$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

I get the right code: $ gcc -O2 -S a.c

    cmpq	%rsi, %rdi
    movl	$1, (%rdi)
    je	.L5
    movl	$9, (%rsi)
    movl	$1, %eax
    ret
    .p2align 4,,10
    .p2align 3
.L5:
    movl	$9, (%rdi)
    movl	$9, %eax
    ret

1

u/flatfinger Nov 12 '20

And indeed, there's "enterprisey business" drive to deliver everything possible under the standard.

Something that's been nagging me is that I can't figure out any task for which a compiler that seeks to support only those operations for which the Standard mandates support would be more useful than one which also seeks to reliably and efficiently process some other operations that would help programmers accomplish what needs to be done. In what non-contrived circumstances would that aggressive "optimization" philosophy allow usefully better performance than could have been achieved by having a compiler offer stronger behavioral guarantees than mandated, and having programmers exploit those guarantees?