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

compiler: give ExternAbi truly stable Hash and Ord #136901

Merged

Conversation

workingjubilee
Copy link
Member

Currently, ExternAbi has a bunch of code to handle the reality that, as an enum, adding more variants to it will risk it hashing differently. It forces all of those variants to be added in a fixed order, except this means that the order of the variants doesn't correspond to any logical order except "historical accident". This is all to avoid having to rebless two tests. Perhaps there were more, once upon a time? But then we invented normalization in our test suite to handle exactly this sort of issue in a more general way.

There are two options here:

  • Get rid of all the logical overhead and shrug, embracing blessing a couple of tests sometimes
  • Change ExternAbi to have an ordering and hash that doesn't depend on the number of variants

As ExternAbi is essentially a strongly-typed string, and thus no two strings can be identical, this implements the second of the two by hand-implementing Ord and Hash to make the hashing and comparison based on the string! This will diff the current hashes, but they will diff no more after this.

@rustbot
Copy link
Collaborator

rustbot commented Feb 12, 2025

r? @cjgillot

rustbot has assigned @cjgillot.
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 A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 12, 2025

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@workingjubilee
Copy link
Member Author

As one might imagine, the first two commits are the cleanup I was doing before I noticed we could do this.

@rust-log-analyzer

This comment has been minimized.

@workingjubilee workingjubilee force-pushed the stabilize-externabi-hashing-forever branch from e7103b9 to 2150c83 Compare February 12, 2025 04:16
Directly map each ExternAbi variant to its string and back again.
This has a few advantages:
- By making the ABIs compare equal to their strings, we can easily
  lexicographically sort them and use that sorted slice at runtime.
- We no longer need a workaround to make sure the hashes remain stable,
  as they already naturally are (by being the hashes of unique strings).
- The compiler can carry around less &str wide pointers
These were a way to ensure hashes were stable over time for ExternAbi,
but simply hashing the strings is more stable in the face of changes.
As a result, we can do away with them.
These can be entirely replaced by the FromStr implementation.
@workingjubilee
Copy link
Member Author

Also I'm fine with just yeeting the complexity, the thing is just that I only really noticed it was just two tests after I fixed up everything.

@rust-log-analyzer

This comment has been minimized.

@workingjubilee workingjubilee force-pushed the stabilize-externabi-hashing-forever branch from 2150c83 to f8570e8 Compare February 12, 2025 04:23
@workingjubilee workingjubilee removed the A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) label Feb 12, 2025
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

LGTM

@compiler-errors
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 12, 2025

📌 Commit f8570e8 has been approved by compiler-errors

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 Feb 12, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 12, 2025
…llaumeGomez

Rollup of 10 pull requests

Successful merges:

 - rust-lang#136758 (tests: `-Copt-level=3` instead of `-O` in assembly tests)
 - rust-lang#136761 (tests: `-Copt-level=3` instead of `-O` in codegen tests)
 - rust-lang#136784 (Nuke `Buffer` abstraction from `librustdoc`, take 2 💣)
 - rust-lang#136838 (Check whole `Unsize` predicate for escaping bound vars)
 - rust-lang#136848 (add docs and ut for bootstrap util cache)
 - rust-lang#136871 (dev-guide: Link to `t-lang` procedures for new features)
 - rust-lang#136890 (Change swap_nonoverlapping from lang to library UB)
 - rust-lang#136901 (compiler: give `ExternAbi` truly stable `Hash` and `Ord`)
 - rust-lang#136907 (compiler: Make middle errors `pub(crate)` and bury the dead code)
 - rust-lang#136916 (use cc archiver as default in `cc2ar`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 27dc222 into rust-lang:master Feb 12, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Feb 12, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 12, 2025
Rollup merge of rust-lang#136901 - workingjubilee:stabilize-externabi-hashing-forever, r=compiler-errors

compiler: give `ExternAbi` truly stable `Hash` and `Ord`

Currently, `ExternAbi` has a bunch of code to handle the reality that, as an enum, adding more variants to it will risk it hashing differently. It forces all of those variants to be added in a fixed order, except this means that the order of the variants doesn't correspond to any logical order except "historical accident". This is all to avoid having to rebless two tests. Perhaps there were more, once upon a time? But then we invented normalization in our test suite to handle exactly this sort of issue in a more general way.

There are two options here:
- Get rid of all the logical overhead and shrug, embracing blessing a couple of tests sometimes
- Change `ExternAbi` to have an ordering and hash that doesn't depend on the number of variants

As `ExternAbi` is essentially a strongly-typed string, and thus no two strings can be identical, this implements the second of the two by hand-implementing `Ord` and `Hash` to make the hashing and comparison based on the string! This will diff the current hashes, but they will diff no more after this.
@workingjubilee workingjubilee deleted the stabilize-externabi-hashing-forever branch February 13, 2025 06:04
@Kobzol
Copy link
Contributor

Kobzol commented Feb 18, 2025

@rust-timer build fe90d7e

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (fe90d7e): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.4%, 0.7%] 16
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 4.0%, secondary 5.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
4.0% [2.2%, 5.8%] 2
Regressions ❌
(secondary)
5.2% [4.0%, 6.4%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.0% [2.2%, 5.8%] 2

Cycles

Results (primary 1.2%, secondary 2.6%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.2% [1.1%, 1.2%] 3
Regressions ❌
(secondary)
2.6% [2.3%, 3.4%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.2% [1.1%, 1.2%] 3

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 788.874s -> 788.126s (-0.09%)
Artifact size: 347.76 MiB -> 347.86 MiB (0.03%)

@rustbot rustbot added the perf-regression Performance regression. label Feb 18, 2025
@Kobzol
Copy link
Contributor

Kobzol commented Feb 19, 2025

FYI, this PR caused a tiny performance regression on a bunch of secondary benchmarks in #136943. But I don't think that it warrants further action.

@Kobzol
Copy link
Contributor

Kobzol commented Feb 19, 2025

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants