Can different optimization levels lead to functionally different code?

前端 未结 13 1852
[愿得一人]
[愿得一人] 2020-12-13 12:39

I am curious about the liberties that a compiler has when optimizing. Let\'s limit this question to GCC and C/C++ (any version, any flavour of standard):

Is it possi

相关标签:
13条回答
  • 2020-12-13 13:02

    The portion of the C++ standard that applies is §1.9 "Program execution". It reads, in part:

    conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. ...

    A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. ...

    The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions. ...

    So, yes, code may behave differently at different optimization levels, but (assuming that all levels produce a conforming compiler), but they cannot behave observably differently.

    EDIT: Allow me to correct my conclusion: Yes, code may behave differently at different optimization levels as long as each behavior is observably identical to one of the behaviors of the standard's abstract machine.

    0 讨论(0)
  • 2020-12-13 13:04

    Anything that is Undefined Behavior according to the standard can change its behavior depending on optimization level (or moon-phase, for that matter).

    0 讨论(0)
  • 2020-12-13 13:07

    Got some interesting example in my OS course today. We analized some software mutex that could be damaged on optimization because the compiler does not know about the parallel execution.

    The compiler can reorder statements that do not operate on dependent data. As I already statet in parallelized code this dependencie is hidden for the compiler so it could break. The example I gave would lead to some hard times in debugging as the threadsafety is broken and your code behaves unpredictable because of OS-scheduling issues and concurrent access errors.

    0 讨论(0)
  • 2020-12-13 13:09

    Since copy constructor calls can be optimized away, even if they have side effects, having copy constructors with side-effects will cause unoptimized and optimized code to behave differently.

    0 讨论(0)
  • 2020-12-13 13:11

    a.c:

    char *f1(void) { return "hello"; }
    

    b.c:

    #include <stdio.h>
    
    char *f1(void);
    
    int main()
    {
        if (f1() == "hello") printf("yes\n");
            else printf("no\n");
    }
    

    Output depends on whether merge string constants optimization is enabled or disabled:

    $ gcc a.c b.c -o a -fno-merge-constants; ./a
    no
    $ gcc a.c b.c -o a -fmerge-constants; ./a
    yes

    0 讨论(0)
  • 2020-12-13 13:12

    Is it possible to write code which behaves differently depending on which optimization level it was compiled with?

    Only if you trigger a compiler's bug.

    EDIT

    This example behaves differently on gcc 4.5.2:

    void foo(int i) {
      foo(i+1);
    }
    
    main() {
      foo(0);
    }
    

    Compiled with -O0 creates a program crashing with a segmentation fault.
    Compiled with -O2 creates a program entering an endless loop.

    0 讨论(0)
提交回复
热议问题