Skip to content

Commit 4e6640b

Browse files
authored
Rollup merge of #146631 - Zalathar:di-builder, r=nnethercote
cg_llvm: Replace some DIBuilder wrappers with LLVM-C API bindings (part 3) - Part of #134001 - Follow-up to #136375 - Follow-up to #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 #136632, plus `LLVMDIBuilderCreateStructType`.
2 parents 76f1159 + af88d14 commit 4e6640b

File tree

5 files changed

+149
-190
lines changed

5 files changed

+149
-190
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ use self::type_map::{DINodeCreationResult, Stub, UniqueTypeId};
3232
use super::CodegenUnitDebugContext;
3333
use super::namespace::mangled_name_of_instance;
3434
use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
35-
use super::utils::{
36-
DIB, create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit,
37-
};
35+
use super::utils::{DIB, debug_context, get_namespace_for_item, is_node_local_to_unit};
3836
use crate::common::{AsCCharPtr, CodegenCx};
3937
use crate::debuginfo::dwarf_const;
4038
use crate::debuginfo::metadata::type_map::build_type_with_children;
@@ -119,17 +117,17 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>(
119117
.try_to_target_usize(cx.tcx)
120118
.expect("expected monomorphic const in codegen") as c_longlong;
121119

122-
let subrange =
123-
unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
120+
let subrange = unsafe { llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) };
121+
let subscripts = &[subrange];
124122

125-
let subscripts = create_DIArray(DIB(cx), &[subrange]);
126123
let di_node = unsafe {
127-
llvm::LLVMRustDIBuilderCreateArrayType(
124+
llvm::LLVMDIBuilderCreateArrayType(
128125
DIB(cx),
129126
size.bits(),
130127
align.bits() as u32,
131128
element_type_di_node,
132-
subscripts,
129+
subscripts.as_ptr(),
130+
subscripts.len() as c_uint,
133131
)
134132
};
135133

@@ -175,17 +173,13 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
175173
"ptr_type={ptr_type}, pointee_type={pointee_type}",
176174
);
177175

178-
let di_node = unsafe {
179-
llvm::LLVMRustDIBuilderCreatePointerType(
180-
DIB(cx),
181-
pointee_type_di_node,
182-
pointer_size.bits(),
183-
pointer_align.abi.bits() as u32,
184-
0, // Ignore DWARF address space.
185-
ptr_type_debuginfo_name.as_c_char_ptr(),
186-
ptr_type_debuginfo_name.len(),
187-
)
188-
};
176+
let di_node = create_pointer_type(
177+
cx,
178+
pointee_type_di_node,
179+
pointer_size,
180+
pointer_align.abi,
181+
&ptr_type_debuginfo_name,
182+
);
189183

190184
DINodeCreationResult { di_node, already_stored_in_typemap: false }
191185
}
@@ -233,17 +227,13 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
233227

234228
// The data pointer type is a regular, thin pointer, regardless of whether this
235229
// is a slice or a trait object.
236-
let data_ptr_type_di_node = unsafe {
237-
llvm::LLVMRustDIBuilderCreatePointerType(
238-
DIB(cx),
239-
pointee_type_di_node,
240-
addr_field.size.bits(),
241-
addr_field.align.abi.bits() as u32,
242-
0, // Ignore DWARF address space.
243-
std::ptr::null(),
244-
0,
245-
)
246-
};
230+
let data_ptr_type_di_node = create_pointer_type(
231+
cx,
232+
pointee_type_di_node,
233+
addr_field.size,
234+
addr_field.align.abi,
235+
"",
236+
);
247237

248238
smallvec![
249239
build_field_di_node(
@@ -318,7 +308,7 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
318308

319309
debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
320310

321-
let fn_di_node = create_subroutine_type(cx, create_DIArray(DIB(cx), &signature_di_nodes[..]));
311+
let fn_di_node = create_subroutine_type(cx, &signature_di_nodes[..]);
322312

323313
// This is actually a function pointer, so wrap it in pointer DI.
324314
let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
@@ -329,26 +319,44 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
329319
}
330320
_ => unreachable!(),
331321
};
332-
let di_node = unsafe {
333-
llvm::LLVMRustDIBuilderCreatePointerType(
334-
DIB(cx),
335-
fn_di_node,
336-
size.bits(),
337-
align.bits() as u32,
338-
0, // Ignore DWARF address space.
339-
name.as_c_char_ptr(),
340-
name.len(),
341-
)
342-
};
322+
let di_node = create_pointer_type(cx, fn_di_node, size, align, &name);
343323

344324
DINodeCreationResult::new(di_node, false)
345325
}
346326

347327
pub(super) fn create_subroutine_type<'ll>(
348328
cx: &CodegenCx<'ll, '_>,
349-
signature: &'ll DICompositeType,
329+
signature: &[Option<&'ll llvm::Metadata>],
350330
) -> &'ll DICompositeType {
351-
unsafe { llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(cx), signature) }
331+
unsafe {
332+
llvm::LLVMDIBuilderCreateSubroutineType(
333+
DIB(cx),
334+
None, // ("File" is ignored and has no effect)
335+
signature.as_ptr(),
336+
signature.len() as c_uint,
337+
DIFlags::FlagZero, // (default value)
338+
)
339+
}
340+
}
341+
342+
fn create_pointer_type<'ll>(
343+
cx: &CodegenCx<'ll, '_>,
344+
pointee_ty: &'ll llvm::Metadata,
345+
size: Size,
346+
align: Align,
347+
name: &str,
348+
) -> &'ll llvm::Metadata {
349+
unsafe {
350+
llvm::LLVMDIBuilderCreatePointerType(
351+
DIB(cx),
352+
pointee_ty,
353+
size.bits(),
354+
align.bits() as u32,
355+
0, // Ignore DWARF address space.
356+
name.as_ptr(),
357+
name.len(),
358+
)
359+
}
352360
}
353361

