Problem with GCC reordering with -O0

Programming, for all ages and all languages.
Post Reply
User avatar
inx
Member
Member
Posts: 142
Joined: Wed Mar 05, 2008 12:52 am

Problem with GCC reordering with -O0

Post by inx »

Apologies for the stupid question, but I can't seem to find a solution to this... I've got a cross compiler to i386-elf-pc built, and I'm compiling with -O0. This configuration has never given me any trouble with my kernel at all, however.. When building my userland test cases, it's rearranging things terribly. i.e. I have a loop that waits for an incoming connection, saves the descriptor, then listens for a message, and is supposed to send a reply when said message arrives. However, it is getting the incoming connection, saying it's waiting for the message, receiving the message (which clears the queue), THEN saying, erroneously, that it's waiting for the incoming connection, and then finally moving to the "waiting for message loop", and not ever sending the reply because the queue has already cleared by the premature message checking..
I'm not really sure what's going on with this.. Isn't it supposed to not reorder/refactor loops unless optimization is turned on? And why would it execute my receive message system call outside of the loop, and then still execute the loop later?
*sigh* Sorry for the rambling nature of this message. It's late, and I'm really not understanding this..
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Problem with GCC reordering with -O0

Post by pcmattman »

You might want to disassemble the output and verify that it is in fact coming out right. If it is, then you may have a bug elsewhere.
User avatar
inx
Member
Member
Posts: 142
Joined: Wed Mar 05, 2008 12:52 am

Re: Problem with GCC reordering with -O0

Post by inx »

Strange. I disassembled it, and with a cursory glance, it seems to be in the right order, but it does not behave so. I'm double checking exactly what's happening with my loops now..
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Problem with GCC reordering with -O0

Post by Combuster »

Well, then its time to stop blaming the compiler (It's always right!), and start blaming your code.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
inx
Member
Member
Posts: 142
Joined: Wed Mar 05, 2008 12:52 am

Re: Problem with GCC reordering with -O0

Post by inx »

Understood. I wasn't meaning to blame the compiler, I was more wondering if there was a common mistake I was making and missing in my searches.. The odd thing is that much of the code is shared between the client and server tests, and the client side works properly..
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Problem with GCC reordering with -O0

Post by yemista »

Combuster wrote:Well, then its time to stop blaming the compiler (It's always right!), and start blaming your code.
The compiler may always be right, but that doesnt mean its doing what you want it to do, just what you told it to do
dude101
Member
Member
Posts: 56
Joined: Thu Apr 09, 2009 10:26 pm

Re: Problem with GCC reordering with -O0

Post by dude101 »

Make sure you use volatile correctly. I've seen errors where people don't use volatile correctly and then blame the compiler when it changes the orders of loads and stores to optimize.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: Problem with GCC reordering with -O0

Post by earlz »

dude101 wrote:Make sure you use volatile correctly. I've seen errors where people don't use volatile correctly and then blame the compiler when it changes the orders of loads and stores to optimize.
yea, volatile and firends get interesting with pointers..
iirc volatile char * and char volatile* are completely different things
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: Problem with GCC reordering with -O0

Post by skyking »

The usual way I go when I think the compiler produces code that I didn't expect is to break down the steps.

1) Do the preprocessor produce the expected result?
2) Do the compiler produce the expected result (check the assembler code)?
3) Does the program run the generated code?

If you've disassembled the code and it's as you expect then you've probably checked all these, but did you disassemble the object file, executable or even the code after loading it?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Problem with GCC reordering with -O0

Post by Solar »

earlz wrote:iirc volatile char * and char volatile* are completely different things
IIRC, no.

My advice: Always use qualifiers (const, volatile) postfix. That way you're always correct. (Doesn't work the other way around.)
Every good solution is obvious once you've found it.
User avatar
inx
Member
Member
Posts: 142
Joined: Wed Mar 05, 2008 12:52 am

Re: Problem with GCC reordering with -O0

Post by inx »

Thank you all for the advice. :) I did end up getting the bug fixed, and my impromptu userspace lib is working as expected now.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Problem with GCC reordering with -O0

Post by qw »

earlz wrote:iirc volatile char * and char volatile* are completely different things
No they aren't, but volatile char * and char *volatile are. The difference is subtle, but the first is a pointer to volatile char, the latter is a volatile pointer to char.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Problem with GCC reordering with -O0

Post by Candy »

Combuster wrote:... stop blaming the compiler (It's always right!)
It's not always right. It's just about always right. Always keep in the back of your head that it may be wrong - just narrow down the test case sufficiently. I've reported & gotten fixed two definite compiler bugs and added one warning (which will be in GCC from 4.6 and on). Microsoft tends to admit that it's a bug but that old code relies on it so they won't fix it.

Long story short: Search for alternative explanations for at least a month unless you're on the fringes of a language. The bugs I found were (1) incorrect handling of overloads based on the volatile/constness of a member function pointer argument crashing the compiler and (2) GCC 4.3.0 which handled X<Y<void()>> without space incorrectly (and it was the first version that claimed to do that). If you're doing something simpler, don't bet on finding a bug.
Hobbes wrote:
earlz wrote:iirc volatile char * and char volatile* are completely different things
No they aren't, but volatile char * and char *volatile are. The difference is subtle, but the first is a pointer to volatile char, the latter is a volatile pointer to char.
The example we use for this in the C++ course we teach is (as a member function)

const(1) char const(2) * const(3) func(const(4) char const(5) * const(6)) const(7) {

}

OF these 7 consts, 5 locations are useful and do something. Two are dupes.

(1) and (2) are both making the char pointed to const. (3) makes the pointer itself (but not what it points to!) const. (4) and (5) do that for the argument, making the pointed-to const. (6) makes the argument itself const, which is usually pointless (as it's a copy anyway). (7) makes the function const (literally: it makes the implicit this pointer into a const pointer).

Most people recommend leaving out (1) and (4) and reading the type from right to left. So,

char const * volatile

would be a volatile pointer to a const char. Which is correct!
inx wrote:Thank you all for the advice. :) I did end up getting the bug fixed, and my impromptu userspace lib is working as expected now.
Most importantly, what was it?
nikito
Member
Member
Posts: 42
Joined: Thu Jul 15, 2010 7:16 pm

Re: Problem with GCC reordering with -O0

Post by nikito »

The more expected place the errors are is in the code.

But the compilers are not infallible. Even an CPU needs his microcode updates.

Patience, Inx. If you feel stuck with the problem, just give yourself a break. This often helps me.
Post Reply