Skip to content

indirect-const-stabilize the exact_div intrinsic #139163

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

Merged
merged 1 commit into from
Apr 13, 2025

Conversation

scottmcm
Copy link
Member

@rustbot
Copy link
Collaborator

rustbot commented Mar 31, 2025

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 31, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 31, 2025

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@scottmcm scottmcm added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 31, 2025
@scottmcm
Copy link
Member Author

Hello lang folks!

I propose we const-stabilize the exact_div intrinsic for indirect use.

This is the equivalent of udiv exact or sdiv exact in LLVM: a division that, in addition to needing to not wrap or divide by zero, is UB if the numerator is not an exact multiple of the denominator. (Well, in LLVM it's poison, but to Rust the intrinsic is UB.)

It was originally added in #61885 for use converting the byte instance between pointers in the slice iterator to item differences, since an iterator over i32s can never have pointers 6 bytes apart, for example. That's no longer done in library code, though, as instead it uses the ptr_offset_from_unsigned intrinsic that uses the same mechanism internally.

It's coming up now because as_chunks uses it internally, so we'd need to allow this to stabilize that with its current implementation. (Technically as_chunks doesn't need it, but as_chunks_unchecked uses it.)

I think we should just stabilize it, even if libs decides to change that implementation, because it's not a big deal to allow in const. Everything it can do could be done with hint::assert_unchecked(a % b == 0), so we're not enabling any complicated new things. Abstracted a bit, this is just the simple case of value-based UB on integers that doesn't open any of the complicated doors, because it just cares about the values, not about state (in the machine or shadowy state).

And it would be legal to implement it as just /, so it's not forcing us to have some complicated implementation going forward. (Not that the implementation today is hard, and most of the pieces in the compiler to deal with it we'd need anyway for offset_from_unsigned.)

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented Mar 31, 2025

Team member @scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Mar 31, 2025
@RalfJung
Copy link
Member

RalfJung commented Mar 31, 2025

FWIW, we don't yet have confirmation whether slice_as_chunks is also part of the t-libs-api FCP in #74985, so it's not clear yet whether exact_div will be needed. But I agree it doesn't hurt to const-stabilize this in either case.

@@ -2786,6 +2786,7 @@ pub const fn carrying_mul_add<T: ~const fallback::CarryingMulAdd<Unsigned = U>,
/// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
///
/// This intrinsic does not have a stable counterpart.
#[rustc_intrinsic_const_stable_indirect]
Copy link
Member

@RalfJung RalfJung Mar 31, 2025

Choose a reason for hiding this comment

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

With this, you should be able to remove the rustc_const_unstable from the slice_as_chunks methods. (They will still remain unstable due to #[unstable].)

Doing so is a good conformation to ensure there's nothing else preventing their const stabilization.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, good point! Thanks!

@RalfJung
Copy link
Member

As usual please Cc @rust-lang/wg-const-eval on const stabilizations.

This is a simple intrinsic, acting on immediate values with a clear spec. So no concerns from my side.

@scottmcm scottmcm force-pushed the stabilize-exact_div branch from ee4e4f9 to 308d5ae Compare March 31, 2025 06:38
@traviscross
Copy link
Contributor

All sounds right.

@rfcbot reviewed

@jhpratt
Copy link
Member

jhpratt commented Mar 31, 2025

LGTM; r=me when appropriate

@traviscross traviscross added the I-lang-nominated Nominated for discussion during a lang team meeting. label Apr 1, 2025
@tmandry
Copy link
Member

tmandry commented Apr 2, 2025

@rfcbot reviewed

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Apr 2, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Apr 2, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@traviscross traviscross removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Apr 2, 2025
@scottmcm
Copy link
Member Author

FWIW, we don't yet have confirmation whether slice_as_chunks is also part of the t-libs-api FCP

For tracking purposes, this is resolved now that libs-api redid the FCP in #74985 (comment) -- it includes as_chunks_unchecked, which is the one that actually cares about this intrinsic.

@riking
Copy link

riking commented Apr 11, 2025

And it would be legal to implement it as just /, so it's not forcing us to have some complicated implementation going forward.

This intrinsic should have a fallback implementation of exactly that, possibly with the assert(a%b), now that fallbacks are possible.

@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label Apr 12, 2025
@rfcbot rfcbot added to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Apr 12, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Apr 12, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@RalfJung
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Apr 12, 2025

📌 Commit 56c08d9 has been approved by RalfJung

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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 12, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 13, 2025
…enton

Rollup of 8 pull requests

Successful merges:

 - rust-lang#139163 (indirect-const-stabilize the `exact_div` intrinsic)
 - rust-lang#139276 (Revert "Disable `f16` on Aarch64 without `neon`")
 - rust-lang#139315 (Switch `time` to `jiff` for time formatting in ICE dumps)
 - rust-lang#139382 (Update windows-bindgen to 0.61.0)
 - rust-lang#139688 (rustdoc-search: add unbox flag to Result aliases)
 - rust-lang#139701 (docs: clarify uint exponent for `is_power_of_two`)
 - rust-lang#139705 (Removed outdated ui test suite README, give reasons for disabled tests)
 - rust-lang#139713 (Fix typo in documentation)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 13, 2025
…enton

Rollup of 8 pull requests

Successful merges:

 - rust-lang#139163 (indirect-const-stabilize the `exact_div` intrinsic)
 - rust-lang#139276 (Revert "Disable `f16` on Aarch64 without `neon`")
 - rust-lang#139315 (Switch `time` to `jiff` for time formatting in ICE dumps)
 - rust-lang#139382 (Update windows-bindgen to 0.61.0)
 - rust-lang#139688 (rustdoc-search: add unbox flag to Result aliases)
 - rust-lang#139701 (docs: clarify uint exponent for `is_power_of_two`)
 - rust-lang#139705 (Removed outdated ui test suite README, give reasons for disabled tests)
 - rust-lang#139713 (Fix typo in documentation)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2722e82 into rust-lang:master Apr 13, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 13, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 13, 2025
Rollup merge of rust-lang#139163 - scottmcm:stabilize-exact_div, r=RalfJung

indirect-const-stabilize the `exact_div` intrinsic

See rust-lang#74985 (comment)
@scottmcm scottmcm deleted the stabilize-exact_div branch April 13, 2025 07:54
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Apr 19, 2025
…lfJung

indirect-const-stabilize the `exact_div` intrinsic

See rust-lang#74985 (comment)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants