Skip to content

Commit 61f55d2

Browse files
authored
Merge d6c3e89 into a7fc463
2 parents a7fc463 + d6c3e89 commit 61f55d2

File tree

5 files changed

+254
-11
lines changed

5 files changed

+254
-11
lines changed

library/core/src/cmp.rs

+84
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::{self, Break, Continue};
3233

3334
/// Trait for comparisons using the equality operator.
3435
///
@@ -1446,6 +1447,54 @@ pub macro PartialOrd($item:item) {
14461447
/* compiler built-in */
14471448
}
14481449

1450+
/// Helpers for chaining together field PartialOrds into the full type's ordering.
1451+
///
1452+
/// If the two values are equal, returns `ControlFlow::Continue`.
1453+
/// If the two values are not equal, returns `ControlFlow::Break(self OP other)`.
1454+
///
1455+
/// This allows simple types like `i32` and `f64` to just emit two comparisons
1456+
/// directly, instead of needing to optimize the 3-way comparison.
1457+
///
1458+
/// Currently this is done using specialization, but it doesn't need that:
1459+
/// it could be provided methods on `PartialOrd` instead and work fine.
1460+
pub(crate) trait SpecChainingPartialOrd<Rhs>: PartialOrd<Rhs> {
1461+
fn spec_chain_lt(&self, other: &Rhs) -> ControlFlow<bool>;
1462+
fn spec_chain_le(&self, other: &Rhs) -> ControlFlow<bool>;
1463+
fn spec_chain_gt(&self, other: &Rhs) -> ControlFlow<bool>;
1464+
fn spec_chain_ge(&self, other: &Rhs) -> ControlFlow<bool>;
1465+
}
1466+
1467+
impl<T: PartialOrd<U>, U> SpecChainingPartialOrd<U> for T {
1468+
#[inline]
1469+
default fn spec_chain_lt(&self, other: &U) -> ControlFlow<bool> {
1470+
match PartialOrd::partial_cmp(self, other) {
1471+
Some(Equal) => Continue(()),
1472+
c => Break(c.is_some_and(Ordering::is_lt)),
1473+
}
1474+
}
1475+
#[inline]
1476+
default fn spec_chain_le(&self, other: &U) -> ControlFlow<bool> {
1477+
match PartialOrd::partial_cmp(self, other) {
1478+
Some(Equal) => Continue(()),
1479+
c => Break(c.is_some_and(Ordering::is_le)),
1480+
}
1481+
}
1482+
#[inline]
1483+
default fn spec_chain_gt(&self, other: &U) -> ControlFlow<bool> {
1484+
match PartialOrd::partial_cmp(self, other) {
1485+
Some(Equal) => Continue(()),
1486+
c => Break(c.is_some_and(Ordering::is_gt)),
1487+
}
1488+
}
1489+
#[inline]
1490+
default fn spec_chain_ge(&self, other: &U) -> ControlFlow<bool> {
1491+
match PartialOrd::partial_cmp(self, other) {
1492+
Some(Equal) => Continue(()),
1493+
c => Break(c.is_some_and(Ordering::is_ge)),
1494+
}
1495+
}
1496+
}
1497+
14491498
/// Compares and returns the minimum of two values.
14501499
///
14511500
/// Returns the first argument if the comparison determines them to be equal.
@@ -1741,6 +1790,7 @@ where
17411790
mod impls {
17421791
use crate::cmp::Ordering::{self, Equal, Greater, Less};
17431792
use crate::hint::unreachable_unchecked;
1793+
use crate::ops::ControlFlow::{self, Break, Continue};
17441794

17451795
macro_rules! partial_eq_impl {
17461796
($($t:ty)*) => ($(
@@ -1779,6 +1829,36 @@ mod impls {
17791829

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

1832+
macro_rules! chaining_impl {
1833+
($t:ty) => {
1834+
// These implementations are the same for `Ord` or `PartialOrd` types
1835+
// because if either is NAN the `==` test will fail so we end up in
1836+
// the `Break` case and the comparison will correctly return `false`.
1837+
impl super::SpecChainingPartialOrd<$t> for $t {
1838+
#[inline]
1839+
fn spec_chain_lt(&self, other: &Self) -> ControlFlow<bool> {
1840+
let (lhs, rhs) = (*self, *other);
1841+
if lhs == rhs { Continue(()) } else { Break(lhs < rhs) }
1842+
}
1843+
#[inline]
1844+
fn spec_chain_le(&self, other: &Self) -> ControlFlow<bool> {
1845+
let (lhs, rhs) = (*self, *other);
1846+
if lhs == rhs { Continue(()) } else { Break(lhs <= rhs) }
1847+
}
1848+
#[inline]
1849+
fn spec_chain_gt(&self, other: &Self) -> ControlFlow<bool> {
1850+
let (lhs, rhs) = (*self, *other);
1851+
if lhs == rhs { Continue(()) } else { Break(lhs > rhs) }
1852+
}
1853+
#[inline]
1854+
fn spec_chain_ge(&self, other: &Self) -> ControlFlow<bool> {
1855+
let (lhs, rhs) = (*self, *other);
1856+
if lhs == rhs { Continue(()) } else { Break(lhs >= rhs) }
1857+
}
1858+
}
1859+
};
1860+
}
1861+
17821862
macro_rules! partial_ord_impl {
17831863
($($t:ty)*) => ($(
17841864
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1801,6 +1881,8 @@ mod impls {
18011881
#[inline(always)]
18021882
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
18031883
}
1884+
1885+
chaining_impl!($t);
18041886
)*)
18051887
}
18061888

@@ -1840,6 +1922,8 @@ mod impls {
18401922
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
18411923
}
18421924

1925+
chaining_impl!($t);
1926+
18431927
#[stable(feature = "rust1", since = "1.0.0")]
18441928
impl Ord for $t {
18451929
#[inline]

library/core/src/tuple.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// See core/src/primitive_docs.rs for documentation.
22

33
use crate::cmp::Ordering::{self, *};
4+
use crate::cmp::SpecChainingPartialOrd;
45
use crate::marker::{ConstParamTy_, StructuralPartialEq, UnsizedConstParamTy};
6+
use crate::ops::ControlFlow::{Break, Continue};
57

68
// Recursive macro for implementing n-ary tuple functions and operations
79
//
@@ -80,19 +82,19 @@ macro_rules! tuple_impls {
8082
}
8183
#[inline]
8284
fn lt(&self, other: &($($T,)+)) -> bool {
83-
lexical_ord!(lt, Less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
85+
lexical_ord!(lt, spec_chain_lt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
8486
}
8587
#[inline]
8688
fn le(&self, other: &($($T,)+)) -> bool {
87-
lexical_ord!(le, Less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
89+
lexical_ord!(le, spec_chain_le, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
8890
}
8991
#[inline]
9092
fn ge(&self, other: &($($T,)+)) -> bool {
91-
lexical_ord!(ge, Greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
93+
lexical_ord!(ge, spec_chain_ge, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
9294
}
9395
#[inline]
9496
fn gt(&self, other: &($($T,)+)) -> bool {
95-
lexical_ord!(gt, Greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
97+
lexical_ord!(gt, spec_chain_gt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
9698
}
9799
}
98100
}
@@ -171,15 +173,16 @@ macro_rules! maybe_tuple_doc {
171173
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, opt_is_lt, a1, b1,
172174
// a2, b2, a3, b3)` (and similarly for `lexical_cmp`)
173175
//
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`.
176+
// `$chain_rel` is the method from `SpecChainingPartialOrd` to use for all but the
177+
// final value, to produce better results for simple primitives.
176178
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),+) }
179+
($rel: ident, $chain_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
180+
match SpecChainingPartialOrd::$chain_rel(&$a, &$b) {
181+
Break(val) => val,
182+
Continue(()) => lexical_ord!($rel, $chain_rel, $($rest_a, $rest_b),+),
183+
}
181184
}};
182-
($rel: ident, $ne_rel: ident, $a:expr, $b:expr) => {
185+
($rel: ident, $chain_rel: ident, $a:expr, $b:expr) => {
183186
// Use the specific method for the last element
184187
PartialOrd::$rel(&$a, &$b)
185188
};
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)>::le) {
8+
scope 2 (inlined core::tuple::<impl PartialOrd for (f32, f32)>::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 std::cmp::SpecChainingPartialOrd<f32> for f32>::spec_chain_le) {
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>::le) {
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 = 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: f32);
58+
StorageLive(_10);
59+
_10 = copy ((*_2).1: f32);
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+
}
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 std::cmp::SpecChainingPartialOrd<u16> for u16>::spec_chain_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)