Thread: Problem with gcc -O3 please help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Problem with gcc -O3 please help

    Hi everyone,
    I ran across a really strange problem with the -O3 optimization of gcc, and I would appreciate any help because I really have a hard time understanding the problem. I have boiled down my considerably longer code to a simple example that shows the behaviour. What it does is simply allocating an array, setting its 0-element to 0 and then setting it to one in a loop that is executed only once. The code
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main() {
    
      int i;
      double R4 = 0;
      int *activedirections;
      int nactive=0;
      int holder;
      activedirections = malloc((2*3+1) *sizeof(int));
      activedirections[0] = 0;
      for (i=1; i<=1; i++) {
          activedirections[nactive] = i;
          nactive++;
      }
      holder = activedirections[0];
      printf("activedirections[0] = %i\n",activedirections[0]);
      printf("activedirections[0] = %i\n",activedirections[0]);
      printf("holder              = %i\n",holder);
      free(activedirections);
    }
    when compiled with gcc and no options produces:
    Code:
    activedirections[0] = 1
    activedirections[0] = 1
    holder              = 1
    as expected. Compiling with -O3 however yields:
    Code:
    activedirections[0] = 0
    activedirections[0] = 1
    holder              = 0
    I have no idea how a value can change in between two identical printf's

    Any help would be great.

    Thanks
    Last edited by forceael; 03-01-2010 at 05:03 AM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Problem with gcc -O3 please help

    I also executed your program using gcc compiler only.

    I got same output for both gcc, gcc -O3.
    activedirections[0] = 1
    activedirections[0] = 1
    holder = 1

    -O is for optimizing the C code. Which version you are using and Which OS you are using can you explain that.

    I am using gcc in Linux OS.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Hey, thank you for trying it out

    my /proc/version reads

    Linux version 2.6.27.4-X86_64-SMP (root@xxxx) (gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)) #1 SMP Tue Oct 28 16:31:30 CET 2008

    and gcc -dumpversion outputs
    4.3.2


    Also the problem also accurs with -O2 but not with -O1

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Most likely "-O3" just unrolled the loop. But it seems to have stuffed it up.

    This seems to be a bug in gcc. What does the assembly look like? Which version of gcc?

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    -O3 is the highest level optimization , first of all we need to find how your code changed after
    optimization

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    What does the assembly look like?
    How do I find that out?

    I also found that the -Os option produces the strange results.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Problem with gcc -O3 please help

    I am using 4.1.2 Gcc version.

    Actually, for all the optimization level I got only the same results. you are using improvised GCC version. so that only you got some different output I think so.

    But I need to work with 4.3.2 or read about that.Then only I can reply.

    And If it is possible you can also try with 4.1.2 version and check the difference.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    I think this is the assembly:

    prog.s
    Code:
            .file   "prog.c"
            .section        .rodata.str1.1,"aMS",@progbits,1
    .LC0:
            .string "activedirections[0] = %i\n"
    .LC1:
            .string "holder              = %i\n"
            .text
    .globl main
            .type   main, @function
    main:
    .LFB2:
            pushq   %rbx
    .LCFI0:
            movl    $4, %edi
            call    malloc
            xorl    %esi, %esi
            movq    %rax, %rbx
            movl    $1, (%rax)
            movl    $.LC0, %edi
            xorl    %eax, %eax
            call    printf
            movl    (%rbx), %esi
            movl    $.LC0, %edi
            xorl    %eax, %eax
            call    printf
            xorl    %esi, %esi
            movl    $.LC1, %edi
            xorl    %eax, %eax
            call    printf
            movq    %rbx, %rdi
            call    free
            popq    %rbx
            ret
    .LFE2:
            .size   main, .-main
            .section        .eh_frame,"a",@progbits
    .Lframe1:
            .long   .LECIE1-.LSCIE1
    .LSCIE1:
            .long   0x0
            .byte   0x1
            .string "zR"
            .uleb128 0x1
            .sleb128 -8
            .byte   0x10
            .uleb128 0x1
            .byte   0x3
            .byte   0xc
            .uleb128 0x7
            .uleb128 0x8
            .byte   0x90
            .uleb128 0x1
            .align 8
    .LECIE1:
    .LSFDE1:
            .long   .LEFDE1-.LASFDE1
    .LASFDE1:
            .long   .LASFDE1-.Lframe1
            .long   .LFB2
            .long   .LFE2-.LFB2
            .uleb128 0x0
            .byte   0x4
            .long   .LCFI0-.LFB2
            .byte   0xe
            .uleb128 0x10
            .byte   0x83
            .uleb128 0x2
            .align 8
    .LEFDE1:
            .ident  "GCC: (Debian 4.3.2-1.1) 4.3.2"
            .section        .note.GNU-stack,"",@progbits

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    I tried with gcc 4.1.3 and 4.2.4 and both worked fine.
    Is there some place I should post it to make the right people aware of it?
    I guess this resolves my problem, i will switch to gcc-4.2 and hope nothing else will change...

    Thanks for all your help so far

  10. #10

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    I added it to the gcc bug library


    Bug 43219 - O2, O3 and Os produce wrong code

    see what happens...

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Hmm.

    Code:
    .LCFI0:
            movl    $4, %edi
            call    malloc
            xorl    %esi, %esi
            movq    %rax, %rbx
            movl    $1, (%rax)
            movl    $.LC0, %edi
            xorl    %eax, %eax
            call    printf
            movl    (%rbx), %esi
            movl    $.LC0, %edi
            xorl    %eax, %eax
            call    printf
            xorl    %esi, %esi
            movl    $.LC1, %edi
            xorl    %eax, %eax
            call    printf
            movq    %rbx, %rdi
            call    free
            popq    %rbx
            ret
    Apparently, the second argument is getting passed in %esi. It's obvious from the above that on the first call to printf(), %esi is not getting set to the correct value -- look at the blue line, which is loading the value "1" into the second argument during the second call. The compiler did not do this for the first call.

    Bona fide compiler bug, assuming the code you posted is the actual code. These are actually quite rare. Congrats on finding one.

    This bug is either in the data-flow analysis portion of the optimizer, or in the peephole optimizer inside the architecture-specific backend, it seems like.
    Last edited by brewbuck; 03-01-2010 at 02:26 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird problem in constant definition
    By psx-c in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 01:25 PM
  2. Replies: 4
    Last Post: 09-02-2007, 08:47 PM
  3. Compiles on gcc 3.3 but not on gcc 4.0.3
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2007, 12:24 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. gcc
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-22-2003, 03:46 PM