Skip to content
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

Fix non-associativity of Instant math on aarch64-apple-darwin targets #103594

Merged
merged 3 commits into from
Nov 18, 2022
Merged

Fix non-associativity of Instant math on aarch64-apple-darwin targets #103594

merged 3 commits into from
Nov 18, 2022

Conversation

maniwani
Copy link
Contributor

@maniwani maniwani commented Oct 26, 2022

This is a duplicate of #94100 (since the original author is unresponsive), which resolves #91417.

On aarch64-apple-darwin targets, the internal resolution of Instant is lower than that of Duration, so math between them becomes non-associative with small-enough durations.

This PR makes this target use the standard Unix implementation (where Instant has 1ns resolution), but with CLOCK_UPTIME_RAW so it still returns the same values as mach_absolute_time1.

(Edit: I need someone to confirm that this still works, I do not have access to an M1 device.)

Footnotes

  1. https://www.manpagez.com/man/3/clock_gettime/

@rustbot
Copy link
Collaborator

rustbot commented Oct 26, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Oct 26, 2022
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 26, 2022
@thomcc
Copy link
Member

thomcc commented Oct 27, 2022

I have a good amount of familiarity with these targets (and can test on a fairly wide range of them), so I'd like to take this. I'm a little hesitant about this change, but the current situation is admittedly fairly undesirable. I'll probably get to the review this weekend.

r? thomcc

@rustbot rustbot assigned thomcc and unassigned Mark-Simulacrum Oct 27, 2022
@thomcc
Copy link
Member

thomcc commented Nov 13, 2022

This doesn't compile on aarch64-apple-darwin.

error[E0308]: mismatched types
   --> library/std/src/sys/unix/time.rs:293:39
    |
293 |             const clock_id: clock_t = libc::CLOCK_UPTIME_RAW;
    |                                       ^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `u32`

error[E0308]: mismatched types
   --> library/std/src/sys/unix/time.rs:323:43
    |
323 |             SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
    |                             ------------- ^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `u32`
    |                             |
    |                             arguments to this function are incorrect
    |
note: associated function defined here
   --> library/std/src/sys/unix/time.rs:333:16
    |
333 |         pub fn now(clock: clock_t) -> Timespec {
    |                ^^^ --------------
help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit
    |
323 |             SystemTime { t: Timespec::now(libc::CLOCK_REALTIME.try_into().unwrap()) }
    |                                                               ++++++++++++++++++++

error[E0308]: mismatched types
    --> library/std/src/sys/unix/time.rs:362:46
     |
362  |             cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
     |                          ------------------- ^^^^^ expected `u32`, found `i32`
     |                          |
     |                          arguments to this function are incorrect
     |
note: function defined here
    --> /Users/thom/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.135/src/unix/bsd/apple/mod.rs:5092:12
     |
5092 |     pub fn clock_gettime(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int;
     |            ^^^^^^^^^^^^^
help: you can convert an `i32` to a `u32` and panic if the converted value doesn't fit
     |
362  |             cvt(unsafe { libc::clock_gettime(clock.try_into().unwrap(), t.as_mut_ptr()) }).unwrap();
     |                                                   ++++++++++++++++++++

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2022
@maniwani
Copy link
Contributor Author

maniwani commented Nov 13, 2022

I think the problem is the type alias used here.

#[cfg(not(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon")))]
pub type clock_t = libc::c_int;
#[cfg(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon"))]
pub type clock_t = libc::c_ulong;

Is there any reason to even have these? libc seems to have a clockid_t alias for every platform already.

@thomcc
Copy link
Member

thomcc commented Nov 13, 2022

If it's in libc on every platform there's no reason to have it in std::sys

@rust-log-analyzer

This comment has been minimized.

@thomcc
Copy link
Member

thomcc commented Nov 13, 2022

Hmm, it seems that miri doesn't emulate clock_gettime on macOS. @RalfJung, any opinions as to how you'd like us to handle this?

@thomcc
Copy link
Member

thomcc commented Nov 14, 2022

This looks good to me. Seems to work on M1 too. libc::CLOCK_UPTIME_RAW is the same as mach_absolute_time effectively, which we already use, so I don't see a need to deliberate over this like we have on some other platforms. Thanks.

@bors r+

(Also, I like your username and avatar)

@bors
Copy link
Contributor

bors commented Nov 14, 2022

📌 Commit 015ab65 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 14, 2022
@thomcc
Copy link
Member

thomcc commented Nov 14, 2022

Ah, wait, this is failing. I forgot that we need to resolve the situation with miri.

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 14, 2022
@thomcc
Copy link
Member

thomcc commented Nov 14, 2022

Probably the best solution is to continue using the x86_64 code on aarch64 when under miri). Pretty soon we should be able to globally improve the handling of this (assuming the min macOS version bump goes through).

@RalfJung
Copy link
Member

RalfJung commented Nov 14, 2022 via email

@@ -149,7 +149,11 @@ impl From<libc::timespec> for Timespec {
}
}

