Skip to content

Commit 27e2709

Browse files
committed
Improve coordinator channel handling
Remove usage of Any, reduce visibility of fields and remove unused backend arguments.
1 parent b56aaec commit 27e2709

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::any::Any;
21
use std::assert_matches::assert_matches;
32
use std::marker::PhantomData;
43
use std::path::{Path, PathBuf};
@@ -372,8 +371,6 @@ pub struct CodegenContext<B: WriteBackendMethods> {
372371
/// The incremental compilation session directory, or None if we are not
373372
/// compiling incrementally
374373
pub incr_comp_session_dir: Option<PathBuf>,
375-
/// Channel back to the main control thread to send messages to
376-
pub coordinator_send: Sender<Box<dyn Any + Send>>,
377374
/// `true` if the codegen should be run in parallel.
378375
///
379376
/// Depends on [`ExtraBackendMethods::supports_parallel()`] and `-Zno_parallel_backend`.
@@ -1122,10 +1119,10 @@ fn start_executing_work<B: ExtraBackendMethods>(
11221119
autodiff_items: &[AutoDiffItem],
11231120
shared_emitter: SharedEmitter,
11241121
codegen_worker_send: Sender<CguMessage>,
1125-
coordinator_receive: Receiver<Box<dyn Any + Send>>,
1122+
coordinator_receive: Receiver<Message<B>>,
11261123
regular_config: Arc<ModuleConfig>,
11271124
allocator_config: Arc<ModuleConfig>,
1128-
tx_to_llvm_workers: Sender<Box<dyn Any + Send>>,
1125+
tx_to_llvm_workers: Sender<Message<B>>,
11291126
) -> thread::JoinHandle<Result<CompiledModules, ()>> {
11301127
let coordinator_send = tx_to_llvm_workers;
11311128
let sess = tcx.sess;
@@ -1153,7 +1150,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
11531150
let coordinator_send2 = coordinator_send.clone();
11541151
let helper = jobserver::client()
11551152
.into_helper_thread(move |token| {
1156-
drop(coordinator_send2.send(Box::new(Message::Token::<B>(token))));
1153+
drop(coordinator_send2.send(Message::Token::<B>(token)));
11571154
})
11581155
.expect("failed to spawn helper thread");
11591156

@@ -1187,7 +1184,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
11871184
remark: sess.opts.cg.remark.clone(),
11881185
remark_dir,
11891186
incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),
1190-
coordinator_send,
11911187
expanded_args: tcx.sess.expanded_args.clone(),
11921188
diag_emitter: shared_emitter.clone(),
11931189
output_filenames: Arc::clone(tcx.output_filenames(())),
@@ -1423,7 +1419,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
14231419
let (item, _) =
14241420
work_items.pop().expect("queue empty - queue_full_enough() broken?");
14251421
main_thread_state = MainThreadState::Lending;
1426-
spawn_work(&cgcx, &mut llvm_start_time, item);
1422+
spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
14271423
}
14281424
}
14291425
} else if codegen_state == Completed {
@@ -1502,7 +1498,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
15021498
MainThreadState::Idle => {
15031499
if let Some((item, _)) = work_items.pop() {
15041500
main_thread_state = MainThreadState::Lending;
1505-
spawn_work(&cgcx, &mut llvm_start_time, item);
1501+
spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
15061502
} else {
15071503
// There is no unstarted work, so let the main thread
15081504
// take over for a running worker. Otherwise the
@@ -1538,16 +1534,15 @@ fn start_executing_work<B: ExtraBackendMethods>(
15381534
while running_with_own_token < tokens.len()
15391535
&& let Some((item, _)) = work_items.pop()
15401536
{
1541-
spawn_work(&cgcx, &mut llvm_start_time, item);
1537+
spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
15421538
running_with_own_token += 1;
15431539
}
15441540
}
15451541

15461542
// Relinquish accidentally acquired extra tokens.
15471543
tokens.truncate(running_with_own_token);
15481544

1549-
let msg = coordinator_receive.recv().unwrap();
1550-
match *msg.downcast::<Message<B>>().ok().unwrap() {
1545+
match coordinator_receive.recv().unwrap() {
15511546
// Save the token locally and the next turn of the loop will use
15521547
// this to spawn a new unit of work, or it may get dropped
15531548
// immediately if we have no more work to spawn.
@@ -1769,6 +1764,7 @@ pub(crate) struct WorkerFatalError;
17691764

17701765
fn spawn_work<'a, B: ExtraBackendMethods>(
17711766
cgcx: &'a CodegenContext<B>,
1767+
coordinator_send: Sender<Message<B>>,
17721768
llvm_start_time: &mut Option<VerboseTimingGuard<'a>>,
17731769
work: WorkItem<B>,
17741770
) {
@@ -1782,7 +1778,7 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
17821778
// Set up a destructor which will fire off a message that we're done as
17831779
// we exit.
17841780
struct Bomb<B: ExtraBackendMethods> {
1785-
coordinator_send: Sender<Box<dyn Any + Send>>,
1781+
coordinator_send: Sender<Message<B>>,
17861782
result: Option<Result<WorkItemResult<B>, FatalError>>,
17871783
}
17881784
impl<B: ExtraBackendMethods> Drop for Bomb<B> {
@@ -1794,11 +1790,11 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
17941790
}
17951791
None => Message::WorkItem::<B> { result: Err(None) },
17961792
};
1797-
drop(self.coordinator_send.send(Box::new(msg)));
1793+
drop(self.coordinator_send.send(msg));
17981794
}
17991795
}
18001796

1801-
let mut bomb = Bomb::<B> { coordinator_send: cgcx.coordinator_send.clone(), result: None };
1797+
let mut bomb = Bomb::<B> { coordinator_send, result: None };
18021798

18031799
// Execute the work itself, and if it finishes successfully then flag
18041800
// ourselves as a success as well.
@@ -2003,7 +1999,7 @@ impl SharedEmitterMain {
20031999
}
20042000

20052001
pub struct Coordinator<B: ExtraBackendMethods> {
2006-
pub sender: Sender<Box<dyn Any + Send>>,
2002+
sender: Sender<Message<B>>,
20072003
future: Option<thread::JoinHandle<Result<CompiledModules, ()>>>,
20082004
// Only used for the Message type.
20092005
phantom: PhantomData<B>,
@@ -2020,7 +2016,7 @@ impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
20202016
if let Some(future) = self.future.take() {
20212017
// If we haven't joined yet, signal to the coordinator that it should spawn no more
20222018
// work, and wait for worker threads to finish.
2023-
drop(self.sender.send(Box::new(Message::CodegenAborted::<B>)));
2019+
drop(self.sender.send(Message::CodegenAborted::<B>));
20242020
drop(future.join());
20252021
}
20262022
}
@@ -2079,7 +2075,7 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
20792075
pub(crate) fn codegen_finished(&self, tcx: TyCtxt<'_>) {
20802076
self.wait_for_signal_to_codegen_item();
20812077
self.check_for_errors(tcx.sess);
2082-
drop(self.coordinator.sender.send(Box::new(Message::CodegenComplete::<B>)));
2078+
drop(self.coordinator.sender.send(Message::CodegenComplete::<B>));
20832079
}
20842080

20852081
pub(crate) fn check_for_errors(&self, sess: &Session) {
@@ -2100,28 +2096,25 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
21002096
}
21012097

21022098
pub(crate) fn submit_codegened_module_to_llvm<B: ExtraBackendMethods>(
2103-
_backend: &B,
2104-
tx_to_llvm_workers: &Sender<Box<dyn Any + Send>>,
2099+
coordinator: &Coordinator<B>,
21052100
module: ModuleCodegen<B::Module>,
21062101
cost: u64,
21072102
) {
21082103
let llvm_work_item = WorkItem::Optimize(module);
2109-
drop(tx_to_llvm_workers.send(Box::new(Message::CodegenDone::<B> { llvm_work_item, cost })));
2104+
drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost }));
21102105
}
21112106

