Skip to content

Commit 3f91bbc

Browse files
authored
Rollup merge of #140950 - clubby789:nonzero-ord-test, r=Mark-Simulacrum
More option optimization tests I noticed that although adding a manual implementation for PartialOrd on Option in #122024, I didn't add a test so that we can easily check if this behavior has improved. This also adds a couple of `should-fail` tests - this will allow us to remove these hacky implementations if upstream LLVM improves.
2 parents 045ac21 + 5eb47ee commit 3f91bbc

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

tests/codegen/option-niche-eq.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ min-llvm-version: 20
12
//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled
23
#![crate_type = "lib"]
34

@@ -24,6 +25,18 @@ pub fn non_zero_signed_eq(l: Option<NonZero<i64>>, r: Option<NonZero<i64>>) -> b
2425
l == r
2526
}
2627

28+
// FIXME(#49892)
29+
// This currently relies on a manual implementation of `PartialOrd`/`Ord` for `Option`
30+
// Once LLVM is better able to optimize this pattern, we can return to using a derive.
31+
// CHECK-LABEL: @non_zero_ord
32+
#[no_mangle]
33+
pub fn non_zero_ord(a: Option<NonZero<u32>>, b: Option<NonZero<u32>>) -> bool {
34+
// CHECK: start:
35+
// CHECK-NEXT: icmp ult i32
36+
// CHECK-NEXT: ret i1
37+
a < b
38+
}
39+
2740
// CHECK-LABEL: @non_null_eq
2841
#[no_mangle]
2942
pub fn non_null_eq(l: Option<NonNull<u8>>, r: Option<NonNull<u8>>) -> bool {
@@ -61,13 +74,3 @@ pub fn niche_eq(l: Option<EnumWithNiche>, r: Option<EnumWithNiche>) -> bool {
6174
// CHECK-NEXT: ret i1
6275
l == r
6376
}
64-
65-
// FIXME: This should work too
66-
// // FIXME-CHECK-LABEL: @bool_eq
67-
// #[no_mangle]
68-
// pub fn bool_eq(l: Option<bool>, r: Option<bool>) -> bool {
69-
// // FIXME-CHECK: start:
70-
// // FIXME-CHECK-NEXT: icmp eq i8
71-
// // FIXME-CHECK-NEXT: ret i1
72-
// l == r
73-
// }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ should-fail
2+
//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled
3+
//! FIXME(#49892)
4+
//! Tests that LLVM does not fully optimize comparisons of `Option<bool>`.
5+
//! If this starts passing, it can be moved to `tests/codegen/option-niche-eq.rs`
6+
#![crate_type = "lib"]
7+
8+
// CHECK-LABEL: @bool_eq
9+
#[no_mangle]
10+
pub fn bool_eq(l: Option<bool>, r: Option<bool>) -> bool {
11+
// CHECK: start:
12+
// CHECK-NEXT: icmp eq i8
13+
// CHECK-NEXT: ret i1
14+
l == r
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ should-fail
2+
//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled
3+
//! FIXME(#49892)
4+
//! Test that the derived implementation of `PartialEq` for `Option` is not fully
5+
//! optimized by LLVM. If this starts passing, the test and manual impl should
6+
//! be removed.
7+
#![crate_type = "lib"]
8+
9+
use std::num::NonZero;
10+
11+
#[derive(Copy, Clone, PartialEq, Eq)]
12+
pub enum Option<T> {
13+
None,
14+
Some(T),
15+
}
16+
17+
// CHECK-LABEL: @non_zero_eq
18+
#[no_mangle]
19+
pub fn non_zero_eq(l: Option<NonZero<u32>>, r: Option<NonZero<u32>>) -> bool {
20+
// CHECK: start:
21+
// CHECK-NEXT: icmp eq i32
22+
// CHECK-NEXT: ret i1
23+
l == r
24+
}

0 commit comments

Comments
 (0)