Skip to content

Commit

Permalink
Rollup merge of rust-lang#70511 - ecstatic-morse:mir-dataflow-graphvi…
Browse files Browse the repository at this point in the history
…z, r=davidtwco

Add `-Z dump-mir-dataflow` flag for dumping dataflow results visualization

Previously, to visualize the results of a MIR dataflow pass, one had to add a `#[rustc_mir(borrowck_graphviz_postflow)]` attribute to functions of interest. However, there is no way to specify this attribute on closures and generators, so it was impossible to view results for these MIR bodies.

This PR adds a flag, `-Z dump-mir-dataflow`, which will output the dataflow results for any functions specified in `-Z dump-mir` to the output directory specified by `-Z dump-mir-dir`. This behavior is modeled on the `-Z dump-mir-graphviz` flag.
  • Loading branch information
Centril authored Apr 1, 2020
2 parents 1a87c49 + 4d1194c commit 23a7fd4
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/librustc_interface/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ fn test_debugging_options_tracking_hash() {
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.dump_mir_graphviz = true;
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.dump_mir_dataflow = true;
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());

// Make sure changing a [TRACKED] option changes the hash
opts = reference.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
regioncx: &RegionInferenceContext<'_>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
) {
if !mir_util::dump_enabled(infcx.tcx, "nll", source) {
if !mir_util::dump_enabled(infcx.tcx, "nll", source.def_id()) {
return;
}

Expand Down
21 changes: 20 additions & 1 deletion src/librustc_mir/dataflow/framework/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc_span::symbol::{sym, Symbol};

use super::graphviz;
use super::{Analysis, GenKillAnalysis, GenKillSet, Results};
use crate::util::pretty::dump_enabled;

/// A solver for dataflow problems.
pub struct Engine<'a, 'tcx, A>
Expand Down Expand Up @@ -400,12 +401,25 @@ where
let attrs = match RustcMirAttrs::parse(tcx, def_id) {
Ok(attrs) => attrs,

// Invalid `rustc_mir` attrs will be reported using `span_err`.
// Invalid `rustc_mir` attrs are reported in `RustcMirAttrs::parse`
Err(()) => return Ok(()),
};

let path = match attrs.output_path(A::NAME) {
Some(path) => path,

None if tcx.sess.opts.debugging_opts.dump_mir_dataflow
&& dump_enabled(tcx, A::NAME, def_id) =>
{
let mut path = PathBuf::from(&tcx.sess.opts.debugging_opts.dump_mir_dir);

let item_name = ty::print::with_forced_impl_filename_line(|| {
tcx.def_path(def_id).to_filename_friendly_no_crate()
});
path.push(format!("rustc.{}.{}.dot", item_name, A::NAME));
path
}

None => return Ok(()),
};

Expand All @@ -430,7 +444,12 @@ where

let graphviz = graphviz::Formatter::new(body, def_id, results, &mut *formatter);
dot::render_opts(&graphviz, &mut buf, &[dot::RenderOption::Monospace])?;

if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&path, buf)?;

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn on_mir_pass<'tcx>(
body: &Body<'tcx>,
is_after: bool,
) {
if mir_util::dump_enabled(tcx, pass_name, source) {
if mir_util::dump_enabled(tcx, pass_name, source.def_id()) {
mir_util::dump_mir(
tcx,
Some(pass_num),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/util/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub fn dump_mir<'tcx>(
body: &Body<'tcx>,
result: &LivenessResult,
) {
if !dump_enabled(tcx, pass_name, source) {
if !dump_enabled(tcx, pass_name, source.def_id()) {
return;
}
let node_path = ty::print::with_forced_impl_filename_line(|| {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ pub fn dump_mir<'tcx, F>(
) where
F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
{
if !dump_enabled(tcx, pass_name, source) {
if !dump_enabled(tcx, pass_name, source.def_id()) {
return;
}

dump_matched_mir_node(tcx, pass_num, pass_name, disambiguator, source, body, extra_data);
}

pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, source: MirSource<'tcx>) -> bool {
pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, def_id: DefId) -> bool {
let filters = match tcx.sess.opts.debugging_opts.dump_mir {
None => return false,
Some(ref filters) => filters,
};
let node_path = ty::print::with_forced_impl_filename_line(|| {
// see notes on #41697 below
tcx.def_path_str(source.def_id())
tcx.def_path_str(def_id)
});
filters.split('|').any(|or_filter| {
or_filter.split('&').all(|and_filter| {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_session/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"the directory the MIR is dumped into"),
dump_mir_graphviz: bool = (false, parse_bool, [UNTRACKED],
"in addition to `.mir` files, create graphviz `.dot` files"),
dump_mir_dataflow: bool = (false, parse_bool, [UNTRACKED],
"in addition to `.mir` files, create graphviz `.dot` files with dataflow results"),
dump_mir_exclude_pass_number: bool = (false, parse_bool, [UNTRACKED],
"if set, exclude the pass number when dumping MIR (used in tests)"),
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
Expand Down

0 comments on commit 23a7fd4

Please sign in to comment.