9

This part of the gcc manual is pretty obscure and i can't understand the usage of the forceinline attribute after repeated attempts.

I'm defining an object and certain functions to manipulate that object. Few of those functions can use atomic instructions and i want the compiler to inline those functions. However i do not want to write those functions in the header file and declare them with "static inline" like in the linux kernel.

Is there a way to force gcc to inline functions from another translation unit ?

sgupta
  • 1,214
  • 2
  • 15
  • 29

1 Answers1

10

you can use the always_inline attribute, for example:

void foo () __attribute__((always_inline));

From the docs

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.

Note1: There's no need to use inline if you use the always_inline attribute

Note2: If the function could not be inlined you will get a warning, if for example the definition is not available when compiling, however, at a higher optimization gcc can still inline it into the caller, there's a specific switch for that too:

-funit-at-a-time

From the docs:

Optimization levels -O2 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.

Note3: It is not necessary to have an explicit prototype so you can use the attribute on the function defintion:

__attribute__((always_inline)) void foo() {
   //some code
}

Also see this discussion, it answers some of your questions.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • does it work even when the definition of the function is not available when a certain unit is being compiled ? how does the compiler handle such situation.. – sgupta Nov 05 '12 at 08:45
  • 1
    @user1075375 how can the definition of a function not be available ? it must be there otherwise it won't compile/link – iabdalkader Nov 05 '12 at 08:46
  • 1
    "If a function implementation is not in the header file and in a separate compilation unit, it cannot be inlined unless you have a compiler that can do LTCG" from http://stackoverflow.com/questions/5187735/how-to-inline-string-h-function-on-linux?rq=1 – sgupta Nov 05 '12 at 08:46
  • the unit in which the function is defined could be compiled after the unit which calls the function. – sgupta Nov 05 '12 at 08:47
  • @user1075375 good point, but regardless of the order of compilation, the object files must be linked at the end, to produce the binary, at that point the function is inlined or embedded in the object file that uses it. – iabdalkader Nov 05 '12 at 08:55
  • thanks, now i've another doubt, i know i should have the attribute of the function in the function prototype, but should i also include the attributes in the function declaration explicitly ? – sgupta Nov 05 '12 at 09:14