Hacker News new | ask | show | jobs
by glitchc 8 days ago
I don't think GCC is doing something special, rather that -O3 might be optimizing out the subtraction. Try with -O0, I suspect you will get the same result as clang.
2 comments

The GCC compiler changes the order of the operations such that the assignment happens after the print for x.

call __printf_chk@PLT mov ecx, DWORD PTR 0[rbp] mov rdx, rbp mov edi, 2 mov DWORD PTR -96[rbx], 7

The "special" thing of note is that GCC considers it acceptable to change the semantics of this program depending on the optimization level.
That depends entirely on how you define “the semantics.” Any optimizing compiler has to draw a line between what the program is meant to do and how the emitted code actually makes it happen. The proper location of that line is not obvious. Can it turn x++,x++ into x+=2? That’s normally fine, potentially disastrous if x is a memory mapped register on an external device.

C and C++ happen to have a particularly restrictive view on what “the semantics” of a program really are, such that there are a lot of things you can write in syntactically-legal C that officially erase the semantic meaning of the code. Within that definition of the program’s semantics, gcc’s optimizer is not supposed to change them, and any such change would be a bug. You’re taking a more expansive view of program semantics than the actual language specifies. If you do that, then any optimizing compiler is going to consider it to be acceptable to violate your special bespoke definition of them.

> Can it turn x++,x++ into x+=2

For this particular example the line should be clear. If the variable is volatile, memory access can't be removed or reordered. If it's not volatile, everything is fair game.

Mandatory Godbolt link: https://godbolt.org/z/fjKs4hbjo

It’s clear by the language spec. It’s not clear based on how people want it to work or think it should work. And the inevitable stream of comments complaining about C compiler optimizers every time this gets discussed tells us that people are not willing to just consider where the language spec draws the line.
More like code that falls through might be reordered in the binary and the C abstract machine has no concept of that.

Or strange issues with optimizing infinite loops and the Halting problem preventing a full automated solution.

The C compilers like GCC just hide stuff away and break things silently.

Because doing so doesn’t change the semantics of the program as defined by the language.

If you open the possibility of other notions of “the semantics” besides what the language spec lays out, then changing those semantics is an inherent part of an optimizer.

What people (IMO fairly reasonably) want is a different C language spec with less undefined behavior and compilers that follow that spec. But people don’t think about it clearly and skip major parts of that picture when complaining about it.

We seem to agree.

Many people program for their hardware (what else can be actually tested) or maybe qemu or virtualbox or vmware or a specific external microcontroller.

Very few people want to program for the C abstract machine. The C abstract machine doesn’t even really have dlopen(3). It also has to be compatible with Harvard architecture, von Neumann architecture, two's complement signed machine word integers, one's complement signed machine word integers, and sign-magnitude signed machine word integers.

> The proper location of that line is not obvious.

If that's true, you have a bad language.

What is known to be the case with C, but this is not a desirable nor a necessary state of affairs. A language can and should have defined semantics.

I’m not talking about whether a language has defined semantics. C’s semantics are plenty well defined, much better than the current crop of more modern languages that are mostly defined by their single implementation.

I’m talking about the question of what should be covered by the official semantics. That’s inherently subjective and a matter of opinion.

Gcc’s optimizer does not accept changing language-level semantics based on which optimizations are performed. That the above comment thinks it does means that the commenter has a different notion of what should count as “the semantics” from what the language spec says. Which is a perfectly legitimate opinion, but if you take that position then you have to accept that other people will have other ideas of what should count, and therefore any optimizer might change “the semantics” of code according to at least some of those ideas.