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

Prototype: Add unstable -Z reference-niches option #113166

Merged
merged 13 commits into from
Jul 21, 2023

Conversation

moulins
Copy link
Contributor

@moulins moulins commented Jun 29, 2023

MCP: rust-lang/compiler-team#641
Relevant RFC: rust-lang/rfcs#3204

This prototype adds a new -Z reference-niches option, controlling the range of valid bit-patterns for reference types (&T and &mut T), thereby enabling new enum niching opportunities. Like -Z randomize-layout, this setting is crate-local; as such, references to built-in types (primitives, tuples, ...) are not affected.

The possible settings are (here, MAX denotes the all-1 bit-pattern):

-Z reference-niches= Valid range
null (the default) 1..=MAX
size 1..=(MAX- size)
align align..=MAX.align_down_to(align)
size,align align..=(MAX-size).align_down_to(align)

This is very WIP, and I'm not sure the approach I've taken here is the best one, but stage 1 tests pass locally; I believe this is in a good enough state to unleash this upon unsuspecting 3rd-party code, and see what breaks.

@rustbot
Copy link
Collaborator

rustbot commented Jun 29, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jackh726 (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jun 29, 2023
@moulins moulins marked this pull request as ready for review June 29, 2023 17:50
@rustbot
Copy link
Collaborator

rustbot commented Jun 29, 2023

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 29, 2023

r? @oli-obk

@moulins
Copy link
Contributor Author

moulins commented Jun 29, 2023

This should fix the stage 2 rustc build.

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Jun 30, 2023

This prototype adds a new -Z reference-niches option, controlling the range of valid bit-patterns for reference types (&T and &mut T), thereby enabling new enum niching opportunities. Like -Z randomize-layout, this setting is crate-local; as such, references to built-in types (primitives, tuples, ...) are not affected.

I am confused by this comment. Does this mean that it is the crate where T was defined that decides whether &T gets the layout optimization? That's not how the docs sound like.

Also does this mean &i32 never gets the new niches?

compiler/rustc_const_eval/src/interpret/discriminant.rs Outdated Show resolved Hide resolved
compiler/rustc_const_eval/src/interpret/discriminant.rs Outdated Show resolved Hide resolved
compiler/rustc_const_eval/src/interpret/memory.rs Outdated Show resolved Hide resolved
compiler/rustc_const_eval/src/interpret/memory.rs Outdated Show resolved Hide resolved
compiler/rustc_const_eval/src/interpret/memory.rs Outdated Show resolved Hide resolved
compiler/rustc_abi/src/lib.rs Outdated Show resolved Hide resolved
tests/ui/lint/invalid_value.stderr Show resolved Hide resolved
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Please rewrite your git history so that every commit that affects any tests, has its tests blessed, so we can track test changes as the commits progress.

compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_ty_utils/src/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Show resolved Hide resolved
@oli-obk
Copy link
Contributor

oli-obk commented Jun 30, 2023

I had another comment that github ate and then I forgot it. Either I'll remember in a second round of reviewing or it's not that important.

This is really cool!

@bors try @rust-timer queue (let's see that none of the changes affect perf even with the feature gate disabled)

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 30, 2023
@bors
Copy link
Contributor

bors commented Jun 30, 2023

⌛ Trying commit bffc832610817ff707c591994d02864d3225263c with merge 43dda3cbf8a740d44e87bca311d4827e17b2ab4e...

@bors
Copy link
Contributor

bors commented Jun 30, 2023

☀️ Try build successful - checks-actions
Build commit: 43dda3cbf8a740d44e87bca311d4827e17b2ab4e (43dda3cbf8a740d44e87bca311d4827e17b2ab4e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (43dda3cbf8a740d44e87bca311d4827e17b2ab4e): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.9% [0.7%, 0.9%] 5
Regressions ❌
(secondary)
1.2% [1.1%, 1.3%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.9% [0.7%, 0.9%] 5

Max RSS (memory usage)

Results

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.8% [0.4%, 4.4%] 23
Regressions ❌
(secondary)
2.4% [2.0%, 3.4%] 9
Improvements ✅
(primary)
-2.9% [-2.9%, -2.9%] 1
Improvements ✅
(secondary)
-2.2% [-4.6%, -1.0%] 3
All ❌✅ (primary) 1.6% [-2.9%, 4.4%] 24

Cycles

Results

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)
2.1% [2.1%, 2.1%] 2
Regressions ❌
(secondary)
1.8% [1.8%, 1.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 2

Binary size

Results

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)
- - 0
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Bootstrap: 659.918s -> 667.792s (1.19%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jun 30, 2023
Still more complexity, but this allows computing exact `NaiveLayout`s
for null-optimized enums, and thus allows calls like
`transmute::<Option<&T>, &U>()` to work in generic contexts.
@oli-obk
Copy link
Contributor

oli-obk commented Jul 21, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jul 21, 2023

📌 Commit 7f10908 has been approved by oli-obk

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 21, 2023
@bors
Copy link
Contributor

bors commented Jul 21, 2023

⌛ Testing commit 7f10908 with merge 557359f...

@bors
Copy link
Contributor

bors commented Jul 21, 2023

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 557359f to master...

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (557359f): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.3%, 1.1%] 19
Regressions ❌
(secondary)
1.0% [0.3%, 1.2%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.7% [0.3%, 1.1%] 19

Max RSS (memory usage)

Results

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)
2.3% [0.9%, 4.3%] 13
Regressions ❌
(secondary)
2.2% [1.8%, 2.9%] 7
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
-3.4% [-5.9%, -2.3%] 4
All ❌✅ (primary) 2.0% [-2.2%, 4.3%] 14

Cycles

Results

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)
3.0% [1.0%, 4.2%] 10
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.0% [1.0%, 4.2%] 10

Binary size

Results

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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 1

Bootstrap: 652.363s -> 658.335s (0.92%)

dtolnay added a commit to dtolnay/rust that referenced this pull request Jul 22, 2023
…r=oli-obk"

This reverts commit 557359f, reversing
changes made to 1e6c09a.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 22, 2023
Revert "Prototype: Add unstable `-Z reference-niches` option"

Clean revert of rust-lang#113166. I confirmed this fixes rust-lang#113941.
@pnkfelix
Copy link
Member

reverted by PR #113946, which restored the performance, at least for the truly significant regressive cases.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Jul 26, 2023
antoyo pushed a commit to antoyo/rust that referenced this pull request Oct 9, 2023
Prototype: Add unstable `-Z reference-niches` option

MCP: rust-lang/compiler-team#641
Relevant RFC: rust-lang/rfcs#3204

This prototype adds a new `-Z reference-niches` option, controlling the range of valid bit-patterns for reference types (`&T` and `&mut T`), thereby enabling new enum niching opportunities. Like `-Z randomize-layout`, this setting is crate-local; as such, references to built-in types (primitives, tuples, ...) are not affected.

The possible settings are (here, `MAX` denotes the all-1 bit-pattern):
| `-Z reference-niches=` | Valid range |
|:---:|:---:|
| `null` (the default) | `1..=MAX` |
| `size` | `1..=(MAX- size)` |
| `align` | `align..=MAX.align_down_to(align)` |
| `size,align` | `align..=(MAX-size).align_down_to(align)` |

------

This is very WIP, and I'm not sure the approach I've taken here is the best one, but stage 1 tests pass locally; I believe this is in a good enough state to unleash this upon unsuspecting 3rd-party code, and see what breaks.
antoyo pushed a commit to antoyo/rust that referenced this pull request Oct 9, 2023
…r=oli-obk"

This reverts commit 557359f, reversing
changes made to 1e6c09a.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 31, 2024
Add range metadata to slice lengths

This adds range information to the slice-len in fat pointers if we can conservatively determine that the pointee is not a ZST without having to normalize the pointee type.

I only intended to pass the `!range` to llvm but apparently this also lets the length in fat pointers be used for its niches 😅.

Ideally this would use the naive-layout computation from rust-lang#113166 to calculate a better approximation of the pointee size, but that PR got reverted.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 11, 2024
Add range metadata to slice lengths

This adds range information to the slice-len in fat pointers if we can conservatively determine that the pointee is not a ZST without having to normalize the pointee type.

I only intended to pass the `!range` to llvm but apparently this also lets the length in fat pointers be used for its niches 😅.

Ideally this would use the naive-layout computation from rust-lang#113166 to calculate a better approximation of the pointee size, but that PR got reverted.
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 10, 2024
Add range metadata to slice lengths

This adds range information to the slice-len in fat pointers if we can conservatively determine that the pointee is not a ZST without having to normalize the pointee type.

I only intended to pass the `!range` to llvm but apparently this also lets the length in fat pointers be used for its niches 😅.

Ideally this would use the naive-layout computation from rust-lang#113166 to calculate a better approximation of the pointee size, but that PR got reverted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) merged-by-bors This PR was explicitly merged by bors. 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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants