1
Fork 0

Rollup merge of #138314 - haenoe:autodiff-inner-function, r=ZuseZ4

fix usage of `autodiff` macro with inner functions

This PR adds additional handling into the expansion step of the `std::autodiff` macro (in `compiler/rustc_builtin_macros/src/autodiff.rs`), which allows the macro to be applied to inner functions.

```rust
#![feature(autodiff)]
use std::autodiff::autodiff;

fn main() {
    #[autodiff(d_inner, Forward, Dual, DualOnly)]
    fn inner(x: f32) -> f32 {
        x * x
    }
}
```

Previously, the compiler didn't allow this due to only handling `Annotatable::Item` and `Annotatable::AssocItem` and missing the handling of `Annotatable::Stmt`. This resulted in the rather generic error

```
error: autodiff must be applied to function
 --> src/main.rs:6:5
  |
6 | /     fn inner(x: f32) -> f32 {
7 | |         x * x
8 | |     }
  | |_____^

error: could not compile `enzyme-test` (bin "enzyme-test") due to 1 previous error
```

This issue was originally reported [here](https://github.com/EnzymeAD/rust/issues/184).

Quick question: would it make sense to add a ui test to ensure there is no regression on this?
This is my first contribution, so I'm extra grateful for any piece of feedback!! :D

r? `@oli-obk`

Tracking issue for autodiff: #124509
This commit is contained in:
Jakub Beránek 2025-04-07 08:23:34 +02:00 committed by GitHub
commit 5a43d92382
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 109 additions and 48 deletions

View file

@ -17,7 +17,7 @@ mod llvm_enzyme {
use rustc_ast::visit::AssocCtxt::*; use rustc_ast::visit::AssocCtxt::*;
use rustc_ast::{ use rustc_ast::{
self as ast, AssocItemKind, BindingMode, ExprKind, FnRetTy, FnSig, Generics, ItemKind, self as ast, AssocItemKind, BindingMode, ExprKind, FnRetTy, FnSig, Generics, ItemKind,
MetaItemInner, PatKind, QSelf, TyKind, MetaItemInner, PatKind, QSelf, TyKind, Visibility,
}; };
use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::{Ident, Span, Symbol, kw, sym}; use rustc_span::{Ident, Span, Symbol, kw, sym};
@ -72,6 +72,16 @@ mod llvm_enzyme {
} }
} }
// Get information about the function the macro is applied to
fn extract_item_info(iitem: &P<ast::Item>) -> Option<(Visibility, FnSig, Ident)> {
match &iitem.kind {
ItemKind::Fn(box ast::Fn { sig, ident, .. }) => {
Some((iitem.vis.clone(), sig.clone(), ident.clone()))
}
_ => None,
}
}
pub(crate) fn from_ast( pub(crate) fn from_ast(
ecx: &mut ExtCtxt<'_>, ecx: &mut ExtCtxt<'_>,
meta_item: &ThinVec<MetaItemInner>, meta_item: &ThinVec<MetaItemInner>,
@ -199,32 +209,26 @@ mod llvm_enzyme {
return vec![item]; return vec![item];
} }
let dcx = ecx.sess.dcx(); let dcx = ecx.sess.dcx();
// first get the annotable item:
let (primal, sig, is_impl): (Ident, FnSig, bool) = match &item { // first get information about the annotable item:
Annotatable::Item(iitem) => { let Some((vis, sig, primal)) = (match &item {
let (ident, sig) = match &iitem.kind { Annotatable::Item(iitem) => extract_item_info(iitem),
ItemKind::Fn(box ast::Fn { ident, sig, .. }) => (ident, sig), Annotatable::Stmt(stmt) => match &stmt.kind {
_ => { ast::StmtKind::Item(iitem) => extract_item_info(iitem),
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() }); _ => None,
return vec![item]; },
}
};
(*ident, sig.clone(), false)
}
Annotatable::AssocItem(assoc_item, Impl { of_trait: false }) => { Annotatable::AssocItem(assoc_item, Impl { of_trait: false }) => {
let (ident, sig) = match &assoc_item.kind { match &assoc_item.kind {
ast::AssocItemKind::Fn(box ast::Fn { ident, sig, .. }) => (ident, sig), ast::AssocItemKind::Fn(box ast::Fn { sig, ident, .. }) => {
_ => { Some((assoc_item.vis.clone(), sig.clone(), ident.clone()))
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
return vec![item];
} }
}; _ => None,
(*ident, sig.clone(), true) }
}
_ => {
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
return vec![item];
} }
_ => None,
}) else {
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
return vec![item];
}; };
let meta_item_vec: ThinVec<MetaItemInner> = match meta_item.kind { let meta_item_vec: ThinVec<MetaItemInner> = match meta_item.kind {
@ -238,15 +242,6 @@ mod llvm_enzyme {
let has_ret = has_ret(&sig.decl.output); let has_ret = has_ret(&sig.decl.output);
let sig_span = ecx.with_call_site_ctxt(sig.span); let sig_span = ecx.with_call_site_ctxt(sig.span);
let vis = match &item {
Annotatable::Item(iitem) => iitem.vis.clone(),
Annotatable::AssocItem(assoc_item, _) => assoc_item.vis.clone(),
_ => {
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
return vec![item];
}
};
// create TokenStream from vec elemtents: // create TokenStream from vec elemtents:
// meta_item doesn't have a .tokens field // meta_item doesn't have a .tokens field
let mut ts: Vec<TokenTree> = vec![]; let mut ts: Vec<TokenTree> = vec![];
@ -379,6 +374,22 @@ mod llvm_enzyme {
} }
Annotatable::AssocItem(assoc_item.clone(), i) Annotatable::AssocItem(assoc_item.clone(), i)
} }
Annotatable::Stmt(ref mut stmt) => {
match stmt.kind {
ast::StmtKind::Item(ref mut iitem) => {
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &attr.kind)) {
iitem.attrs.push(attr);
}
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &inline_never.kind))
{
iitem.attrs.push(inline_never.clone());
}
}
_ => unreachable!("stmt kind checked previously"),
};
Annotatable::Stmt(stmt.clone())
}
_ => { _ => {
unreachable!("annotatable kind checked previously") unreachable!("annotatable kind checked previously")
} }
@ -389,22 +400,40 @@ mod llvm_enzyme {
delim: rustc_ast::token::Delimiter::Parenthesis, delim: rustc_ast::token::Delimiter::Parenthesis,
tokens: ts, tokens: ts,
}); });
let d_attr = outer_normal_attr(&rustc_ad_attr, new_id, span); let d_attr = outer_normal_attr(&rustc_ad_attr, new_id, span);
let d_annotatable = if is_impl { let d_annotatable = match &item {
let assoc_item: AssocItemKind = ast::AssocItemKind::Fn(asdf); Annotatable::AssocItem(_, _) => {
let d_fn = P(ast::AssocItem { let assoc_item: AssocItemKind = ast::AssocItemKind::Fn(asdf);
attrs: thin_vec![d_attr, inline_never], let d_fn = P(ast::AssocItem {
id: ast::DUMMY_NODE_ID, attrs: thin_vec![d_attr, inline_never],
span, id: ast::DUMMY_NODE_ID,
vis, span,
kind: assoc_item, vis,
tokens: None, kind: assoc_item,
}); tokens: None,
Annotatable::AssocItem(d_fn, Impl { of_trait: false }) });
} else { Annotatable::AssocItem(d_fn, Impl { of_trait: false })
let mut d_fn = ecx.item(span, thin_vec![d_attr, inline_never], ItemKind::Fn(asdf)); }
d_fn.vis = vis; Annotatable::Item(_) => {
Annotatable::Item(d_fn) let mut d_fn = ecx.item(span, thin_vec![d_attr, inline_never], ItemKind::Fn(asdf));
d_fn.vis = vis;
Annotatable::Item(d_fn)
}
Annotatable::Stmt(_) => {
let mut d_fn = ecx.item(span, thin_vec![d_attr, inline_never], ItemKind::Fn(asdf));
d_fn.vis = vis;
Annotatable::Stmt(P(ast::Stmt {
id: ast::DUMMY_NODE_ID,
kind: ast::StmtKind::Item(d_fn),
span,
}))
}
_ => {
unreachable!("item kind checked previously")
}
}; };
return vec![orig_annotatable, d_annotatable]; return vec![orig_annotatable, d_annotatable];

