Skip to content

Commit 9759fdc

Browse files
committed
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, added in LLVM 19.
1 parent f95c996 commit 9759fdc

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
@@ -12,6 +12,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef;
1212
use rustc_codegen_ssa::traits::*;
1313
use rustc_data_structures::small_c_str::SmallCStr;
1414
use rustc_hir::def_id::DefId;
15+
use rustc_middle::bug;
1516
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1617
use rustc_middle::ty::layout::{
1718
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
@@ -873,6 +874,35 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
873874
unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
874875
}
875876

877+
fn three_way_compare(
878+
&mut self,
879+
ty: Ty<'tcx>,
880+
lhs: Self::Value,
881+
rhs: Self::Value,
882+
) -> Option<Self::Value> {
883+
// FIXME: See comment on the definition of `three_way_compare`.
884+
if crate::llvm_util::get_version() < (20, 0, 0) {
885+
return None;
886+
}
887+
888+
let name = match (ty.is_signed(), ty.primitive_size(self.tcx).bits()) {
889+
(true, 8) => "llvm.scmp.i8.i8",
890+
(true, 16) => "llvm.scmp.i8.i16",
891+
(true, 32) => "llvm.scmp.i8.i32",
892+
(true, 64) => "llvm.scmp.i8.i64",
893+
(true, 128) => "llvm.scmp.i8.i128",
894+
895+
(false, 8) => "llvm.ucmp.i8.i8",
896+
(false, 16) => "llvm.ucmp.i8.i16",
897+
(false, 32) => "llvm.ucmp.i8.i32",
898+
(false, 64) => "llvm.ucmp.i8.i64",
899+
(false, 128) => "llvm.ucmp.i8.i128",
900+
901+
_ => bug!("three-way compare unsupported for type {ty:?}"),
902+
};
903+
Some(self.call_intrinsic(name, &[lhs, rhs]))
904+
}
905+
876906
/* Miscellaneous instructions */
877907
fn memcpy(
878908
&mut self,

compiler/rustc_codegen_llvm/src/context.rs

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

1049+
ifn!("llvm.scmp.i8.i8", fn(t_i8, t_i8) -> t_i8);
1050+
ifn!("llvm.scmp.i8.i16", fn(t_i16, t_i16) -> t_i8);
1051+
ifn!("llvm.scmp.i8.i32", fn(t_i32, t_i32) -> t_i8);
1052+
ifn!("llvm.scmp.i8.i64", fn(t_i64, t_i64) -> t_i8);
1053+
ifn!("llvm.scmp.i8.i128", fn(t_i128, t_i128) -> t_i8);
1054+
1055+
ifn!("llvm.ucmp.i8.i8", fn(t_i8, t_i8) -> t_i8);
1056+
ifn!("llvm.ucmp.i8.i16", fn(t_i16, t_i16) -> t_i8);
1057+
ifn!("llvm.ucmp.i8.i32", fn(t_i32, t_i32) -> t_i8);
1058+
ifn!("llvm.ucmp.i8.i64", fn(t_i64, t_i64) -> t_i8);
1059+
ifn!("llvm.ucmp.i8.i128", fn(t_i128, t_i128) -> t_i8);
1060+
10491061
ifn!("llvm.lifetime.start.p0i8", fn(t_i64, ptr) -> void);
10501062
ifn!("llvm.lifetime.end.p0i8", fn(t_i64, ptr) -> void);
10511063

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+3
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
926926
mir::BinOp::Cmp => {
927927
use std::cmp::Ordering;
928928
assert!(!is_float);
929+
if let Some(value) = bx.three_way_compare(input_ty, lhs, rhs) {
930+
return value;
931+
}
929932
let pred = |op| base::bin_op_to_icmp_predicate(op, is_signed);
930933
if bx.cx().tcx().sess.opts.optimize == OptLevel::No {
931934
// 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
@@ -309,6 +309,18 @@ pub trait BuilderMethods<'a, 'tcx>:
309309
fn icmp(&mut self, op: IntPredicate, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
310310
fn fcmp(&mut self, op: RealPredicate, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
311311

312+
/// Returns `-1` if `lhs < rhs`, `0` if `lhs == rhs`, and `1` if `lhs > rhs`.
313+
// FIXME: Move the default implementation from `codegen_scalar_binop` into this method and
314+
// remove the `Option` return once LLVM 20 is the minimum version.
315+
fn three_way_compare(
316+
&mut self,
317+
_ty: Ty<'tcx>,
318+
_lhs: Self::Value,
319+
_rhs: Self::Value,
320+
) -> Option<Self::Value> {
321+
None
322+
}
323+
312324
fn memcpy(
313325
&mut self,
314326
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)