Auto merge of #133984 - DaniPopes:scmp-ucmp, r=scottmcm
Lower BinOp::Cmp to llvm.{s,u}cmp.* intrinsics Lowers `mir::BinOp::Cmp` (`three_way_compare` intrinsic) to the corresponding LLVM `llvm.{s,u}cmp.i8.*` intrinsics. These are the intrinsics mentioned in https://github.com/rust-lang/rust/pull/118310, which are now available in LLVM 19. I couldn't find any follow-up PRs/discussions about this, please let me know if I missed something. r? `@scottmcm`
This commit is contained in:
commit
1df5affaca
8 changed files with 124 additions and 42 deletions
|
@ -1,5 +1,4 @@
|
|||
//@ compile-flags: -C opt-level=1 -Z merge-functions=disabled
|
||||
//@ only-x86_64
|
||||
//@ min-llvm-version: 20
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//@ revisions: llvm-pre-20 llvm-20
|
||||
//@ [llvm-20] min-llvm-version: 20
|
||||
//@ [llvm-pre-20] max-llvm-major-version: 19
|
||||
//@ compile-flags: -C opt-level=3
|
||||
//@ compile-flags: -C opt-level=3 -Zmerge-functions=disabled
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
||||
|
@ -13,7 +13,7 @@ use std::cmp::Ordering;
|
|||
// CHECK-LABEL: @cmp_signed
|
||||
#[no_mangle]
|
||||
pub fn cmp_signed(a: i64, b: i64) -> Ordering {
|
||||
// llvm-20: @llvm.scmp.i8.i64
|
||||
// llvm-20: call{{.*}} i8 @llvm.scmp.i8.i64
|
||||
// llvm-pre-20: icmp slt
|
||||
// llvm-pre-20: icmp ne
|
||||
// llvm-pre-20: zext i1
|
||||
|
@ -24,10 +24,39 @@ pub fn cmp_signed(a: i64, b: i64) -> Ordering {
|
|||
// CHECK-LABEL: @cmp_unsigned
|
||||
#[no_mangle]
|
||||
pub fn cmp_unsigned(a: u32, b: u32) -> Ordering {
|
||||
// llvm-20: @llvm.ucmp.i8.i32
|
||||
// llvm-20: call{{.*}} i8 @llvm.ucmp.i8.i32
|
||||
// llvm-pre-20: icmp ult
|
||||
// llvm-pre-20: icmp ne
|
||||
// llvm-pre-20: zext i1
|
||||
// llvm-pre-20: select i1
|
||||
a.cmp(&b)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @cmp_char
|
||||
#[no_mangle]
|
||||
pub fn cmp_char(a: char, b: char) -> Ordering {
|
||||
// llvm-20: call{{.*}} i8 @llvm.ucmp.i8.i32
|
||||
// llvm-pre-20: icmp ult
|
||||
// llvm-pre-20: icmp ne
|
||||
// llvm-pre-20: zext i1
|
||||
// llvm-pre-20: select i1
|
||||
a.cmp(&b)
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @cmp_tuple
|
||||
#[no_mangle]
|
||||
pub fn cmp_tuple(a: (i16, u16), b: (i16, u16)) -> Ordering {
|
||||
// llvm-20-DAG: call{{.*}} i8 @llvm.ucmp.i8.i16
|
||||
// llvm-20-DAG: call{{.*}} i8 @llvm.scmp.i8.i16
|
||||
// llvm-20: ret i8
|
||||
// llvm-pre-20: icmp slt
|
||||
// llvm-pre-20: icmp ne
|
||||
// llvm-pre-20: zext i1
|
||||
// llvm-pre-20: select i1
|
||||
// llvm-pre-20: icmp ult
|
||||
// llvm-pre-20: icmp ne
|
||||
// llvm-pre-20: zext i1
|
||||
// llvm-pre-20: select i1
|
||||
// llvm-pre-20: select i1
|
||||
a.cmp(&b)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
//@ [DEBUG] compile-flags: -C opt-level=0
|
||||
//@ [OPTIM] compile-flags: -C opt-level=3
|
||||
//@ compile-flags: -C no-prepopulate-passes
|
||||
//@ min-llvm-version: 20
|
||||
|
||||
#![crate_type = "lib"]
|
||||
#![feature(core_intrinsics)]
|
||||
|
@ -12,17 +13,8 @@ use std::intrinsics::three_way_compare;
|
|||
// CHECK-LABEL: @signed_cmp
|
||||
// CHECK-SAME: (i16{{.*}} %a, i16{{.*}} %b)
|
||||
pub fn signed_cmp(a: i16, b: i16) -> std::cmp::Ordering {
|
||||
// DEBUG: %[[GT:.+]] = icmp sgt i16 %a, %b
|
||||
// DEBUG: %[[ZGT:.+]] = zext i1 %[[GT]] to i8
|
||||
// DEBUG: %[[LT:.+]] = icmp slt i16 %a, %b
|
||||
// DEBUG: %[[ZLT:.+]] = zext i1 %[[LT]] to i8
|
||||
// DEBUG: %[[R:.+]] = sub nsw i8 %[[ZGT]], %[[ZLT]]
|
||||
|
||||
// OPTIM: %[[LT:.+]] = icmp slt i16 %a, %b
|
||||
// OPTIM: %[[NE:.+]] = icmp ne i16 %a, %b
|
||||
// OPTIM: %[[CGE:.+]] = select i1 %[[NE]], i8 1, i8 0
|
||||
// OPTIM: %[[CGEL:.+]] = select i1 %[[LT]], i8 -1, i8 %[[CGE]]
|
||||
// OPTIM: ret i8 %[[CGEL]]
|
||||
// CHECK: %[[CMP:.+]] = call i8 @llvm.scmp.i8.i16(i16 %a, i16 %b)
|
||||
// CHECK-NEXT: ret i8 %[[CMP]]
|
||||
three_way_compare(a, b)
|
||||
}
|
||||
|
||||
|
@ -30,16 +22,7 @@ pub fn signed_cmp(a: i16, b: i16) -> std::cmp::Ordering {
|
|||
// CHECK-LABEL: @unsigned_cmp
|
||||
// CHECK-SAME: (i16{{.*}} %a, i16{{.*}} %b)
|
||||
pub fn unsigned_cmp(a: u16, b: u16) -> std::cmp::Ordering {
|
||||
// DEBUG: %[[GT:.+]] = icmp ugt i16 %a, %b
|
||||
// DEBUG: %[[ZGT:.+]] = zext i1 %[[GT]] to i8
|
||||
// DEBUG: %[[LT:.+]] = icmp ult i16 %a, %b
|
||||
// DEBUG: %[[ZLT:.+]] = zext i1 %[[LT]] to i8
|
||||
// DEBUG: %[[R:.+]] = sub nsw i8 %[[ZGT]], %[[ZLT]]
|
||||
|
||||
// OPTIM: %[[LT:.+]] = icmp ult i16 %a, %b
|
||||
// OPTIM: %[[NE:.+]] = icmp ne i16 %a, %b
|
||||
// OPTIM: %[[CGE:.+]] = select i1 %[[NE]], i8 1, i8 0
|
||||
// OPTIM: %[[CGEL:.+]] = select i1 %[[LT]], i8 -1, i8 %[[CGE]]
|
||||
// OPTIM: ret i8 %[[CGEL]]
|
||||
// CHECK: %[[CMP:.+]] = call i8 @llvm.ucmp.i8.i16(i16 %a, i16 %b)
|
||||
// CHECK-NEXT: ret i8 %[[CMP]]
|
||||
three_way_compare(a, b)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue