From 336bb0afea102a0e4ec7f56c364c7cd0d2acb902 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 20:50:17 +0200 Subject: [PATCH 1/4] Rename run_lto_pass_manager to optimize_fat and remove thin parameter --- compiler/rustc_codegen_gcc/src/lib.rs | 10 +++++----- compiler/rustc_codegen_llvm/src/lib.rs | 17 ++++++++--------- compiler/rustc_codegen_ssa/src/traits/write.rs | 11 +++++------ 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index 497a28354d813..2b960582879cb 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -229,6 +229,11 @@ impl WriteBackendMethods for GccCodegenBackend { Ok(()) } + fn optimize_fat(_cgcx: &CodegenContext, _module: &ModuleCodegen, _config: &ModuleConfig) -> Result<(), FatalError> { + // TODO(antoyo) + Ok(()) + } + unsafe fn optimize_thin(_cgcx: &CodegenContext, _thin: &mut ThinModule) -> Result, FatalError> { unimplemented!(); } @@ -245,11 +250,6 @@ impl WriteBackendMethods for GccCodegenBackend { unimplemented!(); } - fn run_lto_pass_manager(_cgcx: &CodegenContext, _module: &ModuleCodegen, _config: &ModuleConfig, _thin: bool) -> Result<(), FatalError> { - // TODO(antoyo) - Ok(()) - } - fn run_link(cgcx: &CodegenContext, diag_handler: &Handler, modules: Vec>) -> Result, FatalError> { back::write::link(cgcx, diag_handler, modules) } diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index 3152c505af0e5..3fa906f497d11 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -210,6 +210,14 @@ impl WriteBackendMethods for LlvmCodegenBackend { ) -> Result<(), FatalError> { back::write::optimize(cgcx, diag_handler, module, config) } + fn optimize_fat( + cgcx: &CodegenContext, + module: &ModuleCodegen, + config: &ModuleConfig, + ) -> Result<(), FatalError> { + let diag_handler = cgcx.create_diag_handler(); + back::lto::run_pass_manager(cgcx, &diag_handler, module, config, false) + } unsafe fn optimize_thin( cgcx: &CodegenContext, thin: &mut ThinModule, @@ -230,15 +238,6 @@ impl WriteBackendMethods for LlvmCodegenBackend { fn serialize_module(module: ModuleCodegen) -> (String, Self::ModuleBuffer) { (module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod())) } - fn run_lto_pass_manager( - cgcx: &CodegenContext, - module: &ModuleCodegen, - 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 diff --git a/compiler/rustc_codegen_ssa/src/traits/write.rs b/compiler/rustc_codegen_ssa/src/traits/write.rs index 93fbee2b49bb5..4914de20b05b6 100644 --- a/compiler/rustc_codegen_ssa/src/traits/write.rs +++ b/compiler/rustc_codegen_ssa/src/traits/write.rs @@ -41,6 +41,11 @@ pub trait WriteBackendMethods: 'static + Sized + Clone { module: &ModuleCodegen, config: &ModuleConfig, ) -> Result<(), FatalError>; + fn optimize_fat( + cgcx: &CodegenContext, + llmod: &ModuleCodegen, + config: &ModuleConfig, + ) -> Result<(), FatalError>; unsafe fn optimize_thin( cgcx: &CodegenContext, thin: &mut ThinModule, @@ -53,12 +58,6 @@ pub trait WriteBackendMethods: 'static + Sized + Clone { ) -> Result; fn prepare_thin(module: ModuleCodegen) -> (String, Self::ThinBuffer); fn serialize_module(module: ModuleCodegen) -> (String, Self::ModuleBuffer); - fn run_lto_pass_manager( - cgcx: &CodegenContext, - llmod: &ModuleCodegen, - config: &ModuleConfig, - thin: bool, - ) -> Result<(), FatalError>; } pub trait ThinBufferMethods: Send + Sync { From ee94ff254aceccd27919f26c02541277a3ca7dd7 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 20:51:17 +0200 Subject: [PATCH 2/4] Let LtoModuleCodegen::optimize take self by value --- compiler/rustc_codegen_gcc/src/lib.rs | 4 ++-- compiler/rustc_codegen_llvm/src/back/lto.rs | 4 ++-- compiler/rustc_codegen_llvm/src/lib.rs | 2 +- compiler/rustc_codegen_ssa/src/back/lto.rs | 13 ++++++------- compiler/rustc_codegen_ssa/src/back/write.rs | 2 +- compiler/rustc_codegen_ssa/src/traits/write.rs | 2 +- 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index 2b960582879cb..72da59a1bf808 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -213,7 +213,7 @@ impl WriteBackendMethods for GccCodegenBackend { unimplemented!(); } }; - Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: vec![] }) + Ok(LtoModuleCodegen::Fat { module, _serialized_bitcode: vec![] }) } fn run_thin_lto(_cgcx: &CodegenContext, _modules: Vec<(String, Self::ThinBuffer)>, _cached_modules: Vec<(SerializedModule, WorkProduct)>) -> Result<(Vec>, Vec), FatalError> { @@ -234,7 +234,7 @@ impl WriteBackendMethods for GccCodegenBackend { Ok(()) } - unsafe fn optimize_thin(_cgcx: &CodegenContext, _thin: &mut ThinModule) -> Result, FatalError> { + unsafe fn optimize_thin(_cgcx: &CodegenContext, _thin: ThinModule) -> Result, FatalError> { unimplemented!(); } diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 5c63bd8c1bd8d..570410caaabed 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -353,7 +353,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>); @@ -726,7 +726,7 @@ impl Drop for ThinBuffer { } pub unsafe fn optimize_thin_module( - thin_module: &mut ThinModule, + thin_module: ThinModule, cgcx: &CodegenContext, ) -> Result, FatalError> { let diag_handler = cgcx.create_diag_handler(); diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index 3fa906f497d11..c3db6352a531c 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -220,7 +220,7 @@ impl WriteBackendMethods for LlvmCodegenBackend { } unsafe fn optimize_thin( cgcx: &CodegenContext, - thin: &mut ThinModule, + thin: ThinModule, ) -> Result, FatalError> { back::lto::optimize_thin_module(thin, cgcx) } diff --git a/compiler/rustc_codegen_ssa/src/back/lto.rs b/compiler/rustc_codegen_ssa/src/back/lto.rs index d6ae689f254b1..6dc8cb5038e12 100644 --- a/compiler/rustc_codegen_ssa/src/back/lto.rs +++ b/compiler/rustc_codegen_ssa/src/back/lto.rs @@ -42,7 +42,7 @@ pub struct ThinShared { pub enum LtoModuleCodegen { Fat { - module: Option>, + module: ModuleCodegen, _serialized_bitcode: Vec>, }, @@ -64,19 +64,18 @@ impl LtoModuleCodegen { /// It's intended that the module returned is immediately code generated and /// dropped, and then this LTO module is dropped. pub unsafe fn optimize( - &mut self, + self, cgcx: &CodegenContext, ) -> Result, FatalError> { - match *self { - LtoModuleCodegen::Fat { ref mut module, .. } => { - let module = module.take().unwrap(); + match self { + LtoModuleCodegen::Fat { module, .. } => { { let config = cgcx.config(module.kind); - B::run_lto_pass_manager(cgcx, &module, config, false)?; + B::optimize_fat(cgcx, &module, config)?; } Ok(module) } - LtoModuleCodegen::Thin(ref mut thin) => B::optimize_thin(cgcx, thin), + LtoModuleCodegen::Thin(thin) => B::optimize_thin(cgcx, thin), } } diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 98dc5fe8d6424..88293dec01cac 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -889,7 +889,7 @@ fn execute_copy_from_cache_work_item( fn execute_lto_work_item( cgcx: &CodegenContext, - mut module: lto::LtoModuleCodegen, + module: lto::LtoModuleCodegen, module_config: &ModuleConfig, ) -> Result, FatalError> { let module = unsafe { module.optimize(cgcx)? }; diff --git a/compiler/rustc_codegen_ssa/src/traits/write.rs b/compiler/rustc_codegen_ssa/src/traits/write.rs index 4914de20b05b6..ed0f659180926 100644 --- a/compiler/rustc_codegen_ssa/src/traits/write.rs +++ b/compiler/rustc_codegen_ssa/src/traits/write.rs @@ -48,7 +48,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone { ) -> Result<(), FatalError>; unsafe fn optimize_thin( cgcx: &CodegenContext, - thin: &mut ThinModule, + thin: ThinModule, ) -> Result, FatalError>; unsafe fn codegen( cgcx: &CodegenContext, From fab72301d9089761766b389df209986c2bc38e5d Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 20:58:42 +0200 Subject: [PATCH 3/4] Remove config parameter of optimize_fat and avoid interior mutability for module --- compiler/rustc_codegen_gcc/src/lib.rs | 2 +- compiler/rustc_codegen_llvm/src/back/lto.rs | 13 +++++-------- compiler/rustc_codegen_llvm/src/lib.rs | 5 ++--- compiler/rustc_codegen_ssa/src/back/lto.rs | 5 ++--- compiler/rustc_codegen_ssa/src/traits/write.rs | 3 +-- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index 72da59a1bf808..684b845c38a29 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -229,7 +229,7 @@ impl WriteBackendMethods for GccCodegenBackend { Ok(()) } - fn optimize_fat(_cgcx: &CodegenContext, _module: &ModuleCodegen, _config: &ModuleConfig) -> Result<(), FatalError> { + fn optimize_fat(_cgcx: &CodegenContext, _module: &mut ModuleCodegen) -> Result<(), FatalError> { // TODO(antoyo) Ok(()) } diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 570410caaabed..b5b2a27d2378d 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -6,9 +6,7 @@ use crate::llvm::{self, build_string, False, True}; use crate::{llvm_util, LlvmCodegenBackend, ModuleLlvm}; use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared}; use rustc_codegen_ssa::back::symbol_export; -use rustc_codegen_ssa::back::write::{ - CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryConfig, -}; +use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, TargetMachineFactoryConfig}; use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind}; use rustc_data_structures::fx::FxHashMap; @@ -578,11 +576,11 @@ fn thin_lto( pub(crate) fn run_pass_manager( cgcx: &CodegenContext, diag_handler: &Handler, - module: &ModuleCodegen, - config: &ModuleConfig, + module: &mut ModuleCodegen, thin: bool, ) -> Result<(), FatalError> { 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 // LTO-specific optimization passes that LLVM provides. @@ -743,7 +741,7 @@ pub unsafe fn optimize_thin_module( // that LLVM Context and Module. let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names); 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 }, name: thin_module.name().to_string(), kind: ModuleKind::Regular, @@ -859,8 +857,7 @@ pub unsafe fn optimize_thin_module( // little differently. { info!("running thin lto passes over {}", module.name); - let config = cgcx.config(module.kind); - run_pass_manager(cgcx, &diag_handler, &module, config, true)?; + run_pass_manager(cgcx, &diag_handler, &mut module, true)?; save_temp_bitcode(cgcx, &module, "thin-lto-after-pm"); } } diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index c3db6352a531c..b48c738124d04 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -212,11 +212,10 @@ impl WriteBackendMethods for LlvmCodegenBackend { } fn optimize_fat( cgcx: &CodegenContext, - module: &ModuleCodegen, - config: &ModuleConfig, + module: &mut ModuleCodegen, ) -> Result<(), FatalError> { let diag_handler = cgcx.create_diag_handler(); - back::lto::run_pass_manager(cgcx, &diag_handler, module, config, false) + back::lto::run_pass_manager(cgcx, &diag_handler, module, false) } unsafe fn optimize_thin( cgcx: &CodegenContext, diff --git a/compiler/rustc_codegen_ssa/src/back/lto.rs b/compiler/rustc_codegen_ssa/src/back/lto.rs index 6dc8cb5038e12..3c5b0e5cc43ba 100644 --- a/compiler/rustc_codegen_ssa/src/back/lto.rs +++ b/compiler/rustc_codegen_ssa/src/back/lto.rs @@ -68,10 +68,9 @@ impl LtoModuleCodegen { cgcx: &CodegenContext, ) -> Result, FatalError> { match self { - LtoModuleCodegen::Fat { module, .. } => { + LtoModuleCodegen::Fat { mut module, .. } => { { - let config = cgcx.config(module.kind); - B::optimize_fat(cgcx, &module, config)?; + B::optimize_fat(cgcx, &mut module)?; } Ok(module) } diff --git a/compiler/rustc_codegen_ssa/src/traits/write.rs b/compiler/rustc_codegen_ssa/src/traits/write.rs index ed0f659180926..e54ec34f1ce37 100644 --- a/compiler/rustc_codegen_ssa/src/traits/write.rs +++ b/compiler/rustc_codegen_ssa/src/traits/write.rs @@ -43,8 +43,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone { ) -> Result<(), FatalError>; fn optimize_fat( cgcx: &CodegenContext, - llmod: &ModuleCodegen, - config: &ModuleConfig, + llmod: &mut ModuleCodegen, ) -> Result<(), FatalError>; unsafe fn optimize_thin( cgcx: &CodegenContext, From 78c65a52db0016f489862ecea92a09ad66a2b960 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 21:20:08 +0200 Subject: [PATCH 4/4] Merge new_metadata into codegen_allocator --- compiler/rustc_codegen_gcc/src/lib.rs | 12 +++++------- compiler/rustc_codegen_llvm/src/lib.rs | 13 ++++++------- compiler/rustc_codegen_ssa/src/back/lto.rs | 4 +--- compiler/rustc_codegen_ssa/src/base.rs | 11 ++--------- compiler/rustc_codegen_ssa/src/traits/backend.rs | 4 +--- 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index 684b845c38a29..58996a9db78ad 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -139,14 +139,12 @@ impl CodegenBackend for GccCodegenBackend { } impl ExtraBackendMethods for GccCodegenBackend { - fn new_metadata<'tcx>(&self, _tcx: TyCtxt<'tcx>, _mod_name: &str) -> Self::Module { - GccContext { + fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) -> Self::Module { + let mut mods = GccContext { context: Context::default(), - } - } - - 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) } + }; + unsafe { allocator::codegen(tcx, &mut mods, module_name, kind, has_alloc_error_handler); } + mods } fn compile_codegen_unit<'tcx>(&self, tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen, u64) { diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index b48c738124d04..b035923956954 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -104,19 +104,18 @@ impl Drop for TimeTraceProfiler { } impl ExtraBackendMethods for LlvmCodegenBackend { - fn new_metadata(&self, tcx: TyCtxt<'_>, mod_name: &str) -> ModuleLlvm { - ModuleLlvm::new_metadata(tcx, mod_name) - } - fn codegen_allocator<'tcx>( &self, tcx: TyCtxt<'tcx>, - module_llvm: &mut ModuleLlvm, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool, - ) { - unsafe { allocator::codegen(tcx, module_llvm, module_name, kind, has_alloc_error_handler) } + ) -> ModuleLlvm { + 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( &self, diff --git a/compiler/rustc_codegen_ssa/src/back/lto.rs b/compiler/rustc_codegen_ssa/src/back/lto.rs index 3c5b0e5cc43ba..cb6244050df24 100644 --- a/compiler/rustc_codegen_ssa/src/back/lto.rs +++ b/compiler/rustc_codegen_ssa/src/back/lto.rs @@ -69,9 +69,7 @@ impl LtoModuleCodegen { ) -> Result, FatalError> { match self { LtoModuleCodegen::Fat { mut module, .. } => { - { - B::optimize_fat(cgcx, &mut module)?; - } + B::optimize_fat(cgcx, &mut module)?; Ok(module) } LtoModuleCodegen::Thin(thin) => B::optimize_thin(cgcx, thin), diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 019c9c179d8e1..5bc95614c197c 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -575,15 +575,8 @@ pub fn codegen_crate( } else if let Some(kind) = tcx.allocator_kind(()) { let llmod_id = cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string(); - let mut module_llvm = backend.new_metadata(tcx, &llmod_id); - tcx.sess.time("write_allocator_module", || { - backend.codegen_allocator( - tcx, - &mut module_llvm, - &llmod_id, - kind, - tcx.lang_items().oom().is_some(), - ) + let module_llvm = tcx.sess.time("write_allocator_module", || { + backend.codegen_allocator(tcx, &llmod_id, kind, tcx.lang_items().oom().is_some()) }); Some(ModuleCodegen { name: llmod_id, module_llvm, kind: ModuleKind::Allocator }) diff --git a/compiler/rustc_codegen_ssa/src/traits/backend.rs b/compiler/rustc_codegen_ssa/src/traits/backend.rs index 856b774258316..1e53c73d1bb4a 100644 --- a/compiler/rustc_codegen_ssa/src/traits/backend.rs +++ b/compiler/rustc_codegen_ssa/src/traits/backend.rs @@ -114,15 +114,13 @@ pub trait CodegenBackend { } pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync { - fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module; fn codegen_allocator<'tcx>( &self, tcx: TyCtxt<'tcx>, - module_llvm: &mut Self::Module, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool, - ); + ) -> Self::Module; /// This generates the codegen unit and returns it along with /// a `u64` giving an estimate of the unit's processing cost. fn compile_codegen_unit(