Skip to content

Commit 2136250

Browse files
Rollup merge of #161275 - bushrat011899:variadic_min_refactor, r=scottmcm
Refactor `core::cmp::{smallest, largest}` & add `mir-opt` test Tracking Issue: #160728 # Description Based on [this](#160687 (comment)) feedback from the initial implementation, I've added a `mir-opt` test to highlight the amount of MIR added by using a variadic `min` implementation. ~~To my knowledge, there isn't a way to actually test the MIR generated by the actual implementation in `core`, so I've replicated a smaller representative sample in a test instead. Please see [GodBolt](https://godbolt.org/z/1qGzboeo3) for a more detailed comparison between the (unoptimised) MIR for the current and splatted versions of `min` (`max` omitted since it's identical).~~ ~~In short, the splat version does produce more MIR, especially for the additional trait implementations for larger tuple sizes. However, the added MIR almost exclusively comes from the indirection of calling a method on a trait, and not specifically the `#[rustc_splat]` variadic implementation. Because of this, it may be worth considering a MIR pass which could inline this kind of static dispatch.~~ Moving to a pre-codegen test with some MIR optimisation has narrowed the gap between `smallest` and `min` to basically nothing (an extra scope from the looks of it). I'm guessing the chained `a.min(b).min(c).min(d)` style (with a lack of explicit temporaries) is something MIR is able to readily optimise! From experimenting with this test, I also found a simpler way to implement `smaller` and `larger`. The most notable difference is instead of passing all arguments through the splat, I've pulled out the first argument so they aren't de/tupled. It improves codegen slightly, but more importantly simplifies the macro implementation. --- ## Notes - No AI tooling of any kind was used during the creation of this PR.
2 parents b255ecd + d0706de commit 2136250

4 files changed

Lines changed: 115 additions & 49 deletions

File tree

‎library/core/src/cmp.rs‎

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,24 +1967,19 @@ macro impl_tuples($($mac:ident,)+) {
19671967
}
19681968

19691969
/// Implementation detail for [`smallest`] and [`largest`].
1970-
/// Marker indicating that `Self` is a tuple where all members are of the same type.
1970+
/// Marker indicating that `Self` is a tuple where all members are of the type `T`.
1971+
/// Cannot be an associated type as we require the empty tuple to implement this trait
1972+
/// for all types `T`.
19711973
#[diagnostic::on_unimplemented(message = "`{Self}` is not a homogeneous tuple")]
19721974
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
19731975
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
1974-
const trait HomogeneousTuple: crate::marker::Tuple {
1975-
/// The type of each item in this tuple.
1976-
type Item;
1977-
}
1976+
const trait HomogeneousTuple<T>: crate::marker::Tuple {}
19781977

