Compiler optimisation levels

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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Compiler optimisation levels

Post by JamesM »

So my kernel compiles fine at -O0 - I thought I'd give it a go at other optimisation levels to see what happens.

Well, If I enable ANY optimisations at all my kernel page faults. I'm sure I could work out why/where given time, but what interested me was the readelf output for kernels compiled with different optimisation levels.

Including the .text,.data and .bss sections my kernel had size:

O0: 0x14000
O1: 0x13000
O2: 0x15000
O3: 0x16000

(My linker script page-aligns everything so this is rounded up quite a bit).

The interesting part was that I was expecting the size of the sections to go DOWN, not UP when optimisations are applied. Why is this not the case? What exactly is GCC optimising?

Also related: What optimisation level do your kernels get built with? Which is optimal?

A few thoughts... ;)

JamesM
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Post by Korona »

I guess GCC will add more code and data alignments to the binary if you use a higher optimization level. It might also do some method inlining. GCC will also try to use instructions that utilize the pipelines and caches as much as possible. If you want to get the smallest possible binary take a look at the -Os switch. I don't use C in my kernel, but do not see why you should not use -O3 in the kernel. If your code does not work right when compiled with -O there are most likely some volatile keywords missing.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

O3 would give you loop unrolling, branch alignment, function alignment and other mechanism for gaining speed. In general more speed means larger code, mainly due to alignment and loop unroll. use gcc -v --help for all optimisation options or use man gcc if you want a good read.
Author of COBOS
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

I build mine with -O3 mainly because I use a few template classes to make code clearer, simpler and better maintainable which on -O3 get compiled to about 4 instructions. -O0 would give at least a number of function calls, which is always bigger (in this particular case).
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

I compile mine -Os. Anything higher than -O1 actually causes my kernel to lock up on boot. I never quite figured that out but of well it didn't make it any faster anyways. -Os saves me 52kb so I consider it worth it.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

frank wrote:I compile mine -Os. Anything higher than -O1 actually causes my kernel to lock up on boot. I never quite figured that out but of well it didn't make it any faster anyways. -Os saves me 52kb so I consider it worth it.
You can safely asume that you OS has a big BUG somewhere because the lockup should not happen. My kernel compiles fine under any optimisation. The -Os is specified solely for code size thus it is logical that this is the smallest kernel. With respect to the performance it propblably will be noticable with a lot of threads and use lang switches. Though the smallest code might actually have better cache usage. I never investigated this.
Author of COBOS
uglyoldbob
Member
Member
Posts: 63
Joined: Tue Feb 13, 2007 10:46 am

Post by uglyoldbob »

That's good to know, especially for bug testing. Mine doesn't have any right now (that I know of). However using Os versus O3 only saves 12 bytes for my kernel.
User avatar
Solar
Member
Member
Posts: 7637
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Compiler optimisation levels

Post by Solar »

JamesM wrote:The interesting part was that I was expecting the size of the sections to go DOWN, not UP when optimisations are applied. Why is this not the case? What exactly is GCC optimising?

Also related: What optimisation level do your kernels get built with? Which is optimal?
Hmmmm... if in doubt, why not look it up in the manual?
Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results.

[...]

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program.

[...]

Optimization levels -O and above, in particular, enable unit-at-a-time mode, which allows the compiler to consider information gained from later functions in the file when compiling a function. Compiling multiple files at once to a single output file in unit-at-a-time mode allows the compiler to use information gained from all of the files when compiling each of them.

[...]

-O
-O1
Optimize.

[...]

-O2
Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify -O2.

[...]

-O3
Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops and -fgcse-after-reload options.

[...]

-O0
Do not optimize. This is the default.

-Os
Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.
The same documentation page also lists explicitly which optimizations are enabled at which level.

Personally, and lacking a self-made kernel to speak of, I use -Os to compile my Linux. ;)
Every good solution is obvious once you've found it.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Compiler optimisation levels

Post by 01000101 »

I'm not sure how normal this is, but:

