From bebcb9da216ab439a8628219f8021b1770134a35 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Mon, 12 May 2025 14:49:12 +0000 Subject: [PATCH 1/2] Add test for Ord impl for Option::NonZero --- tests/codegen/option-niche-eq.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/codegen/option-niche-eq.rs b/tests/codegen/option-niche-eq.rs index 9c5ed9ce57a5e..2be9e2bc48927 100644 --- a/tests/codegen/option-niche-eq.rs +++ b/tests/codegen/option-niche-eq.rs @@ -24,6 +24,18 @@ pub fn non_zero_signed_eq(l: Option>, r: Option>) -> b l == r } +// Test for #49892 +// This currently relies on a manual implementation of `PartialOrd`/`Ord` for `Option` +// Once LLVM is better able to optimize this pattern, we can return to using a derive. +// CHECK-LABEL: @non_zero_ord +#[no_mangle] +pub fn non_zero_ord(a: Option>, b: Option>) -> bool { + // CHECK: start: + // CHECK-NEXT: icmp ult i32 + // CHECK-NEXT: ret i1 + a < b +} + // CHECK-LABEL: @non_null_eq #[no_mangle] pub fn non_null_eq(l: Option>, r: Option>) -> bool { From 5eb47eeb851d4428642197cea3a566bcfa5f726a Mon Sep 17 00:00:00 2001 From: clubby789 Date: Mon, 12 May 2025 15:09:32 +0000 Subject: [PATCH 2/2] Add failing tests for some Option optimizations --- tests/codegen/option-niche-eq.rs | 13 ++-------- .../option-niche-unfixed/option-bool-eq.rs | 15 ++++++++++++ .../option-niche-unfixed/option-nonzero-eq.rs | 24 +++++++++++++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 tests/codegen/option-niche-unfixed/option-bool-eq.rs create mode 100644 tests/codegen/option-niche-unfixed/option-nonzero-eq.rs diff --git a/tests/codegen/option-niche-eq.rs b/tests/codegen/option-niche-eq.rs index 2be9e2bc48927..a39e2870a0f4f 100644 --- a/tests/codegen/option-niche-eq.rs +++ b/tests/codegen/option-niche-eq.rs @@ -1,3 +1,4 @@ +//@ min-llvm-version: 20 //@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled #![crate_type = "lib"] @@ -24,7 +25,7 @@ pub fn non_zero_signed_eq(l: Option>, r: Option>) -> b l == r } -// Test for #49892 +// FIXME(#49892) // This currently relies on a manual implementation of `PartialOrd`/`Ord` for `Option` // Once LLVM is better able to optimize this pattern, we can return to using a derive. // CHECK-LABEL: @non_zero_ord @@ -73,13 +74,3 @@ pub fn niche_eq(l: Option, r: Option) -> bool { // CHECK-NEXT: ret i1 l == r } - -// FIXME: This should work too -// // FIXME-CHECK-LABEL: @bool_eq -// #[no_mangle] -// pub fn bool_eq(l: Option, r: Option) -> bool { -// // FIXME-CHECK: start: -// // FIXME-CHECK-NEXT: icmp eq i8 -// // FIXME-CHECK-NEXT: ret i1 -// l == r -// } diff --git a/tests/codegen/option-niche-unfixed/option-bool-eq.rs b/tests/codegen/option-niche-unfixed/option-bool-eq.rs new file mode 100644 index 0000000000000..fa0e7836afb9e --- /dev/null +++ b/tests/codegen/option-niche-unfixed/option-bool-eq.rs @@ -0,0 +1,15 @@ +//@ should-fail +//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled +//! FIXME(#49892) +//! Tests that LLVM does not fully optimize comparisons of `Option`. +//! If this starts passing, it can be moved to `tests/codegen/option-niche-eq.rs` +#![crate_type = "lib"] + +// CHECK-LABEL: @bool_eq +#[no_mangle] +pub fn bool_eq(l: Option, r: Option) -> bool { + // CHECK: start: + // CHECK-NEXT: icmp eq i8 + // CHECK-NEXT: ret i1 + l == r +} diff --git a/tests/codegen/option-niche-unfixed/option-nonzero-eq.rs b/tests/codegen/option-niche-unfixed/option-nonzero-eq.rs new file mode 100644 index 0000000000000..308856cfb7e9d --- /dev/null +++ b/tests/codegen/option-niche-unfixed/option-nonzero-eq.rs @@ -0,0 +1,24 @@ +//@ should-fail +//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled +//! FIXME(#49892) +//! Test that the derived implementation of `PartialEq` for `Option` is not fully +//! optimized by LLVM. If this starts passing, the test and manual impl should +//! be removed. +#![crate_type = "lib"] + +use std::num::NonZero; + +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum Option { + None, + Some(T), +} + +// CHECK-LABEL: @non_zero_eq +#[no_mangle] +pub fn non_zero_eq(l: Option>, r: Option>) -> bool { + // CHECK: start: + // CHECK-NEXT: icmp eq i32 + // CHECK-NEXT: ret i1 + l == r +}