Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

disable ConstProp of floats #113416

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_mir_transform/src/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
rvalue: &Rvalue<'tcx>,
place: Place<'tcx>,
) -> Option<()> {
if place.ty(&self.ecx.frame().body.local_decls, *self.ecx.tcx).ty.is_floating_point() {
// Our apfloat is old and buggy (https://github.com/rust-lang/rust/issues/113409).
// Let's not risk wrong optimization results -- just do nothing for floats.
// This affects at least binary ops and casts, so just skip all rvalues.
// LLVM has a less buggy apfloat and will take care of const-propagation.
return None;
}

match rvalue {
Rvalue::BinaryOp(op, box (left, right))
| Rvalue::CheckedBinaryOp(op, box (left, right)) => {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
rvalue: &Rvalue<'tcx>,
state: &mut State<Self::Value>,
) -> ValueOrPlace<Self::Value> {
if rvalue.ty(self.local_decls, self.tcx).is_floating_point() {
// Our apfloat is old and buggy (https://github.com/rust-lang/rust/issues/113409).
// Let's not risk wrong optimization results -- just do nothing for floats.
// This affects at least binary ops and casts, so just skip all rvalues.
// LLVM has a less buggy apfloat and will take care of const-propagation.
return ValueOrPlace::TOP;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is better to short-circuit in handle_rvalue then. But it would really help if the docs said whether TOP or BOTTOM represents "could be any value, statically unknown"...


match rvalue {
Rvalue::Cast(
kind @ (CastKind::IntToInt
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/const_prop/issue-102403.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-pass
// compile-flags: -O -Zmir-opt-level=3 -Cno-prepopulate-passes

// Regression test for a broken MIR optimization.
pub fn f() -> f64 {
std::hint::black_box(-1.0) % std::hint::black_box(-1.0)
}

pub fn g() -> f64 {
-1.0 % -1.0
}

pub fn main() {
assert_eq!(f().signum(), g().signum());
}
8 changes: 8 additions & 0 deletions tests/ui/const_prop/issue-113407.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-pass
// compile-flags: -O -Zmir-opt-level=3 -Cno-prepopulate-passes

// Regression test for a broken MIR optimization.
pub fn main() {
let f = f64::from_bits(0x19873cc2) as f32;
assert_eq!(f.to_bits(), 0);
}