Skip to content

Commit 2d1fc63

Browse files
committed
adding a working rlib autodiff test
1 parent 0dfdb6c commit 2d1fc63

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub fn f(x: f64, y: f64) -> f64 {
2+
2.0 * x + y
3+
}
4+
5+
pub fn g(x: f64) -> f64 {
6+
2.0 * x
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(autodiff)]
2+
extern crate simple_dep;
3+
use std::autodiff::*;
4+
5+
#[inline(never)]
6+
pub fn f2(x: f64) -> f64 {
7+
x.sin()
8+
}
9+
10+
#[autodiff_forward(df1_lib, Dual, Dual)]
11+
pub fn _f1(x: f64) -> f64 {
12+
simple_dep::f(x, x) * f2(x)
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern crate foo;
2+
3+
fn main() {
4+
//dbg!("Running main.rs");
5+
let enzyme_y1_lib = foo::df1_lib(1.5, 1.0);
6+
println!("output1: {:?}", enzyme_y1_lib.0);
7+
println!("output2: {:?}", enzyme_y1_lib.1);
8+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//@ needs-enzyme
2+
//@ ignore-cross-compile
3+
4+
use run_make_support::{cwd, run, rustc};
5+
6+
fn main() {
7+
// Build the dependency crate.
8+
rustc()
9+
.input("dep.rs")
10+
.arg("-Zautodiff=Enable")
11+
.arg("--edition=2024")
12+
.arg("-Copt-level=3")
13+
.arg("--crate-name=simple_dep")
14+
.arg("-Clinker-plugin-lto")
15+
.arg("--crate-type=lib")
16+
.emit("dep-info,metadata,link")
17+
.run();
18+
19+
let cwd = cwd();
20+
let cwd_str = cwd.to_string_lossy();
21+
22+
let mydep = format!("-Ldependency={cwd_str}");
23+
24+
let simple_dep_rlib =
25+
format!("--extern=simple_dep={}", cwd.join("libsimple_dep.rlib").to_string_lossy());
26+
27+
// Build the main library that depends on `simple_dep`.
28+
rustc()
29+
.input("lib.rs")
30+
.arg("-Zautodiff=Enable")
31+
.arg("--edition=2024")
32+
.arg("-Copt-level=3")
33+
.arg("--crate-name=foo")
34+
.arg("-Clinker-plugin-lto")
35+
.arg("--crate-type=lib")
36+
.emit("dep-info,metadata,link")
37+
.arg(&mydep)
38+
.arg(&simple_dep_rlib)
39+
.run();
40+
41+
let foo_rlib = format!("--extern=foo={}", cwd.join("libfoo.rlib").to_string_lossy());
42+
43+
// Build the final binary linking both rlibs.
44+
rustc()
45+
.input("main.rs")
46+
.arg("-Zautodiff=Enable")
47+
.arg("--edition=2024")
48+
.arg("-Copt-level=3")
49+
.arg("--crate-name=foo")
50+
.arg("-Clto=fat")
51+
.arg("--crate-type=bin")
52+
.emit("dep-info,link")
53+
.arg(&mydep)
54+
.arg(&foo_rlib)
55+
.arg(&simple_dep_rlib)
56+
.run();
57+
58+
// Run the binary and check its output.
59+
let binary = run("foo");
60+
assert!(binary.status().success(), "binary failed to run");
61+
62+
let binary_out = binary.stdout();
63+
let output = String::from_utf8_lossy(&binary_out);
64+
assert!(output.contains("output1: 4.488727439718245"));
65+
assert!(output.contains("output2: 3.3108023673168265"));
66+
}

0 commit comments

Comments
 (0)