Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cg_llvm: Replace most of our DIBuilder wrappers with LLVM-C API bindings #134009

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1d8fe72
Make our `DIFlags` match `LLVMDIFlags` in the LLVM-C API
Zalathar Dec 7, 2024
d759865
Introduce `DIBuilderBox`, an owning pointer to `DIBuilder`
Zalathar Dec 7, 2024
362b4aa
Use `LLVMDIBuilderFinalize`
Zalathar Dec 7, 2024
aa9417e
Use `LLVMDIBuilderCreateNameSpace`
Zalathar Dec 7, 2024
8f346a4
Use `LLVMDIBuilderCreateLexicalBlock`
Zalathar Dec 7, 2024
d4c0568
Use `LLVMDIBuilderCreateLexicalBlockFile`
Zalathar Dec 7, 2024
2d3b7b7
Use `LLVMDIBuilderCreateDebugLocation`
Zalathar Dec 7, 2024
ed9a4d2
Use `LLVMDIBuilderCreateSubroutineType`
Zalathar Dec 7, 2024
71e8169
Use `LLVMDIBuilderCreateUnionType`
Zalathar Dec 7, 2024
6230db2
Use `LLVMDIBuilderCreateArrayType`
Zalathar Dec 7, 2024
6596c94
Use `LLVMDIBuilderCreateBasicType`
Zalathar Dec 7, 2024
50194be
Use `LLVMDIBuilderCreatePointerType`
Zalathar Dec 7, 2024
3be1ccb
Use `LLVMDIBuilderCreateStructType`
Zalathar Dec 7, 2024
1629274
Use `LLVMDIBuilderCreateMemberType`
Zalathar Dec 7, 2024
86d7061
Use `LLVMDIBuilderCreateStaticMemberType`
Zalathar Dec 7, 2024
ef9cf38
Use `LLVMDIBuilderCreateTypedef`
Zalathar Dec 7, 2024
c04657f
Use `LLVMDIBuilderGetOrCreateSubrange`
Zalathar Dec 7, 2024
7e5ed45
Use `LLVMDIBuilderGetOrCreateArray`
Zalathar Dec 7, 2024
1a3ef56
Use `LLVMDIBuilderCreateExpression` and adjust our insert function
Zalathar Dec 7, 2024
a740567
Use constants for DWARF opcodes, instead of FFI calls
Zalathar Dec 8, 2024
b3fc61f
Properly unwrap `LLVMDIBuilderRef` in the remaining wrapper functions
Zalathar Dec 7, 2024
057f238
Remove `LLVMRustDIFlags`
Zalathar Dec 7, 2024
505665a
Add FIXME for auditing optional parameters passed to DIBuilder
Zalathar Dec 7, 2024
1837dbe
Explain why the remaining `LLVMRustDIBuilder*` wrappers exist
Zalathar Dec 8, 2024
d6297f8
Explain why (some) pointer/length strings are `*const c_uchar`
Zalathar Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn make_mir_scope<'ll, 'tcx>(
})
}
None => unsafe {
llvm::LLVMRustDIBuilderCreateLexicalBlock(
llvm::LLVMDIBuilderCreateLexicalBlock(
DIB(cx),
parent_scope.dbg_scope,
file_metadata,
Expand Down
80 changes: 48 additions & 32 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::{iter, ptr};

use libc::{c_char, c_longlong, c_uint};
use libc::{c_char, c_uint};
use rustc_abi::{Align, Size};
use rustc_codegen_ssa::debuginfo::type_names::{VTableNameKind, cpp_like_debuginfo};
use rustc_codegen_ssa::traits::*;
Expand All @@ -27,9 +27,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::metadata::type_map::build_type_with_children;
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
Expand Down Expand Up @@ -123,21 +121,26 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>(

let (size, align) = cx.size_and_align_of(array_type);

let upper_bound = len
.try_to_target_usize(cx.tcx)
.expect("expected monomorphic const in codegen") as c_longlong;
let upper_bound =
len.try_to_target_usize(cx.tcx).expect("expected monomorphic const in codegen");

let subrange =
unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
let subrange = unsafe {
llvm::LLVMDIBuilderGetOrCreateSubrange(
DIB(cx),
/* LowerBound */ 0i64,
/* Count */ upper_bound as i64,
Comment on lines +130 to +131
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have to be positive numbers, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVM declares them as int64_t, in both the C and C++ APIs. 🤷‍♂️

Or are you suggesting that we assert that they're non-negative?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am indeed suggesting that.

)
};

let subscripts = create_DIArray(DIB(cx), &[subrange]);
let subscripts = &[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,
)
};

Expand Down Expand Up @@ -182,13 +185,13 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
);

let di_node = unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
llvm::LLVMDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
data_layout.pointer_size.bits(),
data_layout.pointer_align.abi.bits() as u32,
0, // Ignore DWARF address space.
ptr_type_debuginfo_name.as_c_char_ptr(),
ptr_type_debuginfo_name.as_ptr(),
ptr_type_debuginfo_name.len(),
)
};
Expand Down Expand Up @@ -240,7 +243,7 @@ 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(
llvm::LLVMDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
addr_field.size.bits(),
Expand Down Expand Up @@ -325,9 +328,12 @@ 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 = unsafe {
llvm::LLVMRustDIBuilderCreateSubroutineType(
llvm::LLVMDIBuilderCreateSubroutineType(
DIB(cx),
create_DIArray(DIB(cx), &signature_di_nodes[..]),
/* File (unused) */ None,
signature_di_nodes.as_ptr(),
signature_di_nodes.len() as c_uint,
DIFlags::FlagZero,
)
};

Expand All @@ -342,13 +348,13 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
_ => unreachable!(),
};
let di_node = unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
llvm::LLVMDIBuilderCreatePointerType(
DIB(cx),
fn_di_node,
size,
align,
0, // Ignore DWARF address space.
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
)
};
Expand Down Expand Up @@ -515,12 +521,13 @@ fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll D
// FIXME: it might make sense to use an actual pointer type here
// so that debuggers can show the address.
let name = "<recur_type>";
llvm::LLVMRustDIBuilderCreateBasicType(
llvm::LLVMDIBuilderCreateBasicType(
DIB(cx),
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
cx.tcx.data_layout.pointer_size.bits(),
DW_ATE_unsigned,
DIFlags::FlagZero,
)
}
})
Expand Down Expand Up @@ -803,12 +810,13 @@ fn build_basic_type_di_node<'ll, 'tcx>(
};

