Rollup merge of #96432 - SparrowLii:dbg_scope, r=davidtwco

not need `Option` for `dbg_scope`

This PR fixes a few FIXME about not using `Option` in `dbg_scope` field of `DebugScope`, during `create_function_debug_context` func in codegen parts.
Added a `BitSet<SourceScope>` parameter to `make_mir_scope` to indicate whether the `DebugScope` has been instantiated.
cc ````@eddyb````
This commit is contained in:
Dylan DPC 2022-04-28 02:40:34 +02:00 committed by GitHub
commit d956d014f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 28 deletions

View File

@ -20,7 +20,6 @@ pub fn compute_mir_scopes<'ll, 'tcx>(
cx: &CodegenCx<'ll, 'tcx>, cx: &CodegenCx<'ll, 'tcx>,
instance: Instance<'tcx>, instance: Instance<'tcx>,
mir: &Body<'tcx>, mir: &Body<'tcx>,
fn_dbg_scope: &'ll DIScope,
debug_context: &mut FunctionDebugContext<&'ll DIScope, &'ll DILocation>, debug_context: &mut FunctionDebugContext<&'ll DIScope, &'ll DILocation>,
) { ) {
// Find all scopes with variables defined in them. // Find all scopes with variables defined in them.
@ -38,40 +37,41 @@ pub fn compute_mir_scopes<'ll, 'tcx>(
// Nothing to emit, of course. // Nothing to emit, of course.
None None
}; };
let mut instantiated = BitSet::new_empty(mir.source_scopes.len());
// Instantiate all scopes. // Instantiate all scopes.
for idx in 0..mir.source_scopes.len() { for idx in 0..mir.source_scopes.len() {
let scope = SourceScope::new(idx); let scope = SourceScope::new(idx);
make_mir_scope(cx, instance, mir, fn_dbg_scope, &variables, debug_context, scope); make_mir_scope(cx, instance, mir, &variables, debug_context, &mut instantiated, scope);
} }
assert!(instantiated.count() == mir.source_scopes.len());
} }
fn make_mir_scope<'ll, 'tcx>( fn make_mir_scope<'ll, 'tcx>(
cx: &CodegenCx<'ll, 'tcx>, cx: &CodegenCx<'ll, 'tcx>,
instance: Instance<'tcx>, instance: Instance<'tcx>,
mir: &Body<'tcx>, mir: &Body<'tcx>,
fn_dbg_scope: &'ll DIScope,
variables: &Option<BitSet<SourceScope>>, variables: &Option<BitSet<SourceScope>>,
debug_context: &mut FunctionDebugContext<&'ll DIScope, &'ll DILocation>, debug_context: &mut FunctionDebugContext<&'ll DIScope, &'ll DILocation>,
instantiated: &mut BitSet<SourceScope>,
scope: SourceScope, scope: SourceScope,
) { ) {
if debug_context.scopes[scope].dbg_scope.is_some() { if instantiated.contains(scope) {
return; return;
} }
let scope_data = &mir.source_scopes[scope]; let scope_data = &mir.source_scopes[scope];
let parent_scope = if let Some(parent) = scope_data.parent_scope { let parent_scope = if let Some(parent) = scope_data.parent_scope {
make_mir_scope(cx, instance, mir, fn_dbg_scope, variables, debug_context, parent); make_mir_scope(cx, instance, mir, variables, debug_context, instantiated, parent);
debug_context.scopes[parent] debug_context.scopes[parent]
} else { } else {
// The root is the function itself. // The root is the function itself.
let loc = cx.lookup_debug_loc(mir.span.lo()); let loc = cx.lookup_debug_loc(mir.span.lo());
debug_context.scopes[scope] = DebugScope { debug_context.scopes[scope] = DebugScope {
dbg_scope: Some(fn_dbg_scope),
inlined_at: None,
file_start_pos: loc.file.start_pos, file_start_pos: loc.file.start_pos,
file_end_pos: loc.file.end_pos, file_end_pos: loc.file.end_pos,
..debug_context.scopes[scope]
}; };
instantiated.insert(scope);
return; return;
}; };
@ -79,6 +79,7 @@ fn make_mir_scope<'ll, 'tcx>(
// Do not create a DIScope if there are no variables defined in this // Do not create a DIScope if there are no variables defined in this
// MIR `SourceScope`, and it's not `inlined`, to avoid debuginfo bloat. // MIR `SourceScope`, and it's not `inlined`, to avoid debuginfo bloat.
debug_context.scopes[scope] = parent_scope; debug_context.scopes[scope] = parent_scope;
instantiated.insert(scope);
return; return;
} }
@ -100,7 +101,7 @@ fn make_mir_scope<'ll, 'tcx>(
None => unsafe { None => unsafe {
llvm::LLVMRustDIBuilderCreateLexicalBlock( llvm::LLVMRustDIBuilderCreateLexicalBlock(
DIB(cx), DIB(cx),
parent_scope.dbg_scope.unwrap(), parent_scope.dbg_scope,
file_metadata, file_metadata,
loc.line, loc.line,
loc.col, loc.col,
@ -116,9 +117,10 @@ fn make_mir_scope<'ll, 'tcx>(
}); });
debug_context.scopes[scope] = DebugScope { debug_context.scopes[scope] = DebugScope {
dbg_scope: Some(dbg_scope), dbg_scope,
inlined_at: inlined_at.or(parent_scope.inlined_at), inlined_at: inlined_at.or(parent_scope.inlined_at),
file_start_pos: loc.file.start_pos, file_start_pos: loc.file.start_pos,
file_end_pos: loc.file.end_pos, file_end_pos: loc.file.end_pos,
}; };
instantiated.insert(scope);
} }

View File

@ -286,9 +286,8 @@ fn create_function_debug_context(
} }
// Initialize fn debug context (including scopes). // Initialize fn debug context (including scopes).
// FIXME(eddyb) figure out a way to not need `Option` for `dbg_scope`.
let empty_scope = DebugScope { let empty_scope = DebugScope {
dbg_scope: None, dbg_scope: self.dbg_scope_fn(instance, fn_abi, Some(llfn)),
inlined_at: None, inlined_at: None,
file_start_pos: BytePos(0), file_start_pos: BytePos(0),
file_end_pos: BytePos(0), file_end_pos: BytePos(0),
@ -297,13 +296,7 @@ fn create_function_debug_context(
FunctionDebugContext { scopes: IndexVec::from_elem(empty_scope, &mir.source_scopes) }; FunctionDebugContext { scopes: IndexVec::from_elem(empty_scope, &mir.source_scopes) };
// Fill in all the scopes, with the information from the MIR body. // Fill in all the scopes, with the information from the MIR body.
compute_mir_scopes( compute_mir_scopes(self, instance, mir, &mut fn_debug_context);
self,
instance,
mir,
self.dbg_scope_fn(instance, fn_abi, Some(llfn)),
&mut fn_debug_context,
);
Some(fn_debug_context) Some(fn_debug_context)
} }

View File

@ -39,8 +39,7 @@ pub struct PerLocalVarDebugInfo<'tcx, D> {
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct DebugScope<S, L> { pub struct DebugScope<S, L> {
// FIXME(eddyb) this should never be `None`, after initialization. pub dbg_scope: S,
pub dbg_scope: Option<S>,
/// Call site location, if this scope was inlined from another function. /// Call site location, if this scope was inlined from another function.
pub inlined_at: Option<L>, pub inlined_at: Option<L>,
@ -61,17 +60,12 @@ pub fn adjust_dbg_scope_for_span<Cx: CodegenMethods<'tcx, DIScope = S, DILocatio
cx: &Cx, cx: &Cx,
span: Span, span: Span,
) -> S { ) -> S {
// FIXME(eddyb) this should never be `None`.
let dbg_scope = self
.dbg_scope
.unwrap_or_else(|| bug!("`dbg_scope` is only `None` during initialization"));
let pos = span.lo(); let pos = span.lo();
if pos < self.file_start_pos || pos >= self.file_end_pos { if pos < self.file_start_pos || pos >= self.file_end_pos {
let sm = cx.sess().source_map(); let sm = cx.sess().source_map();
cx.extend_scope_to_file(dbg_scope, &sm.lookup_char_pos(pos).file) cx.extend_scope_to_file(self.dbg_scope, &sm.lookup_char_pos(pos).file)
} else { } else {
dbg_scope self.dbg_scope
} }
} }
} }