21122107
pub(crate) fn submit_post_lto_module_to_llvm<B: ExtraBackendMethods>(
2113-
_backend: &B,
2114-
tx_to_llvm_workers: &Sender<Box<dyn Any + Send>>,
2108+
coordinator: &Coordinator<B>,
21152109
module: CachedModuleCodegen,
21162110
) {
21172111
let llvm_work_item = WorkItem::CopyPostLtoArtifacts(module);
2118-
drop(tx_to_llvm_workers.send(Box::new(Message::CodegenDone::<B> { llvm_work_item, cost: 0 })));
2112+
drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost: 0 }));
21192113
}
21202114

21212115
pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
2122-
_backend: &B,
21232116
tcx: TyCtxt<'_>,
2124-
tx_to_llvm_workers: &Sender<Box<dyn Any + Send>>,
2117+
coordinator: &Coordinator<B>,
21252118
module: CachedModuleCodegen,
21262119
) {
21272120
let filename = pre_lto_bitcode_filename(&module.name);
@@ -2135,10 +2128,10 @@ pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
21352128
})
21362129
};
21372130
// Schedule the module to be loaded
2138-
drop(tx_to_llvm_workers.send(Box::new(Message::AddImportOnlyModule::<B> {
2131+
drop(coordinator.sender.send(Message::AddImportOnlyModule::<B> {
21392132
module_data: SerializedModule::FromUncompressedFile(mmap),
21402133
work_product: module.source,
2141-
})));
2134+
}));
21422135
}
21432136

21442137
fn pre_lto_bitcode_filename(module_name: &str) -> String {

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
702702
// These modules are generally cheap and won't throw off scheduling.
703703
let cost = 0;
704704
submit_codegened_module_to_llvm(
705-
&backend,
706-
&ongoing_codegen.coordinator.sender,
705+
&ongoing_codegen.coordinator,
707706
ModuleCodegen::new_allocator(llmod_id, module_llvm),
708707
cost,
709708
);
@@ -800,18 +799,12 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
800799
// compilation hang on post-monomorphization errors.
801800
tcx.dcx().abort_if_errors();
802801

803-
submit_codegened_module_to_llvm(
804-
&backend,
805-
&ongoing_codegen.coordinator.sender,
806-
module,
807-
cost,
808-
);
802+
submit_codegened_module_to_llvm(&ongoing_codegen.coordinator, module, cost);
809803
}
810804
CguReuse::PreLto => {
811805
submit_pre_lto_module_to_llvm(
812-
&backend,
813806
tcx,
814-
&ongoing_codegen.coordinator.sender,
807+
&ongoing_codegen.coordinator,
815808
CachedModuleCodegen {
816809
name: cgu.name().to_string(),
817810
source: cgu.previous_work_product(tcx),
@@ -820,8 +813,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
820813
}
821814
CguReuse::PostLto => {
822815
submit_post_lto_module_to_llvm(
823-
&backend,
824-
&ongoing_codegen.coordinator.sender,
816+
&ongoing_codegen.coordinator,
825817
CachedModuleCodegen {
826818
name: cgu.name().to_string(),
827819
source: cgu.previous_work_product(tcx),

0 commit comments

Comments
 (0)