about summary refs log tree commit diff
path: root/tests/codegen-llvm/comparison-operators-2-struct.rs
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-07-21 14:34:12 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-07-22 14:28:48 +0200
commita27f3e3fd1e4d16160f8885b6b06665b5319f56c (patch)
treeb033935392cbadf6f85d2dbddf433a88e323aeeb /tests/codegen-llvm/comparison-operators-2-struct.rs
parented93c1783b404d728d4809973a0550eb33cd293f (diff)
downloadrust-a27f3e3fd1e4d16160f8885b6b06665b5319f56c.tar.gz
rust-a27f3e3fd1e4d16160f8885b6b06665b5319f56c.zip
Rename `tests/codegen` into `tests/codegen-llvm`
Diffstat (limited to 'tests/codegen-llvm/comparison-operators-2-struct.rs')
-rw-r--r--tests/codegen-llvm/comparison-operators-2-struct.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/codegen-llvm/comparison-operators-2-struct.rs b/tests/codegen-llvm/comparison-operators-2-struct.rs
new file mode 100644
index 00000000000..e179066ebfd
--- /dev/null
+++ b/tests/codegen-llvm/comparison-operators-2-struct.rs
@@ -0,0 +1,61 @@
+//@ compile-flags: -C opt-level=1
+//@ min-llvm-version: 20
+
+// The `derive(PartialOrd)` for a 2-field type 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.
+
+#![crate_type = "lib"]
+
+use std::cmp::Ordering;
+
+#[derive(PartialOrd, PartialEq)]
+pub struct Foo(i32, u32);
+
+// CHECK-LABEL: @check_lt(
+// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
+#[no_mangle]
+pub fn check_lt(a: Foo, b: Foo) -> bool {
+    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R0:.+]] = icmp slt i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R1:.+]] = icmp ult i32 %[[A1]], %[[B1]]
+    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
+    // CHECK-NEXT: ret i1 %[[R]]
+    a < b
+}
+
+// CHECK-LABEL: @check_le(
+// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
+#[no_mangle]
+pub fn check_le(a: Foo, b: Foo) -> bool {
+    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R0:.+]] = icmp sle i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R1:.+]] = icmp ule i32 %[[A1]], %[[B1]]
+    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
+    // CHECK-NEXT: ret i1 %[[R]]
+    a <= b
+}
+
+// CHECK-LABEL: @check_gt(
+// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
+#[no_mangle]
+pub fn check_gt(a: Foo, b: Foo) -> bool {
+    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R0:.+]] = icmp sgt i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R1:.+]] = icmp ugt i32 %[[A1]], %[[B1]]
+    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
+    // CHECK-NEXT: ret i1 %[[R]]
+    a > b
+}
+
+// CHECK-LABEL: @check_ge(
+// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
+#[no_mangle]
+pub fn check_ge(a: Foo, b: Foo) -> bool {
+    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R0:.+]] = icmp sge i32 %[[A0]], %[[B0]]
+    // CHECK-DAG: %[[R1:.+]] = icmp uge i32 %[[A1]], %[[B1]]
+    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
+    // CHECK-NEXT: ret i1 %[[R]]
+    a >= b
+}