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

[rustc_ty_utils] Add the LLVM noalias parameter attribute to drop_in_place in certain cases. #103614

Closed
wants to merge 5 commits into from

Conversation

pcwalton
Copy link
Contributor

LLVM can make use of the noalias parameter attribute on the parameter to drop_in_place in areas like argument promotion. Because the Rust compiler fully controls the code for drop_in_place, it can soundly deduce parameter attributes on it. In the case of a value that has a programmer-defined Drop implementation, we know that the first thing drop_in_place will do is pass a pointer to the object to Drop::drop. Drop::drop takes &mut, so it must be guaranteed that there are no pointers to the object upon entering that function. Therefore, it should be safe to mark noalias there.

With this patch, we mark noalias only when the type is a value with a programmer-defined Drop implementation. This is possibly overly conservative, but I thought that proceeding cautiously was best in this instance.

@rustbot
Copy link
Collaborator

rustbot commented Oct 27, 2022

r? @jackh726

(rustbot has picked a reviewer for you, use r? to override)

@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. labels Oct 27, 2022
@rust-log-analyzer

This comment has been minimized.

@pcwalton
Copy link
Contributor Author

r? @oli-obk

(Feel free to reassign review to someone else) :)

@rustbot rustbot assigned oli-obk and unassigned jackh726 Oct 27, 2022
@oli-obk
Copy link
Contributor

oli-obk commented Oct 31, 2022

Please also add comments on drop_in_place in libstd to make sure anyone touching it is aware they should also touch this logic

@pcwalton pcwalton marked this pull request as draft November 3, 2022 09:22
@pcwalton
Copy link
Contributor Author

pcwalton commented Nov 3, 2022

Per discussions on Zulip, I'm going to change this to a draft while we wait on Miri results to figure out what the impact of this change will be.

…in_place` in certain cases.

LLVM can make use of the `noalias` parameter attribute on the parameter to
`drop_in_place` in areas like argument promotion. Because the Rust compiler
fully controls the code for `drop_in_place`, it can soundly deduce parameter
attributes on it. In the case of a value that has a programmer-defined Drop
implementation, we know that the first thing `drop_in_place` will do is pass a
pointer to the object to `Drop::drop`. `Drop::drop` takes `&mut`, so it must be
guaranteed that there are no pointers to the object upon entering that
function. Therefore, it should be safe to mark `noalias` there.

With this patch, we mark `noalias` only when the type is a value with a
programmer-defined Drop implementation. This is possibly overly conservative,
but I thought that proceeding cautiously was best in this instance.
… unconditionally.

We've done measurements with Miri and have determined that `noalias` shouldn't
break code. The requirements that allow us to add dereferenceable and align
have been long documented in the standard library documentation.
@pcwalton pcwalton marked this pull request as ready for review November 18, 2022 00:14
@rustbot
Copy link
Collaborator

rustbot commented Nov 18, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@pcwalton
Copy link
Contributor Author

r? @oli-obk

cc @RalfJung @saethlin

@rustbot
Copy link
Collaborator

rustbot commented Nov 18, 2022

Could not assign reviewer from: oli-obk.
User(s) oli-obk are either the PR author or are already assigned, and there are no other candidates.
Use r? to specify someone else to assign.

///
/// * The value `to_drop` points to must be valid for dropping, which may mean it must uphold
/// additional invariants - this is type-dependent.
/// * While `drop_in_place` is executing, the only way to access parts of
Copy link
Member

Choose a reason for hiding this comment

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

@RalfJung Would it be better to say

Suggested change
/// * While `drop_in_place` is executing, the only way to access parts of
/// * As soon as `drop_in_place` begins executing, the only way to access parts of

Copy link
Member

Choose a reason for hiding this comment

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

What is the difference you are getting at here?

One drop_in_place returns, old references can be used again, at least as far as the aliasing model goes.

@rust-log-analyzer

This comment has been minimized.

@pcwalton
Copy link
Contributor Author

This patch reduces the number of stack-to-stack copies from 2.50% to 2.36%, a 6% decrease.

@RalfJung
Copy link
Member

RalfJung commented Nov 18, 2022 via email

@thomcc
Copy link
Member

thomcc commented Nov 18, 2022

Just out of idle curiosity1...

@bors try @rust-timer queue

Footnotes

  1. I don't expect tons but who knows how many of those stack/stack copies are in important places 👀

@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 Nov 18, 2022
@bors
Copy link
Contributor

bors commented Nov 18, 2022

⌛ Trying commit 53f21aa with merge 7250e7fb50ffb04be8243ed3f0d9baa1caa63d4a...

@pcwalton
Copy link
Contributor Author

The Miri version of this is #103957. I'd prefer if we could land that first.

As long as it doesn't take too long. That PR has been sitting idle for 5 days. I don't want this to bitrot.

@RalfJung
Copy link
Member

Bitrot risk for this PR seems low and 5 days is not very long. I don't think this is an urgent change either.

Comment on lines +439 to +448
/// Immediately upon executing, `drop_in_place` takes out a mutable borrow on the
/// pointed-to-value. Effectively, this function is implemented like so:
///
/// ```
/// # struct Foo { x: i32 }
/// unsafe fn drop_in_place(to_drop: *mut Foo) {
/// let mut value = &mut *to_drop;
/// // ... drop the fields of `value` ...
/// }
/// ```
Copy link
Member

