Repository navigation
Description
std::autodiff requires fat-lto to work (although we intend to support embed-bc in the future).
Since it's an experimental feature, we require users to set -Zautodiff=Enable to be able to use it as well.
A while ago I opened an issue to automatically enable fat-lto if the user sets the autodiff=Enable flag, so users can't forget about it. The implementation PR was a nice improvement for most cases. Unfortunately, though, I just noticed that it looks like the implementation had a slightly different effect than desired:
I tried this code:
cargo init <name>
cd <name>
cargo add faer
RUSTFLAGS="-Z autodiff=Enable" cargo +enzyme build -rI expected to see this happen: compiles
Instead, this happened:
Compiling libm v0.2.15
Compiling proc-macro2 v1.0.101
Compiling autocfg v1.5.0
Compiling unicode-ident v1.0.19
Compiling quote v1.0.41
Compiling crossbeam-utils v0.8.21
Compiling bytemuck v1.24.0
Compiling rand_core v0.6.4
Compiling cfg-if v1.0.3
Compiling nano-gemm-codegen v0.1.0
Compiling rayon-core v1.13.0
Compiling version_check v0.9.5
Compiling libc v0.2.176
Compiling reborrow v0.5.5
Compiling either v1.15.0
Compiling dyn-stack-macros v0.1.0
Compiling paste v1.0.15
Compiling bitflags v2.9.4
Compiling zerocopy v0.8.27
error: lto cannot be used for `proc-macro` crate type without `-Zdylib-lto`
error: could not compile `dyn-stack-macros` (lib) due to 1 previous errorThe same example used to compile though, if we revert the PR and manually set "lto=fat" in our Cargo.toml.
So apparently the PR enforces fat-lto for slightly more than the equivalent Cargo.toml setting? It would be good if we could match it and not try to fat-lto proc-macro crates.
cc @Hayden602 and @helldawg in case you want to look into this follow-up issue.
Activity
bjorn3 commentedon Oct 10, 2025
Using
RUSTFLAGSwill pass the flags to all rustc invocations unless you use--target, in which case it will skip it for build scripts and proc macros. Cargo knows that proc-macros can't be LTOed and as such ignores them if you set the LTO profile config.RUSTFLAGS="-Clto=fat"would have the same issue.Fix autodiff incorrectly applying fat-lto to proc-macro crates
Rollup merge of rust-lang#147792 - osamakader:fix-autodiff-proc-macro…
Rollup merge of rust-lang#147792 - osamakader:fix-autodiff-proc-macro…
Rollup merge of rust-lang#147792 - osamakader:fix-autodiff-proc-macro…
Unrolled build for #147792
ZuseZ4 commentedon Oct 19, 2025
Thanks @bjorn3 and @osamakader.
Re-opening since the application still doesn't pass. The error message is slightly different now.
Also, for any interested contributor, if you want to test a possible fix but not build LLVM from source, this PR helps: #147888. It allows building autodiff without having to build LLVM.
osamakader commentedon Oct 20, 2025
Thanks @ZuseZ4 will try to fix that ASAP.
4 remaining items
bjorn3 commentedon Oct 25, 2025
Autodiff will require a Cargo.toml change (or setting an env var) one way or another for as long as it depends on LTO. Cargo assumes that nobody changes the LTO configuration outside of it's control and uses this assumption to set flags that significantly reduce the size of the target dir while causing compilation failures if the actual LTO configuration doesn't match what Cargo expects. With a profile config for autodiff, Cargo can set all the right flags for your as user.
ZuseZ4 commentedon Oct 25, 2025
So presumably two autodiff profiles adrelease and addebug which we add to cargo would replace both the lto=fat and the RUSTFLAGS=-Zautodiff=Enable env variable? That would help
bjorn3 commentedon Oct 25, 2025
I'm not sure if you could convince the cargo team to get dedicated profiles. I was referring to a profile option
autodiff = truesimilar to ones likelto = trueoropt-level = 3that you can specify within the[profile.release]and[profile.debug]sections.ZuseZ4 commentedon Oct 25, 2025
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Unrolled build for #148855
Rollup merge of #148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
ZuseZ4 commentedon Dec 24, 2025
Closing since the main issue has been solved, I created a new issue to track bjorn's cargo.toml recommendation here: #150345