1
Fork 0

Rollup merge of #96587 - bjorn3:refactor_backend_write, r=michaelwoerister

Refactor the WriteBackendMethods and ExtraBackendMethods traits

The new interface is slightly less confusing and is easier to implement for non-LLVM backends.
This commit is contained in:
Yuki Okushi 2022-05-03 14:58:57 +09:00 committed by GitHub
commit 329a73dbd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 71 deletions

View file

@ -139,14 +139,12 @@ impl CodegenBackend for GccCodegenBackend {
} }
impl ExtraBackendMethods for GccCodegenBackend { impl ExtraBackendMethods for GccCodegenBackend {
fn new_metadata<'tcx>(&self, _tcx: TyCtxt<'tcx>, _mod_name: &str) -> Self::Module { fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) -> Self::Module {
GccContext { let mut mods = GccContext {
context: Context::default(), context: Context::default(),
} };
} unsafe { allocator::codegen(tcx, &mut mods, module_name, kind, has_alloc_error_handler); }
mods
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, mods: &mut Self::Module, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
unsafe { allocator::codegen(tcx, mods, module_name, kind, has_alloc_error_handler) }
} }
fn compile_codegen_unit<'tcx>(&self, tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen<Self::Module>, u64) { fn compile_codegen_unit<'tcx>(&self, tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen<Self::Module>, u64) {
@ -213,7 +211,7 @@ impl WriteBackendMethods for GccCodegenBackend {
unimplemented!(); unimplemented!();
} }
}; };
Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: vec![] }) Ok(LtoModuleCodegen::Fat { module, _serialized_bitcode: vec![] })
} }
fn run_thin_lto(_cgcx: &CodegenContext<Self>, _modules: Vec<(String, Self::ThinBuffer)>, _cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> { fn run_thin_lto(_cgcx: &CodegenContext<Self>, _modules: Vec<(String, Self::ThinBuffer)>, _cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
@ -229,7 +227,12 @@ impl WriteBackendMethods for GccCodegenBackend {
Ok(()) Ok(())
} }
unsafe fn optimize_thin(_cgcx: &CodegenContext<Self>, _thin: &mut ThinModule<Self>) -> Result<ModuleCodegen<Self::Module>, FatalError> { fn optimize_fat(_cgcx: &CodegenContext<Self>, _module: &mut ModuleCodegen<Self::Module>) -> Result<(), FatalError> {
// TODO(antoyo)
Ok(())
}
unsafe fn optimize_thin(_cgcx: &CodegenContext<Self>, _thin: ThinModule<Self>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
unimplemented!(); unimplemented!();
} }
@ -245,11 +248,6 @@ impl WriteBackendMethods for GccCodegenBackend {
unimplemented!(); unimplemented!();
} }
fn run_lto_pass_manager(_cgcx: &CodegenContext<Self>, _module: &ModuleCodegen<Self::Module>, _config: &ModuleConfig, _thin: bool) -> Result<(), FatalError> {
// TODO(antoyo)
Ok(())
}
fn run_link(cgcx: &CodegenContext<Self>, diag_handler: &Handler, modules: Vec<ModuleCodegen<Self::Module>>) -> Result<ModuleCodegen<Self::Module>, FatalError> { fn run_link(cgcx: &CodegenContext<Self>, diag_handler: &Handler, modules: Vec<ModuleCodegen<Self::Module>>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
back::write::link(cgcx, diag_handler, modules) back::write::link(cgcx, diag_handler, modules)
} }

View file

@ -6,9 +6,7 @@ use crate::llvm::{self, build_string, False, True};
use crate::{llvm_util, LlvmCodegenBackend, ModuleLlvm}; use crate::{llvm_util, LlvmCodegenBackend, ModuleLlvm};
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared}; use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
use rustc_codegen_ssa::back::symbol_export; use rustc_codegen_ssa::back::symbol_export;
use rustc_codegen_ssa::back::write::{ use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, TargetMachineFactoryConfig};
CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryConfig,
};
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind}; use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
@ -353,7 +351,7 @@ fn fat_lto(
} }
} }
Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: serialized_bitcode }) Ok(LtoModuleCodegen::Fat { module, _serialized_bitcode: serialized_bitcode })
} }
crate struct Linker<'a>(&'a mut llvm::Linker<'a>); crate struct Linker<'a>(&'a mut llvm::Linker<'a>);
@ -578,11 +576,11 @@ fn thin_lto(
pub(crate) fn run_pass_manager( pub(crate) fn run_pass_manager(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
diag_handler: &Handler, diag_handler: &Handler,
module: &ModuleCodegen<ModuleLlvm>, module: &mut ModuleCodegen<ModuleLlvm>,
config: &ModuleConfig,
thin: bool, thin: bool,
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
let _timer = cgcx.prof.extra_verbose_generic_activity("LLVM_lto_optimize", &*module.name); let _timer = cgcx.prof.extra_verbose_generic_activity("LLVM_lto_optimize", &*module.name);
let config = cgcx.config(module.kind);
// Now we have one massive module inside of llmod. Time to run the // Now we have one massive module inside of llmod. Time to run the
// LTO-specific optimization passes that LLVM provides. // LTO-specific optimization passes that LLVM provides.
@ -726,7 +724,7 @@ impl Drop for ThinBuffer {
} }
pub unsafe fn optimize_thin_module( pub unsafe fn optimize_thin_module(
thin_module: &mut ThinModule<LlvmCodegenBackend>, thin_module: ThinModule<LlvmCodegenBackend>,
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> { ) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
let diag_handler = cgcx.create_diag_handler(); let diag_handler = cgcx.create_diag_handler();
@ -743,7 +741,7 @@ pub unsafe fn optimize_thin_module(
// that LLVM Context and Module. // that LLVM Context and Module.
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names); let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &diag_handler)? as *const _; let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &diag_handler)? as *const _;
let module = ModuleCodegen { let mut module = ModuleCodegen {
module_llvm: ModuleLlvm { llmod_raw, llcx, tm }, module_llvm: ModuleLlvm { llmod_raw, llcx, tm },
name: thin_module.name().to_string(), name: thin_module.name().to_string(),
kind: ModuleKind::Regular, kind: ModuleKind::Regular,
@ -859,8 +857,7 @@ pub unsafe fn optimize_thin_module(
// little differently. // little differently.
{ {
info!("running thin lto passes over {}", module.name); info!("running thin lto passes over {}", module.name);
let config = cgcx.config(module.kind); run_pass_manager(cgcx, &diag_handler, &mut module, true)?;
run_pass_manager(cgcx, &diag_handler, &module, config, true)?;
save_temp_bitcode(cgcx, &module, "thin-lto-after-pm"); save_temp_bitcode(cgcx, &module, "thin-lto-after-pm");
} }
} }

View file

@ -104,19 +104,18 @@ impl Drop for TimeTraceProfiler {
} }
impl ExtraBackendMethods for LlvmCodegenBackend { impl ExtraBackendMethods for LlvmCodegenBackend {
fn new_metadata(&self, tcx: TyCtxt<'_>, mod_name: &str) -> ModuleLlvm {
ModuleLlvm::new_metadata(tcx, mod_name)
}
fn codegen_allocator<'tcx>( fn codegen_allocator<'tcx>(
&self, &self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
module_llvm: &mut ModuleLlvm,
module_name: &str, module_name: &str,
kind: AllocatorKind, kind: AllocatorKind,
has_alloc_error_handler: bool, has_alloc_error_handler: bool,
) { ) -> ModuleLlvm {
unsafe { allocator::codegen(tcx, module_llvm, module_name, kind, has_alloc_error_handler) } let mut module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
unsafe {
allocator::codegen(tcx, &mut module_llvm, module_name, kind, has_alloc_error_handler);
}
module_llvm
} }
fn compile_codegen_unit( fn compile_codegen_unit(
&self, &self,
@ -210,9 +209,16 @@ impl WriteBackendMethods for LlvmCodegenBackend {
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
back::write::optimize(cgcx, diag_handler, module, config) back::write::optimize(cgcx, diag_handler, module, config)
} }
fn optimize_fat(
cgcx: &CodegenContext<Self>,
module: &mut ModuleCodegen<Self::Module>,
) -> Result<(), FatalError> {
let diag_handler = cgcx.create_diag_handler();
back::lto::run_pass_manager(cgcx, &diag_handler, module, false)
}
unsafe fn optimize_thin( unsafe fn optimize_thin(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
thin: &mut ThinModule<Self>, thin: ThinModule<Self>,
) -> Result<ModuleCodegen<Self::Module>, FatalError> { ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
back::lto::optimize_thin_module(thin, cgcx) back::lto::optimize_thin_module(thin, cgcx)
} }
@ -230,15 +236,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) { fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod())) (module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
} }
fn run_lto_pass_manager(
cgcx: &CodegenContext<Self>,
module: &ModuleCodegen<Self::Module>,
config: &ModuleConfig,
thin: bool,
) -> Result<(), FatalError> {
let diag_handler = cgcx.create_diag_handler();
back::lto::run_pass_manager(cgcx, &diag_handler, module, config, thin)
}
} }
unsafe impl Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis unsafe impl Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis

