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

Hash ty::Const using its pointer #90951

Closed
wants to merge 5 commits into from

Conversation

fee1-dead
Copy link
Member

ty::Const is always interned, so using its pointer for hashing and equality always works.

By setting fields private and using getter functions instead of accessing the fields directly, we guarantee that a ty::Const is never constructed without being interned.

@rust-highfive
Copy link
Collaborator

Some changes occured to the CTFE / Miri engine

cc @rust-lang/miri

@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 Nov 16, 2021
@fee1-dead
Copy link
Member Author

@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 Nov 16, 2021
@bors
Copy link
Contributor

bors commented Nov 16, 2021

⌛ Trying commit c08ad47d35c6ef722fdf6479ad67052ad4f9ab1f with merge 43984a53f55c0c3d3071d63a0de7d5ea04454ef1...

@fee1-dead
Copy link
Member Author

.. do try builds need cg_clif? let's find out..

@bors
Copy link
Contributor

bors commented Nov 16, 2021

💔 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 Nov 16, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@fee1-dead

This comment has been minimized.

@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 Nov 16, 2021
@rust-log-analyzer

This comment has been minimized.

@fee1-dead

This comment has been minimized.

@fee1-dead fee1-dead force-pushed the iref branch 2 times, most recently from f8d46ef to 6194cc8 Compare November 16, 2021 17:08
@matthiaskrgr
Copy link
Member

matthiaskrgr commented Nov 16, 2021

@fee1-dead @bors retry is for retrying to merge already approved PRS that failed in ci. It does NOT "retry" a @bors try build. try and retry are for different things.
@bors r-

@matthiaskrgr
Copy link
Member

If you want to retry a perf run, just
@bors try @rust-timer queue
again

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

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

@bors
Copy link
Contributor

bors commented Nov 16, 2021

⌛ Trying commit 6194cc8705cd39e0cd3b79a2795ca8dc1fa19ae3 with merge 2d9c1f2f85f568c21d4eb21c7f5bf5d2e9168ef7...

@rust-log-analyzer

This comment has been minimized.

@fee1-dead
Copy link
Member Author

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

bors commented Nov 18, 2021

⌛ Trying commit b824865 with merge a99a0ac3f4ccc1aaa8d59194fc97e793be2cc227...

@bors
Copy link
Contributor

bors commented Nov 18, 2021

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

@rust-timer
Copy link
Collaborator

Queued a99a0ac3f4ccc1aaa8d59194fc97e793be2cc227 with parent 6414e0b, future comparison URL.

@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 18, 2021
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a99a0ac3f4ccc1aaa8d59194fc97e793be2cc227): comparison url.

Summary: This change led to moderate relevant mixed results 🤷 in compiler performance.

  • Small improvement in instruction counts (up to -0.7% on incr-full builds of ctfe-stress-4)
  • Moderate regression in instruction counts (up to 0.7% on full builds of helloworld)

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 removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 18, 2021
@fee1-dead
Copy link
Member Author

See Zulip (archive) about the perf regression

@michaelwoerister
Copy link
Member

Thanks for the PR, @fee1-dead!

This is a rather large change set and the performance impact seems to be quite ambiguous. Would you say that it also improves code quality? Looking at performance alone I'm don't think it's worth the trouble.

@fee1-dead
Copy link
Member Author

fee1-dead commented Nov 30, 2021

I would argue that the performance impact isn't ambiguous, since the regression is related to dividing codegen units and opaque stuff that isn't directly caused by this PR.

Hashing/Comparing interned pointers is almost always better than looking at the data. This should ideally be done to every interned type.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 30, 2021

Maybe run just the part that makes the fields private through perf so we get it independently of the hashing and equality changes?

@michaelwoerister
Copy link
Member

Hashing/Comparing interned pointers is almost always better than looking at the data. This should ideally be done to every interned type.

In general that makes sense to me. I'll take a closer look tomorrow.

Copy link
Member

@michaelwoerister michaelwoerister left a comment

Choose a reason for hiding this comment

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

OK, I took a closer look. Thanks again for the PR, @fee1-dead!

It looks correct to me except for the missing equality impls for &'tcx Const<'tcx>. Can you add those?

In general (and this is already the case before this PR) I think it is rather fishy that we don't have a proper smart pointer type for interned things and instead rely on &'tcx Foo<'tcx> to probably do the right thing and people adding custom impls around that.


impl<'tcx> Eq for Interned<'tcx, Const<'tcx>> {}

#[repr(transparent)]
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment that #[repr(transparent)] is needed because of the Borrow impl below.

let c: &'tcx Const<'tcx> = *self;
(c as *const Const<'tcx>).hash(state)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't there be impl PartialEq for &'tcx Const<'tcx> and impl Eq for &'tcx Const<'tcx> that follow the same pattern too? Otherwise the HashMap will do a deep equality check during insertion, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

AIUI this isn't necessary because library/core/src/cmp.rs provides impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A...


#[repr(transparent)]
#[derive(Clone, Copy)]
struct CstHash<'tcx>(Const<'tcx>);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe CstHash could be renamed to something more expressive, like ConstInterningKey or something.


pub val: ConstKind<'tcx>,
pub(super) val: ConstKind<'tcx>,
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment that the fields have restricted visibility because we want to prevent Const from being constructed in a non-interned way.

@michaelwoerister michaelwoerister 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 6, 2021
@tmiasko
Copy link
Contributor

tmiasko commented Dec 6, 2021

By setting fields private and using getter functions instead of accessing the fields directly, we guarantee that a ty::Const is never constructed without being interned.

ty::Const implements Copy and Clone.

@michaelwoerister
Copy link
Member

ty::Const implements Copy and Clone.

Indeed! That's a potential correctness problem. Can these impls be removed?

@JohnCSimon
Copy link
Member

Ping from triage:
@fee1-dead can you post your status on this PR?

@fee1-dead
Copy link
Member Author

#93148 is a better approach; Thus this is obsolete

@fee1-dead fee1-dead closed this Jan 23, 2022
@fee1-dead fee1-dead deleted the iref branch January 28, 2022 10:58
nnethercote added a commit to nnethercote/rust that referenced this pull request Feb 21, 2022
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.