Skip to content

Commit a06e6e4

Browse files
authored
Rollup merge of rust-lang#148855 - ZuseZ4:autodiff-lto-error, r=bjorn3
Error if an autodiff user does not set lto=fat Based on your feedback, I started to provide a nice error message for a lack of `lto=fat`, instead of us forcing it. In a next step, we should replace `RUSTFLAGS="-Zautodiff=Enable"` with another Cargo.toml setting, as discussed here: rust-lang#147487 (comment) As another improvement, we should also figure out why rlib builds do not properly obey the fat=lto setting. `@bjorn3`
2 parents c0e13c3 + c8bae8c commit a06e6e4

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
2+
codegen_llvm_autodiff_without_lto = using the autodiff feature requires setting `lto="fat"` in your Cargo.toml
23
34
codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err}
45

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3232
}
3333
}
3434

35+
#[derive(Diagnostic)]
36+
#[diag(codegen_llvm_autodiff_without_lto)]
37+
pub(crate) struct AutoDiffWithoutLto;
38+
3539
#[derive(Diagnostic)]
3640
#[diag(codegen_llvm_autodiff_without_enable)]
3741
pub(crate) struct AutoDiffWithoutEnable;

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::abi::FnAbiLlvmExt;
2525
use crate::builder::Builder;
2626
use crate::builder::autodiff::{adjust_activity_to_abi, generate_enzyme_call};
2727
use crate::context::CodegenCx;
28-
use crate::errors::AutoDiffWithoutEnable;
28+
use crate::errors::{AutoDiffWithoutEnable, AutoDiffWithoutLto};
2929
use crate::llvm::{self, Metadata, Type, Value};
3030
use crate::type_of::LayoutLlvmExt;
3131
use crate::va_arg::emit_va_arg;
@@ -1146,6 +1146,9 @@ fn codegen_autodiff<'ll, 'tcx>(
11461146
if !tcx.sess.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable) {
11471147
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutEnable);
11481148
}
1149+
if tcx.sess.lto() != rustc_session::config::Lto::Fat {
1150+
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
1151+
}
11491152

11501153
let fn_args = instance.args;
11511154
let callee_ty = instance.ty(tcx, bx.typing_env());

compiler/rustc_session/src/session.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,6 @@ impl Session {
594594

595595
/// Calculates the flavor of LTO to use for this compilation.
596596
pub fn lto(&self) -> config::Lto {
597-
// Autodiff currently requires fat-lto to have access to the llvm-ir of all (indirectly) used functions and types.
598-
// fat-lto is the easiest solution to this requirement, but quite expensive.
599-
// FIXME(autodiff): Make autodiff also work with embed-bc instead of fat-lto.
600-
// Don't apply fat-lto to proc-macro crates as they cannot use fat-lto without -Zdylib-lto
601-
if self.opts.autodiff_enabled() && !self.opts.crate_types.contains(&CrateType::ProcMacro) {
602-
return config::Lto::Fat;
603-
}
604-
605597
// If our target has codegen requirements ignore the command line
606598
if self.target.requires_lto {
607599
return config::Lto::Fat;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: using the autodiff feature requires setting `lto="fat"` in your Cargo.toml
2+
3+
error: aborting due to 1 previous error
4+

tests/ui/autodiff/no_lto_flag.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ needs-enzyme
2+
//@ no-prefer-dynamic
3+
//@ revisions: with_lto no_lto
4+
//@[with_lto] compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat
5+
//@[no_lto] compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=thin
6+
7+
#![feature(autodiff)]
8+
//@[no_lto] build-fail
9+
//@[with_lto] build-pass
10+
11+
// Autodiff requires users to enable lto=fat (for now).
12+
// In the past, autodiff did not run if users forget to enable fat-lto, which caused functions to
13+
// returning zero-derivatives. That's obviously wrong and confusing to users. We now added a check
14+
// which will abort compilation instead.
15+
16+
use std::autodiff::autodiff_reverse;
17+
//[no_lto]~? ERROR using the autodiff feature requires setting `lto="fat"` in your Cargo.toml
18+
19+
#[autodiff_reverse(d_square, Duplicated, Active)]
20+
fn square(x: &f64) -> f64 {
21+
*x * *x
22+
}
23+
24+
fn main() {
25+
let xf64: f64 = std::hint::black_box(3.0);
26+
27+
let mut df_dxf64: f64 = std::hint::black_box(0.0);
28+
29+
let _output_f64 = d_square(&xf64, &mut df_dxf64, 1.0);
30+
assert_eq!(6.0, df_dxf64);
31+
}

0 commit comments

Comments
 (0)