Stream: t-compiler/help

Topic: when to pass LTO flag


Elichai Turkel (May 26 2020 at 10:09, on Zulip):

Hi,
in gcc/clang lto requires you to pass -flto to both the compiler and linker.
but when I put lto = true in the Cargo.toml and compile with --verbose I see -Clto only in the last rustc invocation and not in all of the deps invocations. how does this work?

bjorn3 (May 26 2020 at 10:23, on Zulip):

When compiling an rlib, the LLVM bitcode is included by default in the resulting rlib in addition to the object files. -Clto is only used to chose between linking the object files and performing LTO using the bitcode.

bjorn3 (May 26 2020 at 10:25, on Zulip):

On nightly you can use -Cembed-bitcode=no to avoid generating LLVM bitcode for rlibs. I believe cargo will use this flag when it lands on stable if lto is disabled to save disk space and a bit of compilation time.

bjorn3 (May 26 2020 at 10:26, on Zulip):

-Clinker-plugin-lto disables the generation of object files in the rlibs I believe. I am not sure though.

Eric Huss (May 26 2020 at 13:44, on Zulip):

Just to expand on what bjorn3 said:

Default rlib behavior = both object code and llvm bitcode
-Clinker-plugin-lto = bitcode only (artifact will only be used with LTO)
-Cembed-bitcode=no = object code only (will never be used with LTO)

I think, usually, with a regular rlib, -C lto would just be ignored. Cargo avoids passing it for libraries because the flag is incompatible with other things like -C prefer-dynamic.

This has all recently changed in nightly (1.45), and the details are in https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/lto.rs

Last update: Sep 27 2020 at 13:00UTC