Skip to content

Implement autodiff using intrinsics #142640

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 3 commits into
base: master
Choose a base branch
from
Draft

Conversation

Sa4dUs
Copy link
Contributor

@Sa4dUs Sa4dUs commented Jun 17, 2025

This PR aims to handle derivatives generated by autodiff as compiler intrinsics, using the #[rustc_intrinsic] attr, so we can get rid of a lot of our frontend code, and in the backend, just lower it into a declaration.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 17, 2025
Comment on lines +200 to +217
_ if tcx.has_attr(def_id, sym::rustc_autodiff) => {
// NOTE(Sa4dUs): This is a hacky way to get the autodiff items
// so we can focus on the lowering of the intrinsic call

// `diff_items` is empty even when autodiff is enabled, and if we're here,
// it's because some function was marked as intrinsic and had the `rustc_autodiff` attr
let diff_items = tcx.collect_and_partition_mono_items(()).autodiff_items;

// this shouldn't happen?
if diff_items.is_empty() {
bug!("no autodiff items found for {def_id:?}");
}

// TODO(Sa4dUs): generate the enzyme call itself, based on the logic in `builder.rs`

// Just gen the fallback body for now
return Err(ty::Instance::new_raw(def_id, instance.args));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is the part you got stuck on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda. I tried to use the collect_and_partition_mono_items just as a hacky way of getting autodiff_items, so i can focus on the declaration. Once that was working, I would focus on how to get that information in the best way possible.

Copy link
Contributor Author

@Sa4dUs Sa4dUs Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for context, during yesterday's meeting, we came to the conclusion that, for the time being, it's acceptable to copy and paste the collector logic from within the provider's code, just to focus on the declaration, but still nice to have feedback since, in some moment, we'll need to get to AutoDiffItems in a decent way. collect_and_partition_mono_items(()).autodiff_items being [] in autodiff code seems weird, so it would be nice to know why it's happening, or if it is some kind of bug.

@workingjubilee
Copy link
Member

@Sa4dUs Can you provide the motivation in the PR desc for context?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants