Skip to content

Commit e703606

Browse files
committed
Increase parallelism in various locations
1 parent ae9173d commit e703606

File tree

8 files changed

+137
-82
lines changed

8 files changed

+137
-82
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -2323,14 +2323,21 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
23232323

23242324
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), ErrorGuaranteed> {
23252325
let items = tcx.hir_module_items(module);
2326-
let res = items
2327-
.par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2328-
.and(items.par_impl_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2329-
.and(items.par_trait_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2330-
.and(
2331-
items.par_foreign_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)),
2332-
)
2333-
.and(items.par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
2326+
let res =
2327+
items
2328+
.try_par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2329+
.and(
2330+
items.try_par_impl_items(|item| {
2331+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2332+
}),
2333+
)
2334+
.and(items.try_par_trait_items(|item| {
2335+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2336+
}))
2337+
.and(items.try_par_foreign_items(|item| {
2338+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2339+
}))
2340+
.and(items.try_par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
23342341
if module == LocalModDefId::CRATE_DEF_ID {
23352342
super::entry::check_for_entry_fn(tcx);
23362343
}

Diff for: compiler/rustc_hir_analysis/src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ mod variance;
9494

9595
pub use errors::NoVariantNamed;
9696
use rustc_abi::ExternAbi;
97+
use rustc_data_structures::sync::par_for_each_in;
9798
use rustc_hir as hir;
9899
use rustc_hir::def::DefKind;
99100
use rustc_middle::middle;
@@ -179,6 +180,9 @@ pub fn provide(providers: &mut Providers) {
179180
pub fn check_crate(tcx: TyCtxt<'_>) {
180181
let _prof_timer = tcx.sess.timer("type_check_crate");
181182

183+
// Run dependencies of type checking before entering the loops below
184+
tcx.ensure_done().inferred_outlives_crate(());
185+
182186
tcx.sess.time("coherence_checking", || {
183187
// When discarding query call results, use an explicit type to indicate
184188
// what we are intending to discard, to help future type-based refactoring.
@@ -188,9 +192,10 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
188192
let _: R = tcx.ensure_ok().check_mod_type_wf(module);
189193
});
190194

191-
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
195+
par_for_each_in(tcx.all_local_trait_impls(()), |(trait_def_id, _)| {
192196
let _: R = tcx.ensure_ok().coherent_trait(trait_def_id);
193-
}
197+
});
198+
194199
// these queries are executed for side-effects (error reporting):
195200
let _: R = tcx.ensure_ok().crate_inherent_impls_validity_check(());
196201
let _: R = tcx.ensure_ok().crate_inherent_impls_overlap_check(());

Diff for: compiler/rustc_interface/src/passes.rs

+62-42
Original file line numberDiff line numberDiff line change
@@ -912,10 +912,24 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
912912
CStore::from_tcx(tcx).report_unused_deps(tcx);
913913
},
914914
{
915+
// Prefetch this as it is used later by the loop below
916+
// to prevent multiple threads from blocking on it.
917+
tcx.ensure_done().get_lang_items(());
918+
919+
let _timer = tcx.sess.timer("misc_module_passes");
915920
tcx.par_hir_for_each_module(|module| {
916921
tcx.ensure_ok().check_mod_loops(module);
917922
tcx.ensure_ok().check_mod_attrs(module);
918923
tcx.ensure_ok().check_mod_naked_functions(module);
924+
});
925+
},
926+
{
927+
// Prefetch this as it is used later by the loop below
928+
// to prevent multiple threads from blocking on it.
929+
tcx.ensure_done().stability_index(());
930+
931+
let _timer = tcx.sess.timer("check_unstable_api_usage");
932+
tcx.par_hir_for_each_module(|module| {
919933
tcx.ensure_ok().check_mod_unstable_api_usage(module);
920934
});
921935
},
@@ -950,28 +964,45 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
950964
// This improves performance by allowing lock-free access to them.
951965
tcx.untracked().definitions.freeze();
952966

953-
sess.time("MIR_borrow_checking", || {
954-
tcx.par_hir_body_owners(|def_id| {
955-
// Run unsafety check because it's responsible for stealing and
956-
// deallocating THIR.
957-
tcx.ensure_ok().check_unsafety(def_id);
958-
tcx.ensure_ok().mir_borrowck(def_id)
959-
});
960-
});
961-
sess.time("MIR_effect_checking", || {
962-
tcx.par_hir_body_owners(|def_id| {
963-
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
964-
965-
// If we need to codegen, ensure that we emit all errors from
966-
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
967-
// them later during codegen.
968-
if tcx.sess.opts.output_types.should_codegen()
969-
|| tcx.hir_body_const_context(def_id).is_some()
967+
sess.time("misc_checking_2", || {
968+
parallel!(
969+
{
970+
// Prefetch this as it is used later by lint checking and privacy checking.
971+
tcx.ensure_done().effective_visibilities(());
972+
},
973+
{
974+
sess.time("MIR_borrow_checking", || {
975+
tcx.par_hir_body_owners(|def_id| {
976+
// Run unsafety check because it's responsible for stealing and
977+
// deallocating THIR.
978+
tcx.ensure_ok().check_unsafety(def_id);
979+
tcx.ensure_ok().mir_borrowck(def_id)
980+
});
981+
});
982+
},
970983
{
971-
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
984+
sess.time("MIR_effect_checking", || {
985+
tcx.par_hir_body_owners(|def_id| {
986+
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
987+
988+
// If we need to codegen, ensure that we emit all errors from
989+
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
990+
// them later during codegen.
991+
if tcx.sess.opts.output_types.should_codegen()
992+
|| tcx.hir_body_const_context(def_id).is_some()
993+
{
994+
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
995+
}
996+
});
997+
});
998+
},
999+
{
1000+
sess.time("layout_testing", || layout_test::test_layout(tcx));
1001+
sess.time("abi_testing", || abi_test::test_abi(tcx));
9721002
}
973-
});
1003+
)
9741004
});
1005+
9751006
sess.time("coroutine_obligations", || {
9761007
tcx.par_hir_body_owners(|def_id| {
9771008
if tcx.is_coroutine(def_id.to_def_id()) {
@@ -988,9 +1019,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
9881019
});
9891020
});
9901021

991-
sess.time("layout_testing", || layout_test::test_layout(tcx));
992-
sess.time("abi_testing", || abi_test::test_abi(tcx));
993-
9941022
// If `-Zvalidate-mir` is set, we also want to compute the final MIR for each item
9951023
// (either its `mir_for_ctfe` or `optimized_mir`) since that helps uncover any bugs
9961024
// in MIR optimizations that may only be reachable through codegen, or other codepaths
@@ -1026,26 +1054,18 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
10261054
sess.time("misc_checking_3", || {
10271055
parallel!(
10281056
{
1029-
tcx.ensure_ok().effective_visibilities(());
1030-
1031-
parallel!(
1032-
{
1033-
tcx.ensure_ok().check_private_in_public(());
1034-
},
1035-
{
1036-
tcx.par_hir_for_each_module(|module| {
1037-
tcx.ensure_ok().check_mod_deathness(module)
1038-
});
1039-
},
1040-
{
1041-
sess.time("lint_checking", || {
1042-
rustc_lint::check_crate(tcx);
1043-
});
1044-
},
1045-
{
1046-
tcx.ensure_ok().clashing_extern_declarations(());
1047-
}
1048-
);
1057+
tcx.ensure_ok().check_private_in_public(());
1058+
},
1059+
{
1060+
tcx.par_hir_for_each_module(|module| tcx.ensure_ok().check_mod_deathness(module));
1061+
},
1062+
{
1063+
sess.time("lint_checking", || {
1064+
rustc_lint::check_crate(tcx);
1065+
});
1066+
},
1067+
{
1068+
tcx.ensure_ok().clashing_extern_declarations(());
10491069
},
10501070
{
10511071
sess.time("privacy_checking_modules", || {

Diff for: compiler/rustc_middle/src/hir/mod.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod place;
99
use rustc_data_structures::fingerprint::Fingerprint;
1010
use rustc_data_structures::sorted_map::SortedMap;
1111
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
12-
use rustc_data_structures::sync::{DynSend, DynSync, try_par_for_each_in};
12+
use rustc_data_structures::sync::{DynSend, DynSync, par_for_each_in, try_par_for_each_in};
1313
use rustc_hir::def::DefKind;
1414
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1515
use rustc_hir::*;
@@ -79,40 +79,56 @@ impl ModuleItems {
7979
self.owners().map(|id| id.def_id)
8080
}
8181

82-
pub fn par_items(
82+
pub fn try_par_items(
8383
&self,
8484
f: impl Fn(ItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
8585
) -> Result<(), ErrorGuaranteed> {
8686
try_par_for_each_in(&self.free_items[..], |&id| f(id))
8787
}
8888

89-
pub fn par_trait_items(
89+
pub fn try_par_trait_items(
9090
&self,
9191
f: impl Fn(TraitItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
9292
) -> Result<(), ErrorGuaranteed> {
9393
try_par_for_each_in(&self.trait_items[..], |&id| f(id))
9494
}
9595

96-
pub fn par_impl_items(
96+
pub fn try_par_impl_items(
9797
&self,
9898
f: impl Fn(ImplItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
9999
) -> Result<(), ErrorGuaranteed> {
100100
try_par_for_each_in(&self.impl_items[..], |&id| f(id))
101101
}
102102

103-
pub fn par_foreign_items(
103+
pub fn try_par_foreign_items(
104104
&self,
105105
f: impl Fn(ForeignItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
106106
) -> Result<(), ErrorGuaranteed> {
107107
try_par_for_each_in(&self.foreign_items[..], |&id| f(id))
108108
}
109109

110-
pub fn par_opaques(
110+
pub fn try_par_opaques(
111111
&self,
112112
f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
113113
) -> Result<(), ErrorGuaranteed> {
114114
try_par_for_each_in(&self.opaques[..], |&id| f(id))
115115
}
116+
117+
pub fn par_items(&self, f: impl Fn(ItemId) + DynSend + DynSync) {
118+
par_for_each_in(&self.free_items[..], |&id| f(id))
119+
}
120+
121+
pub fn par_trait_items(&self, f: impl Fn(TraitItemId) + DynSend + DynSync) {
122+
par_for_each_in(&self.trait_items[..], |&id| f(id))
123+
}
124+
125+
pub fn par_impl_items(&self, f: impl Fn(ImplItemId) + DynSend + DynSync) {
126+
par_for_each_in(&self.impl_items[..], |&id| f(id))
127+
}
128+
129+
pub fn par_foreign_items(&self, f: impl Fn(ForeignItemId) + DynSend + DynSync) {
130+
par_for_each_in(&self.foreign_items[..], |&id| f(id))
131+
}
116132
}
117133

118134
impl<'tcx> TyCtxt<'tcx> {

Diff for: compiler/rustc_monomorphize/src/collector.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ use std::path::PathBuf;
209209

210210
use rustc_attr_parsing::InlineAttr;
211211
use rustc_data_structures::fx::FxIndexMap;
212-
use rustc_data_structures::sync::{MTLock, par_for_each_in};
212+
use rustc_data_structures::sync::{MTLock, join, par_for_each_in};
213213
use rustc_data_structures::unord::{UnordMap, UnordSet};
214214
use rustc_hir as hir;
215215
use rustc_hir::def::DefKind;
@@ -1671,9 +1671,20 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
16711671
) -> (Vec<MonoItem<'tcx>>, UsageMap<'tcx>) {
16721672
let _prof_timer = tcx.prof.generic_activity("monomorphization_collector");
16731673

1674-
let roots = tcx
1675-
.sess
1676-
.time("monomorphization_collector_root_collections", || collect_roots(tcx, strategy));
1674+
let (roots, _) = join(
1675+
|| {
1676+
tcx.sess.time("monomorphization_collector_root_collections", || {
1677+
collect_roots(tcx, strategy)
1678+
})
1679+
},
1680+
|| {
1681+
if tcx.sess.opts.share_generics() {
1682+
// Prefetch upstream_monomorphizations as it's very likely to be used in
1683+
// code generation later and this is decent spot to compute it.
1684+
tcx.ensure_ok().upstream_monomorphizations(());
1685+
}
1686+
},
1687+
);
16771688

16781689
debug!("building mono item graph, beginning at roots");
16791690

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

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
error[E0391]: cycle detected when computing predicates of `Foo`
2-
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
3-
|
4-
LL | struct Foo {
5-
| ^^^^^^^^^^
6-
|
7-
note: ...which requires computing inferred outlives-predicates of `Foo`...
8-
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
1+
error[E0391]: cycle detected when computing the inferred outlives-predicates for items in this crate
92
|
10-
LL | struct Foo {
11-
| ^^^^^^^^^^
12-
= note: ...which requires computing the inferred outlives-predicates for items in this crate...
133
note: ...which requires computing type of `Foo::bar`...
144
--> $DIR/cycle-iat-inside-of-adt.rs:8:5
155
|
@@ -20,12 +10,18 @@ note: ...which requires computing normalized predicates of `Foo`...
2010
|
2111
LL | struct Foo {
2212
| ^^^^^^^^^^
23-
= note: ...which again requires computing predicates of `Foo`, completing the cycle
24-
note: cycle used when checking that `Foo` is well-formed
13+
note: ...which requires computing predicates of `Foo`...
14+
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
15+
|
16+
LL | struct Foo {
17+
| ^^^^^^^^^^
18+
note: ...which requires computing inferred outlives-predicates of `Foo`...
2519
--> $DIR/cycle-iat-inside-of-adt.rs:7:1
2620
|
2721
LL | struct Foo {
2822
| ^^^^^^^^^^
23+
= note: ...which again requires computing the inferred outlives-predicates for items in this crate, completing the cycle
24+
= note: cycle used when running analysis passes on this crate
2925
= 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
3026

3127
error: aborting due to 1 previous error

Diff for: tests/ui/span/issue-35987.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct Foo<T: Clone>(T);
33
use std::ops::Add;
44

55
impl<T: Clone, Add> Add for Foo<T> {
6-
//~^ ERROR expected trait, found type parameter
6+
//~^ ERROR expected trait, found type parameter
77
type Output = usize;
88

99
fn add(self, rhs: Self) -> Self::Output {

Diff for: tests/ui/traits/object/suggestion-trait-object-issue-139174.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ error[E0404]: expected trait, found builtin type `usize`
1616
LL | x: usize + 'a,
1717
| ^^^^^ not a trait
1818

19+
error[E0782]: expected a type, found a trait
20+
--> $DIR/suggestion-trait-object-issue-139174.rs:16:8
21+
|
22+
LL | x: usize + 'a,
23+
| ^^^^^^^^^^
24+
1925
error[E0782]: expected a type, found a trait
2026
--> $DIR/suggestion-trait-object-issue-139174.rs:3:36
2127
|
@@ -28,12 +34,6 @@ error[E0782]: expected a type, found a trait
2834
LL | fn create_adder<'a>(x: i32) -> usize + 'a {
2935
| ^^^^^^^^^^
3036

31-
error[E0782]: expected a type, found a trait
32-
--> $DIR/suggestion-trait-object-issue-139174.rs:16:8
33-
|
34-
LL | x: usize + 'a,
35-
| ^^^^^^^^^^
36-
3737
error: aborting due to 6 previous errors
3838

3939
Some errors have detailed explanations: E0404, E0782.

0 commit comments

Comments
 (0)