Skip to content

Commit

Permalink
Simplify Message.
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
nnethercote committed Jun 21, 2023
1 parent 757c290 commit 88cd8f9
Showing 1 changed file with 44 additions and 64 deletions.
108 changes: 44 additions & 64 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ fn produce_final_output_artifacts(
// These are used in linking steps and will be cleaned up afterward.
}

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

enum WorkItemResult<B: WriteBackendMethods> {
pub(crate) enum WorkItemResult<B: WriteBackendMethods> {
Compiled(CompiledModule),
NeedsLink(ModuleCodegen<B::Module>),
NeedsFatLTO(FatLTOInput<B>),
Expand Down Expand Up @@ -923,23 +923,10 @@ fn finish_intra_module_work<B: ExtraBackendMethods>(
}
}

pub enum Message<B: WriteBackendMethods> {
pub(crate) enum Message<B: WriteBackendMethods> {
Token(io::Result<Acquired>),
NeedsFatLTO {
result: FatLTOInput<B>,
worker_id: usize,
},
NeedsThinLTO {
name: String,
thin_buffer: B::ThinBuffer,
worker_id: usize,
},
NeedsLink {
module: ModuleCodegen<B::Module>,
worker_id: usize,
},
Done {
result: Result<CompiledModule, Option<WorkerFatalError>>,
WorkItem {
result: Result<WorkItemResult<B>, Option<WorkerFatalError>>,
worker_id: usize,
},
CodegenDone {
Expand Down Expand Up @@ -1481,50 +1468,54 @@ fn start_executing_work<B: ExtraBackendMethods>(
codegen_done = true;
codegen_aborted = true;
}
Message::Done { result: Ok(compiled_module), worker_id } => {

Message::WorkItem { result, worker_id } => {
free_worker(worker_id);
match compiled_module.kind {
ModuleKind::Regular => {
compiled_modules.push(compiled_module);

match result {
Ok(WorkItemResult::Compiled(compiled_module)) => {
match compiled_module.kind {
ModuleKind::Regular => {
compiled_modules.push(compiled_module);
}
ModuleKind::Allocator => {
assert!(compiled_allocator_module.is_none());
compiled_allocator_module = Some(compiled_module);
}
ModuleKind::Metadata => bug!("Should be handled separately"),
}
}
Ok(WorkItemResult::NeedsLink(module)) => {
needs_link.push(module);
}
Ok(WorkItemResult::NeedsFatLTO(fat_lto_input)) => {
assert!(!started_lto);
needs_fat_lto.push(fat_lto_input);
}
Ok(WorkItemResult::NeedsThinLTO(name, thin_buffer)) => {
assert!(!started_lto);
needs_thin_lto.push((name, thin_buffer));
}
ModuleKind::Allocator => {
assert!(compiled_allocator_module.is_none());
compiled_allocator_module = Some(compiled_module);
Err(Some(WorkerFatalError)) => {
// Like `CodegenAborted`, wait for remaining work to finish.
codegen_done = true;
codegen_aborted = true;
}
Err(None) => {
// If the thread failed that means it panicked, so
// we abort immediately.
bug!("worker thread panicked");
}
ModuleKind::Metadata => bug!("Should be handled separately"),
}
}
Message::NeedsLink { module, worker_id } => {
free_worker(worker_id);
needs_link.push(module);
}
Message::NeedsFatLTO { result, worker_id } => {
assert!(!started_lto);
free_worker(worker_id);
needs_fat_lto.push(result);
}
Message::NeedsThinLTO { name, thin_buffer, worker_id } => {
assert!(!started_lto);
free_worker(worker_id);
needs_thin_lto.push((name, thin_buffer));
}

Message::AddImportOnlyModule { module_data, work_product } => {
assert!(!started_lto);
assert!(!codegen_done);
assert_eq!(main_thread_worker_state, MainThreadWorkerState::Codegenning);
lto_import_only_modules.push((module_data, work_product));
main_thread_worker_state = MainThreadWorkerState::Idle;
}
// If the thread failed that means it panicked, so we abort immediately.
Message::Done { result: Err(None), worker_id: _ } => {
bug!("worker thread panicked");
}
Message::Done { result: Err(Some(WorkerFatalError)), worker_id } => {
// Similar to CodegenAborted, wait for remaining work to finish.
free_worker(worker_id);
codegen_done = true;
codegen_aborted = true;
}
}
}

Expand Down Expand Up @@ -1643,22 +1634,11 @@ fn spawn_work<B: ExtraBackendMethods>(cgcx: CodegenContext<B>, work: WorkItem<B>
fn drop(&mut self) {
let worker_id = self.worker_id;
let msg = match self.result.take() {
Some(Ok(WorkItemResult::Compiled(m))) => {
Message::Done::<B> { result: Ok(m), worker_id }
}
Some(Ok(WorkItemResult::NeedsLink(m))) => {
Message::NeedsLink::<B> { module: m, worker_id }
}
Some(Ok(WorkItemResult::NeedsFatLTO(m))) => {
Message::NeedsFatLTO::<B> { result: m, worker_id }
}
Some(Ok(WorkItemResult::NeedsThinLTO(name, thin_buffer))) => {
Message::NeedsThinLTO::<B> { name, thin_buffer, worker_id }
}
Some(Ok(result)) => Message::WorkItem::<B> { result: Ok(result), worker_id },
Some(Err(FatalError)) => {
Message::Done::<B> { result: Err(Some(WorkerFatalError)), worker_id }
Message::WorkItem::<B> { result: Err(Some(WorkerFatalError)), worker_id }
}
None => Message::Done::<B> { result: Err(None), worker_id },
None => Message::WorkItem::<B> { result: Err(None), worker_id },
};
drop(self.coordinator_send.send(Box::new(msg)));
}
Expand Down

0 comments on commit 88cd8f9

Please sign in to comment.