GCC Optimisations

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
srg

GCC Optimisations

Post by srg »

Hi

I was just wondering what optimisation level in GCC you compile your OSs with.

With what I have so far, I don't have any. Is it unsafe to use optinisation levels when still developing the kernel, driver etc etc in the first place.

srg
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GCC Optimisations

Post by Solar »

Optimizations should never break anything. Increasing optimization levels are not more risky, they usually just trade off more space for less execution time.

Higher optimization levels also tend to confuse debuggers, but unless you provide remote-gdb-stubs, that isn't an issue in kernel land.

Also note that not setting optimization levels explicitly simply tells gcc to use *default* optimizations.

Generally, -O2 is considered to be a "sane" setting. What that implies has changed significantly in recent GCC versions.
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:GCC Optimisations

Post by Candy »

srg wrote: Hi

I was just wondering what optimisation level in GCC you compile your OSs with.

With what I have so far, I don't have any. Is it unsafe to use optinisation levels when still developing the kernel, driver etc etc in the first place.

srg
Well, with my new configure system you just specify it yourself. Min is -O1 (so it at least inlines the inline funcs, otherwise it screws that up & produces multiple outputs) and max is -O3, although I usually run with -O2. O2 outputs the most readable code imo.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GCC Optimisations

Post by Pype.Clicker »

i personnally use -O3 or -O2 to enforce small functions inlining. The reason why people generally don't want to debug optimized code is because debuggers like GDB will be completely confused if some variable have been moved to registers, etc.

But since GDB will be of little use for debugging your kernel anyway, you could enable optimizations right from the start ;)
Neuromancer

Re:GCC Optimisations

Post by Neuromancer »

I use -O0 because my own-written (s)printf functions do not work with a higher level of optimization. My code is ok, since at level 0 works and compiles with other compilers.
GCC is 3.2.2
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GCC Optimisations

Post by Pype.Clicker »

Neur0m'ancer wrote: I use -O0 because my own-written (s)printf functions do not work with a higher level of optimization. My code is ok, since at level 0 works and compiles with other compilers.
GCC is 3.2.2
maybe needing volatiles here and there ?
proxy

Re:GCC Optimisations

Post by proxy »

I use -O0 because my own-written (s)printf functions do not work with a higher level of optimization. My code is ok, since at level 0 works and compiles with other compilers.
GCC is 3.2.2
while it may work, this is generally not a good thing to have in your code. I have seen code that breaks aliasing rules do things like that. (for example, taking an unsigned short and casting it to a char * so you can get the hi/low byte..clever, but technically the compiler is allowed to output garbage for that).

I would compile with as many warnings enabled as possible to find that. (-W -Wall for starters)

proxy
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GCC Optimisations

Post by Solar »

That's -Wextra for later versions of GCC; -W is deprecated if memory serves.

You might also consider adding -Werror, so that you do not fall into the trap of thinking "ah, it's just a warning".
Every good solution is obvious once you've found it.
Post Reply