Skip to content

Commit 3516544

Browse files
committed
Increase parallelism in various locations
1 parent caccb4d commit 3516544

File tree

11 files changed

+206
-152
lines changed

11 files changed

+206
-152
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

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

22442244
pub(super) fn check_type_wf(tcx: TyCtxt<'_>, (): ()) -> Result<(), ErrorGuaranteed> {
22452245
let items = tcx.hir_crate_items(());
2246-
let res = items
2247-
.par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2248-
.and(items.par_impl_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2249-
.and(items.par_trait_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)))
2250-
.and(
2251-
items.par_foreign_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id)),
2252-
)
2253-
.and(items.par_nested_bodies(|item| tcx.ensure_ok().check_well_formed(item)))
2254-
.and(items.par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
2246+
let res =
2247+
items
2248+
.try_par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
2249+
.and(
2250+
items.try_par_impl_items(|item| {
2251+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2252+
}),
2253+
)
2254+
.and(items.try_par_trait_items(|item| {
2255+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2256+
}))
2257+
.and(items.try_par_foreign_items(|item| {
2258+
tcx.ensure_ok().check_well_formed(item.owner_id.def_id)
2259+
}))
2260+
.and(items.try_par_nested_bodies(|item| tcx.ensure_ok().check_well_formed(item)))
2261+
.and(items.try_par_opaques(|item| tcx.ensure_ok().check_well_formed(item)));
22552262
super::entry::check_for_entry_fn(tcx);
22562263

22572264
res

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ mod variance;
9191

9292
pub use errors::NoVariantNamed;
9393
use rustc_abi::{CVariadicStatus, ExternAbi};
94+
use rustc_data_structures::sync::par_for_each_in;
9495
use rustc_hir::def::DefKind;
9596
use rustc_hir::lints::DelayedLint;
9697
use rustc_hir::{self as hir};
@@ -167,16 +168,20 @@ fn emit_delayed_lint(lint: &DelayedLint, tcx: TyCtxt<'_>) {
167168
pub fn check_crate(tcx: TyCtxt<'_>) {
168169
let _prof_timer = tcx.sess.timer("type_check_crate");
169170

171+
// Run dependencies of type checking before entering the loops below
172+
tcx.ensure_done().inferred_outlives_crate(());
173+
170174
tcx.sess.time("coherence_checking", || {
171175
// When discarding query call results, use an explicit type to indicate
172176
// what we are intending to discard, to help future type-based refactoring.
173177
type R = Result<(), ErrorGuaranteed>;
174178

175179
let _: R = tcx.ensure_ok().check_type_wf(());
176180

177-
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
178-
let _: R = tcx.ensure_ok().coherent_trait(trait_def_id);
179-
}
181+
par_for_each_in(tcx.all_local_trait_impls(()), |(trait_def_id, _)| {
182+
let _: R = tcx.ensure_ok().coherent_trait(*trait_def_id);
183+
});
184+
180185
// these queries are executed for side-effects (error reporting):
181186
let _: R = tcx.ensure_ok().crate_inherent_impls_validity_check(());
182187
let _: R = tcx.ensure_ok().crate_inherent_impls_overlap_check(());

compiler/rustc_interface/src/passes.rs

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,19 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10631063
{
10641064
tcx.ensure_ok().exportable_items(LOCAL_CRATE);
10651065
tcx.ensure_ok().stable_order_of_exportable_impls(LOCAL_CRATE);
1066+
1067+
// Prefetch this as it is used later by the loop below
1068+
// to prevent multiple threads from blocking on it.
1069+
tcx.ensure_done().get_lang_items(());
1070+
1071+
let _timer = tcx.sess.timer("misc_module_passes");
10661072
tcx.par_hir_for_each_module(|module| {
10671073
tcx.ensure_ok().check_mod_attrs(module);
1074+
});
1075+
},
1076+
{
1077+
let _timer = tcx.sess.timer("check_unstable_api_usage");
1078+
tcx.par_hir_for_each_module(|module| {
10681079
tcx.ensure_ok().check_mod_unstable_api_usage(module);
10691080
});
10701081
},
@@ -1086,43 +1097,55 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10861097
// This improves performance by allowing lock-free access to them.
10871098
tcx.untracked().definitions.freeze();
10881099

1089-
sess.time("MIR_borrow_checking", || {
1090-
tcx.par_hir_body_owners(|def_id| {
1091-
if !tcx.is_typeck_child(def_id.to_def_id()) {
1092-
// Child unsafety and borrowck happens together with the parent
1093-
tcx.ensure_ok().check_unsafety(def_id);
1094-
tcx.ensure_ok().mir_borrowck(def_id);
1095-
tcx.ensure_ok().check_transmutes(def_id);
1096-
}
1097-
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
1100+
sess.time("misc_checking_2", || {
1101+
parallel!(
1102+
{
1103+
// Prefetch this as it is used later by lint checking and privacy checking.
1104+
tcx.ensure_done().effective_visibilities(());
1105+
},
1106+
{
1107+
sess.time("misc_body_checking", || {
1108+
tcx.par_hir_body_owners(|def_id| {
1109+
if !tcx.is_typeck_child(def_id.to_def_id()) {
1110+
// Child unsafety and borrowck happens together with the parent
1111+
tcx.ensure_ok().check_unsafety(def_id);
1112+
tcx.ensure_ok().mir_borrowck(def_id);
1113+
tcx.ensure_ok().check_transmutes(def_id);
1114+
}
1115+
tcx.ensure_ok().has_ffi_unwind_calls(def_id);
1116+
1117+
// If we need to codegen, ensure that we emit all errors from
1118+
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
1119+
// them later during codegen.
1120+
if tcx.sess.opts.output_types.should_codegen()
1121+
|| tcx.hir_body_const_context(def_id).is_some()
1122+
{
1123+
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
1124+
}
10981125

1099-
// If we need to codegen, ensure that we emit all errors from
1100-
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
1101-
// them later during codegen.
1102-
if tcx.sess.opts.output_types.should_codegen()
1103-
|| tcx.hir_body_const_context(def_id).is_some()
1126+
if tcx.is_coroutine(def_id.to_def_id()) {
1127+
tcx.ensure_ok().mir_coroutine_witnesses(def_id);
1128+
let _ = tcx.ensure_ok().check_coroutine_obligations(
1129+
tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
1130+
);
1131+
if !tcx.is_async_drop_in_place_coroutine(def_id.to_def_id()) {
1132+
// Eagerly check the unsubstituted layout for cycles.
1133+
tcx.ensure_ok().layout_of(
1134+
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
1135+
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
1136+
);
1137+
}
1138+
}
1139+
});
1140+
});
1141+
},
11041142
{
1105-
tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
1143+
sess.time("layout_testing", || layout_test::test_layout(tcx));
1144+
sess.time("abi_testing", || abi_test::test_abi(tcx));
11061145
}
1107-
if tcx.is_coroutine(def_id.to_def_id()) {
1108-
tcx.ensure_ok().mir_coroutine_witnesses(def_id);
1109-
let _ = tcx.ensure_ok().check_coroutine_obligations(
1110-
tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
1111-
);
1112-
if !tcx.is_async_drop_in_place_coroutine(def_id.to_def_id()) {
1113-
// Eagerly check the unsubstituted layout for cycles.
1114-
tcx.ensure_ok().layout_of(
1115-
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
1116-
.as_query_input(tcx.type_of(def_id).instantiate_identity()),
1117-
);
1118-
}
1119-
}
1120-
});
1146+
)
11211147
});
11221148

1123-
sess.time("layout_testing", || layout_test::test_layout(tcx));
1124-
sess.time("abi_testing", || abi_test::test_abi(tcx));
1125-
11261149
// If `-Zvalidate-mir` is set, we also want to compute the final MIR for each item
11271150
// (either its `mir_for_ctfe` or `optimized_mir`) since that helps uncover any bugs
11281151
// in MIR optimizations that may only be reachable through codegen, or other codepaths
@@ -1158,28 +1181,20 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
11581181
sess.time("misc_checking_3", || {
11591182
parallel!(
11601183
{
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-
);
1184+
tcx.par_hir_for_each_module(|module| {
1185+
tcx.ensure_ok().check_private_in_public(module)
1186+
})
1187+
},
1188+
{
1189+
tcx.par_hir_for_each_module(|module| tcx.ensure_ok().check_mod_deathness(module));
1190+
},
1191+
{
1192+
sess.time("lint_checking", || {
1193+
rustc_lint::check_crate(tcx);
1194+
});
1195+
},
1196+
{
1197+
tcx.ensure_ok().clashing_extern_declarations(());
11831198
},
11841199
{
11851200
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;
1414
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1515
use rustc_hir::lints::DelayedLint;
@@ -92,47 +92,63 @@ impl ModuleItems {
9292
}
9393

9494
/// Closures and inline consts
95-
pub fn par_nested_bodies(
95+
pub fn try_par_nested_bodies(
9696
&self,
9797
f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
9898
) -> Result<(), ErrorGuaranteed> {
9999
try_par_for_each_in(&self.nested_bodies[..], |&&id| f(id))
100100
}
101101

102-
pub fn par_items(
102+
pub fn try_par_items(
103103
&self,
104104
f: impl Fn(ItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
105105
) -> Result<(), ErrorGuaranteed> {
106106
try_par_for_each_in(&self.free_items[..], |&&id| f(id))
107107
}
108108

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

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

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

130-
pub fn par_opaques(
130+
pub fn try_par_opaques(
131131
&self,
132132
f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
133133
) -> Result<(), ErrorGuaranteed> {
134134
try_par_for_each_in(&self.opaques[..], |&&id| f(id))
135135
}
136+
137+
pub fn par_items(&self, f: impl Fn(ItemId) + DynSend + DynSync) {
138+
par_for_each_in(&self.free_items[..], |&&id| f(id))
139+
}
140+
141+
pub fn par_trait_items(&self, f: impl Fn(TraitItemId) + DynSend + DynSync) {
142+
par_for_each_in(&self.trait_items[..], |&&id| f(id))
143+
}
144+
145+
pub fn par_impl_items(&self, f: impl Fn(ImplItemId) + DynSend + DynSync) {
146+
par_for_each_in(&self.impl_items[..], |&&id| f(id))
147+
}
148+
149+
pub fn par_foreign_items(&self, f: impl Fn(ForeignItemId) + DynSend + DynSync) {
150+
par_for_each_in(&self.foreign_items[..], |&&id| f(id))
151+
}
136152
}
137153

138154
impl<'tcx> TyCtxt<'tcx> {

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ mod autodiff;
210210
use std::cell::OnceCell;
211211

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::attrs::InlineAttr;
@@ -1739,9 +1739,20 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
17391739
) -> (Vec<MonoItem<'tcx>>, UsageMap<'tcx>) {
17401740
let _prof_timer = tcx.prof.generic_activity("monomorphization_collector");
17411741

1742-
let roots = tcx
1743-
.sess
1744-
.time("monomorphization_collector_root_collections", || collect_roots(tcx, strategy));
1742+
let (roots, _) = join(
1743+
|| {
1744+
tcx.sess.time("monomorphization_collector_root_collections", || {
1745+
collect_roots(tcx, strategy)
1746+
})
1747+
},
1748+
|| {
1749+
if tcx.sess.opts.share_generics() {
1750+
// Prefetch upstream_monomorphizations as it's very likely to be used in
1751+
// code generation later and this is decent spot to compute it.
1752+
tcx.ensure_ok().upstream_monomorphizations(());
1753+
}
1754+
},
1755+
);
17451756

17461757
debug!("building mono item graph, beginning at roots");
17471758

compiler/rustc_privacy/src/lib.rs

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

18831883
let crate_items = tcx.hir_module_items(module_def_id);
1884-
let _ = crate_items.par_items(|id| Ok(checker.check_item(id)));
1885-
let _ = crate_items.par_foreign_items(|id| Ok(checker.check_foreign_item(id)));
1884+
crate_items.par_items(|id| checker.check_item(id));
1885+
crate_items.par_foreign_items(|id| checker.check_foreign_item(id));
18861886
}

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,6 @@ LL | #![link(name = "x")]
281281
|
282282
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
283283

284-
warning: the feature `rust1` has been stable since 1.0.0 and no longer requires an attribute to enable
285-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:100:12
286-
|
287-
LL | #![feature(rust1)]
288-
| ^^^^^
289-
|
290-
= note: `#[warn(stable_features)]` on by default
291-
292284
warning: crate-level attribute should be in the root module
293285
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:505:17
294286
|
@@ -637,6 +629,14 @@ help: add a `!`
637629
LL | #![no_builtins] impl S { }
638630
| +
639631

632+
warning: the feature `rust1` has been stable since 1.0.0 and no longer requires an attribute to enable
633+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:100:12
634+
|
635+
LL | #![feature(rust1)]
636+
| ^^^^^
637+
|
638+
= note: `#[warn(stable_features)]` on by default
639+
640640
warning: `#[macro_use]` attribute cannot be used on functions
641641
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:191:5
642642
|

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

0 commit comments

Comments
 (0)