Fix Enzyme autodiff ICE by anchoring metadata markers in core#155059
Fix Enzyme autodiff ICE by anchoring metadata markers in core#155059purahan wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
52242e4 to
22bdafe
Compare
This comment has been minimized.
This comment has been minimized.
Fixes a crash where Enzyme fails to compute derivatives when compiling with LTO. Previously, global metadata markers (like `enzyme_dup`) were declared in the LLVM backend (`rustc_codegen_llvm`). However, LLVM's Link-Time Optimization aggressively stripped their initializers, causing Enzyme to misinterpret them as active external variables rather than constant structural markers. This fix resolves the issue by defining these structural anchors directly within the `core` library as `#[no_mangle]` statics. By anchoring them in the standard library, they are natively protected from LTO stripping, ensuring Enzyme correctly parses them as constant metadata during derivative generation.
22bdafe to
73c60ae
Compare
|
@rustbot ready |
|
r? me Hi! Do you have any tests or a reproducer which was broken and which this change is fixing? |
|
Hey @ZuseZ4, thanks for reviewing! Yeah, the ICE triggers specifically when combining the autodiff feature with Link-Time Optimization (-C lto=fat). Here’s a minimal reproducer: #![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];
}
fn main() {
let x = [2.0, 2.0];
let mut bx = [0.0, 0.0];
let mut y = 0.0;
let mut by = 1.0;
df(&x, &mut bx, &mut y, &mut by);
assert_eq!(bx, [6.0, 2.0]);
}If you compile that with Basically, because those dummy metadata globals were only declared in the C++ backend, LTO was aggressively stripping their initializers out as dead code. Moving them directly into Let me know if you want me to drop this reproducer into |
Fixes a crash where Enzyme fails to compute derivatives when compiling with LTO. Previously, global metadata markers (like
enzyme_dup) were declared in the LLVM backend (rustc_codegen_llvm). However, LLVM's Link-Time Optimization aggressively stripped their initializers, causing Enzyme to misinterpret them as active external variables rather than constant structural markers.This fix resolves the issue by defining these structural anchors directly within the
corelibrary as#[no_mangle]statics. By anchoring them in the standard library, they are natively protected from LTO stripping, ensuring Enzyme correctly parses them as constant metadata during derivative generation.