Skip to content

Extend mir dump to dump each region #45013

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

Merged
merged 10 commits into from
Oct 13, 2017
1 change: 1 addition & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl DepGraph {
for (current_dep_node_index, edges) in current_dep_graph.edges.iter_enumerated() {
let start = edge_list_data.len() as u32;
// This should really just be a memcpy :/
edge_list_data.extend(edges.iter().map(|i| SerializedDepNodeIndex(i.index)));
edge_list_data.extend(edges.iter().map(|i| SerializedDepNodeIndex::new(i.index())));
let end = edge_list_data.len() as u32;

debug_assert_eq!(current_dep_node_index.index(), edge_list_indices.len());
Expand Down
18 changes: 1 addition & 17 deletions src/librustc/dep_graph/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,7 @@ use dep_graph::DepNode;
use ich::Fingerprint;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};

/// The index of a DepNode in the SerializedDepGraph::nodes array.
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Debug,
RustcEncodable, RustcDecodable)]
pub struct SerializedDepNodeIndex(pub u32);

impl Idx for SerializedDepNodeIndex {
#[inline]
fn new(idx: usize) -> Self {
assert!(idx <= ::std::u32::MAX as usize);
SerializedDepNodeIndex(idx as u32)
}

#[inline]
fn index(self) -> usize {
self.0 as usize
}
}
newtype_index!(SerializedDepNodeIndex);

/// Data for use when recompiling the **current crate**.
#[derive(Debug, RustcEncodable, RustcDecodable)]
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(conservative_impl_trait)]
#![feature(const_fn)]
#![feature(core_intrinsics)]
#![feature(i128_type)]
#![cfg_attr(windows, feature(libc))]
Expand Down Expand Up @@ -71,7 +72,7 @@ extern crate graphviz;
extern crate libc;
extern crate owning_ref;
extern crate rustc_back;
extern crate rustc_data_structures;
#[macro_use] extern crate rustc_data_structures;
extern crate serialize;
extern crate rustc_const_math;
extern crate rustc_errors as errors;
Expand Down
24 changes: 0 additions & 24 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,6 @@ pub mod visit;
pub mod transform;
pub mod traversal;

macro_rules! newtype_index {
($name:ident, $debug_name:expr) => (
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
RustcEncodable, RustcDecodable)]
pub struct $name(u32);

impl Idx for $name {
fn new(value: usize) -> Self {
assert!(value < (u32::MAX) as usize);
$name(value as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}

impl Debug for $name {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{}{}", $debug_name, self.0)
}
}
)
}

/// Types for locals
type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;

Expand Down
37 changes: 37 additions & 0 deletions src/librustc_data_structures/indexed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ impl Idx for u32 {
fn index(self) -> usize { self as usize }
}

#[macro_export]
macro_rules! newtype_index {
($name:ident) => (
newtype_index!($name, unsafe { ::std::intrinsics::type_name::<$name>() });
);

($name:ident, $debug_name:expr) => (
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
RustcEncodable, RustcDecodable)]
pub struct $name(u32);

impl $name {
// HACK use for constants
#[allow(unused)]
const fn const_new(x: u32) -> Self {
$name(x)
}
}

impl Idx for $name {
fn new(value: usize) -> Self {
assert!(value < (::std::u32::MAX) as usize);
$name(value as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}

impl ::std::fmt::Debug for $name {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(fmt, "{}{}", $debug_name, self.0)
}
}
)
}

#[derive(Clone, PartialEq, Eq)]
pub struct IndexVec<I: Idx, T> {
pub raw: Vec<T>,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ rustc_const_eval = { path = "../librustc_const_eval" }
rustc_const_math = { path = "../librustc_const_math" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_errors = { path = "../librustc_errors" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
18 changes: 3 additions & 15 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
mem::transmute::<Mir, Mir<'tcx>>(mir)
};

mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );

mir
})
Expand Down Expand Up @@ -227,7 +227,7 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
mem::transmute::<Mir, Mir<'tcx>>(mir)
};

mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );

mir
})
Expand Down Expand Up @@ -311,19 +311,7 @@ struct CFG<'tcx> {
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct ScopeId(u32);

impl Idx for ScopeId {
fn new(index: usize) -> ScopeId {
assert!(index < (u32::MAX as usize));
ScopeId(index as u32)
}

fn index(self) -> usize {
self.0 as usize
}
}
newtype_index!(ScopeId);

///////////////////////////////////////////////////////////////////////////
/// The `BlockAnd` "monad" packages up the new basic block along with a
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!

