-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
autodiff: Normalize typetree struct field types #160701
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
Dnreikronos
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
Dnreikronos:autodiff/typetree_normalize_array_const
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
14 changes: 14 additions & 0 deletions
14
tests/run-make/autodiff/type-trees/array-const-len-typetree/array-const-len.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 @@ | ||
| ; Pointer-to-[T; N]: normalize N, keep a compact [-1] float child under the pointee. | ||
| PTR-LABEL: define{{.*}}@copy_ptr_array( | ||
| PTR-NOT: define | ||
| PTR: call void @llvm.memcpy{{.*}}"enzyme_type"="{[0]:Pointer, [0,0]:Pointer, [0,0,-1]:Float@float, [0,8]:Float@float, [0,12]:Float@float}" | ||
|
|
||
| ; Inline [T; N]: first float element plus the distinct `scale: i32` field. | ||
| ; Struct flattening currently collapses array `-1` to the field base, so only | ||
| ; byte 0 of `data` is classified until Enzyme gains a compact range encoding. | ||
| INLINE-LABEL: define{{.*}}@copy_inline_array( | ||
| INLINE-NOT: define | ||
| INLINE: call void @llvm.memcpy{{.*}}"enzyme_type"="{[0]:Pointer, [0,0]:Float@float, [0,32]:Integer}" | ||
|
|
||
| ADIFF-LABEL: define{{.*}}@ptr_array_sum( | ||
| ADIFF-SAME: ptr{{.*}}"enzyme_type"="{[-1]:Pointer, [-1,0]:Pointer, [-1,0,-1]:Float@float, [-1,8]:Float@float, [-1,12]:Float@float}" |
19 changes: 19 additions & 0 deletions
19
tests/run-make/autodiff/type-trees/array-const-len-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("array-const-len.check").check_prefix("PTR").stdin_buf(&ir).run(); | ||
| llvm_filecheck().patterns("array-const-len.check").check_prefix("INLINE").stdin_buf(&ir).run(); | ||
| llvm_filecheck().patterns("array-const-len.check").check_prefix("ADIFF").stdin_buf(&ir).run(); | ||
| } |
53 changes: 53 additions & 0 deletions
53
tests/run-make/autodiff/type-trees/array-const-len-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,53 @@ | ||
| #![crate_type = "lib"] | ||
| #![feature(autodiff)] | ||
|
|
||
| use std::autodiff::autodiff_reverse; | ||
|
|
||
| // Regression for #160635: anon-const array lengths in struct fields need | ||
| // normalization before typetree walks. `*mut [f32; N]` used to ICE in | ||
| // `struct_tail_for_codegen` (deepest trailing field / unsizing tail), and a | ||
| // plain `[f32; N]` field used to emit an empty TypeTree. | ||
| // | ||
| // `scale` is `i32` (not `f32`) so a naive `[-1]:Float` over the whole struct | ||
| // would misclassify it. Array metadata has to stay bounded to `data`. | ||
|
|
||
| const N: usize = 8; | ||
|
|
||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct PtrArray { | ||
| pub p: *mut [f32; N], | ||
| pub q: f32, | ||
| pub r: f32, | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| #[inline(never)] | ||
| pub unsafe fn copy_ptr_array(a: &PtrArray, b: &mut PtrArray) { | ||
| *b = *a; | ||
| } | ||
|
|
||
| #[autodiff_reverse(d_ptr_array_sum, Duplicated, Active)] | ||
| #[no_mangle] | ||
| #[inline(never)] | ||
| pub fn ptr_array_sum(s: &PtrArray) -> f32 { | ||
| s.q + s.r | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub fn exercise_ptr_array_sum(s: &PtrArray, ds: &mut PtrArray) -> f32 { | ||
| d_ptr_array_sum(s, ds, 1.0) | ||
| } | ||
|
|
||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct InlineArray { | ||
| pub data: [f32; N], | ||
| pub scale: i32, | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| #[inline(never)] | ||
| pub unsafe fn copy_inline_array(a: &InlineArray, b: &mut InlineArray) { | ||
| *b = *a; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wsmoses what typetree do you expect to be generated here, especially for larger N, of say 2048? Currently we only generate it for offset 0 of data and for scale.
Generating 2048 offsets seems extremely wasteful and -1 would be wrong, in the general case were scale and data have different base types (e.g. scale were int).
Afaik Enzyme doesn't directly let frontend generate typetree ranges?
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same place I'm stuck tbh. dense offsets for N=2048 feel pretty wasteful, and -1 is wrong once
scaleisn't float. afaik Enzyme still doesn't give frontends a clean range form, so I'm not trying to "solve" that in this PR.imo we keep the normalize fix + the i32 scale regression here, and wait for @wsmoses on what rustc should emit for big inline arrays. ltm what you'd want that follow-up to look like.