Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 89d93f7

Browse files
authoredMar 21, 2025
Rollup merge of rust-lang#138627 - EnzymeAD:autodiff-cleanups, r=oli-obk
Autodiff cleanups Splitting out some cleanups to reduce the size of my batching PR and simplify ````@haenoe```` 's [PR](rust-lang#138314). r? ````@oli-obk```` Tracking: - rust-lang#124509
2 parents 3f6d0d3 + 81b2d55 commit 89d93f7

File tree

7 files changed

+209
-176
lines changed

7 files changed

+209
-176
lines changed
 

‎compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 95 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ mod llvm_enzyme {
2626

2727
use crate::errors;
2828

29+
pub(crate) fn outer_normal_attr(
30+
kind: &P<rustc_ast::NormalAttr>,
31+
id: rustc_ast::AttrId,
32+
span: Span,
33+
) -> rustc_ast::Attribute {
34+
let style = rustc_ast::AttrStyle::Outer;
35+
let kind = rustc_ast::AttrKind::Normal(kind.clone());
36+
rustc_ast::Attribute { kind, id, style, span }
37+
}
38+
2939
// If we have a default `()` return type or explicitley `()` return type,
3040
// then we often can skip doing some work.
3141
fn has_ret(ty: &FnRetTy) -> bool {
@@ -224,20 +234,8 @@ mod llvm_enzyme {
224234
.filter(|a| **a == DiffActivity::Active || **a == DiffActivity::ActiveOnly)
225235
.count() as u32;
226236
let (d_sig, new_args, idents, errored) = gen_enzyme_decl(ecx, &sig, &x, span);
227-
let new_decl_span = d_sig.span;
228237
let d_body = gen_enzyme_body(
229-
ecx,
230-
&x,
231-
n_active,
232-
&sig,
233-
&d_sig,
234-
primal,
235-
&new_args,
236-
span,
237-
sig_span,
238-
new_decl_span,
239-
idents,
240-
errored,
238+
ecx, &x, n_active, &sig, &d_sig, primal, &new_args, span, sig_span, idents, errored,
241239
);
242240
let d_ident = first_ident(&meta_item_vec[0]);
243241

@@ -270,36 +268,39 @@ mod llvm_enzyme {
270268
};
271269
let inline_never_attr = P(ast::NormalAttr { item: inline_item, tokens: None });
272270
let new_id = ecx.sess.psess.attr_id_generator.mk_attr_id();
273-
let attr: ast::Attribute = ast::Attribute {
274-
kind: ast::AttrKind::Normal(rustc_ad_attr.clone()),
275-
id: new_id,
276-
style: ast::AttrStyle::Outer,
277-
span,
278-
};
271+
let attr = outer_normal_attr(&rustc_ad_attr, new_id, span);
279272
let new_id = ecx.sess.psess.attr_id_generator.mk_attr_id();
280-
let inline_never: ast::Attribute = ast::Attribute {
281-
kind: ast::AttrKind::Normal(inline_never_attr),
282-
id: new_id,
283-
style: ast::AttrStyle::Outer,
284-
span,
285-
};
273+
let inline_never = outer_normal_attr(&inline_never_attr, new_id, span);
274+
275+
// We're avoid duplicating the attributes `#[rustc_autodiff]` and `#[inline(never)]`.
276+
fn same_attribute(attr: &ast::AttrKind, item: &ast::AttrKind) -> bool {
277+
match (attr, item) {
278+
(ast::AttrKind::Normal(a), ast::AttrKind::Normal(b)) => {
279+
let a = &a.item.path;
280+
let b = &b.item.path;
281+
a.segments.len() == b.segments.len()
282+
&& a.segments.iter().zip(b.segments.iter()).all(|(a, b)| a.ident == b.ident)
283+
}
284+
_ => false,
285+
}
286+
}
286287

287288
// Don't add it multiple times:
288289
let orig_annotatable: Annotatable = match item {
289290
Annotatable::Item(ref mut iitem) => {
290-
if !iitem.attrs.iter().any(|a| a.id == attr.id) {
291+
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &attr.kind)) {
291292
iitem.attrs.push(attr);
292293
}
293-
if !iitem.attrs.iter().any(|a| a.id == inline_never.id) {
294+
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &inline_never.kind)) {
294295
iitem.attrs.push(inline_never.clone());
295296
}
296297
Annotatable::Item(iitem.clone())
297298
}
298299
Annotatable::AssocItem(ref mut assoc_item, i @ Impl) => {
299-
if !assoc_item.attrs.iter().any(|a| a.id == attr.id) {
300+
if !assoc_item.attrs.iter().any(|a| same_attribute(&a.kind, &attr.kind)) {
300301
assoc_item.attrs.push(attr);
301302
}
302-
if !assoc_item.attrs.iter().any(|a| a.id == inline_never.id) {
303+
if !assoc_item.attrs.iter().any(|a| same_attribute(&a.kind, &inline_never.kind)) {
303304
assoc_item.attrs.push(inline_never.clone());
304305
}
305306
Annotatable::AssocItem(assoc_item.clone(), i)
@@ -314,13 +315,7 @@ mod llvm_enzyme {
314315
delim: rustc_ast::token::Delimiter::Parenthesis,
315316
tokens: ts,
316317
});
317-
let d_attr: ast::Attribute = ast::Attribute {
318-
kind: ast::AttrKind::Normal(rustc_ad_attr.clone()),
319-
id: new_id,
320-
style: ast::AttrStyle::Outer,
321-
span,
322-
};
323-
318+
let d_attr = outer_normal_attr(&rustc_ad_attr, new_id, span);
324319
let d_annotatable = if is_impl {
325320
let assoc_item: AssocItemKind = ast::AssocItemKind::Fn(asdf);
326321
let d_fn = P(ast::AssocItem {
@@ -361,30 +356,27 @@ mod llvm_enzyme {
361356
ty
362357
}
363358

364-
/// We only want this function to type-check, since we will replace the body
365-
/// later on llvm level. Using `loop {}` does not cover all return types anymore,
366-
/// so instead we build something that should pass. We also add a inline_asm
367-
/// line, as one more barrier for rustc to prevent inlining of this function.
368-
/// FIXME(ZuseZ4): We still have cases of incorrect inlining across modules, see
369-
/// <https://github.com/EnzymeAD/rust/issues/173>, so this isn't sufficient.
370-
/// It also triggers an Enzyme crash if we due to a bug ever try to differentiate
371-
/// this function (which should never happen, since it is only a placeholder).
372-
/// Finally, we also add back_box usages of all input arguments, to prevent rustc
373-
/// from optimizing any arguments away.
374-
fn gen_enzyme_body(
359+
// Will generate a body of the type:
360+
// ```
361+
// {
362+
// unsafe {
363+
// asm!("NOP");
364+
// }
365+
// ::core::hint::black_box(primal(args));
366+
// ::core::hint::black_box((args, ret));
367+
// <This part remains to be done by following function>
368+
// }
369+
// ```
370+
fn init_body_helper(
375371
ecx: &ExtCtxt<'_>,
376-
x: &AutoDiffAttrs,
377-
n_active: u32,
378-
sig: &ast::FnSig,
379-
d_sig: &ast::FnSig,
372+
span: Span,
380373
primal: Ident,
381374
new_names: &[String],
382-
span: Span,
383375
sig_span: Span,
384376
new_decl_span: Span,
385-
idents: Vec<Ident>,
377+
idents: &[Ident],
386378
errored: bool,
387-
) -> P<ast::Block> {
379+
) -> (P<ast::Block>, P<ast::Expr>, P<ast::Expr>, P<ast::Expr>) {
388380
let blackbox_path = ecx.std_path(&[sym::hint, sym::black_box]);
389381
let noop = ast::InlineAsm {
390382
asm_macro: ast::AsmMacro::Asm,
@@ -433,6 +425,51 @@ mod llvm_enzyme {
433425
}
434426
body.stmts.push(ecx.stmt_semi(black_box_remaining_args));
435427

428+
(body, primal_call, black_box_primal_call, blackbox_call_expr)
429+
}
430+
431+
/// We only want this function to type-check, since we will replace the body
432+
/// later on llvm level. Using `loop {}` does not cover all return types anymore,
433+
/// so instead we manually build something that should pass the type checker.
434+
/// We also add a inline_asm line, as one more barrier for rustc to prevent inlining
435+
/// or const propagation. inline_asm will also triggers an Enzyme crash if due to another
436+
/// bug would ever try to accidentially differentiate this placeholder function body.
437+
/// Finally, we also add back_box usages of all input arguments, to prevent rustc
438+
/// from optimizing any arguments away.
439+
fn gen_enzyme_body(
440+
ecx: &ExtCtxt<'_>,
441+
x: &AutoDiffAttrs,
442+
n_active: u32,
443+
sig: &ast::FnSig,
444+
d_sig: &ast::FnSig,
445+
primal: Ident,
446+
new_names: &[String],
447+
span: Span,
448+
sig_span: Span,
449+
idents: Vec<Ident>,
450+
errored: bool,
451+
) -> P<ast::Block> {
452+
let new_decl_span = d_sig.span;
453+
454+
// Just adding some default inline-asm and black_box usages to prevent early inlining
455+
// and optimizations which alter the function signature.
456+
//
457+
// The bb_primal_call is the black_box call of the primal function. We keep it around,
458+
// since it has the convenient property of returning the type of the primal function,
459+
// Remember, we only care to match types here.
460+
// No matter which return we pick, we always wrap it into a std::hint::black_box call,
461+
// to prevent rustc from propagating it into the caller.
462+
let (mut body, primal_call, bb_primal_call, bb_call_expr) = init_body_helper(
463+
ecx,
464+
span,
465+
primal,
466+
new_names,
467+
sig_span,
468+
new_decl_span,
469+
&idents,
470+
errored,
471+
);
472+
436473
if !has_ret(&d_sig.decl.output) {
437474
// there is no return type that we have to match, () works fine.
438475
return body;
@@ -444,7 +481,7 @@ mod llvm_enzyme {
444481

445482
if primal_ret && n_active == 0 && x.mode.is_rev() {
446483
// We only have the primal ret.
447-
body.stmts.push(ecx.stmt_expr(black_box_primal_call));
484+
body.stmts.push(ecx.stmt_expr(bb_primal_call));
448485
return body;
449486
}
450487

@@ -536,11 +573,11 @@ mod llvm_enzyme {
536573
return body;
537574
}
538575
[arg] => {
539-
ret = ecx.expr_call(new_decl_span, blackbox_call_expr, thin_vec![arg.clone()]);
576+
ret = ecx.expr_call(new_decl_span, bb_call_expr, thin_vec![arg.clone()]);
540577
}
541578
args => {
542579
let ret_tuple: P<ast::Expr> = ecx.expr_tuple(span, args.into());
543-
ret = ecx.expr_call(new_decl_span, blackbox_call_expr, thin_vec![ret_tuple]);
580+
ret = ecx.expr_call(new_decl_span, bb_call_expr, thin_vec![ret_tuple]);
544581
}
545582
}
546583
assert!(has_ret(&d_sig.decl.output));
@@ -553,7 +590,7 @@ mod llvm_enzyme {
553590
ecx: &ExtCtxt<'_>,
554591
span: Span,
555592
primal: Ident,
556-
idents: Vec<Ident>,
593+
idents: &[Ident],
557594
) -> P<ast::Expr> {
558595
let has_self = idents.len() > 0 && idents[0].name == kw::SelfLower;
559596
if has_self {

‎compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 110 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,113 @@ fn get_params(fnc: &Value) -> Vec<&Value> {
2828
}
2929
}
3030

31+
fn match_args_from_caller_to_enzyme<'ll>(
32+
cx: &SimpleCx<'ll>,
33+
args: &mut Vec<&'ll llvm::Value>,
34+
inputs: &[DiffActivity],
35+
outer_args: &[&'ll llvm::Value],
36+
) {
37+
debug!("matching autodiff arguments");
38+
// We now handle the issue that Rust level arguments not always match the llvm-ir level
39+
// arguments. A slice, `&[f32]`, for example, is represented as a pointer and a length on
40+
// llvm-ir level. The number of activities matches the number of Rust level arguments, so we
41+
// need to match those.
42+
// FIXME(ZuseZ4): This logic is a bit more complicated than it should be, can we simplify it
43+
// using iterators and peek()?
44+
let mut outer_pos: usize = 0;
45+
let mut activity_pos = 0;
46+
47+
let enzyme_const = cx.create_metadata("enzyme_const".to_string()).unwrap();
48+
let enzyme_out = cx.create_metadata("enzyme_out".to_string()).unwrap();
49+
let enzyme_dup = cx.create_metadata("enzyme_dup".to_string()).unwrap();
50+
let enzyme_dupnoneed = cx.create_metadata("enzyme_dupnoneed".to_string()).unwrap();
51+
52+
while activity_pos < inputs.len() {
53+
let diff_activity = inputs[activity_pos as usize];
54+
// Duplicated arguments received a shadow argument, into which enzyme will write the
55+
// gradient.
56+
let (activity, duplicated): (&Metadata, bool) = match diff_activity {
57+
DiffActivity::None => panic!("not a valid input activity"),
58+
DiffActivity::Const => (enzyme_const, false),
59+
DiffActivity::Active => (enzyme_out, false),
60+
DiffActivity::ActiveOnly => (enzyme_out, false),
61+
DiffActivity::Dual => (enzyme_dup, true),
62+
DiffActivity::DualOnly => (enzyme_dupnoneed, true),
63+
DiffActivity::Duplicated => (enzyme_dup, true),
64+
DiffActivity::DuplicatedOnly => (enzyme_dupnoneed, true),
65+
DiffActivity::FakeActivitySize => (enzyme_const, false),
66+
};
67+
let outer_arg = outer_args[outer_pos];
68+
args.push(cx.get_metadata_value(activity));
69+
args.push(outer_arg);
70+
if duplicated {
71+
// We know that duplicated args by construction have a following argument,
72+
// so this can not be out of bounds.
73+
let next_outer_arg = outer_args[outer_pos + 1];
74+
let next_outer_ty = cx.val_ty(next_outer_arg);
75+
// FIXME(ZuseZ4): We should add support for Vec here too, but it's less urgent since
76+
// vectors behind references (&Vec<T>) are already supported. Users can not pass a
77+
// Vec by value for reverse mode, so this would only help forward mode autodiff.
78+
let slice = {
79+
if activity_pos + 1 >= inputs.len() {
80+
// If there is no arg following our ptr, it also can't be a slice,
81+
// since that would lead to a ptr, int pair.
82+
false
83+
} else {
84+
let next_activity = inputs[activity_pos + 1];
85+
// We analyze the MIR types and add this dummy activity if we visit a slice.
86+
next_activity == DiffActivity::FakeActivitySize
87+
}
88+
};
89+
if slice {
90+
// A duplicated slice will have the following two outer_fn arguments:
91+
// (..., ptr1, int1, ptr2, int2, ...). We add the following llvm-ir to our __enzyme call:
92+
// (..., metadata! enzyme_dup, ptr, ptr, int1, ...).
93+
// FIXME(ZuseZ4): We will upstream a safety check later which asserts that
94+
// int2 >= int1, which means the shadow vector is large enough to store the gradient.
95+
assert!(unsafe {
96+
llvm::LLVMRustGetTypeKind(next_outer_ty) == llvm::TypeKind::Integer
97+
});
98+
let next_outer_arg2 = outer_args[outer_pos + 2];
99+
let next_outer_ty2 = cx.val_ty(next_outer_arg2);
100+
assert!(unsafe {
101+
llvm::LLVMRustGetTypeKind(next_outer_ty2) == llvm::TypeKind::Pointer
102+
});
103+
let next_outer_arg3 = outer_args[outer_pos + 3];
104+
let next_outer_ty3 = cx.val_ty(next_outer_arg3);
105+
assert!(unsafe {
106+
llvm::LLVMRustGetTypeKind(next_outer_ty3) == llvm::TypeKind::Integer
107+
});
108+
args.push(next_outer_arg2);
109+
args.push(cx.get_metadata_value(enzyme_const));
110+
args.push(next_outer_arg);
111+
outer_pos += 4;
112+
activity_pos += 2;
113+
} else {
114+
// A duplicated pointer will have the following two outer_fn arguments:
115+
// (..., ptr, ptr, ...). We add the following llvm-ir to our __enzyme call:
116+
// (..., metadata! enzyme_dup, ptr, ptr, ...).
117+
if matches!(diff_activity, DiffActivity::Duplicated | DiffActivity::DuplicatedOnly)
118+
{
119+
assert!(
120+
unsafe { llvm::LLVMRustGetTypeKind(next_outer_ty) }
121+
== llvm::TypeKind::Pointer
122+
);
123+
}
124+
// In the case of Dual we don't have assumptions, e.g. f32 would be valid.
125+
args.push(next_outer_arg);
126+
outer_pos += 2;
127+
activity_pos += 1;
128+
}
129+
} else {
130+
// We do not differentiate with resprect to this argument.
131+
// We already added the metadata and argument above, so just increase the counters.
132+
outer_pos += 1;
133+
activity_pos += 1;
134+
}
135+
}
136+
}
137+
31138
/// When differentiating `fn_to_diff`, take a `outer_fn` and generate another
32139
/// function with expected naming and calling conventions[^1] which will be
33140
/// discovered by the enzyme LLVM pass and its body populated with the differentiated
@@ -43,9 +150,6 @@ fn generate_enzyme_call<'ll>(
43150
outer_fn: &'ll Value,
44151
attrs: AutoDiffAttrs,
45152
) {
46-
let inputs = attrs.input_activity;
47-
let output = attrs.ret_activity;
48-
49153
// We have to pick the name depending on whether we want forward or reverse mode autodiff.
50154
let mut ad_name: String = match attrs.mode {
51155
DiffMode::Forward => "__enzyme_fwddiff",
@@ -132,111 +236,13 @@ fn generate_enzyme_call<'ll>(
132236
let mut args = Vec::with_capacity(num_args as usize + 1);
133237
args.push(fn_to_diff);
134238

135-
let enzyme_const = cx.create_metadata("enzyme_const".to_string()).unwrap();
136-
let enzyme_out = cx.create_metadata("enzyme_out".to_string()).unwrap();
137-
let enzyme_dup = cx.create_metadata("enzyme_dup".to_string()).unwrap();
138-
let enzyme_dupnoneed = cx.create_metadata("enzyme_dupnoneed".to_string()).unwrap();
139239
let enzyme_primal_ret = cx.create_metadata("enzyme_primal_return".to_string()).unwrap();
140-
141-
match output {
142-
DiffActivity::Dual => {
143-
args.push(cx.get_metadata_value(enzyme_primal_ret));
144-
}
145-
DiffActivity::Active => {
146-
args.push(cx.get_metadata_value(enzyme_primal_ret));
147-
}
148-
_ => {}
240+
if matches!(attrs.ret_activity, DiffActivity::Dual | DiffActivity::Active) {
241+
args.push(cx.get_metadata_value(enzyme_primal_ret));
149242
}
150243

151-
debug!("matching autodiff arguments");
152-
// We now handle the issue that Rust level arguments not always match the llvm-ir level
153-
// arguments. A slice, `&[f32]`, for example, is represented as a pointer and a length on
154-
// llvm-ir level. The number of activities matches the number of Rust level arguments, so we
155-
// need to match those.
156-
// FIXME(ZuseZ4): This logic is a bit more complicated than it should be, can we simplify it
157-
// using iterators and peek()?
158-
let mut outer_pos: usize = 0;
159-
let mut activity_pos = 0;
160244
let outer_args: Vec<&llvm::Value> = get_params(outer_fn);
161-
while activity_pos < inputs.len() {
162-
let diff_activity = inputs[activity_pos as usize];
163-
// Duplicated arguments received a shadow argument, into which enzyme will write the
164-
// gradient.
165-
let (activity, duplicated): (&Metadata, bool) = match diff_activity {
166-
DiffActivity::None => panic!("not a valid input activity"),
167-
DiffActivity::Const => (enzyme_const, false),
168-
DiffActivity::Active => (enzyme_out, false),
169-
DiffActivity::ActiveOnly => (enzyme_out, false),
170-
DiffActivity::Dual => (enzyme_dup, true),
171-
DiffActivity::DualOnly => (enzyme_dupnoneed, true),
172-
DiffActivity::Duplicated => (enzyme_dup, true),
173-
DiffActivity::DuplicatedOnly => (enzyme_dupnoneed, true),
174-
DiffActivity::FakeActivitySize => (enzyme_const, false),
175-
};
176-
let outer_arg = outer_args[outer_pos];
177-
args.push(cx.get_metadata_value(activity));
178-
args.push(outer_arg);
179-
if duplicated {
180-
// We know that duplicated args by construction have a following argument,
181-
// so this can not be out of bounds.
182-
let next_outer_arg = outer_args[outer_pos + 1];
183-
let next_outer_ty = cx.val_ty(next_outer_arg);
184-
// FIXME(ZuseZ4): We should add support for Vec here too, but it's less urgent since
185-
// vectors behind references (&Vec<T>) are already supported. Users can not pass a
186-
// Vec by value for reverse mode, so this would only help forward mode autodiff.
187-
let slice = {
188-
if activity_pos + 1 >= inputs.len() {
189-
// If there is no arg following our ptr, it also can't be a slice,
190-
// since that would lead to a ptr, int pair.
191-
false
192-
} else {
193-
let next_activity = inputs[activity_pos + 1];
194-
// We analyze the MIR types and add this dummy activity if we visit a slice.
195-
next_activity == DiffActivity::FakeActivitySize
196-
}
197-
};
198-
if slice {
199-
// A duplicated slice will have the following two outer_fn arguments:
200-
// (..., ptr1, int1, ptr2, int2, ...). We add the following llvm-ir to our __enzyme call:
201-
// (..., metadata! enzyme_dup, ptr, ptr, int1, ...).
202-
// FIXME(ZuseZ4): We will upstream a safety check later which asserts that
203-
// int2 >= int1, which means the shadow vector is large enough to store the gradient.
204-
assert!(llvm::LLVMRustGetTypeKind(next_outer_ty) == llvm::TypeKind::Integer);
205-
let next_outer_arg2 = outer_args[outer_pos + 2];
206-
let next_outer_ty2 = cx.val_ty(next_outer_arg2);
207-
assert!(llvm::LLVMRustGetTypeKind(next_outer_ty2) == llvm::TypeKind::Pointer);
208-
let next_outer_arg3 = outer_args[outer_pos + 3];
209-
let next_outer_ty3 = cx.val_ty(next_outer_arg3);
210-
assert!(llvm::LLVMRustGetTypeKind(next_outer_ty3) == llvm::TypeKind::Integer);
211-
args.push(next_outer_arg2);
212-
args.push(cx.get_metadata_value(enzyme_const));
213-
args.push(next_outer_arg);
214-
outer_pos += 4;
215-
activity_pos += 2;
216-
} else {
217-
// A duplicated pointer will have the following two outer_fn arguments:
218-
// (..., ptr, ptr, ...). We add the following llvm-ir to our __enzyme call:
219-
// (..., metadata! enzyme_dup, ptr, ptr, ...).
220-
if matches!(
221-
diff_activity,
222-
DiffActivity::Duplicated | DiffActivity::DuplicatedOnly
223-
) {
224-
assert!(
225-
llvm::LLVMRustGetTypeKind(next_outer_ty) == llvm::TypeKind::Pointer
226-
);
227-
}
228-
// In the case of Dual we don't have assumptions, e.g. f32 would be valid.
229-
args.push(next_outer_arg);
230-
outer_pos += 2;
231-
activity_pos += 1;
232-
}
233-
} else {
234-
// We do not differentiate with resprect to this argument.
235-
// We already added the metadata and argument above, so just increase the counters.
236-
outer_pos += 1;
237-
activity_pos += 1;
238-
}
239-
}
245+
match_args_from_caller_to_enzyme(&cx, &mut args, &attrs.input_activity, &outer_args);
240246

241247
let call = builder.call(enzyme_ty, ad_fn, &args, None);
242248

‎compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -790,16 +790,10 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
790790

791791
// check for exactly one autodiff attribute on placeholder functions.
792792
// There should only be one, since we generate a new placeholder per ad macro.
793-
// FIXME(ZuseZ4): re-enable this check. Currently we add multiple, which doesn't cause harm but
794-
// looks strange e.g. under cargo-expand.
795793
let attr = match &attrs[..] {
796794
[] => return None,
797795
[attr] => attr,
798-
// These two attributes are the same and unfortunately duplicated due to a previous bug.
799-
[attr, _attr2] => attr,
800796
_ => {
801-
//FIXME(ZuseZ4): Once we fixed our parser, we should also prohibit the two-attribute
802-
//branch above.
803797
span_bug!(attrs[1].span(), "cg_ssa: rustc_autodiff should only exist once per source");
804798
}
805799
};

‎tests/pretty/autodiff_forward.pp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
pub fn f3(x: &[f64], y: f64) -> f64 {
5454
::core::panicking::panic("not implemented")
5555
}
56-
#[rustc_autodiff(ForwardFirst, Dual, Const, Const,)]
56+
#[rustc_autodiff(Forward, Dual, Const, Const,)]
5757
#[inline(never)]
5858
pub fn df3(x: &[f64], bx: &[f64], y: f64) -> f64 {
5959
unsafe { asm!("NOP", options(pure, nomem)); };
@@ -73,10 +73,6 @@
7373
}
7474
#[rustc_autodiff]
7575
#[inline(never)]
76-
#[rustc_autodiff]
77-
#[inline(never)]
78-
#[rustc_autodiff]
79-
#[inline(never)]
8076
pub fn f5(x: &[f64], y: f64) -> f64 {
8177
::core::panicking::panic("not implemented")
8278
}

‎tests/pretty/autodiff_forward.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn f2(x: &[f64], y: f64) -> f64 {
1919
unimplemented!()
2020
}
2121

22-
#[autodiff(df3, ForwardFirst, Dual, Const, Const)]
22+
#[autodiff(df3, Forward, Dual, Const, Const)]
2323
pub fn f3(x: &[f64], y: f64) -> f64 {
2424
unimplemented!()
2525
}

‎tests/pretty/autodiff_reverse.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
pub fn f3(x: &[f64], y: f64) -> f64 {
5252
::core::panicking::panic("not implemented")
5353
}
54-
#[rustc_autodiff(ReverseFirst, Duplicated, Const, Active,)]
54+
#[rustc_autodiff(Reverse, Duplicated, Const, Active,)]
5555
#[inline(never)]
5656
pub fn df3(x: &[f64], dx: &mut [f64], y: f64, dret: f64) -> f64 {
5757
unsafe { asm!("NOP", options(pure, nomem)); };

‎tests/pretty/autodiff_reverse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn f1(x: &[f64], y: f64) -> f64 {
1818
#[autodiff(df2, Reverse)]
1919
pub fn f2() {}
2020

21-
#[autodiff(df3, ReverseFirst, Duplicated, Const, Active)]
21+
#[autodiff(df3, Reverse, Duplicated, Const, Active)]
2222
pub fn f3(x: &[f64], y: f64) -> f64 {
2323
unimplemented!()
2424
}

0 commit comments

Comments
 (0)
Please sign in to comment.