crate2nix icon indicating copy to clipboard operation
crate2nix copied to clipboard

How to enable LTO optimizations ?

Open lblasc opened this issue 5 years ago • 7 comments

Hello,

Thank you for this great project. I've managed to generate pretty big Cargo.nix from our workspace and it works like a charm.

Is there a way to enable LTO optimizations in release mode ?

lblasc avatar Nov 12 '20 10:11 lblasc

If someone is interested in this, I did quick hack in nixpkgs and enabled LTO + codegen-units=1 in release build:

~https://github.com/tvbeat/nixpkgs/commit/2aa98b7cc4738ac191dbed21afdf07604f4f43c8~ https://github.com/tvbeat/nixpkgs/commit/fd43d7c989d6b1549d0bac8177b19c57c184bd34

lblasc avatar Nov 20 '20 10:11 lblasc

Hi @lblasc, thanks for posting the hack for this here :)

kolloch avatar Nov 28 '20 15:11 kolloch

@lblasc did you try to upstream this to nixpkgs?

fogti avatar Feb 26 '21 17:02 fogti

@zseri Unfortunately I don't have time to prepare it for upstream inclusion.

Next thing on my agenda is to get singe crate to build in parallel, this causes major build slowdown (for us), especially with LTO enabled, then I will try to find time to upstream both things.

lblasc avatar Mar 22 '21 08:03 lblasc

I upstreamed a part of the patch in https://github.com/NixOS/nixpkgs/pull/117277, but excluded the LTO part, as it doesn't seem to be generally applicable (e.g. there seem to be some crates which have performance regressions when compiled with LTO), and I don't know if/how to add the necessary parameter to buildRustCrate, especially, because that flag should propagate to dependencies.

fogti avatar Mar 22 '21 22:03 fogti

Thank you for your efforts on this, LTO support in buildRustCrate is far from trivial, maybe to add some callback function and let user reason whether to enable LTO for matching crate.

Upstream is using codegen-units as a way of rustc parallelization, which affects LLVM part of compilation process therefore setting different values to $NIX_BUILD_CORES will affect resulting binary size and optimization level - different build machines will produce different binaries.

lblasc avatar Mar 23 '21 07:03 lblasc