Skip to content

nicer autodiff error handling #142842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_ssa/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ codegen_ssa_aix_strip_not_used = using host's `strip` binary to cross-compile to

codegen_ssa_archive_build_failure = failed to build archive at `{$path}`: {$error}

codegen_ssa_autodiff_without_lto = using the autodiff feature requires using fat-lto
codegen_ssa_autodiff_lib_unsupported = using the autodiff feature with library builds is not yet supported

codegen_ssa_autodiff_without_lto = using the autodiff feature requires using fat-lto.

codegen_ssa_bare_instruction_set = `#[instruction_set]` requires an argument

Expand Down
13 changes: 10 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use tracing::debug;
use super::link::{self, ensure_removed};
use super::lto::{self, SerializedModule};
use super::symbol_export::symbol_name_for_instance_in_crate;
use crate::errors::{AutodiffWithoutLto, ErrorCreatingRemarkDir};
use crate::errors::{AutodiffLibraryBuild, AutodiffWithoutLto, ErrorCreatingRemarkDir};
use crate::traits::*;
use crate::{
CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind,
Expand Down Expand Up @@ -419,7 +419,12 @@ fn generate_lto_work<B: ExtraBackendMethods>(
} else {
if !autodiff.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe pick a random (like the first) entry from this list and use its span so in case of a larger crate the user is aware of why this is being emitted. I could see it being confusing in case someone is adding cfgs to turn on/off autodiff and got sth wrong in the process

let dcx = cgcx.create_dcx();
dcx.handle().emit_fatal(AutodiffWithoutLto {});
if cgcx.crate_types.contains(&CrateType::Rlib) {
dcx.handle().emit_fatal(AutodiffLibraryBuild {});
}
if cgcx.lto != Lto::Fat {
dcx.handle().emit_fatal(AutodiffWithoutLto {});
}
}
assert!(needs_fat_lto.is_empty());
let (lto_modules, copy_jobs) = B::run_thin_lto(cgcx, needs_thin_lto, import_only_modules)
Expand Down Expand Up @@ -1456,6 +1461,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
if needs_fat_lto.is_empty()
&& needs_thin_lto.is_empty()
&& lto_import_only_modules.is_empty()
&& autodiff_items.is_empty()
{
// Nothing more to do!
break;
Expand All @@ -1469,13 +1475,14 @@ fn start_executing_work<B: ExtraBackendMethods>(
assert!(!started_lto);
started_lto = true;

let autodiff_items = mem::take(&mut autodiff_items);
let needs_fat_lto = mem::take(&mut needs_fat_lto);
let needs_thin_lto = mem::take(&mut needs_thin_lto);
let import_only_modules = mem::take(&mut lto_import_only_modules);

for (work, cost) in generate_lto_work(
&cgcx,
autodiff_items.clone(),
autodiff_items,
needs_fat_lto,
needs_thin_lto,
import_only_modules,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub(crate) struct CguNotRecorded<'a> {
#[diag(codegen_ssa_autodiff_without_lto)]
pub struct AutodiffWithoutLto;

#[derive(Diagnostic)]
#[diag(codegen_ssa_autodiff_lib_unsupported)]
pub struct AutodiffLibraryBuild;

#[derive(Diagnostic)]
#[diag(codegen_ssa_unknown_reuse_kind)]
pub(crate) struct UnknownReuseKind {
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/autodiff/autodiff_in_rlib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(autodiff)]
#![crate_type = "rlib"]
//@ needs-enzyme
//@ compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat
//@ build-fail

// We test that we fail to compile if a user applied an autodiff_ macro in src/lib.rs,
// since autodiff doesn't work in libraries yet. In the past we used to just return zeros in the
// autodiffed functions, which is obviously confusing and wrong, so erroring is an improvement.

use std::autodiff::autodiff_reverse;
//~? ERROR: using the autodiff feature with library builds is not yet supported

#[autodiff_reverse(d_square, Duplicated, Active)]
pub fn square(x: &f64) -> f64 {
*x * *x
}
4 changes: 4 additions & 0 deletions tests/ui/autodiff/autodiff_in_rlib.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: using the autodiff feature with library builds is not yet supported

error: aborting due to 1 previous error

4 changes: 4 additions & 0 deletions tests/ui/autodiff/no_lto_flag.no_lto.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: using the autodiff feature requires using fat-lto.

error: aborting due to 1 previous error

31 changes: 31 additions & 0 deletions tests/ui/autodiff/no_lto_flag.rs
Original file line number Diff line number Diff line change
@@ -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 using fat-lto.

#[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);
}
Loading