Rollup merge of #146631 - Zalathar:di-builder, r=nnethercote

cg_llvm: Replace some DIBuilder wrappers with LLVM-C API bindings (part 3)

- Part of rust-lang/rust#134001
- Follow-up to rust-lang/rust#136375
- Follow-up to rust-lang/rust#136632

---

This is another batch of LLVMDIBuilder binding migrations, replacing some our own LLVMRust bindings with bindings to upstream LLVM-C APIs.

This PR migrates all of the bindings that were touched by rust-lang/rust#136632, plus `LLVMDIBuilderCreateStructType`.
This commit is contained in:
Stuart Cook 2025-09-17 14:56:49 +10:00 committed by GitHub
commit 4e6640be66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 149 additions and 190 deletions

View file

@ -32,9 +32,7 @@ use self::type_map::{DINodeCreationResult, Stub, UniqueTypeId};
use super::CodegenUnitDebugContext;
use super::namespace::mangled_name_of_instance;
use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
use super::utils::{
DIB, create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit,
};
use super::utils::{DIB, debug_context, get_namespace_for_item, is_node_local_to_unit};
use crate::common::{AsCCharPtr, CodegenCx};
use crate::debuginfo::dwarf_const;
use crate::debuginfo::metadata::type_map::build_type_with_children;
@ -119,17 +117,17 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>(
.try_to_target_usize(cx.tcx)
.expect("expected monomorphic const in codegen") as c_longlong;
let subrange =
unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
let subrange = unsafe { llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) };
let subscripts = &[subrange];
let subscripts = create_DIArray(DIB(cx), &[subrange]);
let di_node = unsafe {
llvm::LLVMRustDIBuilderCreateArrayType(
llvm::LLVMDIBuilderCreateArrayType(
DIB(cx),
size.bits(),
align.bits() as u32,
element_type_di_node,
subscripts,
subscripts.as_ptr(),
subscripts.len() as c_uint,
)
};
@ -175,17 +173,13 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
"ptr_type={ptr_type}, pointee_type={pointee_type}",
);
let di_node = unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
pointer_size.bits(),
pointer_align.abi.bits() as u32,
0, // Ignore DWARF address space.
ptr_type_debuginfo_name.as_c_char_ptr(),
ptr_type_debuginfo_name.len(),
)
};
let di_node = create_pointer_type(
cx,
pointee_type_di_node,
pointer_size,
pointer_align.abi,
&ptr_type_debuginfo_name,
);
DINodeCreationResult { di_node, already_stored_in_typemap: false }
}
@ -233,17 +227,13 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
// The data pointer type is a regular, thin pointer, regardless of whether this
// is a slice or a trait object.
let data_ptr_type_di_node = unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
addr_field.size.bits(),
addr_field.align.abi.bits() as u32,
0, // Ignore DWARF address space.
std::ptr::null(),
0,
)
};
let data_ptr_type_di_node = create_pointer_type(
cx,
pointee_type_di_node,
addr_field.size,
addr_field.align.abi,
"",
);
smallvec![
build_field_di_node(
@ -318,7 +308,7 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
let fn_di_node = create_subroutine_type(cx, create_DIArray(DIB(cx), &signature_di_nodes[..]));
let fn_di_node = create_subroutine_type(cx, &signature_di_nodes[..]);
// This is actually a function pointer, so wrap it in pointer DI.
let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
@ -329,26 +319,44 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
}
_ => unreachable!(),
};
let di_node = unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
fn_di_node,
size.bits(),
align.bits() as u32,
0, // Ignore DWARF address space.
name.as_c_char_ptr(),
name.len(),
)
};
let di_node = create_pointer_type(cx, fn_di_node, size, align, &name);
DINodeCreationResult::new(di_node, false)
}
pub(super) fn create_subroutine_type<'ll>(
cx: &CodegenCx<'ll, '_>,
signature: &'ll DICompositeType,
signature: &[Option<&'ll llvm::Metadata>],
) -> &'ll DICompositeType {
unsafe { llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(cx), signature) }
unsafe {
llvm::LLVMDIBuilderCreateSubroutineType(
DIB(cx),
None, // ("File" is ignored and has no effect)
signature.as_ptr(),
signature.len() as c_uint,
DIFlags::FlagZero, // (default value)
)
}
}
fn create_pointer_type<'ll>(
cx: &CodegenCx<'ll, '_>,
pointee_ty: &'ll llvm::Metadata,
size: Size,
align: Align,
name: &str,
) -> &'ll llvm::Metadata {
unsafe {
llvm::LLVMDIBuilderCreatePointerType(
DIB(cx),
pointee_ty,
size.bits(),
align.bits() as u32,
0, // Ignore DWARF address space.
name.as_ptr(),
name.len(),
)
}
}
/// Create debuginfo for `dyn SomeTrait` types. Currently these are empty structs
@ -834,12 +842,13 @@ fn create_basic_type<'ll, 'tcx>(
encoding: u32,
) -> &'ll DIBasicType {
unsafe {
llvm::LLVMRustDIBuilderCreateBasicType(
llvm::LLVMDIBuilderCreateBasicType(
DIB(cx),
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
size.bits(),
encoding,
DIFlags::FlagZero,
)
}
}

