1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
//@ revisions: debug release
//@[debug] compile-flags: -Zautodiff=Enable,NoTT -C opt-level=0 -Clto=fat
//@[release] compile-flags: -Zautodiff=Enable,NoTT -C opt-level=3 -Clto=fat
//@ no-prefer-dynamic
//@ needs-enzyme
// This test checks that Rust types are lowered to LLVM-IR types in a way
// we expect and Enzyme can handle. We explicitly check release mode to
// ensure that LLVM's O3 pipeline doesn't rewrite function signatures
// into forms that Enzyme can't process correctly.
#![feature(autodiff)]
use std::autodiff::{autodiff_forward, autodiff_reverse};
#[derive(Copy, Clone)]
struct Input {
x: f32,
y: f32,
}
#[derive(Copy, Clone)]
struct Wrapper {
z: f32,
}
#[derive(Copy, Clone)]
struct NestedInput {
x: f32,
y: Wrapper,
}
fn square(x: f32) -> f32 {
x * x
}
// CHECK-LABEL: ; abi_handling::df1
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal { float, float }
// debug-SAME: (ptr align 4 %x, ptr align 4 %bx_0)
// release-NEXT: define internal fastcc float
// release-SAME: (float %x.0.val, float %x.4.val)
// CHECK-LABEL: ; abi_handling::f1
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal float
// debug-SAME: (ptr align 4 %x)
// release-NEXT: define internal fastcc noundef float
// release-SAME: (float %x.0.val, float %x.4.val)
#[autodiff_forward(df1, Dual, Dual)]
#[inline(never)]
fn f1(x: &[f32; 2]) -> f32 {
x[0] + x[1]
}
// CHECK-LABEL: ; abi_handling::df2
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal { float, float }
// debug-SAME: (ptr %f, float %x, float %dret)
// release-NEXT: define internal fastcc float
// release-SAME: (float noundef %x)
// CHECK-LABEL: ; abi_handling::f2
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal float
// debug-SAME: (ptr %f, float %x)
// release-NEXT: define internal fastcc noundef float
// release-SAME: (float noundef %x)
#[autodiff_reverse(df2, Const, Active, Active)]
#[inline(never)]
fn f2(f: fn(f32) -> f32, x: f32) -> f32 {
f(x)
}
// CHECK-LABEL: ; abi_handling::df3
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal { float, float }
// debug-SAME: (ptr align 4 %x, ptr align 4 %bx_0, ptr align 4 %y, ptr align 4 %by_0)
// release-NEXT: define internal fastcc { float, float }
// release-SAME: (float %x.0.val)
// CHECK-LABEL: ; abi_handling::f3
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal float
// debug-SAME: (ptr align 4 %x, ptr align 4 %y)
// release-NEXT: define internal fastcc noundef float
// release-SAME: (float %x.0.val)
#[autodiff_forward(df3, Dual, Dual, Dual)]
#[inline(never)]
fn f3<'a>(x: &'a f32, y: &'a f32) -> f32 {
*x * *y
}
// CHECK-LABEL: ; abi_handling::df4
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal { float, float }
// debug-SAME: (float %x.0, float %x.1, float %bx_0.0, float %bx_0.1)
// release-NEXT: define internal fastcc { float, float }
// release-SAME: (float noundef %x.0, float noundef %x.1)
// CHECK-LABEL: ; abi_handling::f4
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal float
// debug-SAME: (float %x.0, float %x.1)
// release-NEXT: define internal fastcc noundef float
// release-SAME: (float noundef %x.0, float noundef %x.1)
#[autodiff_forward(df4, Dual, Dual)]
#[inline(never)]
fn f4(x: (f32, f32)) -> f32 {
x.0 * x.1
}
// CHECK-LABEL: ; abi_handling::df5
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal { float, float }
// debug-SAME: (float %i.0, float %i.1, float %bi_0.0, float %bi_0.1)
// release-NEXT: define internal fastcc { float, float }
// release-SAME: (float noundef %i.0, float noundef %i.1)
// CHECK-LABEL: ; abi_handling::f5
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal float
// debug-SAME: (float %i.0, float %i.1)
// release-NEXT: define internal fastcc noundef float
// release-SAME: (float noundef %i.0, float noundef %i.1)
#[autodiff_forward(df5, Dual, Dual)]
#[inline(never)]
fn f5(i: Input) -> f32 {
i.x + i.y
}
// CHECK-LABEL: ; abi_handling::df6
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal { float, float }
// debug-SAME: (float %i.0, float %i.1, float %bi_0.0, float %bi_0.1)
// release-NEXT: define internal fastcc { float, float }
// release-SAME: float noundef %i.0, float noundef %i.1
// release-SAME: float noundef %bi_0.0, float noundef %bi_0.1
// CHECK-LABEL: ; abi_handling::f6
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal float
// debug-SAME: (float %i.0, float %i.1)
// release-NEXT: define internal fastcc noundef float
// release-SAME: (float noundef %i.0, float noundef %i.1)
#[autodiff_forward(df6, Dual, Dual)]
#[inline(never)]
fn f6(i: NestedInput) -> f32 {
i.x + i.y.z * i.y.z
}
// CHECK-LABEL: ; abi_handling::df7
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal { float, float }
// debug-SAME: (ptr align 4 %x.0, ptr align 4 %x.1, ptr align 4 %bx_0.0, ptr align 4 %bx_0.1)
// release-NEXT: define internal fastcc { float, float }
// release-SAME: (float %x.0.0.val, float %x.1.0.val)
// CHECK-LABEL: ; abi_handling::f7
// CHECK-NEXT: Function Attrs
// debug-NEXT: define internal float
// debug-SAME: (ptr align 4 %x.0, ptr align 4 %x.1)
// release-NEXT: define internal fastcc noundef float
// release-SAME: (float %x.0.0.val, float %x.1.0.val)
#[autodiff_forward(df7, Dual, Dual)]
#[inline(never)]
fn f7(x: (&f32, &f32)) -> f32 {
x.0 * x.1
}
fn main() {
let x = std::hint::black_box(2.0);
let y = std::hint::black_box(3.0);
let z = std::hint::black_box(4.0);
static Y: f32 = std::hint::black_box(3.2);
let in_f1 = [x, y];
dbg!(f1(&in_f1));
let res_f1 = df1(&in_f1, &[1.0, 0.0]);
dbg!(res_f1);
dbg!(f2(square, x));
let res_f2 = df2(square, x, 1.0);
dbg!(res_f2);
dbg!(f3(&x, &Y));
let res_f3 = df3(&x, &Y, &1.0, &0.0);
dbg!(res_f3);
let in_f4 = (x, y);
dbg!(f4(in_f4));
let res_f4 = df4(in_f4, (1.0, 0.0));
dbg!(res_f4);
let in_f5 = Input { x, y };
dbg!(f5(in_f5));
let res_f5 = df5(in_f5, Input { x: 1.0, y: 0.0 });
dbg!(res_f5);
let in_f6 = NestedInput { x, y: Wrapper { z: y } };
dbg!(f6(in_f6));
let res_f6 = df6(in_f6, NestedInput { x, y: Wrapper { z } });
dbg!(res_f6);
let in_f7 = (&x, &y);
dbg!(f7(in_f7));
let res_f7 = df7(in_f7, (&1.0, &0.0));
dbg!(res_f7);
}
|