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

Less unsafe in dangling/without_provenance #135344

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

scottmcm
Copy link
Member

This PR was inspired by the new NonNull::without_provenance (cc #135243 (comment)) since it made me realize that we could write NonNull::dangling in completely-safe code using other existing things.

Then doing that led me to a few more places that could be simplified, like now that GVN will optimize Transmute-then-PtrToPtr, we can just implement ptr::without_provenance by calling ptr::without_provenance_mut since the shipped rlib of core ends up with the same single statement as the implementation (thanks to GVN merging the steps) and thus there's no need to duplicate the transmute -- and more importantly, no need to repeat a long safety comment.

There did end up being a couple of other changes needed to avoid exploding certain bits of MIR, though -- like <Box<[i32]>>::default()'s MIR originally got way worse as certain things didn't inline, or had a bunch of extraneous UbChecks -- so there's a couple of other changes to solve that.

@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 10, 2025
bb3: {
StorageLive(_10);
_10 = const {0x1 as *mut ()};
_9 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb4, unwind unreachable];
Copy link
Member Author

Choose a reason for hiding this comment

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

annot: it was always impossible to trigger this UbCheck -- there's no user value passed to Box<[_]>::default() -- so having it here was just making rustc do more work for no reason.

(Similarly for <&[_]>::default() and friends as well.)

@joboet
Copy link
Member

joboet commented Jan 14, 2025

This looks very good, great idea!
r=me if perf is green.

@bors try @rust-timer queue

@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 Jan 14, 2025
@joboet joboet assigned joboet and unassigned ibraheemdev Jan 14, 2025
@bors
Copy link
Contributor

bors commented Jan 14, 2025

⌛ Trying commit c0d5988 with merge f97ac68...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 14, 2025
Less unsafe in `dangling`/`without_provenance`

This PR was inspired by the new `NonNull::without_provenance` (cc rust-lang#135243 (comment)) since it made me realize that we could write `NonNull::dangling` in completely-safe code using other existing things.

Then doing that led me to a few more places that could be simplified, like now that GVN will optimize Transmute-then-PtrToPtr, we can just implement `ptr::without_provenance` by calling `ptr::without_provenance_mut` since the shipped rlib of `core` ends up with the same single statement as the implementation (thanks to GVN merging the steps) and thus there's no need to duplicate the `transmute` -- and more importantly, no need to repeat a long safety comment.

There did end up being a couple of other changes needed to avoid exploding certain bits of MIR, though -- like `<Box<[i32]>>::default()`'s MIR originally got way worse as certain things didn't inline, or had a bunch of extraneous UbChecks -- so there's a couple of other changes to solve that.
@bors
Copy link
Contributor

bors commented Jan 14, 2025

☀️ Try build successful - checks-actions
Build commit: f97ac68 (f97ac682281f313dccb58a15ed0c3b90972f9d4b)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f97ac68): comparison URL.

Overall result: ❌✅ regressions and improvements - 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)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 5
Improvements ✅
(primary)
-0.5% [-0.8%, -0.3%] 2
Improvements ✅
(secondary)
-1.1% [-1.6%, -0.5%] 4
All ❌✅ (primary) 0.4% [-0.8%, 2.2%] 3

Max RSS (memory usage)