View file

@ -1,5 +1,6 @@
use std::cell::RefCell;
use libc::c_uint;
use rustc_abi::{Align, Size, VariantIdx};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
@ -9,7 +10,7 @@ use rustc_middle::bug;
use rustc_middle::ty::{self, ExistentialTraitRef, Ty, TyCtxt};
use super::{DefinitionLocation, SmallVec, UNKNOWN_LINE_NUMBER, unknown_file_metadata};
use crate::common::{AsCCharPtr, CodegenCx};
use crate::common::CodegenCx;
use crate::debuginfo::utils::{DIB, create_DIArray, debug_context};
use crate::llvm::debuginfo::{DIFlags, DIScope, DIType};
use crate::llvm::{self};
@ -191,7 +192,7 @@ pub(super) fn stub<'ll, 'tcx>(
containing_scope: Option<&'ll DIScope>,
flags: DIFlags,
) -> StubInfo<'ll, 'tcx> {
let empty_array = create_DIArray(DIB(cx), &[]);
let no_elements: &[Option<&llvm::Metadata>] = &[];
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
let (file_metadata, line_number) = if let Some(def_location) = def_location {
@ -207,10 +208,10 @@ pub(super) fn stub<'ll, 'tcx>(
_ => None,
};
unsafe {
llvm::LLVMRustDIBuilderCreateStructType(
llvm::LLVMDIBuilderCreateStructType(
DIB(cx),
containing_scope,
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
file_metadata,
line_number,
@ -218,28 +219,30 @@ pub(super) fn stub<'ll, 'tcx>(
align.bits() as u32,
flags,
None,
empty_array,
0,
no_elements.as_ptr(),
no_elements.len() as c_uint,
0u32, // (Objective-C runtime version; default is 0)
vtable_holder,
unique_type_id_str.as_c_char_ptr(),
unique_type_id_str.as_ptr(),
unique_type_id_str.len(),
)
}
}
Stub::Union => unsafe {
llvm::LLVMRustDIBuilderCreateUnionType(
llvm::LLVMDIBuilderCreateUnionType(
DIB(cx),
containing_scope,
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
file_metadata,
line_number,
size.bits(),
align.bits() as u32,
flags,
Some(empty_array),
0,
unique_type_id_str.as_c_char_ptr(),
no_elements.as_ptr(),
no_elements.len() as c_uint,
0u32, // (Objective-C runtime version; default is 0)
unique_type_id_str.as_ptr(),
unique_type_id_str.len(),
)
},

View file

@ -349,7 +349,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
let file_metadata = file_metadata(self, &loc.file);
let function_type_metadata =
create_subroutine_type(self, get_function_signature(self, fn_abi));
create_subroutine_type(self, &get_function_signature(self, fn_abi));
let mut name = String::with_capacity(64);
type_names::push_item_name(tcx, def_id, false, &mut name);
@ -441,9 +441,9 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
fn get_function_signature<'ll, 'tcx>(
cx: &CodegenCx<'ll, 'tcx>,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
) -> &'ll DIArray {
) -> Vec<Option<&'ll llvm::Metadata>> {
if cx.sess().opts.debuginfo != DebugInfo::Full {
return create_DIArray(DIB(cx), &[]);
return vec![];
}
let mut signature = Vec::with_capacity(fn_abi.args.len() + 1);
@ -484,7 +484,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
.extend(fn_abi.args.iter().map(|arg| Some(type_di_node(cx, arg.layout.ty))));
}
create_DIArray(DIB(cx), &signature[..])
signature
}
fn get_template_parameters<'ll, 'tcx>(

View file

@ -24,9 +24,9 @@ use rustc_target::spec::SymbolVisibility;
use super::RustString;
use super::debuginfo::{
DIArray, DIBasicType, DIBuilder, DICompositeType, DIDerivedType, DIDescriptor, DIEnumerator,
DIFile, DIFlags, DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
DISubrange, DITemplateTypeParameter, DIType, DIVariable, DebugEmissionKind, DebugNameTableKind,
DIArray, DIBasicType, DIBuilder, DIDerivedType, DIDescriptor, DIEnumerator, DIFile, DIFlags,
DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram, DISubrange,
DITemplateTypeParameter, DIType, DIVariable, DebugEmissionKind, DebugNameTableKind,
};
use crate::llvm;
@ -1871,6 +1871,78 @@ unsafe extern "C" {
Scope: &'ll Metadata,
InlinedAt: Option<&'ll Metadata>,
) -> &'ll Metadata;
pub(crate) fn LLVMDIBuilderCreateSubroutineType<'ll>(
Builder: &DIBuilder<'ll>,
File: Option<&'ll Metadata>, // (ignored and has no effect)
ParameterTypes: *const Option<&'ll Metadata>,
NumParameterTypes: c_uint,
Flags: DIFlags, // (default is `DIFlags::DIFlagZero`)
) -> &'ll Metadata;
pub(crate) fn LLVMDIBuilderCreateUnionType<'ll>(
Builder: &DIBuilder<'ll>,
Scope: Option<&'ll Metadata>,
Name: *const c_uchar, // See "PTR_LEN_STR".
NameLen: size_t,
File: &'ll Metadata,
LineNumber: c_uint,
SizeInBits: u64,
AlignInBits: u32,
Flags: DIFlags,
Elements: *const Option<&'ll Metadata>,
NumElements: c_uint,
RunTimeLang: c_uint, // (optional Objective-C runtime version; default is 0)
UniqueId: *const c_uchar, // See "PTR_LEN_STR".
UniqueIdLen: size_t,
) -> &'ll Metadata;
pub(crate) fn LLVMDIBuilderCreateArrayType<'ll>(
Builder: &DIBuilder<'ll>,
Size: u64,
Align: u32,
Ty: &'ll Metadata,
Subscripts: *const &'ll Metadata,
NumSubscripts: c_uint,
) -> &'ll Metadata;
pub(crate) fn LLVMDIBuilderCreateBasicType<'ll>(
Builder: &DIBuilder<'ll>,
Name: *const c_uchar, // See "PTR_LEN_STR".
NameLen: size_t,
SizeInBits: u64,
Encoding: c_uint, // (`LLVMDWARFTypeEncoding`)
Flags: DIFlags, // (default is `DIFlags::DIFlagZero`)
) -> &'ll Metadata;
pub(crate) fn LLVMDIBuilderCreatePointerType<'ll>(
Builder: &DIBuilder<'ll>,
PointeeTy: &'ll Metadata,
SizeInBits: u64,
AlignInBits: u32,
AddressSpace: c_uint, // (optional DWARF address space; default is 0)
Name: *const c_uchar, // See "PTR_LEN_STR".
NameLen: size_t,
) -> &'ll Metadata;
pub(crate) fn LLVMDIBuilderCreateStructType<'ll>(
Builder: &DIBuilder<'ll>,
Scope: Option<&'ll Metadata>,
Name: *const c_uchar, // See "PTR_LEN_STR".
NameLen: size_t,
File: &'ll Metadata,
LineNumber: c_uint,
SizeInBits: u64,
AlignInBits: u32,
Flags: DIFlags,
DerivedFrom: Option<&'ll Metadata>,
Elements: *const Option<&'ll Metadata>,
NumElements: c_uint,
RunTimeLang: c_uint, // (optional Objective-C runtime version; default is 0)
VTableHolder: Option<&'ll Metadata>,
UniqueId: *const c_uchar, // See "PTR_LEN_STR".
UniqueIdLen: size_t,
) -> &'ll Metadata;
}
#[link(name = "llvm-wrapper", kind = "static")]
@ -2173,11 +2245,6 @@ unsafe extern "C" {
SourceLen: size_t,
) -> &'a DIFile;
pub(crate) fn LLVMRustDIBuilderCreateSubroutineType<'a>(
Builder: &DIBuilder<'a>,
ParameterTypes: &'a DIArray,
) -> &'a DICompositeType;
pub(crate) fn LLVMRustDIBuilderCreateFunction<'a>(
Builder: &DIBuilder<'a>,
Scope: &'a DIDescriptor,
@ -2211,14 +2278,6 @@ unsafe extern "C" {
TParam: &'a DIArray,
) -> &'a DISubprogram;
pub(crate) fn LLVMRustDIBuilderCreateBasicType<'a>(
Builder: &DIBuilder<'a>,
Name: *const c_char,
NameLen: size_t,
SizeInBits: u64,
Encoding: c_uint,
) -> &'a DIBasicType;
pub(crate) fn LLVMRustDIBuilderCreateTypedef<'a>(
Builder: &DIBuilder<'a>,
Type: &'a DIBasicType,
@ -2229,34 +2288,6 @@ unsafe extern "C" {
Scope: Option<&'a DIScope>,
) -> &'a DIDerivedType;
pub(crate) fn LLVMRustDIBuilderCreatePointerType<'a>(
Builder: &DIBuilder<'a>,
PointeeTy: &'a DIType,
SizeInBits: u64,
AlignInBits: u32,
AddressSpace: c_uint,
Name: *const c_char,
NameLen: size_t,
) -> &'a DIDerivedType;
pub(crate) fn LLVMRustDIBuilderCreateStructType<'a>(
Builder: &DIBuilder<'a>,
Scope: Option<&'a DIDescriptor>,
Name: *const c_char,
NameLen: size_t,
File: &'a DIFile,
LineNumber: c_uint,
SizeInBits: u64,
AlignInBits: u32,
Flags: DIFlags,
DerivedFrom: Option<&'a DIType>,
Elements: &'a DIArray,
RunTimeLang: c_uint,
VTableHolder: Option<&'a DIType>,
UniqueId: *const c_char,
UniqueIdLen: size_t,
) -> &'a DICompositeType;
pub(crate) fn LLVMRustDIBuilderCreateMemberType<'a>(
Builder: &DIBuilder<'a>,
Scope: &'a DIDescriptor,
@ -2336,14 +2367,6 @@ unsafe extern "C" {
AlignInBits: u32,
) -> &'a DIVariable;
pub(crate) fn LLVMRustDIBuilderCreateArrayType<'a>(
Builder: &DIBuilder<'a>,
Size: u64,
AlignInBits: u32,
Ty: &'a DIType,
Subscripts: &'a DIArray,
) -> &'a DIType;
pub(crate) fn LLVMRustDIBuilderGetOrCreateSubrange<'a>(
Builder: &DIBuilder<'a>,
Lo: i64,
@ -2389,22 +2412,6 @@ unsafe extern "C" {
IsScoped: bool,
) -> &'a DIType;
pub(crate) fn LLVMRustDIBuilderCreateUnionType<'a>(
Builder: &DIBuilder<'a>,
Scope: Option<&'a DIScope>,
Name: *const c_char,
NameLen: size_t,
File: &'a DIFile,
LineNumber: c_uint,
SizeInBits: u64,
AlignInBits: u32,
Flags: DIFlags,
Elements: Option<&'a DIArray>,
RunTimeLang: c_uint,
UniqueId: *const c_char,
UniqueIdLen: size_t,
) -> &'a DIType;
pub(crate) fn LLVMRustDIBuilderCreateVariantPart<'a>(
Builder: &DIBuilder<'a>,
Scope: &'a DIScope,

