Skip to content
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

fix usage of autodiff macro with inner functions #138314

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

haenoe
Copy link

@haenoe haenoe commented Mar 10, 2025

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.

#![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.

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 10, 2025
@jieyouxu jieyouxu added the F-autodiff `#![feature(autodiff)]` label Mar 10, 2025
@ZuseZ4
Copy link
Contributor

ZuseZ4 commented Mar 11, 2025

Thank you for looking into this! Yes, adding a test to avoid regressions would be great.
UI tests are usually more towards stdout/stderr (or runtime) testing. Since this one now works, I would add it under tests/pretty/autodiff_reverse.{rs|pp}. You can just add an example by renaming main to f6, and use --bless when running the test.
With git diff you can then check if the change is what you expect it to be.
The whole test suite just got reworked recently, so it has quite nice docs these days: https://rustc-dev-guide.rust-lang.org/tests/compiletest.html
https://rustc-dev-guide.rust-lang.org/tests/intro.html
and https://rustc-dev-guide.rust-lang.org/tests/best-practices.html are probably the relevant ones.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 11, 2025

@bors delegate=ZuseZ4

@bors
Copy link
Contributor

bors commented Mar 11, 2025

✌️ @ZuseZ4, you can now approve this pull request!

If @oli-obk told you to "r=me" after making some further change, please make that change, then do @bors r=@oli-obk

@haenoe
Copy link
Author

haenoe commented Mar 15, 2025

Thank you for looking into this! Yes, adding a test to avoid regressions would be great. UI tests are usually more towards stdout/stderr (or runtime) testing. Since this one now works, I would add it under tests/pretty/autodiff_reverse.{rs|pp}. You can just add an example by renaming main to f6, and use --bless when running the test. With git diff you can then check if the change is what you expect it to be. The whole test suite just got reworked recently, so it has quite nice docs these days: https://rustc-dev-guide.rust-lang.org/tests/compiletest.html https://rustc-dev-guide.rust-lang.org/tests/intro.html and https://rustc-dev-guide.rust-lang.org/tests/best-practices.html are probably the relevant ones.

@ZuseZ4 , thank you for providing more insights into this! The pretty printer tests do indeed sound like a good place for this. Also I've got to express how cool and insightful the docs are -- this is really not something to be taken as granted :D

However, while looking into this I noticed that both tests/pretty/autodiff_{forward,reverse}.rs are currently failing due to the fact that they still include functions which use the autodiff macro in conjuction with DiffMode::{Forward,Reverse}First. These two were removed with commit 061abbc36928cce784c54463c266f4d43d14d419.

It makes sense to open a separate PR fixing these issues before continuing with the inner function test, right?
Have a nice evening ^^'

@ZuseZ4
Copy link
Contributor

ZuseZ4 commented Mar 15, 2025

Yes I agree, I think one reason why I ended up being a rustc developer rather than working on other projects is the documentation. I don't have a pc this weekend, but my batching PR contains fixes for those. You can cherry pick them if you want, or I'll make a PR for that on Monday. I had hoped to get it done for the weekend, but unfortunately ran out of time.

@haenoe
Copy link
Author

haenoe commented Mar 15, 2025

Okidoki! Don't stress, I'll just rebase when the changes are in master.
Enjoy your weekend!

@ZuseZ4 ZuseZ4 mentioned this pull request Mar 17, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 21, 2025
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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 21, 2025
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
Kobzol added a commit to Kobzol/rust that referenced this pull request Mar 21, 2025
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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 21, 2025
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
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 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
@bors
Copy link
Contributor

bors commented Mar 21, 2025

☔ The latest upstream changes (presumably #138791) made this pull request unmergeable. Please resolve the merge conflicts.

@haenoe haenoe force-pushed the autodiff-inner-function branch from 5024ae1 to 35812e2 Compare March 21, 2025 23:31
@haenoe
Copy link
Author

haenoe commented Mar 23, 2025

Hey @ZuseZ4
Rebased this to master and also tried to integrate your fixes regarding the duplicated attributes via your same_attribute function.
Quick question: Do you think this simple test suffices to test the expansion logic? Or would you add one/two more advanced usages (e.g. multiple derivatives).

@ZuseZ4
Copy link
Contributor

ZuseZ4 commented Mar 24, 2025

looks good, but I will check again later when I'm on my computer. For the test I would just add a second autodiff macro with an in any way different config to the same function/test, to verify that even after future refactors we don't add inline or autodiff attributes multiple time to the source functions.
Also, we usually use bug! instead of unreachable if we have previously tested assumptions, can you move them over?

@bors
Copy link
Contributor

bors commented Mar 25, 2025

☔ The latest upstream changes (presumably #138933) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-autodiff `#![feature(autodiff)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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.

None yet

6 participants