Skip to content
/ rust Public
forked from rust-lang/rust

Commit 1ba9b78

Browse files
authored
Rollup merge of rust-lang#138135 - scottmcm:chaining-ord, r=Mark-Simulacrum
Simplify `PartialOrd` on tuples containing primitives We noticed in rust-lang#133984 (comment) that currently the tuple comparison code, while it [does optimize down](https://github.com/rust-lang/rust/blob/master/tests/codegen/comparison-operators-2-tuple.rs) today, is kinda huge: <https://rust.godbolt.org/z/xqMoeYbhE> This PR changes the tuple code to go through an overridable "chaining" version of the comparison functions, so that for simple things like `(i16, u16)` and `(f32, f32)` (as seen in the new MIR pre-codegen test) we just directly get the ```rust if lhs.0 == rhs.0 { lhs.0 OP rhs.0 } else { lhs.1 OP rhs.1 } ``` version in MIR, rather than emitting a mess for LLVM to have to clean up. Test added in the first commit, so you can see the MIR diff in the second one.
2 parents 9a243cf + 7781346 commit 1ba9b78

File tree

5 files changed

+265
-11
lines changed

5 files changed

+265
-11
lines changed

library/core/src/cmp.rs

+96
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod bytewise;
2929
pub(crate) use bytewise::BytewiseEq;
3030

3131
use self::Ordering::*;
32+
use crate::ops::ControlFlow;
3233

3334
/// Trait for comparisons using the equality operator.
3435
///
@@ -1435,6 +1436,67 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
14351436
fn ge(&self, other: &Rhs) -> bool {
14361437
self.partial_cmp(other).is_some_and(Ordering::is_ge)
14371438
}
1439+
1440+
/// If `self == other`, returns `ControlFlow::Continue(())`.
1441+
/// Otherwise, returns `ControlFlow::Break(self < other)`.
1442+
///
1443+
/// This is useful for chaining together calls when implementing a lexical
1444+
/// `PartialOrd::lt`, as it allows types (like primitives) which can cheaply
1445+
/// check `==` and `<` separately to do rather than needing to calculate
1446+
/// (then optimize out) the three-way `Ordering` result.
1447+
#[inline]
1448+
#[must_use]
1449+
// Added to improve the behaviour of tuples; not necessarily stabilization-track.
1450+
#[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
1451+
#[doc(hidden)]
1452+
fn __chaining_lt(&self, other: &Rhs) -> ControlFlow<bool> {
1453+
default_chaining_impl(self, other, Ordering::is_lt)
1454+
}
1455+
1456+
/// Same as `__chaining_lt`, but for `<=` instead of `<`.
1457+
#[inline]
1458+
#[must_use]
1459+
#[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
1460+
#[doc(hidden)]
1461+
fn __chaining_le(&self, other: &Rhs) -> ControlFlow<bool> {
1462+
default_chaining_impl(self, other, Ordering::is_le)
1463+
}
1464+
1465+
/// Same as `__chaining_lt`, but for `>` instead of `<`.
1466+
#[inline]
1467+
#[must_use]
1468+
#[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
1469+
#[doc(hidden)]
1470+
fn __chaining_gt(&self, other: &Rhs) -> ControlFlow<bool> {
1471+
default_chaining_impl(self, other, Ordering::is_gt)
1472+
}
1473+
1474+
/// Same as `__chaining_lt`, but for `>=` instead of `<`.
1475+
#[inline]
1476+
#[must_use]
1477+
#[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
1478+
#[doc(hidden)]
1479+
fn __chaining_ge(&self, other: &Rhs) -> ControlFlow<bool> {
1480+
default_chaining_impl(self, other, Ordering::is_ge)
1481+
}
1482+
}
1483+
1484+
fn default_chaining_impl<T: ?Sized, U: ?Sized>(
1485+
lhs: &T,
1486+
rhs: &U,
1487+
p: impl FnOnce(Ordering) -> bool,
1488+
) -> ControlFlow<bool>
1489+
where
1490+
T: PartialOrd<U>,
1491+
{
1492+
// It's important that this only call `partial_cmp` once, not call `eq` then
1493+
// one of the relational operators. We don't want to `bcmp`-then-`memcp` a
1494+
// `String`, for example, or similarly for other data structures (#108157).
1495+
match <T as PartialOrd<U>>::partial_cmp(lhs, rhs) {
1496+
Some(Equal) => ControlFlow::Continue(()),
1497+
Some(c) => ControlFlow::Break(p(c)),
1498+
None => ControlFlow::Break(false),
1499+
}
14381500
}
14391501

