This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Optimization o2 vs o3 issue
- From: Manish Baphna <manish_baphna at yahoo dot com>
- To: Andrew Haley <aph at redhat dot com>
- Cc: gcc-help at gcc dot gnu dot org, John Fine <johnsfine at verizon dot net>
- Date: Mon, 22 Dec 2008 18:57:30 +0530 (IST)
- Subject: Re: Optimization o2 vs o3 issue
- Reply-to: manish_baphna at yahoo dot com
Hi
1.Its NOT multi threaded application.
2. I created a small example where this issue is visible. Let me know what you think after it.
///////////// bug.cpp ///////////
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std ;
typedef unsigned char uint8_t ;
class DBus {
public:
uint8_t data[16] ;
};
int main(){
DBus dout ;
long long unsigned upper_data = 0xFFFFFFFff ;
long long unsigned int mask = 0xFF ;
for(int count=0 ; count < 8 ; count++)
{
dout.data[15 - count] = (uint8_t)((upper_data & (mask << ((8+count)*8)))>>((8+count)*8));
//cout << "Dout data " << (uint64_t)((dout.data)[15-count]) << endl; // Line to be played with
}
for(int count=0 ; count < 8 ; count++)
{
cout << "Dout data " << (uint64_t)((dout.data)[15-count]) << endl;
}
return 1 ;
};
///////////// end of bug.cpp /////////
Step1 : Compile using o2/o3
bash>> g++-4.1.1 -g3 -O2 -fPIC -Wall -W -Wcast-qual -Wpointer-arith
bash>> g++-Woverloaded-virtual -Werror -m64 -Werror bug.cpp
bash>> ./a.out
Dout data 255
Dout data 255
Dout data 255
Dout data 255
Dout data 15
Dout data 0
Dout data 0
Dout data 0
bash>> g++-4.1.1 -g3 -O3 -fPIC -Wall -W -Wcast-qual -Wpointer-arith
bash>> g++-Woverloaded-virtual -Werror -m64 -Werror bug.cpp
bash>> ./a.out
Dout data 0
Dout data 0
Dout data 0
Dout data 0
Dout data 0
Dout data 0
Dout data 0
Dout data 0
Now if you uncomment the cout line , o3 would also work !!
Let me know what you guys think of it.
Best Regards,
Manish
--- On Mon, 22/12/08, Andrew Haley <aph@redhat.com> wrote:
> From: Andrew Haley <aph@redhat.com>
> Subject: Re: Optimization o2 vs o3 issue
> To: manish_baphna@yahoo.com
> Cc: gcc-help@gcc.gnu.org, "John Fine" <johnsfine@verizon.net>
> Date: Monday, 22 December, 2008, 4:50 PM
> Manish Baphna wrote:
>
> > A quick update, Though I couldn't really found
> real issue , I found
> > an interesting solution which worked. magic word is
> 'volatile' :)
> >
> >
> > Original two lines were
> >
> > dout.data[15 - count]
> > = (uint8_t)(((long long unsigned int)
> > (*(soc_cpr_dbusdataout_->sig_value_upper))
> > & (mask <<
> ((8+count)*8)))>>((8+count)*8));
> >
> > cout << "Dout data " <<
> (uint64_t)((dout.data)[15-count]) << endl;
> >
> > What I did was inserting this line just above Line#1
> > volatile uint32_t shift_val = ((8+count)*8) ;
> > and used this in above Line#1 , and then I removed
> Line#2 and this time it worked.
> > ( First I tried without using 'volatile' and
> it didn't help )
> > Another workaround that did the same thing was making
> > count as volatile.
>
> Interesting. Is this a multi-threaded application? The
> other
> possibility is an array bounds overflow.
>
> > These two lines are in for loop which goes for count =
> 0 to 7 , I
> > don't know why compiler is being lazy here to read
> it everytime
> > unless I make it volatile.
>
> The compiler isn't supposed to read it everytime unless
> you make it
> volatile.
>
> > Should it be logged as bug in gcc somewhere ?
>
> Not until you can come up with a test cases that someone
> else can run.
>
> Andrew.
Be the first one to try the new Messenger 9 Beta! Go to http://in.messenger.yahoo.com/win/