at -O0 on all files, my final binary is 28k
at -O1 on all files, my final binary is 24k
at -O2 on all files, my final binary is 24k
at -O3 on all files, my final binary is 28k
at -Os on all files, my final binary is 20k

shouldn't -O0 optimization produce a smaller size than -O1+?

All optimizations are stable with my OS.

The actual compilation line is: gcc -O3 -mtune=pentium4 -ffreestanding -c "../Net/p2p.c" -o np.o
-ffreestanding should be all i need as it sets __STDC_HOSTED bit to 0 to tell the compiler not to include any external functions, correct?
User avatar
neon
Member
Member
Posts: 1568
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Compiler optimisation levels

Post by neon »

Optimization has nothing to do with your program size. With a higher optimization level, the compiler is more free to provide different types of optimization tricks on your code (loop unrolling, table lookups+creations, etc..) that will provide faster code at the cost of increased program size.

*edit: Oh right, your other question ;) My System Core "kernel" is set at /O1 which is minimum size, currently at 8K in size.

Interestingly it only seems to work on /O1 (minimum size) and /O2 (Maximum Speed). Setting it to /Od (disabled) or /Ox (full optimization) triple faults. Im going to have to look into that.... :)

*edit2: I do see whats going on though... It seems that my kernels entry point routine does not get properly called, instead another of my routines gets called. When the routine issues its RET, we go back to address 0...I think you know the rest of the story from here...

*edit3: Yey! Found and fixed the problem! :D Seems there was (yet another) problem with my bootloaders MZ kernel parsing code (ugh). I also made it so that I do not need to use MSVC++'s function order (Order.txt) method to reorder the functions either, which is nice! :) ie, it calls the entry point directly no matter where its located at.

Now it works with all optimization levels as well :D
Last edited by neon on Fri Jun 27, 2008 9:32 pm, edited 3 times in total.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: Compiler optimisation levels

Post by piranha »

-O1 works fine.

-O2:
GRUB Error 13: Invalid or unsupported executable file format.
Anyone know why this might be?

-O3: Pagefault.

-Os: Invalid Opcode.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Compiler optimisation levels

Post by 01000101 »

I think I might be going with the following as my optimized binary.

gcc -O3 -march=pentium4 -ffast-math -funroll-loops -ffreestanding

that will be the compilation for each file.
with all of those, it brings my binary to a whopping 32k. 8)

No issues with it at all, its been running good and I have been testing all of the functions and keeping it in debug mode for now just to make sure nothing is wrong. I do notice a speed increase, but it could just be a placebo effect.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Compiler optimisation levels

Post by 01000101 »

for anyone that is interested, I compared various optimizations to my kernel and recorded the results.

the format is in x,y (x = time for my video setup code), (y = time for my PCI loop and RTL8139 setup code).
I used the timer (func. 3) and used the countdown from 0xFFFF to find the results. I recorded the start and finish times and negated to get the ticks elapsed.

Code: Select all

-ffreestanding
produced 178, 1363

-Os -ffreestanding
produced 127, 1346 

-O1 -ffreestanding
produced 175, 1345

-O2 -ffreestanding
produced 127, 1343

-O3 -ffreestanding
produced 127, 1331

-O3 -march=pentium4 -ffreestanding
produced 127, 1323

-O3 -march=pentium4 -funroll-loops -ffreestanding
produced 127, 1323 

-O3 -march=pentium4 -ffast-math -ffreestanding
produced 127, 1322 

-O3 -march=pentium4 -ffast-math -funroll-loops -ffreestanding
produced 127, 1322
As you can see, the -march=pentium4 makes a bit of difference, as does the -O3 and -ffast-math. The jump from -O0/1 to -Os/2/3 is huge in relative terms. Well, that was my experience, and it deffinately gave me some reassurance as to the actual aid from optimisations.

I will probably throw in the '-fprefetch-loop-arrays' optimization in my OS as it is polling based and uses large arrays for packets and signatures.

btw, these were all tested twice and they all had a error margin of +/- 1.
Post Reply