2

I have been trying to enable LTO on some code I have, and am trying to figure out the instructions to do it. GCC is what I am using

I see some instructions mentioned here: https://johnysswlab.com/link-time-optimizations-new-way-to-do-compiler-optimizations/

This is basically the set in the link above: To enable LTO, follow these simple steps:

Add option -flto to the invocation of compiler. Add option -flto to the invocation of the linker. Additionally, you need to add all options from the compiler invocations to the invocation of the linker. So if you called your compiler with “-march=i486 -O3 -fno-stack-protector“, you will need to pass the same options to the linker. Now you compile your program as regular. Unless you are using a very old version of the compiler, you shouldn’t expect any problems here.

I see this as well which talks about AR being switched to gcc-ar as an example:

How to write LTO-enabled code?

Questions:

  1. Is the set of instructions in this link enough? https://johnysswlab.com/link-time-optimizations-new-way-to-do-compiler-optimizations/ or is there something additional I need to do? Same for static and dynamic library compiles?

  2. How do I verify after my final build that LTO indeed was used?

Marcus White
  • 41
  • 1
  • 6

1 Answers1

2

Is the set of instructions in this link enough?

Yes.

is there something additional I need to do?

No.

Same for static and dynamic library compiles?

Yes.

How do I verify after my final build that LTO indeed was used?

How to detect code compiled with LTO?

Clang: How to check if LTO was performed

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • isnt the second link for checking with clang? I did find a few links for gcc - but they are old - want to make sure doing the right thing – Marcus White Mar 19 '22 at 15:51
  • `isnt the second link for checking with clang?` Yes it is. – KamilCuk Mar 19 '22 at 16:05
  • Does LTO work with dynamic libraries as well as static? I am seeing conflicting information about that? – Marcus White Mar 19 '22 at 18:31
  • What does it mean "work"? You use link time optimization when building a dynamic library, you can include specific compiler link time optimization data (GIMPLE for gcc) in object files. In the context of dynamic linker, LTO makes no sense, there is no optimization. – KamilCuk Mar 19 '22 at 18:44
  • Sorry - should have clarified. My question was: If I compile the dynamic libraries using LTO options, and then at the end, create the binary by dynamically linking against libraries against some other sources and create a binary, LTO will not help for this case? I think based on what you said, LTO will not help in this case? Is there a way to use LTO for cases where the libraries are dynamic and I use the dynamic linker in the end? – Marcus White Mar 21 '22 at 03:31
  • `the dynamic libraries` You have to understand the big difference between dynamic and statlic libraries. Dynamic libraries do not affect the executable in any way, they are _not_ included in the executable, they are only for a list of symbols when linking. They are separate. LTO will help when creating the executable and when creating the dynamic library, these are two separate processes. `Is there a way to use LTO for cases where the libraries are dynamic` Yes, to build the dynamic library itself. – KamilCuk Mar 21 '22 at 07:56
  • That would also imply using static libraries be better than dynamic with LTO? – Marcus White Apr 21 '22 at 00:54
  • "Better" is vague, subjective. A static library and shared library are different things, and they are unrelated to LTO, they are not "better" or worse, they have advantages and disadvantages to each other in specific workflows, overall both accomplish different goals. See `man ld.so`, compare with `man ld`. – KamilCuk Apr 21 '22 at 01:25
  • My comment more was regarding LTO - to confirm my understanding. Since a static library is built into the executable(making the executable itself larger), vs dynamic which is loaded at runtime, isnt there a higher possibility of better LTO with static libraries vs dynamic for a project? If I had an opportunity to do static or dynamic, all other things being equal, wouldnt LTO do better with static rather than dynamic? – Marcus White May 12 '22 at 20:20
  • `isnt there a higher possibility of better LTO with static libraries vs dynamic for a project?` very much yes. `wouldnt LTO do better` yes – KamilCuk May 12 '22 at 21:56