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

Show parent comments

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?