#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
#[cfg(any(
all(target_os = "macos", not(target_arch = "aarch64")),
Copy link
Member

Choose a reason for hiding this comment

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

I guess that would be just adding a not(miri) here. Please also open an issue at https://github.com/rust-lang/miri/issues to track removing this Miri hack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

@thomcc
Copy link
Member

thomcc commented Nov 14, 2022

Is the plan to use clock_gettime on all macos targets?

Plausibly. I'd probably prefer using clock_gettime_nsec_np given that what we actually want is nanoseconds, and apple's docs recommend that you prefer it if possible. But clock_gettime is an easier migration path.

@RalfJung
Copy link
Member

The 'np' makes me thing that will require a dedicated implementation. ;) But it's fine, these clock APIs are usually not a lot of work. (Unlike synchronization APIs...)

@thomcc
Copy link
Member

thomcc commented Nov 14, 2022

The 'np' makes me thing that will require a dedicated implementation

Yes, most likely. But yeah, I don't expect it to be very complex.

@thomcc
Copy link
Member

thomcc commented Nov 14, 2022

LGTM

@bors r+

@bors
Copy link
Contributor

bors commented Nov 14, 2022

📌 Commit f4f5159 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 14, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 18, 2022
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#101162 (Migrate rustc_resolve to use SessionDiagnostic, part # 1)
 - rust-lang#103386 (Don't allow `CoerceUnsized` into `dyn*` (except for trait upcasting))
 - rust-lang#103405 (Detect incorrect chaining of if and if let conditions and recover)
 - rust-lang#103594 (Fix non-associativity of `Instant` math on `aarch64-apple-darwin` targets)
 - rust-lang#104006 (Add variant_name function to `LangItem`)
 - rust-lang#104494 (Migrate GUI test to use functions)
 - rust-lang#104516 (rustdoc: clean up sidebar width CSS)
 - rust-lang#104550 (fix a typo)

Failed merges:

 - rust-lang#104554 (Use `ErrorGuaranteed::unchecked_claim_error_was_emitted` less)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6419151 into rust-lang:master Nov 18, 2022
@rustbot rustbot added this to the 1.67.0 milestone Nov 18, 2022
bors added a commit to rust-lang/miri that referenced this pull request Nov 19, 2022
implement clock_gettime on macos

and pull in rustc changes so we can test this against rust-lang/rust#103594.
bors added a commit to rust-lang/miri that referenced this pull request Nov 19, 2022
implement clock_gettime on macos

and pull in rustc changes so we can test this against rust-lang/rust#103594.

Fixes #2664
RalfJung pushed a commit to RalfJung/rust that referenced this pull request Nov 20, 2022
implement clock_gettime on macos

and pull in rustc changes so we can test this against rust-lang#103594.

Fixes rust-lang/miri#2664
flip1995 pushed a commit to flip1995/rust that referenced this pull request Nov 21, 2022
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#101162 (Migrate rustc_resolve to use SessionDiagnostic, part # 1)
 - rust-lang#103386 (Don't allow `CoerceUnsized` into `dyn*` (except for trait upcasting))
 - rust-lang#103405 (Detect incorrect chaining of if and if let conditions and recover)
 - rust-lang#103594 (Fix non-associativity of `Instant` math on `aarch64-apple-darwin` targets)
 - rust-lang#104006 (Add variant_name function to `LangItem`)
 - rust-lang#104494 (Migrate GUI test to use functions)
 - rust-lang#104516 (rustdoc: clean up sidebar width CSS)
 - rust-lang#104550 (fix a typo)

Failed merges:

 - rust-lang#104554 (Use `ErrorGuaranteed::unchecked_claim_error_was_emitted` less)

r? `@ghost`
`@rustbot` modify labels: rollup
@maniwani maniwani deleted the fix-issue-91417 branch January 5, 2023 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Instant + Duration produce wrong result on aarch64-apple-darwin
8 participants