Skip to content

Commit 832fcfb

Browse files
committed
Introduce DIBuilderBox, an owning pointer to DIBuilder
1 parent 854f225 commit 832fcfb

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
931931

932932
unsafe {
933933
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
934-
debug_context.builder,
934+
debug_context.builder.as_ref(),
935935
name_in_debuginfo.as_c_char_ptr(),
936936
name_in_debuginfo.len(),
937937
work_dir.as_c_char_ptr(),
@@ -944,7 +944,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
944944
);
945945

946946
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
947-
debug_context.builder,
947+
debug_context.builder.as_ref(),
948948
dwarf_const::DW_LANG_Rust,
949949
compile_unit_file,
950950
producer.as_c_char_ptr(),

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::builder::Builder;
3434
use crate::common::{AsCCharPtr, CodegenCx};
3535
use crate::llvm;
3636
use crate::llvm::debuginfo::{
37-
DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
37+
DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
3838
DIVariable,
3939
};
4040
use crate::value::Value;
@@ -61,26 +61,18 @@ const DW_TAG_arg_variable: c_uint = 0x101;
6161
/// A context object for maintaining all state needed by the debuginfo module.
6262
pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> {
6363
llmod: &'ll llvm::Module,
64-
builder: &'ll mut DIBuilder<'ll>,
64+
builder: DIBuilderBox<'ll>,
6565
created_files: RefCell<UnordMap<Option<(StableSourceFileId, SourceFileHash)>, &'ll DIFile>>,
6666

6767
type_map: metadata::TypeMap<'ll, 'tcx>,
6868
namespace_map: RefCell<DefIdMap<&'ll DIScope>>,
6969
recursion_marker_type: OnceCell<&'ll DIType>,
7070
}
7171

72-
impl Drop for CodegenUnitDebugContext<'_, '_> {
73-
fn drop(&mut self) {
74-
unsafe {
75-
llvm::LLVMRustDIBuilderDispose(&mut *(self.builder as *mut _));
76-
}
77-
}
78-
}
79-
8072
impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
8173
pub(crate) fn new(llmod: &'ll llvm::Module) -> Self {
8274
debug!("CodegenUnitDebugContext::new");
83-
let builder = unsafe { llvm::LLVMRustDIBuilderCreate(llmod) };
75+
let builder = DIBuilderBox::new(llmod);
8476
// DIBuilder inherits context from the module, so we'd better use the same one
8577
CodegenUnitDebugContext {
8678
llmod,
@@ -93,7 +85,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
9385
}
9486

9587
pub(crate) fn finalize(&self, sess: &Session) {
96-
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder) };
88+
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder.as_ref()) };
9789

9890
match sess.target.debuginfo_kind {
9991
DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => {

compiler/rustc_codegen_llvm/src/debuginfo/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) fn debug_context<'a, 'll, 'tcx>(
4141
#[inline]
4242
#[allow(non_snake_case)]
4343
pub(crate) fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
44-
cx.dbg_cx.as_ref().unwrap().builder
44+
cx.dbg_cx.as_ref().unwrap().builder.as_ref()
4545
}
4646

4747
pub(crate) fn get_namespace_for_item<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -789,12 +789,50 @@ pub type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void
789789
pub type InlineAsmDiagHandlerTy = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
790790

791791
pub mod debuginfo {
792+
use std::ptr;
793+
792794
use bitflags::bitflags;
793795

794796
use super::{InvariantOpaque, Metadata};
797+
use crate::llvm::{self, Module};
795798

799+
/// Opaque target type for references to an LLVM debuginfo builder.
800+
///
801+
/// `&'_ DIBuilder<'ll>` corresponds to `LLVMDIBuilderRef`, which is the
802+
/// LLVM-C wrapper for `DIBuilder *`.
803+
///
804+
/// Debuginfo builders are created and destroyed during codegen, so the
805+
/// builder reference typically has a shorter lifetime than the LLVM
806+
/// session (`'ll`) that it participates in.
796807
#[repr(C)]
797-
pub struct DIBuilder<'a>(InvariantOpaque<'a>);
808+
pub struct DIBuilder<'ll>(InvariantOpaque<'ll>);
809+
810+
/// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
811+
/// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
812+
/// needed for debuginfo FFI calls.
813+
pub(crate) struct DIBuilderBox<'ll> {
814+
raw: ptr::NonNull<DIBuilder<'ll>>,
815+
}
816+
817+
impl<'ll> DIBuilderBox<'ll> {
818+
pub(crate) fn new(llmod: &'ll Module) -> Self {
819+
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
820+
let raw = ptr::NonNull::new(raw).unwrap();
821+
Self { raw }
822+
}
823+
824+
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
825+
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
826+
// for as long as `&self` is.
827+
unsafe { self.raw.as_ref() }
828+
}
829+
}
830+
831+
impl<'ll> Drop for DIBuilderBox<'ll> {
832+
fn drop(&mut self) {
833+
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
834+
}
835+
}
798836

799837
pub type DIDescriptor = Metadata;
800838
pub type DILocation = Metadata;
@@ -1672,6 +1710,13 @@ unsafe extern "C" {
16721710
) -> &'a Value;
16731711
}
16741712

1713+
// FFI bindings for `DIBuilder` functions in the LLVM-C API.
1714+
// Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
1715+
unsafe extern "C" {
1716+
pub(crate) fn LLVMCreateDIBuilder<'ll>(M: &'ll Module) -> *mut DIBuilder<'ll>;
1717+
pub(crate) fn LLVMDisposeDIBuilder<'ll>(Builder: ptr::NonNull<DIBuilder<'ll>>);
1718+
}
1719+
16751720
#[link(name = "llvm-wrapper", kind = "static")]
16761721
unsafe extern "C" {
16771722
pub fn LLVMRustInstallErrorHandlers();
@@ -1939,10 +1984,6 @@ unsafe extern "C" {
19391984
ValueLen: size_t,
19401985
);
19411986

1942-
pub fn LLVMRustDIBuilderCreate(M: &Module) -> &mut DIBuilder<'_>;
1943-
1944-
pub fn LLVMRustDIBuilderDispose<'a>(Builder: &'a mut DIBuilder<'a>);
1945-
19461987
pub fn LLVMRustDIBuilderFinalize(Builder: &DIBuilder<'_>);
19471988

19481989
pub fn LLVMRustDIBuilderCreateCompileUnit<'a>(

0 commit comments

Comments
 (0)