Skip to content

Commit fa2b381

Browse files
committed
rustc_codegen_llvm: move DISubprogram creation to a dbg_scope_fn method.
1 parent 9d57c41 commit fa2b381

File tree

6 files changed

+52
-36
lines changed

6 files changed

+52
-36
lines changed

compiler/rustc_codegen_llvm/src/common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl Funclet<'ll> {
8080

8181
impl BackendTypes for CodegenCx<'ll, 'tcx> {
8282
type Value = &'ll Value;
83+
// FIXME(eddyb) replace this with a `Function` "subclass" of `Value`.
8384
type Function = &'ll Value;
8485

8586
type BasicBlock = &'ll BasicBlock;

compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_codegen_ssa::traits::*;
55

66
use crate::common::CodegenCx;
77
use crate::llvm;
8-
use crate::llvm::debuginfo::{DIScope, DISubprogram};
8+
use crate::llvm::debuginfo::DIScope;
99
use rustc_middle::mir::{Body, SourceScope};
1010
use rustc_session::config::DebugInfo;
1111

@@ -16,7 +16,7 @@ use rustc_index::vec::Idx;
1616
pub fn compute_mir_scopes(
1717
cx: &CodegenCx<'ll, '_>,
1818
mir: &Body<'_>,
19-
fn_metadata: &'ll DISubprogram,
19+
fn_dbg_scope: &'ll DIScope,
2020
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
2121
) {
2222
// Find all the scopes with variables defined in them.
@@ -37,16 +37,16 @@ pub fn compute_mir_scopes(
3737
// Instantiate all scopes.
3838
for idx in 0..mir.source_scopes.len() {
3939
let scope = SourceScope::new(idx);
40-
make_mir_scope(cx, &mir, fn_metadata, &has_variables, debug_context, scope);
40+
make_mir_scope(cx, &mir, fn_dbg_scope, &has_variables, debug_context, scope);
4141
}
4242
}
4343

4444
fn make_mir_scope(
4545
cx: &CodegenCx<'ll, '_>,
4646
mir: &Body<'_>,
47-
fn_metadata: &'ll DISubprogram,
47+
fn_dbg_scope: &'ll DIScope,
4848
has_variables: &BitSet<SourceScope>,
49-
debug_context: &mut FunctionDebugContext<&'ll DISubprogram>,
49+
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
5050
scope: SourceScope,
5151
) {
5252
if debug_context.scopes[scope].is_valid() {
@@ -55,13 +55,13 @@ fn make_mir_scope(
5555

5656
let scope_data = &mir.source_scopes[scope];
5757
let parent_scope = if let Some(parent) = scope_data.parent_scope {
58-
make_mir_scope(cx, mir, fn_metadata, has_variables, debug_context, parent);
58+
make_mir_scope(cx, mir, fn_dbg_scope, has_variables, debug_context, parent);
5959
debug_context.scopes[parent]
6060
} else {
6161
// The root is the function itself.
6262
let loc = cx.lookup_debug_loc(mir.span.lo());
6363
debug_context.scopes[scope] = DebugScope {
64-
scope_metadata: Some(fn_metadata),
64+
scope_metadata: Some(fn_dbg_scope),
6565
file_start_pos: loc.file.start_pos,
6666
file_end_pos: loc.file.end_pos,
6767
};

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,36 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
235235
return None;
236236
}
237237

238-
let span = mir.span;
238+
// Initialize fn debug context (including scopes).
239+
// FIXME(eddyb) figure out a way to not need `Option` for `scope_metadata`.
240+
let empty_scope = DebugScope {
241+
scope_metadata: None,
242+
file_start_pos: BytePos(0),
243+
file_end_pos: BytePos(0),
244+
};
245+
let mut fn_debug_context =
246+
FunctionDebugContext { scopes: IndexVec::from_elem(empty_scope, &mir.source_scopes) };
239247

240-
// This can be the case for functions inlined from another crate
241-
if span.is_dummy() {
242-
// FIXME(simulacrum): Probably can't happen; remove.
243-
return None;
244-
}
248+
// Fill in all the scopes, with the information from the MIR body.
249+
compute_mir_scopes(
250+
self,
251+
mir,
252+
self.dbg_scope_fn(instance, fn_abi, Some(llfn)),
253+
&mut fn_debug_context,
254+
);
255+
256+
Some(fn_debug_context)
257+
}
245258

259+
fn dbg_scope_fn(
260+
&self,
261+
instance: Instance<'tcx>,
262+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
263+
maybe_definition_llfn: Option<&'ll Value>,
264+
) -> &'ll DIScope {
246265
let def_id = instance.def_id();
247266
let containing_scope = get_containing_scope(self, instance);
267+
let span = self.tcx.def_span(def_id);
248268
let loc = self.lookup_debug_loc(span.lo());
249269
let file_metadata = file_metadata(self, &loc.file);
250270

@@ -291,8 +311,8 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
291311
}
292312
}
293313

294-
let fn_metadata = unsafe {
295-
llvm::LLVMRustDIBuilderCreateFunction(
314+
unsafe {
315+
return llvm::LLVMRustDIBuilderCreateFunction(
296316
DIB(self),
297317
containing_scope,
298318
name.as_ptr().cast(),
@@ -305,26 +325,11 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
305325
scope_line.unwrap_or(UNKNOWN_LINE_NUMBER),
306326
flags,
307327
spflags,
308-
llfn,
328+
maybe_definition_llfn,
309329
template_parameters,
310330
None,
311-
)
312-
};
313-
314-
// Initialize fn debug context (including scopes).
315-
// FIXME(eddyb) figure out a way to not need `Option` for `scope_metadata`.
316-
let null_scope = DebugScope {
317-
scope_metadata: None,
318-
file_start_pos: BytePos(0),
319-
file_end_pos: BytePos(0),
320-
};
321-
let mut fn_debug_context =
322-
FunctionDebugContext { scopes: IndexVec::from_elem(null_scope, &mir.source_scopes) };
323-
324-
// Fill in all the scopes, with the information from the MIR body.
325-
compute_mir_scopes(self, mir, fn_metadata, &mut fn_debug_context);
326-
327-
return Some(fn_debug_context);
331+
);
332+
}
328333

329334
fn get_function_signature<'ll, 'tcx>(
330335
cx: &CodegenCx<'ll, 'tcx>,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ extern "C" {
18541854
ScopeLine: c_uint,
18551855
Flags: DIFlags,
18561856
SPFlags: DISPFlags,
1857-
Fn: &'a Value,
1857+
MaybeFn: Option<&'a Value>,
18581858
TParam: &'a DIArray,
18591859
Decl: Option<&'a DIDescriptor>,
18601860
) -> &'a DISubprogram;

compiler/rustc_codegen_ssa/src/traits/debuginfo.rs

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ pub trait DebugInfoMethods<'tcx>: BackendTypes {
2121
mir: &mir::Body<'_>,
2222
) -> Option<FunctionDebugContext<Self::DIScope>>;
2323

24+
// FIXME(eddyb) find a common convention for all of the debuginfo-related
25+
// names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
26+
fn dbg_scope_fn(
27+
&self,
28+
instance: Instance<'tcx>,
29+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
30+
maybe_definition_llfn: Option<Self::Function>,
31+
) -> Self::DIScope;
32+
2433
fn extend_scope_to_file(
2534
&self,
2635
scope_metadata: Self::DIScope,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFunction(
733733
const char *LinkageName, size_t LinkageNameLen,
734734
LLVMMetadataRef File, unsigned LineNo,
735735
LLVMMetadataRef Ty, unsigned ScopeLine, LLVMRustDIFlags Flags,
736-
LLVMRustDISPFlags SPFlags, LLVMValueRef Fn, LLVMMetadataRef TParam,
736+
LLVMRustDISPFlags SPFlags, LLVMValueRef MaybeFn, LLVMMetadataRef TParam,
737737
LLVMMetadataRef Decl) {
738738
DITemplateParameterArray TParams =
739739
DITemplateParameterArray(unwrap<MDTuple>(TParam));
@@ -750,7 +750,8 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFunction(
750750
unwrapDI<DIFile>(File), LineNo,
751751
unwrapDI<DISubroutineType>(Ty), ScopeLine, llvmFlags,
752752
llvmSPFlags, TParams, unwrapDIPtr<DISubprogram>(Decl));
753-
unwrap<Function>(Fn)->setSubprogram(Sub);
753+
if (MaybeFn)
754+
unwrap<Function>(MaybeFn)->setSubprogram(Sub);
754755
return wrap(Sub);
755756
}
756757

0 commit comments

Comments
 (0)