Skip to content

Commit f4342d2

Browse files
committed
Increase parallelism in various locations
1 parent 370143f commit f4342d2

10 files changed

Lines changed: 201 additions & 152 deletions

File tree

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,15 +2305,22 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
23052305

23062306
pub(super) fn check_type_wf(tcx: TyCtxt<'_>, (): ()) -> Result<(), ErrorGuaranteed> {
23072307
let items = tcx.hir_crate_items(());
2308-
let res = items
2309-
.par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2310-
.and(items.par_impl_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2311-
.and(items.par_trait_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2312-
.and(
2313-
items.par_foreign_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)),
2314-
)
2315-
.and(items.par_nested_bodies(|item| tcx.ensure_ok().check_well_formed(item)))
2316-
.and(items.par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
2308+
let res =
2309+
items
2310+
.try_par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2311+
.and(
2312+
items.try_par_impl_items(|item| {
2313+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2314+
}),
2315+
)
2316+
.and(items.try_par_trait_items(|item| {
2317+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2318+
}))
2319+
.and(items.try_par_foreign_items(|item| {
2320+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2321+
}))
2322+
.and(items.try_par_nested_bodies(|item| tcx.ensure_ok().check_well_formed(item)))
2323+
.and(items.try_par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
23172324
super::entry::check_for_entry_fn(tcx);
23182325

23192326
res

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ mod variance;
8484

8585
pub use errors::NoVariantNamed;
8686
use rustc_abi::{CVariadicStatus, ExternAbi};
87-
use rustc_hir::attrs::AttributeKind;
87+
88+
use rustc_data_structures::sync::par_for_each_in;use rustc_hir::attrs::AttributeKind;
8889
use rustc_hir::def::DefKind;
8990
use rustc_hir::lints::DelayedLint;
9091
use rustc_hir::{
@@ -175,16 +176,20 @@ fn emit_delayed_lint(lint: &DelayedLint, tcx: TyCtxt<'_>) {
175176
pub fn check_crate(tcx: TyCtxt<'_>) {
176177
let _prof_timer = tcx.sess.timer("type_check_crate");
177178

179+
// Run dependencies of type checking before entering the loops below
180+
tcx.ensure_done().inferred_outlives_crate(());
181+
178182
tcx.sess.time("coherence_checking", || {
179183
// When discarding query call results, use an explicit type to indicate
180184
// what we are intending to discard, to help future type-based refactoring.
181185
type R = Result<(), ErrorGuaranteed>;
182186

183187
let _: R = tcx.ensure_ok().check_type_wf(());
184188

185-
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
186-
let _: R = tcx.ensure_ok().coherent_trait(trait_def_id);
187-
}
189+
par_for_each_in(tcx.all_local_trait_impls(()), |(trait_def_id, _)| {
190+
let _: R = tcx.ensure_ok().coherent_trait(*trait_def_id);
191+
});
192+
188193
// these queries are executed for side-effects (error reporting):
189194
let _: R = tcx.ensure_ok().crate_inherent_impls_validity_check(());
190195
let _: R = tcx.ensure_ok().crate_inherent_impls_overlap_check(());

compiler/rustc_interface/src/passes.rs

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,19 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10681068
{
10691069
tcx.ensure_ok().exportable_items(LOCAL_CRATE);
10701070
tcx.ensure_ok().stable_order_of_exportable_impls(LOCAL_CRATE);
1071+
1072+
// Prefetch this as it is used later by the loop below
1073+
// to prevent multiple threads from blocking on it.
1074+
tcx.ensure_done().get_lang_items(());
1075+
1076+
let _timer = tcx.sess.timer("misc_module_passes");
10711077
tcx.par_hir_for_each_module(|module| {
10721078
tcx.ensure_ok().check_mod_attrs(module);
1079+
});
1080+
},
1081+
{
1082+
let _timer = tcx.sess.timer("check_unstable_api_usage");
1083+
tcx.par_hir_for_each_module(|module| {
10731084
tcx.ensure_ok().check_mod_unstable_api_usage(module);
10741085
});
10751086
},
@@ -1091,49 +1102,56 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10911102
// This improves performance by allowing lock-free access to them.
10921103
tcx.untracked().definitions.freeze();
10931104

1094-
sess.time("MIR_borrow_checking", || {
1095-
tcx.par_hir_body_owners(|def_id| {
1096-
let not_typeck_child = !tcx.is_typeck_child(def_id.to_def_id());
1097-
if not_typeck_child {
1098-
// Child unsafety and borrowck happens together with the parent
1099-
tcx.ensure_ok().check_unsafety(def_id);
1100-
}
1101-
if tcx.is_trivial_const(def_id) {
1102-
return;
1103-
}
1104-
if not_typeck_child {
1105-
tcx.ensure_ok().mir_borrowck(def_id);
1106-
tcx.ensure_ok().check_transmutes(def_id);
1107-
}
1108-
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
1109-
tcx.ensure_ok().check_liveness(def_id);
1110-
1111-
// If we need to codegen, ensure that we emit all errors from
1112-
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
1113-
// them later during codegen.
1114-
if tcx.sess.opts.output_types.should_codegen()
1115-
|| tcx.hir_body_const_context(def_id).is_some()
1105+
sess.time("misc_checking_2", || {
1106+
parallel!(
11161107
{
1117-
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
1118-
}
1119-
if tcx.is_coroutine(def_id.to_def_id()) {
1120-
tcx.ensure_ok().mir_coroutine_witnesses(def_id);
1121-
let _ = tcx.ensure_ok().check_coroutine_obligations(
1122-
tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
1123-
);
1124-
if !tcx.is_async_drop_in_place_coroutine(def_id.to_def_id()) {
1125-
// Eagerly check the unsubstituted layout for cycles.
1126-
tcx.ensure_ok().layout_of(
1127-
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
1128-
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
1129-
);
1130-
}
1108+
// Prefetch this as it is used later by lint checking and privacy checking.
1109+
tcx.ensure_done().effective_visibilities(());
1110+
},
1111+
{
1112+
sess.time("misc_body_checking", || {
1113+
tcx.par_hir_body_owners(|def_id| {
1114+
if !tcx.is_typeck_child(def_id.to_def_id()) {
1115+
// Child unsafety and borrowck happens together with the parent
1116+
tcx.ensure_ok().check_unsafety(def_id);
1117+
tcx.ensure_ok().mir_borrowck(def_id);
1118+
tcx.ensure_ok().check_transmutes(def_id);
1119+
}
1120+
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
1121+
1122+
// If we need to codegen, ensure that we emit all errors from
1123+
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
1124+
// them later during codegen.
1125+
if tcx.sess.opts.output_types.should_codegen()
1126+
|| tcx.hir_body_const_context(def_id).is_some()
1127+
{
1128+
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
1129+
}
1130+
1131+
if tcx.is_coroutine(def_id.to_def_id()) {
1132+
tcx.ensure_ok().mir_coroutine_witnesses(def_id);
1133+
let _ = tcx.ensure_ok().check_coroutine_obligations(
1134+
tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
1135+
);
1136+
if !tcx.is_async_drop_in_place_coroutine(def_id.to_def_id()) {
1137+
// Eagerly check the unsubstituted layout for cycles.
1138+
tcx.ensure_ok().layout_of(
1139+
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
1140+
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
1141+
);
1142+
}
1143+
}
1144+
});
1145+
});
1146+
},
1147+
{
1148+
sess.time("layout_testing", || layout_test::test_layout(tcx));
1149+
sess.time("abi_testing", || abi_test::test_abi(tcx));
11311150
}
1132-
});
1151+
)
11331152
});
11341153

1135-
sess.time("layout_testing", || layout_test::test_layout(tcx));
1136-
sess.time("abi_testing", || abi_test::test_abi(tcx));
1154+
}
11371155
}
11381156

11391157
/// Runs the type-checking, region checking and other miscellaneous analysis
@@ -1158,28 +1176,20 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
11581176
sess.time("misc_checking_3", || {
11591177
parallel!(
11601178
{
1161-
tcx.ensure_ok().effective_visibilities(());
1162-
1163-
parallel!(
1164-
{
1165-
tcx.par_hir_for_each_module(|module| {
1166-
tcx.ensure_ok().check_private_in_public(module)
1167-
})
1168-
},
1169-
{
1170-
tcx.par_hir_for_each_module(|module| {
1171-
tcx.ensure_ok().check_mod_deathness(module)
1172-
});
1173-
},
1174-
{
1175-
sess.time("lint_checking", || {
1176-
rustc_lint::check_crate(tcx);
1177-
});
1178-
},
1179-
{
1180-
tcx.ensure_ok().clashing_extern_declarations(());
1181-
}
1182-
);
1179+
tcx.par_hir_for_each_module(|module| {
1180+
tcx.ensure_ok().check_private_in_public(module)
1181+
})
1182+
},
1183+
{
1184+
tcx.par_hir_for_each_module(|module| tcx.ensure_ok().check_mod_deathness(module));
1185+
},
1186+
{
1187+
sess.time("lint_checking", || {
1188+
rustc_lint::check_crate(tcx);
1189+
});
1190+
},
1191+
{
1192+
tcx.ensure_ok().clashing_extern_declarations(());
11831193
},
11841194
{
11851195
sess.time("privacy_checking_modules", || {

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 23 additions & 7 deletions
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, Res};
1414
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1515
use rustc_hir::lints::DelayedLint;
@@ -99,47 +99,63 @@ impl ModuleItems {
9999
}
100100

101101
/// Closures and inline consts
102-
pub fn par_nested_bodies(
102+
pub fn try_par_nested_bodies(
103103
&self,
104104
f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
105105
) -> Result<(), ErrorGuaranteed> {
106106
try_par_for_each_in(&self.nested_bodies[..], |&&id| f(id))
107107
}
108108

109-
pub fn par_items(
109+
pub fn try_par_items(
110110
&self,
111111
f: impl Fn(ItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
112112
) -> Result<(), ErrorGuaranteed> {
113113
try_par_for_each_in(&self.free_items[..], |&&id| f(id))
114114
}
115115

116-
pub fn par_trait_items(
116+
pub fn try_par_trait_items(
117117
&self,
118118
f: impl Fn(TraitItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
119119
) -> Result<(), ErrorGuaranteed> {
120120
try_par_for_each_in(&self.trait_items[..], |&&id| f(id))
121121
}
122122

123-
pub fn par_impl_items(
123+
pub fn try_par_impl_items(
124124
&self,
125125
f: impl Fn(ImplItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
126126
) -> Result<(), ErrorGuaranteed> {
127127
try_par_for_each_in(&self.impl_items[..], |&&id| f(id))
128128
}
129129

130-
pub fn par_foreign_items(
130+
pub fn try_par_foreign_items(
131131
&self,
132132
f: impl Fn(ForeignItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
133133
) -> Result<(), ErrorGuaranteed> {
134134
try_par_for_each_in(&self.foreign_items[..], |&&id| f(id))
135135
}
136136

137-
pub fn par_opaques(
137+
pub fn try_par_opaques(
138138
&self,
139139
f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
140140
) -> Result<(), ErrorGuaranteed> {
141141
try_par_for_each_in(&self.opaques[..], |&&id| f(id))
142142
}
143+
144+
pub fn par_items(&self, f: impl Fn(ItemId) + DynSend + DynSync) {
145+
par_for_each_in(&self.free_items[..], |&&id| f(id))
146+
}
147+
148+
pub fn par_trait_items(&self, f: impl Fn(TraitItemId) + DynSend + DynSync) {
149+
par_for_each_in(&self.trait_items[..], |&&id| f(id))
150+
}
151+
152+
pub fn par_impl_items(&self, f: impl Fn(ImplItemId) + DynSend + DynSync) {
153+
par_for_each_in(&self.impl_items[..], |&&id| f(id))
154+
}
155+
156+
pub fn par_foreign_items(&self, f: impl Fn(ForeignItemId) + DynSend + DynSync) {
157+
par_for_each_in(&self.foreign_items[..], |&&id| f(id))
158+
}
143159
}
144160

145161
impl<'tcx> TyCtxt<'tcx> {

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ use std::cell::OnceCell;
211211
use std::ops::ControlFlow;
212212

213213
use rustc_data_structures::fx::FxIndexMap;
214-
use rustc_data_structures::sync::{MTLock, par_for_each_in};
214+
use rustc_data_structures::sync::{MTLock, join, par_for_each_in};
215215
use rustc_data_structures::unord::{UnordMap, UnordSet};
216216
use rustc_hir as hir;
217217
use rustc_hir::attrs::InlineAttr;
@@ -1806,9 +1806,20 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
18061806
) -> (Vec<MonoItem<'tcx>>, UsageMap<'tcx>) {
18071807
let _prof_timer = tcx.prof.generic_activity("monomorphization_collector");
18081808

1809-
let roots = tcx
1810-
.sess
1811-
.time("monomorphization_collector_root_collections", || collect_roots(tcx, strategy));
1809+
let (roots, _) = join(
1810+
|| {
1811+
tcx.sess.time("monomorphization_collector_root_collections", || {
1812+
collect_roots(tcx, strategy)
1813+
})
1814+
},
1815+
|| {
1816+
if tcx.sess.opts.share_generics() {
1817+
// Prefetch upstream_monomorphizations as it's very likely to be used in
1818+
// code generation later and this is decent spot to compute it.
1819+
tcx.ensure_ok().upstream_monomorphizations(());
1820+
}
1821+
},
1822+
);
18121823

18131824
debug!("building mono item graph, beginning at roots");
18141825

compiler/rustc_privacy/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,6 @@ fn check_private_in_public(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
18821882
let checker = PrivateItemsInPublicInterfacesChecker { tcx, effective_visibilities };
18831883

18841884
let crate_items = tcx.hir_module_items(module_def_id);
1885-
let _ = crate_items.par_items(|id| Ok(checker.check_item(id)));
1886-
let _ = crate_items.par_foreign_items(|id| Ok(checker.check_foreign_item(id)));
1885+
crate_items.par_items(|id| checker.check_item(id));
1886+
crate_items.par_foreign_items(|id| checker.check_foreign_item(id));
18871887
}

tests/ui/macros/macro-span-issue-116502.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | _
55
| ^ not allowed in type signatures
66
...
77
LL | struct S<T = m!()>(m!(), T)
8-
| ---- in this macro invocation
8+
| ---- in this macro invocation
99
|
1010
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

@@ -15,7 +15,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
1515
LL | _
1616
| ^ not allowed in type signatures
1717
...
18-
LL | T: Trait<m!()>;
18+
LL | struct S<T = m!()>(m!(), T)
1919
| ---- in this macro invocation
2020
|
2121
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -26,8 +26,8 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
2626
LL | _
2727
| ^ not allowed in type signatures
2828
...
29-
LL | struct S<T = m!()>(m!(), T)
30-
| ---- in this macro invocation
29+
LL | T: Trait<m!()>;
30+
| ---- in this macro invocation
3131
|
3232
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
3333

tests/ui/span/issue-35987.rs

Lines changed: 1 addition & 1 deletion
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 {

0 commit comments

Comments
 (0)