How do I force gcc to inline a function?

71,368

Solution 1

Yes.

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.

Solution 2

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).

Solution 3

Yes, it will. That doesn't mean it's a good idea.

Solution 4

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.

Solution 5

Yes. It will inline the function regardless of any other options set. See here.

Share:
71,368
HaltingState
Author by

HaltingState

Updated on January 04, 2020

Comments

  • HaltingState
    HaltingState over 4 years

    Does __attribute__((always_inline)) force a function to be inlined by gcc?

  • Basile Starynkevitch
    Basile Starynkevitch almost 12 years
    Inlining a function won't always improve performance (e.g. cache issues).
  • Zak
    Zak over 9 years
    I appreciate the msvc vs. gcc comparison!
  • DCBillen
    DCBillen over 9 years
    Example of a very good reason I use it sometimes: When developing audio DSP applications sometimes the debug build can't process fast enough to keep up with the sample rate. By forcing things like accessor functions to inline I am able to test and debug.
  • underscore_d
    underscore_d over 8 years
    and yet the top answer has a direct quote that specifically says "this attribute inlines the function". I see no reference to any "candidate" there at all. What is your source?
  • johnnycrash
    johnnycrash over 8 years
    I agree but I force inlining way more than it. I use __forceinline on thousands of functions and have saved 20% of a 600 server farm. Assuming that the compiler will make the best decision WRT inlining is simply not true. The compiler is guessing. Educated guess or not, it is still a guess. The compiler doesn't know that you wrote the function to optimize out expressions formed with constant parameters. Etc etc etc.
  • No-Bugs Hare
    No-Bugs Hare almost 7 years
    Correction: it doesn't necessarily mean it's a good idea. Sometimes it is.
  • Bilow
    Bilow over 6 years
    This is true for the inline keyword, not for the always_inline attribute, see here
  • Géza Török
    Géza Török over 6 years
    template function code tends not to get inlined, but rather every specialization gets into a separate .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.
  • RedOrav
    RedOrav over 6 years
    I see, thanks for the explanation @GézaTörök that explains the behavior. Most math classes are quite hard to write without templating (not to mention the perf gains), so it seems like explicitly forcing inline is often a must.
  • slashmais
    slashmais almost 6 years
    with gcc you also need to specify inline explicitly: __attribute__((always_inline)) inline YourFunc(... else you will get warning: always_inline function might not be inlinable [-Wattributes]