-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Autodiff flags #139700
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
Merged
Merged
Autodiff flags #139700
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
75f86e6
fix LooseTypes flag and PrintMod behaviour, add debug helper
ZuseZ4 31578dc
fix "could not find source function" error by preventing function mer…
ZuseZ4 5ea9125
update documentation
ZuseZ4 f79a992
add tests for merge_function handling
ZuseZ4 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//@ compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat | ||
//@ no-prefer-dynamic | ||
//@ needs-enzyme | ||
// | ||
// Each autodiff invocation creates a new placeholder function, which we will replace on llvm-ir | ||
// level. If a user tries to differentiate two identical functions within the same compilation unit, | ||
// then LLVM might merge them in release mode before AD. In that case we can't rewrite one of the | ||
// merged placeholder function anymore, and compilation would fail. We prevent this by disabling | ||
// LLVM's merge_function pass before AD. Here we implicetely test that our solution keeps working. | ||
// We also explicetly test that we keep running merge_function after AD, by checking for two | ||
// identical function calls in the LLVM-IR, while having two different calls in the Rust code. | ||
#![feature(autodiff)] | ||
|
||
use std::autodiff::autodiff; | ||
|
||
#[autodiff(d_square, Reverse, Duplicated, Active)] | ||
fn square(x: &f64) -> f64 { | ||
x * x | ||
} | ||
|
||
#[autodiff(d_square2, Reverse, Duplicated, Active)] | ||
fn square2(x: &f64) -> f64 { | ||
x * x | ||
} | ||
|
||
// CHECK:; identical_fnc::main | ||
// CHECK-NEXT:; Function Attrs: | ||
// CHECK-NEXT:define internal void @_ZN13identical_fnc4main17hf4dbc69c8d2f9130E() | ||
// CHECK-NEXT:start: | ||
// CHECK-NOT:br | ||
// CHECK-NOT:ret | ||
// CHECK:; call identical_fnc::d_square | ||
// CHECK-NEXT: call fastcc void @_ZN13identical_fnc8d_square17h4c364207a2f8e06dE(double %x.val, ptr noalias noundef nonnull align 8 dereferenceable(8) %dx1) | ||
// CHECK-NEXT:; call identical_fnc::d_square | ||
// CHECK-NEXT: call fastcc void @_ZN13identical_fnc8d_square17h4c364207a2f8e06dE(double %x.val, ptr noalias noundef nonnull align 8 dereferenceable(8) %dx2) | ||
|
||
fn main() { | ||
let x = std::hint::black_box(3.0); | ||
let mut dx1 = std::hint::black_box(1.0); | ||
let mut dx2 = std::hint::black_box(1.0); | ||
let _ = d_square(&x, &mut dx1, 1.0); | ||
let _ = d_square2(&x, &mut dx2, 1.0); | ||
assert_eq!(dx1, 6.0); | ||
assert_eq!(dx2, 6.0); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.