Skip to content

Commit 6ea5244

Browse files
committed
Move some methods to WriteBackendMethods
1 parent eff0d4c commit 6ea5244

5 files changed

Lines changed: 47 additions & 49 deletions

File tree

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,6 @@ impl ExtraBackendMethods for GccCodegenBackend {
371371
self.lto_supported.load(Ordering::SeqCst),
372372
)
373373
}
374-
375-
fn target_machine_factory(
376-
&self,
377-
_sess: &Session,
378-
_opt_level: OptLevel,
379-
_features: &[String],
380-
) -> TargetMachineFactoryFn<Self> {
381-
// TODO(antoyo): set opt level.
382-
Arc::new(|_, _| ())
383-
}
384374
}
385375

386376
#[derive(Clone, Copy, PartialEq)]
@@ -429,6 +419,16 @@ impl WriteBackendMethods for GccCodegenBackend {
429419
type ModuleBuffer = ModuleBuffer;
430420
type ThinData = ();
431421

422+
fn target_machine_factory(
423+
&self,
424+
_sess: &Session,
425+
_opt_level: OptLevel,
426+
_features: &[String],
427+
) -> TargetMachineFactoryFn<Self> {
428+
// TODO(antoyo): set opt level.
429+
Arc::new(|_, _| ())
430+
}
431+
432432
fn optimize_and_codegen_fat_lto(
433433
cgcx: &CodegenContext,
434434
prof: &SelfProfilerRef,

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
116116
) -> (ModuleCodegen<ModuleLlvm>, u64) {
117117
base::compile_codegen_unit(tcx, cgu_name)
118118
}
119+
}
120+
121+
impl WriteBackendMethods for LlvmCodegenBackend {
122+
type Module = ModuleLlvm;
123+
type ModuleBuffer = back::lto::ModuleBuffer;
124+
type TargetMachine = OwnedTargetMachine;
125+
type ThinData = back::lto::ThinData;
126+
fn thread_profiler() -> Box<dyn Any> {
127+
Box::new(TimeTraceProfiler::new())
128+
}
119129
fn target_machine_factory(
120130
&self,
121131
sess: &Session,
@@ -124,17 +134,6 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
124134
) -> TargetMachineFactoryFn<Self> {
125135
back::write::target_machine_factory(sess, optlvl, target_features)
126136
}
127-
128-
fn thread_profiler() -> Box<dyn Any> {
129-
Box::new(TimeTraceProfiler::new())
130-
}
131-
}
132-
133-
impl WriteBackendMethods for LlvmCodegenBackend {
134-
type Module = ModuleLlvm;
135-
type ModuleBuffer = back::lto::ModuleBuffer;
136-
type TargetMachine = OwnedTargetMachine;
137-
type ThinData = back::lto::ThinData;
138137
fn optimize_and_codegen_fat_lto(
139138
cgcx: &CodegenContext,
140139
prof: &SelfProfilerRef,

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ pub struct CodegenContext {
354354
pub parallel: bool,
355355
}
356356

357-
fn generate_thin_lto_work<B: ExtraBackendMethods>(
357+
fn generate_thin_lto_work<B: WriteBackendMethods>(
358358
cgcx: &CodegenContext,
359359
prof: &SelfProfilerRef,
360360
dcx: DiagCtxtHandle<'_>,
@@ -822,7 +822,7 @@ pub(crate) fn compute_per_cgu_lto_type(
822822
}
823823
}
824824

825-
fn execute_optimize_work_item<B: ExtraBackendMethods>(
825+
fn execute_optimize_work_item<B: WriteBackendMethods>(
826826
cgcx: &CodegenContext,
827827
prof: &SelfProfilerRef,
828828
shared_emitter: SharedEmitter,
@@ -967,7 +967,7 @@ fn execute_copy_from_cache_work_item(
967967
}
968968
}
969969

970-
fn do_fat_lto<B: ExtraBackendMethods>(
970+
fn do_fat_lto<B: WriteBackendMethods>(
971971
cgcx: &CodegenContext,
972972
prof: &SelfProfilerRef,
973973
shared_emitter: SharedEmitter,
@@ -999,7 +999,7 @@ fn do_fat_lto<B: ExtraBackendMethods>(
999999
)
10001000
}
10011001

1002-
fn do_thin_lto<B: ExtraBackendMethods>(
1002+
fn do_thin_lto<B: WriteBackendMethods>(
10031003
cgcx: &CodegenContext,
10041004
prof: &SelfProfilerRef,
10051005
shared_emitter: SharedEmitter,
@@ -1152,7 +1152,7 @@ fn do_thin_lto<B: ExtraBackendMethods>(
11521152
compiled_modules
11531153
}
11541154

1155-
fn execute_thin_lto_work_item<B: ExtraBackendMethods>(
1155+
fn execute_thin_lto_work_item<B: WriteBackendMethods>(
11561156
cgcx: &CodegenContext,
11571157
prof: &SelfProfilerRef,
11581158
shared_emitter: SharedEmitter,
@@ -1879,7 +1879,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
18791879
#[must_use]
18801880
pub(crate) struct WorkerFatalError;
18811881

1882-
fn spawn_work<'a, B: ExtraBackendMethods>(
1882+
fn spawn_work<'a, B: WriteBackendMethods>(
18831883
cgcx: &CodegenContext,
18841884
prof: &'a SelfProfilerRef,
18851885
shared_emitter: SharedEmitter,
@@ -1922,7 +1922,7 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
19221922
std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");
19231923
}
19241924

1925-
fn spawn_thin_lto_work<B: ExtraBackendMethods>(
1925+
fn spawn_thin_lto_work<B: WriteBackendMethods>(
19261926
cgcx: &CodegenContext,
19271927
prof: &SelfProfilerRef,
19281928
shared_emitter: SharedEmitter,
@@ -2109,20 +2109,20 @@ impl SharedEmitterMain {
21092109
}
21102110
}
21112111

2112-
pub struct Coordinator<B: ExtraBackendMethods> {
2112+
pub struct Coordinator<B: WriteBackendMethods> {
21132113
sender: Sender<Message<B>>,
21142114
future: Option<thread::JoinHandle<Result<MaybeLtoModules<B>, ()>>>,
21152115
// Only used for the Message type.
21162116
phantom: PhantomData<B>,
21172117
}
21182118

2119-
impl<B: ExtraBackendMethods> Coordinator<B> {
2119+
impl<B: WriteBackendMethods> Coordinator<B> {
21202120
fn join(mut self) -> std::thread::Result<Result<MaybeLtoModules<B>, ()>> {
21212121
self.future.take().unwrap().join()
21222122
}
21232123
}
21242124

2125-
impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
2125+
impl<B: WriteBackendMethods> Drop for Coordinator<B> {
21262126
fn drop(&mut self) {
21272127
if let Some(future) = self.future.take() {
21282128
// If we haven't joined yet, signal to the coordinator that it should spawn no more
@@ -2133,7 +2133,7 @@ impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
21332133
}
21342134
}
21352135

2136-
pub struct OngoingCodegen<B: ExtraBackendMethods> {
2136+
pub struct OngoingCodegen<B: WriteBackendMethods> {
21372137
pub backend: B,
21382138
pub output_filenames: Arc<OutputFilenames>,
21392139
// Field order below is intended to terminate the coordinator thread before two fields below
@@ -2144,7 +2144,7 @@ pub struct OngoingCodegen<B: ExtraBackendMethods> {
21442144
pub shared_emitter_main: SharedEmitterMain,
21452145
}
21462146

2147-
impl<B: ExtraBackendMethods> OngoingCodegen<B> {
2147+
impl<B: WriteBackendMethods> OngoingCodegen<B> {
21482148
pub fn join(self, sess: &Session) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
21492149
self.shared_emitter_main.check(sess, true);
21502150

@@ -2267,7 +2267,7 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
22672267
}
22682268
}
22692269

2270-
pub(crate) fn submit_codegened_module_to_llvm<B: ExtraBackendMethods>(
2270+
pub(crate) fn submit_codegened_module_to_llvm<B: WriteBackendMethods>(
22712271
coordinator: &Coordinator<B>,
22722272
module: ModuleCodegen<B::Module>,
22732273
cost: u64,
@@ -2276,15 +2276,15 @@ pub(crate) fn submit_codegened_module_to_llvm<B: ExtraBackendMethods>(
22762276
drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost }));
22772277
}
22782278

2279-
pub(crate) fn submit_post_lto_module_to_llvm<B: ExtraBackendMethods>(
2279+
pub(crate) fn submit_post_lto_module_to_llvm<B: WriteBackendMethods>(
22802280
coordinator: &Coordinator<B>,
22812281
module: CachedModuleCodegen,
22822282
) {
22832283
let llvm_work_item = WorkItem::CopyPostLtoArtifacts(module);
22842284
drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost: 0 }));
22852285
}
22862286

2287-
pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
2287+
pub(crate) fn submit_pre_lto_module_to_llvm<B: WriteBackendMethods>(
22882288
tcx: TyCtxt<'_>,
22892289
coordinator: &Coordinator<B>,
22902290
module: CachedModuleCodegen,

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_middle::util::Providers;
1212
use rustc_session::Session;
13-
use rustc_session::config::{self, CrateType, OutputFilenames, PrintRequest};
13+
use rustc_session::config::{CrateType, OutputFilenames, PrintRequest};
1414
use rustc_span::Symbol;
1515

1616
use super::CodegenObject;
1717
use super::write::WriteBackendMethods;
1818
use crate::back::archive::ArArchiveBuilderBuilder;
1919
use crate::back::link::link_binary;
20-
use crate::back::write::TargetMachineFactoryFn;
2120
use crate::{CompiledModules, CrateInfo, ModuleCodegen, TargetConfig};
2221

2322
pub trait BackendTypes {
@@ -162,17 +161,6 @@ pub trait ExtraBackendMethods:
162161
cgu_name: Symbol,
163162
) -> (ModuleCodegen<Self::Module>, u64);
164163

165-
fn target_machine_factory(
166-
&self,
167-
sess: &Session,
168-
opt_level: config::OptLevel,
169-
target_features: &[String],
170-
) -> TargetMachineFactoryFn<Self>;
171-
172-
fn thread_profiler() -> Box<dyn Any> {
173-
Box::new(())
174-
}
175-
176164
/// Returns `true` if this backend can be safely called from multiple threads.
177165
///
178166
/// Defaults to `true`.

compiler/rustc_codegen_ssa/src/traits/write.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use std::any::Any;
12
use std::path::PathBuf;
23

34
use rustc_data_structures::profiling::SelfProfilerRef;
45
use rustc_errors::DiagCtxtHandle;
56
use rustc_middle::dep_graph::WorkProduct;
7+
use rustc_session::{Session, config};
68

79
use crate::back::lto::{SerializedModule, ThinModule};
810
use crate::back::write::{
@@ -16,6 +18,15 @@ pub trait WriteBackendMethods: Clone + 'static {
1618
type ModuleBuffer: ModuleBufferMethods;
1719
type ThinData: Send + Sync;
1820

21+
fn thread_profiler() -> Box<dyn Any> {
22+
Box::new(())
23+
}
24+
fn target_machine_factory(
25+
&self,
26+
sess: &Session,
27+
opt_level: config::OptLevel,
28+
target_features: &[String],
29+
) -> TargetMachineFactoryFn<Self>;
1930
/// Performs fat LTO by merging all modules into a single one, running autodiff
2031
/// if necessary and running any further optimizations
2132
fn optimize_and_codegen_fat_lto(

0 commit comments

Comments
 (0)