Skip to content

Commit 1a2c489

Browse files
authored
Merge pull request rust-lang#1489 from rust-lang/parallel_rustc
Translate MIR to clif ir in parallel with parallel rustc
2 parents 893ba53 + a167142 commit 1a2c489

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

src/concurrency_limiter.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_session::Session;
66
// FIXME don't panic when a worker thread panics
77

88
pub(super) struct ConcurrencyLimiter {
9-
helper_thread: Option<HelperThread>,
9+
helper_thread: Option<Mutex<HelperThread>>,
1010
state: Arc<Mutex<state::ConcurrencyLimiterState>>,
1111
available_token_condvar: Arc<Condvar>,
1212
finished: bool,
@@ -39,14 +39,14 @@ impl ConcurrencyLimiter {
3939
})
4040
.unwrap();
4141
ConcurrencyLimiter {
42-
helper_thread: Some(helper_thread),
42+
helper_thread: Some(Mutex::new(helper_thread)),
4343
state,
4444
available_token_condvar,
4545
finished: false,
4646
}
4747
}
4848

49-
pub(super) fn acquire(&mut self, dcx: &rustc_errors::DiagCtxt) -> ConcurrencyLimiterToken {
49+
pub(super) fn acquire(&self, dcx: &rustc_errors::DiagCtxt) -> ConcurrencyLimiterToken {
5050
let mut state = self.state.lock().unwrap();
5151
loop {
5252
state.assert_invariants();
@@ -73,16 +73,11 @@ impl ConcurrencyLimiter {
7373
}
7474
}
7575

76-
self.helper_thread.as_mut().unwrap().request_token();
76+
self.helper_thread.as_ref().unwrap().lock().unwrap().request_token();
7777
state = self.available_token_condvar.wait(state).unwrap();
7878
}
7979
}
8080

81-
pub(super) fn job_already_done(&mut self) {
82-
let mut state = self.state.lock().unwrap();
83-
state.job_already_done();
84-
}
85-
8681
pub(crate) fn finished(mut self) {
8782
self.helper_thread.take();
8883

@@ -190,14 +185,6 @@ mod state {
190185
self.assert_invariants();
191186
}
192187

193-
pub(super) fn job_already_done(&mut self) {
194-
self.assert_invariants();
195-
self.pending_jobs -= 1;
196-
self.assert_invariants();
197-
self.drop_excess_capacity();
198-
self.assert_invariants();
199-
}
200-
201188
pub(super) fn poison(&mut self, error: String) {
202189
self.poisoned = true;
203190
self.stored_error = Some(error);

src/driver/aot.rs

+33-32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_codegen_ssa::errors as ssa_errors;
1515
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
1616
use rustc_data_structures::profiling::SelfProfilerRef;
1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
18+
use rustc_data_structures::sync::{par_map, IntoDynSyncSend};
1819
use rustc_metadata::fs::copy_to_stdout;
1920
use rustc_metadata::EncodedMetadata;
2021
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
@@ -604,39 +605,39 @@ pub(crate) fn run_aot(
604605

605606
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
606607

607-
let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len());
608+
let (todo_cgus, done_cgus) =
609+
cgus.into_iter().enumerate().partition::<Vec<_>, _>(|&(i, _)| match cgu_reuse[i] {
610+
_ if backend_config.disable_incr_cache => true,
611+
CguReuse::No => true,
612+
CguReuse::PreLto | CguReuse::PostLto => false,
613+
});
614+
615+
let concurrency_limiter = IntoDynSyncSend(ConcurrencyLimiter::new(tcx.sess, todo_cgus.len()));
608616

609617
let modules = tcx.sess.time("codegen mono items", || {
610-
cgus.iter()
611-
.enumerate()
612-
.map(|(i, cgu)| {
613-
let cgu_reuse =
614-
if backend_config.disable_incr_cache { CguReuse::No } else { cgu_reuse[i] };
615-
match cgu_reuse {
616-
CguReuse::No => {
617-
let dep_node = cgu.codegen_dep_node(tcx);
618-
tcx.dep_graph
619-
.with_task(
620-
dep_node,
621-
tcx,
622-
(
623-
backend_config.clone(),
624-
global_asm_config.clone(),
625-
cgu.name(),
626-
concurrency_limiter.acquire(tcx.dcx()),
627-
),
628-
module_codegen,
629-
Some(rustc_middle::dep_graph::hash_result),
630-
)
631-
.0
632-
}
633-
CguReuse::PreLto | CguReuse::PostLto => {
634-
concurrency_limiter.job_already_done();
635-
OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
636-
}
637-
}
638-
})
639-
.collect::<Vec<_>>()
618+
let mut modules: Vec<_> = par_map(todo_cgus, |(_, cgu)| {
619+
let dep_node = cgu.codegen_dep_node(tcx);
620+
tcx.dep_graph
621+
.with_task(
622+
dep_node,
623+
tcx,
624+
(
625+
backend_config.clone(),
626+
global_asm_config.clone(),
627+
cgu.name(),
628+
concurrency_limiter.acquire(tcx.dcx()),
629+
),
630+
module_codegen,
631+
Some(rustc_middle::dep_graph::hash_result),
632+
)
633+
.0
634+
});
635+
modules.extend(
636+
done_cgus
637+
.into_iter()
638+
.map(|(_, cgu)| OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))),
639+
);
640+
modules
640641
});
641642

642643
let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string());
@@ -705,6 +706,6 @@ pub(crate) fn run_aot(
705706
metadata_module,
706707
metadata,
707708
crate_info: CrateInfo::new(tcx, target_cpu),
708-
concurrency_limiter,
709+
concurrency_limiter: concurrency_limiter.0,
709710
})
710711
}

0 commit comments

Comments
 (0)