Skip to content

Commit fd02567

Browse files
committed
Auto merge of rust-lang#105121 - oli-obk:simpler-cheaper-dump_mir, r=nnethercote
Cheaper `dump_mir` take two alternative to rust-lang#105083 r? `@nnethercote`
2 parents 1195b67 + c7e94b0 commit fd02567

File tree

13 files changed

+60
-110
lines changed

13 files changed

+60
-110
lines changed

compiler/rustc_borrowck/src/nll.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(crate) fn replace_regions_in_mir<'tcx>(
7373
// Replace all remaining regions with fresh inference variables.
7474
renumber::renumber_mir(infcx, body, promoted);
7575

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

7878
universal_regions
7979
}
@@ -331,7 +331,7 @@ pub(super) fn dump_mir_results<'tcx>(
331331
return;
332332
}
333333

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

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

366365
// Also dump the inference graph constraints as a graphviz file.
367366
let _: io::Result<()> = try {
368-
let mut file =
369-
create_dump_file(infcx.tcx, "regioncx.scc.dot", None, "nll", &0, body.source)?;
367+
let mut file = create_dump_file(infcx.tcx, "regioncx.scc.dot", false, "nll", &0, body)?;
370368
regioncx.dump_graphviz_scc_constraints(&mut file)?;
371369
};
372370
}

compiler/rustc_middle/src/mir/mod.rs

+4-37
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,9 @@ impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
100100
/// pass will be named after the type, and it will consist of a main
101101
/// loop that goes over each available MIR and applies `run_pass`.
102102
pub trait MirPass<'tcx> {
103-
fn name(&self) -> Cow<'_, str> {
103+
fn name(&self) -> &str {
104104
let name = std::any::type_name::<Self>();
105-
if let Some(tail) = name.rfind(':') {
106-
Cow::from(&name[tail + 1..])
107-
} else {
108-
Cow::from(name)
109-
}
105+
if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }
110106
}
111107

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

