diff --git a/compiler/rustc_codegen_llvm/messages.ftl b/compiler/rustc_codegen_llvm/messages.ftl index ce9a51b539d83..c9d28160d66f7 100644 --- a/compiler/rustc_codegen_llvm/messages.ftl +++ b/compiler/rustc_codegen_llvm/messages.ftl @@ -1,4 +1,5 @@ codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable +codegen_llvm_autodiff_without_lto = using the autodiff feature requires setting `lto="fat"` in your Cargo.toml codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err} diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 627b0c9ff3b33..629afee8a6677 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -32,6 +32,10 @@ impl Diagnostic<'_, G> for ParseTargetMachineConfig<'_> { } } +#[derive(Diagnostic)] +#[diag(codegen_llvm_autodiff_without_lto)] +pub(crate) struct AutoDiffWithoutLto; + #[derive(Diagnostic)] #[diag(codegen_llvm_autodiff_without_enable)] pub(crate) struct AutoDiffWithoutEnable; diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 0626cb3f2f16b..bbe3b5b59f5b4 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -24,7 +24,7 @@ use crate::abi::FnAbiLlvmExt; use crate::builder::Builder; use crate::builder::autodiff::{adjust_activity_to_abi, generate_enzyme_call}; use crate::context::CodegenCx; -use crate::errors::AutoDiffWithoutEnable; +use crate::errors::{AutoDiffWithoutEnable, AutoDiffWithoutLto}; use crate::llvm::{self, Metadata, Type, Value}; use crate::type_of::LayoutLlvmExt; use crate::va_arg::emit_va_arg; @@ -1145,6 +1145,9 @@ fn codegen_autodiff<'ll, 'tcx>( if !tcx.sess.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable) { let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutEnable); } + if tcx.sess.lto() != rustc_session::config::Lto::Fat { + let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto); + } let fn_args = instance.args; let callee_ty = instance.ty(tcx, bx.typing_env()); diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 9fb3c35d5ef73..fe13bb821c9b7 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -594,14 +594,6 @@ impl Session { /// Calculates the flavor of LTO to use for this compilation. pub fn lto(&self) -> config::Lto { - // Autodiff currently requires fat-lto to have access to the llvm-ir of all (indirectly) used functions and types. - // fat-lto is the easiest solution to this requirement, but quite expensive. - // FIXME(autodiff): Make autodiff also work with embed-bc instead of fat-lto. - // Don't apply fat-lto to proc-macro crates as they cannot use fat-lto without -Zdylib-lto - if self.opts.autodiff_enabled() && !self.opts.crate_types.contains(&CrateType::ProcMacro) { - return config::Lto::Fat; - } - // If our target has codegen requirements ignore the command line if self.target.requires_lto { return config::Lto::Fat; diff --git a/tests/ui/autodiff/no_lto_flag.no_lto.stderr b/tests/ui/autodiff/no_lto_flag.no_lto.stderr new file mode 100644 index 0000000000000..abc6af5b62965 --- /dev/null +++ b/tests/ui/autodiff/no_lto_flag.no_lto.stderr @@ -0,0 +1,4 @@ +error: using the autodiff feature requires setting `lto="fat"` in your Cargo.toml + +error: aborting due to 1 previous error + diff --git a/tests/ui/autodiff/no_lto_flag.rs b/tests/ui/autodiff/no_lto_flag.rs new file mode 100644 index 0000000000000..6194b1effc34f --- /dev/null +++ b/tests/ui/autodiff/no_lto_flag.rs @@ -0,0 +1,31 @@ +//@ needs-enzyme +//@ no-prefer-dynamic +//@ revisions: with_lto no_lto +//@[with_lto] compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat +//@[no_lto] compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=thin + +#![feature(autodiff)] +//@[no_lto] build-fail +//@[with_lto] build-pass + +// Autodiff requires users to enable lto=fat (for now). +// In the past, autodiff did not run if users forget to enable fat-lto, which caused functions to +// returning zero-derivatives. That's obviously wrong and confusing to users. We now added a check +// which will abort compilation instead. + +use std::autodiff::autodiff_reverse; +//[no_lto]~? ERROR using the autodiff feature requires setting `lto="fat"` in your Cargo.toml + +#[autodiff_reverse(d_square, Duplicated, Active)] +fn square(x: &f64) -> f64 { + *x * *x +} + +fn main() { + let xf64: f64 = std::hint::black_box(3.0); + + let mut df_dxf64: f64 = std::hint::black_box(0.0); + + let _output_f64 = d_square(&xf64, &mut df_dxf64, 1.0); + assert_eq!(6.0, df_dxf64); +}