Skip to content

Split autodiff into autodiff_forward and autodiff_reverse #140697

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

Conversation

Sa4dUs
Copy link
Contributor

@Sa4dUs Sa4dUs commented May 6, 2025

This PR splits #[autodiff] macro so #[autodiff(df, Reverse, args)] would become #[autodiff_reverse(df, args)] and #[autodiff(df, Forward, args)] would become #[autodiff_forwad(df, args)].

Pending tasks:

  • Fix cannot find a built-in macro with name error.
  • Update the tests to reflect the new macro structure.
  • Review the implementation for better code.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 6, 2025
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 self-assigned this May 6, 2025
@ZuseZ4 ZuseZ4 added the F-autodiff `#![feature(autodiff)]` label May 6, 2025
@ZuseZ4
Copy link
Member

ZuseZ4 commented May 6, 2025

I'd almost bet it has to do with bootstrapping, see e.g. https://github.com/rust-lang/rust/pull/129458/files#diff-ea30f57e78f3b861b784ae315b3ac31358b7cea2aa14a668084ad412de12e586
It will be removed by other people later after landing, that's probably why you didn't see it anymore when just looking at the current code for it. Ping me if that doesn't solve the issue, then I can try to play around with it locally.

@jieyouxu
Copy link
Member

jieyouxu commented May 7, 2025

I don't remember exactly, can built-in attributes be namespaced now? E.g. #[autodiff::forward()] and #[autodiff::reverse()]. That way, only one "top-level" name would cause macro ambiguities from the built-in attribute (autodiff), instead of two.

@ZuseZ4
Copy link
Member

ZuseZ4 commented May 7, 2025

I think :: looks much prettier than _. Our attribute is called rustc_autodiff and not autodiff, so I don't think there will be ambiguities either way.
@jdonszelmann is probably the right person to ask about parsing. (see the question above)

@jieyouxu
Copy link
Member

jieyouxu commented May 7, 2025

Right sorry, I keep forgetting there's two

Pending fix.
```
error: cannot find a built-in macro with name `autodiff_forward`
    --> library\core\src\macros\mod.rs:1542:5
     |
1542 | /     pub macro autodiff_forward($item:item) {
1543 | |         /* compiler built-in */
1544 | |     }
     | |_____^

error: cannot find a built-in macro with name `autodiff_reverse`
    --> library\core\src\macros\mod.rs:1549:5
     |
1549 | /     pub macro autodiff_reverse($item:item) {
1550 | |         /* compiler built-in */
1551 | |     }
     | |_____^

error: could not compile `core` (lib) due to 2 previous errors
```
@Sa4dUs Sa4dUs force-pushed the split-autodiff branch from 34f259b to c47f0e3 Compare May 8, 2025 20:04
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Sa4dUs added 3 commits May 10, 2025 00:58
UI tests are pending, will depend on error messages change.
…des (`Forward`, `Reverse`)

Since the mode is no longer part of `meta_item`, we must insert it manually (otherwise macro expansion with `#[rustc_autodiff]` won't work).

This can be revised later if a more structured representation becomes necessary (using enums, annotated structs, etc).

Some tests are currently failing. I'll address them next.
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4
Copy link
Member

ZuseZ4 commented May 10, 2025

Try build/x86_64-unknown-linux-gnu/llvm/bin/clang-format compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp -i (Adjusted for all the cpp files you touched).

@Sa4dUs
Copy link
Contributor Author

Sa4dUs commented May 10, 2025

I'd say the tidy error it's because compiler/rustc_builtin_macros/messages.ftl has the message builtin_macros_autodiff_mode, but it's not being used. I still need to fix a bug when parsing the arguments (I'm assuming it's a silly mistake with an index or array boundary), and once everything works properly, I'll take care of errors and such, which I assume will need to be adjusted somehow for the new macros.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-19 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#18 exporting to docker image format
#18 sending tarball 20.6s done
#18 DONE 28.6s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-19]
[CI_JOB_NAME=x86_64-gnu-llvm-19]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Listening on address 127.0.0.1:4226
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-19', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'build.print-step-timings', '--enable-verbose-tests', '--set', 'build.metrics', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--set', 'gcc.download-ci-gcc=true', '--enable-new-symbol-mangling']
configure: build.build          := x86_64-unknown-linux-gnu
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-19/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
3    |
4 LL | use std::autodiff::autodiff_forward;
5    |     ------------------------------- previous import of the macro `autodiff_forward` here
- ...
+ LL | extern crate my_macro;
7 LL | use my_macro::autodiff_forward; // bring `autodiff_forward` in scope
8    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ `autodiff_forward` reimported here
9    |

13 LL | use my_macro::autodiff_forward as other_autodiff_forward; // bring `autodiff_forward` in scope
14    |                                +++++++++++++++++++++++++
15 
- error: this rustc version does not support autodiff_forward
+ error: this rustc version does not support autodiff
17   --> $DIR/visibility.rs:13:1
---
To only update this specific test, also pass `--test-args autodiff/visibility.rs`

error in revision `std_autodiff`: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/autodiff/visibility.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--cfg" "std_autodiff" "--check-cfg" "cfg(test,FALSE,std_autodiff,no_std_autodiff)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/autodiff/visibility.std_autodiff" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/autodiff/visibility.std_autodiff/auxiliary" "--extern" "my_macro=/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/autodiff/visibility.std_autodiff/auxiliary/libmy_macro.so"
stdout: none
--- stderr -------------------------------
error[E0252]: the name `autodiff_forward` is defined multiple times
##[error]  --> /checkout/tests/ui/autodiff/visibility.rs:11:5
   |
LL | use std::autodiff::autodiff_forward;
   |     ------------------------------- previous import of the macro `autodiff_forward` here
LL | extern crate my_macro;
LL | use my_macro::autodiff_forward; // bring `autodiff_forward` in scope
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ `autodiff_forward` reimported here
   |
   = note: `autodiff_forward` must be defined only once in the macro namespace of this module
help: you can use `as` to change the binding name of the import
   |
LL | use my_macro::autodiff_forward as other_autodiff_forward; // bring `autodiff_forward` in scope
   |                                +++++++++++++++++++++++++

error: this rustc version does not support autodiff
##[error]  --> /checkout/tests/ui/autodiff/visibility.rs:13:1
   |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) 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. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants