-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Stop generating alloca
s & memcmp
for simple short array equality
#85828
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d05eafa
Move the `PartialEq` and `Eq` impls for arrays to a separate file
scottmcm 2456495
Stop generating `alloca`s+`memcmp` for simple array equality
scottmcm b63b2f1
PR feedback
scottmcm 1216353
Implement the raw_eq intrinsic in codegen_cranelift
scottmcm 039a3ba
Add another codegen test, array_eq_zero
scottmcm 3d2869c
PR Feedback: Don't put SSA-only types in `CValue`s
scottmcm 6444f24
Use cranelift's `Type::int` instead of doing the match myself
scottmcm 07fb5ee
Adjust the threshold to look at the ABI, not just the size
scottmcm d064494
Bless a UI test
scottmcm 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N] | ||
where | ||
A: PartialEq<B>, | ||
{ | ||
#[inline] | ||
fn eq(&self, other: &[B; N]) -> bool { | ||
SpecArrayEq::spec_eq(self, other) | ||
} | ||
#[inline] | ||
fn ne(&self, other: &[B; N]) -> bool { | ||
SpecArrayEq::spec_ne(self, other) | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A, B, const N: usize> PartialEq<[B]> for [A; N] | ||
where | ||
A: PartialEq<B>, | ||
{ | ||
#[inline] | ||
fn eq(&self, other: &[B]) -> bool { | ||
self[..] == other[..] | ||
} | ||
#[inline] | ||
fn ne(&self, other: &[B]) -> bool { | ||
self[..] != other[..] | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A, B, const N: usize> PartialEq<[A; N]> for [B] | ||
where | ||
B: PartialEq<A>, | ||
{ | ||
#[inline] | ||
fn eq(&self, other: &[A; N]) -> bool { | ||
self[..] == other[..] | ||
} | ||
#[inline] | ||
fn ne(&self, other: &[A; N]) -> bool { | ||
self[..] != other[..] | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A, B, const N: usize> PartialEq<&[B]> for [A; N] | ||
where | ||
A: PartialEq<B>, | ||
{ | ||
#[inline] | ||
fn eq(&self, other: &&[B]) -> bool { | ||
self[..] == other[..] | ||
} | ||
#[inline] | ||
fn ne(&self, other: &&[B]) -> bool { | ||
self[..] != other[..] | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A, B, const N: usize> PartialEq<[A; N]> for &[B] | ||
where | ||
B: PartialEq<A>, | ||
{ | ||
#[inline] | ||
fn eq(&self, other: &[A; N]) -> bool { | ||
self[..] == other[..] | ||
} | ||
#[inline] | ||
fn ne(&self, other: &[A; N]) -> bool { | ||
self[..] != other[..] | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N] | ||
where | ||
A: PartialEq<B>, | ||
{ | ||
#[inline] | ||
fn eq(&self, other: &&mut [B]) -> bool { | ||
self[..] == other[..] | ||
} | ||
#[inline] | ||
fn ne(&self, other: &&mut [B]) -> bool { | ||
self[..] != other[..] | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B] | ||
where | ||
B: PartialEq<A>, | ||
{ | ||
#[inline] | ||
fn eq(&self, other: &[A; N]) -> bool { | ||
self[..] == other[..] | ||
} | ||
#[inline] | ||
fn ne(&self, other: &[A; N]) -> bool { | ||
self[..] != other[..] | ||
} | ||
} | ||
|
||
// NOTE: some less important impls are omitted to reduce code bloat | ||
// __impl_slice_eq2! { [A; $N], &'b [B; $N] } | ||
// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] } | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<T: Eq, const N: usize> Eq for [T; N] {} | ||
|
||
trait SpecArrayEq<Other, const N: usize>: Sized { | ||
fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool; | ||
fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool; | ||
} | ||
|
||
impl<T: PartialEq<Other>, Other, const N: usize> SpecArrayEq<Other, N> for T { | ||
default fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool { | ||
a[..] == b[..] | ||
} | ||
default fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool { | ||
a[..] != b[..] | ||
} | ||
} | ||
|
||
impl<T: PartialEq<U> + IsRawEqComparable<U>, U, const N: usize> SpecArrayEq<U, N> for T { | ||
#[cfg(bootstrap)] | ||
fn spec_eq(a: &[T; N], b: &[U; N]) -> bool { | ||
a[..] == b[..] | ||
} | ||
#[cfg(not(bootstrap))] | ||
fn spec_eq(a: &[T; N], b: &[U; N]) -> bool { | ||
// SAFETY: This is why `IsRawEqComparable` is an `unsafe trait`. | ||
unsafe { | ||
let b = &*b.as_ptr().cast::<[T; N]>(); | ||
crate::intrinsics::raw_eq(a, b) | ||
} | ||
} | ||
fn spec_ne(a: &[T; N], b: &[U; N]) -> bool { | ||
!Self::spec_eq(a, b) | ||
} | ||
} | ||
|
||
/// `U` exists on here mostly because `min_specialization` didn't let me | ||
/// repeat the `T` type parameter in the above specialization, so instead | ||
/// the `T == U` constraint comes from the impls on this. | ||
/// # Safety | ||
/// - Neither `Self` nor `U` has any padding. | ||
/// - `Self` and `U` have the same layout. | ||
/// - `Self: PartialEq<U>` is byte-wise (this means no floats, among other things) | ||
#[rustc_specialization_trait] | ||
unsafe trait IsRawEqComparable<U> {} | ||
|
||
macro_rules! is_raw_comparable { | ||
($($t:ty),+) => {$( | ||
unsafe impl IsRawEqComparable<$t> for $t {} | ||
)+}; | ||
} | ||
is_raw_comparable!(bool, char, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
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.
The memcmp in
slice::cmp
has a FIXME for the return type, should that go here too?