Skip to content

Commit 3828489

Browse files
committed
Auto merge of rust-lang#77581 - ecstatic-morse:dataflow-dump-mir-graphviz, r=davidtwco
Use `pretty::create_dump_file` for dumping dataflow results The old code wasn't incorporating promoteds into the path, meaning other `dot` files could get clobbered. Use the MIR dump infrastructure to generate paths so that this doesn't occur in the future.
2 parents f1dab24 + 3b87398 commit 3828489

File tree

1 file changed

+24
-24
lines changed
  • compiler/rustc_mir/src/dataflow/framework

1 file changed

+24
-24
lines changed

compiler/rustc_mir/src/dataflow/framework/engine.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::borrow::BorrowMut;
44
use std::ffi::OsString;
5-
use std::fs;
65
use std::path::PathBuf;
76

87
use rustc_ast as ast;
@@ -12,7 +11,7 @@ use rustc_hir::def_id::DefId;
1211
use rustc_index::bit_set::BitSet;
1312
use rustc_index::vec::{Idx, IndexVec};
1413
use rustc_middle::mir::{self, traversal, BasicBlock};
15-
use rustc_middle::ty::{self, TyCtxt};
14+
use rustc_middle::ty::TyCtxt;
1615
use rustc_span::symbol::{sym, Symbol};
1716

1817
use super::fmt::DebugWithContext;
@@ -21,7 +20,7 @@ use super::{
2120
visit_results, Analysis, Direction, GenKill, GenKillAnalysis, GenKillSet, JoinSemiLattice,
2221
ResultsCursor, ResultsVisitor,
2322
};
24-
use crate::util::pretty::dump_enabled;
23+
use crate::util::pretty::{create_dump_file, dump_enabled};
2524

2625
/// A dataflow analysis that has converged to fixpoint.
2726
pub struct Results<'tcx, A>
@@ -249,7 +248,7 @@ where
249248

250249
let res = write_graphviz_results(tcx, &body, &results, pass_name);
251250
if let Err(e) = res {
252-
warn!("Failed to write graphviz dataflow results: {}", e);
251+
error!("Failed to write graphviz dataflow results: {}", e);
253252
}
254253

255254
results
@@ -270,6 +269,9 @@ where
270269
A: Analysis<'tcx>,
271270
A::Domain: DebugWithContext<A>,
272271
{
272+
use std::fs;
273+
use std::io::{self, Write};
274+
273275
let def_id = body.source.def_id();
274276
let attrs = match RustcMirAttrs::parse(tcx, def_id) {
275277
Ok(attrs) => attrs,
@@ -278,35 +280,36 @@ where
278280
Err(()) => return Ok(()),
279281
};
280282

281-
let path = match attrs.output_path(A::NAME) {
282-
Some(path) => path,
283+
let mut file = match attrs.output_path(A::NAME) {
284+
Some(path) => {
285+
debug!("printing dataflow results for {:?} to {}", def_id, path.display());
286+
if let Some(parent) = path.parent() {
287+
fs::create_dir_all(parent)?;
288+
}
289+
io::BufWriter::new(fs::File::create(&path)?)
290+
}
283291

284292
None if tcx.sess.opts.debugging_opts.dump_mir_dataflow
285293
&& dump_enabled(tcx, A::NAME, def_id) =>
286294
{
287-
// FIXME: Use some variant of `pretty::dump_path` for this
288-
let mut path = PathBuf::from(&tcx.sess.opts.debugging_opts.dump_mir_dir);
289-
290-
let crate_name = tcx.crate_name(def_id.krate);
291-
let item_name = ty::print::with_forced_impl_filename_line(|| {
292-
tcx.def_path(def_id).to_filename_friendly_no_crate()
293-
});
294-
295-
let pass_name = pass_name.map(|s| format!(".{}", s)).unwrap_or_default();
296-
297-
path.push(format!("{}.{}.{}{}.dot", crate_name, item_name, A::NAME, pass_name));
298-
path
295+
create_dump_file(
296+
tcx,
297+
".dot",
298+
None,
299+
A::NAME,
300+
&pass_name.unwrap_or("-----"),
301+
body.source,
302+
)?
299303
}
300304

301-
None => return Ok(()),
305+
_ => return Ok(()),
302306
};
303307

304308
let style = match attrs.formatter {
305309
Some(sym::two_phase) => graphviz::OutputStyle::BeforeAndAfter,
306310
_ => graphviz::OutputStyle::AfterOnly,
307311
};
308312

309-
debug!("printing dataflow results for {:?} to {}", def_id, path.display());
310313
let mut buf = Vec::new();
311314

312315
let graphviz = graphviz::Formatter::new(body, results, style);
@@ -317,10 +320,7 @@ where
317320
}
318321
dot::render_opts(&graphviz, &mut buf, &render_opts)?;
319322

320-
if let Some(parent) = path.parent() {
321-
fs::create_dir_all(parent)?;
322-
}
323-
fs::write(&path, buf)?;
323+
file.write_all(&buf)?;
324324

325325
Ok(())
326326
}

0 commit comments

Comments
 (0)