Skip to content

Commit eca0a7e

Browse files
Noratriebpietroalbini
authored andcommitted
Disable jump threading of float equality
Jump threading stores values as `u128` (`ScalarInt`) and does its comparisons for equality as integer comparisons. This works great for integers. Sadly, not everything is an integer. Floats famously have wonky equality semantcs, with `NaN!=NaN` and `0.0 == -0.0`. This does not match our beautiful integer bitpattern equality and therefore causes things to go horribly wrong. While jump threading could be extended to support floats by remembering that they're floats in the value state and handling them properly, it's signficantly easier to just disable it for now.
1 parent 0514789 commit eca0a7e

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

compiler/rustc_mir_transform/src/jump_threading.rs

+7
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
494494
BinOp::Ne => ScalarInt::FALSE,
495495
_ => return None,
496496
};
497+
if value.const_.ty().is_floating_point() {
498+
// Floating point equality does not follow bit-patterns.
499+
// -0.0 and NaN both have special rules for equality,
500+
// and therefore we cannot use integer comparisons for them.
501+
// Avoid handling them, though this could be extended in the future.
502+
return None;
503+
}
497504
let value = value.const_.normalize(self.tcx, self.param_env).try_to_scalar_int()?;
498505
let conds = conditions.map(self.arena, |c| Condition {
499506
value,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
- // MIR for `floats` before JumpThreading
2+
+ // MIR for `floats` after JumpThreading
3+
4+
fn floats() -> u32 {
5+
let mut _0: u32;
6+
let _1: f64;
7+
let mut _2: bool;
8+
let mut _3: bool;
9+
let mut _4: f64;
10+
scope 1 {
11+
debug x => _1;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
StorageLive(_2);
17+
_2 = const true;
18+
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
19+
+ goto -> bb1;
20+
}
21+
22+
bb1: {
23+
_1 = const -0f64;
24+
goto -> bb3;
25+
}
26+
27+
bb2: {
28+
_1 = const 1f64;
29+
goto -> bb3;
30+
}
31+
32+
bb3: {
33+
StorageDead(_2);
34+
StorageLive(_3);
35+
StorageLive(_4);
36+
_4 = _1;
37+
_3 = Eq(move _4, const 0f64);
38+
switchInt(move _3) -> [0: bb5, otherwise: bb4];
39+
}
40+
41+
bb4: {
42+
StorageDead(_4);
43+
_0 = const 0_u32;
44+
goto -> bb6;
45+
}
46+
47+
bb5: {
48+
StorageDead(_4);
49+
_0 = const 1_u32;
50+
goto -> bb6;
51+
}
52+
53+
bb6: {
54+
StorageDead(_3);
55+
StorageDead(_1);
56+
return;
57+
}
58+
}
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
- // MIR for `floats` before JumpThreading
2+
+ // MIR for `floats` after JumpThreading
3+
4+
fn floats() -> u32 {
5+
let mut _0: u32;
6+
let _1: f64;
7+
let mut _2: bool;
8+
let mut _3: bool;
9+
let mut _4: f64;
10+
scope 1 {
11+
debug x => _1;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
StorageLive(_2);
17+
_2 = const true;
18+
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
19+
+ goto -> bb1;
20+
}
21+
22+
bb1: {
23+
_1 = const -0f64;
24+
goto -> bb3;
25+
}
26+
27+
bb2: {
28+
_1 = const 1f64;
29+
goto -> bb3;
30+
}
31+
32+
bb3: {
33+
StorageDead(_2);
34+
StorageLive(_3);
35+
StorageLive(_4);
36+
_4 = _1;
37+
_3 = Eq(move _4, const 0f64);
38+
switchInt(move _3) -> [0: bb5, otherwise: bb4];
39+
}
40+
41+
bb4: {
42+
StorageDead(_4);
43+
_0 = const 0_u32;
44+
goto -> bb6;
45+
}
46+
47+
bb5: {
48+
StorageDead(_4);
49+
_0 = const 1_u32;
50+
goto -> bb6;
51+
}
52+
53+
bb6: {
54+
StorageDead(_3);
55+
StorageDead(_1);
56+
return;
57+
}
58+
}
59+

tests/mir-opt/jump_threading.rs

+12
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,16 @@ fn assume(a: u8, b: bool) -> u8 {
506506
}
507507
}
508508

509+
fn floats() -> u32 {
510+
// CHECK-LABEL: fn floats(
511+
// CHECK: switchInt(
512+
513+
// Test for issue #128243, where float equality was assumed to be bitwise.
514+
// When adding float support, it must be ensured that this continues working properly.
515+
let x = if true { -0.0 } else { 1.0 };
516+
if x == 0.0 { 0 } else { 1 }
517+
}
518+
509519
fn main() {
510520
// CHECK-LABEL: fn main(
511521
too_complex(Ok(0));
@@ -520,6 +530,7 @@ fn main() {
520530
disappearing_bb(7);
521531
aggregate(7);
522532
assume(7, false);
533+
floats();
523534
}
524535

525536
// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
@@ -534,3 +545,4 @@ fn main() {
534545
// EMIT_MIR jump_threading.disappearing_bb.JumpThreading.diff
535546
// EMIT_MIR jump_threading.aggregate.JumpThreading.diff
536547
// EMIT_MIR jump_threading.assume.JumpThreading.diff
548+
// EMIT_MIR jump_threading.floats.JumpThreading.diff

0 commit comments

Comments
 (0)