|
| 1 | +//@ compile-flags: -Zautodiff=Enable,NoTT,NoPostopt -C no-prepopulate-passes -C opt-level=3 -Clto=fat |
| 2 | +//@ no-prefer-dynamic |
| 3 | +//@ needs-enzyme |
| 4 | + |
| 5 | +#![feature(autodiff)] |
| 6 | +use std::autodiff::*; |
| 7 | + |
| 8 | +// Usually we would store the return value of the differentiated function. |
| 9 | +// However, if the return type is void or an empty struct, |
| 10 | +// we don't need to store anything. Verify this, since it caused a bug. |
| 11 | + |
| 12 | +// CHECK:; void_ret::main |
| 13 | +// CHECK-NEXT: ; Function Attrs: |
| 14 | +// CHECK-NEXT: define internal |
| 15 | +// CHECK-NOT: store {} undef, ptr undef |
| 16 | +// CHECK: ret void |
| 17 | + |
| 18 | +#[autodiff_reverse(bar, Duplicated, Duplicated)] |
| 19 | +pub fn foo(r: &[f64; 10], res: &mut f64) { |
| 20 | + let mut output = [0.0; 10]; |
| 21 | + output[0] = r[0]; |
| 22 | + output[1] = r[1] * r[2]; |
| 23 | + output[2] = r[4] * r[5]; |
| 24 | + output[3] = r[2] * r[6]; |
| 25 | + output[4] = r[1] * r[7]; |
| 26 | + output[5] = r[2] * r[8]; |
| 27 | + output[6] = r[1] * r[9]; |
| 28 | + output[7] = r[5] * r[6]; |
| 29 | + output[8] = r[5] * r[7]; |
| 30 | + output[9] = r[4] * r[8]; |
| 31 | + *res = output.iter().sum(); |
| 32 | +} |
| 33 | +fn main() { |
| 34 | + let inputs = Box::new([3.1; 10]); |
| 35 | + let mut d_inputs = Box::new([0.0; 10]); |
| 36 | + let mut res = Box::new(0.0); |
| 37 | + let mut d_res = Box::new(1.0); |
| 38 | + |
| 39 | + bar(&inputs, &mut d_inputs, &mut res, &mut d_res); |
| 40 | + dbg!(&d_inputs); |
| 41 | +} |
0 commit comments