Skip to content

Commit ece16b1

Browse files
committed
adding a working rlib autodiff test
1 parent 34abdf2 commit ece16b1

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
//f2(x)
13+
simple_dep::f(x, x) * f2(x)
14+
}
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 = format!(
25+
"--extern=simple_dep={}",
26+
cwd.join("libsimple_dep.rlib").to_string_lossy()
27+
);
28+
29+
// Build the main library that depends on `simple_dep`.
30+
rustc()
31+
.input("lib.rs")
32+
.arg("-Zautodiff=Enable")
33+
.arg("--edition=2024")
34+
.arg("-Copt-level=3")
35+
.arg("--crate-name=foo")
36+
.arg("-Clinker-plugin-lto")
37+
.arg("--crate-type=lib")
38+
.emit("dep-info,metadata,link")
39+
.arg(&mydep)
40+
.arg(&simple_dep_rlib)
41+
.run();
42+
43+
let foo_rlib = format!(
44+
"--extern=foo={}",
45+
cwd.join("libfoo.rlib").to_string_lossy()
46+
);
47+
48+
// Build the final binary linking both rlibs.
49+
rustc()
50+
.input("main.rs")
51+
.arg("-Zautodiff=Enable")
52+
.arg("--edition=2024")
53+
.arg("-Copt-level=3")
54+
.arg("--crate-name=foo")
55+
.arg("-Clto=fat")
56+
.arg("--crate-type=bin")
57+
.emit("dep-info,link")
58+
.arg(&mydep)
59+
.arg(&foo_rlib)
60+
.arg(&simple_dep_rlib)
61+
.run();
62+
63+
// Run the binary and check its output.
64+
let binary = run("foo");
65+
assert!(binary.status().success(), "binary failed to run");
66+
67+
let binary_out = binary.stdout();
68+
let output = String::from_utf8_lossy(&binary_out);
69+
assert!(output.contains("output1: 4.488727439718245"));
70+
assert!(output.contains("output2: 3.3108023673168265"));
71+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ needs-enzyme
2+
//@ ignore-cross-compile
3+
4+
use run_make_support::{llvm_filecheck, rfs, rustc, cwd, run};
5+
6+
fn main() {
7+
rustc().input("dep.rs").arg("-Zautodiff=Enable").arg("--edition=2024").arg("--crate-name=simple_dep").arg("-Clinker-plugin-lto").arg("--crate-type=lib").emit("dep-info,metadata,link").run();
8+
9+
let ext_simple_dep_rlib = cwd().join("libsimple_dep.rlib").to_string_lossy().to_string();
10+
let simple_dep_rlib = "--extern=simple_dep=".to_owned() + &ext_simple_dep_rlib;
11+
12+
let current_dir = cwd().to_string_lossy().to_string();
13+
let mydep = "-Ldependency=".to_owned() + &current_dir;
14+
15+
rustc().input("lib.rs").arg("-Zautodiff=Enable").arg("--edition=2024").arg("--crate-name=foo").arg("-Clinker-plugin-lto").arg("--crate-type=lib").emit("dep-info,metadata,link").arg(&mydep).arg(&simple_dep_rlib).run();
16+
17+
let foo_rlib = "--extern=foo=".to_owned() + &cwd().join("libfoo.rlib").to_string_lossy();
18+
19+
rustc().input("main.rs").arg("-Zautodiff=Enable").arg("--edition=2024").arg("--crate-name=foo").arg("-Clto=fat").arg("--crate-type=bin").emit("dep-info,link").arg(&mydep).arg(&foo_rlib).arg(&simple_dep_rlib).run();
20+
21+
let binary = run("foo");
22+
assert!(binary.status().success(), "binary failed to run");
23+
24+
let output = binary.stdout();
25+
let output_str = String::from_utf8_lossy(&output);
26+
27+
assert!(output_str.contains("output1: 4.488727439718245"));
28+
assert!(output_str.contains("output2: 3.3108023673168265"));
29+
}

0 commit comments

Comments
 (0)