View file

@ -29,6 +29,8 @@ pub fn f1(x: &[f64], y: f64) -> f64 {
// Make sure, that we add the None for the default return. // Make sure, that we add the None for the default return.
// We want to make sure that we can use the macro for functions defined inside of functions
::core::panicking::panic("not implemented") ::core::panicking::panic("not implemented")
} }
#[rustc_autodiff(Forward, 1, Dual, Const, Dual)] #[rustc_autodiff(Forward, 1, Dual, Const, Dual)]
@ -158,4 +160,25 @@ fn f8_1(x: &f32, bx_0: &f32) -> f32 {
::core::hint::black_box((bx_0,)); ::core::hint::black_box((bx_0,));
::core::hint::black_box(<f32>::default()) ::core::hint::black_box(<f32>::default())
} }
pub fn f9() {
#[rustc_autodiff]
#[inline(never)]
fn inner(x: f32) -> f32 { x * x }
#[rustc_autodiff(Forward, 1, Dual, Dual)]
#[inline(never)]
fn d_inner_2(x: f32, bx_0: f32) -> (f32, f32) {
unsafe { asm!("NOP", options(pure, nomem)); };
::core::hint::black_box(inner(x));
::core::hint::black_box((bx_0,));
::core::hint::black_box(<(f32, f32)>::default())
}
#[rustc_autodiff(Forward, 1, Dual, DualOnly)]
#[inline(never)]
fn d_inner_1(x: f32, bx_0: f32) -> f32 {
unsafe { asm!("NOP", options(pure, nomem)); };
::core::hint::black_box(inner(x));
::core::hint::black_box((bx_0,));
::core::hint::black_box(<f32>::default())
}
}
fn main() {} fn main() {}

View file

@ -54,4 +54,13 @@ fn f8(x: &f32) -> f32 {
unimplemented!() unimplemented!()
} }
// We want to make sure that we can use the macro for functions defined inside of functions
pub fn f9() {
#[autodiff(d_inner_1, Forward, Dual, DualOnly)]
#[autodiff(d_inner_2, Forward, Dual, Dual)]
fn inner(x: f32) -> f32 {
x * x
}
}
fn main() {} fn main() {}