llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.09k stars 12k forks source link

Document how to use LTO on Windows #55840

Open kayhayen opened 2 years ago

kayhayen commented 2 years ago

I am using LTO with Clang on many platforms with my Python compiler (https://nuitka.net) Nuitka.

On Windows, I didn't manage to get LTO to work, neither with winlibs, nor with the official Windows download:

$ LANG=C PATH=/c/Program\ Files/LLVM/bin/:$PATH clang --version
clang version 14.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

It gives an "unknown file format" error with winlibs, and with LLVM official download from above, a localize error that indicates "invalid or damaged file" (localized, I didn't figure out how to disable that yet). With file it states the file is LLVM IR bitcode.

I am assuming that the gold LLVM plugin is not available or something. But it's absolutely not obvious to me, otherwise than find not revealing anything *gold*.

Can you confirm or deny if LTO is supposed to work with the official download on Windows. I assume the GPLv3 nature might be an issue for you?

efriedma-quic commented 2 years ago

LTO on Windows should work with lld, for both MSVC and MinGW; you might need to explicitly specify -fuse-ld=lld to enable that.

Not sure whether the gold plugin works with the MinGW linker, but even if it does, I probably wouldn't recommend that; lld-link is more well tested.

fhahn commented 2 years ago

Retitled to something actionable.

alvinhochun commented 2 years ago

When I tried LTO with the https://github.com/mstorsjo/llvm-mingw toolchain some time ago, adding -flto=thin (you can also use -flto for full LTO) to CXXFLAGS and LDFLAGS just works. This toolchain uses LLD by default.

I don't know about the MSVC target, but https://clang.llvm.org/docs/ThinLTO.html#basic does have documented usage with clang-cl and lld-link so I assume it should work.

I don't think gold supports Windows or COFF at all, am I right?

efriedma-quic commented 2 years ago

I don't think gold supports Windows or COFF at all, am I right?

The "gold plugin" got its name back when the plugin API was only supported by gold, but regular binutils ld also got support for the plugin API at some point.