Skip to content

Commit a167142

Browse files
committed
Translate MIR to clif ir in parallel with parallel rustc
On dev-desktop the advantage of cg_clif over cg_llvm on simple-raytracer is 15% when parallel rustc is disabled. With -Zthreads=16 the advantage goes from 5% to 22% with this change.
1 parent 50b3427 commit a167142

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/concurrency_limiter.rs

Lines changed: 4 additions & 4 deletions
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,7 +73,7 @@ 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
}

src/driver/aot.rs

Lines changed: 28 additions & 28 deletions
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};
@@ -611,34 +612,33 @@ pub(crate) fn run_aot(
611612
CguReuse::PreLto | CguReuse::PostLto => false,
612613
});
613614

614-
let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, todo_cgus.len());
615-
616-
let modules =
617-
tcx.sess.time("codegen mono items", || {
618-
todo_cgus
619-
.into_iter()
620-
.map(|(_, cgu)| {
621-
let dep_node = cgu.codegen_dep_node(tcx);
622-
tcx.dep_graph
623-
.with_task(
624-
dep_node,
625-
tcx,
626-
(
627-
backend_config.clone(),
628-
global_asm_config.clone(),
629-
cgu.name(),
630-
concurrency_limiter.acquire(tcx.dcx()),
631-
),
632-
module_codegen,
633-
Some(rustc_middle::dep_graph::hash_result),
634-
)
635-
.0
636-
})
637-
.chain(done_cgus.into_iter().map(|(_, cgu)| {
638-
OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
639-
}))
640-
.collect::<Vec<_>>()
615+
let concurrency_limiter = IntoDynSyncSend(ConcurrencyLimiter::new(tcx.sess, todo_cgus.len()));
616+
617+
let modules = tcx.sess.time("codegen mono items", || {
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
641634
});
635+
modules.extend(
636+
done_cgus
637+
.into_iter()
638+
.map(|(_, cgu)| OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))),
639+
);
640+
modules
641+
});
642642

643643
let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string());
644644
let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
@@ -706,6 +706,6 @@ pub(crate) fn run_aot(
706706
metadata_module,
707707
metadata,
708708
crate_info: CrateInfo::new(tcx, target_cpu),
709-
concurrency_limiter,
709+
concurrency_limiter: concurrency_limiter.0,
710710
})
711711
}

0 commit comments

Comments
 (0)