Skip to content

Move DepKind and query TLS to rustc_query_system#86038

Closed
cjgillot wants to merge 5 commits intorust-lang:masterfrom
cjgillot:tls
Closed

Move DepKind and query TLS to rustc_query_system#86038
cjgillot wants to merge 5 commits intorust-lang:masterfrom
cjgillot:tls

Conversation

@cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Jun 5, 2021

r? @ghost

@cjgillot
Copy link
Contributor Author

cjgillot commented Jun 5, 2021

@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 Jun 5, 2021
@bors
Copy link
Collaborator

bors commented Jun 5, 2021

⌛ Trying commit 84a156cb46350ea4ca35884c4a910b62343f0677 with merge b1ce53c0cc21a9301621cb4076f77bf3ec601d7b...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 5, 2021

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

@rust-timer
Copy link
Collaborator

Queued b1ce53c0cc21a9301621cb4076f77bf3ec601d7b with parent 9104c89, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (b1ce53c0cc21a9301621cb4076f77bf3ec601d7b): comparison url.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot added 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 Jun 6, 2021
@mati865
Copy link
Member

mati865 commented Jun 6, 2021

31% diesel-doc regression is red herring: #85993 (comment)

Copy link
Member

@bjorn3 bjorn3 Jul 1, 2021

Choose a reason for hiding this comment

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

Could these Options be removed and instead clear the ImplicitContext in the tls when they would be None?

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 don't think so. The implicitctxt is still required to detect cycles and to print query stack even when the dependencies are ignored.

Copy link
Member

Choose a reason for hiding this comment

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

And the other fields? At least one can be made non-optional, right? I don't see what the difference between no implicitctxt and an empty implicitctxt would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, query should not need to be an option.

@cjgillot cjgillot force-pushed the tls branch 3 times, most recently from a5bfc56 to a91b82e Compare July 6, 2021 18:51
@JohnCSimon JohnCSimon 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 Jul 31, 2021
@JohnCSimon
Copy link
Member

Ping from triage:
@cjgillot assigning back to author to resolve merge conflicts

@camelid
Copy link
Member

camelid commented Aug 20, 2021

triage: Is this PR experimental? If so, you can replace S-waiting-on-author with S-experimental to prevent it from being triaged.

@rust-lang rust-lang deleted a comment from bors Aug 21, 2021
@rust-lang rust-lang deleted a comment from bors Aug 21, 2021
@rust-lang rust-lang deleted a comment from bors Aug 21, 2021
@bors
Copy link
Collaborator

bors commented Feb 24, 2022

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

@rust-timer
Copy link
Collaborator

Queued 38e911f90695efec7806bf6f6c1dccc01a05c130 with parent 4b043fa, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (38e911f90695efec7806bf6f6c1dccc01a05c130): comparison url.

Summary: This benchmark run shows 98 relevant improvements 🎉 to instruction counts.

  • Arithmetic mean of relevant improvements: -0.7%
  • Largest improvement in instruction counts: -3.9% on full builds of keccak 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.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Feb 25, 2022
@Mark-Simulacrum Mark-Simulacrum removed the perf-regression-triaged The performance regression has been triaged. label Feb 25, 2022
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

Would be nice to squash the commits in, and modulo some nits this seems good to me. I think in a fully ideal world the query system would be less tightly coupled to the particular queries, but in practice we both care about performance and the abstractions actually hurt us more than help, I suspect.

Copy link
Member

Choose a reason for hiding this comment

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

nit: "current" TyCtxt seems a little misleading -- we really expect just one, right? I wonder if there's some slight alternate framing that would work well here.

FWIW, I think the thread_local nature here may be important for things like unit tests and such which want to spin up their own TyCtxt's -- I'm not sure we should drop the TLS variable. At the very least, we should make sure we don't have UB or so if there's conflicts.

Copy link
Member

Choose a reason for hiding this comment

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

There may be multiple TyCtxt in the same process if you use rustc_driver on multiple threads.

Copy link
Member

Choose a reason for hiding this comment

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

Minor nit: Maybe TLV should be an AtomicPtr? I'm not sure if that causes us lifetime troubles.

@Mark-Simulacrum Mark-Simulacrum 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 Feb 26, 2022
@cjgillot
Copy link
Contributor Author

I finally got back to this PR.

In this new version, I stop moving the macros to rustc_query_system, and side with @Zoxc's suggestion to make DepKind a newtype over u16. This may be useful if we want to define queries in crates other than rustc_middle one day.

The global pointer to GlobalCtxt is now a thread-local *const GlobalCtxt<'static>. Since all the jobs in the compilation thread pool have the same GlobalCtxt, this should not trigger any issue, while at the same time allowing to run two instances of rustc in different threads as asked by @bjorn3.

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

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

@bors
Copy link
Collaborator

bors commented Apr 16, 2022

⌛ Trying commit 4387490 with merge 0794d7f4f502d8a0e9a4a71f7669e4adda756b81...

@bors
Copy link
Collaborator

bors commented Apr 16, 2022

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

@rust-timer
Copy link
Collaborator

Queued 0794d7f4f502d8a0e9a4a71f7669e4adda756b81 with parent febce1f, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0794d7f4f502d8a0e9a4a71f7669e4adda756b81): comparison url.

Summary:

  • Primary benchmarks: mixed results
  • Secondary benchmarks: mixed results
Regressions 😿
(primary)
Regressions 😿
(secondary)
Improvements 🎉
(primary)
Improvements 🎉
(secondary)
All 😿 🎉
(primary)
count1 10 21 9 35 19
mean2 1.0% 2.6% -0.4% -0.6% 0.4%
max 3.4% 8.2% -0.7% -1.6% 3.4%

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

Footnotes

  1. number of relevant changes

  2. the arithmetic mean of the percent change

@cjgillot
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

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

@bors
Copy link
Collaborator

bors commented Apr 16, 2022

⌛ Trying commit 8b8c12e with merge 8227b19194e5b9a2926d15786b99e6cc9f73389f...

@bors
Copy link
Collaborator

bors commented Apr 16, 2022

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

@rust-timer
Copy link
Collaborator

Queued 8227b19194e5b9a2926d15786b99e6cc9f73389f with parent 878c783, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8227b19194e5b9a2926d15786b99e6cc9f73389f): comparison url.

Summary:

  • Primary benchmarks: mixed results
  • Secondary benchmarks: mixed results
Regressions 😿
(primary)
Regressions 😿
(secondary)
Improvements 🎉
(primary)
Improvements 🎉
(secondary)
All 😿 🎉
(primary)
count1 4 13 28 42 32
mean2 2.0% 3.7% -0.4% -0.5% -0.1%
max 3.2% 7.9% -0.7% -0.9% 3.2%

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

Footnotes

  1. number of relevant changes

  2. the arithmetic mean of the percent change

/// query. It is updated when
/// executing a new query.
#[derive(Clone, Default)]
struct ImplicitCtxt<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should keep the tcx field as a *const (). That lets you move more of the tls module intact and the parallel compiler doesn't regress. with_opt can convert it back in rustc_middle.

You'll want to add an assertion about GlobalCtxt being Sync too, since the type is now erased from ImplicitCtxt.

// Extend the lifetime of the GlobalCtxt so the new thread can know of it.
// The current thread will not free it, it is deadlocked.
let tcx: &'static GlobalCtxt<'static> =
tls::with(|tcx| &*(*tcx as *const GlobalCtxt<'_>).cast());
Copy link
Contributor

Choose a reason for hiding this comment

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

This is incorrect since it's only set on one of Rayon's threads. The deadlock handler can be called on any thread. The current state of things is also broken to be fair.

We can not use the Rayon "Task Local Value" either, since the deadlock handler can also be called from a thread with no rustc task is running. My solution to this was to point all threads to a lock which holds the GlobalCtxt when available. It was reverted in #74969.

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) 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.