Skip to contentSkip to conversation

optimization in kernel compile

DISCUSSION ishare 2 participants 6 messages Mar 22, 2013 – Mar 24, 2013

lore
Share & tools
lore

ishare

author
#0 lore raw

Is it needed or must to compile fs and driver with -O2 option when compiling kernel ?

thanks!

#1 lore raw
Quoted 1 line · ishare

Is it needed or must to compile fs and driver with -O2 option when compiling kernel ?

It's not strictly mandatory to use -O2 (for a while, -Os was the default). There are a few places that for correctness, you *cannot* use -O0. For instance, a few places where we use builtin_return_address() inside an inline (-O0 won't inline so builtin_return_address() ends up returning a pointer to a function when we want the function's parent).

Since gdb and friends are able to deal with -O2 compiled code just fine,
there's really no reason *not* to optimize the kernel.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130322/b33ab2fe/attachment.bin

author
#2 lore raw
Quoted 8 lines · Valdis.Kletnieks at vt.edu
Quoted 1 line · ishare

Is it needed or must to compile fs and driver with -O2 option when compiling kernel ?

It's not strictly mandatory to use -O2 (for a while, -Os was the default). There are a few places that for correctness, you *cannot* use -O0. For instance, a few places where we use builtin_return_address() inside an inline (-O0 won't inline so builtin_return_address() ends up returning a pointer to a function when we want the function's parent).

So it will cause an error ?

Quoted 3 lines

Since gdb and friends are able to deal with -O2 compiled code just fine, there's really no reason *not* to optimize the kernel.

the debug information will be stripped by -O2 ,for example ,you can not touch the value of some varibles at stack , and debugging will not run line by line, instead , the source jump in unexpectable order .

#3 lore raw
Quoted 1 line · ishare
Quoted 1 line

are a few places that for correctness, you *cannot* use -O0. For instance,

a

Quoted 5 lines
Quoted 3 lines

few places where we use builtin_return_address() inside an inline (-O0 won't inline so builtin_return_address() ends up returning a pointer to a function when we want the function's parent).

So it will cause an error ?

Yes, there are places where failing to optimize causes errors.

Consider this code:

static inline foo (return builtin_return_address());

int bar ( x = foo());

If you don't optimize, x ends up with a pointer into bar. If it gets inlined because you're optimizing, x ends up pointing to bar's caller. This breaks stuff like the function tracer.

Quoted 4 lines
Quoted 2 lines

Since gdb and friends are able to deal with -O2 compiled code just fine, there's really no reason *not* to optimize the kernel.

the debug information will be stripped by -O2 ,for example ,you can not touch

No debug information is stripped by -O2. Debug information isn't emitted if you don't compile with -g. At one time, long ago (quite possibly literally "before you were born" for some of the younger readers on the list), gcc was unable to generate -g output if the optimizer was invoked. But that was last century (gcc 2.95 era).

Quoted 2 lines

the value of some varibles at stack , and debugging will not run line by line, instead , the source jump in unexpectable order .

I'm probably going to piss a bunch of people off by saying this, but:

If your C skills aren't up to debugging code that's been compiled with
-O2, maybe you shouldn't be poking around inside the kernel.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130322/ac48344c/attachment-0001.bin

#4 lore raw
Quoted 5 lines · Valdis.Kletnieks at vt.edu

No debug information is stripped by -O2. Debug information isn't emitted if you don't compile with -g. At one time, long ago (quite possibly literally "before you were born" for some of the younger readers on the list), gcc was unable to generate -g output if the optimizer was invoked. But that was last century (gcc 2.95 era).

GCC 4.8 was officially released today (since I sent the previous note).

Quoted 1 line

From the release notes:

"A new general optimization level, -Og, has been introduced. It addresses the need for fast compilation and a superior debugging experience while providing a reasonable level of runtime performance. Overall experience for development should be better than the default optimization level -O0."

The current Linus tree does build with 4.8. I do *not* know if earlier
releases build correctly (or how far back), nor if -Og is sufficient
optimization to allow correct kernel functioning. But it's something to
look at.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130322/4fbf18c9/attachment.bin

author
#5 lore raw

If I use -O2, then during debuging , a print command will get : value is optimized out !

Is there mothod to get rid of this ?

thanks.