GCC Order Guarantee

I have to delve into C and block free programming. With this, I wonder what gcc guarantee can give me that a program runs exactly the same as I write it, and no register optimization is performed at a particular step, and no operation changes in its ordering.

As I understand it at present, it guarantees that operations with memory occur in the same order and method calls occur in the same order as operations with itself and memory. A reservation may arise between reordering.

Register optimization can be disabled using the volatile keyword.

Are there any additional warranties or corner cases that imply C and especially gcc?

+5
source share
3 answers

If your code depends on not reordering operations and not some other optimization, this is just “undefined behavior”.

If you are looking for such guarantees, you are simply looking for a guarantee that you will write a broken program that runs correctly. You should use the semantics that are represented by your language used. If you need assumptions about how the language implements actions on given os and machines, you are completely mistaken!

If you really have any guarantee for the real version of the compiler and the used pad, this will not be guaranteed in this function! Thus, the wording “guarantee” is also not true. If this is a simple answer: there is no guarantee! The language has semantics and guarantees the compiler for its implementation. Not more!

+1
source

The compiler should only not change the meaning of the program.

The value of the program is determined by the semantics of its C operators, for example, a volatile counter is a way to refinance, on a semantic level, interaction with external agents.
volatile alone, however, is useless when it comes to thread synchronization, it only has local effects.

Thus, you should understand only what the C standard implies, if the standard does not specify ordering semantics for the operator or any side effect, then it is not.

To optimize the code, the compiler must prove that optimization does not change the meaning.
This is usually a complex (or even insoluble) problem, so it is only performed in a simple context.

Consider

 #include <stdio.h> int simple(const int a, const int b) { int c = a + b; //3x Memory operation? int d = c*c; //2x Memory operation? return d+d; //Memory operation? } int main() { int a = 0; //Memory operation? int b = 0; //Memory operation? a = simple(2, 3); //Function call + Memory operation? b = simple(3, 4); //Function call + Memory operation? printf("%d %d\n", a, b); //Function call + 3x Memory operation? return 0; } 

The C standard states that a = simple(2, 3); holds until b = simple(3, 4); because the end of the expression is a point in the sequence.

This is gcc code with full optimization.

 lea 0x18f0(%rip),%rcx # 0x100403030, "%d %d\n" mov $0x62,%r8d mov $0x32,%edx callq 0x100401110 <printf> 

I used cygwin, so ABI is Windows. It is equivalent

 printf("%d %d\n", 50, 98); 

This is an ad hoc example, the simple function is clean and accepts compile-time constant expressions , so the result is known at compile time.
This is proof that gcc is needed to optimize calls .


When writing code without locking, you don’t have to worry about compiler optimization at all if you use the correct semantics (e.g. volatile for read and write access as side effects only for optimization ).

What you really need to worry about is memory order , as stated in my comment.
C11 finally confirms all this in its memory model.

+1
source

Order guarantees between threads in C can only be achieved with stdatomic.h from C11 or compiler-specific extensions. In all other cases, the only thing that the compiler should guarantee is that the externally visible behavior of the program (this could be roughly translated into: function calls and memory references are not under the control of compilers) is similar to interpretation in accordance with the standard. Before the C11 threads did not exist in terms of the C standard, therefore, it did not deal with threaded behavior.

+1
source

All Articles