Does __attribute__((always_inline))
force a function to be inlined by gcc?
Yes.
- From documentation v4.1.2
- From documentation latest
always_inline
Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level was specified.
It should. I'm a big fan of manual inlining. Sure, used in excess it's a bad thing. But often times when optimizing code, there will be one or two functions that simply have to be inlined or performance goes down the toilet. And frankly, in my experience C compilers typically do not inline those functions when using the inline keyword.
I'm perfectly willing to let the compiler inline most of my code for me. It's only those half dozen or so absolutely vital cases that I really care about. People say "compilers do a good job at this." I'd like to see proof of that, please. So far, I've never seen a C compiler inline a vital piece of code I told it to without using some sort of forced inline syntax (__forceinline
on msvc __attribute__((always_inline))
on gcc).
inline
explicitly: __attribute__((always_inline)) inline YourFunc(...
else you will get warning: always_inline function might not be inlinable [-Wattributes]
–
Wald Yes, it will. That doesn't necessarily mean it's a good idea.
According to the gcc optimize options documentation, you can tune inlining with parameters:
-finline-limit=n
By default, GCC limits the size of functions that can be inlined. This flag
allows coarse control of this limit. n is the size of functions that can be
inlined in number of pseudo instructions.
Inlining is actually controlled by a number of parameters, which may be specified
individually by using --param name=value. The -finline-limit=n option sets some
of these parameters as follows:
max-inline-insns-single is set to n/2.
max-inline-insns-auto is set to n/2.
I suggest reading more in details about all the parameters for inlining, and setting them appropriately.
I want to add here that I have a SIMD math library where inlining is absolutely critical for performance. Initially I set all functions to inline but the disassembly showed that even for the most trivial operators it would decide to actually call the function. Both MSVC and Clang showed this, with all optimization flags on.
I did as suggested in other posts in SO and added __forceinline
for MSVC and __attribute__((always_inline))
for all other compilers. There was a consistent 25-35% improvement in performance in various tight loops with operations ranging from basic multiplies to sines.
I didn't figure out why they had such a hard time inlining (perhaps templated code is harder?) but the bottom line is: there are very valid use cases for inlining manually and huge speedups to be gained.
If you're curious this is where I implemented it. https://github.com/redorav/hlslpp
.text
section in order to make "vague linkage" possible (see gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Vague-Linkage.html). By forcing inlining, you basically lose the ability to explicitly specialize template functions in different compilation units. –
Boff Yes. It will inline the function regardless of any other options set. See here.
One can also use __always_inline
. I have been using that for C++ member functions for GCC 4.8.1. But could not found a good explanation in GCC doc.
Actually the answer is "no". All it means is that the function is a candidate for inlining even with optimizations disabled.
inline
keyword, not for the always_inline
attribute, see here –
Berglund © 2022 - 2024 — McMap. All rights reserved.
inline
and__attribute__((always_inline))
. I just tested this on Android NDK r10d with GCC. Perhaps not a standard environment but from what I've read this requirement is the same for all platforms. – Glottalint forexample ( const char * fmt, ... );
– Oestrin.Escape()
the handles or not, if the functions are not ensured to be inline? If the compiler fails to inline such functions, V8 will make new handle scopes! – Junko