Skip to content

Commit 84a4633

Browse files
authored
Rollup merge of #70511 - ecstatic-morse:mir-dataflow-graphviz, 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.
2 parents 99009bf + 4d1194c commit 84a4633

File tree

7 files changed

+30
-7
lines changed

7 files changed

+30
-7
lines changed

src/librustc_interface/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ fn test_debugging_options_tracking_hash() {
562562
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
563563
opts.debugging_opts.dump_mir_graphviz = true;
564564
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
565+
opts.debugging_opts.dump_mir_dataflow = true;
566+
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
565567

566568
// Make sure changing a [TRACKED] option changes the hash
567569
opts = reference.clone();

src/librustc_mir/borrow_check/nll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
317317
regioncx: &RegionInferenceContext<'_>,
318318
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
319319
) {
320-
if !mir_util::dump_enabled(infcx.tcx, "nll", source) {
320+
if !mir_util::dump_enabled(infcx.tcx, "nll", source.def_id()) {
321321
return;
322322
}
323323

src/librustc_mir/dataflow/framework/engine.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_span::symbol::{sym, Symbol};
1515

1616
use super::graphviz;
1717
use super::{Analysis, GenKillAnalysis, GenKillSet, Results};
18+
use crate::util::pretty::dump_enabled;
1819

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

403-
// Invalid `rustc_mir` attrs will be reported using `span_err`.
404+
// Invalid `rustc_mir` attrs are reported in `RustcMirAttrs::parse`
404405
Err(()) => return Ok(()),
405406
};
406407

407408
let path = match attrs.output_path(A::NAME) {
408409
Some(path) => path,
410+
411+
None if tcx.sess.opts.debugging_opts.dump_mir_dataflow
412+
&& dump_enabled(tcx, A::NAME, def_id) =>
413+
{
414+
let mut path = PathBuf::from(&tcx.sess.opts.debugging_opts.dump_mir_dir);
415+
416+
let item_name = ty::print::with_forced_impl_filename_line(|| {
417+
tcx.def_path(def_id).to_filename_friendly_no_crate()
418+
});
419+
path.push(format!("rustc.{}.{}.dot", item_name, A::NAME));
420+
path
421+
}
422+
409423
None => return Ok(()),
410424
};
411425

@@ -430,7 +444,12 @@ where
430444

431445
let graphviz = graphviz::Formatter::new(body, def_id, results, &mut *formatter);
432446
dot::render_opts(&graphviz, &mut buf, &[dot::RenderOption::Monospace])?;
447+
448+
if let Some(parent) = path.parent() {
449+
fs::create_dir_all(parent)?;
450+
}
433451
fs::write(&path, buf)?;
452+
434453
Ok(())
435454
}
436455

src/librustc_mir/transform/dump_mir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn on_mir_pass<'tcx>(
4646
body: &Body<'tcx>,
4747
is_after: bool,
4848
) {
49-
if mir_util::dump_enabled(tcx, pass_name, source) {
49+
if mir_util::dump_enabled(tcx, pass_name, source.def_id()) {
5050
mir_util::dump_mir(
5151
tcx,
5252
Some(pass_num),

src/librustc_mir/util/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub fn dump_mir<'tcx>(
265265
body: &Body<'tcx>,
266266
result: &LivenessResult,
267267
) {
268-
if !dump_enabled(tcx, pass_name, source) {
268+
if !dump_enabled(tcx, pass_name, source.def_id()) {
269269
return;
270270
}
271271
let node_path = ty::print::with_forced_impl_filename_line(|| {

src/librustc_mir/util/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ pub fn dump_mir<'tcx, F>(
7878
) where
7979
F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
8080
{
81-
if !dump_enabled(tcx, pass_name, source) {
81+
if !dump_enabled(tcx, pass_name, source.def_id()) {
8282
return;
8383
}
8484

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

88-
pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, source: MirSource<'tcx>) -> bool {
88+
pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, def_id: DefId) -> bool {
8989
let filters = match tcx.sess.opts.debugging_opts.dump_mir {
9090
None => return false,
9191
Some(ref filters) => filters,
9292
};
9393
let node_path = ty::print::with_forced_impl_filename_line(|| {
9494
// see notes on #41697 below
95-
tcx.def_path_str(source.def_id())
95+
tcx.def_path_str(def_id)
9696
});
9797
filters.split('|').any(|or_filter| {
9898
or_filter.split('&').all(|and_filter| {

src/librustc_session/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
835835
"the directory the MIR is dumped into"),
836836
dump_mir_graphviz: bool = (false, parse_bool, [UNTRACKED],
837837
"in addition to `.mir` files, create graphviz `.dot` files"),
838+
dump_mir_dataflow: bool = (false, parse_bool, [UNTRACKED],
839+
"in addition to `.mir` files, create graphviz `.dot` files with dataflow results"),
838840
dump_mir_exclude_pass_number: bool = (false, parse_bool, [UNTRACKED],
839841
"if set, exclude the pass number when dumping MIR (used in tests)"),
840842
mir_emit_retag: bool = (false, parse_bool, [TRACKED],

0 commit comments

Comments
 (0)