Skip to content

Commit

Permalink
Auto merge of rust-lang#105121 - oli-obk:simpler-cheaper-dump_mir, r=…
Browse files Browse the repository at this point in the history
…nnethercote

Cheaper `dump_mir` take two

alternative to rust-lang#105083

r? `@nnethercote`
  • Loading branch information
bors committed Dec 4, 2022
2 parents 1195b67 + c7e94b0 commit fd02567
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 110 deletions.
10 changes: 4 additions & 6 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) fn replace_regions_in_mir<'tcx>(
// Replace all remaining regions with fresh inference variables.
renumber::renumber_mir(infcx, body, promoted);

dump_mir(infcx.tcx, None, "renumber", &0, body, |_, _| Ok(()));
dump_mir(infcx.tcx, false, "renumber", &0, body, |_, _| Ok(()));

universal_regions
}
Expand Down Expand Up @@ -331,7 +331,7 @@ pub(super) fn dump_mir_results<'tcx>(
return;
}

dump_mir(infcx.tcx, None, "nll", &0, body, |pass_where, out| {
dump_mir(infcx.tcx, false, "nll", &0, body, |pass_where, out| {
match pass_where {
// Before the CFG, dump out the values for each region variable.
PassWhere::BeforeCFG => {
Expand All @@ -358,15 +358,13 @@ pub(super) fn dump_mir_results<'tcx>(

// Also dump the inference graph constraints as a graphviz file.
let _: io::Result<()> = try {
let mut file =
create_dump_file(infcx.tcx, "regioncx.all.dot", None, "nll", &0, body.source)?;
let mut file = create_dump_file(infcx.tcx, "regioncx.all.dot", false, "nll", &0, body)?;
regioncx.dump_graphviz_raw_constraints(&mut file)?;
};

// Also dump the inference graph constraints as a graphviz file.
let _: io::Result<()> = try {
let mut file =
create_dump_file(infcx.tcx, "regioncx.scc.dot", None, "nll", &0, body.source)?;
let mut file = create_dump_file(infcx.tcx, "regioncx.scc.dot", false, "nll", &0, body)?;
regioncx.dump_graphviz_scc_constraints(&mut file)?;
};
}
Expand Down
41 changes: 4 additions & 37 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,9 @@ impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
/// pass will be named after the type, and it will consist of a main
/// loop that goes over each available MIR and applies `run_pass`.
pub trait MirPass<'tcx> {
fn name(&self) -> Cow<'_, str> {
fn name(&self) -> &str {
let name = std::any::type_name::<Self>();
if let Some(tail) = name.rfind(':') {
Cow::from(&name[tail + 1..])
} else {
Cow::from(name)
}
if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }
}

/// Returns `true` if this pass is enabled with the current combination of compiler flags.
Expand Down Expand Up @@ -182,35 +178,6 @@ impl RuntimePhase {
}
}

impl Display for MirPhase {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
MirPhase::Built => write!(f, "built"),
MirPhase::Analysis(p) => write!(f, "analysis-{}", p),
MirPhase::Runtime(p) => write!(f, "runtime-{}", p),
}
}
}

impl Display for AnalysisPhase {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
AnalysisPhase::Initial => write!(f, "initial"),
AnalysisPhase::PostCleanup => write!(f, "post_cleanup"),
}
}
}

impl Display for RuntimePhase {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
RuntimePhase::Initial => write!(f, "initial"),
RuntimePhase::PostCleanup => write!(f, "post_cleanup"),
RuntimePhase::Optimized => write!(f, "optimized"),
}
}
}

