Skip to content

Fix ICE when using autodiff with other macros - #165391

Open
bb1yd wants to merge 1 commit into
rust-lang:mainfrom
bb1yd:fix-autodiff-ice
Open

bb1yd wants to merge 1 commit into
rust-lang:mainfrom
bb1yd:fix-autodiff-ice

Conversation

@bb1yd

@bb1yd bb1yd commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #152889

outer_normal_attr didn't add token stream to the attribute. This will cause an ICE when rustc tries to convert the ast node into token stream and send to a macro (e.g proc macro or attribute macro rules).

LLM disclosure: I locate the cause of this issue by myself. I only ask the LLM what is the common/correct way to create an attribute in a builtin macro, and it suggest attr_word.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 3, 2026
@bb1yd
bb1yd marked this pull request as ready for review November 3, 2026 11:30
@rustbot

rustbot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_builtin_macros/src/autodiff.rs

cc @ZuseZ4

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 3, 2026
@rustbot

rustbot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 23 candidates

@mejrs mejrs left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This might fix the ICE, but the way this macro forwards attributes is fundamentally broken, it should be properly fixed.

The problem is that the macro generates smth like

#[other_attr]
#[rustc_autodif]
fn foo(){}

and if other_attr is a macro, it gets to observe the implementation details of the autodiff macros. It also exposes errors such as the one encountered here, from when we tried putting #[rustc_test_entrypoint_marker] in the expansion of the test macro.

Instead, there should be an allowlist for attributes, and the original example should emit an error. See

for attr in attrs {
for how the eii macro does it.
From a glance it looks like currently only the inline attribute is forwarded.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 3, 2026
@rustbot

rustbot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@@ -363,7 +360,7 @@ mod llvm_enzyme {
(ast::AttrKind::Normal(a), ast::AttrKind::Normal(b)) => {
let a = &a.item.path;

@bb1yd bb1yd Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I found another bug while changing the code😅, the current code of same_attribute only checks whether the paths are the same not including the argument. This means we're adding #[inline(never)] to the generated function if there's an inline attribute on the original function, even when it's #[inline(always)] or #[inline].

#![feature(prelude_import)]
#![no_std]
#![feature(macro_attr)]
#![feature(autodiff)]
extern crate std;
#[prelude_import]
use ::std::prelude::rust_2015::*;

macro_rules! forward_referenced_attr { attr() {} => {} }

use std::autodiff::autodiff_reverse;

#[inline(always)]
#[rustc_autodiff]
fn foo() {}
#[rustc_autodiff(Reverse, 1, None)]
#[inline(never)]
fn Active() { ::core::intrinsics::autodiff(foo as fn(), Active, ()) }

fn main() {}

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will also fix this in this PR.

@rustbot

rustbot commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@bb1yd

This comment was marked as resolved.

@rust-log-analyzer

This comment has been minimized.

@mejrs mejrs left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm I hadn't thought about that people apparently like to apply this macro multiple times...

View changes since this review

if is_builtin_attr_name(name)
|| name == sym::autodiff_forward
|| name == sym::autodiff_reverse =>
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This checks that the macro is named autodiff_forward or autodiff_reverse, not that it is.
item.name() is also None if it used like #[std::autodiff::autodiff_reverse]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unfortunately there is no way to tell that it really is that macro.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I vaguely remember that someone (@oli-obk ?) was working on adding name or type resolution to some macros. It sounds quite tricky to get such a prototype merged with all the obvious challenges. But if there's a chance, it might be worth adding a FIXME?

Other than that, a user written macro with name should be rejected, see the visibility.rs ui test.
There is one ui test using #[std::autodiff::autodiff_reverse], but I don't see too many users writing it.

@mejrs

mejrs commented Nov 8, 2026

Copy link
Copy Markdown
Member

@ZuseZ4 Can you explain the purpose of #[rustc_autodiff] (without arguments)? It appears unused, I think it is best to just delete its use.

It's only retrieved once, like

    let Some(Some(mut diff_attrs)) =
        find_attr!(tcx, fn_diff.def_id(), RustcAutodiff(attr) => attr.clone())
    else {
        bug!("could not find autodiff attrs")
    };

so it does appear like AttributeKind::RustcAutodiff(None) is never read.

@ZuseZ4

ZuseZ4 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

@mejrs thanks for helping with the review.
autodiff doesn't rewrite the original function to which it is applied. It instead creates a new function with a placeholder/dummy body. The dummy body will then be replaced with the correct gradient code, derived from the original (unchanged function).

The rustc_autodiff attribute was originally there so we could collect the source functions, which we'd then later differentiate on llvm-ir level. E.g. to make sure they don't get dead-code-eliminated before we process them. We refactored this a few times, so maybe we could get rid of it by now.

@Sa4dUs do you know if we still need rustc_autodiff (with no args)?

@rust-bors

rust-bors Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

[ICE]: attribute is missing tokens: Attribute ...

6 participants