Results (primary 0.7%, secondary 2.4%)

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)
5.1% [4.3%, 6.5%] 3
Regressions ❌
(secondary)
2.4% [2.4%, 2.5%] 3
Improvements ✅
(primary)
-3.7% [-4.5%, -2.8%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.7% [-4.5%, 6.5%] 6

Cycles

Results (primary 1.6%, 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.6% [1.6%, 1.6%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-2.6%, -2.6%] 1
All ❌✅ (primary) 1.6% [1.6%, 1.6%] 1

Binary size

Results (primary 0.0%, secondary -1.1%)

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.1% [0.0%, 0.5%] 19
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 31
Improvements ✅
(secondary)
-1.1% [-2.4%, -0.0%] 19
All ❌✅ (primary) 0.0% [-0.2%, 0.5%] 50

Bootstrap: 763.089s -> 764.045s (0.13%)
Artifact size: 326.08 MiB -> 326.03 MiB (-0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 14, 2025
@scottmcm
Copy link
Member Author

The perf change here looks like inlining churn to me, more than any substantial move one way or the other. Some losses, some wins on icounts, but not a ton of them. Both wins and losses for binary size in both debug and opt.

The one primary regression has a very different codegen schedule:
image
Which makes it look more to me like it got unlucky on codegen unit splitting more than this change being a fundamental pessimization. Interestingly, while its opt-full is +2.21% on icounts, its opt-incr-full is only +0.07%.

@bors r=joboet

@bors
Copy link
Contributor

bors commented Jan 15, 2025

📌 Commit c0d5988 has been approved by joboet

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 Jan 15, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 15, 2025
Less unsafe in `dangling`/`without_provenance`

This PR was inspired by the new `NonNull::without_provenance` (cc rust-lang#135243 (comment)) since it made me realize that we could write `NonNull::dangling` in completely-safe code using other existing things.

Then doing that led me to a few more places that could be simplified, like now that GVN will optimize Transmute-then-PtrToPtr, we can just implement `ptr::without_provenance` by calling `ptr::without_provenance_mut` since the shipped rlib of `core` ends up with the same single statement as the implementation (thanks to GVN merging the steps) and thus there's no need to duplicate the `transmute` -- and more importantly, no need to repeat a long safety comment.

There did end up being a couple of other changes needed to avoid exploding certain bits of MIR, though -- like `<Box<[i32]>>::default()`'s MIR originally got way worse as certain things didn't inline, or had a bunch of extraneous UbChecks -- so there's a couple of other changes to solve that.
@bors
Copy link
Contributor

bors commented Jan 15, 2025

⌛ Testing commit c0d5988 with merge cfa4bd0...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jan 15, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 15, 2025
@scottmcm
Copy link
Member Author

Doh, conflicted with myself. Rebased atop #135236 and fixed the MIR.

@bors r=joboet

@bors
Copy link
Contributor

bors commented Jan 15, 2025

📌 Commit 27be78c has been approved by joboet

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 Jan 15, 2025
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

Oops, CI failed
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 15, 2025
@scottmcm scottmcm closed this Jan 15, 2025
@scottmcm scottmcm reopened this Jan 15, 2025
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

Didn't change anything in the rebase but CI's passing now 🤷

@bors r=joboet

@bors
Copy link
Contributor

bors commented Jan 16, 2025

📌 Commit c18718c has been approved by joboet

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 Jan 16, 2025
@bors
Copy link
Contributor

bors commented Jan 16, 2025

⌛ Testing commit c18718c with merge d8a64098c9d0fb25699f657c6efff0bb418f7e18...

@bors
Copy link
Contributor

bors commented Jan 16, 2025

☀️ Test successful - checks-actions
Approved by: joboet
Pushing d8a6409 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 16, 2025
@bors bors merged commit d8a6409 into rust-lang:master Jan 16, 2025
7 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 16, 2025
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d8a6409): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

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

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)
1.2% [0.4%, 2.1%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-0.9%, -0.3%] 3
Improvements ✅
(secondary)
-1.1% [-1.6%, -0.6%] 4
All ❌✅ (primary) 0.2% [-0.9%, 2.1%] 5

Max RSS (memory usage)

Results (primary 0.3%, secondary 1.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)
3.3% [1.3%, 4.8%] 5
Regressions ❌
(secondary)
2.4% [2.1%, 2.7%] 2
Improvements ✅
(primary)
-4.7% [-7.2%, -2.7%] 3
Improvements ✅
(secondary)
-1.1% [-1.1%, -1.1%] 1
All ❌✅ (primary) 0.3% [-7.2%, 4.8%] 8

Cycles

Results (primary -0.0%, secondary -2.9%)

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.5% [1.5%, 1.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.8% [-0.8%, -0.8%] 2
Improvements ✅
(secondary)
-2.9% [-3.4%, -2.1%] 3
All ❌✅ (primary) -0.0% [-0.8%, 1.5%] 3

Binary size

Results (primary -0.1%, secondary -1.1%)

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.1% [0.0%, 0.1%] 15
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.5%, -0.0%] 33
Improvements ✅
(secondary)
-1.1% [-2.4%, -0.0%] 19
All ❌✅ (primary) -0.1% [-0.5%, 0.1%] 48

Bootstrap: 764.032s -> 762.749s (-0.17%)
Artifact size: 326.06 MiB -> 326.07 MiB (0.00%)

}
}
scope 17 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
scope 18 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
Copy link
Member Author

Choose a reason for hiding this comment

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

annot: note that there's no changes to the actual MIR blocks (statements/terminators) in any of these pre-codegen tests, just changes to which things ended up inlined along the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. 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. T-libs Relevant to the library 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