Status of CONFIG_FORCED_INLINING?
I notice that feature-removal-schedule.txt has CONFIG_FORCED_INLINING scheduled to go away most of a year ago. My question is what replaces it:
Does #define inline __always_inline become the new standard and uses of __always_inline be removed, or should all instances of "inline" either be removed or replaced with __always_inline? (Or are there going to be two keywords meaning exactly the same thing going forward?)
Rob
Quoted 3 lines · Rob Landley
I notice that feature-removal-schedule.txt has
CONFIG_FORCED_INLININGscheduled to go away most of a year ago. My question is what replaces it:Does #define inline
__always_inlinebecome the new standard and uses of__always_inlinebe removed, or should all instances of "inline" either be removed or replaced with__always_inline? (Or are there going to be two keywords meaning exactly the same thing going forward?)
it should be that we do not force gcc to inline on the "normal" inline keyword, and we mark the cases that HAVE to be inlined for correctness reasons as __always_inline.
Quoted 11 lines · Arjan van de Ven
Quoted 6 lines · Rob Landley
I notice that feature-removal-schedule.txt has
CONFIG_FORCED_INLININGscheduled to go away most of a year ago. My question is what replaces it:
Does #define inline__always_inlinebecome the new standard and uses of__always_inlinebe removed, or should all instances of "inline" either be removed or replaced with__always_inline? (Or are there going to be two keywords meaning exactly the same thing going forward?)it should be that we do not force gcc to inline on the "normal" inline keyword, and we mark the cases that HAVE to be inlined for correctness reasons as
__always_inline.
What about performance reasons?
We habe "inline" code in header files that heavily relies on being nearly completely optimized away after being inlined. Especially with -Os it could even sound logical for a compiler to never inline a non-forced "inline"'d three line function with 2 callers.
And we need only two different inline levels (__always_inline and "let the compiler decide"), not three (__always_inline, inline and "let the compiler decide").
The rules are simple:
- every static function in a header file must be __always_inline
- no function in a C file should be marked as __always_inline/inline
- in extreme rare cases there might be exceptions from the latter
Your suggestion is possible, but please also send a patch that turns every "inline" in header files into __always_inline...
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 1 line
- every static function in a header file must be
__always_inline
Why? Why does it matter whether a function is defined in a .h file or a .c file? Can't the compiler decide better than we can whether something should be inlined or not?
Your argument seems to imply that we should never use the inline keyword at all.
- R.
Quoted 3 lines · Adrian Bunk
What about performance reasons?
We habe "inline" code in header files that heavily relies on being nearly completely optimized away after being inlined.
fair
Quoted 1 line
Especially with -Os it could even sound logical for a compiler to never inline a non-forced "inline"'d three line function with 2 callers.
but you said "I Care about size more than performance". Your argument is thus absolutely incorrect.
Quoted 2 lines
The rules are simple:
- every static function in a header file must be__always_inline
wrong.
Quoted 2 lines
Your suggestion is possible, but please also send a patch that turns every "inline" in header files into
__always_inline...
this is 1) insane and 2) if inlines in headers are so big gcc decides to not inline them.. they're too big and don't belong in the header.
Quoted 8 lines · Roland Dreier
Quoted 1 line
- every static function in a header file must be
__always_inlineWhy? Why does it matter whether a function is defined in a .h file or a .c file? Can't the compiler decide better than we can whether something should be inlined or not?
Your argument seems to imply that we should never use the inline keyword at all.
i hate to be in the middle of one of these again, but i think i initiated this topic way back when when i (like rob landley) asked why that config option was still around when it's been listed for deletion for a year.
regardless of its good or bad points, one way or the other, something should be updated.
rday
Signature · 7 lines
======================================================================== Robert P. J. Day Linux Consulting, Training and Annoying Kernel Pedantry Waterloo, Ontario, CANADA http://fsdev.net/wiki/index.php?title=Main_Page ========================================================================
Quoted 4 lines · Adrian Bunk
And we need only two different inline levels (
__always_inlineand "let the compiler decide"), not three (__always_inline, inline and "let the compiler decide").
"inline" is "let the compiler decide". If it is not, then it is "let the compiler decide, based on my bias that I think it should be inlined".
Jan
Quoted 10 lines · Robert P. J. Day
Quoted 8 lines · Roland Dreier
Quoted 1 line
- every static function in a header file must be
__always_inlineWhy? Why does it matter whether a function is defined in a .h file or a .c file? Can't the compiler decide better than we can whether something should be inlined or not?
Your argument seems to imply that we should never use the inline keyword at all.
Do we ever use the "register" keyword anymore? I don't make "suggestions" to gcc, I hit it with a clue-by-by four.
Quoted 4 lines
i hate to be in the middle of one of these again, but i think i initiated this topic way back when when i (like rob landley) asked why that config option was still around when it's been listed for deletion for a year.
I'm actually trying to write documentation on it. Temporary copy at:
http://landley.net/kdocs/inline.html
Quoted 2 lines
regardless of its good or bad points, one way or the other, something should be updated.
I'd be happy to just figure out what the policy is. It seems like the "inline" keyword should no longer be used, and either say __always_inline or leave it to the compiler. If there's a good counter-argument, I'd love to hear it.
Rob
Quoted 8 lines · Roland Dreier
Quoted 1 line
- every static function in a header file must be
__always_inlineWhy? Why does it matter whether a function is defined in a .h file or a .c file? Can't the compiler decide better than we can whether something should be inlined or not?
Your argument seems to imply that we should never use the inline keyword at all.
A function only belongs into a header file if we always want it inlined, otherwise it belongs into a C file.
Quoted 1 line
- R.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 5 lines · Arjan van de Ven
Quoted 3 lines · Adrian Bunk
What about performance reasons?
We habe "inline" code in header files that heavily relies on being nearly completely optimized away after being inlined.
Quoted 7 lines
fair
Quoted 2 lines
Especially with -Os it could even sound logical for a compiler to never inline a non-forced "inline"'d three line function with 2 callers.
but you said "I Care about size more than performance". Your argument is thus absolutely incorrect.
Theoretically, you are right.
Practically, this would imply removing the CONFIG_CC_OPTIMIZE_FOR_SIZE option several distributions currently enable by default since it has been shown that it often generates faster code...
Quoted 10 lines
Quoted 2 lines
The rules are simple:
- every static function in a header file must be__always_inlinewrong.
Quoted 2 lines
Your suggestion is possible, but please also send a patch that turns every "inline" in header files into
__always_inline...this is 1) insane and 2) if inlines in headers are so big gcc decides to not inline them.. they're too big and don't belong in the header.
Exactly.
So there's no point in having a non-forced inline.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 9 lines · Jan Engelhardt
Quoted 4 lines · Adrian Bunk
And we need only two different inline levels (
__always_inlineand "let the compiler decide"), not three (__always_inline, inline and "let the compiler decide")."inline" is "let the compiler decide". If it is not, then it is "let the compiler decide, based on my bias that I think it should be inlined".
Wrong.
"static" is "let the compiler decide".
Quoted 1 line
Jan
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 2 lines
A function only belongs into a header file if we always want it inlined, otherwise it belongs into a C file.
Again, why? Why don't we trust the compiler to decide if a function should be inlined or not, even if the definition happens to be in a .h file?
It seems like a perfectly valid optimization for the compiler to only emit code once for a function and then call it where it is used, even if that function happens to be defined in a .h file.
- R.
Quoted 11 lines · Adrian Bunk
Quoted 6 lines · Jan Engelhardt
Quoted 3 lines · Adrian Bunk
And we need only two different inline levels (
__always_inlineand "let the compiler decide"), not three (__always_inline, inline and "let the compiler decide")."inline" is "let the compiler decide". If it is not, then it is "let the compiler decide, based on my bias that I think it should be inlined".
Wrong.
"static" is "let the compiler decide".
and "inline" is "give the compiler a suggestion". Not more than that.
Quoted 8 lines · Jan Engelhardt
Quoted 4 lines · Adrian Bunk
And we need only two different inline levels (
__always_inlineand "let the compiler decide"), not three (__always_inline, inline and "let the compiler decide")."inline" is "let the compiler decide".
The compiler decides anyway. That's why we need a noinline to tell it _not_ to spontaneously inline things it shouldn't.
Quoted 2 lines
If it is not, then it is "let the
compiler decide, based on my bias that I think it should be inlined".
Things like unlikely() actually produce different code (and are, technically speaking, workarounds for the high branch misprediction penalty in modern processor designs).
Things like "register" are a hint to the compiler that it's free to ignore. How often do we use the "register" keyword in Linux? Register allocation is totally different on different processors, and deprived of context the hint is almomst meaningless. If it doesn't guarantee anything and has far less of a performance impact than the difference between -O2 and -Os (or between gcc 3.4 and 4.1), then it's probably an unnecessary complication.
Inlining is even worse because whether or not it's a performance win depends on the L1 cache size, cache line size and alignment, L2 cache, DRAM fetch latency, and so on. This varies all over the map within a given processor line, let alone between processors.
If we actually need something inlined, we can tell it to do so reliably with __always_inline (and change __always_inline to be MORE explicit each time gcc breaks the previous way of saying "yes really inline it dammit"). If we can live with it not being inlined, why not leave it to the compiler whether or not to inline it?
Rob
Quoted 1 line · Rob Landley
If the compiler can emit a warning "inline insanely large", we can use that to fix it. But a warning is not the same as silently doing something other than what we told it to do.
It's not silent! that's what "inline" without the force is for! Once you force it you shut the warning up!!!!
Quoted 6 lines · Roland Dreier
Quoted 2 lines
A function only belongs into a header file if we always want it inlined, otherwise it belongs into a C file.
Again, why? Why don't we trust the compiler to decide if a function should be inlined or not, even if the definition happens to be in a .h file?
Because the purpose of .h files is to be included in more than one .c file. (Otherwise it should be a .c file.)
And if you #include a non-inlined definition in two .c files, the compiler will emit two copies into two separate .o files. What you're hoping is that the linker will notice they're identical and merge them, and last I checked I couldn't even reliably get it to do that with constant strings.
Quoted 3 lines
It seems like a perfectly valid optimization for the compiler to only emit code once for a function and then call it where it is used, even if that function happens to be defined in a .h file.
If we put it in a header, it's because we want it inlined. If we don't want it inlined it SHOULDN'T BE IN THE HEADER.
If the compiler can emit a warning "inline insanely large", we can use that to fix it. But a warning is not the same as silently doing something other than what we told it to do.
Quoted 1 line
- R.
Rob
Quoted 4 lines
And if you #include a non-inlined definition in two .c files, the compiler will emit two copies into two separate .o files. What you're hoping is that the linker will notice they're identical and merge them, and last I checked I couldn't even reliably get it to do that with constant strings.
No, I don't care if the linker merges it or not. In fact I hope that maybe the compiler is smart enough to optimize the function for the sites it's called from in a particular .c file.
But a function defined in a .h file had better be static, so it shouldn't matter if there are two copies of it in the final linked image (any more than it matters if there are 100 inlined copies of it).
Quoted 10 lines · Roland Dreier
Quoted 2 lines
A function only belongs into a header file if we always want it inlined, otherwise it belongs into a C file.
Again, why? Why don't we trust the compiler to decide if a function should be inlined or not, even if the definition happens to be in a .h file?
It seems like a perfectly valid optimization for the compiler to only emit code once for a function and then call it where it is used, even if that function happens to be defined in a .h file.
The compiler will always inline it when it's called once from a C file, and it might not inline it there when it's called more than once from another C file. So in the end, we have it not only out-of-line but also inlined in several places.
Functions in header files should either be extremely short so that inlining them makes sense, or always optimize to something extremely short after being inlined.
If it's an optimization to emit the code only once, then it's a bug that it's in a header file.
Quoted 1 line
- R.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 5 lines · Roland Dreier
Quoted 4 lines
And if you #include a non-inlined definition in two .c files, the compiler will emit two copies into two separate .o files. What you're hoping is that the linker will notice they're identical and merge them, and last I checked I couldn't even reliably get it to do that with constant strings.
Quoted 7 lines
No, I don't care if the linker merges it or not. In fact I hope that maybe the compiler is smart enough to optimize the function for the sites it's called from in a particular .c file.
But a function defined in a .h file had better be static, so it shouldn't matter if there are two copies of it in the final linked image (any more than it matters if there are 100 inlined copies of it).
The problem is that inline functions in headers are intended to be called from different C files.
gcc might not inline it in the C files where it is called more than once.
But it will always inline it if it's called only once.
One of both will be suboptimal, but from gcc's perspective it was optimal.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 2 lines · Arjan van de Ven
Quoted 1 line · Rob Landley
If the compiler can emit a warning "inline insanely large", we can use
that to
Quoted 1 line
Quoted 1 line
fix it. But a warning is not the same as silently doing something other
than
Quoted 4 lines
Quoted 1 line
what we told it to do.
It's not silent! that's what "inline" without the force is for! Once you force it you shut the warning up!!!!
So presumably, a debug option could #define __always_inline to just "inline" in order to get warning messages to look at?
Rob
Quoted 10 lines
The problem is that inline functions in headers are intended to be called from different C files.
gcc might not inline it in the C files where it is called more than once.
But it will always inline it if it's called only once.
One of both will be suboptimal, but from gcc's perspective it was optimal.
Yes, we could probably get huge benefits from --combine and/or -fwhole-program to let gcc see more than one file at a time.
But I still don't see the issue with having gcc do the best it can on each file it compiles. If you force the inlining, then that means that on files where not inlining was better, you've forced gcc to generate worse code. (I don't see how not inlining could be locally better on a single file but globally worse, even though it generated better code on each compiled file)
Quoted 14 lines · Roland Dreier
Quoted 10 lines
The problem is that inline functions in headers are intended to be called from different C files.
gcc might not inline it in the C files where it is called more than once.
But it will always inline it if it's called only once.
One of both will be suboptimal, but from gcc's perspective it was optimal.
Yes, we could probably get huge benefits from --combine and/or -fwhole-program to let gcc see more than one file at a time.
Quoted 6 lines
But I still don't see the issue with having gcc do the best it can on each file it compiles. If you force the inlining, then that means that on files where not inlining was better, you've forced gcc to generate worse code. (I don't see how not inlining could be locally better on a single file but globally worse, even though it generated better code on each compiled file)
Can you give examples where for one function it differs between different C files whether it should be inlined or not?
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon Seed