Skip to content

Commit e42a3ba

Browse files
authored
Rollup merge of #153324 - ZuseZ4:fix-ad-impl-parsing, r=oli-obk
fix autodiff parsing for non-trait impl fixes: #153322 @Sa4dUs Looks like we missed a case. But also, going through the code, line 455 seems suspicious to me: `Annotatable::AssocItem(d_fn, Impl { of_trait: false })` Are we sure that this should always be an Impl, and never an impl of a trait? r? @oli-obk
2 parents 5bd6a0c + 52e0f3f commit e42a3ba

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ mod llvm_enzyme {
214214
// first get information about the annotable item: visibility, signature, name and generic
215215
// parameters.
216216
// these will be used to generate the differentiated version of the function
217-
let Some((vis, sig, primal, generics, impl_of_trait)) = (match &item {
217+
let Some((vis, sig, primal, generics, is_impl)) = (match &item {
218218
Annotatable::Item(iitem) => {
219219
extract_item_info(iitem).map(|(v, s, p, g)| (v, s, p, g, false))
220220
}
@@ -224,13 +224,13 @@ mod llvm_enzyme {
224224
}
225225
_ => None,
226226
},
227-
Annotatable::AssocItem(assoc_item, Impl { of_trait }) => match &assoc_item.kind {
227+
Annotatable::AssocItem(assoc_item, Impl { of_trait: _ }) => match &assoc_item.kind {
228228
ast::AssocItemKind::Fn(box ast::Fn { sig, ident, generics, .. }) => Some((
229229
assoc_item.vis.clone(),
230230
sig.clone(),
231231
ident.clone(),
232232
generics.clone(),
233-
*of_trait,
233+
true,
234234
)),
235235
_ => None,
236236
},
@@ -328,7 +328,7 @@ mod llvm_enzyme {
328328
span,
329329
&d_sig,
330330
&generics,
331-
impl_of_trait,
331+
is_impl,
332332
)],
333333
);
334334

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ compile-flags: -Zautodiff=Enable -Zautodiff=NoPostopt -C opt-level=3 -Clto=fat
2+
//@ no-prefer-dynamic
3+
//@ needs-enzyme
4+
5+
// Just check it does not crash for now
6+
// CHECK: ;
7+
#![feature(autodiff)]
8+
9+
use std::autodiff::autodiff_reverse;
10+
11+
#[derive(Clone)]
12+
struct OptProblem {
13+
a: f64,
14+
b: f64,
15+
}
16+
17+
impl OptProblem {
18+
#[autodiff_reverse(d_objective, Duplicated, Duplicated, Duplicated)]
19+
fn objective(&self, x: &[f64], out: &mut f64) {
20+
*out = self.a + x[0].sqrt() * self.b
21+
}
22+
}
23+
24+
fn main() {
25+
let p = OptProblem { a: 1., b: 2. };
26+
let x = [2.0];
27+
28+
let mut p_shadow = OptProblem { a: 0., b: 0. };
29+
let mut dx = [0.0];
30+
let mut out = 0.0;
31+
let mut dout = 1.0;
32+
33+
p.d_objective(&mut p_shadow, &x, &mut dx, &mut out, &mut dout);
34+
35+
dbg!(dx);
36+
}

0 commit comments

Comments
 (0)