1
Fork 0

cg_ssa: introduce TargetMachineFactoryFn alias

This commit removes the `TargetMachineFactory` struct and adds a
`TargetMachineFactoryFn` type alias which is used everywhere that the
previous, long type was used.

Signed-off-by: David Wood <david@davidtw.co>
This commit is contained in:
David Wood 2020-09-23 16:57:50 +01:00
parent cf49c2a1ef
commit 6890312ea3
No known key found for this signature in database
GPG key ID: 2592E76C87381FD9
5 changed files with 16 additions and 21 deletions

View file

@ -728,7 +728,7 @@ pub unsafe fn optimize_thin_module(
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();
let tm = (cgcx.tm_factory.0)().map_err(|e| write::llvm_err(&diag_handler, &e))?; let tm = (cgcx.tm_factory)().map_err(|e| write::llvm_err(&diag_handler, &e))?;
// Right now the implementation we've got only works over serialized // Right now the implementation we've got only works over serialized
// modules, so we create a fresh new LLVM context and parse the module // modules, so we create a fresh new LLVM context and parse the module

View file

@ -11,7 +11,9 @@ use crate::llvm_util;
use crate::type_::Type; use crate::type_::Type;
use crate::LlvmCodegenBackend; use crate::LlvmCodegenBackend;
use crate::ModuleLlvm; use crate::ModuleLlvm;
use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig}; use rustc_codegen_ssa::back::write::{
BitcodeSection, CodegenContext, EmitObj, ModuleConfig, TargetMachineFactoryFn,
};
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen}; use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
@ -129,7 +131,7 @@ fn to_llvm_code_model(code_model: Option<CodeModel>) -> llvm::CodeModel {
pub fn target_machine_factory( pub fn target_machine_factory(
sess: &Session, sess: &Session,
optlvl: config::OptLevel, optlvl: config::OptLevel,
) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> { ) -> TargetMachineFactoryFn<LlvmCodegenBackend> {
let reloc_model = to_llvm_relocation_model(sess.relocation_model()); let reloc_model = to_llvm_relocation_model(sess.relocation_model());
let (opt_level, _) = to_llvm_opt_settings(optlvl); let (opt_level, _) = to_llvm_opt_settings(optlvl);

View file

@ -19,7 +19,9 @@ use back::write::{create_informational_target_machine, create_target_machine};
pub use llvm_util::target_features; pub use llvm_util::target_features;
use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::expand::allocator::AllocatorKind;
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule}; use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig}; use rustc_codegen_ssa::back::write::{
CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryFn,
};
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::ModuleCodegen; use rustc_codegen_ssa::ModuleCodegen;
use rustc_codegen_ssa::{CodegenResults, CompiledModule}; use rustc_codegen_ssa::{CodegenResults, CompiledModule};
@ -34,7 +36,6 @@ use rustc_span::symbol::Symbol;
use std::any::Any; use std::any::Any;
use std::ffi::CStr; use std::ffi::CStr;
use std::sync::Arc;
mod back { mod back {
pub mod archive; pub mod archive;
@ -109,7 +110,7 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
&self, &self,
sess: &Session, sess: &Session,
optlvl: OptLevel, optlvl: OptLevel,
) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> { ) -> TargetMachineFactoryFn<Self> {
back::write::target_machine_factory(sess, optlvl) back::write::target_machine_factory(sess, optlvl)
} }
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str { fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str {
@ -352,7 +353,7 @@ impl ModuleLlvm {
unsafe { unsafe {
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names); let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
let llmod_raw = back::lto::parse_module(llcx, name, buffer, handler)?; let llmod_raw = back::lto::parse_module(llcx, name, buffer, handler)?;
let tm = match (cgcx.tm_factory.0)() { let tm = match (cgcx.tm_factory)() {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
handler.struct_err(&e).emit(); handler.struct_err(&e).emit();

View file

@ -274,16 +274,8 @@ impl ModuleConfig {
} }
} }
// HACK(eddyb) work around `#[derive]` producing wrong bounds for `Clone`. pub type TargetMachineFactoryFn<B> =
pub struct TargetMachineFactory<B: WriteBackendMethods>( Arc<dyn Fn() -> Result<<B as WriteBackendMethods>::TargetMachine, String> + Send + Sync>;
pub Arc<dyn Fn() -> Result<B::TargetMachine, String> + Send + Sync>,
);
impl<B: WriteBackendMethods> Clone for TargetMachineFactory<B> {
fn clone(&self) -> Self {
TargetMachineFactory(self.0.clone())
}
}
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>; pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
@ -305,7 +297,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
pub regular_module_config: Arc<ModuleConfig>, pub regular_module_config: Arc<ModuleConfig>,
pub metadata_module_config: Arc<ModuleConfig>, pub metadata_module_config: Arc<ModuleConfig>,
pub allocator_module_config: Arc<ModuleConfig>, pub allocator_module_config: Arc<ModuleConfig>,
pub tm_factory: TargetMachineFactory<B>, pub tm_factory: TargetMachineFactoryFn<B>,
pub msvc_imps_needed: bool, pub msvc_imps_needed: bool,
pub is_pe_coff: bool, pub is_pe_coff: bool,
pub target_pointer_width: u32, pub target_pointer_width: u32,
@ -1020,7 +1012,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
regular_module_config: regular_config, regular_module_config: regular_config,
metadata_module_config: metadata_config, metadata_module_config: metadata_config,
allocator_module_config: allocator_config, allocator_module_config: allocator_config,
tm_factory: TargetMachineFactory(backend.target_machine_factory(tcx.sess, ol)), tm_factory: backend.target_machine_factory(tcx.sess, ol),
total_cgus, total_cgus,
msvc_imps_needed: msvc_imps_needed(tcx), msvc_imps_needed: msvc_imps_needed(tcx),
is_pe_coff: tcx.sess.target.is_like_windows, is_pe_coff: tcx.sess.target.is_like_windows,

View file

@ -1,5 +1,6 @@
use super::write::WriteBackendMethods; use super::write::WriteBackendMethods;
use super::CodegenObject; use super::CodegenObject;
use crate::back::write::TargetMachineFactoryFn;
use crate::{CodegenResults, ModuleCodegen}; use crate::{CodegenResults, ModuleCodegen};
use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::expand::allocator::AllocatorKind;
@ -21,7 +22,6 @@ use rustc_target::spec::Target;
pub use rustc_data_structures::sync::MetadataRef; pub use rustc_data_structures::sync::MetadataRef;
use std::any::Any; use std::any::Any;
use std::sync::Arc;
pub trait BackendTypes { pub trait BackendTypes {
type Value: CodegenObject; type Value: CodegenObject;
@ -123,7 +123,7 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
&self, &self,
sess: &Session, sess: &Session,
opt_level: config::OptLevel, opt_level: config::OptLevel,
) -> Arc<dyn Fn() -> Result<Self::TargetMachine, String> + Send + Sync>; ) -> TargetMachineFactoryFn<Self>;
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str; fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;
fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>; fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>;
} }