354362
/// Create debuginfo for `dyn SomeTrait` types. Currently these are empty structs
@@ -834,12 +842,13 @@ fn create_basic_type<'ll, 'tcx>(
834842
encoding: u32,
835843
) -> &'ll DIBasicType {
836844
unsafe {
837-
llvm::LLVMRustDIBuilderCreateBasicType(
845+
llvm::LLVMDIBuilderCreateBasicType(
838846
DIB(cx),
839-
name.as_c_char_ptr(),
847+
name.as_ptr(),
840848
name.len(),
841849
size.bits(),
842850
encoding,
851+
DIFlags::FlagZero,
843852
)
844853
}
845854
}

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::RefCell;
22

3+
use libc::c_uint;
34
use rustc_abi::{Align, Size, VariantIdx};
45
use rustc_data_structures::fingerprint::Fingerprint;
56
use rustc_data_structures::fx::FxHashMap;
@@ -9,7 +10,7 @@ use rustc_middle::bug;
910
use rustc_middle::ty::{self, ExistentialTraitRef, Ty, TyCtxt};
1011

1112
use super::{DefinitionLocation, SmallVec, UNKNOWN_LINE_NUMBER, unknown_file_metadata};
12-
use crate::common::{AsCCharPtr, CodegenCx};
13+
use crate::common::CodegenCx;
1314
use crate::debuginfo::utils::{DIB, create_DIArray, debug_context};
1415
use crate::llvm::debuginfo::{DIFlags, DIScope, DIType};
1516
use crate::llvm::{self};
@@ -191,7 +192,7 @@ pub(super) fn stub<'ll, 'tcx>(
191192
containing_scope: Option<&'ll DIScope>,
192193
flags: DIFlags,
193194
) -> StubInfo<'ll, 'tcx> {
194-
let empty_array = create_DIArray(DIB(cx), &[]);
195+
let no_elements: &[Option<&llvm::Metadata>] = &[];
195196
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
196197

197198
let (file_metadata, line_number) = if let Some(def_location) = def_location {
@@ -207,39 +208,41 @@ pub(super) fn stub<'ll, 'tcx>(
207208
_ => None,
208209
};
209210
unsafe {
210-
llvm::LLVMRustDIBuilderCreateStructType(
211+
llvm::LLVMDIBuilderCreateStructType(
211212
DIB(cx),
212213
containing_scope,
213-
name.as_c_char_ptr(),
214+
name.as_ptr(),
214215
name.len(),
215216
file_metadata,
216217
line_number,
217218
size.bits(),
218219
align.bits() as u32,
219220
flags,
220221
None,
221-
empty_array,
222-
0,
222+
no_elements.as_ptr(),
223+
no_elements.len() as c_uint,
224+
0u32, // (Objective-C runtime version; default is 0)
223225
vtable_holder,
224-
unique_type_id_str.as_c_char_ptr(),
226+
unique_type_id_str.as_ptr(),
225227
unique_type_id_str.len(),
226228
)
227229
}
228230
}
229231
Stub::Union => unsafe {
230-
llvm::LLVMRustDIBuilderCreateUnionType(
232+
llvm::LLVMDIBuilderCreateUnionType(
231233
DIB(cx),
232234
containing_scope,
233-
name.as_c_char_ptr(),
235+
name.as_ptr(),
234236
name.len(),
235237
file_metadata,
236238
line_number,
237239
size.bits(),
238240
align.bits() as u32,
239241
flags,
240-
Some(empty_array),
241-
0,
242-
unique_type_id_str.as_c_char_ptr(),
242+
no_elements.as_ptr(),
243+
no_elements.len() as c_uint,
244+
0u32, // (Objective-C runtime version; default is 0)
245+
unique_type_id_str.as_ptr(),
243246
unique_type_id_str.len(),
244247
)
245248
},

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
349349
let file_metadata = file_metadata(self, &loc.file);
350350

351351
let function_type_metadata =
352-
create_subroutine_type(self, get_function_signature(self, fn_abi));
352+
create_subroutine_type(self, &get_function_signature(self, fn_abi));
353353

354354
let mut name = String::with_capacity(64);
355355
type_names::push_item_name(tcx, def_id, false, &mut name);
@@ -441,9 +441,9 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
441441
fn get_function_signature<'ll, 'tcx>(
442442
cx: &CodegenCx<'ll, 'tcx>,
443443
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
444-
) -> &'ll DIArray {
444+
) -> Vec<Option<&'ll llvm::Metadata>> {
445445
if cx.sess().opts.debuginfo != DebugInfo::Full {
446-
return create_DIArray(DIB(cx), &[]);
446+
return vec![];
447447
}
448448

449449
let mut signature = Vec::with_capacity(fn_abi.args.len() + 1);
@@ -484,7 +484,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
484484
.extend(fn_abi.args.iter().map(|arg| Some(type_di_node(cx, arg.layout.ty))));
485485
}
486486

487-
create_DIArray(DIB(cx), &signature[..])
487+
signature
488488
}
489489

490490
fn get_template_parameters<'ll, 'tcx>(

0 commit comments

Comments
 (0)