Skip to content

Commit 9263df8

Browse files
committed
Increase parallelism in various locations
1 parent 0f73f0f commit 9263df8

File tree

8 files changed

+141
-85
lines changed

8 files changed

+141
-85
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

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

23522352
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), ErrorGuaranteed> {
23532353
let items = tcx.hir_module_items(module);
2354-
let res = items
2355-
.par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2356-
.and(items.par_impl_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2357-
.and(items.par_trait_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2358-
.and(
2359-
items.par_foreign_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)),
2360-
)
2361-
.and(items.par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
2354+
let res =
2355+
items
2356+
.try_par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2357+
.and(
2358+
items.try_par_impl_items(|item| {
2359+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2360+
}),
2361+
)
2362+
.and(items.try_par_trait_items(|item| {
2363+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2364+
}))
2365+
.and(items.try_par_foreign_items(|item| {
2366+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2367+
}))
2368+
.and(items.try_par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
23622369
if module == LocalModDefId::CRATE_DEF_ID {
23632370
super::entry::check_for_entry_fn(tcx);
23642371
}

compiler/rustc_hir_analysis/src/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ mod variance;
9393

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

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

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

compiler/rustc_interface/src/passes.rs

+65-44
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,25 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
956956
{
957957
tcx.ensure_ok().exportable_items(LOCAL_CRATE);
958958
tcx.ensure_ok().stable_order_of_exportable_impls(LOCAL_CRATE);
959+
960+
// Prefetch this as it is used later by the loop below
961+
// to prevent multiple threads from blocking on it.
962+
tcx.ensure_done().get_lang_items(());
963+
964+
let _timer = tcx.sess.timer("misc_module_passes");
959965
tcx.par_hir_for_each_module(|module| {
960966
tcx.ensure_ok().check_mod_loops(module);
961967
tcx.ensure_ok().check_mod_attrs(module);
962968
tcx.ensure_ok().check_mod_naked_functions(module);
969+
});
970+
},
971+
{
972+
// Prefetch this as it is used later by the loop below
973+
// to prevent multiple threads from blocking on it.
974+
tcx.ensure_done().stability_index(());
975+
976+
let _timer = tcx.sess.timer("check_unstable_api_usage");
977+
tcx.par_hir_for_each_module(|module| {
963978
tcx.ensure_ok().check_mod_unstable_api_usage(module);
964979
});
965980
},
@@ -994,30 +1009,47 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
9941009
// This improves performance by allowing lock-free access to them.
9951010
tcx.untracked().definitions.freeze();
9961011

997-
sess.time("MIR_borrow_checking", || {
998-
tcx.par_hir_body_owners(|def_id| {
999-
// Run unsafety check because it's responsible for stealing and
1000-
// deallocating THIR.
1001-
tcx.ensure_ok().check_unsafety(def_id);
1002-
if !tcx.is_typeck_child(def_id.to_def_id()) {
1003-
tcx.ensure_ok().mir_borrowck(def_id)
1004-
}
1005-
});
1006-
});
1007-
sess.time("MIR_effect_checking", || {
1008-
tcx.par_hir_body_owners(|def_id| {
1009-
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
1010-
1011-
// If we need to codegen, ensure that we emit all errors from
1012-
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
1013-
// them later during codegen.
1014-
if tcx.sess.opts.output_types.should_codegen()
1015-
|| tcx.hir_body_const_context(def_id).is_some()
1012+
sess.time("misc_checking_2", || {
1013+
parallel!(
1014+
{
1015+
// Prefetch this as it is used later by lint checking and privacy checking.
1016+
tcx.ensure_done().effective_visibilities(());
1017+
},
10161018
{
1017-
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
1019+
sess.time("MIR_borrow_checking", || {
1020+
tcx.par_hir_body_owners(|def_id| {
1021+
// Run unsafety check because it's responsible for stealing and
1022+
// deallocating THIR.
1023+
tcx.ensure_ok().check_unsafety(def_id);
1024+
if !tcx.is_typeck_child(def_id.to_def_id()) {
1025+
tcx.ensure_ok().mir_borrowck(def_id)
1026+
}
1027+
});
1028+
});
1029+
},
1030+
{
1031+
sess.time("MIR_effect_checking", || {
1032+
tcx.par_hir_body_owners(|def_id| {
1033+
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
1034+
1035+
// If we need to codegen, ensure that we emit all errors from
1036+
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
1037+
// them later during codegen.
1038+
if tcx.sess.opts.output_types.should_codegen()
1039+
|| tcx.hir_body_const_context(def_id).is_some()
1040+
{
1041+
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
1042+
}
1043+
});
1044+
});
1045+
},
1046+
{
1047+
sess.time("layout_testing", || layout_test::test_layout(tcx));
1048+
sess.time("abi_testing", || abi_test::test_abi(tcx));
10181049
}
1019-
});
1050+
)
10201051
});
1052+
10211053
sess.time("coroutine_obligations", || {
10221054
tcx.par_hir_body_owners(|def_id| {
10231055
if tcx.is_coroutine(def_id.to_def_id()) {
@@ -1036,9 +1068,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10361068
});
10371069
});
10381070

1039-
sess.time("layout_testing", || layout_test::test_layout(tcx));
1040-
sess.time("abi_testing", || abi_test::test_abi(tcx));
1041-
10421071
// If `-Zvalidate-mir` is set, we also want to compute the final MIR for each item
10431072
// (either its `mir_for_ctfe` or `optimized_mir`) since that helps uncover any bugs
10441073
// in MIR optimizations that may only be reachable through codegen, or other codepaths
@@ -1074,26 +1103,18 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
10741103
sess.time("misc_checking_3", || {
10751104
parallel!(
10761105
{
1077-
tcx.ensure_ok().effective_visibilities(());
1078-
1079-
parallel!(
1080-
{
1081-
tcx.ensure_ok().check_private_in_public(());
1082-
},
1083-
{
1084-
tcx.par_hir_for_each_module(|module| {
1085-
tcx.ensure_ok().check_mod_deathness(module)
1086-
});
1087-
},
1088-
{
1089-
sess.time("lint_checking", || {
1090-
rustc_lint::check_crate(tcx);
1091-
});
1092-
},
1093-
{
1094-
tcx.ensure_ok().clashing_extern_declarations(());
1095-
}
1096-
);
1106+
tcx.ensure_ok().check_private_in_public(());
1107+
},
1108+
{
1109+
tcx.par_hir_for_each_module(|module| tcx.ensure_ok().check_mod_deathness(module));
1110+
},
1111+
{
1112+
sess.time("lint_checking", || {
1113+
rustc_lint::check_crate(tcx);
1114+
});
1115+
},
1116+
{
1117+
tcx.ensure_ok().clashing_extern_declarations(());
10971118
},
10981119
{
10991120
sess.time("privacy_checking_modules", || {

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

compiler/rustc_monomorphize/src/collector.rs

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

211211
use rustc_attr_parsing::InlineAttr;
212212
use rustc_data_structures::fx::FxIndexMap;
213-
use rustc_data_structures::sync::{MTLock, par_for_each_in};
213+
use rustc_data_structures::sync::{MTLock, join, par_for_each_in};
214214
use rustc_data_structures::unord::{UnordMap, UnordSet};
215215
use rustc_hir as hir;
216216
use rustc_hir::def::DefKind;
@@ -1692,9 +1692,20 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
16921692
) -> (Vec<MonoItem<'tcx>>, UsageMap<'tcx>) {
16931693
let _prof_timer = tcx.prof.generic_activity("monomorphization_collector");
16941694

1695-
let roots = tcx
1696-
.sess
1697-
.time("monomorphization_collector_root_collections", || collect_roots(tcx, strategy));
1695+
let (roots, _) = join(
1696+
|| {
1697+
tcx.sess.time("monomorphization_collector_root_collections", || {
1698+
collect_roots(tcx, strategy)
1699+
})
1700+
},
1701+
|| {
1702+
if tcx.sess.opts.share_generics() {
1703+
// Prefetch upstream_monomorphizations as it's very likely to be used in
1704+
// code generation later and this is decent spot to compute it.
1705+
tcx.ensure_ok().upstream_monomorphizations(());
1706+
}
1707+
},
1708+
);
16981709

16991710
debug!("building mono item graph, beginning at roots");
17001711

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

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 {

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)