crate2nix
crate2nix copied to clipboard
How to enable LTO optimizations ?
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 ?
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
Hi @lblasc, thanks for posting the hack for this here :)
@lblasc did you try to upstream this to nixpkgs?
@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.
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.
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.