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

safely transmute<&List<Ty<'tcx>>, &List<GenericArg<'tcx>>> #93505

Merged
merged 7 commits into from
Feb 21, 2022

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Jan 31, 2022

This PR has 3 relevant steps which are is split in distinct commits.

The first commit now interns List<Ty<'tcx>> and List<GenericArg<'tcx>> together, potentially reusing memory while allowing free conversions between these two using List<Ty<'tcx>>::as_substs() and SubstsRef<'tcx>::try_as_type_list().

Using this, we then use &'tcx List<Ty<'tcx>> instead of a SubstsRef<'tcx> for tuple fields, simplifying a bunch of code.

Finally, as tuple fields and other generic arguments now use a different TypeFoldable<'tcx> impl, we optimize the impl for List<Ty<'tcx>> improving perf by slightly less than 1% in tuple heavy benchmarks.

@rust-highfive
Copy link
Collaborator

Some changes occurred in src/tools/clippy.

cc @rust-lang/clippy

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 31, 2022
@rust-highfive
Copy link
Collaborator

r? @michaelwoerister

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 31, 2022
@lcnr
Copy link
Contributor Author

lcnr commented Jan 31, 2022

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

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

bors commented Jan 31, 2022

⌛ Trying commit 443bbb12b2f09a19a08527640a9988d13e56203d with merge c462201b7d359b74f466604390e92b54810fe960...

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jan 31, 2022

💔 Test failed - checks-actions

@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-review Status: Awaiting review from the assignee but also interested parties. labels Jan 31, 2022
@rustbot rustbot added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Jan 31, 2022
@lcnr
Copy link
Contributor Author

lcnr commented Jan 31, 2022

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@bors
Copy link
Contributor

bors commented Jan 31, 2022

⌛ Trying commit e4d2a5f747ca4fa5ec9a1ec042e1ae4d006da63d with merge a70d158d75abc4ec966880549ca103eb120f8cb8...

@bors
Copy link
Contributor

bors commented Jan 31, 2022

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

@rust-timer
Copy link
Collaborator

Queued a70d158d75abc4ec966880549ca103eb120f8cb8 with parent 415c9f9, future comparison URL.

// Can't use the macros as we have reuse the `substs` here.
impl<'a, 'tcx> Lift<'tcx> for &'a List<Ty<'a>> {
type Lifted = &'tcx List<Ty<'tcx>>;
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the transmute inside it, in instances such as this one, I personally like to spell out the aliases explicitly:

Suggested change
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
fn lift_to_tcx(self: &'a List<Ty<'a>>, tcx: TyCtxt<'tcx>) -> Option<&'tcx List<Ty<'tcx>>> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i generally agree, though, considering that all other Lift impls also don't do that i am going to keep the code as is for now.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a70d158d75abc4ec966880549ca103eb120f8cb8): comparison url.

Summary: This benchmark run shows 18 relevant improvements 🎉 but 13 relevant regressions 😿 to instruction counts.

  • Average relevant regression: 1.4%
  • Average relevant improvement: -0.9%
  • Largest improvement in instruction counts: -1.3% on full builds of ucd check
  • Largest regression in instruction counts: 2.4% on full builds of deeply-nested-async check

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

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 led 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

@rustbot rustbot added perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 31, 2022
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors
Copy link
Contributor

bors commented Feb 21, 2022

💔 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 Feb 21, 2022
@lcnr
Copy link
Contributor Author

lcnr commented Feb 21, 2022

seems spurious again? 🤔

@bors retry

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

bors commented Feb 21, 2022

⌛ Testing commit 80f56cd with merge 03a8cc7...

@bors
Copy link
Contributor

bors commented Feb 21, 2022

☀️ Test successful - checks-actions
Approved by: michaelwoerister
Pushing 03a8cc7 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 21, 2022
@bors bors merged commit 03a8cc7 into rust-lang:master Feb 21, 2022
@rustbot rustbot added this to the 1.61.0 milestone Feb 21, 2022
@lcnr lcnr deleted the substsref-vs-ty-list branch February 21, 2022 19:42
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (03a8cc7): comparison url.