View file

@ -1026,13 +1026,6 @@ LLVMRustDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
CSInfo, oSource));
}
extern "C" LLVMMetadataRef
LLVMRustDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
LLVMMetadataRef ParameterTypes) {
return wrap(unwrap(Builder)->createSubroutineType(
DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
}
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFunction(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
@ -1071,14 +1064,6 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateMethod(
return wrap(Sub);
}
extern "C" LLVMMetadataRef
LLVMRustDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
size_t NameLen, uint64_t SizeInBits,
unsigned Encoding) {
return wrap(unwrap(Builder)->createBasicType(StringRef(Name, NameLen),
SizeInBits, Encoding));
}
extern "C" LLVMMetadataRef
LLVMRustDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
const char *Name, size_t NameLen,
@ -1089,29 +1074,6 @@ LLVMRustDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
LineNo, unwrapDIPtr<DIScope>(Scope)));
}
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreatePointerType(
LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy, uint64_t SizeInBits,
uint32_t AlignInBits, unsigned AddressSpace, const char *Name,
size_t NameLen) {
return wrap(unwrap(Builder)->createPointerType(
unwrapDI<DIType>(PointeeTy), SizeInBits, AlignInBits, AddressSpace,
StringRef(Name, NameLen)));
}
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateStructType(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
LLVMMetadataRef DerivedFrom, LLVMMetadataRef Elements, unsigned RunTimeLang,
LLVMMetadataRef VTableHolder, const char *UniqueId, size_t UniqueIdLen) {
return wrap(unwrap(Builder)->createStructType(
unwrapDI<DIDescriptor>(Scope), StringRef(Name, NameLen),
unwrapDI<DIFile>(File), LineNumber, SizeInBits, AlignInBits,
fromRust(Flags), unwrapDI<DIType>(DerivedFrom),
DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang,
unwrapDI<DIType>(VTableHolder), StringRef(UniqueId, UniqueIdLen)));
}
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariantPart(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
@ -1218,15 +1180,6 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariable(
}
}
extern "C" LLVMMetadataRef
LLVMRustDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
uint32_t AlignInBits, LLVMMetadataRef Ty,
LLVMMetadataRef Subscripts) {
return wrap(unwrap(Builder)->createArrayType(
Size, AlignInBits, unwrapDI<DIType>(Ty),
DINodeArray(unwrapDI<MDTuple>(Subscripts))));
}
extern "C" LLVMMetadataRef
LLVMRustDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder, int64_t Lo,
int64_t Count) {
@ -1275,19 +1228,6 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateEnumerationType(
/* RunTimeLang */ 0, "", IsScoped));
}
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateUnionType(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
LLVMMetadataRef Elements, unsigned RunTimeLang, const char *UniqueId,
size_t UniqueIdLen) {
return wrap(unwrap(Builder)->createUnionType(
unwrapDI<DIDescriptor>(Scope), StringRef(Name, NameLen),
unwrapDI<DIFile>(File), LineNumber, SizeInBits, AlignInBits,
fromRust(Flags), DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang,
StringRef(UniqueId, UniqueIdLen)));
}
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateTemplateTypeParameter(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
size_t NameLen, LLVMMetadataRef Ty) {