[llvm] [LTO] Drop the weak function if there is a non-weak global symbol (PR #76287)

via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 26 10:35:36 PST 2023


bjorn3 wrote:

> Is rust using one of these tools or is it interfacing with the module linking directly via LLVM libraries?

Rustc's LLVM backend directly interfaces with libLLVM both for generating the LLVM modules as well as doing LTO. This is necessary for what rustc calls "thin local lto". This is thinLTO, except only applying to a single crate (library/smallest compilation unit) rather than than the linker input. This mode was introduced to allow creating multiple codegen units for a single crate in release mode (to improve build performance) without hurting runtime performance due to not being able to do optimizations across codegen units. Having rustc do LTO using libLLVM is also a lot easier than convincing the linker to use the right libLTO for the LLVM version used by rustc. While for most linkers rustc could ship a matching libLTO, this wouldn't work for lld as far as I understand it, so if the system installation of LLVM is older than rustc (which it is for most users as rustc uses the latest LLVM version while distros generally keep an older one for the entire lifespan of the distro), then you are out of luck. Rustc does support `-Clinker-plugin-lto` to let the linker do LTO just like clang does, but in that case it is your own responsibility to make sure the right libLTO version is used. By setting the right clang version as linker, by passing the path to libLTO as argument to `-Clinker-plugin-lto` or by using the right lld version.

That said for fat LTO I believe we use `llvm::Linker::linkInModule` (https://github.com/rust-lang/rust/blob/e1fadb2c35a6082867a037f012bfdfc5eb686211/compiler/rustc_llvm/llvm-wrapper/Linker.cpp#L44)

For thinLTO it seems we duplicated a fair amount of logic from LLVM: https://github.com/rust-lang/rust/blob/e1fadb2c35a6082867a037f012bfdfc5eb686211/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp#L1242-L1355 for the global analysis and then https://github.com/rust-lang/rust/blob/e1fadb2c35a6082867a037f012bfdfc5eb686211/compiler/rustc_codegen_llvm/src/back/lto.rs#L726-L781 for the per-module optimizations. (The last code snippet references some functions named `LLVMRust*`. These are wrappers around the C++ api exposing a C interface. Rust doesn't have native C++ FFI capability.)

https://github.com/llvm/llvm-project/pull/76287


More information about the llvm-commits mailing list