Skip to content

Commit 3e30f53

Browse files
committed
Auto merge of #134009 - Zalathar:llvm-di, r=<try>
cg_llvm: Replace most of our DIBuilder wrappers with LLVM-C API bindings Many of our `LLVMRust` wrapper functions for building debug info can be replaced with their equivalents in the LLVM-C API. This mostly implements #134001, though it punts on some of the trickier cases noted at #134001 (comment). --- Marking as draft for now, because I want to give myself more time to review my own changes. r? workingjubilee
2 parents 1daec06 + 23c29e8 commit 3e30f53

File tree

10 files changed

+453
-570
lines changed

10 files changed

+453
-570
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn make_mir_scope<'ll, 'tcx>(
135135
})
136136
}
137137
None => unsafe {
138-
llvm::LLVMRustDIBuilderCreateLexicalBlock(
138+
llvm::LLVMDIBuilderCreateLexicalBlock(
139139
DIB(cx),
140140
parent_scope.dbg_scope,
141141
file_metadata,

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+48-32
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
44
use std::path::{Path, PathBuf};
55
use std::{iter, ptr};
66

7-
use libc::{c_char, c_longlong, c_uint};
7+
use libc::{c_char, c_uint};
88
use rustc_abi::{Align, Size};
99
use rustc_codegen_ssa::debuginfo::type_names::{VTableNameKind, cpp_like_debuginfo};
1010
use rustc_codegen_ssa::traits::*;
@@ -27,9 +27,7 @@ use self::type_map::{DINodeCreationResult, Stub, UniqueTypeId};
2727
use super::CodegenUnitDebugContext;
2828
use super::namespace::mangled_name_of_instance;
2929
use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
30-
use super::utils::{
31-
DIB, create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit,
32-
};
30+
use super::utils::{DIB, debug_context, get_namespace_for_item, is_node_local_to_unit};
3331
use crate::common::{AsCCharPtr, CodegenCx};
3432
use crate::debuginfo::metadata::type_map::build_type_with_children;
3533
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
@@ -123,21 +121,26 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>(
123121

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

126-
let upper_bound = len
127-
.try_to_target_usize(cx.tcx)
128-
.expect("expected monomorphic const in codegen") as c_longlong;
124+
let upper_bound =
125+
len.try_to_target_usize(cx.tcx).expect("expected monomorphic const in codegen");
129126

130-
let subrange =
131-
unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
127+
let subrange = unsafe {
128+
llvm::LLVMDIBuilderGetOrCreateSubrange(
129+
DIB(cx),
130+
/* LowerBound */ 0i64,
131+
/* Count */ upper_bound as i64,
132+
)
133+
};
132134

133-
let subscripts = create_DIArray(DIB(cx), &[subrange]);
135+
let subscripts = &[subrange];
134136
let di_node = unsafe {
135-
llvm::LLVMRustDIBuilderCreateArrayType(
137+
llvm::LLVMDIBuilderCreateArrayType(
136138
DIB(cx),
137139
size.bits(),
138140
align.bits() as u32,
139141
element_type_di_node,
140-
subscripts,
142+
subscripts.as_ptr(),
143+
subscripts.len() as c_uint,
141144
)
142145
};
143146

@@ -182,13 +185,13 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
182185
);
183186

184187
let di_node = unsafe {
185-
llvm::LLVMRustDIBuilderCreatePointerType(
188+
llvm::LLVMDIBuilderCreatePointerType(
186189
DIB(cx),
187190
pointee_type_di_node,
188191
data_layout.pointer_size.bits(),
189192
data_layout.pointer_align.abi.bits() as u32,
190193
0, // Ignore DWARF address space.
191-
ptr_type_debuginfo_name.as_c_char_ptr(),
194+
ptr_type_debuginfo_name.as_ptr(),
192195
ptr_type_debuginfo_name.len(),
193196
)
194197
};
@@ -240,7 +243,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
240243
// The data pointer type is a regular, thin pointer, regardless of whether this
241244
// is a slice or a trait object.
242245
let data_ptr_type_di_node = unsafe {
243-
llvm::LLVMRustDIBuilderCreatePointerType(
246+
llvm::LLVMDIBuilderCreatePointerType(
244247
DIB(cx),
245248
pointee_type_di_node,
246249
addr_field.size.bits(),
@@ -325,9 +328,12 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
325328
debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
326329

327330
let fn_di_node = unsafe {
328-
llvm::LLVMRustDIBuilderCreateSubroutineType(
331+
llvm::LLVMDIBuilderCreateSubroutineType(
329332
DIB(cx),
330-
create_DIArray(DIB(cx), &signature_di_nodes[..]),
333+
/* File (unused) */ None,
334+
signature_di_nodes.as_ptr(),
335+
signature_di_nodes.len() as c_uint,
336+
DIFlags::FlagZero,
331337
)
332338
};
333339

@@ -342,13 +348,13 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
342348
_ => unreachable!(),
343349
};
344350
let di_node = unsafe {
345-
llvm::LLVMRustDIBuilderCreatePointerType(
351+
llvm::LLVMDIBuilderCreatePointerType(
346352
DIB(cx),
347353
fn_di_node,
348354
size,
349355
align,
350356
0, // Ignore DWARF address space.
351-
name.as_c_char_ptr(),
357+
name.as_ptr(),
352358
name.len(),
353359
)
354360
};
@@ -515,12 +521,13 @@ fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll D
515521
// FIXME: it might make sense to use an actual pointer type here
516522
// so that debuggers can show the address.
517523
let name = "<recur_type>";
518-
llvm::LLVMRustDIBuilderCreateBasicType(
524+
llvm::LLVMDIBuilderCreateBasicType(
519525
DIB(cx),
520-
name.as_c_char_ptr(),
526+
name.as_ptr(),
521527
name.len(),
522528
cx.tcx.data_layout.pointer_size.bits(),
523529
DW_ATE_unsigned,
530+
DIFlags::FlagZero,
524531
)
525532
}
526533
})
@@ -803,12 +810,13 @@ fn build_basic_type_di_node<'ll, 'tcx>(
803810
};
804811

805812
let ty_di_node = unsafe {
806-
llvm::LLVMRustDIBuilderCreateBasicType(
813+
llvm::LLVMDIBuilderCreateBasicType(
807814
DIB(cx),
808-
name.as_c_char_ptr(),
815+
name.as_ptr(),
809816
name.len(),
810817
cx.size_of(t).bits(),
811818
encoding,
819+
DIFlags::FlagZero,
812820
)
813821
};
814822

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

826834
let typedef_di_node = unsafe {
827-
llvm::LLVMRustDIBuilderCreateTypedef(
835+
llvm::LLVMDIBuilderCreateTypedef(
828836
DIB(cx),
829837
ty_di_node,
830-
typedef_name.as_c_char_ptr(),
838+
typedef_name.as_ptr(),
831839
typedef_name.len(),
832840
unknown_file_metadata(cx),
833-
0,
834-
None,
841+
/* LineNo */ 0u32,
842+
/* Scope */ None,
843+
/* AlignInBits (optional) */ 0u32,
835844
)
836845
};
837846

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

946955
unsafe {
947956
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
948-
debug_context.builder,
957+
debug_context.builder.as_ref(),
949958
name_in_debuginfo.as_c_char_ptr(),
950959
name_in_debuginfo.len(),
951960
work_dir.as_c_char_ptr(),
@@ -958,7 +967,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
958967
);
959968

960969
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
961-
debug_context.builder,
970+
debug_context.builder.as_ref(),
962971
DW_LANG_RUST,
963972
compile_unit_file,
964973
producer.as_c_char_ptr(),
@@ -999,10 +1008,10 @@ fn build_field_di_node<'ll, 'tcx>(
9991008
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
10001009
};
10011010
unsafe {
1002-
llvm::LLVMRustDIBuilderCreateMemberType(
1011+
llvm::LLVMDIBuilderCreateMemberType(
10031012
DIB(cx),
10041013
owner,
1005-
name.as_c_char_ptr(),
1014+
name.as_ptr(),
10061015
name.len(),
10071016
file_metadata,
10081017
line_number,
@@ -1630,7 +1639,14 @@ pub(crate) fn extend_scope_to_file<'ll>(
16301639
file: &SourceFile,
16311640
) -> &'ll DILexicalBlock {
16321641
let file_metadata = file_metadata(cx, file);
1633-
unsafe { llvm::LLVMRustDIBuilderCreateLexicalBlockFile(DIB(cx), scope_metadata, file_metadata) }
1642+
unsafe {
1643+
llvm::LLVMDIBuilderCreateLexicalBlockFile(
1644+
DIB(cx),
1645+
scope_metadata,
1646+
file_metadata,
1647+
/* Discriminator (default) */ 0u32,
1648+
)
1649+
}
16341650
}
16351651

16361652
fn tuple_field_name(field_index: usize) -> Cow<'static, str> {

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1111
use rustc_middle::ty::{self, AdtDef, CoroutineArgs, CoroutineArgsExt, Ty};
1212
use smallvec::smallvec;
1313

14-
use crate::common::{AsCCharPtr, CodegenCx};
14+
use crate::common::CodegenCx;
1515
use crate::debuginfo::metadata::enums::DiscrResult;
1616
use crate::debuginfo::metadata::type_map::{self, Stub, UniqueTypeId};
1717
use crate::debuginfo::metadata::{
@@ -381,10 +381,10 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
381381
None,
382382
),
383383
unsafe {
384-
llvm::LLVMRustDIBuilderCreateStaticMemberType(
384+
llvm::LLVMDIBuilderCreateStaticMemberType(
385385
DIB(cx),
386386
enum_type_di_node,
387-
TAG_FIELD_NAME.as_c_char_ptr(),
387+
TAG_FIELD_NAME.as_ptr(),
388388
TAG_FIELD_NAME.len(),
389389
unknown_file_metadata(cx),
390390
UNKNOWN_LINE_NUMBER,
@@ -571,10 +571,10 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
571571

572572
let build_assoc_const =
573573
|name: &str, type_di_node: &'ll DIType, value: u64, align: Align| unsafe {
574-
llvm::LLVMRustDIBuilderCreateStaticMemberType(
574+
llvm::LLVMDIBuilderCreateStaticMemberType(
575575
DIB(cx),
576576
wrapper_struct_type_di_node,
577-
name.as_c_char_ptr(),
577+
name.as_ptr(),
578578
name.len(),
579579
unknown_file_metadata(cx),
580580
UNKNOWN_LINE_NUMBER,
@@ -829,10 +829,10 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
829829
// the build_field_di_node() function does not support specifying a source location,
830830
// which is something that we don't do anywhere else.
831831
unsafe {
832-
llvm::LLVMRustDIBuilderCreateMemberType(
832+
llvm::LLVMDIBuilderCreateMemberType(
833833
DIB(cx),
834834
enum_type_di_node,
835-
field_name.as_c_char_ptr(),
835+
field_name.as_ptr(),
836836
field_name.len(),
837837
file_di_node,
838838
line_number,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@ fn build_discr_member_di_node<'ll, 'tcx>(
366366
let (size, align) = cx.size_and_align_of(tag_base_type);
367367

368368
unsafe {
369-
Some(llvm::LLVMRustDIBuilderCreateMemberType(
369+
Some(llvm::LLVMDIBuilderCreateMemberType(
370370
DIB(cx),
371371
containing_scope,
372-
tag_name.as_c_char_ptr(),
372+
tag_name.as_ptr(),
373373
tag_name.len(),
374374
unknown_file_metadata(cx),
375375
UNKNOWN_LINE_NUMBER,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::bug;
99
use rustc_middle::ty::{self, PolyExistentialTraitRef, Ty, TyCtxt};
1010

1111
use super::{DefinitionLocation, SmallVec, UNKNOWN_LINE_NUMBER, unknown_file_metadata};
12-
use crate::common::{AsCCharPtr, CodegenCx};
12+
use crate::common::CodegenCx;
1313
use crate::debuginfo::utils::{DIB, create_DIArray, debug_context};
1414
use crate::llvm::debuginfo::{DIFlags, DIScope, DIType};
1515
use crate::llvm::{self};
@@ -191,7 +191,6 @@ pub(super) fn stub<'ll, 'tcx>(
191191
containing_scope: Option<&'ll DIScope>,
192192
flags: DIFlags,
193193
) -> StubInfo<'ll, 'tcx> {
194-
let empty_array = create_DIArray(DIB(cx), &[]);
195194
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
196195

197196
let (file_metadata, line_number) = if let Some(def_location) = def_location {
@@ -207,39 +206,41 @@ pub(super) fn stub<'ll, 'tcx>(
207206
_ => None,
208207
};
209208
unsafe {
210-
llvm::LLVMRustDIBuilderCreateStructType(
209+
llvm::LLVMDIBuilderCreateStructType(
211210
DIB(cx),
212211
containing_scope,
213-
name.as_c_char_ptr(),
212+
name.as_ptr(),
214213
name.len(),
215214
file_metadata,
216215
line_number,
217216
size.bits(),
218217
align.bits() as u32,
219218
flags,
220-
None,
221-
empty_array,
222-
0,
219+
/* DerivedFrom */ None,
220+
/* Elements */ (&[]).as_ptr(),
221+
/* NumElements */ 0u32,
222+
/* RunTimeLang */ 0u32,
223223
vtable_holder,
224-
unique_type_id_str.as_c_char_ptr(),
224+
unique_type_id_str.as_ptr(),
225225
unique_type_id_str.len(),
226226
)
227227
}
228228
}
229229
Stub::Union => unsafe {
230-
llvm::LLVMRustDIBuilderCreateUnionType(
230+
llvm::LLVMDIBuilderCreateUnionType(
231231
DIB(cx),
232232
containing_scope,
233-
name.as_c_char_ptr(),
233+
name.as_ptr(),
234234
name.len(),
235235
file_metadata,
236236
line_number,
237237
size.bits(),
238238
align.bits() as u32,
239239
flags,
240-
Some(empty_array),
241-
0,
242-
unique_type_id_str.as_c_char_ptr(),
240+
/* Elements */ (&[]).as_ptr(),
241+
/* NumElements */ 0u32,
242+
/* RunTimeLang */ 0u32,
243+
unique_type_id_str.as_ptr(),
243244
unique_type_id_str.len(),
244245
)
245246
},

0 commit comments

Comments
 (0)