-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |
| issue = "none" | ||
| )] | ||
|
|
||
| use crate::alloc::Layout; | ||
| use crate::ffi::{VaArgSafe, VaList}; | ||
| use crate::marker::{ConstParamTy, DiscriminantKind, PointeeSized, Tuple}; | ||
| use crate::{mem, ptr}; | ||
|
|
@@ -2887,6 +2888,26 @@ pub const fn type_id_vtable( | |
| ) | ||
| } | ||
|
|
||
| /// The size and alignment of the referenced value in bytes. | ||
| /// | ||
| /// The stabilized version of this intrinsic is [`Layout::for_value_raw`]. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// See [`Layout::for_value_raw`] for safety conditions. | ||
| #[rustc_nounwind] | ||
| #[unstable(feature = "core_intrinsics", issue = "none")] | ||
| #[rustc_intrinsic] | ||
| // This adds no semantics or UB atop just calling `size_of_val`+`align_of_val`. | ||
| #[miri::intrinsic_fallback_is_spec] | ||
| pub const unsafe fn layout_of_val<T: ?Sized>(ptr: *const T) -> Layout { | ||
| // SAFETY: we pass along the prerequisites of these functions to the caller | ||
| let (size, align) = unsafe { (size_of_val(ptr), align_of_val(ptr)) }; | ||
| // SAFETY: The size and alignment of a valid allocation (or type) | ||
| // always meet the requirements of `Layout`. | ||
| unsafe { Layout::from_size_align_unchecked(size, align) } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a follow-up: now that we have
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to keep them, since
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting question that maybe |
||
|
|
||
| /// Compute the type information of a concrete type. | ||
| /// It can only be called at compile time, the backends do | ||
| /// not implement it. | ||
|
|
||
| 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]]} |
| 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; | ||
| } | ||
| } | ||
|
|
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.
Returning a library type from an intrinsic is a kind of layering violation. Sometimes it's necessary, but here it seems easier to just return a tuple?
View changes since the review