I’m learning something about LTO,libLTO.so is mentioned in the following article,but when I use ld.lld and ld.gold,all of them do not use libLTO.so, I want know where is this dynamic library used.
command and code are as follows:
// use ld.gold
clang a.c -flto -c
clang b.c -flto -c
clang -flto a.o b.o -o main -fuse-ld=gold
or
// use ld.lld
clang a.c -flto -c
clang b.c -flto -c
clang -flto a.o b.o -o main -fuse-ld=lld
// ----- a.c ---
#include <stdio.h>
extern void foo1(void);
extern void foo4(void);
void foo2(void) {
printf("Foo2\n");
}
void foo3(void) {
foo4();
}
int main(void) {
foo1();
}
// --- b.c ---
#include <stdio.h>
extern void foo2(void);
void foo1(void) {
foo2();
}
void foo4(void) {
printf("Foo4");
}
https://llvm.org/docs/LinkTimeOptimization.html#phase-1-read-llvm-bitcode-files
As noted https://llvm.org/docs/LinkTimeOptimization.html#liblto, libLTO.so provides a C interface to the LTO implementation. This is used by some out of tree linkers, but not by the LLVM gold plugin or by lld, which both link directly to the native C++ LTO API. That interface is defined in https://github.com/llvm/llvm-project/blob/73c6248cc2cc3acd01c3580bfdc64825c09a0fd6/llvm/include/llvm/LTO/LTO.h (in particular the add() and run() methods). The C API is much more limited and legacy at this point.
@pcc probably this documentation needs to be updated a bit - is there any documentation on the new C++ API? I’m not aware of any but want to confirm.
@hippyll123 while the general model of LTO compilation on that documentation page is correct, I would look at the header pointed to above and its use in lld or gold plugin for examples on the actual interface. E.g.:
For lld: https://github.com/llvm/llvm-project/blob/73c6248cc2cc3acd01c3580bfdc64825c09a0fd6/lld/ELF/LTO.cpp#L270 and https://github.com/llvm/llvm-project/blob/73c6248cc2cc3acd01c3580bfdc64825c09a0fd6/lld/ELF/LTO.cpp#L319
For gold-plugin: https://github.com/llvm/llvm-project/blob/73c6248cc2cc3acd01c3580bfdc64825c09a0fd6/llvm/tools/gold/gold-plugin.cpp#L1012
1 Like
Hi teresa,
Thanks for the explanation. But I am looking for some more info. I am new LTO world.
What is the job of plugin? Does it act as a “bridge” between gold linker and LLM world?
The gold linker has a plugin model for interfacing with compiler LTO support. On the LLVM side there is a gold-plugin.cpp (see the link in my prior response) that implements the various plugin handlers invoked by gold. It then interfaces with the LLVM LTO API functions. See also The LLVM gold plugin — LLVM 18.0.0git documentation to understand how to set up gold to interface with the LLVM gold plugin.
However, if you are not tied to gold, I would recommend using lld which is part of LLVM and interfaces directly with the LLVM LTO API. It is simpler to set up and use simply because you can configure your LLVM project to build it alongside everything else. See also https://lld.llvm.org/
Teresa