Skip to content

Commit a30931c

Browse files
committed
renamed target machine wrapper -> target machine box and clarified its semantics with comments
1 parent 92d6157 commit a30931c

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

compiler/rustc_codegen_llvm/src/back/target_machine_wrapper.rs renamed to compiler/rustc_codegen_llvm/src/back/target_machine_box.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ use rustc_data_structures::small_c_str::SmallCStr;
99

1010
use crate::{errors::LlvmError, llvm};
1111

12+
/// Provides Box like semantics for the foreign llvm::TargetMachine type.
13+
/// Responsible for safely creating and disposing llvm::TargetMachine via ffi functions.
14+
/// Not cloneable as there is no clone function for llvm::TargetMachine.
1215
#[repr(transparent)]
13-
pub struct TargetMachineWrapper {
16+
pub struct TargetMachineBox {
1417
tm_unique: NonNull<llvm::TargetMachine>,
1518
phantom: PhantomData<llvm::TargetMachine>,
1619
}
1720

18-
impl TargetMachineWrapper {
21+
impl TargetMachineBox {
1922
pub fn new(
2023
triple: &CStr,
2124
cpu: &CStr,
@@ -79,7 +82,7 @@ impl TargetMachineWrapper {
7982
}
8083
}
8184

82-
impl Deref for TargetMachineWrapper {
85+
impl Deref for TargetMachineBox {
8386
type Target = llvm::TargetMachine;
8487

8588
fn deref(&self) -> &Self::Target {
@@ -88,10 +91,10 @@ impl Deref for TargetMachineWrapper {
8891
}
8992
}
9093

91-
impl Drop for TargetMachineWrapper {
94+
impl Drop for TargetMachineBox {
9295
fn drop(&mut self) {
9396
// SAFETY: constructing ensures we have a valid pointer created by llvm::LLVMRustCreateTargetMachine
94-
// TargetMachineWrapper is not copyable so there is no double free or use after free
97+
// TargetMachineBox is not copyable so there is no double free or use after free
9598
unsafe {
9699
llvm::LLVMRustDisposeTargetMachine(self.tm_unique.as_mut());
97100
}

compiler/rustc_codegen_llvm/src/back/write.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::back::lto::ThinBuffer;
22
use crate::back::profiling::{
33
selfprofile_after_pass_callback, selfprofile_before_pass_callback, LlvmSelfProfiler,
44
};
5-
use crate::back::target_machine_wrapper::TargetMachineWrapper;
5+
use crate::back::target_machine_box::TargetMachineBox;
66
use crate::base;
77
use crate::common;
88
use crate::errors::{
@@ -99,7 +99,7 @@ pub fn write_output_file<'ll>(
9999
}
100100
}
101101

102-
pub fn create_informational_target_machine(sess: &Session) -> TargetMachineWrapper {
102+
pub fn create_informational_target_machine(sess: &Session) -> TargetMachineBox {
103103
let config = TargetMachineFactoryConfig { split_dwarf_file: None };
104104
// Can't use query system here quite yet because this function is invoked before the query
105105
// system/tcx is set up.
@@ -108,7 +108,7 @@ pub fn create_informational_target_machine(sess: &Session) -> TargetMachineWrapp
108108
.unwrap_or_else(|err| llvm_err(sess.diagnostic(), err).raise())
109109
}
110110

111-
pub fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> TargetMachineWrapper {
111+
pub fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> TargetMachineBox {
112112
let split_dwarf_file = if tcx.sess.target_can_use_split_dwarf() {
113113
tcx.output_filenames(()).split_dwarf_path(
114114
tcx.sess.split_debuginfo(),
@@ -260,7 +260,7 @@ pub fn target_machine_factory(
260260
path_mapping.map_prefix(config.split_dwarf_file.unwrap_or_default()).0;
261261
let split_dwarf_file = CString::new(split_dwarf_file.to_str().unwrap()).unwrap();
262262

263-
TargetMachineWrapper::new(
263+
TargetMachineBox::new(
264264
&triple,
265265
&cpu,
266266
&features,

compiler/rustc_codegen_llvm/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern crate rustc_macros;
2222
#[macro_use]
2323
extern crate tracing;
2424

25-
use back::target_machine_wrapper::TargetMachineWrapper;
25+
use back::target_machine_box::TargetMachineBox;
2626
use back::write::{create_informational_target_machine, create_target_machine};
2727

2828
use errors::ParseTargetMachineConfig;
@@ -54,7 +54,7 @@ mod back {
5454
pub mod archive;
5555
pub mod lto;
5656
mod profiling;
57-
pub mod target_machine_wrapper;
57+
pub mod target_machine_box;
5858
pub mod write;
5959
}
6060

@@ -164,7 +164,7 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
164164
impl WriteBackendMethods for LlvmCodegenBackend {
165165
type Module = ModuleLlvm;
166166
type ModuleBuffer = back::lto::ModuleBuffer;
167-
type TargetMachine = TargetMachineWrapper;
167+
type TargetMachine = TargetMachineBox;
168168
type TargetMachineError = crate::errors::LlvmError<'static>;
169169
type ThinData = back::lto::ThinData;
170170
type ThinBuffer = back::lto::ThinBuffer;
@@ -405,7 +405,7 @@ pub struct ModuleLlvm {
405405
llmod_raw: *const llvm::Module,
406406

407407
// independent from llcx and llmod_raw, resources get disposed by drop impl
408-
tm: TargetMachineWrapper,
408+
tm: TargetMachineBox,
409409
}
410410

411411
unsafe impl Send for ModuleLlvm {}

0 commit comments

Comments
 (0)