Skip to content

Commit e23101d

Browse files
committed
Adapt to chili
1 parent 9bfa31f commit e23101d

File tree

11 files changed

+129
-120
lines changed

11 files changed

+129
-120
lines changed

Cargo.lock

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ version = "0.2.1"
450450
source = "registry+https://github.com/rust-lang/crates.io-index"
451451
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
452452

453+
[[package]]
454+
name = "chili"
455+
version = "0.2.1"
456+
source = "git+https://github.com/zetanumbers/chili.git?branch=rustc#7358b3027e7c69e884a97e13f5f95826dc14238d"
457+
453458
[[package]]
454459
name = "chrono"
455460
version = "0.4.40"
@@ -3186,16 +3191,6 @@ dependencies = [
31863191
"tikv-jemalloc-sys",
31873192
]
31883193

3189-
[[package]]
3190-
name = "rustc-rayon-core"
3191-
version = "0.5.1"
3192-
source = "registry+https://github.com/rust-lang/crates.io-index"
3193-
checksum = "2f42932dcd3bcbe484b38a3ccf79b7906fac41c02d408b5b1bac26da3416efdb"
3194-
dependencies = [
3195-
"crossbeam-deque",
3196-
"crossbeam-utils",
3197-
]
3198-
31993194
[[package]]
32003195
name = "rustc-stable-hash"
32013196
version = "0.1.2"
@@ -3543,6 +3538,7 @@ version = "0.0.0"
35433538
dependencies = [
35443539
"arrayvec",
35453540
"bitflags",
3541+
"chili",
35463542
"either",
35473543
"elsa",
35483544
"ena",
@@ -3555,7 +3551,6 @@ dependencies = [
35553551
"parking_lot",
35563552
"portable-atomic",
35573553
"rustc-hash 2.1.1",
3558-
"rustc-rayon-core",
35593554
"rustc-stable-hash",
35603555
"rustc_arena",
35613556
"rustc_graphviz",
@@ -3901,7 +3896,7 @@ dependencies = [
39013896
name = "rustc_interface"
39023897
version = "0.0.0"
39033898
dependencies = [
3904-
"rustc-rayon-core",
3899+
"chili",
39053900
"rustc_abi",
39063901
"rustc_ast",
39073902
"rustc_ast_lowering",
@@ -4061,10 +4056,10 @@ name = "rustc_middle"
40614056
version = "0.0.0"
40624057
dependencies = [
40634058
"bitflags",
4059+
"chili",
40644060
"either",
40654061
"gsgdt",
40664062
"polonius-engine",
4067-
"rustc-rayon-core",
40684063
"rustc_abi",
40694064
"rustc_apfloat",
40704065
"rustc_arena",
@@ -4326,9 +4321,9 @@ dependencies = [
43264321
name = "rustc_query_system"
43274322
version = "0.0.0"
43284323
dependencies = [
4324+
"chili",
43294325
"hashbrown",
43304326
"parking_lot",
4331-
"rustc-rayon-core",
43324327
"rustc_abi",
43334328
"rustc_ast",
43344329
"rustc_attr_data_structures",

compiler/rustc_data_structures/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ indexmap = "2.4.0"
1414
jobserver_crate = { version = "0.1.28", package = "jobserver" }
1515
measureme = "12.0.1"
1616
rustc-hash = "2.0.0"
17-
rustc-rayon-core = { version = "0.5.0" }
17+
chili = { git = "https://github.com/zetanumbers/chili.git", branch = "rustc" }
1818
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
1919
rustc_arena = { path = "../rustc_arena" }
2020
rustc_graphviz = { path = "../rustc_graphviz" }

compiler/rustc_data_structures/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub use self::freeze::{FreezeLock, FreezeReadGuard, FreezeWriteGuard};
4343
pub use self::lock::{Lock, LockGuard, Mode};
4444
pub use self::mode::{is_dyn_thread_safe, set_dyn_thread_safe_mode};
4545
pub use self::parallel::{
46-
join, par_for_each_in, par_map, parallel_guard, scope, try_par_for_each_in,
46+
join, join4, par_for_each_in, par_map, parallel_guard, try_par_for_each_in,
4747
};
4848
pub use self::vec::{AppendOnlyIndexVec, AppendOnlyVec};
4949
pub use self::worker_local::{Registry, WorkerLocal};

compiler/rustc_data_structures/src/sync/parallel.rs

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -58,49 +58,57 @@ where
5858
(a.unwrap(), b.unwrap())
5959
}
6060

61-
/// Runs a list of blocks in parallel. The first block is executed immediately on
62-
/// the current thread. Use that for the longest running block.
63-
#[macro_export]
64-
macro_rules! parallel {
65-
(impl $fblock:block [$($c:expr,)*] [$block:expr $(, $rest:expr)*]) => {
66-
parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
67-
};
68-
(impl $fblock:block [$($blocks:expr,)*] []) => {
69-
$crate::sync::parallel_guard(|guard| {
70-
$crate::sync::scope(|s| {
71-
$(
72-
let block = $crate::sync::FromDyn::from(|| $blocks);
73-
s.spawn(move |_| {
74-
guard.run(move || block.into_inner()());
75-
});
76-
)*
77-
guard.run(|| $fblock);
78-
});
79-
});
80-
};
81-
($fblock:block, $($blocks:block),*) => {
82-
if $crate::sync::is_dyn_thread_safe() {
83-
// Reverse the order of the later blocks since Rayon executes them in reverse order
84-
// when using a single thread. This ensures the execution order matches that
85-
// of a single threaded rustc.
86-
parallel!(impl $fblock [] [$($blocks),*]);
87-
} else {
88-
$crate::sync::parallel_guard(|guard| {
89-
guard.run(|| $fblock);
90-
$(guard.run(|| $blocks);)*
91-
});
92-
}
93-
};
94-
}
95-
96-
// This function only works when `mode::is_dyn_thread_safe()`.
97-
pub fn scope<'scope, OP, R>(op: OP) -> R
61+
pub fn join4<F0, F1, F2, F3, R0, R1, R2, R3>(
62+
oper0: F0,
63+
oper1: F1,
64+
oper2: F2,
65+
oper3: F3,
66+
) -> (R0, R1, R2, R3)
9867
where
99-
OP: FnOnce(&rayon_core::Scope<'scope>) -> R + DynSend,
100-
R: DynSend,
68+
F0: FnOnce() -> R0 + DynSend,
69+
F1: FnOnce() -> R1 + DynSend,
70+
F2: FnOnce() -> R2 + DynSend,
71+
F3: FnOnce() -> R3 + DynSend,
72+
R0: DynSend,
73+
R1: DynSend,
74+
R2: DynSend,
75+
R3: DynSend,
10176
{
102-
let op = FromDyn::from(op);
103-
rayon_core::scope(|s| FromDyn::from(op.into_inner()(s))).into_inner()
77+
if mode::is_dyn_thread_safe() {
78+
let oper0 = FromDyn::from(oper0);
79+
let oper1 = FromDyn::from(oper1);
80+
let oper2 = FromDyn::from(oper2);
81+
let oper3 = FromDyn::from(oper3);
82+
// Swap closures around because Chili executes second one on the current thread
83+
let (r1, (r2, (r3, r0))) = parallel_guard(|guard| {
84+
let mut scope = chili::Scope::global();
85+
scope.join_with_heartbeat_every::<1, _, _, _, _>(
86+
move |_| guard.run(move || FromDyn::from(oper1.into_inner()())),
87+
move |scope| {
88+
scope.join_with_heartbeat_every::<1, _, _, _, _>(
89+
move |_| guard.run(move || FromDyn::from(oper2.into_inner()())),
90+
move |scope| {
91+
scope.join_with_heartbeat_every::<1, _, _, _, _>(
92+
move |_| guard.run(move || FromDyn::from(oper3.into_inner()())),
93+
move |_| guard.run(move || FromDyn::from(oper0.into_inner()())),
94+
)
95+
},
96+
)
97+
},
98+
)
99+
});
100+
(
101+
r0.unwrap().into_inner(),
102+
r1.unwrap().into_inner(),
103+
r2.unwrap().into_inner(),
104+
r3.unwrap().into_inner(),
105+
)
106+
} else {
107+
let (r0, r1, r2, r3) = parallel_guard(|guard| {
108+
(guard.run(oper0), guard.run(oper1), guard.run(oper2), guard.run(oper3))
109+
});
110+
(r0.unwrap(), r1.unwrap(), r2.unwrap(), r3.unwrap())
111+
}
104112
}
105113

106114
#[inline]
@@ -112,10 +120,11 @@ where
112120
if mode::is_dyn_thread_safe() {
113121
let oper_a = FromDyn::from(oper_a);
114122
let oper_b = FromDyn::from(oper_b);
115-
let (a, b) = parallel_guard(|guard| {
116-
rayon_core::join(
117-
move || guard.run(move || FromDyn::from(oper_a.into_inner()())),
118-
move || guard.run(move || FromDyn::from(oper_b.into_inner()())),
123+
let (b, a) = parallel_guard(|guard| {
124+
chili::Scope::global().join_with_heartbeat_every::<1, _, _, _, _>(
125+
// Swap arguments around because Chili executes second one on the current thread
126+
move |_| guard.run(move || FromDyn::from(oper_b.into_inner()())),
127+
move |_| guard.run(move || FromDyn::from(oper_a.into_inner()())),
119128
)
120129
});
121130
(a.unwrap().into_inner(), b.unwrap().into_inner())
@@ -136,6 +145,7 @@ fn par_slice<I: DynSend>(
136145
}
137146

138147
fn par_rec<I: DynSend, F: Fn(&mut I) + DynSync + DynSend>(
148+
scope: &mut chili::Scope<'_>,
139149
items: &mut [I],
140150
state: &State<'_, F>,
141151
) {
@@ -147,16 +157,21 @@ fn par_slice<I: DynSend>(
147157
let (left, right) = items.split_at_mut(items.len() / 2);
148158
let mut left = state.for_each.derive(left);
149159
let mut right = state.for_each.derive(right);
150-
rayon_core::join(move || par_rec(*left, state), move || par_rec(*right, state));
160+
scope.join(
161+
// Swap arguments around because Chili executes second one on the current thread
162+
move |scope| par_rec(scope, *right, state),
163+
move |scope| par_rec(scope, *left, state),
164+
);
151165
}
152166
}
153167

168+
let mut scope = chili::Scope::global();
154169
let state = State {
155170
for_each: FromDyn::from(for_each),
156171
guard,
157172
group: std::cmp::max(items.len() / 128, 1),
158173
};
159-
par_rec(items, &state)
174+
par_rec(&mut scope, items, &state)
160175
}
161176

162177
pub fn par_for_each_in<I: DynSend, T: IntoIterator<Item = I>>(

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8-
rustc-rayon-core = { version = "0.5.0" }
8+
chili = { git = "https://github.com/zetanumbers/chili.git", branch = "rustc" }
99
rustc_ast = { path = "../rustc_ast" }
1010
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
1111
rustc_ast_passes = { path = "../rustc_ast_passes" }

compiler/rustc_interface/src/passes.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use std::{env, fs, iter};
77

88
use rustc_ast as ast;
99
use rustc_codegen_ssa::traits::CodegenBackend;
10-
use rustc_data_structures::parallel;
1110
use rustc_data_structures::steal::Steal;
12-
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal};
11+
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal, join, join4};
1312
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
1413
use rustc_feature::Features;
1514
use rustc_fs_util::try_canonicalize;
@@ -902,8 +901,8 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
902901
rustc_passes::hir_id_validator::check_crate(tcx);
903902
let sess = tcx.sess;
904903
sess.time("misc_checking_1", || {
905-
parallel!(
906-
{
904+
join4(
905+
|| {
907906
sess.time("looking_for_entry_point", || tcx.ensure_ok().entry_fn(()));
908907

909908
sess.time("looking_for_derive_registrar", || {
@@ -912,27 +911,27 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
912911

913912
CStore::from_tcx(tcx).report_unused_deps(tcx);
914913
},
915-
{
914+
|| {
916915
tcx.par_hir_for_each_module(|module| {
917916
tcx.ensure_ok().check_mod_loops(module);
918917
tcx.ensure_ok().check_mod_attrs(module);
919918
tcx.ensure_ok().check_mod_naked_functions(module);
920919
tcx.ensure_ok().check_mod_unstable_api_usage(module);
921920
});
922921
},
923-
{
922+
|| {
924923
sess.time("unused_lib_feature_checking", || {
925924
rustc_passes::stability::check_unused_or_stable_features(tcx)
926925
});
927926
},
928-
{
927+
|| {
929928
// We force these queries to run,
930929
// since they might not otherwise get called.
931930
// This marks the corresponding crate-level attributes
932931
// as used, and ensures that their values are valid.
933932
tcx.ensure_ok().limits(());
934933
tcx.ensure_ok().stability_index(());
935-
}
934+
},
936935
);
937936
});
938937

@@ -1027,36 +1026,36 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
10271026
}
10281027

10291028
sess.time("misc_checking_3", || {
1030-
parallel!(
1031-
{
1029+
join(
1030+
|| {
10321031
tcx.ensure_ok().effective_visibilities(());
10331032

1034-
parallel!(
1035-
{
1033+
join4(
1034+
|| {
10361035
tcx.ensure_ok().check_private_in_public(());
10371036
},
1038-
{
1037+
|| {
10391038
tcx.par_hir_for_each_module(|module| {
10401039
tcx.ensure_ok().check_mod_deathness(module)
10411040
});
10421041
},
1043-
{
1042+
|| {
10441043
sess.time("lint_checking", || {
10451044
rustc_lint::check_crate(tcx);
10461045
});
10471046
},
1048-
{
1047+
|| {
10491048
tcx.ensure_ok().clashing_extern_declarations(());
1050-
}
1049+
},
10511050
);
10521051
},
1053-
{
1052+
|| {
10541053
sess.time("privacy_checking_modules", || {
10551054
tcx.par_hir_for_each_module(|module| {
10561055
tcx.ensure_ok().check_mod_privacy(module);
10571056
});
10581057
});
1059-
}
1058+
},
10601059
);
10611060

10621061
// This check has to be run after all lints are done processing. We don't

0 commit comments

Comments
 (0)