#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(core_intrinsics)]
#![feature(i128_type)]
#![feature(rustc_diagnostic_macros)]
#![feature(placement_in_syntax)]
Expand All @@ -30,7 +32,8 @@ extern crate bitflags;
extern crate graphviz as dot;
#[macro_use]
extern crate rustc;
extern crate rustc_data_structures;
#[macro_use] extern crate rustc_data_structures;
extern crate serialize as rustc_serialize;
extern crate rustc_errors;
#[macro_use]
extern crate syntax;
Expand Down
13 changes: 12 additions & 1 deletion src/librustc_mir/transform/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ impl PassHook for DumpMir {
pass_name,
&Disambiguator { is_after },
source,
mir);
mir,
|_, _| Ok(()) );
for (index, promoted_mir) in mir.promoted.iter_enumerated() {
let promoted_source = MirSource::Promoted(source.item_id(), index);
mir_util::dump_mir(tcx,
Some((suite, pass_num)),
pass_name,
&Disambiguator { is_after },
promoted_source,
promoted_mir,
|_, _| Ok(()) );
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ fn create_generator_drop_shim<'a, 'tcx>(
// unrelated code from the resume part of the function
simplify::remove_dead_blocks(&mut mir);

dump_mir(tcx, None, "generator_drop", &0, source, &mut mir);
dump_mir(tcx, None, "generator_drop", &0, source, &mut mir, |_, _| Ok(()) );

mir
}
Expand Down Expand Up @@ -673,7 +673,7 @@ fn create_generator_resume_function<'a, 'tcx>(
// unrelated code from the drop part of the function
simplify::remove_dead_blocks(mir);

dump_mir(tcx, None, "generator_resume", &0, source, mir);
dump_mir(tcx, None, "generator_resume", &0, source, mir, |_, _| Ok(()) );
}

fn source_info<'a, 'tcx>(mir: &Mir<'tcx>) -> SourceInfo {
Expand Down Expand Up @@ -816,14 +816,14 @@ impl MirPass for StateTransform {
// This is expanded to a drop ladder in `elaborate_generator_drops`.
let drop_clean = insert_clean_drop(mir);

dump_mir(tcx, None, "generator_pre-elab", &0, source, mir);
dump_mir(tcx, None, "generator_pre-elab", &0, source, mir, |_, _| Ok(()) );

// Expand `drop(generator_struct)` to a drop ladder which destroys upvars.
// If any upvars are moved out of, drop elaboration will handle upvar destruction.
// However we need to also elaborate the code generated by `insert_clean_drop`.
elaborate_generator_drops(tcx, def_id, mir);

dump_mir(tcx, None, "generator_post-transform", &0, source, mir);
dump_mir(tcx, None, "generator_post-transform", &0, source, mir, |_, _| Ok(()) );

// Create a copy of our MIR and use it to create the drop shim for the generator
let drop_shim = create_generator_drop_shim(tcx,
Expand Down
38 changes: 35 additions & 3 deletions src/librustc_mir/transform/nll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ use rustc::mir::{Mir, Location, Rvalue, BasicBlock, Statement, StatementKind};
use rustc::mir::visit::{MutVisitor, Lookup};
use rustc::mir::transform::{MirPass, MirSource};
use rustc::infer::{self, InferCtxt};
use rustc::util::nodemap::FxHashSet;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use syntax_pos::DUMMY_SP;
use std::collections::HashMap;
use std::fmt;

use util as mir_util;
use self::mir_util::PassWhere;

#[allow(dead_code)]
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
lookup_map: HashMap<RegionVid, Lookup>,
regions: IndexVec<RegionIndex, Region>,
infcx: InferCtxt<'a, 'gcx, 'tcx>,
}

Expand All @@ -29,15 +36,17 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
NLLVisitor {
infcx,
lookup_map: HashMap::new(),
regions: IndexVec::new(),
}
}

pub fn into_results(self) -> HashMap<RegionVid, Lookup> {
self.lookup_map
}

fn renumber_regions<T>(&self, value: &T) -> T where T: TypeFoldable<'tcx> {
fn renumber_regions<T>(&mut self, value: &T) -> T where T: TypeFoldable<'tcx> {
self.infcx.tcx.fold_regions(value, &mut false, |_region, _depth| {
self.regions.push(Region::default());
self.infcx.next_region_var(infer::MiscVariable(DUMMY_SP))
})
}
Expand Down Expand Up @@ -129,7 +138,7 @@ pub struct NLL;
impl MirPass for NLL {
fn run_pass<'a, 'tcx>(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
_: MirSource,
source: MirSource,
mir: &mut Mir<'tcx>) {
if !tcx.sess.opts.debugging_opts.nll {
return;
Expand All @@ -140,7 +149,30 @@ impl MirPass for NLL {
let mut renumbered_mir = mir.clone();
let mut visitor = NLLVisitor::new(infcx);
visitor.visit_mir(&mut renumbered_mir);
mir_util::dump_mir(tcx, None, "nll", &0, source, mir, |pass_where, out| {
if let PassWhere::BeforeCFG = pass_where {
for (index, value) in visitor.regions.iter_enumerated() {
writeln!(out, "// R{:03}: {:?}", index.0, value)?;
}
}
Ok(())
});
let _results = visitor.into_results();
})
}
}
}

#[derive(Clone, Default, PartialEq, Eq)]
struct Region {
points: FxHashSet<Location>,
}

impl fmt::Debug for Region {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(formatter, "{:?}", self.points)
}
}



newtype_index!(RegionIndex);
2 changes: 1 addition & 1 deletion src/librustc_mir/util/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn write_mir_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
writeln!(w, "{} {{{}}}", prefix, live.join(", "))
};
print(w, " ", &result.ins)?;
write_basic_block(tcx, block, mir, w)?;
write_basic_block(tcx, block, mir, &mut |_, _| Ok(()), w)?;
print(w, " ", &result.outs)?;
if block.index() + 1 != mir.basic_blocks().len() {
writeln!(w, "")?;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ mod graphviz;
mod pretty;
pub mod liveness;

pub use self::pretty::{dump_enabled, dump_mir, write_mir_pretty};
pub use self::pretty::{dump_enabled, dump_mir, write_mir_pretty, PassWhere};
pub use self::graphviz::{write_mir_graphviz};
pub use self::graphviz::write_node_label as write_graphviz_node_label;
Loading