adding a working rlib autodiff test

This commit is contained in:
Manuel Drehwald 2025-11-18 23:37:30 -05:00
parent 0dfdb6c3da
commit 2d1fc63101
4 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,7 @@
pub fn f(x: f64, y: f64) -> f64 {
2.0 * x + y
}
pub fn g(x: f64) -> f64 {
2.0 * x
}

View file

@ -0,0 +1,13 @@
#![feature(autodiff)]
extern crate simple_dep;
use std::autodiff::*;
#[inline(never)]
pub fn f2(x: f64) -> f64 {
x.sin()
}
#[autodiff_forward(df1_lib, Dual, Dual)]
pub fn _f1(x: f64) -> f64 {
simple_dep::f(x, x) * f2(x)
}

View file

@ -0,0 +1,8 @@
extern crate foo;
fn main() {
//dbg!("Running main.rs");
let enzyme_y1_lib = foo::df1_lib(1.5, 1.0);
println!("output1: {:?}", enzyme_y1_lib.0);
println!("output2: {:?}", enzyme_y1_lib.1);
}

View file

@ -0,0 +1,66 @@
//@ needs-enzyme
//@ ignore-cross-compile
use run_make_support::{cwd, run, rustc};
fn main() {
// Build the dependency crate.
rustc()
.input("dep.rs")
.arg("-Zautodiff=Enable")
.arg("--edition=2024")
.arg("-Copt-level=3")
.arg("--crate-name=simple_dep")
.arg("-Clinker-plugin-lto")
.arg("--crate-type=lib")
.emit("dep-info,metadata,link")
.run();
let cwd = cwd();
let cwd_str = cwd.to_string_lossy();
let mydep = format!("-Ldependency={cwd_str}");
let simple_dep_rlib =
format!("--extern=simple_dep={}", cwd.join("libsimple_dep.rlib").to_string_lossy());
// Build the main library that depends on `simple_dep`.
rustc()
.input("lib.rs")
.arg("-Zautodiff=Enable")
.arg("--edition=2024")
.arg("-Copt-level=3")
.arg("--crate-name=foo")
.arg("-Clinker-plugin-lto")
.arg("--crate-type=lib")
.emit("dep-info,metadata,link")
.arg(&mydep)
.arg(&simple_dep_rlib)
.run();
let foo_rlib = format!("--extern=foo={}", cwd.join("libfoo.rlib").to_string_lossy());
// Build the final binary linking both rlibs.
rustc()
.input("main.rs")
.arg("-Zautodiff=Enable")
.arg("--edition=2024")
.arg("-Copt-level=3")
.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();
// Run the binary and check its output.
let binary = run("foo");
assert!(binary.status().success(), "binary failed to run");
let binary_out = binary.stdout();
let output = String::from_utf8_lossy(&binary_out);
assert!(output.contains("output1: 4.488727439718245"));
assert!(output.contains("output2: 3.3108023673168265"));
}