Skip to content

Fix auto diff failing on inherent impl blocks #140104

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

Merged
Merged
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
14 changes: 6 additions & 8 deletions compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,12 @@ mod llvm_enzyme {
ast::StmtKind::Item(iitem) => extract_item_info(iitem),
_ => None,
},
Annotatable::AssocItem(assoc_item, Impl { of_trait: false }) => {
match &assoc_item.kind {
ast::AssocItemKind::Fn(box ast::Fn { sig, ident, .. }) => {
Some((assoc_item.vis.clone(), sig.clone(), ident.clone()))
}
_ => None,
Annotatable::AssocItem(assoc_item, Impl { .. }) => match &assoc_item.kind {
ast::AssocItemKind::Fn(box ast::Fn { sig, ident, .. }) => {
Some((assoc_item.vis.clone(), sig.clone(), ident.clone()))
}
}
_ => None,
},
_ => None,
}) else {
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
Expand Down Expand Up @@ -365,7 +363,7 @@ mod llvm_enzyme {
}
Annotatable::Item(iitem.clone())
}
Annotatable::AssocItem(ref mut assoc_item, i @ Impl { of_trait: false }) => {
Annotatable::AssocItem(ref mut assoc_item, i @ Impl { .. }) => {
if !assoc_item.attrs.iter().any(|a| same_attribute(&a.kind, &attr.kind)) {
assoc_item.attrs.push(attr);
}
Expand Down
41 changes: 41 additions & 0 deletions tests/pretty/autodiff/inherent_impl.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![feature(prelude_import)]
#![no_std]
//@ needs-enzyme

#![feature(autodiff)]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
//@ pretty-mode:expanded
//@ pretty-compare-only
//@ pp-exact:inherent_impl.pp

use std::autodiff::autodiff;

struct Foo {
a: f64,
}

trait MyTrait {
fn f(&self, x: f64)
-> f64;
fn df(&self, x: f64, seed: f64)
-> (f64, f64);
}

impl MyTrait for Foo {
#[rustc_autodiff]
#[inline(never)]
fn f(&self, x: f64) -> f64 {
self.a * 0.25 * (x * x - 1.0 - 2.0 * x.ln())
}
#[rustc_autodiff(Reverse, 1, Const, Active, Active)]
#[inline(never)]
fn df(&self, x: f64, dret: f64) -> (f64, f64) {
unsafe { asm!("NOP", options(pure, nomem)); };
::core::hint::black_box(self.f(x));
::core::hint::black_box((dret,));
::core::hint::black_box((self.f(x), f64::default()))
}
}
24 changes: 24 additions & 0 deletions tests/pretty/autodiff/inherent_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//@ needs-enzyme

#![feature(autodiff)]
//@ pretty-mode:expanded
//@ pretty-compare-only
//@ pp-exact:inherent_impl.pp

use std::autodiff::autodiff;

struct Foo {
a: f64,
}

trait MyTrait {
fn f(&self, x: f64) -> f64;
fn df(&self, x: f64, seed: f64) -> (f64, f64);
}

impl MyTrait for Foo {
#[autodiff(df, Reverse, Const, Active, Active)]
fn f(&self, x: f64) -> f64 {
self.a * 0.25 * (x * x - 1.0 - 2.0 * x.ln())
}
}
Loading