Skip to content

adding UI test to autodiff #142444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@ struct LLVMRustSanitizerOptions {
#ifdef ENZYME
extern "C" void registerEnzymeAndPassPipeline(llvm::PassBuilder &PB,
/* augmentPassBuilder */ bool);

extern "C" {
extern llvm::cl::opt<std::string> EnzymeFunctionToAnalyze;
}
#endif

extern "C" LLVMRustResult LLVMRustOptimize(
Expand Down Expand Up @@ -1069,6 +1073,15 @@ extern "C" LLVMRustResult LLVMRustOptimize(
return LLVMRustResult::Failure;
}

// Check if PrintTAFn was used and add type analysis pass if needed
if (!EnzymeFunctionToAnalyze.empty()) {
if (auto Err = PB.parsePassPipeline(MPM, "print-type-analysis")) {
std::string ErrMsg = toString(std::move(Err));
LLVMRustSetLastError(ErrMsg.c_str());
return LLVMRustResult::Failure;
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes are for fixing PrintTAFn flag, which was not working, should i create new PR for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for it, just make it it's own commit and that the history is clean.
E.g. end up with a first commit fixing the Wrapper, and a second commit introducing the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will clean the history after I am done writing tests at the end

if (PrintAfterEnzyme) {
// Handle the Rust flag `-Zautodiff=PrintModAfter`.
std::string Banner = "Module after EnzymeNewPM";
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/autodiff/type-trees/type-analysis/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ run-pass
//@ compile-flags: -Zautodiff=Enable,PrintTAFn=callee -Zautodiff=NoPostopt -C opt-level=3 -Clto=fat -g
//@ no-prefer-dynamic
//@ needs-enzyme
//@ normalize-stderr: "!(dbg|noundef) ![0-9]+" -> "!$1 !N"
//@ normalize-stderr: "%[0-9]+" -> "%X"
//@ normalize-stdout: "!(dbg|noundef) ![0-9]+" -> "!$1 !N"
//@ normalize-stdout: "%[0-9]+" -> "%X"

#![feature(autodiff)]

use std::autodiff::autodiff_reverse;

#[autodiff_reverse(d_square, Duplicated, Active)]
#[no_mangle]
fn callee(x: &[f32; 3]) -> f32 {
x[0] * x[0] + x[1] * x[1] + x[2] * x[2]
}

fn main() {
let x = [1.0f32, 2.0, 3.0];
let mut df_dx = [0.0f32; 3];
let out = callee(&x);
let out_ = d_square(&x, &mut df_dx, 1.0);
assert_eq!(out, out_);
assert_eq!(2.0, df_dx[0]);
assert_eq!(4.0, df_dx[1]);
assert_eq!(6.0, df_dx[2]);
}
198 changes: 198 additions & 0 deletions tests/ui/autodiff/type-trees/type-analysis/array.stderr

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions tests/ui/autodiff/type-trees/type-analysis/array.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
callee - {[-1]:Float@float} |{[-1]:Pointer}:{}
ptr %X: {[-1]:Pointer, [-1,0]:Float@float, [-1,4]:Float@float, [-1,8]:Float@float}

%X = load float, ptr %X, align 4, !dbg !N, !noundef !N: {[-1]:Float@float}
%X = fmul float %X, %X, !dbg !N: {[-1]:Float@float}
%X = getelementptr inbounds nuw i8, ptr %X, i64 4, !dbg !N: {[-1]:Pointer, [-1,0]:Float@float, [-1,4]:Float@float}
%X = load float, ptr %X, align 4, !dbg !N, !noundef !N: {[-1]:Float@float}
%X = fmul float %X, %X, !dbg !N: {[-1]:Float@float}
%X = fadd float %X, %X, !dbg !N: {[-1]:Float@float}
%X = getelementptr inbounds nuw i8, ptr %X, i64 8, !dbg !N: {[-1]:Pointer, [-1,0]:Float@float}
%X = load float, ptr %X, align 4, !dbg !N, !noundef !N: {[-1]:Float@float}
%X = fmul float %X, %X, !dbg !N: {[-1]:Float@float}
%X = fadd float %X, %X, !dbg !N: {[-1]:Float@float}
ret float %X, !dbg !N: {}
callee - {[-1]:Float@float} |{[-1]:Pointer, [-1,0]:Float@float, [-1,4]:Float@float, [-1,8]:Float@float}:{}
ptr %X: {[-1]:Pointer, [-1,0]:Float@float, [-1,4]:Float@float, [-1,8]:Float@float}

%X = load float, ptr %X, align 4, !dbg !N, !noundef !N: {[-1]:Float@float}
%X = fmul float %X, %X, !dbg !N: {[-1]:Float@float}
%X = getelementptr inbounds nuw i8, ptr %X, i64 4, !dbg !N: {[-1]:Pointer, [-1,0]:Float@float, [-1,4]:Float@float}
%X = load float, ptr %X, align 4, !dbg !N, !noundef !N: {[-1]:Float@float}
%X = fmul float %X, %X, !dbg !N: {[-1]:Float@float}
%X = fadd float %X, %X, !dbg !N: {[-1]:Float@float}
%X = getelementptr inbounds nuw i8, ptr %X, i64 8, !dbg !N: {[-1]:Pointer, [-1,0]:Float@float}
%X = load float, ptr %X, align 4, !dbg !N, !noundef !N: {[-1]:Float@float}
%X = fmul float %X, %X, !dbg !N: {[-1]:Float@float}
%X = fadd float %X, %X, !dbg !N: {[-1]:Float@float}
ret float %X, !dbg !N: {}
41 changes: 41 additions & 0 deletions tests/ui/autodiff/type-trees/type-analysis/array3d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//@ run-pass
//@ compile-flags: -Zautodiff=Enable,PrintTAFn=callee -Zautodiff=NoPostopt -C opt-level=3 -Clto=fat -g
//@ no-prefer-dynamic
//@ needs-enzyme
//@ normalize-stderr: "!(dbg|noundef) ![0-9]+" -> "!$1 !N"
//@ normalize-stderr: "%[0-9]+" -> "%X"
//@ normalize-stdout: "!(dbg|noundef) ![0-9]+" -> "!$1 !N"
//@ normalize-stdout: "%[0-9]+" -> "%X"

#![feature(autodiff)]

use std::autodiff::autodiff_reverse;

#[autodiff_reverse(d_square, Duplicated, Active)]
#[no_mangle]
fn callee(x: &[[[f32; 2]; 2]; 2]) -> f32 {
let mut sum = 0.0;
for i in 0..2 {
for j in 0..2 {
for k in 0..2 {
sum += x[i][j][k] * x[i][j][k];
}
}
}
sum
}

fn main() {
let x = [[[1.0f32, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]];
let mut df_dx = [[[0.0f32; 2]; 2]; 2];
let out = callee(&x);
let out_ = d_square(&x, &mut df_dx, 1.0);
assert_eq!(out, out_);
for i in 0..2 {
for j in 0..2 {
for k in 0..2 {
assert_eq!(df_dx[i][j][k], 2.0 * x[i][j][k]);
}
}
}
}
Loading
Loading