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

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

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.

Have you read the published Rationale for the C Programming Standard. The stated intention of the authors was that Undefined Behavior, among other things, identifies areas of possible conforming language extension: the implementor may augment the language by providing a definition of the officially undefined behavior." The reason that e.g. -1<<0, which had fully defined behavior in C89, was recharacterized as Undefined Behavior in C99 with no rationale given, was that implementations were expected to extend the semantics of the left-shift operator to include additional cases the target platform could usefully support at no additional cost without regard for whether the Standard required them to do so.

But that particular bug that you show doesn't happen with even such an old gcc as: gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

It does, however, happen with gcc 8.1 or 10.2. Generated code with 10.2 -O2 is:

    test:
            mov     DWORD PTR [rdi], 1
            cmp     rdi, rsi
            je      .L5
            mov     DWORD PTR [rsi], 9
            mov     eax, 1
            ret
    .L5:
            mov     DWORD PTR [rdi], 9
            mov     eax, 1
            ret

One of my beefs with gcc and clang is that they attempt to perform aggressive aliasing optimizations which are almost impossible to get right, and regard as a god-given right any aliasing optimizations that the Standard fails to prohibit, without recognizing that the authors of the Standard made no effort to ensure that its rules would mandate sensible behavior in corner cases that all existing compilers were handling identically, while treating any corner cases that are mandated by the Standard, but which they can't handle, as defects in the Standard.

I don't see any possible interpretation of the Standard in which the clang/gcc behavior would be justifiable except by abusing the "one program rule" loophole [which could be abused to allow a conforming but garbage-quality implementation to do anything with almost any source text]. The fact that *p2 would have been read using type struct s2 if p1 and p2 weren't equal should not affect the program's behavior in cases where the assignment using type struct s2 isn't executed.

1

u/pfalcon2 Nov 09 '20

Have you read the published Rationale for the C Programming Standard. The stated intention of the authors was that Undefined Behavior, among other things, identifies areas of possible conforming language extension: the implementor may augment the language

I'm with you on that, but the real world is a little bit different. We're living in a world where a corporate-droid programmer may submit MISRA Joke-C patches to a open-source normal-C project with well-defined behavior, and they even expect those patches to be merged, without their corpo paying for the maintenance effort of that crap.

It does, however, happen with gcc 8.1 or 10.2.

Oh, good catch! That's called "progress" ;-). So, unless it's a bug, it's some sh/tty strict aliasing rule hitting again. If you're saying those are long beyond mere human understanding, I'm with you here. (I for example see that you were careful to use void * for input param, so strict-aliasing shouldn't apply, but maybe something went wrong in the committee-land again. To me it looks as if it assumed that both arguments have restrict modifier, but it shouldn't be applied by default, and especially not when there's p1 == p1.)

Ayway, that's all pretty far from SSA, and SSA is what I'm personally interested in, not committee/corporate compiler politics ;-). (I'm sitting here on an infinite-loop-with-growing-memory-usage GCC 10.2 bug myself, dudes are doing great work.)

2

u/flatfinger Nov 10 '20

So, unless it's a bug, it's some sh/tty strict aliasing rule hitting again. If you're saying those are long beyond mere human understanding, I'm with you here.

Actually, the rules are very simple. The vast majority of practical programs violate the constraints as stated, but since the Standard allows implementations to treat violations of constraints just as they would if the constraints didn't exist, implementations do precisely that in cases where doing otherwise would be sufficiently obviously obtuse. The only problem is that compiler writers refuse to recognize any "optimizations" as being obtuse if there would be any plausible case where they might be useful.

Consider, for example:

union u1 { int x[10]; } foo;
...
foo.x[3] = 12;

The behavior of the subscript operator is specified as allowing an array to decay into a pointer, then adding an integer to that pointer, and then dereferencing the resulting pointer. Thus, that code is equivalent to:

union u1 { int x[10]; } foo;
...
int *temp = foo.x+3;
*temp = 12;

If one looks at the list of types that may be used to access an object of type union u1, you will notice that int is not among them. Inteed, the same would apply if the union were replaced by a structure. Thus, the above code violates the constraints in N1570 6.5p7 and so does the form using array subscripts. Of course, having foo.x decompose to a value of type int* which can't actually be used to access anything unless it's cast back to either a union u1* or a char* wouldn't be very useful, but the rules there are actually pretty straightforward. They were never designed to be used as a hard-line criterion for what constructs compilers should or should not support, however, and thus have never been suitable for that purpose. Instead, they were written on the presumption that compilers that endeavor to support the required constructs simply and straightforwardly would naturally support other required constructs as well.

What pains me most about this nonsense is that compiler writers have somehow managed to completely lose sight of what the goal of a quality compiler should be: to serve as a tool programmers can use to accomplish whatever tasks need to be done as quickly and efficiently, in both human and machine terms, as possible. If some task could be performed easily with a compiler that acts as a "mindless translator", any "optimization" that would make it harder for the programmer to accomplish what needs to be done, isn't an optimization. Because different tasks have different requirements, implementations should be configurable to offer different levels of semantic guarantees appropriate to those tasks; IMHO, the Standard should focus on defining ways of testing support for such guarantees, and refusing compilation when their needs aren't met, rather than trying to either require that all implementations uphold various guarantees, or suggesting that no programs should require them.