-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Implement Integer funnel shifts #145690
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
base: master
Are you sure you want to change the base?
Implement Integer funnel shifts #145690
Conversation
This comment has been minimized.
This comment has been minimized.
I see there is a tracking issue, but not an ACP for this. Could you create one? It's an issue template at https://github.com/rust-lang/libs-team/issues. |
81da1f2
to
b91850b
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
b91850b
to
400a29d
Compare
This comment has been minimized.
This comment has been minimized.
400a29d
to
23601d2
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Some changes occurred in compiler/rustc_codegen_gcc Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
5f4c412
to
63fde15
Compare
This comment has been minimized.
This comment has been minimized.
@bjorn3 could you look into this ICE? The clif code looks fine enough, and I don't have enough expertise on clif to see what's going wrong. |
At the very least there is a bug in the Cranelift IR verifier causing it to panic at https://github.com/bytecodealliance/wasmtime/blob/ecda6e330a946821b5edc0a90bd62288cf7df943/cranelift/codegen/src/verifier/mod.rs#L1907 But the thing you are doing wrong is Edit: I'm not sure how that even passed the typecheck pass of the verifier. |
63fde15
to
3d0b3fb
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you update the tracking issue to have signatures matching rust-lang/libs-team#642 (comment)? This should match then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍🏽, could you check if it makes sense (I'm bad at writing docs etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's easier than that: tracking issues should show function signatures in something like an impl uX { ... }
block, no need to write docs :)
3d0b3fb
to
d1f3861
Compare
Should I add more variants of the functions? (e.g. |
I don't think there is any need in the initial PR here. They can be added as a followup |
Also worth mentioning that the hardest part here is getting the intrinsic and actual functionality working, since all the other variants, at least for shifts, just require a few explicit checks. |
In that case, I guess this PR is ready. @rustbot ready |
#[rustc_nounwind] | ||
#[rustc_const_unstable(feature = "funnel_shifts", issue = "145686")] | ||
#[unstable(feature = "funnel_shifts", issue = "145686")] | ||
#[miri::intrinsic_fallback_is_spec] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This attribute means the fallback implementation must check the size to be valid and do unreachable_unchecked
(or equivalent) in that case.
Please add Miri tests to ensure this works correctly: all cases of UB must be detected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the implementation, I don't think you are checking for UB here. Seems like unchecked_funnel_shl(0, 0, n)
will just work for any n
.
Please don't just blindly add attributes like this, make sure you understand them or ask what to do. Adding this attribute incorrectly introduces hard-to-find bugs into the very tools unsafe code authors trust to find bugs for them.
EDIT: Or, are you relying on the checks in unchecked_shr
/unchecked_shl
? That's super subtle, definitely needs a comment at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also clarifying a bit: since the fallbacks work a bit differently than you'd expect, this kind of info should probably be listed in the SAFETY
comment a few lines below. Something like "UB-checking is done inside the called method" and then adding more explicit comments inside the trait implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not actually testing for UB here, I misunderstood what the attribute means. Should I add such UB checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added some assume
calls. @RalfJung could you check if it's ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fair, I don't think that relying on the attribute missing is reasonable, since you can easily just copy intrinsic bodies here. It makes a lot more sense to just have a triage bot reply to any PR that includes the attribute to make sure people get notified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both you personally and miri as a group were pinged in various ways in #145690 (comment). Then the implementation evolved somewhat but it seems reasonable to assume that this PR already had your attention?
If not, should we add a ping rule for that attribute specifically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there should be a ping rule for the attribute just to be safe, since there appears to be a desire to ping the miri group whenever it's used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have support for "ping on specific text", see rust-lang/triagebot#1851. If we did have that then yes we'd definitely use it here.
To be fair, I don't think that relying on the attribute missing is reasonable, since you can easily just copy intrinsic bodies here.
I don't think it is reasonable to copy an attribute you don't understand, and then just hope it works out. I expect contributors to flag things like this explicitly in the PR so it can be discussed.
We could rename the attribute to make it sound more scary, but it's not clear to me that that would help. It's also hard to find a reasonably concise name that captures every aspect of this attribute.
Both you personally and miri as a group were pinged in various ways
That's true, I forgot miri already got pinged because the intrinsic file changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is reasonable to copy an attribute you don't understand, and then just hope it works out.
I agree, it was a mistake on my part. I didn't think it through, and just copied the similar implementations of intrinsics, partly due to my inexperience in implementing rust-intrinsics. Anyway, thanks you all for all the help in pointing out this mistake
- Add a fallback implementation for the intrinsics Co-Authored-By: folkertdev <folkert@folkertdev.nl>
d1f3861
to
c9bab92
Compare
The Miri subtree was changed cc @rust-lang/miri |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Miri checks look good apart from a small nit, thanks!
#[cfg_attr(miri, track_caller)] | ||
#[inline] | ||
unsafe fn unchecked_funnel_shl(self, rhs: Self, shift: u32) -> Self { | ||
// SAFETY: this is guaranteed by the caller |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// SAFETY: this is guaranteed by the caller | |
// This implementation is also used by Miri so we have to check the precondition. | |
// SAFETY: this is guaranteed by the caller |
(same in unchecked_funnel_shr
)
std::intrinsics::unchecked_funnel_shl(1_u32, 2, 5); | ||
std::intrinsics::unchecked_funnel_shl(1_u32, 2, 31); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this, please add calls in src/tools/miri/tests/pass/intrinsics/integer.rs
that check the result.
(Same for funnel_shr)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are already testing the const-eval implementation in library/coretests/tests/num/uint_macros.rs
, do I need to add these tests too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, fair. But then what's the point of these non-failing calls here?
Parent: #145686
ACP: rust-lang/libs-team#642
This implements funnel shifts on primitive integer types. Implements this for cg_llvm, with a fallback impl for everything else
Thanks @folkertdev for the fixes and tests
cc @rust-lang/libs-api