Skip to content

Commit a354649

Browse files
committed
add a flag to always apply the maximum float error
1 parent 821a44d commit a354649

File tree

7 files changed

+54
-9
lines changed

7 files changed

+54
-9
lines changed

src/tools/miri/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ environment variable. We first document the most relevant and most commonly used
319319
Can be used without a value; in that case the range defaults to `0..64`.
320320
* `-Zmiri-many-seeds-keep-going` tells Miri to really try all the seeds in the given range, even if
321321
a failing seed has already been found. This is useful to determine which fraction of seeds fails.
322+
* `-Zmiri-max-extra-rounding-error` tells Miri to always apply the maximum error to float operations
323+
that do not have a guaranteed precision. The sign of the error is still non-deterministic.
322324
* `-Zmiri-no-extra-rounding-error` stops Miri from adding extra rounding errors to float operations
323325
that do not have a guaranteed precision.
324326
* `-Zmiri-num-cpus` states the number of available CPUs to be reported by miri. By default, the

src/tools/miri/src/bin/miri.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ fn main() {
556556
} else if arg == "-Zmiri-deterministic-floats" {
557557
miri_config.float_nondet = false;
558558
} else if arg == "-Zmiri-no-extra-rounding-error" {
559-
miri_config.float_rounding_error = false;
559+
miri_config.float_rounding_error = miri::FloatRoundingErrorMode::None;
560+
} else if arg == "-Zmiri-max-extra-rounding-error" {
561+
miri_config.float_rounding_error = miri::FloatRoundingErrorMode::Max;
560562
} else if arg == "-Zmiri-strict-provenance" {
561563
miri_config.provenance_mode = ProvenanceMode::Strict;
562564
} else if arg == "-Zmiri-permissive-provenance" {

src/tools/miri/src/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub struct MiriConfig {
112112
/// Whether floating-point operations can behave non-deterministically.
113113
pub float_nondet: bool,
114114
/// Whether floating-point operations can have a non-deterministic rounding error.
115-
pub float_rounding_error: bool,
115+
pub float_rounding_error: FloatRoundingErrorMode,
116116
}
117117

118118
impl Default for MiriConfig {
@@ -154,7 +154,7 @@ impl Default for MiriConfig {
154154
fixed_scheduling: false,
155155
force_intrinsic_fallback: false,
156156
float_nondet: true,
157-
float_rounding_error: true,
157+
float_rounding_error: FloatRoundingErrorMode::Random,
158158
}
159159
}
160160
}

src/tools/miri/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ pub use crate::eval::{MiriConfig, MiriEntryFnType, create_ecx, eval_entry};
142142
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToU64 as _, ToUsize as _};
143143
pub use crate::intrinsics::EvalContextExt as _;
144144
pub use crate::machine::{
145-
AlignmentCheck, AllocExtra, BacktraceStyle, DynMachineCallback, FrameExtra, IsolatedOp,
146-
MachineCallback, MemoryKind, MiriInterpCx, MiriInterpCxExt, MiriMachine, MiriMemoryKind,
147-
PrimitiveLayouts, Provenance, ProvenanceExtra, RejectOpWith, ValidationMode,
145+
AlignmentCheck, AllocExtra, BacktraceStyle, DynMachineCallback, FloatRoundingErrorMode,
146+
FrameExtra, IsolatedOp, MachineCallback, MemoryKind, MiriInterpCx, MiriInterpCxExt,
147+
MiriMachine, MiriMemoryKind, PrimitiveLayouts, Provenance, ProvenanceExtra, RejectOpWith,
148+
ValidationMode,
148149
};
149150
pub use crate::operator::EvalContextExt as _;
150151
pub use crate::provenance_gc::{EvalContextExt as _, LiveAllocs, VisitProvenance, VisitWith};

src/tools/miri/src/machine.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ pub enum ValidationMode {
108108
Deep,
109109
}
110110

111+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
112+
pub enum FloatRoundingErrorMode {
113+
/// Apply a random error (the default).
114+
Random,
115+
/// Don't apply any error.
116+
None,
117+
/// Always apply the maximum error (with a random sign).
118+
Max,
119+
}
120+
111121
/// Extra data stored with each stack frame
112122
pub struct FrameExtra<'tcx> {
113123
/// Extra data for the Borrow Tracker.
@@ -658,7 +668,7 @@ pub struct MiriMachine<'tcx> {
658668
/// Whether floating-point operations can behave non-deterministically.
659669
pub float_nondet: bool,
660670
/// Whether floating-point operations can have a non-deterministic rounding error.
661-
pub float_rounding_error: bool,
671+
pub float_rounding_error: FloatRoundingErrorMode,
662672
}
663673

664674
impl<'tcx> MiriMachine<'tcx> {

src/tools/miri/src/math.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ pub(crate) fn apply_random_float_error<F: rustc_apfloat::Float>(
1515
val: F,
1616
err_scale: i32,
1717
) -> F {
18-
if !ecx.machine.float_nondet || !ecx.machine.float_rounding_error {
18+
if !ecx.machine.float_nondet
19+
|| matches!(ecx.machine.float_rounding_error, FloatRoundingErrorMode::None)
20+
{
1921
return val;
2022
}
2123

@@ -24,7 +26,12 @@ pub(crate) fn apply_random_float_error<F: rustc_apfloat::Float>(
2426
// (When read as binary, the position of the first `1` determines the exponent,
2527
// and the remaining bits fill the mantissa. `PREC` is one plus the size of the mantissa,
2628
// so this all works out.)
27-
let r = F::from_u128(rng.random_range(0..(1 << F::PRECISION))).value;
29+
let r = F::from_u128(match ecx.machine.float_rounding_error {
30+
FloatRoundingErrorMode::Random => rng.random_range(0..(1 << F::PRECISION)),
31+
FloatRoundingErrorMode::Max => (1 << F::PRECISION) - 1, // force max error
32+
FloatRoundingErrorMode::None => unreachable!(),
33+
})
34+
.value;
2835
// Multiply this with 2^(scale - PREC). The result is between 0 and
2936
// 2^PREC * 2^(scale - PREC) = 2^scale.
3037
let err = r.scalbn(err_scale.strict_sub(F::PRECISION.try_into().unwrap()));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! Check that the flags to control the extra rounding error work.
2+
//@revisions: random max none
3+
//@[max]compile-flags: -Zmiri-max-extra-rounding-error
4+
//@[none]compile-flags: -Zmiri-no-extra-rounding-error
5+
#![feature(cfg_select)]
6+
7+
use std::collections::HashSet;
8+
use std::hint::black_box;
9+
10+
fn main() {
11+
let expected = cfg_select! {
12+
random => 13, // FIXME: why is it 13?
13+
max => 2,
14+
none => 1,
15+
};
16+
// Call `sin(0.5)` a bunch of times and see how many different values we get.
17+
let mut values = HashSet::new();
18+
for _ in 0..(expected * 16) {
19+
let val = black_box(0.5f64).sin();
20+
values.insert(val.to_bits());
21+
}
22+
assert_eq!(values.len(), expected);
23+
}

0 commit comments

Comments
 (0)