2024-11-04 11:38:14 -08:00
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext};
|
2020-02-11 22:52:59 +02:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2024-11-04 11:38:14 -08:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-02-11 01:27:33 +02:00
|
|
|
use rustc_index::Idx;
|
2025-01-07 15:19:05 +00:00
|
|
|
use rustc_index::bit_set::DenseBitSet;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{Body, SourceScope};
|
2024-11-15 13:53:31 +01:00
|
|
|
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv};
|
2020-02-11 01:27:33 +02:00
|
|
|
use rustc_middle::ty::{self, Instance};
|
2020-02-11 22:52:59 +02:00
|
|
|
use rustc_session::config::DebugInfo;
|
2025-01-17 09:44:09 -08:00
|
|
|
use rustc_span::{BytePos, DUMMY_SP, hygiene};
|
2015-04-29 18:14:37 +12:00
|
|
|
|
2019-09-26 05:30:10 +00:00
|
|
|
use super::metadata::file_metadata;
|
|
|
|
|
use super::utils::DIB;
|
2023-04-19 10:57:17 +00:00
|
|
|
use crate::common::CodegenCx;
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::llvm;
|
2023-04-19 10:57:17 +00:00
|
|
|
use crate::llvm::debuginfo::{DILocation, DIScope};
|
2016-08-24 19:34:31 -07:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Produces DIScope DIEs for each MIR Scope which has variables defined in it.
|
2020-02-11 01:27:33 +02:00
|
|
|
// FIXME(eddyb) almost all of this should be in `rustc_codegen_ssa::mir::debuginfo`.
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn compute_mir_scopes<'ll, 'tcx>(
|
2020-02-11 01:27:33 +02:00
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
|
instance: Instance<'tcx>,
|
|
|
|
|
mir: &Body<'tcx>,
|
2023-09-01 14:27:21 -07:00
|
|
|
debug_context: &mut FunctionDebugContext<'tcx, &'ll DIScope, &'ll DILocation>,
|
2019-09-11 17:52:39 +03:00
|
|
|
) {
|
2022-03-25 03:50:41 -07:00
|
|
|
// Find all scopes with variables defined in them.
|
|
|
|
|
let variables = if cx.sess().opts.debuginfo == DebugInfo::Full {
|
2025-01-07 15:19:05 +00:00
|
|
|
let mut vars = DenseBitSet::new_empty(mir.source_scopes.len());
|
2020-02-11 22:52:59 +02:00
|
|
|
// FIXME(eddyb) take into account that arguments always have debuginfo,
|
|
|
|
|
// irrespective of their name (assuming full debuginfo is enabled).
|
|
|
|
|
// NOTE(eddyb) actually, on second thought, those are always in the
|
|
|
|
|
// function scope, which always exists.
|
|
|
|
|
for var_debug_info in &mir.var_debug_info {
|
2022-03-25 03:50:41 -07:00
|
|
|
vars.insert(var_debug_info.source_info.scope);
|
2020-02-11 22:52:59 +02:00
|
|
|
}
|
2022-03-25 03:50:41 -07:00
|
|
|
Some(vars)
|
|
|
|
|
} else {
|
|
|
|
|
// Nothing to emit, of course.
|
|
|
|
|
None
|
|
|
|
|
};
|
2025-01-07 15:19:05 +00:00
|
|
|
let mut instantiated = DenseBitSet::new_empty(mir.source_scopes.len());
|
2024-11-04 11:38:14 -08:00
|
|
|
let mut discriminators = FxHashMap::default();
|
2016-04-07 22:35:11 +03:00
|
|
|
// Instantiate all scopes.
|
2018-05-28 14:16:09 +03:00
|
|
|
for idx in 0..mir.source_scopes.len() {
|
|
|
|
|
let scope = SourceScope::new(idx);
|
2024-11-04 11:38:14 -08:00
|
|
|
make_mir_scope(
|
|
|
|
|
cx,
|
|
|
|
|
instance,
|
|
|
|
|
mir,
|
|
|
|
|
&variables,
|
|
|
|
|
debug_context,
|
|
|
|
|
&mut instantiated,
|
|
|
|
|
&mut discriminators,
|
|
|
|
|
scope,
|
|
|
|
|
);
|
2016-04-07 22:35:11 +03:00
|
|
|
}
|
2022-04-26 21:00:19 +08:00
|
|
|
assert!(instantiated.count() == mir.source_scopes.len());
|
2016-04-07 22:35:11 +03:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
fn make_mir_scope<'ll, 'tcx>(
|
2020-02-11 01:27:33 +02:00
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
|
instance: Instance<'tcx>,
|
|
|
|
|
mir: &Body<'tcx>,
|
2025-01-07 15:19:05 +00:00
|
|
|
variables: &Option<DenseBitSet<SourceScope>>,
|
2023-09-01 14:27:21 -07:00
|
|
|
debug_context: &mut FunctionDebugContext<'tcx, &'ll DIScope, &'ll DILocation>,
|
2025-01-07 15:19:05 +00:00
|
|
|
instantiated: &mut DenseBitSet<SourceScope>,
|
2024-11-04 11:38:14 -08:00
|
|
|
discriminators: &mut FxHashMap<BytePos, u32>,
|
2019-09-11 17:52:39 +03:00
|
|
|
scope: SourceScope,
|
|
|
|
|
) {
|
2022-04-26 21:00:19 +08:00
|
|
|
if instantiated.contains(scope) {
|
2016-04-07 22:35:11 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
let scope_data = &mir.source_scopes[scope];
|
2016-04-07 22:35:11 +03:00
|
|
|
let parent_scope = if let Some(parent) = scope_data.parent_scope {
|
2024-11-04 11:38:14 -08:00
|
|
|
make_mir_scope(
|
|
|
|
|
cx,
|
|
|
|
|
instance,
|
|
|
|
|
mir,
|
|
|
|
|
variables,
|
|
|
|
|
debug_context,
|
|
|
|
|
instantiated,
|
|
|
|
|
discriminators,
|
|
|
|
|
parent,
|
|
|
|
|
);
|
2025-01-17 09:44:09 -08:00
|
|
|
debug_context.scopes[parent]
|
2016-04-07 22:35:11 +03:00
|
|
|
} else {
|
|
|
|
|
// The root is the function itself.
|
2023-06-28 08:57:32 +10:00
|
|
|
let file = cx.sess().source_map().lookup_source_file(mir.span.lo());
|
2025-01-17 09:44:09 -08:00
|
|
|
debug_context.scopes[scope] = DebugScope {
|
2023-06-28 08:57:32 +10:00
|
|
|
file_start_pos: file.start_pos,
|
2023-09-03 10:15:35 +00:00
|
|
|
file_end_pos: file.end_position(),
|
2025-01-17 09:44:09 -08:00
|
|
|
..debug_context.scopes[scope]
|
|
|
|
|
};
|
2022-04-26 21:00:19 +08:00
|
|
|
instantiated.insert(scope);
|
2016-04-07 22:35:11 +03:00
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-25 03:50:41 -07:00
|
|
|
if let Some(vars) = variables
|
|
|
|
|
&& !vars.contains(scope)
|
|
|
|
|
&& scope_data.inlined.is_none()
|
|
|
|
|
{
|
2020-02-11 01:27:33 +02:00
|
|
|
// Do not create a DIScope if there are no variables defined in this
|
|
|
|
|
// MIR `SourceScope`, and it's not `inlined`, to avoid debuginfo bloat.
|
2025-01-17 09:44:09 -08:00
|
|
|
debug_context.scopes[scope] = parent_scope;
|
2022-04-26 21:00:19 +08:00
|
|
|
instantiated.insert(scope);
|
2020-02-08 17:58:00 +02:00
|
|
|
return;
|
2016-04-16 21:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-25 00:00:00 +00:00
|
|
|
let loc = cx.lookup_debug_loc(scope_data.span.lo());
|
2023-08-25 19:49:10 -04:00
|
|
|
let file_metadata = file_metadata(cx, &loc.file);
|
2017-04-24 19:01:19 +02:00
|
|
|
|
2024-08-12 19:20:00 -07:00
|
|
|
let dbg_scope = match scope_data.inlined {
|
2020-02-11 01:27:33 +02:00
|
|
|
Some((callee, _)) => {
|
|
|
|
|
// FIXME(eddyb) this would be `self.monomorphize(&callee)`
|
|
|
|
|
// if this is moved to `rustc_codegen_ssa::mir::debuginfo`.
|
2023-09-25 15:46:38 +02:00
|
|
|
let callee = cx.tcx.instantiate_and_normalize_erasing_regions(
|
2023-07-11 22:35:29 +01:00
|
|
|
instance.args,
|
2024-11-15 13:53:31 +01:00
|
|
|
cx.typing_env(),
|
2023-05-29 13:46:10 +02:00
|
|
|
ty::EarlyBinder::bind(callee),
|
2020-02-11 01:27:33 +02:00
|
|
|
);
|
2023-09-01 14:27:21 -07:00
|
|
|
debug_context.inlined_function_scopes.entry(callee).or_insert_with(|| {
|
|
|
|
|
let callee_fn_abi = cx.fn_abi_of_instance(callee, ty::List::empty());
|
|
|
|
|
cx.dbg_scope_fn(callee, callee_fn_abi, None)
|
|
|
|
|
})
|
2020-02-11 01:27:33 +02:00
|
|
|
}
|
2024-08-12 19:20:00 -07:00
|
|
|
None => unsafe {
|
|
|
|
|
llvm::LLVMRustDIBuilderCreateLexicalBlock(
|
|
|
|
|
DIB(cx),
|
|
|
|
|
parent_scope.dbg_scope,
|
|
|
|
|
file_metadata,
|
|
|
|
|
loc.line,
|
|
|
|
|
loc.col,
|
|
|
|
|
)
|
|
|
|
|
},
|
2016-04-07 22:35:11 +03:00
|
|
|
};
|
2020-02-11 01:27:33 +02:00
|
|
|
|
2025-01-17 09:44:09 -08:00
|
|
|
let inlined_at = scope_data.inlined.map(|(_, callsite_span)| {
|
2024-11-18 18:16:22 -08:00
|
|
|
let callsite_span = hygiene::walk_chain_collapsed(callsite_span, mir.span);
|
2020-02-11 01:27:33 +02:00
|
|
|
let callsite_scope = parent_scope.adjust_dbg_scope_for_span(cx, callsite_span);
|
2024-11-04 11:38:14 -08:00
|
|
|
let loc = cx.dbg_loc(callsite_scope, parent_scope.inlined_at, callsite_span);
|
|
|
|
|
|
|
|
|
|
// NB: In order to produce proper debug info for variables (particularly
|
2024-11-18 18:48:10 -08:00
|
|
|
// arguments) in multiply-inlined functions, LLVM expects to see a single
|
2024-11-04 11:38:14 -08:00
|
|
|
// DILocalVariable with multiple different DILocations in the IR. While
|
|
|
|
|
// the source information for each DILocation would be identical, their
|
|
|
|
|
// inlinedAt attributes will be unique to the particular callsite.
|
|
|
|
|
//
|
|
|
|
|
// We generate DILocations here based on the callsite's location in the
|
|
|
|
|
// source code. A single location in the source code usually can't
|
|
|
|
|
// produce multiple distinct calls so this mostly works, until
|
2024-11-18 18:48:10 -08:00
|
|
|
// macros get involved. A macro can generate multiple calls
|
2024-11-04 11:38:14 -08:00
|
|
|
// at the same span, which breaks the assumption that we're going to
|
|
|
|
|
// produce a unique DILocation for every scope we process here. We
|
|
|
|
|
// have to explicitly add discriminators if we see inlines into the
|
|
|
|
|
// same source code location.
|
|
|
|
|
//
|
|
|
|
|
// Note further that we can't key this hashtable on the span itself,
|
|
|
|
|
// because these spans could have distinct SyntaxContexts. We have
|
|
|
|
|
// to key on exactly what we're giving to LLVM.
|
2025-01-17 09:44:09 -08:00
|
|
|
match discriminators.entry(callsite_span.lo()) {
|
2024-11-04 11:38:14 -08:00
|
|
|
Entry::Occupied(mut o) => {
|
|
|
|
|
*o.get_mut() += 1;
|
2025-01-17 09:44:09 -08:00
|
|
|
// NB: We have to emit *something* here or we'll fail LLVM IR verification
|
|
|
|
|
// in at least some circumstances (see issue #135322) so if the required
|
|
|
|
|
// discriminant cannot be encoded fall back to the dummy location.
|
2024-11-04 11:38:14 -08:00
|
|
|
unsafe { llvm::LLVMRustDILocationCloneWithBaseDiscriminator(loc, *o.get()) }
|
2025-01-17 09:44:09 -08:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
cx.dbg_loc(callsite_scope, parent_scope.inlined_at, DUMMY_SP)
|
|
|
|
|
})
|
2024-11-04 11:38:14 -08:00
|
|
|
}
|
|
|
|
|
Entry::Vacant(v) => {
|
|
|
|
|
v.insert(0);
|
2025-01-17 09:44:09 -08:00
|
|
|
loc
|
2024-11-04 11:38:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-17 09:44:09 -08:00
|
|
|
});
|
2020-02-11 01:27:33 +02:00
|
|
|
|
2025-01-17 09:44:09 -08:00
|
|
|
debug_context.scopes[scope] = DebugScope {
|
|
|
|
|
dbg_scope,
|
|
|
|
|
inlined_at: inlined_at.or(parent_scope.inlined_at),
|
|
|
|
|
file_start_pos: loc.file.start_pos,
|
|
|
|
|
file_end_pos: loc.file.end_position(),
|
|
|
|
|
};
|
2022-04-26 21:00:19 +08:00
|
|
|
instantiated.insert(scope);
|
2016-04-07 22:35:11 +03:00
|
|
|
}
|