-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tweak the default PartialOrd::{lt,le,gt,ge}
#106065
Closed
scottmcm
wants to merge
2
commits into
rust-lang:master
from
scottmcm:alternate-partialord-default-impls
Closed
Changes from 1 commit
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 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
This file contains 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,49 @@ | ||
// The `derive(PartialOrd)` for a newtype doesn't override `lt`/`le`/`gt`/`ge`. | ||
// This double-checks that the `Option<Ordering>` intermediate values used | ||
// in the operators for such a type all optimize away. | ||
|
||
// compile-flags: -C opt-level=1 | ||
// min-llvm-version: 15.0 | ||
|
||
#![crate_type = "lib"] | ||
|
||
use std::cmp::Ordering; | ||
|
||
#[derive(PartialOrd, PartialEq)] | ||
pub struct Foo(u16); | ||
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. Hopefully this test will ensure that the problem you saw with |
||
|
||
// CHECK-LABEL: @check_lt | ||
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]]) | ||
#[no_mangle] | ||
pub fn check_lt(a: Foo, b: Foo) -> bool { | ||
// CHECK: %[[R:.+]] = icmp ult i16 %[[A]], %[[B]] | ||
// CHECK-NEXT: ret i1 %[[R]] | ||
a < b | ||
} | ||
|
||
// CHECK-LABEL: @check_le | ||
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]]) | ||
#[no_mangle] | ||
pub fn check_le(a: Foo, b: Foo) -> bool { | ||
// CHECK: %[[R:.+]] = icmp ule i16 %[[A]], %[[B]] | ||
// CHECK-NEXT: ret i1 %[[R]] | ||
a <= b | ||
} | ||
|
||
// CHECK-LABEL: @check_gt | ||
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]]) | ||
#[no_mangle] | ||
pub fn check_gt(a: Foo, b: Foo) -> bool { | ||
// CHECK: %[[R:.+]] = icmp ugt i16 %[[A]], %[[B]] | ||
// CHECK-NEXT: ret i1 %[[R]] | ||
a > b | ||
} | ||
|
||
// CHECK-LABEL: @check_ge | ||
// CHECK-SAME: (i16 %[[A:.+]], i16 %[[B:.+]]) | ||
#[no_mangle] | ||
pub fn check_ge(a: Foo, b: Foo) -> bool { | ||
// CHECK: %[[R:.+]] = icmp uge i16 %[[A]], %[[B]] | ||
// CHECK-NEXT: ret i1 %[[R]] | ||
a >= b | ||
} |
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.
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.
This is now conceptually two checks, rather than just one, so it's possible it's not always better. None is currently 2 here, so the old code was hypothetically just
c == 1
, and now it'sc != 2 && c > 0
. (Of courselt
ends up beingc != 2 && c < 0
, which obviously folds toc < 0
, so that one's probably not impacted.)My hope is that this is still better in practice:
partial_cmp
s are actuallycmp
s, and thus the optimizer will easily notice that the result is neverNone
-- like happens in the codegen test.None
, hopefully jump-threading will usually notice that theNone
becomesfalse
and will again bypass actually running this check at runtime.I'll see if I can prove that out in a codegen test...
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.
Well, I didn't manage to make a great codegen test for this, but I did in passing find two other things:
noundef
on parameters, https://rust-lang.zulipchat.com/#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/We.20will.20want.20a.20lot.20of.20noundefs/near/317472833<=
via 3-way comparison doesn't optimize down llvm/llvm-project#59666