Page 1 of 1
GCC Optimzations
Posted: Fri Mar 10, 2006 7:38 am
by spix
Hi There
I'm in the process of debugging some of my code, which I discovered works sometimes it is compiled and other times it doesn't. Anyway, I got side tracked and experimented with gcc optimizations on my kernel. Using GCC 4.1, -O worked fine, -O2 and -O3 the keyboard stopped working. So I am wondering, when optimizations break my code does that mean that there is some bug I haven't discovered yet in my code, or it is optimizing something it shouldn't and I shouldn't play with optimizations in the first place?
Andrew
Re:GCC Optimzations
Posted: Fri Mar 10, 2006 7:47 am
by Solar
Generally, GCC is very well-tested. Actually, it is better tested with optimizations than without.
Chances you did soemthing fishy in your code: 99.5%
Chances GCC optimization broke: 0.5%
My first guess would be you forgot "volatile" in important places, or did some strange polling things that GCC optimized away.
Re:GCC Optimzations
Posted: Fri Mar 10, 2006 7:54 am
by spix
Thanks for that, you're probably right with the volatile, I've missed those in the past.
I was wondering about the optimizations because sometimes I've read posts on other boards with regards to linux and how compiling with too much optimizations can be bad - I think it was a gentoo forum, and also there was a lot of code that needed to be fixed to work in gcc 4.0 when it came out.
I guess I was wondering mostly because, I can use this as a debugging tool, if it breaks with some optimization but usually works, there must be a hidden bug.
Anyway, thanks for that
Andrew
Re:GCC Optimzations
Posted: Fri Mar 10, 2006 8:42 am
by Solar
spix wrote:
I was wondering about the optimizations because sometimes I've read posts on other boards with regards to linux and how compiling with too much optimizations can be bad - I think it was a gentoo forum, and also there was a lot of code that needed to be fixed to work in gcc 4.0 when it came out.
Correct. That is because most of the software available for Linux is pretty sh*tty. Gee, mommy, look at all those compiler warnings... needless to say, if your code is badly written, running an aggressive optimizer on it won't improve things.
But that's not GCC's fault...
I guess I was wondering mostly because, I can use this as a debugging tool, if it breaks with some optimization but usually works, there must be a hidden bug.
Start with enabling as many compiler warnings as you can find, and also do -Werror so you don't get lazy with warnings. Then make sure you
understood the language details (like 'volatile'). The optimizer is about the last tool I would trust to find my bugs for me.