-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
f64::log
precision change in 1.82 nightly
#128386
Comments
Bisection seems to indicate this comes after updating out compiler-builtins crate: 80d8270 I guess this is "expected"? cc: @nicholasbishop |
This is probably a result of the linkage change in rust-lang/compiler-builtins#594 since the actual implementation hasn't changed in 5 years https://github.com/rust-lang/libm/blob/279e5f6abe0a2ca9066962d9ec894f0df1f417ac/src/math/log.rs.. Could you |
Obviously we want to have the correct values and will try to make things work, thanks bringing it up. Just note that the docs are extremely lenient here:
I think that if you are relying on exact results for CI then that may prove to be pretty platform-dependent. |
FWIW, we just hit it in our CI too; looks like the runtime value no longer equals the compiler constant-folded result on our platforms. (EDIT: forgot to add: we noticed this with |
I believe that is #124364 I'm deliberately not labeling this issue as I-unsound because I think this issue is just noticing a specific instance of the above-linked issue. |
Arguably, this is a true regression, and not a simple "log is fuzzy anyway". Reason being that the resulting error is 2 * f64::EPSILON, and not one ulp as advertised in the implementation of the log function. FYI dwarfdump is attached. As far as I could read it, it is using the compiler's version of log function. Also interesting that in release build there is no issue. |
Thanks for posting that, relevant bit of the above:
Just to make sure, could you do the same with an older nightly and check that the system version was getting linked before? |
I do not have a previous nightly left, but 1.81.0-beta.2 that does not exhibit the bug produces the following The specific section for log function that you refer to is not there. |
I looked into it and it seems that the Since this isn't the intended behavior, we probably have to revert rust-lang/compiler-builtins#594 and selectively only provide math symbols on targets that don't have a system libm. |
Is system libm a good idea? It is rather unlikely that it would have support for recent CPUs in distros like debian... Not sure it would matter though (as log does not seem to be simd-friendly anyway). |
An alternative would be to just fix our |
Where is this advertised? Certainly not in our documentation (to the contrary, the documentation explicitly says that the precision is not guaranteed). Comments in the implementation are not stable guarantees. So no, there's no regression here in the formal sense, though getting higher precision could still be nice. |
Well I think it is not about accuracy but about consistency. The calculation may not be accurate as such, but it should at least be consistent, i.e. you should not be getting different answers depending on the mood of the optimizer. And if there is an error range that is allowed, it should be mapped out and announced to the user. If no error bound is guaranteed, this function could just as well always return 42. |
We explicitly document that the optimizer can affect results of these functions. You may not be happy about that, but there's no bug here. If you want to change the specification for these operations to require them to be deterministic, that's a feature request that should go through our usual process -- like exploring what it would actually take to achieve that (AFAIK this is very non-trivial and might even require LLVM changes), and writing up the rationale and the arguments against and in favor in a pre-RFC or a t-libs ACP or something like that. And yes there are arguments against this; achieving this will likely require disabling some optimizations and hence cost performance. I don't have the expertise to help explore the design space here; you may find folks that can help with this on https://internals.rust-lang.org/ or on Zulip.
Please stop using strawman arguments, that is not constructive. Rust has many places where we don't make hard guarantees for various reasons, but we still aim to provide reasonable outcomes and would consider it a bug if we failed to do so. The line between "reasonable" and "unreasonable" is fuzzy and subjective. Obviously, a |
To anyone interested in concretely fixing this, consider submitting a PR to port the new log implementation from musl to our implementation in the libm crate, which is used by compiler-builtins. Also it would be good to check if any other math functions in libm are similarly outdated compared to the latest implementation in musl. |
I can look into porting the musl code at some point, could be fun to mess with some bits. Are there any guidelines specific to that kind of code? I imagine using platform-specific SIMD is a big no-no there, right? |
If a new log implementation is going to be ported, it may as well be one that gives correctly rounded results in all cases. Looking at this paper, MUSL's implementation doesn't provide correctly rounded results in all cases, whereas it appears that LLVM libc has such an implementation for both |
Regarding platform-specific SIMD: AFAIK it should be fine as long as there is an equivalent non-SIMD implementation for other platforms and for when compiling with the |
@rustbot label +A-floating-point |
Ok I see. I'll try to dig into this properly at some point in the next week or two, will post here if I've given up. |
Both See also my implementation of |
There is ongoing work to improve our @Amjad50 could you post a revert of rust-lang/compiler-builtins#594 and rust-lang/compiler-builtins#577 as applicable, and open an issue (in builtins) to reapply it? |
Yah, I think I can just revert to the old expected behavior for windows/Mac/Linux platforms and only link include the math module for other targets. I'll do it later today. But I'm a bit confused as to why this happened, |
I don't think it was making the symbols weak that caused the problem, instead that we made them available at all. rust-lang/rust didn't get an updated compiler-builtins between when those PRs merged and about a month ago, IIRC. They aren't getting turned strong, the system math symbols are just also weak (referencing The precision effects here are one concern, but another concern I have is that our routines may not be optimized as well as what is available from system libm. So I think it worth some effort to ensure that (which is in progress) before we effectively make our symbols be used by default everywhere, or otherwise change things so our symbols are truly a fallback rather than more or less the default. Thanks for being willing to pick this up. I agree that we only need to change back on platforms that provide their own symbols. |
This fixes such as (rust-lang/rust#128386) where, our implementation is being used on systems where there is already `math` library and its more performant and accurate. So with this change, linux will go back to the previous behavior and not include these functions, windows and apple were generally not affected. Looking at the targets we have builtin now in rust, everything else is probably good to have the math symbols. > A note on the above, the `hermit` os uses `libm` directly for itself, > but I think its Ok to keep providing math in `compiler_builtin` for it, > its technically the same implementation either from `compiler_builtin` > or `hermit-builtins`. Signed-off-by: Amjad Alsharafi <26300843+Amjad50@users.noreply.github.com>
…r=tgross35 Update `compiler_builtins` to `0.1.123` Includes rust-lang/compiler-builtins#680 and fixes rust-lang#128386. Fixed by not including math symbols of `compiler_builtins` into any `unix` target or `wasi`, old behavior is restored r? tgross35
…r=tgross35 Update `compiler_builtins` to `0.1.123` Includes rust-lang/compiler-builtins#680 and fixes rust-lang#128386. Fixed by not including math symbols of `compiler_builtins` into any `unix` target or `wasi`, old behavior is restored r? tgross35
Rollup merge of rust-lang#129715 - Amjad50:update-compiler-builtins, r=tgross35 Update `compiler_builtins` to `0.1.123` Includes rust-lang/compiler-builtins#680 and fixes rust-lang#128386. Fixed by not including math symbols of `compiler_builtins` into any `unix` target or `wasi`, old behavior is restored r? tgross35
Update `compiler_builtins` to `0.1.123` Includes rust-lang/compiler-builtins#680 and fixes rust-lang/rust#128386. Fixed by not including math symbols of `compiler_builtins` into any `unix` target or `wasi`, old behavior is restored r? tgross35
Our CI started to report an error today with nightly on Linux (other platforms are fine).
Simplified the test case is because of a small difference in the computation of
log
.(I know it's bad practice to do equality tests on float, but I thought I'd report this behavior change anyway as i don't know if this was intentional).
Code
I tried this code:
I expected to see this happen:
Hello, world! 2
and no panics. As this happens in stableInstead, this happened:
Version with regression
rustc 1.82.0-nightly (612a33f 2024-07-29)
(Linux only)
The text was updated successfully, but these errors were encountered: