rustc_codegen_llvm/back/
owned_target_machine.rs

1use std::ffi::CStr;
2use std::marker::PhantomData;
3use std::ptr::NonNull;
4
5use rustc_data_structures::small_c_str::SmallCStr;
6
7use crate::errors::LlvmError;
8use crate::llvm;
9
10/// Responsible for safely creating and disposing llvm::TargetMachine via ffi functions.
11/// Not cloneable as there is no clone function for llvm::TargetMachine.
12#[repr(transparent)]
13pub struct OwnedTargetMachine {
14    tm_unique: NonNull<llvm::TargetMachine>,
15    phantom: PhantomData<llvm::TargetMachine>,
16}
17
18impl OwnedTargetMachine {
19    pub(crate) fn new(
20        triple: &CStr,
21        cpu: &CStr,
22        features: &CStr,
23        abi: &CStr,
24        model: llvm::CodeModel,
25        reloc: llvm::RelocModel,
26        level: llvm::CodeGenOptLevel,
27        float_abi: llvm::FloatAbi,
28        function_sections: bool,
29        data_sections: bool,
30        unique_section_names: bool,
31        trap_unreachable: bool,
32        singlethread: bool,
33        verbose_asm: bool,
34        emit_stack_size_section: bool,
35        relax_elf_relocations: bool,
36        use_init_array: bool,
37        split_dwarf_file: &CStr,
38        output_obj_file: &CStr,
39        debug_info_compression: &CStr,
40        use_emulated_tls: bool,
41        use_wasm_eh: bool,
42    ) -> Result<Self, LlvmError<'static>> {
43        // SAFETY: llvm::LLVMRustCreateTargetMachine copies pointed to data
44        let tm_ptr = unsafe {
45            llvm::LLVMRustCreateTargetMachine(
46                triple.as_ptr(),
47                cpu.as_ptr(),
48                features.as_ptr(),
49                abi.as_ptr(),
50                model,
51                reloc,
52                level,
53                float_abi,
54                function_sections,
55                data_sections,
56                unique_section_names,
57                trap_unreachable,
58                singlethread,
59                verbose_asm,
60                emit_stack_size_section,
61                relax_elf_relocations,
62                use_init_array,
63                split_dwarf_file.as_ptr(),
64                output_obj_file.as_ptr(),
65                debug_info_compression.as_ptr(),
66                use_emulated_tls,
67                use_wasm_eh,
68            )
69        };
70
71        NonNull::new(tm_ptr)
72            .map(|tm_unique| Self { tm_unique, phantom: PhantomData })
73            .ok_or_else(|| LlvmError::CreateTargetMachine { triple: SmallCStr::from(triple) })
74    }
75
76    /// Returns inner `llvm::TargetMachine` type.
77    ///
78    /// This could be a `Deref` implementation, but `llvm::TargetMachine` is an extern type and
79    /// `Deref::Target: ?Sized`.
80    pub fn raw(&self) -> &llvm::TargetMachine {
81        // SAFETY: constructing ensures we have a valid pointer created by
82        // llvm::LLVMRustCreateTargetMachine.
83        unsafe { self.tm_unique.as_ref() }
84    }
85}
86
87impl Drop for OwnedTargetMachine {
88    fn drop(&mut self) {
89        // SAFETY: constructing ensures we have a valid pointer created by
90        // llvm::LLVMRustCreateTargetMachine OwnedTargetMachine is not copyable so there is no
91        // double free or use after free.
92        unsafe { llvm::LLVMDisposeTargetMachine(self.tm_unique) };
93    }
94}