GCC -flto and inline keyword

2.1k Views Asked by At

My question is simple, does the keyword inline make a difference to the view of the link time optimization? With link time optimization I mean a GCC version which supports -flto(Link time optimization).

For example:

main.c

#include "b.h"

int main() {
    print_x(2);
    return 0;
}

b.h

extern void print_x(int x);

b.c

#include "b.h"
#include "stdio.h"

inline void print_x(int x) {
    printf("%d\n", x);
}

Will the inline keyword in b.c make a difference when the linker does LTO(Link time optimization)?

1

There are 1 best solutions below

0
On BEST ANSWER

A compiler can, in principle, use the presence of the inline keyword to alter its heuristics. However, how much the presence of the inline specifier alters its heuristics is an implementation detail; even to the point of ignoring it (6.7.4.5):

[...] Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined. [121]

[121] For example, an implementation might never perform inline substitution, or might only perform inline substitutions to calls in the scope of an inline declaration.

The C standard does not mention LTO, so there is not much more to be said on that regard.


Now, of course, a compiler can have different heuristics and treat the inline keyword differently depending on whether it is compiling in LTO mode or not. Checking the manual and/or the implementation of your compiler is required to answer that question and possibly varies from version to version.

For GCC in particular, there is the documentation on the -flto option and on LTO internals. The issue, however, is that GCC does not currently give many details on its users' manual. Therefore, you cannot rely on it to be stable, even if you can read the current implementation and see what the heuristics are.

Anyway, given how wildly the inlining decisions of the compiler vary (vendor, version, options, etc.), there is no much point on trying to adapt your code around it. If you really need to alter the inlining decisions, you should use specific hints provided by your compiler, rather than trying to tweak the results of its algorithms. For instance, for GCC, try to use __attribute__((always_inline)).

Related: Link-time optimization and inline