Skip to content

Commit ec1df88

Browse files
committed
Increase parallelism in various locations
1 parent d3c9082 commit ec1df88

17 files changed

+292
-141
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1964,10 +1964,11 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
19641964

19651965
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), ErrorGuaranteed> {
19661966
let items = tcx.hir_module_items(module);
1967-
let mut res = items.par_items(|item| tcx.ensure().check_well_formed(item.owner_id));
1968-
res = res.and(items.par_impl_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
1969-
res = res.and(items.par_trait_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
1970-
res = res.and(items.par_foreign_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
1967+
let mut res = items.try_par_items(|item| tcx.ensure().check_well_formed(item.owner_id));
1968+
res = res.and(items.try_par_impl_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
1969+
res = res.and(items.try_par_trait_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
1970+
res =
1971+
res.and(items.try_par_foreign_items(|item| tcx.ensure().check_well_formed(item.owner_id)));
19711972
if module == LocalModDefId::CRATE_DEF_ID {
19721973
super::entry::check_for_entry_fn(tcx);
19731974
}

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ mod type_of;
5454
// Main entry point
5555

5656
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
57-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
57+
tcx.hir().par_visit_item_likes_in_module(module_def_id, || CollectItemTypesVisitor { tcx });
5858
}
5959

6060
pub fn provide(providers: &mut Providers) {

compiler/rustc_hir_analysis/src/lib.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ mod outlives;
9898
pub mod structured_errors;
9999
mod variance;
100100

101+
use rustc_data_structures::sync::par_for_each_in;
101102
use rustc_errors::ErrorGuaranteed;
102103
use rustc_hir as hir;
103104
use rustc_middle::middle;
@@ -163,7 +164,11 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
163164
// this ensures that later parts of type checking can assume that items
164165
// have valid types and not error
165166
tcx.sess.time("type_collecting", || {
166-
tcx.hir().for_each_module(|module| tcx.ensure().collect_mod_item_types(module))
167+
// Run dependencies of type collecting before entering the loop
168+
tcx.ensure_with_value().inferred_outlives_crate(());
169+
170+
let _prof_timer = tcx.sess.timer("type_collecting_loop");
171+
tcx.hir().par_for_each_module(|module| tcx.ensure().collect_mod_item_types(module));
167172
});
168173

169174
// FIXME(matthewjasper) We shouldn't need to use `track_errors` anywhere in this function
@@ -179,9 +184,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
179184
// Check impls constrain their parameters
180185
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_impl_wf(module));
181186

182-
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
183-
tcx.ensure().coherent_trait(trait_def_id);
184-
}
187+
par_for_each_in(tcx.all_local_trait_impls(()), |(trait_def_id, _)| {
188+
tcx.ensure().coherent_trait(*trait_def_id);
189+
});
185190

186191
// these queries are executed for side-effects (error reporting):
187192
tcx.ensure().crate_inherent_impls(());

compiler/rustc_interface/src/passes.rs

+67-47
Original file line numberDiff line numberDiff line change
@@ -706,14 +706,28 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
706706
CStore::from_tcx(tcx).report_unused_deps(tcx);
707707
},
708708
{
709+
// Prefetch this as it is used later by the loop below
710+
// to prevent multiple threads from blocking on it.
711+
tcx.ensure_with_value().get_lang_items(());
712+
713+
let _timer = tcx.sess.timer("misc_module_passes");
709714
tcx.hir().par_for_each_module(|module| {
710715
tcx.ensure().check_mod_loops(module);
711716
tcx.ensure().check_mod_attrs(module);
712717
tcx.ensure().check_mod_naked_functions(module);
713-
tcx.ensure().check_mod_unstable_api_usage(module);
714718
tcx.ensure().check_mod_const_bodies(module);
715719
});
716720
},
721+
{
722+
// Prefetch this as it is used later by the loop below
723+
// to prevent multiple threads from blocking on it.
724+
tcx.ensure_with_value().stability_index(());
725+
726+
let _timer = tcx.sess.timer("check_unstable_api_usage");
727+
tcx.hir().par_for_each_module(|module| {
728+
tcx.ensure().check_mod_unstable_api_usage(module);
729+
});
730+
},
717731
{
718732
sess.time("unused_lib_feature_checking", || {
719733
rustc_passes::stability::check_unused_or_stable_features(tcx)
@@ -733,32 +747,48 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
733747
// passes are timed inside typeck
734748
rustc_hir_analysis::check_crate(tcx)?;
735749

736-
sess.time("MIR_borrow_checking", || {
737-
tcx.hir().par_body_owners(|def_id| {
738-
// Run unsafety check because it's responsible for stealing and
739-
// deallocating THIR.
740-
tcx.ensure().check_unsafety(def_id);
741-
tcx.ensure().mir_borrowck(def_id)
742-
});
743-
});
744-
745-
sess.time("MIR_effect_checking", || {
746-
for def_id in tcx.hir().body_owners() {
747-
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
748-
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);
749-
}
750-
tcx.ensure().has_ffi_unwind_calls(def_id);
751-
752-
// If we need to codegen, ensure that we emit all errors from
753-
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
754-
// them later during codegen.
755-
if tcx.sess.opts.output_types.should_codegen()
756-
|| tcx.hir().body_const_context(def_id).is_some()
750+
sess.time("misc_checking_2", || {
751+
parallel!(
757752
{
758-
tcx.ensure().mir_drops_elaborated_and_const_checked(def_id);
759-
tcx.ensure().unused_generic_params(ty::InstanceDef::Item(def_id.to_def_id()));
753+
// Prefetch this as it is used later by lint checking and privacy checking.
754+
tcx.ensure_with_value().effective_visibilities(());
755+
},
756+
{
757+
sess.time("MIR_borrow_checking", || {
758+
tcx.hir().par_body_owners(|def_id| {
759+
// Run unsafety check because it's responsible for stealing and
760+
// deallocating THIR.
761+
tcx.ensure().check_unsafety(def_id);
762+
tcx.ensure().mir_borrowck(def_id)
763+
});
764+
});
765+
},
766+
{
767+
sess.time("MIR_effect_checking", || {
768+
for def_id in tcx.hir().body_owners() {
769+
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
770+
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);
771+
}
772+
tcx.ensure().has_ffi_unwind_calls(def_id);
773+
774+
// If we need to codegen, ensure that we emit all errors from
775+
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
776+
// them later during codegen.
777+
if tcx.sess.opts.output_types.should_codegen()
778+
|| tcx.hir().body_const_context(def_id).is_some()
779+
{
780+
tcx.ensure().mir_drops_elaborated_and_const_checked(def_id);
781+
tcx.ensure()
782+
.unused_generic_params(ty::InstanceDef::Item(def_id.to_def_id()));
783+
}
784+
}
785+
});
786+
},
787+
{
788+
sess.time("layout_testing", || layout_test::test_layout(tcx));
789+
sess.time("abi_testing", || abi_test::test_abi(tcx));
760790
}
761-
}
791+
)
762792
});
763793

764794
tcx.hir().par_body_owners(|def_id| {
@@ -768,9 +798,6 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
768798
}
769799
});
770800

771-
sess.time("layout_testing", || layout_test::test_layout(tcx));
772-
sess.time("abi_testing", || abi_test::test_abi(tcx));
773-
774801
// Avoid overwhelming user with errors if borrow checking failed.
775802
// I'm not sure how helpful this is, to be honest, but it avoids a
776803
// lot of annoying errors in the ui tests (basically,
@@ -783,25 +810,18 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
783810
sess.time("misc_checking_3", || {
784811
parallel!(
785812
{
786-
tcx.ensure().effective_visibilities(());
787-
788-
parallel!(
789-
{
790-
tcx.ensure().check_private_in_public(());
791-
},
792-
{
793-
tcx.hir()
794-
.par_for_each_module(|module| tcx.ensure().check_mod_deathness(module));
795-
},
796-
{
797-
sess.time("lint_checking", || {
798-
rustc_lint::check_crate(tcx);
799-
});
800-
},
801-
{
802-
tcx.ensure().clashing_extern_declarations(());
803-
}
804-
);
813+
tcx.ensure().check_private_in_public(());
814+
},
815+
{
816+
tcx.hir().par_for_each_module(|module| tcx.ensure().check_mod_deathness(module));
817+
},
818+
{
819+
sess.time("lint_checking", || {
820+
rustc_lint::check_crate(tcx);
821+
});
822+
},
823+
{
824+
tcx.ensure().clashing_extern_declarations(());
805825
},
806826
{
807827
sess.time("privacy_checking_modules", || {

compiler/rustc_middle/src/hir/map/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,34 @@ impl<'hir> Map<'hir> {
521521
}
522522
}
523523

524+
/// A parallel version of `visit_item_likes_in_module`.
525+
pub fn par_visit_item_likes_in_module<V>(
526+
&self,
527+
module: LocalModDefId,
528+
make_visitor: impl Fn() -> V + DynSync,
529+
) where
530+
V: Visitor<'hir>,
531+
{
532+
let module = self.tcx.hir_module_items(module);
533+
534+
parallel!(
535+
{
536+
module.par_items(|id| make_visitor().visit_item(self.item(id)));
537+
},
538+
{
539+
module.par_trait_items(|id| make_visitor().visit_trait_item(self.trait_item(id)));
540+
},
541+
{
542+
module.par_impl_items(|id| make_visitor().visit_impl_item(self.impl_item(id)));
543+
},
544+
{
545+
module.par_foreign_items(|id| {
546+
make_visitor().visit_foreign_item(self.foreign_item(id))
547+
});
548+
}
549+
);
550+
}
551+
524552
pub fn for_each_module(self, mut f: impl FnMut(LocalModDefId)) {
525553
let crate_items = self.tcx.hir_crate_items(());
526554
for module in crate_items.submodules.iter() {

compiler/rustc_middle/src/hir/mod.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod place;
88

99
use crate::query::Providers;
1010
use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
11-
use rustc_data_structures::sync::{try_par_for_each_in, DynSend, DynSync};
11+
use rustc_data_structures::sync::{par_for_each_in, try_par_for_each_in, DynSend, DynSync};
1212
use rustc_hir::def::DefKind;
1313
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1414
use rustc_hir::*;
@@ -56,33 +56,49 @@ impl ModuleItems {
5656
self.owners().map(|id| id.def_id)
5757
}
5858

59-
pub fn par_items(
59+
pub fn try_par_items(
6060
&self,
6161
f: impl Fn(ItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
6262
) -> Result<(), ErrorGuaranteed> {
6363
try_par_for_each_in(&self.items[..], |&id| f(id))
6464
}
6565

66-
pub fn par_trait_items(
66+
pub fn try_par_trait_items(
6767
&self,
6868
f: impl Fn(TraitItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
6969
) -> Result<(), ErrorGuaranteed> {
7070
try_par_for_each_in(&self.trait_items[..], |&id| f(id))
7171
}
7272

73-
pub fn par_impl_items(
73+
pub fn try_par_impl_items(
7474
&self,
7575
f: impl Fn(ImplItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
7676
) -> Result<(), ErrorGuaranteed> {
7777
try_par_for_each_in(&self.impl_items[..], |&id| f(id))
7878
}
7979

80-
pub fn par_foreign_items(
80+
pub fn try_par_foreign_items(
8181
&self,
8282
f: impl Fn(ForeignItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
8383
) -> Result<(), ErrorGuaranteed> {
8484
try_par_for_each_in(&self.foreign_items[..], |&id| f(id))
8585
}
86+
87+
pub fn par_items(&self, f: impl Fn(ItemId) + DynSend + DynSync) {
88+
par_for_each_in(&self.items[..], |&id| f(id))
89+
}
90+
91+
pub fn par_trait_items(&self, f: impl Fn(TraitItemId) + DynSend + DynSync) {
92+
par_for_each_in(&self.trait_items[..], |&id| f(id))
93+
}
94+
95+
pub fn par_impl_items(&self, f: impl Fn(ImplItemId) + DynSend + DynSync) {
96+
par_for_each_in(&self.impl_items[..], |&id| f(id))
97+
}
98+
99+
pub fn par_foreign_items(&self, f: impl Fn(ForeignItemId) + DynSend + DynSync) {
100+
par_for_each_in(&self.foreign_items[..], |&id| f(id))
101+
}
86102
}
87103

88104
impl<'tcx> TyCtxt<'tcx> {

compiler/rustc_monomorphize/src/collector.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
//! regardless of whether it is actually needed or not.
166166
167167
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
168-
use rustc_data_structures::sync::{par_for_each_in, MTLock, MTLockRef};
168+
use rustc_data_structures::sync::{join, par_for_each_in, MTLock, MTLockRef};
169169
use rustc_hir as hir;
170170
use rustc_hir::def::DefKind;
171171
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
@@ -260,8 +260,19 @@ pub fn collect_crate_mono_items(
260260
) -> (FxHashSet<MonoItem<'_>>, UsageMap<'_>) {
261261
let _prof_timer = tcx.prof.generic_activity("monomorphization_collector");
262262

263-
let roots =
264-
tcx.sess.time("monomorphization_collector_root_collections", || collect_roots(tcx, mode));
263+
let (roots, _) = join(
264+
|| {
265+
tcx.sess
266+
.time("monomorphization_collector_root_collections", || collect_roots(tcx, mode))
267+
},
268+
|| {
269+
if tcx.sess.opts.share_generics() {
270+
// Prefetch upstream_monomorphizations as it's very likely to be used in
271+
// code generation later and this is decent spot to compute it.
272+
tcx.ensure().upstream_monomorphizations(());
273+
}
274+
},
275+
);
265276

266277
debug!("building mono item graph, beginning at roots");
267278

tests/ui/associated-inherent-types/bugs/cycle-iat-inside-of-adt.stderr

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
1-
error[E0391]: cycle detected when computing predicates of `Foo`
1+
error[E0391]: cycle detected when computing the inferred outlives predicates for items in this crate
2+
|
3+
note: ...which requires computing type of `Foo::bar`...
4+
--> $DIR/cycle-iat-inside-of-adt.rs:8:5
5+
|
6+
LL | bar: Self::Bar,
7+
| ^^^^^^^^^^^^^^
8+
note: ...which requires computing normalized predicates of `Foo`...
29
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
310
|
411
LL | struct Foo {
512
| ^^^^^^^^^^
6-
|
713
note: ...which requires computing predicates of `Foo`...
814
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
915
|
1016
LL | struct Foo {
1117
| ^^^^^^^^^^
12-
note: ...which requires computing inferred outlives predicates of `Foo`...
18+
note: ...which requires computing predicates of `Foo`...
1319
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
1420
|
1521
LL | struct Foo {
1622
| ^^^^^^^^^^
17-
= note: ...which requires computing the inferred outlives predicates for items in this crate...
18-
note: ...which requires computing type of `Foo::bar`...
19-
--> $DIR/cycle-iat-inside-of-adt.rs:8:5
20-
|
21-
LL | bar: Self::Bar,
22-
| ^^^^^^^^^^^^^^
23-
note: ...which requires computing normalized predicates of `Foo`...
23+
note: ...which requires computing inferred outlives predicates of `Foo`...
2424
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
2525
|
2626
LL | struct Foo {
2727
| ^^^^^^^^^^
28-
= note: ...which again requires computing predicates of `Foo`, completing the cycle
29-
note: cycle used when collecting item types in top-level module
30-
--> $DIR/cycle-iat-inside-of-adt.rs:3:1
31-
|
32-
LL | / #![feature(inherent_associated_types)]
33-
LL | | #![allow(incomplete_features)]
34-
LL | | // FIXME(inherent_associated_types): This should pass.
35-
LL | |
36-
... |
37-
LL | |
38-
LL | | fn main() {}
39-
| |____________^
28+
= note: ...which again requires computing the inferred outlives predicates for items in this crate, completing the cycle
29+
= note: cycle used when running analysis passes on this crate
4030
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
4131

4232
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)