Summary: This benchmark run shows 17 relevant improvements 🎉 but 48 relevant regressions 😿 to instruction counts.

  • Average relevant regression: 1.2%
  • Average relevant improvement: -0.6%
  • Largest improvement in instruction counts: -0.8% on incr-full builds of ucd check
  • Largest regression in instruction counts: 2.7% on full builds of deeply-nested-async check

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

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

@lcnr
Copy link
Contributor Author

lcnr commented Feb 22, 2022

that perf impact is worse then the previous run, going to look into that a bit xx

@@ -208,6 +233,17 @@ pub type InternalSubsts<'tcx> = List<GenericArg<'tcx>>;
pub type SubstsRef<'tcx> = &'tcx InternalSubsts<'tcx>;

impl<'a, 'tcx> InternalSubsts<'tcx> {
/// Checks whether all elements of this list are types, if so, transmute.
pub fn try_as_type_list(&'tcx self) -> Option<&'tcx List<Ty<'tcx>>> {
if self.iter().all(|arg| matches!(arg.unpack(), GenericArgKind::Type(_))) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe checking the pointer tag directly would make it possible for LLVM to optimize this? Something like self.iter().all(|arg| arg.ptr.get() & TAG_MASK == TYPE_TAG)?

Adding #[inline] to this method is a good idea in any case.

@lcnr lcnr mentioned this pull request Mar 14, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 15, 2022
…ster

inline `tuple_fields`

more rust-lang#93505 fun, after this i have no idea what might be causing the perf impact.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 10, 2022
update `hash_stable` for `List<Ty<'tcx>>`

cc rust-lang#93505 (comment)

this is the hottest part changed since the pre-merge perf run
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 19, 2023
…errors

Don't transmute `&List<GenericArg>` <-> `&List<Ty>`

In rust-lang#93505 we allowed safely transmuting between `&List<GenericArg<'_>>` and `&List<Ty<'_>>`. This was possible because `GenericArg` is a tagged pointer and the tag for types is `0b00`, such that a `GenericArg` with a type inside has the same layout as `Ty`.

While this was meant as an optimization, it doesn't look like it was actually any perf or max-rss win (see rust-lang#94799 (comment), rust-lang#94841, rust-lang#110496 (comment)).

Additionally the way it was done is quite fragile — `unsafe` code was not properly documented or contained in a module, types were not marked as `repr(C)` (making the transmutes possibly unsound). All of this makes the code maintenance harder and blocks other possible optimizations (as an example I've found out about these `transmutes` when my change caused them to sigsegv compiler).

Thus, I think we can safely (pun intended) remove those transmutes, making maintenance easier, optimizations possible, code less cursed, etc.

r? `@compiler-errors`
flip1995 pushed a commit to flip1995/rust that referenced this pull request May 5, 2023
…errors

Don't transmute `&List<GenericArg>` <-> `&List<Ty>`

In rust-lang#93505 we allowed safely transmuting between `&List<GenericArg<'_>>` and `&List<Ty<'_>>`. This was possible because `GenericArg` is a tagged pointer and the tag for types is `0b00`, such that a `GenericArg` with a type inside has the same layout as `Ty`.

While this was meant as an optimization, it doesn't look like it was actually any perf or max-rss win (see rust-lang#94799 (comment), rust-lang#94841, rust-lang#110496 (comment)).

Additionally the way it was done is quite fragile — `unsafe` code was not properly documented or contained in a module, types were not marked as `repr(C)` (making the transmutes possibly unsound). All of this makes the code maintenance harder and blocks other possible optimizations (as an example I've found out about these `transmutes` when my change caused them to sigsegv compiler).

Thus, I think we can safely (pun intended) remove those transmutes, making maintenance easier, optimizations possible, code less cursed, etc.

r? `@compiler-errors`
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-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants