-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Add a layout_of_val intrinsic for when you want both size_of_val+align_of_val
#152867
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
scottmcm
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
scottmcm:layout_of_val
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
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
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,76 @@ | ||
| //@ compile-flags: -Copt-level=3 -C no-prepopulate-passes -Z inline-mir | ||
| //@ only-64bit (so I don't need to worry about usize) | ||
| //@ needs-deterministic-layouts | ||
|
|
||
| // Note that the layout algorithm currently puts the align before the size, | ||
| // because the *type* for the size doesn't have a niche. This test may need | ||
| // to be updated if the in-memory field order of `Layout` ever changes. | ||
|
|
||
| #![crate_type = "lib"] | ||
| #![feature(core_intrinsics)] | ||
|
|
||
| use std::alloc::Layout; | ||
| use std::intrinsics::layout_of_val; | ||
|
|
||
| // CHECK-LABEL: @thin_metadata( | ||
| #[no_mangle] | ||
| pub unsafe fn thin_metadata(ptr: *const [u32; 2]) -> Layout { | ||
| // CHECK-NOT: alloca | ||
| // CHECK: ret { i64, i64 } { i64 4, i64 8 } | ||
| layout_of_val(ptr) | ||
| } | ||
|
|
||
| // CHECK-LABEL: @slice_metadata(ptr noundef %ptr.0, i64 noundef %ptr.1) | ||
| #[no_mangle] | ||
| pub unsafe fn slice_metadata(ptr: *const [u32]) -> Layout { | ||
| // CHECK: [[LAYOUT:%.+]] = alloca [16 x i8], align 8 | ||
| // CHECK-NOT: load | ||
| // CHECK-NOT: store | ||
| // CHECK: [[BYTES:%.+]] = mul nuw nsw i64 %ptr.1, 4 | ||
| // CHECK-NEXT: store i64 4, ptr [[LAYOUT]], align 8 | ||
| // CHECK-NEXT: [[SIZEP:%.+]] = getelementptr inbounds i8, ptr [[LAYOUT]], i64 8 | ||
| // CHECK-NEXT: store i64 [[BYTES]], ptr [[SIZEP]], align 8 | ||
| // CHECK-NOT: store | ||
| layout_of_val(ptr) | ||
| } | ||
|
|
||
| pub struct WithTail<T: ?Sized>([u32; 3], T); | ||
|
|
||
| // CHECK-LABEL: @dst_metadata | ||
| // CHECK-SAME: (ptr noundef %ptr.0, ptr{{.+}}%ptr.1) | ||
| #[no_mangle] | ||
| pub unsafe fn dst_metadata(ptr: *const WithTail<dyn std::fmt::Debug>) -> Layout { | ||
| // CHECK: [[LAYOUT:%.+]] = alloca [16 x i8], align 8 | ||
| // CHECK-NOT: load | ||
| // CHECK-NOT: store | ||
| // CHECK: [[DST_SIZEP:%.+]] = getelementptr inbounds i8, ptr %ptr.1, i64 8 | ||
| // CHECK-NEXT: [[DST_SIZE:%.+]] = load i64, ptr [[DST_SIZEP]], align 8, | ||
| // CHECK-SAME: !range [[SIZE_RANGE:.+]], !invariant.load | ||
| // CHECK-NEXT: [[DST_ALIGNP:%.+]] = getelementptr inbounds i8, ptr %ptr.1, i64 16 | ||
| // CHECK-NEXT: [[DST_ALIGN:%.+]] = load i64, ptr [[DST_ALIGNP]], align 8, | ||
| // CHECK-SAME: !range [[ALIGN_RANGE:!.+]], !invariant.load | ||
|
|
||
| // CHECK-NEXT: [[STRUCT_MORE:%.+]] = icmp ugt i64 4, [[DST_ALIGN]] | ||
| // CHECK-NEXT: [[ALIGN:%.+]] = select i1 [[STRUCT_MORE]], i64 4, i64 [[DST_ALIGN]] | ||
|
|
||
| // CHECK-NEXT: [[MINSIZE:%.+]] = add nuw nsw i64 12, [[DST_SIZE]] | ||
| // CHECK-NEXT: [[ALIGN_M1:%.+]] = sub i64 [[ALIGN]], 1 | ||
| // CHECK-NEXT: [[MAXSIZE:%.+]] = add i64 [[MINSIZE]], [[ALIGN_M1]] | ||
| // CHECK-NEXT: [[ALIGN_NEG:%.+]] = sub i64 0, [[ALIGN]] | ||
| // CHECK-NEXT: [[SIZE:%.+]] = and i64 [[MAXSIZE]], [[ALIGN_NEG]] | ||
|
|
||
| // CHECK-NEXT: store i64 [[ALIGN]], ptr [[LAYOUT]], align 8 | ||
| // CHECK-NEXT: [[LAYOUT_SIZEP:%.+]] = getelementptr inbounds i8, ptr [[LAYOUT]], i64 8 | ||
| // CHECK-NEXT: store i64 [[SIZE]], ptr [[LAYOUT_SIZEP]], align 8 | ||
|
|
||
| // CHECK-NOT: store | ||
| // CHECK: load i64, {{.+}} !range [[ALIGNMENT_RANGE:!.+]], | ||
| // CHECK-NOT: store | ||
| layout_of_val(ptr) | ||
| } | ||
|
|
||
| // CHECK-LABEL: declare | ||
|
|
||
| // CHECK: [[ALIGNMENT_RANGE]] = !{i64 1, i64 -[[#0x7FFFFFFFFFFFFFFF]]} | ||
| // CHECK: [[SIZE_RANGE]] = !{i64 0, i64 -[[#0x8000000000000000]]} | ||
| // CHECK: [[ALIGN_RANGE]] = !{i64 1, i64 [[#0x20000001]]} |
22 changes: 22 additions & 0 deletions
22
...mplify/align_or_size_of_sized_val.layout_of_val_sized.InstSimplify-after-simplifycfg.diff
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,22 @@ | ||
| - // MIR for `layout_of_val_sized` before InstSimplify-after-simplifycfg | ||
| + // MIR for `layout_of_val_sized` after InstSimplify-after-simplifycfg | ||
|
|
||
| fn layout_of_val_sized(_1: &T) -> Layout { | ||
| debug val => _1; | ||
| let mut _0: std::alloc::Layout; | ||
| let mut _2: *const T; | ||
|
|
||
| bb0: { | ||
| StorageLive(_2); | ||
| _2 = &raw const (*_1); | ||
| - _0 = layout_of_val::<T>(move _2) -> [return: bb1, unwind unreachable]; | ||
| + _0 = const <T as std::mem::SizedTypeProperties>::LAYOUT; | ||
| + goto -> bb1; | ||
| } | ||
|
|
||
| bb1: { | ||
| StorageDead(_2); | ||
| return; | ||
| } | ||
| } | ||
|
|
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
Oops, something went wrong.
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.
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.
For a follow-up: now that we have
layout_of_valas an intrinsic, do we keepsize_of_valandalign_of_valas intrinsics? Or do we implement them in terms oflayout_of_val?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.
We may want to keep them, since
align_of_val::<[T]>(...)can be simplified toalign_of::<T>()(https://github.com/rust-lang/rust/blob/main/compiler/rustc_mir_transform/src/instsimplify.rs#L247 ), butlayout_of_val::<[T]>(...)can't itself be simplified, and it might be harder to detect that only the alignment is used.