185-
impl Display for MirPhase {
186-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
187-
match self {
188-
MirPhase::Built => write!(f, "built"),
189-
MirPhase::Analysis(p) => write!(f, "analysis-{}", p),
190-
MirPhase::Runtime(p) => write!(f, "runtime-{}", p),
191-
}
192-
}
193-
}
194-
195-
impl Display for AnalysisPhase {
196-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
197-
match self {
198-
AnalysisPhase::Initial => write!(f, "initial"),
199-
AnalysisPhase::PostCleanup => write!(f, "post_cleanup"),
200-
}
201-
}
202-
}
203-
204-
impl Display for RuntimePhase {
205-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
206-
match self {
207-
RuntimePhase::Initial => write!(f, "initial"),
208-
RuntimePhase::PostCleanup => write!(f, "post_cleanup"),
209-
RuntimePhase::Optimized => write!(f, "optimized"),
210-
}
211-
}
212-
}
213-
214181
/// Where a specific `mir::Body` comes from.
215182
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
216183
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
@@ -368,7 +335,7 @@ impl<'tcx> Body<'tcx> {
368335

369336
let mut body = Body {
370337
phase: MirPhase::Built,
371-
pass_count: 1,
338+
pass_count: 0,
372339
source,
373340
basic_blocks: BasicBlocks::new(basic_blocks),
374341
source_scopes,
@@ -403,7 +370,7 @@ impl<'tcx> Body<'tcx> {
403370
pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
404371
let mut body = Body {
405372
phase: MirPhase::Built,
406-
pass_count: 1,
373+
pass_count: 0,
407374
source: MirSource::item(CRATE_DEF_ID.to_def_id()),
408375
basic_blocks: BasicBlocks::new(basic_blocks),
409376
source_scopes: IndexVec::new(),

compiler/rustc_middle/src/mir/pretty.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_middle::mir::interpret::{
1616
Pointer, Provenance,
1717
};
1818
use rustc_middle::mir::visit::Visitor;
19-
use rustc_middle::mir::MirSource;
2019
use rustc_middle::mir::*;
2120
use rustc_middle::ty::{self, TyCtxt};
2221
use rustc_target::abi::Size;
@@ -74,7 +73,7 @@ pub enum PassWhere {
7473
#[inline]
7574
pub fn dump_mir<'tcx, F>(
7675
tcx: TyCtxt<'tcx>,
77-
pass_num: Option<&dyn Display>,
76+
pass_num: bool,
7877
pass_name: &str,
7978
disambiguator: &dyn Display,
8079
body: &Body<'tcx>,
@@ -111,7 +110,7 @@ pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, def_id: DefId) ->
111110

112111
fn dump_matched_mir_node<'tcx, F>(
113112
tcx: TyCtxt<'tcx>,
114-
pass_num: Option<&dyn Display>,
113+
pass_num: bool,
115114
pass_name: &str,
116115
disambiguator: &dyn Display,
117116
body: &Body<'tcx>,
@@ -120,8 +119,7 @@ fn dump_matched_mir_node<'tcx, F>(
120119
F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
121120
{
122121
let _: io::Result<()> = try {
123-
let mut file =
124-
create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body.source)?;
122+
let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body)?;
125123
// see notes on #41697 above
126124
let def_path =
127125
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
@@ -143,16 +141,14 @@ fn dump_matched_mir_node<'tcx, F>(
143141

144142
if tcx.sess.opts.unstable_opts.dump_mir_graphviz {
145143
let _: io::Result<()> = try {
146-
let mut file =
147-
create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, body.source)?;
144+
let mut file = create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, body)?;
148145
write_mir_fn_graphviz(tcx, body, false, &mut file)?;
149146
};
150147
}
151148

152149
if let Some(spanview) = tcx.sess.opts.unstable_opts.dump_mir_spanview {
153150
let _: io::Result<()> = try {
154-
let file_basename =
155-
dump_file_basename(tcx, pass_num, pass_name, disambiguator, body.source);
151+
let file_basename = dump_file_basename(tcx, pass_num, pass_name, disambiguator, body);
156152
let mut file = create_dump_file_with_basename(tcx, &file_basename, "html")?;
157153
if body.source.def_id().is_local() {
158154
write_mir_fn_spanview(tcx, body, spanview, &file_basename, &mut file)?;
@@ -165,11 +161,12 @@ fn dump_matched_mir_node<'tcx, F>(
165161
/// where we should dump a MIR representation output files.
166162
fn dump_file_basename<'tcx>(
167163
tcx: TyCtxt<'tcx>,
168-
pass_num: Option<&dyn Display>,
164+
pass_num: bool,
169165
pass_name: &str,
170166
disambiguator: &dyn Display,
171-
source: MirSource<'tcx>,
167+
body: &Body<'tcx>,
172168
) -> String {
169+
let source = body.source;
173170
let promotion_id = match source.promoted {
174171
Some(id) => format!("-{:?}", id),
175172
None => String::new(),
@@ -178,9 +175,10 @@ fn dump_file_basename<'tcx>(
178175
let pass_num = if tcx.sess.opts.unstable_opts.dump_mir_exclude_pass_number {
179176
String::new()
180177
} else {
181-
match pass_num {
182-
None => ".-------".to_string(),
183-
Some(pass_num) => format!(".{}", pass_num),
178+
if pass_num {
179+
format!(".{:03}-{:03}", body.phase.phase_index(), body.pass_count)
180+
} else {
181+
".-------".to_string()
184182
}
185183
};
186184

@@ -250,14 +248,14 @@ fn create_dump_file_with_basename(
250248
pub fn create_dump_file<'tcx>(
251249
tcx: TyCtxt<'tcx>,
252250
extension: &str,
253-
pass_num: Option<&dyn Display>,
251+
pass_num: bool,
254252
pass_name: &str,
255253
disambiguator: &dyn Display,
256-
source: MirSource<'tcx>,
254+
body: &Body<'tcx>,
257255
) -> io::Result<io::BufWriter<fs::File>> {
258256
create_dump_file_with_basename(
259257
tcx,
260-
&dump_file_basename(tcx, pass_num, pass_name, disambiguator, source),
258+
&dump_file_basename(tcx, pass_num, pass_name, disambiguator, body),
261259
extension,
262260
)
263261
}

compiler/rustc_middle/src/mir/syntax.rs

+13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ pub enum MirPhase {
8989
Runtime(RuntimePhase),
9090
}
9191

92+
impl MirPhase {
93+
pub fn name(&self) -> &'static str {
94+
match *self {
95+
MirPhase::Built => "built",
96+
MirPhase::Analysis(AnalysisPhase::Initial) => "analysis",
97+
MirPhase::Analysis(AnalysisPhase::PostCleanup) => "analysis-post-cleanup",
98+
MirPhase::Runtime(RuntimePhase::Initial) => "runtime",
99+
MirPhase::Runtime(RuntimePhase::PostCleanup) => "runtime-post-cleanup",
100+
MirPhase::Runtime(RuntimePhase::Optimized) => "runtime-optimized",
101+
}
102+
}
103+
}
104+
92105
/// See [`MirPhase::Analysis`].
93106
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)]
94107
#[derive(HashStable)]

compiler/rustc_mir_build/src/build/custom/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(super) fn build_custom_mir<'tcx>(
5757
is_polymorphic: false,
5858
tainted_by_errors: None,
5959
injection_phase: None,
60-
pass_count: 1,
60+
pass_count: 0,
6161
};
6262

6363
body.local_decls.push(LocalDecl::new(return_ty, return_ty_span));

compiler/rustc_mir_dataflow/src/framework/engine.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,7 @@ where
294294
None if tcx.sess.opts.unstable_opts.dump_mir_dataflow
295295
&& dump_enabled(tcx, A::NAME, def_id) =>
296296
{
297-
create_dump_file(
298-
tcx,
299-
".dot",
300-
None,
301-
A::NAME,
302-
&pass_name.unwrap_or("-----"),
303-
body.source,
304-
)?
297+
create_dump_file(tcx, ".dot", false, A::NAME, &pass_name.unwrap_or("-----"), body)?
305298
}
306299

307300
_ => return Ok(()),

compiler/rustc_mir_transform/src/coverage/debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ pub(super) fn dump_coverage_spanview<'tcx>(
638638
let def_id = mir_source.def_id();
639639

640640
let span_viewables = span_viewables(tcx, mir_body, basic_coverage_blocks, &coverage_spans);
641-
let mut file = create_dump_file(tcx, "html", None, pass_name, &0, mir_source)
641+
let mut file = create_dump_file(tcx, "html", false, pass_name, &0, mir_body)
642642
.expect("Unexpected error creating MIR spanview HTML file");
643643
let crate_name = tcx.crate_name(def_id.krate);
644644
let item_name = tcx.def_path(def_id).to_filename_friendly_no_crate();
@@ -739,7 +739,7 @@ pub(super) fn dump_coverage_graphviz<'tcx>(
739739
.join("\n ")
740740
));
741741
}
742-
let mut file = create_dump_file(tcx, "dot", None, pass_name, &0, mir_source)
742+
let mut file = create_dump_file(tcx, "dot", false, pass_name, &0, mir_body)
743743
.expect("Unexpected error creating BasicCoverageBlock graphviz DOT file");
744744
graphviz_writer
745745
.write_graphviz(tcx, &mut file)

