Prevent ICE in autodiff return activity validation
Previously, the compiler did not validate the OUTPUT_ACTIVITY parameters during the macro expansion phase. As a result, invalid configurations would propagate to later stages, causing ICEs such as:
1
2
3
error: internal compiler error:
compiler/rustc_codegen_ssa/src/codegen_attrs.rs:935:13:
Invalid input activity Dual for Reverse mode
This error originated in the code generation phase (compiler/rustc_codegen_ssa/src/codegen_attrs.rs
), making it difficult for users to diagnose and correct the issue.
PR #138231 addresses this by moving the validation of OUTPUT_ACTIVITY to the macro expansion phase, specifically within compiler/rustc_builtin_macros/src/autodiff.rs
. By performing these checks earlier, the compiler can emit clear and informative error messages without encountering ICEs.
In addition, a new error type was introduced in compiler/rustc_builtin_macros/src/errors.rs
:
1
2
3
4
5
6
7
8
#[derive(Diagnostic)]
#[diag(builtin_macros_autodiff_ret_activity)]
pub(crate) struct AutoDiffInvalidRetAct {
#[primary_span]
pub(crate) span: Span,
pub(crate) mode: String,
pub(crate) act: String,
}
Before this change, the following code would cause an ICE:
1
2
3
4
#[autodiff(df, Forward, Dual, Active)]
fn f(x: f32) -> f32 {
unimplemented!()
}
With the changes from PR #138231, the compiler now produces a clear error message:
1
2
3
4
5
6
7
error: invalid return activity Active in Forward Mode
--> $DIR/autodiff_illegal.rs:161:1
|
LL | #[autodiff(df19, Forward, Dual, Active)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
See tests/ui/autodiff/autodiff_illegal.stderr
to have a clearer vision about autodiff errors.
Note on span precision:
Currently, error messages related to invalid activity annotations point to the entire#[autodiff]
attribute, rather than the specific argument causing the issue. This is due to limitations in how spans are passed within thegen_enzyme_decl
function, which only receives the overall attribute span. While some error types (like unknown activities) can target specific spans, improving span precision for return/input activity errors would require broader changes. AFIXME
has been added to revisit this in the future.