Skip to content

Commit 6890312

Browse files
committed
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 <[email protected]>
1 parent cf49c2a commit 6890312

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ pub unsafe fn optimize_thin_module(
728728
cgcx: &CodegenContext<LlvmCodegenBackend>,
729729
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
730730
let diag_handler = cgcx.create_diag_handler();
731-
let tm = (cgcx.tm_factory.0)().map_err(|e| write::llvm_err(&diag_handler, &e))?;
731+
let tm = (cgcx.tm_factory)().map_err(|e| write::llvm_err(&diag_handler, &e))?;
732732

733733
// Right now the implementation we've got only works over serialized
734734
// modules, so we create a fresh new LLVM context and parse the module

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use crate::llvm_util;
1111
use crate::type_::Type;
1212
use crate::LlvmCodegenBackend;
1313
use crate::ModuleLlvm;
14-
use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig};
14+
use rustc_codegen_ssa::back::write::{
15+
BitcodeSection, CodegenContext, EmitObj, ModuleConfig, TargetMachineFactoryFn,
16+
};
1517
use rustc_codegen_ssa::traits::*;
1618
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
1719
use rustc_data_structures::small_c_str::SmallCStr;
@@ -129,7 +131,7 @@ fn to_llvm_code_model(code_model: Option<CodeModel>) -> llvm::CodeModel {
129131
pub fn target_machine_factory(
130132
sess: &Session,
131133
optlvl: config::OptLevel,
132-
) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> {
134+
) -> TargetMachineFactoryFn<LlvmCodegenBackend> {
133135
let reloc_model = to_llvm_relocation_model(sess.relocation_model());
134136

135137
let (opt_level, _) = to_llvm_opt_settings(optlvl);

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use back::write::{create_informational_target_machine, create_target_machine};
1919
pub use llvm_util::target_features;
2020
use rustc_ast::expand::allocator::AllocatorKind;
2121
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
22-
use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig};
22+
use rustc_codegen_ssa::back::write::{
23+
CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryFn,
24+
};
2325
use rustc_codegen_ssa::traits::*;
2426
use rustc_codegen_ssa::ModuleCodegen;
2527
use rustc_codegen_ssa::{CodegenResults, CompiledModule};
@@ -34,7 +36,6 @@ use rustc_span::symbol::Symbol;
3436

3537
use std::any::Any;
3638
use std::ffi::CStr;
37-
use std::sync::Arc;
3839

3940
mod back {
4041
pub mod archive;
@@ -109,7 +110,7 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
109110
&self,
110111
sess: &Session,
111112
optlvl: OptLevel,
112-
) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> {
113+
) -> TargetMachineFactoryFn<Self> {
113114
back::write::target_machine_factory(sess, optlvl)
114115
}
115116
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str {
@@ -352,7 +353,7 @@ impl ModuleLlvm {
352353
unsafe {
353354
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
354355
let llmod_raw = back::lto::parse_module(llcx, name, buffer, handler)?;
355-
let tm = match (cgcx.tm_factory.0)() {
356+
let tm = match (cgcx.tm_factory)() {
356357
Ok(m) => m,
357358
Err(e) => {
358359
handler.struct_err(&e).emit();

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,8 @@ impl ModuleConfig {
274274
}
275275
}
276276

277-
// HACK(eddyb) work around `#[derive]` producing wrong bounds for `Clone`.
278-
pub struct TargetMachineFactory<B: WriteBackendMethods>(
279-
pub Arc<dyn Fn() -> Result<B::TargetMachine, String> + Send + Sync>,
280-
);
281-
282-
impl<B: WriteBackendMethods> Clone for TargetMachineFactory<B> {
283-
fn clone(&self) -> Self {
284-
TargetMachineFactory(self.0.clone())
285-
}
286-
}
277+
pub type TargetMachineFactoryFn<B> =
278+
Arc<dyn Fn() -> Result<<B as WriteBackendMethods>::TargetMachine, String> + Send + Sync>;
287279

288280
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
289281

@@ -305,7 +297,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
305297
pub regular_module_config: Arc<ModuleConfig>,
306298
pub metadata_module_config: Arc<ModuleConfig>,
307299
pub allocator_module_config: Arc<ModuleConfig>,
308-
pub tm_factory: TargetMachineFactory<B>,
300+
pub tm_factory: TargetMachineFactoryFn<B>,
309301
pub msvc_imps_needed: bool,
310302
pub is_pe_coff: bool,
311303
pub target_pointer_width: u32,
@@ -1020,7 +1012,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
10201012
regular_module_config: regular_config,
10211013
metadata_module_config: metadata_config,
10221014
allocator_module_config: allocator_config,
1023-
tm_factory: TargetMachineFactory(backend.target_machine_factory(tcx.sess, ol)),
1015+
tm_factory: backend.target_machine_factory(tcx.sess, ol),
10241016
total_cgus,
10251017
msvc_imps_needed: msvc_imps_needed(tcx),
10261018
is_pe_coff: tcx.sess.target.is_like_windows,

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::write::WriteBackendMethods;
22
use super::CodegenObject;
3+
use crate::back::write::TargetMachineFactoryFn;
34
use crate::{CodegenResults, ModuleCodegen};
45

56
use rustc_ast::expand::allocator::AllocatorKind;
@@ -21,7 +22,6 @@ use rustc_target::spec::Target;
2122
pub use rustc_data_structures::sync::MetadataRef;
2223

2324
use std::any::Any;
24-
use std::sync::Arc;
2525

2626
pub trait BackendTypes {
2727
type Value: CodegenObject;
@@ -123,7 +123,7 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
123123
&self,
124124
sess: &Session,
125125
opt_level: config::OptLevel,
126-
) -> Arc<dyn Fn() -> Result<Self::TargetMachine, String> + Send + Sync>;
126+
) -> TargetMachineFactoryFn<Self>;
127127
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;
128128
fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>;
129129
}

0 commit comments

Comments
 (0)