compiler/rustc_mir_transform/src/dest_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ fn dest_prop_mir_dump<'body, 'tcx>(
787787
round: usize,
788788
) {
789789
let mut reachable = None;
790-
dump_mir(tcx, None, "DestinationPropagation-dataflow", &round, body, |pass_where, w| {
790+
dump_mir(tcx, false, "DestinationPropagation-dataflow", &round, body, |pass_where, w| {
791791
let reachable = reachable.get_or_insert_with(|| traversal::reachable_as_bitset(body));
792792

793793
match pass_where {

compiler/rustc_mir_transform/src/dump_mir.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! This pass just dumps MIR at a specified point.
22
3-
use std::borrow::Cow;
43
use std::fs::File;
54
use std::io;
65

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

1514
impl<'tcx> MirPass<'tcx> for Marker {
16-
fn name(&self) -> Cow<'_, str> {
17-
Cow::Borrowed(self.0)
15+
fn name(&self) -> &str {
16+
self.0
1817
}
1918

2019
fn run_pass(&self, _tcx: TyCtxt<'tcx>, _body: &mut Body<'tcx>) {}

compiler/rustc_mir_transform/src/generator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ fn create_generator_drop_shim<'tcx>(
10001000
// unrelated code from the resume part of the function
10011001
simplify::remove_dead_blocks(tcx, &mut body);
10021002

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

10051005
body
10061006
}
@@ -1171,7 +1171,7 @@ fn create_generator_resume_function<'tcx>(
11711171
// unrelated code from the drop part of the function
11721172
simplify::remove_dead_blocks(tcx, body);
11731173

1174-
dump_mir(tcx, None, "generator_resume", &0, body, |_, _| Ok(()));
1174+
dump_mir(tcx, false, "generator_resume", &0, body, |_, _| Ok(()));
11751175
}
11761176

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

1397-
dump_mir(tcx, None, "generator_pre-elab", &0, body, |_, _| Ok(()));
1397+
dump_mir(tcx, false, "generator_pre-elab", &0, body, |_, _| Ok(()));
13981398

13991399
// Expand `drop(generator_struct)` to a drop ladder which destroys upvars.
14001400
// If any upvars are moved out of, drop elaboration will handle upvar destruction.
14011401
// However we need to also elaborate the code generated by `insert_clean_drop`.
14021402
elaborate_generator_drops(tcx, body);
14031403

1404-
dump_mir(tcx, None, "generator_post-transform", &0, body, |_, _| Ok(()));
1404+
dump_mir(tcx, false, "generator_post-transform", &0, body, |_, _| Ok(()));
14051405

14061406
// Create a copy of our MIR and use it to create the drop shim for the generator
14071407
let drop_shim = create_generator_drop_shim(tcx, &transform, gen_ty, body, drop_clean);

0 commit comments

Comments
 (0)