Add OwnedTargetMachine to manage llvm:TargetMachine. Uses pointers

instead of &'static mut and provides safe interface to create/dispose
it.
This commit is contained in:
Florian Schmiderer 2023-09-17 14:40:22 +02:00
parent fc61fabc24
commit 3409ca65d8
7 changed files with 148 additions and 40 deletions

View file

@ -22,6 +22,7 @@ extern crate rustc_macros;
#[macro_use]
extern crate tracing;
use back::owned_target_machine::OwnedTargetMachine;
use back::write::{create_informational_target_machine, create_target_machine};
use errors::ParseTargetMachineConfig;
@ -52,6 +53,7 @@ use std::io::Write;
mod back {
pub mod archive;
pub mod lto;
pub mod owned_target_machine;
mod profiling;
pub mod write;
}
@ -162,7 +164,7 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
impl WriteBackendMethods for LlvmCodegenBackend {
type Module = ModuleLlvm;
type ModuleBuffer = back::lto::ModuleBuffer;
type TargetMachine = &'static mut llvm::TargetMachine;
type TargetMachine = OwnedTargetMachine;
type TargetMachineError = crate::errors::LlvmError<'static>;
type ThinData = back::lto::ThinData;
type ThinBuffer = back::lto::ThinBuffer;
@ -401,7 +403,9 @@ impl CodegenBackend for LlvmCodegenBackend {
pub struct ModuleLlvm {
llcx: &'static mut llvm::Context,
llmod_raw: *const llvm::Module,
tm: &'static mut llvm::TargetMachine,
// independent from llcx and llmod_raw, resources get disposed by drop impl
tm: OwnedTargetMachine,
}
unsafe impl Send for ModuleLlvm {}
@ -453,7 +457,6 @@ impl ModuleLlvm {
impl Drop for ModuleLlvm {
fn drop(&mut self) {
unsafe {
llvm::LLVMRustDisposeTargetMachine(&mut *(self.tm as *mut _));
llvm::LLVMContextDispose(&mut *(self.llcx as *mut _));
}
}