Skip to content

Optimize jumps in PartialOrd le #83819

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 1 commit into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,8 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn le(&self, other: &Rhs) -> bool {
matches!(self.partial_cmp(other), Some(Less | Equal))
// Pattern `Some(Less | Eq)` optimizes worse than negating `None | Some(Greater)`.
!matches!(self.partial_cmp(other), None | Some(Greater))
}

/// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
Expand Down
39 changes: 39 additions & 0 deletions src/test/codegen/issue-73338-effecient-cmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This test checks that comparison operation
// generated by #[derive(PartialOrd)]
// doesn't contain jumps for C enums

// compile-flags: -Copt-level=3

#![crate_type="lib"]

#[repr(u32)]
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)]
pub enum Foo {
Zero,
One,
Two,
}

#[no_mangle]
pub fn compare_less(a: Foo, b: Foo)->bool{
// CHECK-NOT: br {{.*}}
a < b
}

#[no_mangle]
pub fn compare_le(a: Foo, b: Foo)->bool{
// CHECK-NOT: br {{.*}}
a <= b
}

#[no_mangle]
pub fn compare_ge(a: Foo, b: Foo)->bool{
// CHECK-NOT: br {{.*}}
a >= b
}

#[no_mangle]
pub fn compare_greater(a: Foo, b: Foo)->bool{
// CHECK-NOT: br {{.*}}
a > b
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that this did not need a modification to ge -- maybe hints at some LLVM bug?

Copy link
Contributor Author

@AngelicosPhosphoros AngelicosPhosphoros Apr 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the Option<Ordering> has such u8 values

Some(Less) == 255
Some(Equal) == 0
Some(Greater) == 1
None == 2

so, >= compiles to cmp_result as u8 < 2 which easy to optimize.

Old implementation of <= couldn't be optimized such easily because it was (255 == cmp_result || 0 == cmp_result) which LLVM failed to optimize. After my change it becomes !((cmp_result+1) > 1) which optimized much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, current version of LLVM handle comparison with 2 consequtive numbers better than with 2 different numbers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, OK. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea what the fallout would be if Ordering was changed from (Less, Equal, Greater) == (-1, 0, 1) to (Less, Equal, Greater) == (0, 1, 2)? I didn't find a guarantee for the underlying values being stable and impl Ord for Ordering only relies on Less < Equal < Greater.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know enough for this, I think.

Current solution could be profitable in conversion from memcmp results (compiler can do sign(memcmp(a, b)) to get Ordering).

}