-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
autodiff: Handle slice-tailed DSTs in type trees #160390
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
rust-bors
merged 2 commits into
rust-lang:main
from
Dnreikronos:autodiff/slice_dst_typetree
Aug 5, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
19 changes: 19 additions & 0 deletions
19
tests/run-make/autodiff/type-trees/slice-dst-typetree/rmake.rs
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,19 @@ | ||
| //@ needs-enzyme | ||
| //@ ignore-cross-compile | ||
|
|
||
| use run_make_support::{llvm_filecheck, rfs, rustc}; | ||
|
|
||
| fn main() { | ||
| rustc() | ||
| .input("test.rs") | ||
| .arg("-Zautodiff=Enable,NoPostopt") | ||
| .opt_level("0") | ||
| .arg("-Clto=fat") | ||
| .emit("llvm-ir") | ||
| .run(); | ||
|
|
||
| let ir = rfs::read("test.ll"); | ||
| llvm_filecheck().patterns("slice-dst.check").check_prefix("OSSTR").stdin_buf(&ir).run(); | ||
| llvm_filecheck().patterns("slice-dst.check").check_prefix("HEADER").stdin_buf(&ir).run(); | ||
| llvm_filecheck().patterns("slice-dst.check").check_prefix("ZST").stdin_buf(&ir).run(); | ||
| } |
14 changes: 14 additions & 0 deletions
14
tests/run-make/autodiff/type-trees/slice-dst-typetree/slice-dst.check
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,14 @@ | ||
| ; Preserve element metadata for the `OsStr` regression from #160327. | ||
| OSSTR-LABEL: define void @split_once( | ||
| OSSTR-NOT: define | ||
| OSSTR: call void @llvm.memcpy{{.*}}"enzyme_type"="{[0]:Pointer, [0,0]:Pointer, [0,0,-1]:Integer, [0,16]:Pointer, [0,16,-1]:Integer}" | ||
|
|
||
| ; Preserve both data layout and length metadata for a prefixed slice-tail DST. | ||
| HEADER-LABEL: define{{.*}}@header_sum( | ||
| HEADER-SAME: ptr{{.*}}"enzyme_type"="{[-1]:Pointer, [-1,0]:Float@float, [-1,4]:Float@float}" | ||
| HEADER-SAME: i64 "enzyme_type"="{[0]:Integer}" | ||
|
|
||
| ; ZST elements produce no child metadata under the slice data pointer. | ||
| ZST-LABEL: define{{.*}}@zst_slice_len( | ||
| ZST-SAME: ptr{{.*}}"enzyme_type"="{[-1]:Pointer}" | ||
| ZST-SAME: i64 "enzyme_type"="{[0]:Integer}" |
51 changes: 51 additions & 0 deletions
51
tests/run-make/autodiff/type-trees/slice-dst-typetree/test.rs
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,51 @@ | ||
| #![crate_type = "lib"] | ||
| #![feature(autodiff)] | ||
|
|
||
| use std::autodiff::autodiff_reverse; | ||
| use std::ffi::OsStr; | ||
|
|
||
| // Reduced from `clap_lex::OsStrExt::split_once`. | ||
| #[no_mangle] | ||
| #[inline(never)] | ||
| pub fn split_once<'s>(arg: &'s OsStr, needle: &str) -> Option<(&'s OsStr, &'s OsStr)> { | ||
| let bytes = arg.as_encoded_bytes(); | ||
| let index = bytes.windows(needle.len()).position(|window| window == needle.as_bytes())?; | ||
| let (first, second) = bytes.split_at(index + needle.len()); | ||
| unsafe { | ||
| Some(( | ||
| OsStr::from_encoded_bytes_unchecked(first), | ||
| OsStr::from_encoded_bytes_unchecked(second), | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| pub struct Header<T: ?Sized> { | ||
| tag: f32, | ||
| data: T, | ||
| } | ||
|
|
||
| #[autodiff_reverse(d_header_sum, Duplicated, Active)] | ||
| #[no_mangle] | ||
| #[inline(never)] | ||
| pub fn header_sum(value: &Header<[f32]>) -> f32 { | ||
| value.tag + value.data.iter().sum::<f32>() | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub fn exercise_header_sum(value: &Header<[f32]>, derivative: &mut Header<[f32]>) -> f32 { | ||
| d_header_sum(value, derivative, 1.0) | ||
| } | ||
|
|
||
| // ZST slice elements yield an empty child TypeTree; element size 0 is expected. | ||
| #[autodiff_reverse(d_zst_slice_len, Duplicated, Active)] | ||
| #[no_mangle] | ||
| #[inline(never)] | ||
| pub fn zst_slice_len(slice: &[()]) -> f32 { | ||
| slice.len() as f32 | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub fn exercise_zst_slice_len(slice: &[()], derivative: &mut [()]) -> f32 { | ||
| d_zst_slice_len(slice, derivative, 1.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.