Skip to content

Commit ef4186a

Browse files
committed
Use Cranelift legalization for icmp.i128
The previous translation was wrong for signed 128bit comparisions This fixes several libcore tests
1 parent 5c6bf83 commit ef4186a

File tree

6 files changed

+14
-166
lines changed

6 files changed

+14
-166
lines changed

example/std_example.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ fn main() {
8181
assert_eq!(houndred_f32 as i128, 100);
8282
assert_eq!(houndred_f64 as i128, 100);
8383

84+
// Test signed 128bit comparing
85+
let max = usize::MAX as i128;
86+
if 100i128 < 0i128 || 100i128 > max {
87+
panic!();
88+
}
89+
8490
let _a = 1u32 << 2u8;
8591

8692
let empty: [i32; 0] = [];

patches/0022-core-Disable-not-compiling-tests.patch

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,5 @@ index 6609bc3..241b497 100644
119119

120120
#[test]
121121
#[should_panic(expected = "index 0 greater than length of slice")]
122-
diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs
123-
index b73b621..d6cc3f8 100644
124-
--- a/src/libcore/tests/iter.rs
125-
+++ b/src/libcore/tests/iter.rs
126-
@@ -2541,6 +2541,7 @@ fn test_steps_between() {
127-
128-
// Skip u64/i64 to avoid differences with 32-bit vs 64-bit platforms
129-
130-
+ /*
131-
assert_eq!(Step::steps_between(&20_u128, &200_u128), Some(180_usize));
132-
assert_eq!(Step::steps_between(&-20_i128, &80_i128), Some(100_usize));
133-
if cfg!(target_pointer_width = "64") {
134-
@@ -2552,6 +2553,7 @@ fn test_steps_between() {
135-
Step::steps_between(&-0x1_0000_0000_0000_0000_i128, &0x1_0000_0000_0000_0000_i128,),
136-
None,
137-
);
138-
+ */
139-
}
140-
141-
#[test]
142-
--
122+
--
143123
2.21.0 (Apple Git-122)

patches/0023-core-Ignore-failing-tests.patch

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -66,87 +66,6 @@ index c9096b7..be37fcd 100644
6666
fn test_successors() {
6767
let mut powers_of_10 = successors(Some(1_u16), |n| n.checked_mul(10));
6868
assert_eq!(powers_of_10.by_ref().collect::<Vec<_>>(), &[1, 10, 100, 1_000, 10_000]);
69-
diff --git a/src/libcore/tests/num/bignum.rs b/src/libcore/tests/num/bignum.rs
70-
index b9e15ec..32f6de8 100644
71-
--- a/src/libcore/tests/num/bignum.rs
72-
+++ b/src/libcore/tests/num/bignum.rs
73-
@@ -3,6 +3,7 @@ use core::num::bignum::tests::Big8x3 as Big;
74-
75-
#[test]
76-
#[should_panic]
77-
+#[ignore]
78-
fn test_from_u64_overflow() {
79-
Big::from_u64(0x1000000);
80-
}
81-
@@ -19,12 +20,14 @@ fn test_add() {
82-
83-
#[test]
84-
#[should_panic]
85-
+#[ignore]
86-
fn test_add_overflow_1() {
87-
Big::from_small(1).add(&Big::from_u64(0xffffff));
88-
}
89-
90-
#[test]
91-
#[should_panic]
92-
+#[ignore]
93-
fn test_add_overflow_2() {
94-
Big::from_u64(0xffffff).add(&Big::from_small(1));
95-
}
96-
@@ -42,6 +45,7 @@ fn test_add_small() {
97-
98-
#[test]
99-
#[should_panic]
100-
+#[ignore]
101-
fn test_add_small_overflow() {
102-
Big::from_u64(0xffffff).add_small(1);
103-
}
104-
@@ -76,6 +80,7 @@ fn test_mul_small() {
105-
106-
#[test]
107-
#[should_panic]
108-
+#[ignore]
109-
fn test_mul_small_overflow() {
110-
Big::from_u64(0x800000).mul_small(2);
111-
}
112-
@@ -118,12 +123,14 @@ fn test_mul_pow5() {
113-
114-
#[test]
115-
#[should_panic]
116-
+#[ignore]
117-
fn test_mul_pow5_overflow_1() {
118-
Big::from_small(1).mul_pow5(12);
119-
}
120-
121-
#[test]
122-
#[should_panic]
123-
+#[ignore]
124-
fn test_mul_pow5_overflow_2() {
125-
Big::from_small(230).mul_pow5(8);
126-
}
127-
@@ -141,12 +148,14 @@ fn test_mul_digits() {
128-
129-
#[test]
130-
#[should_panic]
131-
+#[ignore]
132-
fn test_mul_digits_overflow_1() {
133-
Big::from_u64(0x800000).mul_digits(&[2]);
134-
}
135-
136-
#[test]
137-
#[should_panic]
138-
+#[ignore]
139-
fn test_mul_digits_overflow_2() {
140-
Big::from_u64(0x1000).mul_digits(&[0, 0x10]);
141-
}
142-
@@ -206,6 +215,7 @@ fn test_get_bit() {
143-
144-
#[test]
145-
#[should_panic]
146-
+#[ignore]
147-
fn test_get_bit_out_of_range() {
148-
Big::from_small(42).get_bit(24);
149-
}
15069
diff --git a/src/libcore/tests/num/mod.rs b/src/libcore/tests/num/mod.rs
15170
index a17c094..5bb11d2 100644
15271
--- a/src/libcore/tests/num/mod.rs
@@ -159,18 +78,6 @@ index a17c094..5bb11d2 100644
15978
fn from_str_issue7588() {
16079
let u: Option<u8> = u8::from_str_radix("1000", 10).ok();
16180
assert_eq!(u, None);
162-
@@ -613,11 +614,9 @@ test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }
163-
test_impl_try_from_signed_to_unsigned_err! { test_try_i128u8, i128, u8 }
164-
test_impl_try_from_signed_to_unsigned_err! { test_try_i128u16, i128, u16 }
165-
test_impl_try_from_signed_to_unsigned_err! { test_try_i128u32, i128, u32 }
166-
-test_impl_try_from_signed_to_unsigned_err! { test_try_i128u64, i128, u64 }
167-
168-
assume_usize_width! {
169-
test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu8, isize, u8 }
170-
- test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128, usize }
171-
172-
cfg_block! {
173-
#[cfg(target_pointer_width = "16")] {
17481
@@ -640,6 +639,7 @@ macro_rules! test_float {
17582
mod $modname {
17683
// FIXME(nagisa): these tests should test for sign of -0.0

src/common.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -81,51 +81,6 @@ pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
8181
}
8282
}
8383

84-
pub(crate) fn codegen_icmp(
85-
fx: &mut FunctionCx<'_, '_, impl Backend>,
86-
intcc: IntCC,
87-
lhs: Value,
88-
rhs: Value,
89-
) -> Value {
90-
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
91-
let rhs_ty = fx.bcx.func.dfg.value_type(rhs);
92-
assert_eq!(lhs_ty, rhs_ty);
93-
if lhs_ty == types::I128 {
94-
// FIXME legalize `icmp.i128` in Cranelift
95-
96-
let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
97-
let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs);
98-
99-
match intcc {
100-
IntCC::Equal => {
101-
let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
102-
let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
103-
fx.bcx.ins().band(lsb_eq, msb_eq)
104-
}
105-
IntCC::NotEqual => {
106-
let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
107-
let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
108-
fx.bcx.ins().bor(lsb_ne, msb_ne)
109-
}
110-
_ => {
111-
// if msb_eq {
112-
// lsb_cc
113-
// } else {
114-
// msb_cc
115-
// }
116-
117-
let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
118-
let lsb_cc = fx.bcx.ins().icmp(intcc, lhs_lsb, rhs_lsb);
119-
let msb_cc = fx.bcx.ins().icmp(intcc, lhs_msb, rhs_msb);
120-
121-
fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
122-
}
123-
}
124-
} else {
125-
fx.bcx.ins().icmp(intcc, lhs, rhs)
126-
}
127-
}
128-
12984
pub(crate) fn codegen_icmp_imm(
13085
fx: &mut FunctionCx<'_, '_, impl Backend>,
13186
intcc: IntCC,

src/intrinsics/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ macro atomic_minmax($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $
126126
let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
127127

128128
// Compare
129-
let is_eq = codegen_icmp($fx, IntCC::SignedGreaterThan, old, $src);
129+
let is_eq = $fx.bcx.ins().icmp(IntCC::SignedGreaterThan, old, $src);
130130
let new = $fx.bcx.ins().select(is_eq, old, $src);
131131

132132
// Write new
@@ -257,7 +257,7 @@ macro simd_cmp {
257257
if let Some(vector_ty) = vector_ty {
258258
let x = $x.load_scalar($fx);
259259
let y = $y.load_scalar($fx);
260-
let val = codegen_icmp($fx, IntCC::$cc, x, y);
260+
let val = $fx.bcx.ins().icmp(IntCC::$cc, x, y);
261261

262262
// HACK This depends on the fact that icmp for vectors represents bools as 0 and !0, not 0 and 1.
263263
let val = $fx.bcx.ins().raw_bitcast(vector_ty, val);
@@ -271,7 +271,7 @@ macro simd_cmp {
271271
$ret,
272272
|fx, lane_layout, res_lane_layout, x_lane, y_lane| {
273273
let res_lane = match lane_layout.ty.kind {
274-
ty::Uint(_) | ty::Int(_) => codegen_icmp(fx, IntCC::$cc, x_lane, y_lane),
274+
ty::Uint(_) | ty::Int(_) => fx.bcx.ins().icmp(IntCC::$cc, x_lane, y_lane),
275275
_ => unreachable!("{:?}", lane_layout.ty),
276276
};
277277
bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
@@ -288,8 +288,8 @@ macro simd_cmp {
288288
$ret,
289289
|fx, lane_layout, res_lane_layout, x_lane, y_lane| {
290290
let res_lane = match lane_layout.ty.kind {
291-
ty::Uint(_) => codegen_icmp(fx, IntCC::$cc_u, x_lane, y_lane),
292-
ty::Int(_) => codegen_icmp(fx, IntCC::$cc_s, x_lane, y_lane),
291+
ty::Uint(_) => fx.bcx.ins().icmp(IntCC::$cc_u, x_lane, y_lane),
292+
ty::Int(_) => fx.bcx.ins().icmp(IntCC::$cc_s, x_lane, y_lane),
293293
_ => unreachable!("{:?}", lane_layout.ty),
294294
};
295295
bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
@@ -869,7 +869,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
869869
let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
870870

871871
// Compare
872-
let is_eq = codegen_icmp(fx, IntCC::Equal, old, test_old);
872+
let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
873873
let new = fx.bcx.ins().select(is_eq, new, old); // Keep old if not equal to test_old
874874

875875
// Write new

src/num.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn codegen_compare_bin_op<'tcx>(
4646
rhs: Value,
4747
) -> CValue<'tcx> {
4848
let intcc = crate::num::bin_op_to_intcc(bin_op, signed).unwrap();
49-
let val = codegen_icmp(fx, intcc, lhs, rhs);
49+
let val = fx.bcx.ins().icmp(intcc, lhs, rhs);
5050
let val = fx.bcx.ins().bint(types::I8, val);
5151
CValue::by_val(val, fx.layout_of(fx.tcx.types.bool))
5252
}

0 commit comments

Comments
 (0)