Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 607b8f5

Browse files
committedFeb 17, 2025
Auto merge of #133984 - DaniPopes:scmp-ucmp, r=<try>
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 #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`
2 parents ce36a96 + dcdb7ed commit 607b8f5

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed
 

‎compiler/rustc_codegen_llvm/src/builder.rs

+30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef;
1414
use rustc_codegen_ssa::traits::*;
1515
use rustc_data_structures::small_c_str::SmallCStr;
1616
use rustc_hir::def_id::DefId;
17+
use rustc_middle::bug;
1718
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1819
use rustc_middle::ty::layout::{
1920
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
@@ -1022,6 +1023,35 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10221023
unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
10231024
}
10241025

1026+
fn three_way_compare(
1027+
&mut self,
1028+
ty: Ty<'tcx>,
1029+
lhs: Self::Value,
1030+
rhs: Self::Value,
1031+
) -> Option<Self::Value> {
1032+
// FIXME: See comment on the definition of `three_way_compare`.
1033+
if crate::llvm_util::get_version() < (20, 0, 0) {
1034+
return None;
1035+
}
1036+
1037+
let name = match (ty.is_signed(), ty.primitive_size(self.tcx).bits()) {
1038+
(true, 8) => "llvm.scmp.i8.i8",
1039+
(true, 16) => "llvm.scmp.i8.i16",
1040+
(true, 32) => "llvm.scmp.i8.i32",
1041+
(true, 64) => "llvm.scmp.i8.i64",
1042+
(true, 128) => "llvm.scmp.i8.i128",
1043+
1044+
(false, 8) => "llvm.ucmp.i8.i8",
1045+
(false, 16) => "llvm.ucmp.i8.i16",
1046+
(false, 32) => "llvm.ucmp.i8.i32",
1047+
(false, 64) => "llvm.ucmp.i8.i64",
1048+
(false, 128) => "llvm.ucmp.i8.i128",
1049+
1050+
_ => bug!("three-way compare unsupported for type {ty:?}"),
1051+
};
1052+
Some(self.call_intrinsic(name, &[lhs, rhs]))
1053+
}
1054+
10251055
/* Miscellaneous instructions */
10261056
fn memcpy(
10271057
&mut self,

‎compiler/rustc_codegen_llvm/src/context.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,18 @@ impl<'ll> CodegenCx<'ll, '_> {
11081108
ifn!("llvm.usub.sat.i64", fn(t_i64, t_i64) -> t_i64);
11091109
ifn!("llvm.usub.sat.i128", fn(t_i128, t_i128) -> t_i128);
11101110

1111+
ifn!("llvm.scmp.i8.i8", fn(t_i8, t_i8) -> t_i8);
1112+
ifn!("llvm.scmp.i8.i16", fn(t_i16, t_i16) -> t_i8);
1113+
ifn!("llvm.scmp.i8.i32", fn(t_i32, t_i32) -> t_i8);
1114+
ifn!("llvm.scmp.i8.i64", fn(t_i64, t_i64) -> t_i8);
1115+
ifn!("llvm.scmp.i8.i128", fn(t_i128, t_i128) -> t_i8);
1116+
1117+
ifn!("llvm.ucmp.i8.i8", fn(t_i8, t_i8) -> t_i8);
1118+
ifn!("llvm.ucmp.i8.i16", fn(t_i16, t_i16) -> t_i8);
1119+
ifn!("llvm.ucmp.i8.i32", fn(t_i32, t_i32) -> t_i8);
1120+
ifn!("llvm.ucmp.i8.i64", fn(t_i64, t_i64) -> t_i8);
1121+
ifn!("llvm.ucmp.i8.i128", fn(t_i128, t_i128) -> t_i8);
1122+
11111123
ifn!("llvm.lifetime.start.p0i8", fn(t_i64, ptr) -> void);
11121124
ifn!("llvm.lifetime.end.p0i8", fn(t_i64, ptr) -> void);
11131125

‎compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+3
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
992992
mir::BinOp::Cmp => {
993993
use std::cmp::Ordering;
994994
assert!(!is_float);
995+
if let Some(value) = bx.three_way_compare(input_ty, lhs, rhs) {
996+
return value;
997+
}
995998
let pred = |op| base::bin_op_to_icmp_predicate(op, is_signed);
996999
if bx.cx().tcx().sess.opts.optimize == OptLevel::No {
9971000
// FIXME: This actually generates tighter assembly, and is a classic trick

‎compiler/rustc_codegen_ssa/src/traits/builder.rs

+12
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,18 @@ pub trait BuilderMethods<'a, 'tcx>:
371371
fn icmp(&mut self, op: IntPredicate, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
372372
fn fcmp(&mut self, op: RealPredicate, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
373373

374+
/// Returns `-1` if `lhs < rhs`, `0` if `lhs == rhs`, and `1` if `lhs > rhs`.
375+
// FIXME: Move the default implementation from `codegen_scalar_binop` into this method and
376+
// remove the `Option` return once LLVM 20 is the minimum version.
377+
fn three_way_compare(
378+
&mut self,
379+
_ty: Ty<'tcx>,
380+
_lhs: Self::Value,
381+
_rhs: Self::Value,
382+
) -> Option<Self::Value> {
383+
None
384+
}
385+
374386
fn memcpy(
375387
&mut self,
376388
dst: Self::Value,

‎tests/codegen/integer-cmp.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//@ revisions: llvm-pre-20 llvm-20
55
//@ [llvm-20] min-llvm-version: 20
66
//@ [llvm-pre-20] max-llvm-major-version: 19
7-
//@ compile-flags: -C opt-level=3
7+
//@ compile-flags: -C opt-level=3 -Zmerge-functions=disabled
88

99
#![crate_type = "lib"]
1010

@@ -13,7 +13,7 @@ use std::cmp::Ordering;
1313
// CHECK-LABEL: @cmp_signed
1414
#[no_mangle]
1515
pub fn cmp_signed(a: i64, b: i64) -> Ordering {
16-
// llvm-20: @llvm.scmp.i8.i64
16+
// llvm-20: call{{.*}} i8 @llvm.scmp.i8.i64
1717
// llvm-pre-20: icmp slt
1818
// llvm-pre-20: icmp ne
1919
// llvm-pre-20: zext i1
@@ -24,10 +24,39 @@ pub fn cmp_signed(a: i64, b: i64) -> Ordering {
2424
// CHECK-LABEL: @cmp_unsigned
2525
#[no_mangle]
2626
pub fn cmp_unsigned(a: u32, b: u32) -> Ordering {
27-
// llvm-20: @llvm.ucmp.i8.i32
27+
// llvm-20: call{{.*}} i8 @llvm.ucmp.i8.i32
2828
// llvm-pre-20: icmp ult
2929
// llvm-pre-20: icmp ne
3030
// llvm-pre-20: zext i1
3131
// llvm-pre-20: select i1
3232
a.cmp(&b)
3333
}
34+
35+
// CHECK-LABEL: @cmp_char
36+
#[no_mangle]
37+
pub fn cmp_char(a: char, b: char) -> Ordering {
38+
// llvm-20: call{{.*}} i8 @llvm.ucmp.i8.i32
39+
// llvm-pre-20: icmp ult
40+
// llvm-pre-20: icmp ne
41+
// llvm-pre-20: zext i1
42+
// llvm-pre-20: select i1
43+
a.cmp(&b)
44+
}
45+
46+
// CHECK-LABEL: @cmp_tuple
47+
#[no_mangle]
48+
pub fn cmp_tuple(a: (i16, u16), b: (i16, u16)) -> Ordering {
49+
// llvm-20-DAG: call{{.*}} i8 @llvm.ucmp.i8.i16
50+
// llvm-20-DAG: call{{.*}} i8 @llvm.scmp.i8.i16
51+
// llvm-20: ret i8
52+
// llvm-pre-20: icmp slt
53+
// llvm-pre-20: icmp ne
54+
// llvm-pre-20: zext i1
55+
// llvm-pre-20: select i1
56+
// llvm-pre-20: icmp ult
57+
// llvm-pre-20: icmp ne
58+
// llvm-pre-20: zext i1
59+
// llvm-pre-20: select i1
60+
// llvm-pre-20: select i1
61+
a.cmp(&b)
62+
}

0 commit comments

Comments
 (0)
Please sign in to comment.