Skip to content

Commit f62ae7e

Browse files
authoredJul 27, 2024··
Rollup merge of #128271 - Nilstrieb:jump-into-a-can-of-worms-called-float-equality, r=compiler-errors
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. fixes #128243
2 parents 8dd94b8 + f305e18 commit f62ae7e

4 files changed

+137
-0
lines changed
 

‎compiler/rustc_mir_transform/src/jump_threading.rs

+7
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,13 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
509509
BinOp::Ne => ScalarInt::FALSE,
510510
_ => return None,
511511
};
512+
if value.const_.ty().is_floating_point() {
513+
// Floating point equality does not follow bit-patterns.
514+
// -0.0 and NaN both have special rules for equality,
515+
// and therefore we cannot use integer comparisons for them.
516+
// Avoid handling them, though this could be extended in the future.
517+
return None;
518+
}
512519
let value = value.const_.normalize(self.tcx, self.param_env).try_to_scalar_int()?;
513520
let conds = conditions.map(self.arena, |c| Condition {
514521
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
@@ -521,6 +521,16 @@ fn aggregate_copy() -> u32 {
521521
if c == 2 { b.0 } else { 13 }
522522
}
523523

524+
fn floats() -> u32 {
525+
// CHECK-LABEL: fn floats(
526+
// CHECK: switchInt(
527+
528+
// Test for issue #128243, where float equality was assumed to be bitwise.
529+
// When adding float support, it must be ensured that this continues working properly.
530+
let x = if true { -0.0 } else { 1.0 };
531+
if x == 0.0 { 0 } else { 1 }
532+
}
533+
524534
fn main() {
525535
// CHECK-LABEL: fn main(
526536
too_complex(Ok(0));
@@ -535,6 +545,7 @@ fn main() {
535545
disappearing_bb(7);
536546
aggregate(7);
537547
assume(7, false);
548+
floats();
538549
}
539550

540551
// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
@@ -550,3 +561,4 @@ fn main() {
550561
// EMIT_MIR jump_threading.aggregate.JumpThreading.diff
551562
// EMIT_MIR jump_threading.assume.JumpThreading.diff
552563
// EMIT_MIR jump_threading.aggregate_copy.JumpThreading.diff
564+
// EMIT_MIR jump_threading.floats.JumpThreading.diff

0 commit comments

Comments
 (0)
Please sign in to comment.