/// Where a specific `mir::Body` comes from.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
Expand Down Expand Up @@ -368,7 +335,7 @@ impl<'tcx> Body<'tcx> {

let mut body = Body {
phase: MirPhase::Built,
pass_count: 1,
pass_count: 0,
source,
basic_blocks: BasicBlocks::new(basic_blocks),
source_scopes,
Expand Down Expand Up @@ -403,7 +370,7 @@ impl<'tcx> Body<'tcx> {
pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
let mut body = Body {
phase: MirPhase::Built,
pass_count: 1,
pass_count: 0,
source: MirSource::item(CRATE_DEF_ID.to_def_id()),
basic_blocks: BasicBlocks::new(basic_blocks),
source_scopes: IndexVec::new(),
Expand Down
32 changes: 15 additions & 17 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rustc_middle::mir::interpret::{
Pointer, Provenance,
};
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::MirSource;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
use rustc_target::abi::Size;
Expand Down Expand Up @@ -74,7 +73,7 @@ pub enum PassWhere {
#[inline]
pub fn dump_mir<'tcx, F>(
tcx: TyCtxt<'tcx>,
pass_num: Option<&dyn Display>,
pass_num: bool,
pass_name: &str,
disambiguator: &dyn Display,
body: &Body<'tcx>,
Expand Down Expand Up @@ -111,7 +110,7 @@ pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, def_id: DefId) ->

fn dump_matched_mir_node<'tcx, F>(
tcx: TyCtxt<'tcx>,
pass_num: Option<&dyn Display>,
pass_num: bool,
pass_name: &str,
disambiguator: &dyn Display,
body: &Body<'tcx>,
Expand All @@ -120,8 +119,7 @@ fn dump_matched_mir_node<'tcx, F>(
F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
{
let _: io::Result<()> = try {
let mut file =
create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body.source)?;
let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body)?;
// see notes on #41697 above
let def_path =
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
Expand All @@ -143,16 +141,14 @@ fn dump_matched_mir_node<'tcx, F>(

if tcx.sess.opts.unstable_opts.dump_mir_graphviz {
let _: io::Result<()> = try {
let mut file =
create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, body.source)?;
let mut file = create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, body)?;
write_mir_fn_graphviz(tcx, body, false, &mut file)?;
};
}

if let Some(spanview) = tcx.sess.opts.unstable_opts.dump_mir_spanview {
let _: io::Result<()> = try {
let file_basename =
dump_file_basename(tcx, pass_num, pass_name, disambiguator, body.source);
let file_basename = dump_file_basename(tcx, pass_num, pass_name, disambiguator, body);
let mut file = create_dump_file_with_basename(tcx, &file_basename, "html")?;
if body.source.def_id().is_local() {
write_mir_fn_spanview(tcx, body, spanview, &file_basename, &mut file)?;
Expand All @@ -165,11 +161,12 @@ fn dump_matched_mir_node<'tcx, F>(
/// where we should dump a MIR representation output files.
fn dump_file_basename<'tcx>(
tcx: TyCtxt<'tcx>,
pass_num: Option<&dyn Display>,
pass_num: bool,
pass_name: &str,
disambiguator: &dyn Display,
source: MirSource<'tcx>,
body: &Body<'tcx>,
) -> String {
let source = body.source;
let promotion_id = match source.promoted {
Some(id) => format!("-{:?}", id),
None => String::new(),
Expand All @@ -178,9 +175,10 @@ fn dump_file_basename<'tcx>(
let pass_num = if tcx.sess.opts.unstable_opts.dump_mir_exclude_pass_number {
String::new()
} else {
match pass_num {
None => ".-------".to_string(),
Some(pass_num) => format!(".{}", pass_num),
if pass_num {
format!(".{:03}-{:03}", body.phase.phase_index(), body.pass_count)
} else {
".-------".to_string()
}
};

Expand Down Expand Up @@ -250,14 +248,14 @@ fn create_dump_file_with_basename(
pub fn create_dump_file<'tcx>(
tcx: TyCtxt<'tcx>,
extension: &str,
pass_num: Option<&dyn Display>,
pass_num: bool,
pass_name: &str,
disambiguator: &dyn Display,
source: MirSource<'tcx>,
body: &Body<'tcx>,
) -> io::Result<io::BufWriter<fs::File>> {
create_dump_file_with_basename(
tcx,
&dump_file_basename(tcx, pass_num, pass_name, disambiguator, source),
&dump_file_basename(tcx, pass_num, pass_name, disambiguator, body),
extension,
)
}
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ pub enum MirPhase {
Runtime(RuntimePhase),
}

impl MirPhase {
pub fn name(&self) -> &'static str {
match *self {
MirPhase::Built => "built",
MirPhase::Analysis(AnalysisPhase::Initial) => "analysis",
MirPhase::Analysis(AnalysisPhase::PostCleanup) => "analysis-post-cleanup",
MirPhase::Runtime(RuntimePhase::Initial) => "runtime",
MirPhase::Runtime(RuntimePhase::PostCleanup) => "runtime-post-cleanup",
MirPhase::Runtime(RuntimePhase::Optimized) => "runtime-optimized",
}
}
}

/// See [`MirPhase::Analysis`].
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(HashStable)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/custom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(super) fn build_custom_mir<'tcx>(
is_polymorphic: false,
tainted_by_errors: None,
injection_phase: None,
pass_count: 1,
pass_count: 0,
};

body.local_decls.push(LocalDecl::new(return_ty, return_ty_span));
Expand Down
9 changes: 1 addition & 8 deletions compiler/rustc_mir_dataflow/src/framework/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,7 @@ where
None if tcx.sess.opts.unstable_opts.dump_mir_dataflow
&& dump_enabled(tcx, A::NAME, def_id) =>
{
create_dump_file(
tcx,
".dot",
None,
A::NAME,
&pass_name.unwrap_or("-----"),
body.source,
)?
create_dump_file(tcx, ".dot", false, A::NAME, &pass_name.unwrap_or("-----"), body)?
}

_ => return Ok(()),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ pub(super) fn dump_coverage_spanview<'tcx>(
let def_id = mir_source.def_id();

let span_viewables = span_viewables(tcx, mir_body, basic_coverage_blocks, &coverage_spans);
let mut file = create_dump_file(tcx, "html", None, pass_name, &0, mir_source)
let mut file = create_dump_file(tcx, "html", false, pass_name, &0, mir_body)
.expect("Unexpected error creating MIR spanview HTML file");
let crate_name = tcx.crate_name(def_id.krate);
let item_name = tcx.def_path(def_id).to_filename_friendly_no_crate();
Expand Down Expand Up @@ -739,7 +739,7 @@ pub(super) fn dump_coverage_graphviz<'tcx>(
.join("\n ")
));
}
let mut file = create_dump_file(tcx, "dot", None, pass_name, &0, mir_source)
let mut file = create_dump_file(tcx, "dot", false, pass_name, &0, mir_body)
.expect("Unexpected error creating BasicCoverageBlock graphviz DOT file");
graphviz_writer
.write_graphviz(tcx, &mut file)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/dest_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ fn dest_prop_mir_dump<'body, 'tcx>(
round: usize,
) {
let mut reachable = None;
dump_mir(tcx, None, "DestinationPropagation-dataflow", &round, body, |pass_where, w| {
dump_mir(tcx, false, "DestinationPropagation-dataflow", &round, body, |pass_where, w| {
let reachable = reachable.get_or_insert_with(|| traversal::reachable_as_bitset(body));

match pass_where {
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_mir_transform/src/dump_mir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! This pass just dumps MIR at a specified point.
use std::borrow::Cow;
use std::fs::File;
use std::io;

Expand All @@ -13,8 +12,8 @@ use rustc_session::config::{OutputFilenames, OutputType};
pub struct Marker(pub &'static str);

impl<'tcx> MirPass<'tcx> for Marker {
fn name(&self) -> Cow<'_, str> {
Cow::Borrowed(self.0)
fn name(&self) -> &str {
self.0
}

fn run_pass(&self, _tcx: TyCtxt<'tcx>, _body: &mut Body<'tcx>) {}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_transform/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ fn create_generator_drop_shim<'tcx>(
// unrelated code from the resume part of the function
simplify::remove_dead_blocks(tcx, &mut body);

dump_mir(tcx, None, "generator_drop", &0, &body, |_, _| Ok(()));
dump_mir(tcx, false, "generator_drop", &0, &body, |_, _| Ok(()));

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

dump_mir(tcx, None, "generator_resume", &0, body, |_, _| Ok(()));
dump_mir(tcx, false, "generator_resume", &0, body, |_, _| Ok(()));
}

fn insert_clean_drop(body: &mut Body<'_>) -> BasicBlock {
Expand Down Expand Up @@ -1394,14 +1394,14 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
// This is expanded to a drop ladder in `elaborate_generator_drops`.
let drop_clean = insert_clean_drop(body);

dump_mir(tcx, None, "generator_pre-elab", &0, body, |_, _| Ok(()));
dump_mir(tcx, false, "generator_pre-elab", &0, body, |_, _| 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, body);

dump_mir(tcx, None, "generator_post-transform", &0, body, |_, _| Ok(()));
dump_mir(tcx, false, "generator_post-transform", &0, body, |_, _| 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, &transform, gen_ty, body, drop_clean);
Expand Down
Loading

0 comments on commit fd02567

Please sign in to comment.