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

indirect-const-stabilize the exact_div intrinsic #139163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants