1
Fork 0

Auto merge of #73210 - wesleywiser:consts_in_debuginfo, r=oli-obk

[mir-opt] Allow debuginfo to be generated for a constant or a Place

Prior to this commit, debuginfo was always generated by mapping a name
to a Place. This has the side-effect that `SimplifyLocals` cannot remove
locals that are only used for debuginfo because their other uses have
been const-propagated.

To allow these locals to be removed, we now allow debuginfo to point to
a constant value. The `ConstProp` pass detects when debuginfo points to
a local with a known constant value and replaces it with the value. This
allows the later `SimplifyLocals` pass to remove the local.
This commit is contained in:
bors 2020-12-15 08:46:00 +00:00
commit e99a89c7c0
16 changed files with 398 additions and 79 deletions

View file

@ -12,7 +12,7 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
use rustc_middle::mir::{
traversal, Body, ClearCrossCrate, Local, Location, Mutability, Operand, Place, PlaceElem,
PlaceRef,
PlaceRef, VarDebugInfoContents,
};
use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
@ -135,19 +135,21 @@ fn do_mir_borrowck<'a, 'tcx>(
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
for var_debug_info in &input_body.var_debug_info {
if let Some(local) = var_debug_info.place.as_local() {
if let Some(prev_name) = local_names[local] {
if var_debug_info.name != prev_name {
span_bug!(
var_debug_info.source_info.span,
"local {:?} has many names (`{}` vs `{}`)",
local,
prev_name,
var_debug_info.name
);
if let VarDebugInfoContents::Place(place) = var_debug_info.value {
if let Some(local) = place.as_local() {
if let Some(prev_name) = local_names[local] {
if var_debug_info.name != prev_name {
span_bug!(
var_debug_info.source_info.span,
"local {:?} has many names (`{}` vs `{}`)",
local,
prev_name,
var_debug_info.name
);
}
}
local_names[local] = Some(var_debug_info.name);
}
local_names[local] = Some(var_debug_info.name);
}
}

View file

@ -0,0 +1,102 @@
//! Finds locals which are assigned once to a const and unused except for debuginfo and converts
//! their debuginfo to use the const directly, allowing the local to be removed.
use rustc_middle::{
mir::{
visit::{PlaceContext, Visitor},
Body, Constant, Local, Location, Operand, Rvalue, StatementKind, VarDebugInfoContents,
},
ty::TyCtxt,
};
use crate::transform::MirPass;
use rustc_index::{bit_set::BitSet, vec::IndexVec};
pub struct ConstDebugInfo;
impl<'tcx> MirPass<'tcx> for ConstDebugInfo {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if !tcx.sess.opts.debugging_opts.unsound_mir_opts {
return;
}
trace!("running ConstDebugInfo on {:?}", body.source);
for (local, constant) in find_optimization_oportunities(body) {
for debuginfo in &mut body.var_debug_info {
if let VarDebugInfoContents::Place(p) = debuginfo.value {
if p.local == local && p.projection.is_empty() {
trace!(
"changing debug info for {:?} from place {:?} to constant {:?}",
debuginfo.name,
p,
constant
);
debuginfo.value = VarDebugInfoContents::Const(constant);
}
}
}
}
}
}
struct LocalUseVisitor {
local_mutating_uses: IndexVec<Local, u8>,
local_assignment_locations: IndexVec<Local, Option<Location>>,
}
fn find_optimization_oportunities<'tcx>(body: &Body<'tcx>) -> Vec<(Local, Constant<'tcx>)> {
let mut visitor = LocalUseVisitor {
local_mutating_uses: IndexVec::from_elem(0, &body.local_decls),
local_assignment_locations: IndexVec::from_elem(None, &body.local_decls),
};
visitor.visit_body(body);
let mut locals_to_debuginfo = BitSet::new_empty(body.local_decls.len());
for debuginfo in &body.var_debug_info {
if let VarDebugInfoContents::Place(p) = debuginfo.value {
if let Some(l) = p.as_local() {
locals_to_debuginfo.insert(l);
}
}
}
let mut eligable_locals = Vec::new();
for (local, mutating_uses) in visitor.local_mutating_uses.drain_enumerated(..) {
if mutating_uses != 1 || !locals_to_debuginfo.contains(local) {
continue;
}
if let Some(location) = visitor.local_assignment_locations[local] {
let bb = &body[location.block];
// The value is assigned as the result of a call, not a constant
if bb.statements.len() == location.statement_index {
continue;
}
if let StatementKind::Assign(box (p, Rvalue::Use(Operand::Constant(box c)))) =
&bb.statements[location.statement_index].kind
{
if let Some(local) = p.as_local() {
eligable_locals.push((local, *c));
}
}
}
}
eligable_locals
}
impl<'tcx> Visitor<'tcx> for LocalUseVisitor {
fn visit_local(&mut self, local: &Local, context: PlaceContext, location: Location) {
if context.is_mutating_use() {
self.local_mutating_uses[*local] = self.local_mutating_uses[*local].saturating_add(1);
if context.is_place_assignment() {
self.local_assignment_locations[*local] = Some(location);
}
}
}
}

View file

@ -21,6 +21,7 @@ pub mod check_consts;
pub mod check_packed_ref;
pub mod check_unsafety;
pub mod cleanup_post_borrowck;
pub mod const_debuginfo;
pub mod const_prop;
pub mod coverage;
pub mod deaggregator;
@ -408,6 +409,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&remove_noop_landing_pads::RemoveNoopLandingPads,
&simplify::SimplifyCfg::new("final"),
&nrvo::RenameReturnPlace,
&const_debuginfo::ConstDebugInfo,
&simplify::SimplifyLocals,
&multiple_return_terminators::MultipleReturnTerminators,
];

View file

@ -246,14 +246,19 @@ fn get_arm_identity_info<'a, 'tcx>(
tmp_assigned_vars.insert(*r);
}
let dbg_info_to_adjust: Vec<_> =
debug_info
.iter()
.enumerate()
.filter_map(|(i, var_info)| {
if tmp_assigned_vars.contains(var_info.place.local) { Some(i) } else { None }
})
.collect();
let dbg_info_to_adjust: Vec<_> = debug_info
.iter()
.enumerate()
.filter_map(|(i, var_info)| {
if let VarDebugInfoContents::Place(p) = var_info.value {
if tmp_assigned_vars.contains(p.local) {
return Some(i);
}
}
None
})
.collect();
Some(ArmIdentityInfo {
local_temp_0: local_tmp_s0,
@ -340,9 +345,11 @@ fn optimization_applies<'tcx>(
// Check that debug info only points to full Locals and not projections.
for dbg_idx in &opt_info.dbg_info_to_adjust {
let dbg_info = &var_debug_info[*dbg_idx];
if !dbg_info.place.projection.is_empty() {
trace!("NO: debug info for {:?} had a projection {:?}", dbg_info.name, dbg_info.place);
return false;
if let VarDebugInfoContents::Place(p) = dbg_info.value {
if !p.projection.is_empty() {
trace!("NO: debug info for {:?} had a projection {:?}", dbg_info.name, p);
return false;
}
}
}
@ -423,9 +430,15 @@ impl<'tcx> MirPass<'tcx> for SimplifyArmIdentity {
// Fix the debug info to point to the right local
for dbg_index in opt_info.dbg_info_to_adjust {
let dbg_info = &mut debug_info[dbg_index];
assert!(dbg_info.place.projection.is_empty());
dbg_info.place.local = opt_info.local_0;
dbg_info.place.projection = opt_info.dbg_projection;
assert!(
matches!(dbg_info.value, VarDebugInfoContents::Place(_)),
"value was not a Place"
);
if let VarDebugInfoContents::Place(p) = &mut dbg_info.value {
assert!(p.projection.is_empty());
p.local = opt_info.local_0;
p.projection = opt_info.dbg_projection;
}
}
trace!("block is now {:?}", bb.statements);

View file

@ -220,7 +220,7 @@ fn write_graph_label<'tcx, W: Write>(
w,
r#"debug {} =&gt; {};<br align="left"/>"#,
var_debug_info.name,
escape(&var_debug_info.place)
escape(&var_debug_info.value),
)?;
}

View file

@ -495,7 +495,7 @@ fn write_scope_tree(
let indented_debug_info = format!(
"{0:1$}debug {2} => {3:?};",
INDENT, indent, var_debug_info.name, var_debug_info.place,
INDENT, indent, var_debug_info.name, var_debug_info.value,
);
writeln!(