How lld invoke LTO or thinLTO and is there some cases and some method to get the step-by-step message for me to understand how LTO worked?

Hi:
I want to learn how LTO and thinLTO work? But I’m confused how lld invoked either library. How can I find it in lld. Should i be familiar with lld source code? And i also think if there is some easy test case and with step-by- step debugging or some log messages can help me learn this? Is there some method can do stuff like this?
Thank you very much.
ZhengXianLi

Hi ZhengXianLi,

The main entry point into the LTO LLVM code from lld is in https://github.com/llvm/llvm-project/blob/master/lld/ELF/LTO.cpp. Specifically see the call to ltoObj->run which launches the main LTO handling after all of the setup here: https://github.com/llvm/llvm-project/blob/master/lld/ELF/LTO.cpp#L309. Most of the actual LTO handling is in the LLVM LTO code here: https://github.com/llvm/llvm-project/blob/master/llvm/lib/LTO/LTO.cpp
and here: https://github.com/llvm/llvm-project/blob/master/llvm/lib/LTO/LTOBackend.cpp
Note there is an old LTO API implementation in LTOModule.cpp and *LTOCodeGenerator.cpp that is not used by lld, gold or bfd (just ld64 and some custom linkers nowadays that haven’t yet migrated). The newer LTO API implementation in LTO.cpp and LTOBackend.cpp relies on linker resolutions passed by lld (or gold/bfd via gold-plugin.cpp) via that lld file I pointed to.

The following lld test contain some simple test cases for thinLTO:
https://github.com/llvm/llvm-project/blob/master/lld/test/ELF/lto/thinlto.ll

To run them with regular LTO instead of ThinLTO, just remove the -module-summary from the opt invocations.

There’s clang documentation on ThinLTO here: https://clang.llvm.org/docs/ThinLTO.html
and an old blog post on ThinLTO here: https://blog.llvm.org/posts/2016-06-21-thinlto-scalable-and-incremental-lto/
but they don’t get into the details of the code implementation.

Hope that helps,
Teresa

Hi Teresa:
Thank you, It’s very help for me.
ZhengxianLi