I’m reading the introduction of LTO(LLVM Link Time Optimization: Design and Implementation — LLVM 17.0.0git documentation). After compiling a.c and main.c, clang use linker to link both of them and use lto to optimize whole program. The question is that can llvm handle object file? i.e. Can lto remove foo4() in main.o?
AFAIK llvm can only handle llvm bitcode file, but main.o is an object file.
You need to compile the object files with
-flto=thin in order to store bitcode in the object file, then you can run LTO from the linker.
Yes, but the tutorial compile with the following commands, so why lto can optimize main.o ?
% clang -flto -c a.c -o a.o # ← a.o is LLVM bitcode file
% clang -c main.c -o main.o # ← main.o is native object file
% clang -flto a.o main.o -o main # ← standard link command with -flto
Why do you think it is optimising in the linker? What is it you see?
LTO is eliminating all references to foo4. Later, when the linker looks at the compiled binaries, it sees no references to foo4 and can eliminate it at that point. It is not LTO (by itself) eliminating foo4.