View file

@ -42,7 +42,7 @@ pub struct ThinShared<B: WriteBackendMethods> {
pub enum LtoModuleCodegen<B: WriteBackendMethods> { pub enum LtoModuleCodegen<B: WriteBackendMethods> {
Fat { Fat {
module: Option<ModuleCodegen<B::Module>>, module: ModuleCodegen<B::Module>,
_serialized_bitcode: Vec<SerializedModule<B::ModuleBuffer>>, _serialized_bitcode: Vec<SerializedModule<B::ModuleBuffer>>,
}, },
@ -64,19 +64,15 @@ impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
/// It's intended that the module returned is immediately code generated and /// It's intended that the module returned is immediately code generated and
/// dropped, and then this LTO module is dropped. /// dropped, and then this LTO module is dropped.
pub unsafe fn optimize( pub unsafe fn optimize(
&mut self, self,
cgcx: &CodegenContext<B>, cgcx: &CodegenContext<B>,
) -> Result<ModuleCodegen<B::Module>, FatalError> { ) -> Result<ModuleCodegen<B::Module>, FatalError> {
match *self { match self {
LtoModuleCodegen::Fat { ref mut module, .. } => { LtoModuleCodegen::Fat { mut module, .. } => {
let module = module.take().unwrap(); B::optimize_fat(cgcx, &mut module)?;
{
let config = cgcx.config(module.kind);
B::run_lto_pass_manager(cgcx, &module, config, false)?;
}
Ok(module) Ok(module)
} }
LtoModuleCodegen::Thin(ref mut thin) => B::optimize_thin(cgcx, thin), LtoModuleCodegen::Thin(thin) => B::optimize_thin(cgcx, thin),
} }
} }

View file

@ -889,7 +889,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
fn execute_lto_work_item<B: ExtraBackendMethods>( fn execute_lto_work_item<B: ExtraBackendMethods>(
cgcx: &CodegenContext<B>, cgcx: &CodegenContext<B>,
mut module: lto::LtoModuleCodegen<B>, module: lto::LtoModuleCodegen<B>,
module_config: &ModuleConfig, module_config: &ModuleConfig,
) -> Result<WorkItemResult<B>, FatalError> { ) -> Result<WorkItemResult<B>, FatalError> {
let module = unsafe { module.optimize(cgcx)? }; let module = unsafe { module.optimize(cgcx)? };

View file

@ -575,15 +575,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
} else if let Some(kind) = tcx.allocator_kind(()) { } else if let Some(kind) = tcx.allocator_kind(()) {
let llmod_id = let llmod_id =
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string(); cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
let mut module_llvm = backend.new_metadata(tcx, &llmod_id); let module_llvm = tcx.sess.time("write_allocator_module", || {
tcx.sess.time("write_allocator_module", || { backend.codegen_allocator(tcx, &llmod_id, kind, tcx.lang_items().oom().is_some())
backend.codegen_allocator(
tcx,
&mut module_llvm,
&llmod_id,
kind,
tcx.lang_items().oom().is_some(),
)
}); });
Some(ModuleCodegen { name: llmod_id, module_llvm, kind: ModuleKind::Allocator }) Some(ModuleCodegen { name: llmod_id, module_llvm, kind: ModuleKind::Allocator })

View file

@ -114,15 +114,13 @@ pub trait CodegenBackend {
} }
pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync { pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
fn codegen_allocator<'tcx>( fn codegen_allocator<'tcx>(
&self, &self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
module_llvm: &mut Self::Module,
module_name: &str, module_name: &str,
kind: AllocatorKind, kind: AllocatorKind,
has_alloc_error_handler: bool, has_alloc_error_handler: bool,
); ) -> Self::Module;
/// This generates the codegen unit and returns it along with /// This generates the codegen unit and returns it along with
/// a `u64` giving an estimate of the unit's processing cost. /// a `u64` giving an estimate of the unit's processing cost.
fn compile_codegen_unit( fn compile_codegen_unit(

View file

@ -41,9 +41,13 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
module: &ModuleCodegen<Self::Module>, module: &ModuleCodegen<Self::Module>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<(), FatalError>; ) -> Result<(), FatalError>;
fn optimize_fat(
cgcx: &CodegenContext<Self>,
llmod: &mut ModuleCodegen<Self::Module>,
) -> Result<(), FatalError>;
unsafe fn optimize_thin( unsafe fn optimize_thin(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
thin: &mut ThinModule<Self>, thin: ThinModule<Self>,
) -> Result<ModuleCodegen<Self::Module>, FatalError>; ) -> Result<ModuleCodegen<Self::Module>, FatalError>;
unsafe fn codegen( unsafe fn codegen(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
@ -53,12 +57,6 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
) -> Result<CompiledModule, FatalError>; ) -> Result<CompiledModule, FatalError>;
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer); fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer);
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer); fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer);
fn run_lto_pass_manager(
cgcx: &CodegenContext<Self>,
llmod: &ModuleCodegen<Self::Module>,
config: &ModuleConfig,
thin: bool,
) -> Result<(), FatalError>;
} }
pub trait ThinBufferMethods: Send + Sync { pub trait ThinBufferMethods: Send + Sync {