Skip to content

Commit

Permalink
Fix debuginfo so that it points to the correct local
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleywiser committed Jul 3, 2020
1 parent 9248d90 commit 24bfdc9
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 111 deletions.
12 changes: 12 additions & 0 deletions src/librustc_middle/mir/mod.rs
Expand Up @@ -256,6 +256,18 @@ impl<'tcx> Body<'tcx> {
(&mut self.basic_blocks, &mut self.local_decls)
}

#[inline]
pub fn basic_blocks_local_decls_mut_and_var_debug_info(
&mut self,
) -> (
&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
&mut LocalDecls<'tcx>,
&mut Vec<VarDebugInfo<'tcx>>,
) {
self.predecessor_cache.invalidate();
(&mut self.basic_blocks, &mut self.local_decls, &mut self.var_debug_info)
}

/// Returns `true` if a cycle exists in the control-flow graph that is reachable from the
/// `START_BLOCK`.
pub fn is_cfg_cyclic(&self) -> bool {
Expand Down
69 changes: 59 additions & 10 deletions src/librustc_mir/transform/simplify_try.rs
Expand Up @@ -11,10 +11,10 @@

use crate::transform::{simplify, MirPass, MirSource};
use itertools::Itertools as _;
use rustc_index::vec::IndexVec;
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_index::{bit_set::BitSet, vec::IndexVec};
use rustc_middle::mir::visit::{NonUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_middle::ty::{List, Ty, TyCtxt};
use rustc_target::abi::VariantIdx;
use std::iter::{Enumerate, Peekable};
use std::slice::Iter;
Expand Down Expand Up @@ -74,10 +74,19 @@ struct ArmIdentityInfo<'tcx> {

/// The statements that should be removed (turned into nops)
stmts_to_remove: Vec<usize>,

/// Indices of debug variables that need to be adjusted to point to
// `{local_0}.{dbg_projection}`.
dbg_info_to_adjust: Vec<usize>,

/// The projection used to rewrite debug info.
dbg_projection: &'tcx List<PlaceElem<'tcx>>,
}

fn get_arm_identity_info<'a, 'tcx>(
stmts: &'a [Statement<'tcx>],
locals_count: usize,
debug_info: &'a [VarDebugInfo<'tcx>],
) -> Option<ArmIdentityInfo<'tcx>> {
// This can't possibly match unless there are at least 3 statements in the block
// so fail fast on tiny blocks.
Expand Down Expand Up @@ -190,7 +199,7 @@ fn get_arm_identity_info<'a, 'tcx>(
try_eat_storage_stmts(&mut stmt_iter, &mut storage_live_stmts, &mut storage_dead_stmts);

let (get_variant_field_stmt, stmt) = stmt_iter.next()?;
let (local_tmp_s0, local_1, vf_s0) = match_get_variant_field(stmt)?;
let (local_tmp_s0, local_1, vf_s0, dbg_projection) = match_get_variant_field(stmt)?;

try_eat_storage_stmts(&mut stmt_iter, &mut storage_live_stmts, &mut storage_dead_stmts);

Expand Down Expand Up @@ -231,6 +240,19 @@ fn get_arm_identity_info<'a, 'tcx>(
let stmt_to_overwrite =
nop_stmts.iter().find(|stmt_idx| live_idx < **stmt_idx && **stmt_idx < dead_idx);

let mut tmp_assigned_vars = BitSet::new_empty(locals_count);
for (l, r) in &tmp_assigns {
tmp_assigned_vars.insert(*l);
tmp_assigned_vars.insert(*r);
}

let mut dbg_info_to_adjust = Vec::new();
for (i, var_info) in debug_info.iter().enumerate() {
if tmp_assigned_vars.contains(var_info.place.local) {
dbg_info_to_adjust.push(i);
}
}

Some(ArmIdentityInfo {
local_temp_0: local_tmp_s0,
local_1,
Expand All @@ -246,13 +268,16 @@ fn get_arm_identity_info<'a, 'tcx>(
source_info: discr_stmt_source_info,
storage_stmts,
stmts_to_remove: nop_stmts,
dbg_info_to_adjust,
dbg_projection,
})
}

fn optimization_applies<'tcx>(
opt_info: &ArmIdentityInfo<'tcx>,
local_decls: &IndexVec<Local, LocalDecl<'tcx>>,
local_uses: &IndexVec<Local, usize>,
var_debug_info: &[VarDebugInfo<'tcx>],
) -> bool {
trace!("testing if optimization applies...");

Expand Down Expand Up @@ -309,6 +334,15 @@ 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 source_local != opt_info.local_temp_0 {
trace!(
"NO: start of assignment chain does not match enum variant temp: {:?} != {:?}",
Expand Down Expand Up @@ -337,11 +371,14 @@ impl<'tcx> MirPass<'tcx> for SimplifyArmIdentity {

trace!("running SimplifyArmIdentity on {:?}", source);
let local_uses = LocalUseCounter::get_local_uses(body);
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
let (basic_blocks, local_decls, debug_info) =
body.basic_blocks_local_decls_mut_and_var_debug_info();
for bb in basic_blocks {
if let Some(opt_info) = get_arm_identity_info(&bb.statements) {
if let Some(opt_info) =
get_arm_identity_info(&bb.statements, local_decls.len(), debug_info)
{
trace!("got opt_info = {:#?}", opt_info);
if !optimization_applies(&opt_info, local_decls, &local_uses) {
if !optimization_applies(&opt_info, local_decls, &local_uses, &debug_info) {
debug!("optimization skipped for {:?}", source);
continue;
}
Expand Down Expand Up @@ -377,6 +414,14 @@ impl<'tcx> MirPass<'tcx> for SimplifyArmIdentity {

bb.statements.retain(|stmt| stmt.kind != StatementKind::Nop);

// 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;
}

trace!("block is now {:?}", bb.statements);
}
}
Expand All @@ -397,7 +442,9 @@ impl LocalUseCounter {

impl<'tcx> Visitor<'tcx> for LocalUseCounter {
fn visit_local(&mut self, local: &Local, context: PlaceContext, _location: Location) {
if context.is_storage_marker() {
if context.is_storage_marker()
|| context == PlaceContext::NonUse(NonUseContext::VarDebugInfo)
{
return;
}

Expand All @@ -409,13 +456,15 @@ impl<'tcx> Visitor<'tcx> for LocalUseCounter {
/// ```rust
/// _LOCAL_INTO = ((_LOCAL_FROM as Variant).FIELD: TY);
/// ```
fn match_get_variant_field<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, Local, VarField<'tcx>)> {
fn match_get_variant_field<'tcx>(
stmt: &Statement<'tcx>,
) -> Option<(Local, Local, VarField<'tcx>, &'tcx List<PlaceElem<'tcx>>)> {
match &stmt.kind {
StatementKind::Assign(box (place_into, rvalue_from)) => match rvalue_from {
Rvalue::Use(Operand::Copy(pf) | Operand::Move(pf)) => {
let local_into = place_into.as_local()?;
let (local_from, vf) = match_variant_field_place(*pf)?;
Some((local_into, local_from, vf))
Some((local_into, local_from, vf, pf.projection))
}
_ => None,
},
Expand Down
Expand Up @@ -5,12 +5,12 @@
debug x => _1; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:8: 1:9
let mut _0: std::option::Option<std::boxed::Box<()>>; // return place in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:31: 1:46
let mut _2: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
- let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
- let mut _4: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:25: 4:26
- let mut _5: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
- let mut _6: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
scope 1 {
debug x => _3; // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
debug x => ((_0 as Some).0: std::boxed::Box<()>); // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
}

bb0: {
Expand All @@ -19,9 +19,7 @@
}

bb1: {
_3 = move ((_1 as Some).0: std::boxed::Box<()>); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
((_0 as Some).0: std::boxed::Box<()>) = move _3; // scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
discriminant(_0) = 1; // scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
_0 = move _1; // scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
goto -> bb3; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:2:5: 5:6
}

Expand Down
Expand Up @@ -5,12 +5,12 @@
debug x => _1; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:8: 1:9
let mut _0: std::option::Option<std::boxed::Box<()>>; // return place in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:31: 1:46
let mut _2: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
- let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
- let mut _4: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:25: 4:26
- let mut _5: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
- let mut _6: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
scope 1 {
debug x => _3; // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
debug x => ((_0 as Some).0: std::boxed::Box<()>); // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
}

bb0: {
Expand All @@ -19,9 +19,7 @@
}

bb1: {
_3 = move ((_1 as Some).0: std::boxed::Box<()>); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
((_0 as Some).0: std::boxed::Box<()>) = move _3; // scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
discriminant(_0) = 1; // scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
_0 = move _1; // scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
goto -> bb3; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:2:5: 5:6
}

Expand Down
Expand Up @@ -15,22 +15,27 @@
let _10: u32; // in scope 0 at $DIR/simplify_try.rs:6:13: 6:15
let mut _11: u32; // in scope 0 at $DIR/simplify_try.rs:7:8: 7:9
scope 1 {
debug y => _2; // in scope 1 at $DIR/simplify_try.rs:6:9: 6:10
- debug y => _2; // in scope 1 at $DIR/simplify_try.rs:6:9: 6:10
+ debug y => ((_0 as Ok).0: u32); // in scope 1 at $DIR/simplify_try.rs:6:9: 6:10
}
scope 2 {
debug err => _6; // in scope 2 at $DIR/simplify_try.rs:6:14: 6:15
- debug err => _6; // in scope 2 at $DIR/simplify_try.rs:6:14: 6:15
+ debug err => ((_0 as Err).0: i32); // in scope 2 at $DIR/simplify_try.rs:6:14: 6:15
scope 3 {
scope 7 {
debug t => _9; // in scope 7 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
- debug t => _9; // in scope 7 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
+ debug t => ((_0 as Err).0: i32); // in scope 7 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
}
scope 8 {
debug v => _8; // in scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
- debug v => _8; // in scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
+ debug v => ((_0 as Err).0: i32); // in scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
let mut _12: i32; // in scope 8 at $DIR/simplify_try.rs:6:14: 6:15
}
}
}
scope 4 {
debug val => _10; // in scope 4 at $DIR/simplify_try.rs:6:13: 6:15
- debug val => _10; // in scope 4 at $DIR/simplify_try.rs:6:13: 6:15
+ debug val => ((_0 as Ok).0: u32); // in scope 4 at $DIR/simplify_try.rs:6:13: 6:15
scope 5 {
}
}
Expand All @@ -50,35 +55,37 @@
}

bb1: {
StorageLive(_10); // scope 0 at $DIR/simplify_try.rs:6:13: 6:15
_10 = ((_3 as Ok).0: u32); // scope 0 at $DIR/simplify_try.rs:6:13: 6:15
_2 = _10; // scope 5 at $DIR/simplify_try.rs:6:13: 6:15
StorageDead(_10); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
- StorageLive(_10); // scope 0 at $DIR/simplify_try.rs:6:13: 6:15
- _10 = ((_3 as Ok).0: u32); // scope 0 at $DIR/simplify_try.rs:6:13: 6:15
- _2 = _10; // scope 5 at $DIR/simplify_try.rs:6:13: 6:15
- StorageDead(_10); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
+ _0 = move _3; // scope 1 at $DIR/simplify_try.rs:7:5: 7:10
StorageDead(_3); // scope 0 at $DIR/simplify_try.rs:6:15: 6:16
StorageLive(_11); // scope 1 at $DIR/simplify_try.rs:7:8: 7:9
_11 = _2; // scope 1 at $DIR/simplify_try.rs:7:8: 7:9
((_0 as Ok).0: u32) = move _11; // scope 1 at $DIR/simplify_try.rs:7:5: 7:10
discriminant(_0) = 0; // scope 1 at $DIR/simplify_try.rs:7:5: 7:10
StorageDead(_11); // scope 1 at $DIR/simplify_try.rs:7:9: 7:10
- StorageLive(_11); // scope 1 at $DIR/simplify_try.rs:7:8: 7:9
- _11 = _2; // scope 1 at $DIR/simplify_try.rs:7:8: 7:9
- ((_0 as Ok).0: u32) = move _11; // scope 1 at $DIR/simplify_try.rs:7:5: 7:10
- discriminant(_0) = 0; // scope 1 at $DIR/simplify_try.rs:7:5: 7:10
- StorageDead(_11); // scope 1 at $DIR/simplify_try.rs:7:9: 7:10
StorageDead(_2); // scope 0 at $DIR/simplify_try.rs:8:1: 8:2
goto -> bb3; // scope 0 at $DIR/simplify_try.rs:8:2: 8:2
}

bb2: {
StorageLive(_6); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
_6 = ((_3 as Err).0: i32); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
StorageLive(_8); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
StorageLive(_9); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
_9 = _6; // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
_8 = move _9; // scope 7 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
StorageDead(_9); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
StorageLive(_12); // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
_12 = move _8; // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
((_0 as Err).0: i32) = move _12; // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
discriminant(_0) = 1; // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
StorageDead(_12); // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
StorageDead(_8); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
StorageDead(_6); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
- StorageLive(_6); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
- _6 = ((_3 as Err).0: i32); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
- StorageLive(_8); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
- StorageLive(_9); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
- _9 = _6; // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
- _8 = move _9; // scope 7 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
- StorageDead(_9); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
- StorageLive(_12); // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
- _12 = move _8; // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
- ((_0 as Err).0: i32) = move _12; // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
- discriminant(_0) = 1; // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
- StorageDead(_12); // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
- StorageDead(_8); // scope 3 at $DIR/simplify_try.rs:6:14: 6:15
- StorageDead(_6); // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
+ _0 = move _3; // scope 8 at $SRC_DIR/libcore/result.rs:LL:COL
StorageDead(_3); // scope 0 at $DIR/simplify_try.rs:6:15: 6:16
StorageDead(_2); // scope 0 at $DIR/simplify_try.rs:8:1: 8:2
goto -> bb3; // scope 0 at $DIR/simplify_try.rs:6:14: 6:15
Expand Down

0 comments on commit 24bfdc9

Please sign in to comment.