5

With whole-program (a.k.a link-time) optimization turned on, is inlining affected by where a method is implemented?

Without whole-program-optimization turned on, methods which are publicly declared in a header but implemented in the matching cpp obviously can't be inlined during the compilation of a different translation unit (a different cpp which includes the above header). So if you want the method to be inline-able, it must be implemented in the exporting header.

With whole-program-optimization, does it make any difference, inlining-wise, whether the method is implemented in the header or not? That is, is it possible/likely for the compiler to miss an opportunity to inline a method implemented in a cpp, which it would not miss had the method been implemented in the header instead?

I'm specifically interested in the answer for Apple's Clang (-flto flag) and for Visual Studio (/GL flag).

Danra
  • 9,546
  • 5
  • 59
  • 117
  • The compiler can inline a function when it is declared static to a specific module (it can't be called externally). I don't think any compiler will allow a function to exist in both states (inlined and callable from another module). It doesn't matter if you define it in the header or the CPP file, the issue is whether or not it's static to the module it resides in. – BitBank Mar 07 '17 at 15:45
  • @BitBank the question regards inlining of non-static functions/methods inside translation units which include the header in which the functions/methods were declared. – Danra Mar 07 '17 at 15:58
  • it sounds like you answered your own question. How can a function be inlined if it's not static? If the compiler thinks that a function could be called from outside its module, then it can't inline it. I guess a better way to think about it is that if you're depending on the compiler's behavior for a specific situation, you may need to change the structure of your code to guarantee the results you want without depending on the compiler to generate the "ideal" output. – BitBank Mar 07 '17 at 16:57
  • @BitBank You misunderstand. Take a look at https://msdn.microsoft.com/en-us/library/0zza0de8.aspx, for example, where it states that with whole program optimization it's possible to "Inline a function in a module even when the function is defined in another module." – Danra Mar 07 '17 at 17:31

0 Answers0