Generate valid_ranges for enums sign-agnostically - #159509
Conversation
|
Some changes occurred in compiler/rustc_hir/src/attrs cc @jdonszelmann, @JonathanBrouwer Some changes occurred in compiler/rustc_attr_parsing |
This comment has been minimized.
This comment has been minimized.
89681fc to
fc374f1
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
fc374f1 to
f8b4450
Compare
|
Ok, the first version I'd misunderstood how we need to pick tag signedness, so redid that and added a bunch of new tests because there were disturbingly few failures when I'd probably broken a ton of stuff (albeit subtly). OP updated as well to describe the fixed approach. |
|
Not expecting a perf win (I think this does a bit more work now), but to make sure it's not horrible: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Generate `valid_range`s for enums sign-agnostically
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (8d18ada): comparison URL. Overall result: no relevant changes - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countThis perf run didn't have relevant results for this metric. Max RSS (memory usage)This perf run didn't have relevant results for this metric. CyclesResults (primary -2.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 489.126s -> 483.723s (-1.10%) |
This comment has been minimized.
This comment has been minimized.
f8b4450 to
c1e94d9
Compare
|
This PR was rebased onto a different main 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. |
| /// // Both `100..=228` and `..=228 | 100..` are the same size, but we pick the one without zero. | ||
| /// let range = WrappingRange::smallest_range_containing([100, 228], Size::from_bytes(1)); | ||
| /// assert_eq!(range.unwrap(), WrappingRange { start: 100, end: 228 }); | ||
| /// // All 4 possibilities give a length of 193. We pick the one that doesn't contain zero. |
There was a problem hiding this comment.
There are several not containing it, we pick the one that starts closest to zero
There was a problem hiding this comment.
I elaborated this comment; there's only one that doesn't include zero.
(Said otherwise, we have to include all the gaps except one, and there's obviously at most one gap containing zero, so the one range that doesn't include zero is the range that picks that gap.)
c1e94d9 to
9cbba6e
Compare
|
Does this affect #159433? |
|
@theemathas Not at all, because those types already have the correct valid range: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=555d30b3989104ea121c0d35d216a9b1 That bug is just |
|
@bors r+ |
…uwer Rollup of 11 pull requests Successful merges: - #158460 (Remove llvm_enzyme feature outside of bootstrap) - #159509 (Generate `valid_range`s for enums sign-agnostically) - #159632 (CFI: Add support for the adt_const_params feature) - #159671 (Add semver check test command for checking API compatibility of stdlib) - #157058 (Rustdoc label badge for notable traits) - #159717 (Add `-Zimplicit-sysroot-deps`) - #159850 (Add regression test for closure in array-length const generic) - #159994 (Show jobs where a given test was executed in `test-dashboard`) - #160110 (convert rustc_hir::Target inherent methods to From impls) - #160123 (add additional license option for third-party dependencies) - #160131 (bootstrap: remove temporary bors email lookup)
…uwer Rollup of 11 pull requests Successful merges: - #158460 (Remove llvm_enzyme feature outside of bootstrap) - #159509 (Generate `valid_range`s for enums sign-agnostically) - #159632 (CFI: Add support for the adt_const_params feature) - #159671 (Add semver check test command for checking API compatibility of stdlib) - #157058 (Rustdoc label badge for notable traits) - #159717 (Add `-Zimplicit-sysroot-deps`) - #159850 (Add regression test for closure in array-length const generic) - #159994 (Show jobs where a given test was executed in `test-dashboard`) - #160110 (convert rustc_hir::Target inherent methods to From impls) - #160123 (add additional license option for third-party dependencies) - #160131 (bootstrap: remove temporary bors email lookup)
Rollup merge of #159509 - scottmcm:redo-valid-range, r=oli-obk Generate `valid_range`s for enums sign-agnostically Prompted by #159438 (comment), this reworks the generation of the tag layout information for enums. Having gone down the wrong path at first here, I think the core trouble this is having is that the current calculation is trying to serve two incompatible desires: - When picking the `value: Primitive`, we need to respect the signedness of the original *discriminant* numbers as typed in Rust. - When picking the `valid_range: WrappingRange`, we want to *ignore* the signedness (and size!) of the discriminants and look only at the bit values (and size) of the *tags* themselves. This thus splits the handling in `layout_of_enum` into those two parts, handled differently. - When looking at *discriminants* (to eventually call [`discr_range_of_repr`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/layout/trait.IntegerExt.html#tymethod.discr_range_of_repr)), we obey the signedness of the inputs to find the smallest negative discriminant and largest positive discriminant -- notably *without* any wraparound logic. - Then after having used the discriminants to pick a *tag* size, then we run a wrapping-aware check to pick the best `WrappingRange` to represent those tags (which can be a different range than we'd have picked on the discriminants, especially for `repr(Rust)` where the discriminants are `isize`). With apologies for sending this when you'd said you'd look at it, but I got inspired 😅 r? @oli-obk Fixes #159438 --- I have a bunch of commits in here; it's probably best reviewed one at a time. 1. Just moving `WrappingRange` to its own file, since the `lib.rs` is over 2000 lines already so if I was going to add stuff I figured some reorganizing could help. 2. Adding a new constructor to `WrappingRange` that takes an iterator of values and returns a range containing those values. Dealing just in `u128` + `Size`, like `WrappingRange` does in other places, it doesn't even *have* signedness information so structurally is prevented from having any "why are `repr(u8)` and `repr(i8) picking different ranges?" issues. I found it helpful to have this split out from the layout code, for example that it let me put some examples on the doc-comment. Nothing actually *uses* the new function in this commit though. 3. Stop giving layout discriminants as `i128` since <https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/util/struct.Discr.html#structfield.val> points out that they're generated as `u128` in the same way that `WrappingRange` uses already -- even with the field doc that `-1_i8` is generated as `255_u128`, so the `as i128`ing was probably just making everything more confusing here than it needed to be. 4. Adding some tests looking at the generated `Primitive` and `valid_range`, along with rustc support for `#[rustc_dump_layout(largest_niche)]` to help have these tests include specific ERROR annotations rather than just the stderr. The tests pass at this point, so the next commit can update them to show the change. 5. This one actually updates layout code, including using the `WrappingRange` constructor added earlier.
View all comments
Prompted by #159438 (comment), this reworks the generation of the tag layout information for enums.
Having gone down the wrong path at first here, I think the core trouble this is having is that the current calculation is trying to serve two incompatible desires:
value: Primitive, we need to respect the signedness of the original discriminant numbers as typed in Rust.valid_range: WrappingRange, we want to ignore the signedness (and size!) of the discriminants and look only at the bit values (and size) of the tags themselves.This thus splits the handling in
layout_of_enuminto those two parts, handled differently.discr_range_of_repr), we obey the signedness of the inputs to find the smallest negative discriminant and largest positive discriminant -- notably without any wraparound logic.WrappingRangeto represent those tags (which can be a different range than we'd have picked on the discriminants, especially forrepr(Rust)where the discriminants areisize).With apologies for sending this when you'd said you'd look at it, but I got inspired 😅
r? @oli-obk
Fixes #159438
I have a bunch of commits in here; it's probably best reviewed one at a time.
WrappingRangeto its own file, since thelib.rsis over 2000 lines already so if I was going to add stuff I figured some reorganizing could help.WrappingRangethat takes an iterator of values and returns a range containing those values. Dealing just inu128+Size, likeWrappingRangedoes in other places, it doesn't even have signedness information so structurally is prevented from having any "why arerepr(u8)and `repr(i8) picking different ranges?" issues. I found it helpful to have this split out from the layout code, for example that it let me put some examples on the doc-comment. Nothing actually uses the new function in this commit though.i128since https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/util/struct.Discr.html#structfield.val points out that they're generated asu128in the same way thatWrappingRangeuses already -- even with the field doc that-1_i8is generated as255_u128, so theas i128ing was probably just making everything more confusing here than it needed to be.Primitiveandvalid_range, along with rustc support for#[rustc_dump_layout(largest_niche)]to help have these tests include specific ERROR annotations rather than just the stderr. The tests pass at this point, so the next commit can update them to show the change.WrappingRangeconstructor added earlier.