Skip to content

std::autodiff failing in debug builds - cannot compute with global variable #154822

@ZuseZ4

Description

@ZuseZ4

I tried this code:

#![feature(autodiff)]
#![feature(core_intrinsics)]
#![feature(rustc_attrs)]

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>

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.F-autodiff`#![feature(autodiff)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions