Skip to content

Add non-determinism to algebraic intrinsics. #4289

New issue

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

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

Already on GitHub? Sign in to your account

Closed
bjoernager opened this issue Apr 23, 2025 · 5 comments · Fixed by rust-lang/rust#140439
Closed

Add non-determinism to algebraic intrinsics. #4289

bjoernager opened this issue Apr 23, 2025 · 5 comments · Fixed by rust-lang/rust#140439
Labels
A-intrinsics Area: Affects out implementation of Rust intrinsics C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement

Comments

@bjoernager
Copy link
Contributor

bjoernager commented Apr 23, 2025

Tracking issue: rust-lang/rust#136469

Since rust-lang/rust#140172, the result of a call to any of the algebraic_add, algebraic_sub, algebraic_mul, algebraic_div, or algebraic_rem intrinsics has been deterministic in Miri.

Valid programmes do not depend on these computations being exact, thus Miri should add some non-determinism so that any such dependence is more likely to be caught.

@RalfJung RalfJung added C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement A-intrinsics Area: Affects out implementation of Rust intrinsics labels Apr 23, 2025
@workingjubilee
Copy link
Member

workingjubilee commented Apr 28, 2025

What kind of nondet are you thinking of?

I mean, we should probably try to reason a bit about the actual transformations that are allowed rather than just pulling floats out of a random number generator. Valid programs might still use mathematical reasoning (...with some appropriate error bars on either side, of course, to account for the differences between float-exact and real-exact computations).

One option I can think of is to flip a coin and then decide whether or not to compute the next few algebraic math operations to an absurd precision, without rounding inbetween?

@bjoernager
Copy link
Contributor Author

bjoernager commented Apr 29, 2025

Initially, these intrinsics used miri::intrinsics::apply_random_float_error_ulp_to_imm on the result before returning:

// Apply a relative error of 4ULP to simulate non-deterministic precision loss
// due to optimizations.
let res = apply_random_float_error_to_imm(this, res, 2 /* log2(4) */)?;

The final value of 4ULP was set in rust-lang/rust#136457.

There had been some concerns about these computations being conditionally-exact. It seems to me that this is the optimal solution.

@RalfJung
Copy link
Member

Formally, the algebraic operations return an arbitrary floating-point value. We do not make any guarantees on precision, and it is impossible for clients to do soundness-critical reasoning about what these functions return.

In practice, doing this in Miri would probably not help in finding real bugs, people would just disable Miri on the affected tests... so what Miri does is to just appliy a small imprecision, to at least catch code that incorrectly assumes an exact result.

tgross35 added a commit to tgross35/rust that referenced this issue Apr 29, 2025
…et, r=oli-obk

miri: algebraic intrinsics: bring back float non-determinism

Fixes rust-lang/miri#4289
Cc `@bjoernager`
r? `@oli-obk`
Zalathar added a commit to Zalathar/rust that referenced this issue Apr 30, 2025
…et, r=oli-obk

miri: algebraic intrinsics: bring back float non-determinism

Fixes rust-lang/miri#4289
Cc ``@bjoernager``
r? ``@oli-obk``
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 30, 2025
Rollup merge of rust-lang#140439 - RalfJung:miri-algebraic-float-nondet, r=oli-obk

miri: algebraic intrinsics: bring back float non-determinism

Fixes rust-lang/miri#4289
Cc ```@bjoernager```
r? ```@oli-obk```
@nnethercote
Copy link
Contributor

Uh, github is claiming I closed this as completed. I've never seen this issue. I don't know what's going on.

@saethlin
Copy link
Member

saethlin commented May 1, 2025

You have permission to close issues, and you just pushed a commit to the default branch of your fork that uses a closing keyword with this issue number.

I and many other contributors don't keep the master branch of our forks up to date anymore because of this problem.

Fortunately in this case, the issue should be closed.

github-actions bot pushed a commit that referenced this issue May 1, 2025
…-obk

miri: algebraic intrinsics: bring back float non-determinism

Fixes #4289
Cc ```@bjoernager```
r? ```@oli-obk```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-intrinsics Area: Affects out implementation of Rust intrinsics C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants