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

[WIP] make nightly compilers able to parallelize #101566

Closed
wants to merge 19 commits into from

Conversation

SparrowLii
Copy link
Member

@SparrowLii SparrowLii commented Sep 8, 2022

Try to complete the work of #59667. Refactor data structures such as Lock to make parallel compilation available while minimizing regressions and remaining thread safety.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 8, 2022
@lqd
Copy link
Member

lqd commented Sep 8, 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 Sep 8, 2022
@bors
Copy link
Contributor

bors commented Sep 8, 2022

⌛ Trying commit 9201feb16c93bd27499fb0625495e7f6bf447bfd with merge 49d1d103e085e772150709059f0bbb75c5a37a89...

#[cfg(not(parallel_compiler))]
static mut SHARED: bool = false;
#[cfg(parallel_compiler)]
static mut SHARED: bool = true;
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the unstable SyncUnsafeCell<T> instead of static mut for both versions? This would be a great place to test it out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, will try it!

Copy link
Member

Choose a reason for hiding this comment

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

is it ever mutated ?

Copy link
Member Author

@SparrowLii SparrowLii Sep 8, 2022

Choose a reason for hiding this comment

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

not for now, but I think we should change it based on -Z threads=n option in a early stage of compilation

Copy link
Member Author

Choose a reason for hiding this comment

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

We use SyncUnsafeCell<T> now

static THREADS_COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);

fn next() -> u32 {
THREADS_COUNTER.fetch_add(1, SeqCst)
Copy link
Member

Choose a reason for hiding this comment

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

This can be Relaxed instead

@bors
Copy link
Contributor

bors commented Sep 8, 2022

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

@rust-timer
Copy link
Collaborator

Queued 49d1d103e085e772150709059f0bbb75c5a37a89 with parent 44adfcc, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (49d1d103e085e772150709059f0bbb75c5a37a89): comparison URL.

Overall result: ❌ regressions - 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.

mean1 range count2
Regressions ❌
(primary)
7.9% [0.6%, 15.7%] 239
Regressions ❌
(secondary)
7.6% [0.3%, 31.7%] 250
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 7.9% [0.6%, 15.7%] 239

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.

mean1 range count2
Regressions ❌
(primary)
1.5% [1.2%, 1.7%] 3
Regressions ❌
(secondary)
2.4% [1.5%, 4.4%] 15
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-3.5%, -2.7%] 3
All ❌✅ (primary) 1.5% [1.2%, 1.7%] 3

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.

mean1 range count2
Regressions ❌
(primary)
4.5% [1.4%, 8.2%] 173
Regressions ❌
(secondary)
5.0% [1.7%, 18.7%] 168
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.5% [1.4%, 8.2%] 173

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

@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 Sep 8, 2022
@SparrowLii
Copy link
Member Author

SparrowLii commented Sep 8, 2022

Well, the refactored implementation of Lock still reduces the overall efficiency of the compiler by around 8%, which is frustrating but not unexpected.
In fact, I have some crazier ideas for parallel compilation without losing efficiency, which means a new language feature.
That is, if we could allow 'incomplete' generic parameters in struct declarations like this:

trait S<T> {
    type Item = T;
}

struct MyStruct<S<_>> {
    feild1: S<u8>,
    feild2: S<i32>,
    feild3: S<f64>,
}

We only need to decide whether S is RefCell or Mutex, and we can implement both shared and non-shared instances for MyStruct.
I think it's a very exciting idea, and I'll go get more perspectives in the community.

@vacuus
Copy link
Contributor

vacuus commented Sep 9, 2022

If I'm not mistaken, the proposed code is using some sort of higher-kinded type (HKT) feature. The generic associated type feature is close to being merged stabilized (#96709), and is a subset of a theoretical HKT feature. I'm not remotely a lang dev, but GATs have been hard enough to be mostly accepted, and based on the discussion in that PR, getting full HKTs will at least be a slog. At least discussion around HKTs can use parallel rustc as a motivating example. GATs might need to be more established within the culture before HKTs have a fighting chance to be implemented.
In any case, parallel rustc is an awesome effort. Much thanks to you and all the other contributors!

@SparrowLii
Copy link
Member Author

SparrowLii commented Sep 9, 2022

Let me try if we can solve the above mentioned problem with GAT.

@eggyal
Copy link
Contributor

eggyal commented Sep 9, 2022

The pattern can be achieved with GATs: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c087b818a1f98c23bf6df235c193e9cf

@SparrowLii
Copy link
Member Author

Since std::sync::Mutex was optimized not long ago, let's try using it first

@Kobzol
Copy link
Contributor

Kobzol commented Oct 12, 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 Oct 12, 2022
@bors
Copy link
Contributor

bors commented Oct 12, 2022

⌛ Trying commit 0ecf80ccd388abf402c417ffad6ce2cafd864bed with merge d7be50832a0dcadf25609eb8b25f87da3e05dbb3...

@bors
Copy link
Contributor

bors commented Oct 12, 2022

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

@rust-timer
Copy link
Collaborator

Queued d7be50832a0dcadf25609eb8b25f87da3e05dbb3 with parent e6ce562, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d7be50832a0dcadf25609eb8b25f87da3e05dbb3): comparison URL.

Overall result: ❌ regressions - 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.

mean1 range count2
Regressions ❌
(primary)
4.6% [0.4%, 10.2%] 230
Regressions ❌
(secondary)
4.6% [0.4%, 18.1%] 241
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.6% [0.4%, 10.2%] 230

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.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.8% [-2.8%, -2.7%] 2
Improvements ✅
(secondary)
-2.7% [-3.7%, -2.0%] 26
All ❌✅ (primary) -2.8% [-2.8%, -2.7%] 2

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.

mean1 range count2
Regressions ❌
(primary)
5.4% [1.6%, 13.2%] 191
Regressions ❌
(secondary)
6.8% [2.1%, 25.8%] 201
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 5.4% [1.6%, 13.2%] 191

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 12, 2022
@rustbot rustbot added the A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) label Oct 24, 2022
@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 Apr 23, 2023
@bors
Copy link
Contributor

bors commented Apr 23, 2023

⌛ Trying commit f925ea0 with merge da328d6533d0dfe64e6cfd676472a6b2057b2aa9...

@bors
Copy link
Contributor

bors commented Apr 23, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (da328d6533d0dfe64e6cfd676472a6b2057b2aa9): comparison URL.

Overall result: ❌ regressions - 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-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)
3.7% [0.3%, 6.5%] 221
Regressions ❌
(secondary)
3.7% [0.6%, 10.3%] 234
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.7% [0.3%, 6.5%] 221

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)
6.6% [0.7%, 18.9%] 127
Regressions ❌
(secondary)
4.4% [0.8%, 15.8%] 128
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.8% [-3.8%, -3.8%] 1
All ❌✅ (primary) 6.6% [0.7%, 18.9%] 127

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)
4.7% [0.7%, 20.8%] 207
Regressions ❌
(secondary)
5.1% [1.2%, 10.6%] 204
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.7% [0.7%, 20.8%] 207

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 23, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request May 13, 2023
Introduce `DynSend` and `DynSync` auto trait for parallel compiler

part of parallel-rustc rust-lang#101566

This PR introduces `DynSend / DynSync` trait and `FromDyn / IntoDyn` structure in rustc_data_structure::marker. `FromDyn` can dynamically check data structures for thread safety when switching to parallel environments (such as calling `par_for_each_in`). This happens only when `-Z threads > 1` so it doesn't affect single-threaded mode's compile efficiency.

r? `@cjgillot`
@SparrowLii
Copy link
Member Author

(1) Switch serial/parallel at runtime(refactor LockShardedRwLock etc. in rustc_data_structures)
(2) Specialization for QueryCache
(3) Enable parallel compilation only in Full and IncrFull scenarios

With methods above, under 8 threads, Full and IncrFull have an average performance improvement of 10.6%, while IncrUnchanged and IncrPatched have a regression of 3.8%.
#109776 (comment)
Under single thread, the regression is 2.1% on average.
#109776 (comment)

@Dylan-DPC Dylan-DPC marked this pull request as draft May 20, 2023 13:58
@SparrowLii
Copy link
Member Author

We have solved perf problem under multi-threads at current, and will finish the work under single thread this month.

@bjorn3
Copy link
Member

bjorn3 commented Nov 24, 2023

#117435 has enabled parallel rustc on nightly.

@SparrowLii
Copy link
Member Author

Yea, we should close this now

@SparrowLii SparrowLii closed this Nov 25, 2023
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) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.