Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 3 additions & 30 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 @@ -184,30 +180,7 @@ impl RuntimePhase {

impl Display for MirPhase {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this impl?

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"),
}
f.write_str(self.name())
}
}

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
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
29 changes: 6 additions & 23 deletions compiler/rustc_mir_transform/src/pass_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::borrow::Cow;

use rustc_middle::mir::{self, Body, MirPhase, RuntimePhase};
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
Expand All @@ -8,13 +6,9 @@ use crate::{validate, MirPass};

/// Just like `MirPass`, except it cannot mutate `Body`.
pub trait MirLint<'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 }
}

fn is_enabled(&self, _sess: &Session) -> bool {
Expand All @@ -32,7 +26,7 @@ impl<'tcx, T> MirPass<'tcx> for Lint<T>
where
T: MirLint<'tcx>,
{
fn name(&self) -> Cow<'_, str> {
fn name(&self) -> &str {
self.0.name()
}

Expand All @@ -55,7 +49,7 @@ impl<'tcx, T> MirPass<'tcx> for WithMinOptLevel<T>
where
T: MirPass<'tcx>,
{
fn name(&self) -> Cow<'_, str> {
fn name(&self) -> &str {
self.1.name()
}

Expand Down Expand Up @@ -166,11 +160,9 @@ pub fn dump_mir_for_pass<'tcx>(
pass_name: &str,
is_after: bool,
) {
let phase_index = body.phase.phase_index();

mir::dump_mir(
tcx,
Some(&format_args!("{:03}-{:03}", phase_index, body.pass_count)),
true,
pass_name,
if is_after { &"after" } else { &"before" },
body,
Expand All @@ -179,14 +171,5 @@ pub fn dump_mir_for_pass<'tcx>(
}

pub fn dump_mir_for_phase_change<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let phase_index = body.phase.phase_index();

mir::dump_mir(
tcx,
Some(&format_args!("{:03}-000", phase_index)),
&format!("{}", body.phase),
&"after",
body,
|_, _| Ok(()),
)
mir::dump_mir(tcx, true, body.phase.name(), &"after", body, |_, _| Ok(()))
}
5 changes: 2 additions & 3 deletions compiler/rustc_mir_transform/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Vis
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use smallvec::SmallVec;
use std::borrow::Cow;
use std::convert::TryInto;

pub struct SimplifyCfg {
Expand All @@ -57,8 +56,8 @@ pub fn simplify_cfg<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
}

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

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand Down
Loading