@RalfJung RalfJung Nov 18, 2022

Choose a reason for hiding this comment

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

What we actually do is stronger than that: we have an &mut function argument. That makes a difference because for noalias, scope matters.

So if you want to write this in code, I'd suggest something like

/// ```
/// # struct Foo { x: i32 }
/// unsafe fn drop_in_place(to_drop: *mut Foo) {
///     drop_in_place_inner(&mut *to_drop);
///     unsafe fn drop_in_place_inner(to_drop: &mut Foo) {
///         // ... drop the fields of `value` ...
///     }
/// }
/// ```

Comment on lines +455 to +457
/// * `to_drop` must be properly aligned, even if T has size 0.
///
/// * `to_drop` must be nonnull, even if T has size 0.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// * `to_drop` must be properly aligned, even if T has size 0.
///
/// * `to_drop` must be nonnull, even if T has size 0.
/// * `to_drop` must be properly aligned, even if `T` has size 0.
///
/// * `to_drop` must be nonnull, even if `T` has size 0.

@bors
Copy link
Contributor

bors commented Nov 18, 2022

☀️ Try build successful - checks-actions
Build commit: 7250e7fb50ffb04be8243ed3f0d9baa1caa63d4a (7250e7fb50ffb04be8243ed3f0d9baa1caa63d4a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7250e7fb50ffb04be8243ed3f0d9baa1caa63d4a): comparison URL.

Overall result: ❌✅ regressions and improvements - 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-review -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.5% [0.4%, 0.7%] 7
Regressions ❌
(secondary)
3.2% [0.5%, 9.5%] 7
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [-0.6%, 0.7%] 8

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

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)
0.9% [0.7%, 1.1%] 7
Regressions ❌
(secondary)
4.3% [1.2%, 9.2%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.9% [0.7%, 1.1%] 7

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 18, 2022
@RalfJung
Copy link
Member

RalfJung commented Nov 18, 2022 via email

@thomcc
Copy link
Member

thomcc commented Nov 18, 2022

Yeah, the classic problem of measuring performance changes by measuring the compiler itself.

@pcwalton
Copy link
Contributor Author

If folks are concerned about the performance regression, I can disable this in incremental compilation mode. It doesn't sit that well with me though—it's essentially just improving compilation performance by artificially making LLVM able to optimize less.

@pcwalton
Copy link
Contributor Author

Oh, also keep in mind that I'm testing against LLVM 16, while the benchmark presumably isn't.

@thomcc
Copy link
Member

thomcc commented Nov 18, 2022

If folks are concerned about the performance regression, I can disable this in incremental compilation mode

I can't speak to anybody else, but I'm not concerned. Or, my concern is just that the value of this kind of thing can't really be measured using our existing perf suite, but that's an existing issue that shouldn't impact this PR (IMO).

Comment on lines +347 to +350
let is_drop_in_place = match (cx.tcx.lang_items().drop_in_place_fn(), fn_def_id) {
(Some(drop_in_place_fn), Some(fn_def_id)) => drop_in_place_fn == fn_def_id,
_ => false,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let is_drop_in_place = match (cx.tcx.lang_items().drop_in_place_fn(), fn_def_id) {
(Some(drop_in_place_fn), Some(fn_def_id)) => drop_in_place_fn == fn_def_id,
_ => false,
};
let is_drop_in_place = cx.tcx.lang_items().drop_in_place_fn() == Some(fn_def_id);

@oli-obk
Copy link
Contributor

oli-obk commented Dec 13, 2022

@rustbot author

perf regression is entirely in LLVM, so since this causes actual runtime improvements, and the regressions are small in primary tests, this lgtm

@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Dec 13, 2022
@RalfJung
Copy link
Member

#103957 finally landed, so from a Miri / MIR semantics perspective this is good to go now.

There are a bunch of review comments above though.

@JohnCSimon
Copy link
Member

#103957 finally landed, so from a Miri / MIR semantics perspective this is good to go now.

There are a bunch of review comments above though.

@pcwalton Can you please post your status on this PR? It has sat idle for months.

bors added a commit to rust-lang-ci/rust that referenced this pull request May 23, 2023
[rustc_ty_utils] Treat `drop_in_place`'s *mut argument like &mut when adding LLVM attributes

This resurrects PR rust-lang#103614, which has sat idle for a while.

This could probably use a new perf run, since we're on a new LLVM version now.

r? `@oli-obk`
cc `@RalfJung`

---

LLVM can make use of the `noalias` parameter attribute on the parameter to `drop_in_place` in areas like argument promotion. Because the Rust compiler fully controls the code for `drop_in_place`, it can soundly deduce parameter attributes on it.

In rust-lang#103957, Miri was changed to retag `drop_in_place`'s argument as if it was `&mut`, matching this change.
@pnkfelix
Copy link
Member

This ended up landing via PR #111807 if I understand correctly.

@pnkfelix pnkfelix closed this May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.