Move some methods to WriteBackendMethods

This commit is contained in:
bjorn3
2026-02-15 16:38:39 +00:00
parent eff0d4c6f4
commit 6ea5244ebf
5 changed files with 47 additions and 49 deletions
+10 -10
View File
@@ -371,16 +371,6 @@ fn compile_codegen_unit(
self.lto_supported.load(Ordering::SeqCst),
)
}
fn target_machine_factory(
&self,
_sess: &Session,
_opt_level: OptLevel,
_features: &[String],
) -> TargetMachineFactoryFn<Self> {
// TODO(antoyo): set opt level.
Arc::new(|_, _| ())
}
}
#[derive(Clone, Copy, PartialEq)]
@@ -429,6 +419,16 @@ impl WriteBackendMethods for GccCodegenBackend {
type ModuleBuffer = ModuleBuffer;
type ThinData = ();
fn target_machine_factory(
&self,
_sess: &Session,
_opt_level: OptLevel,
_features: &[String],
) -> TargetMachineFactoryFn<Self> {
// TODO(antoyo): set opt level.
Arc::new(|_, _| ())
}
fn optimize_and_codegen_fat_lto(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
+10 -11
View File
@@ -116,6 +116,16 @@ fn compile_codegen_unit(
) -> (ModuleCodegen<ModuleLlvm>, u64) {
base::compile_codegen_unit(tcx, cgu_name)
}
}
impl WriteBackendMethods for LlvmCodegenBackend {
type Module = ModuleLlvm;
type ModuleBuffer = back::lto::ModuleBuffer;
type TargetMachine = OwnedTargetMachine;
type ThinData = back::lto::ThinData;
fn thread_profiler() -> Box<dyn Any> {
Box::new(TimeTraceProfiler::new())
}
fn target_machine_factory(
&self,
sess: &Session,
@@ -124,17 +134,6 @@ fn target_machine_factory(
) -> TargetMachineFactoryFn<Self> {
back::write::target_machine_factory(sess, optlvl, target_features)
}
fn thread_profiler() -> Box<dyn Any> {
Box::new(TimeTraceProfiler::new())
}
}
impl WriteBackendMethods for LlvmCodegenBackend {
type Module = ModuleLlvm;
type ModuleBuffer = back::lto::ModuleBuffer;
type TargetMachine = OwnedTargetMachine;
type ThinData = back::lto::ThinData;
fn optimize_and_codegen_fat_lto(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
+15 -15
View File
@@ -354,7 +354,7 @@ pub struct CodegenContext {
pub parallel: bool,
}
fn generate_thin_lto_work<B: ExtraBackendMethods>(
fn generate_thin_lto_work<B: WriteBackendMethods>(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
dcx: DiagCtxtHandle<'_>,
@@ -822,7 +822,7 @@ pub(crate) fn compute_per_cgu_lto_type(
}
}
fn execute_optimize_work_item<B: ExtraBackendMethods>(
fn execute_optimize_work_item<B: WriteBackendMethods>(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: SharedEmitter,
@@ -967,7 +967,7 @@ fn execute_copy_from_cache_work_item(
}
}
fn do_fat_lto<B: ExtraBackendMethods>(
fn do_fat_lto<B: WriteBackendMethods>(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: SharedEmitter,
@@ -999,7 +999,7 @@ fn do_fat_lto<B: ExtraBackendMethods>(
)
}
fn do_thin_lto<B: ExtraBackendMethods>(
fn do_thin_lto<B: WriteBackendMethods>(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: SharedEmitter,
@@ -1152,7 +1152,7 @@ fn do_thin_lto<B: ExtraBackendMethods>(
compiled_modules
}
fn execute_thin_lto_work_item<B: ExtraBackendMethods>(
fn execute_thin_lto_work_item<B: WriteBackendMethods>(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: SharedEmitter,
@@ -1879,7 +1879,7 @@ fn queue_full_enough(items_in_queue: usize, workers_running: usize) -> bool {
#[must_use]
pub(crate) struct WorkerFatalError;
fn spawn_work<'a, B: ExtraBackendMethods>(
fn spawn_work<'a, B: WriteBackendMethods>(
cgcx: &CodegenContext,
prof: &'a SelfProfilerRef,
shared_emitter: SharedEmitter,
@@ -1922,7 +1922,7 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");
}
fn spawn_thin_lto_work<B: ExtraBackendMethods>(
fn spawn_thin_lto_work<B: WriteBackendMethods>(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: SharedEmitter,
@@ -2109,20 +2109,20 @@ fn check(&self, sess: &Session, blocking: bool) {
}
}
pub struct Coordinator<B: ExtraBackendMethods> {
pub struct Coordinator<B: WriteBackendMethods> {
sender: Sender<Message<B>>,
future: Option<thread::JoinHandle<Result<MaybeLtoModules<B>, ()>>>,
// Only used for the Message type.
phantom: PhantomData<B>,
}
impl<B: ExtraBackendMethods> Coordinator<B> {
impl<B: WriteBackendMethods> Coordinator<B> {
fn join(mut self) -> std::thread::Result<Result<MaybeLtoModules<B>, ()>> {
self.future.take().unwrap().join()
}
}
impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
impl<B: WriteBackendMethods> Drop for Coordinator<B> {
fn drop(&mut self) {
if let Some(future) = self.future.take() {
// If we haven't joined yet, signal to the coordinator that it should spawn no more
@@ -2133,7 +2133,7 @@ fn drop(&mut self) {
}
}
pub struct OngoingCodegen<B: ExtraBackendMethods> {
pub struct OngoingCodegen<B: WriteBackendMethods> {
pub backend: B,
pub output_filenames: Arc<OutputFilenames>,
// Field order below is intended to terminate the coordinator thread before two fields below
@@ -2144,7 +2144,7 @@ pub struct OngoingCodegen<B: ExtraBackendMethods> {
pub shared_emitter_main: SharedEmitterMain,
}
impl<B: ExtraBackendMethods> OngoingCodegen<B> {
impl<B: WriteBackendMethods> OngoingCodegen<B> {
pub fn join(self, sess: &Session) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
self.shared_emitter_main.check(sess, true);
@@ -2267,7 +2267,7 @@ pub(crate) fn wait_for_signal_to_codegen_item(&self) {
}
}
pub(crate) fn submit_codegened_module_to_llvm<B: ExtraBackendMethods>(
pub(crate) fn submit_codegened_module_to_llvm<B: WriteBackendMethods>(
coordinator: &Coordinator<B>,
module: ModuleCodegen<B::Module>,
cost: u64,
@@ -2276,7 +2276,7 @@ pub(crate) fn submit_codegened_module_to_llvm<B: ExtraBackendMethods>(
drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost }));
}
pub(crate) fn submit_post_lto_module_to_llvm<B: ExtraBackendMethods>(
pub(crate) fn submit_post_lto_module_to_llvm<B: WriteBackendMethods>(
coordinator: &Coordinator<B>,
module: CachedModuleCodegen,
) {
@@ -2284,7 +2284,7 @@ pub(crate) fn submit_post_lto_module_to_llvm<B: ExtraBackendMethods>(
drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost: 0 }));
}
pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
pub(crate) fn submit_pre_lto_module_to_llvm<B: WriteBackendMethods>(
tcx: TyCtxt<'_>,
coordinator: &Coordinator<B>,
module: CachedModuleCodegen,
@@ -10,14 +10,13 @@
use rustc_middle::ty::TyCtxt;
use rustc_middle::util::Providers;
use rustc_session::Session;
use rustc_session::config::{self, CrateType, OutputFilenames, PrintRequest};
use rustc_session::config::{CrateType, OutputFilenames, PrintRequest};
use rustc_span::Symbol;
use super::CodegenObject;
use super::write::WriteBackendMethods;
use crate::back::archive::ArArchiveBuilderBuilder;
use crate::back::link::link_binary;
use crate::back::write::TargetMachineFactoryFn;
use crate::{CompiledModules, CrateInfo, ModuleCodegen, TargetConfig};
pub trait BackendTypes {
@@ -162,17 +161,6 @@ fn compile_codegen_unit(
cgu_name: Symbol,
) -> (ModuleCodegen<Self::Module>, u64);
fn target_machine_factory(
&self,
sess: &Session,
opt_level: config::OptLevel,
target_features: &[String],
) -> TargetMachineFactoryFn<Self>;
fn thread_profiler() -> Box<dyn Any> {
Box::new(())
}
/// Returns `true` if this backend can be safely called from multiple threads.
///
/// Defaults to `true`.
@@ -1,8 +1,10 @@
use std::any::Any;
use std::path::PathBuf;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::DiagCtxtHandle;
use rustc_middle::dep_graph::WorkProduct;
use rustc_session::{Session, config};
use crate::back::lto::{SerializedModule, ThinModule};
use crate::back::write::{
@@ -16,6 +18,15 @@ pub trait WriteBackendMethods: Clone + 'static {
type ModuleBuffer: ModuleBufferMethods;
type ThinData: Send + Sync;
fn thread_profiler() -> Box<dyn Any> {
Box::new(())
}
fn target_machine_factory(
&self,
sess: &Session,
opt_level: config::OptLevel,
target_features: &[String],
) -> TargetMachineFactoryFn<Self>;
/// Performs fat LTO by merging all modules into a single one, running autodiff
/// if necessary and running any further optimizations
fn optimize_and_codegen_fat_lto(