2

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

Fredrik
  • 1,389
  • 1
  • 14
  • 32
  • Do you mean "does LTO perform inlining" or are you specifically interested in the effect of using the `inline` keyword? Note that (without LTO) `inline` is "advisory" and the compiler may inline anyway, or choose not to inline regardless. – davmac Mar 27 '18 at 13:51
  • I know that LTO can do inline over different compilation units, my question is if the keyword "inline" makes a difference to the LTO process, which is done by the linker. – Fredrik Mar 27 '18 at 13:53
  • @davmac So, I'm interested in the effect of using the inline keyword, but not from the view of the compiler, but from the linker which does LTO. – Fredrik Mar 27 '18 at 13:58
  • ok. Just wanted to be clear. Unfortunately I cannot help with the answer (although I recall seeing an experiment recently showing that `inline` does affect the compiler's decision to inline, I don't know if this applies across module boundaries in LTO). – davmac Mar 27 '18 at 14:22
  • @davmac Well the fact that the inline keyword can affect the compiler is well known. – Fredrik Mar 27 '18 at 14:45
  • if you say so. As I mentioned, the keyword is advisory only. – davmac Mar 27 '18 at 14:51
  • @davmac I know, that's why I said "can effect" – Fredrik Mar 27 '18 at 14:52
  • So by "can effect" (sic) you mean "can affect in theory"? Well yes, but that's not what I meant. If your question is could `inline` affect LTO's inlining in theory, the answer is clearly "yes". – davmac Mar 27 '18 at 14:54
  • @davmac No that is not clear, it's not clear if LTO is affected by the inline keyword or not, LTO is not done by the compiler. – Fredrik Mar 27 '18 at 15:04
  • LTO is actually done by the compiler, despite the name (possibly via a plugin to the actual linker). And while I don't know whether `inline` affects LTO, it clearly could theoretically do so (the GIMPLE representation that is stored in the object could contain "inline" attributes, and the LTO module of the compiler could honour them). – davmac Mar 27 '18 at 15:08
  • @davmac I'm fairly certain that link time optimization is done by the linker and not the compiler. – Fredrik Mar 27 '18 at 15:09
  • 1
    In absence of a better quote, see https://gcc.gnu.org/projects/lto/lto.pdf - _A new GCC front end will be provided to serve as the link-time optimizer_ - "GCC front end" implies compiler (even if it is called from the linker, as a plugin, as I mentioned). But this is really just word-choice/semantics; let's agree that it doesn't matter. – davmac Mar 27 '18 at 15:12
  • @Fredrik [here](https://stackoverflow.com/questions/5987020/can-the-linker-inline-functions) explain about gcc flto , if you missed to read – ntshetty Mar 29 '18 at 04:29

1 Answers1

1

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

Acorn
  • 24,970
  • 5
  • 40
  • 69