let ty_di_node = unsafe {
llvm::LLVMRustDIBuilderCreateBasicType(
llvm::LLVMDIBuilderCreateBasicType(
DIB(cx),
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
cx.size_of(t).bits(),
encoding,
DIFlags::FlagZero,
)
};

Expand All @@ -824,14 +832,15 @@ fn build_basic_type_di_node<'ll, 'tcx>(
};

let typedef_di_node = unsafe {
llvm::LLVMRustDIBuilderCreateTypedef(
llvm::LLVMDIBuilderCreateTypedef(
DIB(cx),
ty_di_node,
typedef_name.as_c_char_ptr(),
typedef_name.as_ptr(),
typedef_name.len(),
unknown_file_metadata(cx),
0,
None,
/* LineNo */ 0u32,
/* Scope */ None,
/* AlignInBits (optional) */ 0u32,
)
};

Expand Down Expand Up @@ -945,7 +954,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(

unsafe {
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
debug_context.builder,
debug_context.builder.as_ref(),
name_in_debuginfo.as_c_char_ptr(),
name_in_debuginfo.len(),
work_dir.as_c_char_ptr(),
Expand All @@ -958,7 +967,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
);

let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
debug_context.builder,
debug_context.builder.as_ref(),
DW_LANG_RUST,
compile_unit_file,
producer.as_c_char_ptr(),
Expand Down Expand Up @@ -999,10 +1008,10 @@ fn build_field_di_node<'ll, 'tcx>(
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
};
unsafe {
llvm::LLVMRustDIBuilderCreateMemberType(
llvm::LLVMDIBuilderCreateMemberType(
DIB(cx),
owner,
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
file_metadata,
line_number,
Expand Down Expand Up @@ -1630,7 +1639,14 @@ pub(crate) fn extend_scope_to_file<'ll>(
file: &SourceFile,
) -> &'ll DILexicalBlock {
let file_metadata = file_metadata(cx, file);
unsafe { llvm::LLVMRustDIBuilderCreateLexicalBlockFile(DIB(cx), scope_metadata, file_metadata) }
unsafe {
llvm::LLVMDIBuilderCreateLexicalBlockFile(
DIB(cx),
scope_metadata,
file_metadata,
/* Discriminator (default) */ 0u32,
)
}
}

fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
use rustc_middle::ty::{self, AdtDef, CoroutineArgs, CoroutineArgsExt, Ty};
use smallvec::smallvec;

use crate::common::{AsCCharPtr, CodegenCx};
use crate::common::CodegenCx;
use crate::debuginfo::metadata::enums::DiscrResult;
use crate::debuginfo::metadata::type_map::{self, Stub, UniqueTypeId};
use crate::debuginfo::metadata::{
Expand Down Expand Up @@ -381,10 +381,10 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
None,
),
unsafe {
llvm::LLVMRustDIBuilderCreateStaticMemberType(
llvm::LLVMDIBuilderCreateStaticMemberType(
DIB(cx),
enum_type_di_node,
TAG_FIELD_NAME.as_c_char_ptr(),
TAG_FIELD_NAME.as_ptr(),
TAG_FIELD_NAME.len(),
unknown_file_metadata(cx),
UNKNOWN_LINE_NUMBER,
Expand Down Expand Up @@ -571,10 +571,10 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(

let build_assoc_const =
|name: &str, type_di_node: &'ll DIType, value: u64, align: Align| unsafe {
llvm::LLVMRustDIBuilderCreateStaticMemberType(
llvm::LLVMDIBuilderCreateStaticMemberType(
DIB(cx),
wrapper_struct_type_di_node,
name.as_c_char_ptr(),
name.as_ptr(),
name.len(),
unknown_file_metadata(cx),
UNKNOWN_LINE_NUMBER,
Expand Down Expand Up @@ -829,10 +829,10 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
// the build_field_di_node() function does not support specifying a source location,
// which is something that we don't do anywhere else.
unsafe {
llvm::LLVMRustDIBuilderCreateMemberType(
llvm::LLVMDIBuilderCreateMemberType(
DIB(cx),
enum_type_di_node,
field_name.as_c_char_ptr(),
field_name.as_ptr(),
field_name.len(),
file_di_node,
line_number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ fn build_discr_member_di_node<'ll, 'tcx>(
let (size, align) = cx.size_and_align_of(tag_base_type);

unsafe {
Some(llvm::LLVMRustDIBuilderCreateMemberType(
Some(llvm::LLVMDIBuilderCreateMemberType(
DIB(cx),
containing_scope,
tag_name.as_c_char_ptr(),
tag_name.as_ptr(),
tag_name.len(),
unknown_file_metadata(cx),
UNKNOWN_LINE_NUMBER,
Expand Down
27 changes: 14 additions & 13 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_middle::bug;
use rustc_middle::ty::{self, PolyExistentialTraitRef, 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};
Expand Down Expand Up @@ -191,7 +191,6 @@ pub(super) fn stub<'ll, 'tcx>(
containing_scope: Option<&'ll DIScope>,
flags: DIFlags,
) -> StubInfo<'ll, 'tcx> {
let empty_array = create_DIArray(DIB(cx), &[]);
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 {
Expand All @@ -207,39 +206,41 @@ 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,
size.bits(),
align.bits() as u32,
flags,
None,
empty_array,
0,
/* DerivedFrom */ None,
/* Elements */ (&[]).as_ptr(),
/* NumElements */ 0u32,
/* RunTimeLang */ 0u32,
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(),
/* Elements */ (&[]).as_ptr(),
/* NumElements */ 0u32,
/* RunTimeLang */ 0u32,
unique_type_id_str.as_ptr(),
unique_type_id_str.len(),
)
},
Expand Down
Loading
Loading