-
Notifications
You must be signed in to change notification settings - Fork 13.3k
transmute
should also assume non-null pointers
#136735
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//@ compile-flags: -Copt-level=3 | ||
//@ needs-deterministic-layouts (opposite scalar pair orders breaks it) | ||
#![crate_type = "lib"] | ||
|
||
type Demo = [u8; 3]; | ||
|
@@ -7,7 +8,40 @@ type Demo = [u8; 3]; | |
#[no_mangle] | ||
pub fn slice_iter_len_eq_zero(y: std::slice::Iter<'_, Demo>) -> bool { | ||
// CHECK-NOT: sub | ||
// CHECK: %[[RET:.+]] = icmp eq ptr {{%1|%0}}, {{%1|%0}} | ||
// CHECK: %[[RET:.+]] = icmp eq ptr {{%y.0, %y.1|%y.1, %y.0}} | ||
// CHECK: ret i1 %[[RET]] | ||
y.len() == 0 | ||
} | ||
|
||
// CHECK-LABEL: @slice_iter_len_eq_zero_ref | ||
#[no_mangle] | ||
pub fn slice_iter_len_eq_zero_ref(y: &mut std::slice::Iter<'_, Demo>) -> bool { | ||
// CHECK-NOT: sub | ||
// CHECK: %[[A:.+]] = load ptr | ||
// CHECK-SAME: !nonnull | ||
// CHECK: %[[B:.+]] = load ptr | ||
// CHECK-SAME: !nonnull | ||
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. annot: this now goes through the transmute instead of the load-as-nonnull, but still ends up getting the |
||
// CHECK: %[[RET:.+]] = icmp eq ptr %[[A]], %[[B]] | ||
// CHECK: ret i1 %[[RET]] | ||
y.len() == 0 | ||
} | ||
|
||
struct MyZST; | ||
|
||
// CHECK-LABEL: @slice_zst_iter_len_eq_zero | ||
#[no_mangle] | ||
pub fn slice_zst_iter_len_eq_zero(y: std::slice::Iter<'_, MyZST>) -> bool { | ||
// CHECK: %[[RET:.+]] = icmp eq ptr %y.1, null | ||
// CHECK: ret i1 %[[RET]] | ||
y.len() == 0 | ||
} | ||
|
||
// CHECK-LABEL: @slice_zst_iter_len_eq_zero_ref | ||
#[no_mangle] | ||
pub fn slice_zst_iter_len_eq_zero_ref(y: &mut std::slice::Iter<'_, MyZST>) -> bool { | ||
// CHECK: %[[LEN:.+]] = load ptr | ||
// CHECK-NOT: !nonnull | ||
// CHECK: %[[RET:.+]] = icmp eq ptr %[[LEN]], null | ||
// CHECK: ret i1 %[[RET]] | ||
y.len() == 0 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,73 +4,67 @@ fn slice_iter_generic_is_empty(_1: &std::slice::Iter<'_, T>) -> bool { | |
debug it => _1; | ||
let mut _0: bool; | ||
scope 1 (inlined <std::slice::Iter<'_, T> as ExactSizeIterator>::is_empty) { | ||
let mut _2: *const *const T; | ||
let mut _3: *const std::ptr::NonNull<T>; | ||
let mut _8: *const T; | ||
let mut _2: *const T; | ||
let mut _7: *const T; | ||
Comment on lines
-7
to
+8
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. Really not that substantial a difference, but saved a local and means it no longer has the pointer-to-pointer types. |
||
scope 2 { | ||
let _4: std::ptr::NonNull<T>; | ||
let _9: usize; | ||
let _3: std::ptr::NonNull<T>; | ||
let _8: usize; | ||
scope 3 { | ||
} | ||
scope 4 { | ||
scope 8 (inlined <NonNull<T> as PartialEq>::eq) { | ||
let mut _5: std::ptr::NonNull<T>; | ||
scope 7 (inlined <NonNull<T> as PartialEq>::eq) { | ||
let mut _4: std::ptr::NonNull<T>; | ||
let mut _5: *mut T; | ||
let mut _6: *mut T; | ||
let mut _7: *mut T; | ||
scope 9 (inlined NonNull::<T>::as_ptr) { | ||
scope 8 (inlined NonNull::<T>::as_ptr) { | ||
} | ||
scope 10 (inlined NonNull::<T>::as_ptr) { | ||
scope 9 (inlined NonNull::<T>::as_ptr) { | ||
} | ||
} | ||
} | ||
scope 5 (inlined std::ptr::const_ptr::<impl *const T>::addr) { | ||
scope 6 (inlined std::ptr::const_ptr::<impl *const T>::cast::<()>) { | ||
} | ||
} | ||
scope 7 (inlined std::ptr::const_ptr::<impl *const *const T>::cast::<NonNull<T>>) { | ||
} | ||
} | ||
} | ||
|
||
bb0: { | ||
StorageLive(_9); | ||
StorageLive(_8); | ||
StorageLive(_4); | ||
StorageLive(_7); | ||
StorageLive(_3); | ||
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2]; | ||
} | ||
|
||
bb1: { | ||
StorageLive(_3); | ||
StorageLive(_2); | ||
_2 = &raw const ((*_1).1: *const T); | ||
_3 = copy _2 as *const std::ptr::NonNull<T> (PtrToPtr); | ||
_2 = copy ((*_1).1: *const T); | ||
_3 = move _2 as std::ptr::NonNull<T> (Transmute); | ||
StorageDead(_2); | ||
_4 = copy (*_3); | ||
StorageDead(_3); | ||
StorageLive(_6); | ||
StorageLive(_5); | ||
_5 = copy ((*_1).0: std::ptr::NonNull<T>); | ||
_6 = copy _5 as *mut T (Transmute); | ||
StorageDead(_5); | ||
StorageLive(_7); | ||
_7 = copy _4 as *mut T (Transmute); | ||
_0 = Eq(move _6, move _7); | ||
StorageDead(_7); | ||
StorageLive(_4); | ||
_4 = copy ((*_1).0: std::ptr::NonNull<T>); | ||
_5 = copy _4 as *mut T (Transmute); | ||
StorageDead(_4); | ||
StorageLive(_6); | ||
_6 = copy _3 as *mut T (Transmute); | ||
_0 = Eq(move _5, move _6); | ||
StorageDead(_6); | ||
StorageDead(_5); | ||
goto -> bb3; | ||
} | ||
|
||
bb2: { | ||
_8 = copy ((*_1).1: *const T); | ||
_9 = copy _8 as usize (Transmute); | ||
_0 = Eq(copy _9, const 0_usize); | ||
_7 = copy ((*_1).1: *const T); | ||
_8 = copy _7 as usize (Transmute); | ||
_0 = Eq(copy _8, const 0_usize); | ||
goto -> bb3; | ||
} | ||
|
||
bb3: { | ||
StorageDead(_4); | ||
StorageDead(_3); | ||
StorageDead(_7); | ||
StorageDead(_8); | ||
StorageDead(_9); | ||
return; | ||
} | ||
} |
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.
Would it make sense/work to use
NonNull::new_unchecked
here and make the body of that use a transmute instead?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.
In general I would like to move
NonNull
to using transmutes.But in this specific case I really don't want to do that, because it adds a UbCheck which would then have major impact on perf because of just how critical
next
is.(Though maybe after #136771, once the super-critical methods aren't using this helper macro any more, it could be worth trying.)