Skip to content

[WIP] TypeTree support in autodiff #143490

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
12 changes: 11 additions & 1 deletion compiler/rustc_ast/src/expand/autodiff_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::str::FromStr;
use crate::expand::{Decodable, Encodable, HashStable_Generic};
use crate::ptr::P;
use crate::{Ty, TyKind};
use crate::expand::typetree::TypeTree;

/// Forward and Reverse Mode are well known names for automatic differentiation implementations.
/// Enzyme does support both, but with different semantics, see DiffActivity. The First variants
Expand Down Expand Up @@ -85,6 +86,9 @@ pub struct AutoDiffItem {
/// The name of the function being generated
pub target: String,
pub attrs: AutoDiffAttrs,
// --- TypeTree support ---
pub inputs: Vec<TypeTree>,
pub output: TypeTree,
}

#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
Expand Down Expand Up @@ -112,6 +116,10 @@ impl AutoDiffAttrs {
pub fn has_primal_ret(&self) -> bool {
matches!(self.ret_activity, DiffActivity::Active | DiffActivity::Dual)
}
/// New constructor for type tree support
pub fn into_item(self, source: String, target: String, inputs: Vec<TypeTree>, output: TypeTree) -> AutoDiffItem {
AutoDiffItem { source, target, attrs: self, inputs, output }
}
}

impl DiffMode {
Expand Down Expand Up @@ -284,6 +292,8 @@ impl AutoDiffAttrs {
impl fmt::Display for AutoDiffItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Differentiating {} -> {}", self.source, self.target)?;
write!(f, " with attributes: {:?}", self.attrs)
write!(f, " with attributes: {:?}", self.attrs)?;
write!(f, " with inputs: {:?}", self.inputs)?;
write!(f, " with output: {:?}", self.output)
}
}
15 changes: 13 additions & 2 deletions compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ mod llvm_enzyme {
AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ret_activity,
valid_ty_for_activity,
};
use rustc_ast::expand::typetree::{TypeTree, Type, Kind};
use rustc_ast::ptr::P;
use crate::typetree::construct_typetree_from_fnsig;
use rustc_ast::token::{Lit, LitKind, Token, TokenKind};
use rustc_ast::tokenstream::*;
use rustc_ast::visit::AssocCtxt::*;
Expand Down Expand Up @@ -324,6 +326,17 @@ mod llvm_enzyme {
}
let span = ecx.with_def_site_ctxt(expand_span);

// Construct real type trees from function signature
let (inputs, output) = construct_typetree_from_fnsig(&sig);

// Use the new into_item method to construct the AutoDiffItem
let autodiff_item = x.clone().into_item(
primal.to_string(),
first_ident(&meta_item_vec[0]).to_string(),
inputs,
output,
);

let n_active: u32 = x
.input_activity
.iter()
Expand Down Expand Up @@ -1045,5 +1058,3 @@ mod llvm_enzyme {
(d_sig, new_inputs, idents, false)
}
}

pub(crate) use llvm_enzyme::{expand_forward, expand_reverse};
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod pattern_type;
mod source_util;
mod test;
mod trace_macros;
mod typetree;

pub mod asm;
pub mod cmdline_attrs;
Expand Down
Loading
Loading