Skip to content

Introduce debuginfo to statements in MIR #142771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let opt_assignment_rhs_span =
self.find_assignments(local).first().map(|&location| {
if let Some(mir::Statement {
source_info: _,
kind:
mir::StatementKind::Assign(box (
_,
Expand Down
45 changes: 42 additions & 3 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,10 @@ pub struct BasicBlockData<'tcx> {
/// List of statements in this block.
pub statements: Vec<Statement<'tcx>>,

/// All debuginfos happen before the statement.
/// Put debuginfos here when the last statement is eliminated.
pub after_last_stmt_debuginfos: StmtDebugInfos<'tcx>,

/// Terminator for this block.
///
/// N.B., this should generally ONLY be `None` during construction.
Expand Down Expand Up @@ -1369,7 +1373,12 @@ impl<'tcx> BasicBlockData<'tcx> {
terminator: Option<Terminator<'tcx>>,
is_cleanup: bool,
) -> BasicBlockData<'tcx> {
BasicBlockData { statements, terminator, is_cleanup }
BasicBlockData {
statements,
after_last_stmt_debuginfos: StmtDebugInfos::default(),
terminator,
is_cleanup,
}
}

/// Accessor for terminator.
Expand Down Expand Up @@ -1404,6 +1413,36 @@ impl<'tcx> BasicBlockData<'tcx> {
self.terminator().successors()
}
}

pub fn retain_statements<F>(&mut self, mut f: F)
where
F: FnMut(&Statement<'tcx>) -> bool,
{
// Place debuginfos into the next retained statement,
// this `debuginfos` variable is used to cache debuginfos between two retained statements.
let mut debuginfos = StmtDebugInfos::default();
self.statements.retain_mut(|stmt| {
let retain = f(stmt);
if retain {
stmt.debuginfos.prepend(&mut debuginfos);
} else {
debuginfos.append(&mut stmt.debuginfos);
}
retain
});
self.after_last_stmt_debuginfos.prepend(&mut debuginfos);
}

pub fn strip_nops(&mut self) {
self.retain_statements(|stmt| !matches!(stmt.kind, StatementKind::Nop))
}

pub fn drop_debuginfo(&mut self) {
self.after_last_stmt_debuginfos.drop_debuginfo();
for stmt in self.statements.iter_mut() {
stmt.debuginfos.drop_debuginfo();
}
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1708,10 +1747,10 @@ mod size_asserts {

use super::*;
// tidy-alphabetical-start
static_assert_size!(BasicBlockData<'_>, 128);
static_assert_size!(BasicBlockData<'_>, 152);
static_assert_size!(LocalDecl<'_>, 40);
static_assert_size!(SourceScopeData<'_>, 64);
static_assert_size!(Statement<'_>, 32);
static_assert_size!(Statement<'_>, 56);
static_assert_size!(Terminator<'_>, 96);
static_assert_size!(VarDebugInfo<'_>, 88);
// tidy-alphabetical-end
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,9 @@ where
let mut current_location = Location { block, statement_index: 0 };
for statement in &data.statements {
extra_data(PassWhere::BeforeLocation(current_location), w)?;
for debuginfo in statement.debuginfos.iter() {
writeln!(w, "{INDENT}{INDENT}// DBG: {debuginfo:?};")?;
}
let indented_body = format!("{INDENT}{INDENT}{statement:?};");
if options.include_extra_comments {
writeln!(
Expand Down Expand Up @@ -775,6 +778,9 @@ where

// Terminator at the bottom.
extra_data(PassWhere::BeforeLocation(current_location), w)?;
for debuginfo in data.after_last_stmt_debuginfos.iter() {
writeln!(w, "{INDENT}{INDENT}// DBG: {debuginfo:?};")?;
}
if data.terminator.is_some() {
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
if options.include_extra_comments {
Expand Down Expand Up @@ -854,6 +860,16 @@ impl Debug for Statement<'_> {
}
}

impl Debug for StmtDebugInfo<'_> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
match self {
StmtDebugInfo::AssignRef(local, place) => {
write!(fmt, "{local:?} = &{place:?}")
}
}
}
}

impl Display for NonDivergingIntrinsic<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
103 changes: 100 additions & 3 deletions compiler/rustc_middle/src/mir/statement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Functionality for statements, operands, places, and things that appear in them.

use std::ops;

use tracing::{debug, instrument};

use super::interpret::GlobalAlloc;
Expand All @@ -15,17 +17,34 @@ use crate::ty::CoroutineArgsExt;
pub struct Statement<'tcx> {
pub source_info: SourceInfo,
pub kind: StatementKind<'tcx>,
/// Some debuginfos appearing before the primary statement.
pub debuginfos: StmtDebugInfos<'tcx>,
}

impl<'tcx> Statement<'tcx> {
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
/// invalidating statement indices in `Location`s.
pub fn make_nop(&mut self) {
self.kind = StatementKind::Nop
pub fn make_nop(&mut self, drop_debuginfo: bool) {
if matches!(self.kind, StatementKind::Nop) {
return;
}
let replaced_stmt = std::mem::replace(&mut self.kind, StatementKind::Nop);
if !drop_debuginfo {
match replaced_stmt {
StatementKind::Assign(box (place, Rvalue::Ref(_, _, ref_place)))
if let Some(local) = place.as_local() =>
{
self.debuginfos.push(StmtDebugInfo::AssignRef(local, ref_place));
}
_ => {
bug!("debuginfo is not yet supported.")
}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder: should we attempt to keep debuginfo in most cases, and only drop statements that we do not know how to convert? I mean, consider drop_debuginfo to be always false, and replace the bug! by a no-op?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to do this after we've reviewed most of the MIR statements.


pub fn new(source_info: SourceInfo, kind: StatementKind<'tcx>) -> Self {
Statement { source_info, kind }
Statement { source_info, kind, debuginfos: StmtDebugInfos::default() }
}
}

Expand Down Expand Up @@ -63,6 +82,17 @@ impl<'tcx> StatementKind<'tcx> {
_ => None,
}
}

pub fn as_debuginfo(&self) -> Option<StmtDebugInfo<'tcx>> {
match self {
StatementKind::Assign(box (place, Rvalue::Ref(_, _, ref_place)))
if let Some(local) = place.as_local() =>
{
Some(StmtDebugInfo::AssignRef(local, *ref_place))
}
_ => None,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used in dead_store_elimination.rs and liveness.rs. I used it to get or check debuginfo.

}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -939,3 +969,70 @@ impl RawPtrKind {
}
}
}

#[derive(Default, Debug, Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub struct StmtDebugInfos<'tcx>(Vec<StmtDebugInfo<'tcx>>);

impl<'tcx> StmtDebugInfos<'tcx> {
pub fn push(&mut self, debuginfo: StmtDebugInfo<'tcx>) {
self.0.push(debuginfo);
}

pub fn drop_debuginfo(&mut self) {
self.0.clear();
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

pub fn prepend(&mut self, debuginfos: &mut Self) {
if debuginfos.is_empty() {
return;
};
debuginfos.0.append(self);
std::mem::swap(debuginfos, self);
}

pub fn append(&mut self, debuginfos: &mut Self) {
if debuginfos.is_empty() {
return;
};
self.0.append(debuginfos);
}

pub fn extend(&mut self, debuginfos: &Self) {
if debuginfos.is_empty() {
return;
};
self.0.extend_from_slice(debuginfos);
}

pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&StmtDebugInfo<'tcx>) -> bool,
{
self.0.retain(f);
}
}

impl<'tcx> ops::Deref for StmtDebugInfos<'tcx> {
type Target = Vec<StmtDebugInfo<'tcx>>;

#[inline]
fn deref(&self) -> &Vec<StmtDebugInfo<'tcx>> {
&self.0
}
}

impl<'tcx> ops::DerefMut for StmtDebugInfos<'tcx> {
#[inline]
fn deref_mut(&mut self) -> &mut Vec<StmtDebugInfo<'tcx>> {
&mut self.0
}
}

#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub enum StmtDebugInfo<'tcx> {
AssignRef(Local, Place<'tcx>),
}
40 changes: 38 additions & 2 deletions compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ macro_rules! make_mir_visitor {
self.super_source_scope_data(scope_data);
}

fn visit_statement_debuginfo(
&mut self,
stmt_debuginfo: & $($mutability)? StmtDebugInfo<'tcx>,
location: Location
) {
self.super_statement_debuginfo(stmt_debuginfo, location);
}

fn visit_statement(
&mut self,
statement: & $($mutability)? Statement<'tcx>,
Expand Down Expand Up @@ -301,6 +309,7 @@ macro_rules! make_mir_visitor {
{
let BasicBlockData {
statements,
after_last_stmt_debuginfos,
terminator,
is_cleanup: _
} = data;
Expand All @@ -312,8 +321,11 @@ macro_rules! make_mir_visitor {
index += 1;
}

let location = Location { block, statement_index: index };
for debuginfo in after_last_stmt_debuginfos as & $($mutability)? [_] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in &$(mutability)? after_last_stmt_debuginfos?

self.visit_statement_debuginfo(debuginfo, location);
}
if let Some(terminator) = terminator {
let location = Location { block, statement_index: index };
self.visit_terminator(terminator, location);
}
}
Expand Down Expand Up @@ -376,14 +388,38 @@ macro_rules! make_mir_visitor {
}
}

fn super_statement_debuginfo(
&mut self,
stmt_debuginfo: & $($mutability)? StmtDebugInfo<'tcx>,
location: Location
) {
match stmt_debuginfo {
StmtDebugInfo::AssignRef(local, place) => {
self.visit_local(
$(& $mutability)? *local,
PlaceContext::NonUse(NonUseContext::VarDebugInfo),
location
);
self.visit_place(
place,
PlaceContext::NonUse(NonUseContext::VarDebugInfo),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now have visit_place called with NonUse.
But super_place keeps transforming those into a NonMutatingUse. This also means we can have ProjectionElem::Index called with NonUse.
Should we adapt super_place and super_projection_elem to keep them NonUse?

Copy link
Member Author

@dianqk dianqk Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we only need to adapt super_projection_elem, as super_place is already done. #144998

location
);
},
}
}

fn super_statement(
&mut self,
statement: & $($mutability)? Statement<'tcx>,
location: Location
) {
let Statement { source_info, kind } = statement;
let Statement { source_info, kind, debuginfos } = statement;

self.visit_source_info(source_info);
for debuginfo in debuginfos as & $($mutability)? [_] {
self.visit_statement_debuginfo(debuginfo, location);
}
match kind {
StatementKind::Assign(box (place, rvalue)) => {
self.visit_assign(place, rvalue, location);
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_mir_dataflow/src/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use rustc_middle::mir::*;
/// Return the set of locals that appear in debuginfo.
pub fn debuginfo_locals(body: &Body<'_>) -> DenseBitSet<Local> {
let mut visitor = DebuginfoLocals(DenseBitSet::new_empty(body.local_decls.len()));
for debuginfo in body.var_debug_info.iter() {
visitor.visit_var_debug_info(debuginfo);
}
visitor.visit_body(body);
visitor.0
}

struct DebuginfoLocals(DenseBitSet<Local>);

impl Visitor<'_> for DebuginfoLocals {
fn visit_local(&mut self, local: Local, _: PlaceContext, _: Location) {
self.0.insert(local);
fn visit_local(&mut self, local: Local, place_context: PlaceContext, _: Location) {
if place_context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
self.0.insert(local);
}
}
}
Loading