Skip to content

Commit 88cd8f9

Browse files
committed
Simplify Message.
`Message` is an enum with multiple variants. Four of those variants map directly onto the four variants of `WorkItemResult`. This commit reduces those four `Message` variants to a single variant containing a `WorkItemResult`. This requires increasing `WorkItemResult`'s visibility to `pub(crate)` visibility, but `WorkItem` and `Message` can also have their visibility reduced to `pub(crate)`. This change avoids some boilerplate enum translation code, and makes `Message` easier to understand.
1 parent 757c290 commit 88cd8f9

File tree

1 file changed

+44
-64
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+44
-64
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+44-64
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ fn produce_final_output_artifacts(
685685
// These are used in linking steps and will be cleaned up afterward.
686686
}
687687

688-
pub enum WorkItem<B: WriteBackendMethods> {
688+
pub(crate) enum WorkItem<B: WriteBackendMethods> {
689689
/// Optimize a newly codegened, totally unoptimized module.
690690
Optimize(ModuleCodegen<B::Module>),
691691
/// Copy the post-LTO artifacts from the incremental cache to the output
@@ -731,7 +731,7 @@ impl<B: WriteBackendMethods> WorkItem<B> {
731731
}
732732
}
733733

734-
enum WorkItemResult<B: WriteBackendMethods> {
734+
pub(crate) enum WorkItemResult<B: WriteBackendMethods> {
735735
Compiled(CompiledModule),
736736
NeedsLink(ModuleCodegen<B::Module>),
737737
NeedsFatLTO(FatLTOInput<B>),
@@ -923,23 +923,10 @@ fn finish_intra_module_work<B: ExtraBackendMethods>(
923923
}
924924
}
925925

926-
pub enum Message<B: WriteBackendMethods> {
926+
pub(crate) enum Message<B: WriteBackendMethods> {
927927
Token(io::Result<Acquired>),
928-
NeedsFatLTO {
929-
result: FatLTOInput<B>,
930-
worker_id: usize,
931-
},
932-
NeedsThinLTO {
933-
name: String,
934-
thin_buffer: B::ThinBuffer,
935-
worker_id: usize,
936-
},
937-
NeedsLink {
938-
module: ModuleCodegen<B::Module>,
939-
worker_id: usize,
940-
},
941-
Done {
942-
result: Result<CompiledModule, Option<WorkerFatalError>>,
928+
WorkItem {
929+
result: Result<WorkItemResult<B>, Option<WorkerFatalError>>,
943930
worker_id: usize,
944931
},
945932
CodegenDone {
@@ -1481,50 +1468,54 @@ fn start_executing_work<B: ExtraBackendMethods>(
14811468
codegen_done = true;
14821469
codegen_aborted = true;
14831470
}
1484-
Message::Done { result: Ok(compiled_module), worker_id } => {
1471+
1472+
Message::WorkItem { result, worker_id } => {
14851473
free_worker(worker_id);
1486-
match compiled_module.kind {
1487-
ModuleKind::Regular => {
1488-
compiled_modules.push(compiled_module);
1474+
1475+
match result {
1476+
Ok(WorkItemResult::Compiled(compiled_module)) => {
1477+
match compiled_module.kind {
1478+
ModuleKind::Regular => {
1479+
compiled_modules.push(compiled_module);
1480+
}
1481+
ModuleKind::Allocator => {
1482+
assert!(compiled_allocator_module.is_none());
1483+
compiled_allocator_module = Some(compiled_module);
1484+
}
1485+
ModuleKind::Metadata => bug!("Should be handled separately"),
1486+
}
1487+
}
1488+
Ok(WorkItemResult::NeedsLink(module)) => {
1489+
needs_link.push(module);
1490+
}
1491+
Ok(WorkItemResult::NeedsFatLTO(fat_lto_input)) => {
1492+
assert!(!started_lto);
1493+
needs_fat_lto.push(fat_lto_input);
1494+
}
1495+
Ok(WorkItemResult::NeedsThinLTO(name, thin_buffer)) => {
1496+
assert!(!started_lto);
1497+
needs_thin_lto.push((name, thin_buffer));
14891498
}
1490-
ModuleKind::Allocator => {
1491-
assert!(compiled_allocator_module.is_none());
1492-
compiled_allocator_module = Some(compiled_module);
1499+
Err(Some(WorkerFatalError)) => {
1500+
// Like `CodegenAborted`, wait for remaining work to finish.
1501+
codegen_done = true;
1502+
codegen_aborted = true;
1503+
}
1504+
Err(None) => {
1505+
// If the thread failed that means it panicked, so
1506+
// we abort immediately.
1507+
bug!("worker thread panicked");
14931508
}
1494-
ModuleKind::Metadata => bug!("Should be handled separately"),
14951509
}
14961510
}
1497-
Message::NeedsLink { module, worker_id } => {
1498-
free_worker(worker_id);
1499-
needs_link.push(module);
1500-
}
1501-
Message::NeedsFatLTO { result, worker_id } => {
1502-
assert!(!started_lto);
1503-
free_worker(worker_id);
1504-
needs_fat_lto.push(result);
1505-
}
1506-
Message::NeedsThinLTO { name, thin_buffer, worker_id } => {
1507-
assert!(!started_lto);
1508-
free_worker(worker_id);
1509-
needs_thin_lto.push((name, thin_buffer));
1510-
}
1511+
15111512
Message::AddImportOnlyModule { module_data, work_product } => {
15121513
assert!(!started_lto);
15131514
assert!(!codegen_done);
15141515
assert_eq!(main_thread_worker_state, MainThreadWorkerState::Codegenning);
15151516
lto_import_only_modules.push((module_data, work_product));
15161517
main_thread_worker_state = MainThreadWorkerState::Idle;
15171518
}
1518-
// If the thread failed that means it panicked, so we abort immediately.
1519-
Message::Done { result: Err(None), worker_id: _ } => {
1520-
bug!("worker thread panicked");
1521-
}
1522-
Message::Done { result: Err(Some(WorkerFatalError)), worker_id } => {
1523-
// Similar to CodegenAborted, wait for remaining work to finish.
1524-
free_worker(worker_id);
1525-
codegen_done = true;
1526-
codegen_aborted = true;
1527-
}
15281519
}
15291520
}
15301521

@@ -1643,22 +1634,11 @@ fn spawn_work<B: ExtraBackendMethods>(cgcx: CodegenContext<B>, work: WorkItem<B>
16431634
fn drop(&mut self) {
16441635
let worker_id = self.worker_id;
16451636
let msg = match self.result.take() {
1646-
Some(Ok(WorkItemResult::Compiled(m))) => {
1647-
Message::Done::<B> { result: Ok(m), worker_id }
1648-
}
1649-
Some(Ok(WorkItemResult::NeedsLink(m))) => {
1650-
Message::NeedsLink::<B> { module: m, worker_id }
1651-
}
1652-
Some(Ok(WorkItemResult::NeedsFatLTO(m))) => {
1653-
Message::NeedsFatLTO::<B> { result: m, worker_id }
1654-
}
1655-
Some(Ok(WorkItemResult::NeedsThinLTO(name, thin_buffer))) => {
1656-
Message::NeedsThinLTO::<B> { name, thin_buffer, worker_id }
1657-
}
1637+
Some(Ok(result)) => Message::WorkItem::<B> { result: Ok(result), worker_id },
16581638
Some(Err(FatalError)) => {
1659-
Message::Done::<B> { result: Err(Some(WorkerFatalError)), worker_id }
1639+
Message::WorkItem::<B> { result: Err(Some(WorkerFatalError)), worker_id }
16601640
}
1661-
None => Message::Done::<B> { result: Err(None), worker_id },
1641+
None => Message::WorkItem::<B> { result: Err(None), worker_id },
16621642
};
16631643
drop(self.coordinator_send.send(Box::new(msg)));
16641644
}

0 commit comments

Comments
 (0)