Skip to content

Commit 402f34c

Browse files
committed
added test for f32 and f64
Signed-off-by: Karan Janthe <karanjanthe@gmail.com>
1 parent 3c0c1a6 commit 402f34c

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

tests/codegen/autodiff/f32.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
//@ compile-flags: -Zautodiff=Enable -Zautodiff=NoPostopt -C opt-level=3 -Clto=fat -g
1+
//@ compile-flags: -Zautodiff=Enable,PrintTAFn=callee -Zautodiff=NoPostopt -C opt-level=3 -Clto=fat -g
22
//@ no-prefer-dynamic
33
//@ needs-enzyme
44
#![feature(autodiff)]
55

66
use std::autodiff::autodiff_reverse;
77

8+
// CHECK-LABEL: callee
9+
// CHECK: callee - {[-1]:Float@float} |{[-1]:Pointer}:{}
10+
// CHECK: ptr %x: {[-1]:Pointer, [-1,0]:Float@float}
11+
// CHECK: start
12+
// CHECK: %_2 = load float, ptr %x, align 4, !noundef !3: {[-1]:Float@float}
13+
// CHECK: %_0 = fmul float %_2, %_2: {[-1]:Float@float}
14+
// CHECK: ret float %_0: {}
15+
816
#[autodiff_reverse(d_square, Duplicated, Active)]
917
#[no_mangle]
1018
fn callee(x: &f32) -> f32 {
@@ -14,22 +22,9 @@ fn callee(x: &f32) -> f32 {
1422
fn main() {
1523
let x: f32 = 7.0;
1624
let mut df_dx: f32 = 0.0;
17-
d_square(&x, &mut df_dx, 1.0);
18-
}
19-
20-
// CHECK: define float @callee(ptr align 4 {{.*}}) {{.*}} !dbg {
21-
// CHECK-NEXT: start:
22-
// CHECK: #dbg_value
23-
// CHECK: load float
24-
// CHECK: fmul float
25-
// CHECK: ret float
26-
27-
// CHECK: define void @_ZN9f32_debug8d_square{{.*}}(ptr align 4 {{.*}}, ptr align 4 {{.*}}, float {{.*}}) {{.*}} {
28-
// CHECK: call {{.*}} @diffecallee
29-
// CHECK: ret void
25+
let out = callee(&x);
26+
let out_ = d_square(&x, &mut df_dx, 1.0);
3027

31-
// CHECK: define {{.*}} @diffecallee(ptr {{.*}} align 4 {{.*}}, ptr {{.*}} align 4 {{.*}}, float {{.*}}) {{.*}} {
32-
// CHECK: load float
33-
// CHECK: fmul float
34-
// CHECK: store float
35-
// CHECK: ret {{.*}}
28+
assert_eq!(out, out_);
29+
assert_eq!(14.0, df_dx);
30+
}

tests/codegen/autodiff/f64.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ compile-flags: -Zautodiff=Enable,PrintTAFn=square -C opt-level=3 -Clto=fat
2+
//@ no-prefer-dynamic
3+
//@ needs-enzyme
4+
#![feature(autodiff)]
5+
use std::autodiff::autodiff_reverse;
6+
7+
// CHECK-LABEL: square
8+
// CHECK: square - {[-1]:Float@double} |{[-1]:Pointer}:{}
9+
// CHECK: ptr %x: {[-1]:Pointer, [-1,0]:Float@double}
10+
// CHECK: start
11+
// CHECK: %_2 = load double, ptr %x, align 8, !noundef !3: {[-1]:Float@double}
12+
// CHECK: %_0 = fmul double %_2, %_2: {[-1]:Float@double}
13+
// CHECK: ret double %_0: {}
14+
15+
#[autodiff_reverse(d_square, Duplicated, Active)]
16+
#[no_mangle]
17+
fn square(x: &f64) -> f64 {
18+
x * x
19+
}
20+
21+
fn main() {
22+
let x = std::hint::black_box(3.0);
23+
24+
let output = square(&x);
25+
assert_eq!(9.0, output);
26+
27+
let mut df_dx = 0.0;
28+
let output_ = d_square(&x, &mut df_dx, 1.0);
29+
assert_eq!(output, output_);
30+
assert_eq!(6.0, df_dx);
31+
}

0 commit comments

Comments
 (0)