-
-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Refactor core::cmp::{smallest, largest} & add mir-opt test
#161275
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
bushrat011899
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
bushrat011899:variadic_min_refactor
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
2 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1935,24 +1935,19 @@ macro impl_tuples($($mac:ident,)+) { | |||||
| } | ||||||
|
|
||||||
| /// Implementation detail for [`smallest`] and [`largest`]. | ||||||
| /// Marker indicating that `Self` is a tuple where all members are of the same type. | ||||||
| /// Marker indicating that `Self` is a tuple where all members are of the type `T`. | ||||||
| /// Cannot be an associated type as we require the empty tuple to implement this trait | ||||||
| /// for all types `T`. | ||||||
| #[diagnostic::on_unimplemented(message = "`{Self}` is not a homogeneous tuple")] | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const trait HomogeneousTuple: crate::marker::Tuple { | ||||||
| /// The type of each item in this tuple. | ||||||
| type Item; | ||||||
| } | ||||||
| const trait HomogeneousTuple<T>: crate::marker::Tuple {} | ||||||
|
|
||||||
| /// Implements [`HomogeneousTuple`] for a provided tuple. | ||||||
| macro impl_homogeneous_tuple($($($x:ident,)+)?) { | ||||||
| $( | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const impl<T> HomogeneousTuple for ($(${ignore($x)}T,)+) { | ||||||
| type Item = T; | ||||||
| } | ||||||
| )? | ||||||
| macro impl_homogeneous_tuple($($x:ident,)*) { | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const impl<T> HomogeneousTuple<T> for ($(${ignore($x)}T,)*) { } | ||||||
| } | ||||||
|
|
||||||
| impl_tuples! { | ||||||
|
|
@@ -1971,7 +1966,6 @@ impl_tuples! { | |||||
| /// #![feature(cmp_splat)] | ||||||
| /// use std::cmp; | ||||||
| /// | ||||||
| /// assert_eq!(cmp::smallest(1), 1); | ||||||
| /// assert_eq!(cmp::smallest(1, 2), 1); | ||||||
| /// assert_eq!(cmp::smallest(3, 2, 1), 1); | ||||||
| /// assert_eq!(cmp::smallest(1, 2, 3, 4), 1); | ||||||
|
|
@@ -2008,36 +2002,35 @@ impl_tuples! { | |||||
| #[expect(private_bounds, reason = "`SmallestArgs` is an internal implementation detail")] | ||||||
| #[cfg(not(test))] // FIXME: splat interacts poorly with the double linking of `core` in tests | ||||||
| pub const fn smallest<T: [const] Ord + [const] Destruct>( | ||||||
| #[rustc_splat] args: impl [const] SmallestArgs<Item = T>, | ||||||
| v1: T, | ||||||
| v2: T, | ||||||
| #[rustc_splat] args: impl [const] SmallestArgs<T>, | ||||||
| ) -> T { | ||||||
| SmallestArgs::smallest(args) | ||||||
| SmallestArgs::smallest(v1, v2, args) | ||||||
| } | ||||||
|
|
||||||
| /// Implementation detail for [`smallest`]. | ||||||
| #[diagnostic::on_unimplemented(message = "`{Self}` is not a valid set of arguments for `smallest`")] | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const trait SmallestArgs: HomogeneousTuple { | ||||||
| const trait SmallestArgs<T>: HomogeneousTuple<T> { | ||||||
| /// Reduces all elements of a homogeneous tuple to its smallest value. | ||||||
| fn smallest(self) -> Self::Item; | ||||||
| fn smallest(v1: T, v2: T, args: Self) -> T; | ||||||
| } | ||||||
|
|
||||||
| /// Implements [`SmallestArgs`] for a provided tuple if applicable. | ||||||
| macro impl_smallest_args($($x:ident, $($($y:ident,)+)?)?) { | ||||||
| $( | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const impl<T> SmallestArgs for (T, $($(${ignore($y)}T,)+)?) | ||||||
| $(where T: [const] Destruct + [const] Ord, $(${ignore($y)})+)? | ||||||
| { | ||||||
| #[inline] | ||||||
| fn smallest(self) -> Self::Item { | ||||||
| let ($x, $($($y,)+)?) = self; | ||||||
| $($(let $x = $x.min($y);)+)? | ||||||
| $x | ||||||
| } | ||||||
| macro impl_smallest_args($($x:ident,)*) { | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const impl<T> SmallestArgs<T> for ($(${ignore($x)}T,)*) | ||||||
| where | ||||||
| T: [const] Destruct + [const] Ord, | ||||||
| { | ||||||
| #[inline(always)] // improves unoptimised codegen | ||||||
| fn smallest(v1: T, v2: T, ($($x,)*): Self) -> T { | ||||||
| v1.min(v2)$(.min($x))* | ||||||
|
Member
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. ...and having this be
Suggested change
without needing the separate detupling looks really elegant. |
||||||
| } | ||||||
| )? | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl_tuples! { | ||||||
|
|
@@ -2056,7 +2049,6 @@ impl_tuples! { | |||||
| /// #![feature(cmp_splat)] | ||||||
| /// use std::cmp; | ||||||
| /// | ||||||
| /// assert_eq!(cmp::largest(1), 1); | ||||||
| /// assert_eq!(cmp::largest(1, 2), 2); | ||||||
| /// assert_eq!(cmp::largest(3, 2, 1), 3); | ||||||
| /// assert_eq!(cmp::largest(1, 2, 3, 4), 4); | ||||||
|
|
@@ -2093,36 +2085,35 @@ impl_tuples! { | |||||
| #[expect(private_bounds, reason = "`LargestArgs` is an internal implementation detail")] | ||||||
| #[cfg(not(test))] // FIXME: splat interacts poorly with the double linking of `core` in tests | ||||||
| pub const fn largest<T: [const] Ord + [const] Destruct>( | ||||||
| #[rustc_splat] args: impl [const] LargestArgs<Item = T>, | ||||||
| v1: T, | ||||||
| v2: T, | ||||||
| #[rustc_splat] args: impl [const] LargestArgs<T>, | ||||||
| ) -> T { | ||||||
| LargestArgs::largest(args) | ||||||
| LargestArgs::largest(v1, v2, args) | ||||||
| } | ||||||
|
|
||||||
| /// Implementation detail for [`largest`]. | ||||||
| #[diagnostic::on_unimplemented(message = "`{Self}` is not a valid set of arguments for `largest`")] | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const trait LargestArgs: HomogeneousTuple { | ||||||
| const trait LargestArgs<T>: HomogeneousTuple<T> { | ||||||
| /// Reduces all elements of a homogeneous tuple to its largest value. | ||||||
| fn largest(self) -> Self::Item; | ||||||
| fn largest(v1: T, v2: T, args: Self) -> T; | ||||||
| } | ||||||
|
|
||||||
| /// Implements [`LargestArgs`] for a provided tuple if applicable. | ||||||
| macro impl_largest_args($($x:ident, $($($y:ident,)+)?)?) { | ||||||
| $( | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const impl<T> LargestArgs for (T, $($(${ignore($y)}T,)+)?) | ||||||
| $(where T: [const] Destruct + [const] Ord, $(${ignore($y)})+)? | ||||||
| { | ||||||
| #[inline] | ||||||
| fn largest(self) -> Self::Item { | ||||||
| let ($x, $($($y,)+)?) = self; | ||||||
| $($(let $x = $x.max($y);)+)? | ||||||
| $x | ||||||
| } | ||||||
| macro impl_largest_args($($x:ident,)*) { | ||||||
| #[unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| #[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")] | ||||||
| const impl<T> LargestArgs<T> for ($(${ignore($x)}T,)*) | ||||||
| where | ||||||
| T: [const] Destruct + [const] Ord, | ||||||
| { | ||||||
| #[inline(always)] // improves unoptimised codegen | ||||||
| fn largest(v1: T, v2: T, ($($x,)*): Self) -> T { | ||||||
| v1.max(v2)$(.max($x))* | ||||||
| } | ||||||
| )? | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl_tuples! { | ||||||
|
|
||||||
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,51 @@ | ||
| // MIR for `min_direct` after built | ||
|
|
||
| fn min_direct(_1: T, _2: T) -> T { | ||
| debug v1 => _1; | ||
| debug v2 => _2; | ||
| let mut _0: T; | ||
| let mut _3: T; | ||
| let mut _4: T; | ||
|
|
||
| bb0: { | ||
| StorageLive(_3); | ||
| _3 = move _1; | ||
| StorageLive(_4); | ||
| _4 = move _2; | ||
| _0 = <T as Ord>::min(move _3, move _4) -> [return: bb1, unwind: bb4]; | ||
| } | ||
|
|
||
| bb1: { | ||
| StorageDead(_4); | ||
| StorageDead(_3); | ||
| drop(_2) -> [return: bb2, unwind: bb7]; | ||
| } | ||
|
|
||
| bb2: { | ||
| drop(_1) -> [return: bb3, unwind: bb8]; | ||
| } | ||
|
|
||
| bb3: { | ||
| return; | ||
| } | ||
|
|
||
| bb4 (cleanup): { | ||
| drop(_4) -> [return: bb5, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb5 (cleanup): { | ||
| drop(_3) -> [return: bb6, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb6 (cleanup): { | ||
| drop(_2) -> [return: bb7, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb7 (cleanup): { | ||
| drop(_1) -> [return: bb8, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb8 (cleanup): { | ||
| resume; | ||
| } | ||
| } |
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,27 @@ | ||
| // MIR for `min_direct_u8` after built | ||
|
|
||
| fn min_direct_u8(_1: u8, _2: u8) -> u8 { | ||
| debug x => _1; | ||
| debug y => _2; | ||
| let mut _0: u8; | ||
| let mut _3: u8; | ||
| let mut _4: u8; | ||
|
|
||
| bb0: { | ||
| StorageLive(_3); | ||
| _3 = copy _1; | ||
| StorageLive(_4); | ||
| _4 = copy _2; | ||
| _0 = min_direct::<u8>(move _3, move _4) -> [return: bb1, unwind: bb2]; | ||
| } | ||
|
|
||
| bb1: { | ||
| StorageDead(_4); | ||
| StorageDead(_3); | ||
| return; | ||
| } | ||
|
|
||
| bb2 (cleanup): { | ||
| resume; | ||
| } | ||
| } |
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,68 @@ | ||
| // MIR for `min_splatted` after built | ||
|
|
||
| fn min_splatted(_1: T, _2: T, _3: impl MinArgs<T>) -> T { | ||
| debug v1 => _1; | ||
| debug v2 => _2; | ||
| debug args => _3; | ||
| let mut _0: T; | ||
| let mut _4: T; | ||
| let mut _5: T; | ||
| let mut _6: impl MinArgs<T>; | ||
|
|
||
| bb0: { | ||
| StorageLive(_4); | ||
| _4 = move _1; | ||
| StorageLive(_5); | ||
| _5 = move _2; | ||
| StorageLive(_6); | ||
| _6 = move _3; | ||
| _0 = <impl MinArgs<T> as MinArgs<T>>::min_splatted_inner(move _4, move _5, move _6) -> [return: bb1, unwind: bb5]; | ||
| } | ||
|
|
||
| bb1: { | ||
| StorageDead(_6); | ||
| StorageDead(_5); | ||
| StorageDead(_4); | ||
| drop(_3) -> [return: bb2, unwind: bb9]; | ||
| } | ||
|
|
||
| bb2: { | ||
| drop(_2) -> [return: bb3, unwind: bb10]; | ||
| } | ||
|
|
||
| bb3: { | ||
| drop(_1) -> [return: bb4, unwind: bb11]; | ||
| } | ||
|
|
||
| bb4: { | ||
| return; | ||
| } | ||
|
|
||
| bb5 (cleanup): { | ||
| drop(_6) -> [return: bb6, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb6 (cleanup): { | ||
| drop(_5) -> [return: bb7, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb7 (cleanup): { | ||
| drop(_4) -> [return: bb8, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb8 (cleanup): { | ||
| drop(_3) -> [return: bb9, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb9 (cleanup): { | ||
| drop(_2) -> [return: bb10, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb10 (cleanup): { | ||
| drop(_1) -> [return: bb11, unwind terminate(cleanup)]; | ||
| } | ||
|
|
||
| bb11 (cleanup): { | ||
| resume; | ||
| } | ||
| } |
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,31 @@ | ||
| // MIR for `min_splatted_u8` after built | ||
|
|
||
| fn min_splatted_u8(_1: u8, _2: u8) -> u8 { | ||
| debug x => _1; | ||
| debug y => _2; | ||
| let mut _0: u8; | ||
| let mut _3: u8; | ||
| let mut _4: u8; | ||
| let mut _5: (); | ||
|
|
||
| bb0: { | ||
| StorageLive(_3); | ||
| _3 = copy _1; | ||
| StorageLive(_4); | ||
| _4 = copy _2; | ||
| StorageLive(_5); | ||
| _5 = (); | ||
| _0 = min_splatted::<u8, ()>(move _3, move _4, move _5) -> [return: bb1, unwind: bb2]; | ||
| } | ||
|
|
||
| bb1: { | ||
| StorageDead(_5); | ||
| StorageDead(_4); | ||
| StorageDead(_3); | ||
| return; | ||
| } | ||
|
|
||
| bb2 (cleanup): { | ||
| resume; | ||
| } | ||
| } |
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,44 @@ | ||
| //@ skip-filecheck | ||
|
|
||
| #![feature(splat)] | ||
| #![feature(tuple_trait)] | ||
|
|
||
| // Comparing the MIR of a splat-compatible function with it's direct counterpart. | ||
| // It's expected that there will be additional MIR generated due to the layer of | ||
| // indirection added by a trait (MinArgs). This test tracks that overhead. | ||
|
|
||
| use std::marker::Tuple; | ||
|
|
||
| // EMIT_MIR cmp_splat.min_direct_u8.built.after.mir | ||
| pub fn min_direct_u8(x: u8, y: u8) -> u8 { | ||
| min_direct(x, y) | ||
| } | ||
|
|
||
| // EMIT_MIR cmp_splat.min_splatted_u8.built.after.mir | ||
| pub fn min_splatted_u8(x: u8, y: u8) -> u8 { | ||
| min_splatted(x, y) | ||
| } | ||
|
|
||
| // EMIT_MIR cmp_splat.min_direct.built.after.mir | ||
| #[inline] | ||
| pub fn min_direct<T: Ord>(v1: T, v2: T) -> T { | ||
| v1.min(v2) | ||
| } | ||
|
|
||
| // EMIT_MIR cmp_splat.min_splatted.built.after.mir | ||
| #[inline] | ||
| pub fn min_splatted<T: Ord>(v1: T, v2: T, #[rustc_splat] args: impl MinArgs<T>) -> T { | ||
| MinArgs::min_splatted_inner(v1, v2, args) | ||
| } | ||
|
|
||
| trait MinArgs<T>: Tuple { | ||
| fn min_splatted_inner(v1: T, v2: T, args: Self) -> T; | ||
| } | ||
|
|
||
| // EMIT_MIR cmp_splat.{impl#0}-min_splatted_inner.built.after.mir | ||
| impl<T: Ord> MinArgs<T> for () { | ||
| #[inline(always)] | ||
| fn min_splatted_inner(v1: T, v2: T, (): Self) -> T { | ||
| v1.min(v2) | ||
| } | ||
| } |
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.
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.
I didn't know this worked with
splat; cool!My instinct would be to keep the 1-parameter version working, but to do the middle-ground here were it emphasizes that the 0-parameter version doesn't exist without understanding the trait impls. (If in future this could be
#[rustc_splat] args: [T; N], for example, that'd be pretty sweet.)So maybe something like
instead?
View changes since the review