19791978
/// Implements [`HomogeneousTuple`] for a provided tuple.
1980-
macro impl_homogeneous_tuple($($($x:ident,)+)?) {
1981-
$(
1982-
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
1983-
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
1984-
const impl<T> HomogeneousTuple for ($(${ignore($x)}T,)+) {
1985-
type Item = T;
1986-
}
1987-
)?
1979+
macro impl_homogeneous_tuple($($x:ident,)*) {
1980+
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
1981+
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
1982+
const impl<T> HomogeneousTuple<T> for ($(${ignore($x)}T,)*) { }
19881983
}
19891984

19901985
impl_tuples! {
@@ -2040,36 +2035,34 @@ impl_tuples! {
20402035
#[expect(private_bounds, reason = "`SmallestArgs` is an internal implementation detail")]
20412036
#[cfg(not(test))] // FIXME: splat interacts poorly with the double linking of `core` in tests
20422037
pub const fn smallest<T: [const] Ord + [const] Destruct>(
2043-
#[rustc_splat] args: impl [const] SmallestArgs<Item = T>,
2038+
v1: T,
2039+
#[rustc_splat] args: impl [const] SmallestArgs<T>,
20442040
) -> T {
2045-
SmallestArgs::smallest(args)
2041+
SmallestArgs::smallest(v1, args)
20462042
}
20472043

20482044
/// Implementation detail for [`smallest`].
20492045
#[diagnostic::on_unimplemented(message = "`{Self}` is not a valid set of arguments for `smallest`")]
20502046
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
20512047
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
2052-
const trait SmallestArgs: HomogeneousTuple {
2048+
const trait SmallestArgs<T>: HomogeneousTuple<T> {
20532049
/// Reduces all elements of a homogeneous tuple to its smallest value.
2054-
fn smallest(self) -> Self::Item;
2050+
fn smallest(v1: T, args: Self) -> T;
20552051
}
20562052

20572053
/// Implements [`SmallestArgs`] for a provided tuple if applicable.
2058-
macro impl_smallest_args($($x:ident, $($($y:ident,)+)?)?) {
2059-
$(
2060-
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
2061-
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
2062-
const impl<T> SmallestArgs for (T, $($(${ignore($y)}T,)+)?)
2063-
$(where T: [const] Destruct + [const] Ord, $(${ignore($y)})+)?
2064-
{
2065-
#[inline]
2066-
fn smallest(self) -> Self::Item {
2067-
let ($x, $($($y,)+)?) = self;
2068-
$($(let $x = $x.min($y);)+)?
2069-
$x
2070-
}
2054+
macro impl_smallest_args($($x:ident,)*) {
2055+
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
2056+
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
2057+
const impl<T> SmallestArgs<T> for ($(${ignore($x)}T,)*)
2058+
where
2059+
T: [const] Destruct + [const] Ord,
2060+
{
2061+
#[inline(always)] // improves unoptimised codegen
2062+
fn smallest(v1: T, ($($x,)*): Self) -> T {
2063+
v1$(.min($x))*
20712064
}
2072-
)?
2065+
}
20732066
}
20742067

20752068
impl_tuples! {
@@ -2125,36 +2118,34 @@ impl_tuples! {
21252118
#[expect(private_bounds, reason = "`LargestArgs` is an internal implementation detail")]
21262119
#[cfg(not(test))] // FIXME: splat interacts poorly with the double linking of `core` in tests
21272120
pub const fn largest<T: [const] Ord + [const] Destruct>(
2128-
#[rustc_splat] args: impl [const] LargestArgs<Item = T>,
2121+
v1: T,
2122+
#[rustc_splat] args: impl [const] LargestArgs<T>,
21292123
) -> T {
2130-
LargestArgs::largest(args)
2124+
LargestArgs::largest(v1, args)
21312125
}
21322126

21332127
/// Implementation detail for [`largest`].
21342128
#[diagnostic::on_unimplemented(message = "`{Self}` is not a valid set of arguments for `largest`")]
21352129
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
21362130
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
2137-
const trait LargestArgs: HomogeneousTuple {
2131+
const trait LargestArgs<T>: HomogeneousTuple<T> {
21382132
/// Reduces all elements of a homogeneous tuple to its largest value.
2139-
fn largest(self) -> Self::Item;
2133+
fn largest(v1: T, args: Self) -> T;
21402134
}
21412135

21422136
/// Implements [`LargestArgs`] for a provided tuple if applicable.
2143-
macro impl_largest_args($($x:ident, $($($y:ident,)+)?)?) {
2144-
$(
2145-
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
2146-
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
2147-
const impl<T> LargestArgs for (T, $($(${ignore($y)}T,)+)?)
2148-
$(where T: [const] Destruct + [const] Ord, $(${ignore($y)})+)?
2149-
{
2150-
#[inline]
2151-
fn largest(self) -> Self::Item {
2152-
let ($x, $($($y,)+)?) = self;
2153-
$($(let $x = $x.max($y);)+)?
2154-
$x
2155-
}
2137+
macro impl_largest_args($($x:ident,)*) {
2138+
#[unstable(feature = "cmp_splat_internals", issue = "160728")]
2139+
#[rustc_const_unstable(feature = "cmp_splat_internals", issue = "160728")]
2140+
const impl<T> LargestArgs<T> for ($(${ignore($x)}T,)*)
2141+
where
2142+
T: [const] Destruct + [const] Ord,
2143+
{
2144+
#[inline(always)] // improves unoptimised codegen
2145+
fn largest(v1: T, ($($x,)*): Self) -> T {
2146+
v1$(.max($x))*
21562147
}
2157-
)?
2148+
}
21582149
}
21592150

21602151
impl_tuples! {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// MIR for `min_direct_u8` after runtime-optimized
2+
3+
fn min_direct_u8(_1: u8, _2: u8) -> u8 {
4+
debug x => _1;
5+
debug y => _2;
6+
let mut _0: u8;
7+
scope 1 (inlined std::cmp::min::<u8>) {
8+
scope 2 (inlined std::cmp::impls::<impl Ord for u8>::min) {
9+
}
10+
}
11+
12+
bb0: {
13+
_0 = integer_min::<u8>(move _1, move _2) -> [return: bb1, unwind unreachable];
14+
}
15+
16+
bb1: {
17+
return;
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// MIR for `min_splatted_u8` after runtime-optimized
2+
3+
fn min_splatted_u8(_1: u8, _2: u8) -> u8 {
4+
debug x => _1;
5+
debug y => _2;
6+
let mut _0: u8;
7+
scope 1 (inlined smallest::<u8, (u8,)>) {
8+
scope 2 (inlined <(u8,) as std::cmp::SmallestArgs<u8>>::smallest) {
9+
scope 3 {
10+
scope 4 (inlined std::cmp::impls::<impl Ord for u8>::min) {
11+
}
12+
}
13+
}
14+
}
15+
16+
bb0: {
17+
_0 = integer_min::<u8>(move _1, move _2) -> [return: bb1, unwind unreachable];
18+
}
19+
20+
bb1: {
21+
return;
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![feature(cmp_splat)]
2+
3+
// Comparing the MIR of a splat-compatible function with it's direct counterpart.
4+
// This test is to ensure MIR optimizations are able to remove the added layers
5+
// of indirection.
6+
7+
// EMIT_MIR cmp_splat.min_direct_u8.runtime-optimized.after.mir
8+
pub fn min_direct_u8(x: u8, y: u8) -> u8 {
9+
// CHECK-LABEL: fn min_direct_u8(
10+
// CHECK: debug x => _1;
11+
// CHECK: debug y => _2;
12+
// CHECK: bb0: {
13+
// CHECK-NEXT: _0 = integer_min::<u8>(move _1, move _2) -> [return: bb1, unwind unreachable];
14+
// CHECK-NEXT: }
15+
// CHECK: bb1: {
16+
// CHECK-NEXT: return;
17+
// CHECK-NEXT: }
18+
std::cmp::min(x, y)
19+
}
20+
21+
// EMIT_MIR cmp_splat.min_splatted_u8.runtime-optimized.after.mir
22+
pub fn min_splatted_u8(x: u8, y: u8) -> u8 {
23+
// CHECK-LABEL: fn min_splatted_u8(
24+
// CHECK: debug x => _1;
25+
// CHECK: debug y => _2;
26+
// CHECK: bb0: {
27+
// CHECK-NEXT: _0 = integer_min::<u8>(move _1, move _2) -> [return: bb1, unwind unreachable];
28+
// CHECK-NEXT: }
29+
// CHECK: bb1: {
30+
// CHECK-NEXT: return;
31+
// CHECK-NEXT: }
32+
std::cmp::smallest(x, y)
33+
}

0 commit comments

Comments
 (0)