std::autodiff failing in debug builds - cannot compute with global variable
@purahan is already working on this.
Since Apr 5, 2026.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
#![feature(autodiff)]
use std::autodiff::*;
#[autodiff_reverse(df, Duplicated, Duplicated)]
fn f(x: &[f64;2], y: &mut f64) {
*y = x[0] * x[0] + x[1] * x[0]
}
#[autodiff_forward(h, Dual, Dual, Dual, Dual)]
fn wrapper(x: &[f64;2], dx: &mut [f64;2], y: &mut f64, dy: &mut f64) {
df(x, dx, y, dy);
}
fn main() {
let mut y = 0.0;
let x = [2.0, 2.0];
let mut dy = 0.0;
let mut dx = [1.0, 0.0];
let mut bx = [0.0, 0.0];
let mut by = 1.0;
let mut dbx = [0.0, 0.0];
let mut dby = 0.0;
h(&x, &mut dx, &mut bx, &mut dbx, &mut y, &mut dy, &mut by, &mut dby);
assert_eq!(dbx, [2.0, 1.0]);
}
I expected to see this happen: such code used to work
Instead, this happened:
error: src/main.rs:13:48: in function preprocess__RNvCs3nwi6xNxB2t_3foo2df void (ptr, ptr, ptr, ptr): Enzyme: cannot compute with global variable that doesn't have marked shadow global
@enzyme_dup = external global ptr
RUSTFLAGS="-Zautodiff=Enable" cargo +enzyme run
Meta
rustc --version --verbose:
HEAD of main branch
We used to have similar-looking issues when using metadata instead of the named globals for enzyme info like enzyme_dup. cc @oli-obk and I had already agreed back then that we should investigate the real cause rather than working around by switching from metadata to globals. I guess we should get back to it.
It should be trivial to first minimize this to IR level and then we should easily see if it's an Enzyme failure (I don't think so) or just us doing something unlucky around caching/incremental compilation, which obscures the global name for enzyme.
Backtrace
<backtrace>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.