14401502
/// Derive macro generating an impl of the trait [`PartialOrd`].
@@ -1741,6 +1803,7 @@ where
17411803
mod impls {
17421804
use crate::cmp::Ordering::{self, Equal, Greater, Less};
17431805
use crate::hint::unreachable_unchecked;
1806+
use crate::ops::ControlFlow::{self, Break, Continue};
17441807

17451808
macro_rules! partial_eq_impl {
17461809
($($t:ty)*) => ($(
@@ -1779,6 +1842,35 @@ mod impls {
17791842

17801843
eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
17811844

1845+
macro_rules! chaining_methods_impl {
1846+
($t:ty) => {
1847+
// These implementations are the same for `Ord` or `PartialOrd` types
1848+
// because if either is NAN the `==` test will fail so we end up in
1849+
// the `Break` case and the comparison will correctly return `false`.
1850+
1851+
#[inline]
1852+
fn __chaining_lt(&self, other: &Self) -> ControlFlow<bool> {
1853+
let (lhs, rhs) = (*self, *other);
1854+
if lhs == rhs { Continue(()) } else { Break(lhs < rhs) }
1855+
}
1856+
#[inline]
1857+
fn __chaining_le(&self, other: &Self) -> ControlFlow<bool> {
1858+
let (lhs, rhs) = (*self, *other);
1859+
if lhs == rhs { Continue(()) } else { Break(lhs <= rhs) }
1860+
}
1861+
#[inline]
1862+
fn __chaining_gt(&self, other: &Self) -> ControlFlow<bool> {
1863+
let (lhs, rhs) = (*self, *other);
1864+
if lhs == rhs { Continue(()) } else { Break(lhs > rhs) }
1865+
}
1866+
#[inline]
1867+
fn __chaining_ge(&self, other: &Self) -> ControlFlow<bool> {
1868+
let (lhs, rhs) = (*self, *other);
1869+
if lhs == rhs { Continue(()) } else { Break(lhs >= rhs) }
1870+
}
1871+
};
1872+
}
1873+
17821874
macro_rules! partial_ord_impl {
17831875
($($t:ty)*) => ($(
17841876
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1800,6 +1892,8 @@ mod impls {
18001892
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
18011893
#[inline(always)]
18021894
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1895+
1896+
chaining_methods_impl!($t);
18031897
}
18041898
)*)
18051899
}
@@ -1838,6 +1932,8 @@ mod impls {
18381932
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
18391933
#[inline(always)]
18401934
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1935+
1936+
chaining_methods_impl!($t);
18411937
}
18421938

18431939
#[stable(feature = "rust1", since = "1.0.0")]

library/core/src/tuple.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use crate::cmp::Ordering::{self, *};
44
use crate::marker::{ConstParamTy_, StructuralPartialEq, UnsizedConstParamTy};
5+
use crate::ops::ControlFlow::{Break, Continue};
56

67
// Recursive macro for implementing n-ary tuple functions and operations
78
//
@@ -80,19 +81,19 @@ macro_rules! tuple_impls {
8081
}
8182
#[inline]
8283
fn lt(&self, other: &($($T,)+)) -> bool {
83-
lexical_ord!(lt, Less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
84+
lexical_ord!(lt, __chaining_lt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
8485
}
8586
#[inline]
8687
fn le(&self, other: &($($T,)+)) -> bool {
87-
lexical_ord!(le, Less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
88+
lexical_ord!(le, __chaining_le, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
8889
}
8990
#[inline]
9091
fn ge(&self, other: &($($T,)+)) -> bool {
91-
lexical_ord!(ge, Greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
92+
lexical_ord!(ge, __chaining_ge, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
9293
}
9394
#[inline]
9495
fn gt(&self, other: &($($T,)+)) -> bool {
95-
lexical_ord!(gt, Greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
96+
lexical_ord!(gt, __chaining_gt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
9697
}
9798
}
9899
}
@@ -171,15 +172,16 @@ macro_rules! maybe_tuple_doc {
171172
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, opt_is_lt, a1, b1,
172173
// a2, b2, a3, b3)` (and similarly for `lexical_cmp`)
173174
//
174-
// `$ne_rel` is only used to determine the result after checking that they're
175-
// not equal, so `lt` and `le` can both just use `Less`.
175+
// `$chain_rel` is the chaining method from `PartialOrd` to use for all but the
176+
// final value, to produce better results for simple primitives.
176177
macro_rules! lexical_ord {
177-
($rel: ident, $ne_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
178-
let c = PartialOrd::partial_cmp(&$a, &$b);
179-
if c != Some(Equal) { c == Some($ne_rel) }
180-
else { lexical_ord!($rel, $ne_rel, $($rest_a, $rest_b),+) }
178+
($rel: ident, $chain_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
179+
match PartialOrd::$chain_rel(&$a, &$b) {
180+
Break(val) => val,
181+
Continue(()) => lexical_ord!($rel, $chain_rel, $($rest_a, $rest_b),+),
182+
}
181183
}};
182-
($rel: ident, $ne_rel: ident, $a:expr, $b:expr) => {
184+
($rel: ident, $chain_rel: ident, $a:expr, $b:expr) => {
183185
// Use the specific method for the last element
184186
PartialOrd::$rel(&$a, &$b)
185187
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// MIR for `demo_ge_partial` after PreCodegen
2+
3+
fn demo_ge_partial(_1: &(f32, f32), _2: &(f32, f32)) -> bool {
4+
debug a => _1;
5+
debug b => _2;
6+
let mut _0: bool;
7+
scope 1 (inlined std::cmp::impls::<impl PartialOrd for &(f32, f32)>::ge) {
8+
scope 2 (inlined core::tuple::<impl PartialOrd for (f32, f32)>::ge) {
9+
let mut _7: std::ops::ControlFlow<bool>;
10+
let _8: bool;
11+
scope 3 {
12+
}
13+
scope 4 (inlined std::cmp::impls::<impl PartialOrd for f32>::__chaining_ge) {
14+
let mut _3: f32;
15+
let mut _4: f32;
16+
let mut _5: bool;
17+
let mut _6: bool;
18+
scope 5 {
19+
}
20+
}
21+
scope 6 (inlined std::cmp::impls::<impl PartialOrd for f32>::ge) {
22+
let mut _9: f32;
23+
let mut _10: f32;
24+
}
25+
}
26+
}
27+
28+
bb0: {
29+
StorageLive(_7);
30+
StorageLive(_3);
31+
StorageLive(_4);
32+
_3 = copy ((*_1).0: f32);
33+
_4 = copy ((*_2).0: f32);
34+
StorageLive(_5);
35+
_5 = Eq(copy _3, copy _4);
36+
switchInt(move _5) -> [0: bb1, otherwise: bb2];
37+
}
38+
39+
bb1: {
40+
StorageLive(_6);
41+
_6 = Ge(copy _3, copy _4);
42+
_7 = ControlFlow::<bool>::Break(move _6);
43+
StorageDead(_6);
44+
StorageDead(_5);
45+
StorageDead(_4);
46+
StorageDead(_3);
47+
_8 = copy ((_7 as Break).0: bool);
48+
_0 = copy _8;
49+
goto -> bb3;
50+
}
51+
52+
bb2: {
53+
StorageDead(_5);
54+
StorageDead(_4);
55+
StorageDead(_3);
56+
StorageLive(_9);
57+
_9 = copy ((*_1).1: f32);
58+
StorageLive(_10);
59+
_10 = copy ((*_2).1: f32);
60+
_0 = Ge(move _9, move _10);
61+
StorageDead(_10);
62+
StorageDead(_9);
63+
goto -> bb3;
64+
}
65+
66+
bb3: {
67+
StorageDead(_7);
68+
return;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// MIR for `demo_le_total` after PreCodegen
2+
3+
fn demo_le_total(_1: &(u16, i16), _2: &(u16, i16)) -> bool {
4+
debug a => _1;
5+
debug b => _2;
6+
let mut _0: bool;
7+
scope 1 (inlined std::cmp::impls::<impl PartialOrd for &(u16, i16)>::le) {
8+
scope 2 (inlined core::tuple::<impl PartialOrd for (u16, i16)>::le) {
9+
let mut _7: std::ops::ControlFlow<bool>;
10+
let _8: bool;
11+
scope 3 {
12+
}
13+
scope 4 (inlined std::cmp::impls::<impl PartialOrd for u16>::__chaining_le) {
14+
let mut _3: u16;
15+
let mut _4: u16;
16+
let mut _5: bool;
17+
let mut _6: bool;
18+
scope 5 {
19+
}
20+
}
21+
scope 6 (inlined std::cmp::impls::<impl PartialOrd for i16>::le) {
22+
let mut _9: i16;
23+
let mut _10: i16;
24+
}
25+
}
26+
}
27+
28+
bb0: {
29+
StorageLive(_7);
30+
StorageLive(_3);
31+
StorageLive(_4);
32+
_3 = copy ((*_1).0: u16);
33+
_4 = copy ((*_2).0: u16);
34+
StorageLive(_5);
35+
_5 = Eq(copy _3, copy _4);
36+
switchInt(move _5) -> [0: bb1, otherwise: bb2];
37+
}
38+
39+
bb1: {
40+
StorageLive(_6);
41+
_6 = Le(copy _3, copy _4);
42+
_7 = ControlFlow::<bool>::Break(move _6);
43+
StorageDead(_6);
44+
StorageDead(_5);
45+
StorageDead(_4);
46+
StorageDead(_3);
47+
_8 = copy ((_7 as Break).0: bool);
48+
_0 = copy _8;
49+
goto -> bb3;
50+
}
51+
52+
bb2: {
53+
StorageDead(_5);
54+
StorageDead(_4);
55+
StorageDead(_3);
56+
StorageLive(_9);
57+
_9 = copy ((*_1).1: i16);
58+
StorageLive(_10);
59+
_10 = copy ((*_2).1: i16);
60+
_0 = Le(move _9, move _10);
61+
StorageDead(_10);
62+
StorageDead(_9);
63+
goto -> bb3;
64+
}
65+
66+
bb3: {
67+
StorageDead(_7);
68+
return;
69+
}
70+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ compile-flags: -O -Zmir-opt-level=2 -Cdebuginfo=0
2+
//@ needs-unwind
3+
4+
#![crate_type = "lib"]
5+
6+
// EMIT_MIR tuple_ord.demo_le_total.PreCodegen.after.mir
7+
pub fn demo_le_total(a: &(u16, i16), b: &(u16, i16)) -> bool {
8+
// CHECK-LABEL: demo_le_total
9+
a <= b
10+
}
11+
12+
// EMIT_MIR tuple_ord.demo_ge_partial.PreCodegen.after.mir
13+
pub fn demo_ge_partial(a: &(f32, f32), b: &(f32, f32)) -> bool {
14+
// CHECK-LABEL: demo_ge_partial
15+
a >= b
16+
}

0 commit comments

Comments
 (0)