2 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
4 use rustc::mir::visit::Visitor;
5 use rustc::ty::{self, TyCtxt};
6 use rustc::ty::item_path;
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_data_structures::indexed_vec::Idx;
10 use std::fmt::Write as _;
12 use std::io::{self, Write};
13 use std::path::{Path, PathBuf};
14 use super::graphviz::write_mir_fn_graphviz;
15 use transform::MirSource;
17 const INDENT: &str = " ";
18 /// Alignment for lining up comments following MIR statements
19 pub(crate) const ALIGN: usize = 40;
21 /// An indication of where we are in the control flow graph. Used for printing
22 /// extra information in `dump_mir`
24 /// We have not started dumping the control flow graph, but we are about to.
27 /// We just finished dumping the control flow graph. This is right before EOF
30 /// We are about to start dumping the given basic block.
31 BeforeBlock(BasicBlock),
33 /// We are just about to dump the given statement or terminator.
34 BeforeLocation(Location),
36 /// We just dumped the given statement or terminator.
37 AfterLocation(Location),
39 /// We just dumped the terminator for a block but not the closing `}`.
40 AfterTerminator(BasicBlock),
43 /// If the session is properly configured, dumps a human-readable
44 /// representation of the mir into:
47 /// rustc.node<node_id>.<pass_num>.<pass_name>.<disambiguator>
50 /// Output from this function is controlled by passing `-Z dump-mir=<filter>`,
51 /// where `<filter>` takes the following forms:
53 /// - `all` -- dump MIR for all fns, all passes, all everything
54 /// - a filter defined by a set of substrings combined with `&` and `|`
55 /// (`&` has higher precedence). At least one of the `|`-separated groups
56 /// must match; an `|`-separated group matches if all of its `&`-separated
57 /// substrings are matched.
61 /// - `nll` == match if `nll` appears in the name
62 /// - `foo & nll` == match if `foo` and `nll` both appear in the name
63 /// - `foo & nll | typeck` == match if `foo` and `nll` both appear in the name
64 /// or `typeck` appears in the name.
65 /// - `foo & nll | bar & typeck` == match if `foo` and `nll` both appear in the name
66 /// or `typeck` and `bar` both appear in the name.
67 pub fn dump_mir<'a, 'gcx, 'tcx, F>(
68 tcx: TyCtxt<'a, 'gcx, 'tcx>,
69 pass_num: Option<&dyn Display>,
71 disambiguator: &dyn Display,
76 F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
78 if !dump_enabled(tcx, pass_name, source) {
82 let node_path = item_path::with_forced_impl_filename_line(|| {
83 // see notes on #41697 below
84 tcx.item_path_str(source.def_id)
86 dump_matched_mir_node(
98 pub fn dump_enabled<'a, 'gcx, 'tcx>(
99 tcx: TyCtxt<'a, 'gcx, 'tcx>,
103 let filters = match tcx.sess.opts.debugging_opts.dump_mir {
104 None => return false,
105 Some(ref filters) => filters,
107 let node_path = item_path::with_forced_impl_filename_line(|| {
108 // see notes on #41697 below
109 tcx.item_path_str(source.def_id)
111 filters.split('|').any(|or_filter| {
112 or_filter.split('&').all(|and_filter| {
113 and_filter == "all" || pass_name.contains(and_filter) || node_path.contains(and_filter)
118 // #41697 -- we use `with_forced_impl_filename_line()` because
119 // `item_path_str()` would otherwise trigger `type_of`, and this can
120 // run while we are already attempting to evaluate `type_of`.
122 fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(
123 tcx: TyCtxt<'a, 'gcx, 'tcx>,
124 pass_num: Option<&dyn Display>,
127 disambiguator: &dyn Display,
132 F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
134 let _: io::Result<()> = try_block! {
135 let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, source)?;
136 writeln!(file, "// MIR for `{}`", node_path)?;
137 writeln!(file, "// source = {:?}", source)?;
138 writeln!(file, "// pass_name = {}", pass_name)?;
139 writeln!(file, "// disambiguator = {}", disambiguator)?;
140 if let Some(ref layout) = mir.generator_layout {
141 writeln!(file, "// generator_layout = {:?}", layout)?;
144 extra_data(PassWhere::BeforeCFG, &mut file)?;
145 write_user_type_annotations(mir, &mut file)?;
146 write_mir_fn(tcx, source, mir, &mut extra_data, &mut file)?;
147 extra_data(PassWhere::AfterCFG, &mut file)?;
150 if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
151 let _: io::Result<()> = try_block! {
153 create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, source)?;
154 write_mir_fn_graphviz(tcx, source.def_id, mir, &mut file)?;
159 /// Returns the path to the filename where we should dump a given MIR.
160 /// Also used by other bits of code (e.g., NLL inference) that dump
161 /// graphviz data or other things.
163 tcx: TyCtxt<'_, '_, '_>,
165 pass_num: Option<&dyn Display>,
167 disambiguator: &dyn Display,
170 let promotion_id = match source.promoted {
171 Some(id) => format!("-{:?}", id),
172 None => String::new(),
175 let pass_num = if tcx.sess.opts.debugging_opts.dump_mir_exclude_pass_number {
179 None => ".-------".to_string(),
180 Some(pass_num) => format!(".{}", pass_num),
184 let mut file_path = PathBuf::new();
185 file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir));
187 let item_name = tcx.hir()
188 .def_path(source.def_id)
189 .to_filename_friendly_no_crate();
191 let file_name = format!(
192 "rustc.{}{}{}.{}.{}.{}",
201 file_path.push(&file_name);
206 /// Attempts to open a file where we should dump a given MIR or other
207 /// bit of MIR-related data. Used by `mir-dump`, but also by other
208 /// bits of code (e.g., NLL inference) that dump graphviz data or
209 /// other things, and hence takes the extension as an argument.
210 pub(crate) fn create_dump_file(
211 tcx: TyCtxt<'_, '_, '_>,
213 pass_num: Option<&dyn Display>,
215 disambiguator: &dyn Display,
217 ) -> io::Result<fs::File> {
218 let file_path = dump_path(tcx, extension, pass_num, pass_name, disambiguator, source);
219 if let Some(parent) = file_path.parent() {
220 fs::create_dir_all(parent)?;
222 fs::File::create(&file_path)
225 /// Write out a human-readable textual representation for the given MIR.
226 pub fn write_mir_pretty<'a, 'gcx, 'tcx>(
227 tcx: TyCtxt<'a, 'gcx, 'tcx>,
228 single: Option<DefId>,
230 ) -> io::Result<()> {
233 "// WARNING: This output format is intended for human consumers only"
237 "// and is subject to change without notice. Knock yourself out."
240 let mut first = true;
241 for def_id in dump_mir_def_ids(tcx, single) {
242 let mir = &tcx.optimized_mir(def_id);
247 // Put empty lines between all items
251 write_mir_fn(tcx, MirSource::item(def_id), mir, &mut |_, _| Ok(()), w)?;
253 for (i, mir) in mir.promoted.iter_enumerated() {
255 let src = MirSource {
259 write_mir_fn(tcx, src, mir, &mut |_, _| Ok(()), w)?;
265 pub fn write_mir_fn<'a, 'gcx, 'tcx, F>(
266 tcx: TyCtxt<'a, 'gcx, 'tcx>,
273 F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
275 write_mir_intro(tcx, src, mir, w)?;
276 for block in mir.basic_blocks().indices() {
277 extra_data(PassWhere::BeforeBlock(block), w)?;
278 write_basic_block(tcx, block, mir, extra_data, w)?;
279 if block.index() + 1 != mir.basic_blocks().len() {
288 /// Write out a human-readable textual representation for the given basic block.
289 pub fn write_basic_block<'cx, 'gcx, 'tcx, F>(
290 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
297 F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
299 let data = &mir[block];
301 // Basic block label at the top.
302 let cleanup_text = if data.is_cleanup { " // cleanup" } else { "" };
303 let lbl = format!("{}{:?}: {{", INDENT, block);
304 writeln!(w, "{0:1$}{2}", lbl, ALIGN, cleanup_text)?;
306 // List of statements in the middle.
307 let mut current_location = Location {
311 for statement in &data.statements {
312 extra_data(PassWhere::BeforeLocation(current_location), w)?;
313 let indented_mir = format!("{0}{0}{1:?};", INDENT, statement);
319 comment(tcx, statement.source_info),
323 write_extra(tcx, w, |visitor| {
324 visitor.visit_statement(current_location.block, statement, current_location);
327 extra_data(PassWhere::AfterLocation(current_location), w)?;
329 current_location.statement_index += 1;
332 // Terminator at the bottom.
333 extra_data(PassWhere::BeforeLocation(current_location), w)?;
334 let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
340 comment(tcx, data.terminator().source_info),
344 write_extra(tcx, w, |visitor| {
345 visitor.visit_terminator(current_location.block, data.terminator(), current_location);
348 extra_data(PassWhere::AfterLocation(current_location), w)?;
349 extra_data(PassWhere::AfterTerminator(block), w)?;
351 writeln!(w, "{}}}", INDENT)
354 /// After we print the main statement, we sometimes dump extra
355 /// information. There's often a lot of little things "nuzzled up" in
357 fn write_extra<'cx, 'gcx, 'tcx, F>(
358 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
359 write: &mut dyn Write,
363 F: FnMut(&mut ExtraComments<'cx, 'gcx, 'tcx>),
365 let mut extra_comments = ExtraComments {
369 visit_op(&mut extra_comments);
370 for comment in extra_comments.comments {
371 writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
376 struct ExtraComments<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
377 _tcx: TyCtxt<'cx, 'gcx, 'tcx>, // don't need it now, but bet we will soon
378 comments: Vec<String>,
381 impl<'cx, 'gcx, 'tcx> ExtraComments<'cx, 'gcx, 'tcx> {
382 fn push(&mut self, lines: &str) {
383 for line in lines.split('\n') {
384 self.comments.push(line.to_string());
389 impl<'cx, 'gcx, 'tcx> Visitor<'tcx> for ExtraComments<'cx, 'gcx, 'tcx> {
390 fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
391 self.super_constant(constant, location);
392 let Constant { span, ty, user_ty, literal } = constant;
393 self.push("mir::Constant");
394 self.push(&format!("+ span: {:?}", span));
395 self.push(&format!("+ ty: {:?}", ty));
396 if let Some(user_ty) = user_ty {
397 self.push(&format!("+ user_ty: {:?}", user_ty));
399 self.push(&format!("+ literal: {:?}", literal));
402 fn visit_const(&mut self, constant: &&'tcx ty::LazyConst<'tcx>, _: Location) {
403 self.super_const(constant);
405 ty::LazyConst::Evaluated(constant) => {
406 let ty::Const { ty, val, .. } = constant;
407 self.push("ty::Const");
408 self.push(&format!("+ ty: {:?}", ty));
409 self.push(&format!("+ val: {:?}", val));
411 ty::LazyConst::Unevaluated(did, substs) => {
412 self.push("ty::LazyConst::Unevaluated");
413 self.push(&format!("+ did: {:?}", did));
414 self.push(&format!("+ substs: {:?}", substs));
419 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
420 self.super_rvalue(rvalue, location);
422 Rvalue::Aggregate(kind, _) => match **kind {
423 AggregateKind::Closure(def_id, substs) => {
424 self.push("closure");
425 self.push(&format!("+ def_id: {:?}", def_id));
426 self.push(&format!("+ substs: {:#?}", substs));
429 AggregateKind::Generator(def_id, substs, movability) => {
430 self.push("generator");
431 self.push(&format!("+ def_id: {:?}", def_id));
432 self.push(&format!("+ substs: {:#?}", substs));
433 self.push(&format!("+ movability: {:?}", movability));
436 AggregateKind::Adt(_, _, _, Some(user_ty), _) => {
438 self.push(&format!("+ user_ty: {:?}", user_ty));
449 fn comment(tcx: TyCtxt, SourceInfo { span, scope }: SourceInfo) -> String {
453 tcx.sess.source_map().span_to_string(span)
457 /// Prints user-defined variables in a scope tree.
459 /// Returns the total number of variables printed.
463 scope_tree: &FxHashMap<SourceScope, Vec<SourceScope>>,
467 ) -> io::Result<()> {
468 let indent = depth * INDENT.len();
470 let children = match scope_tree.get(&parent) {
471 Some(children) => children,
472 None => return Ok(()),
475 for &child in children {
476 let data = &mir.source_scopes[child];
477 assert_eq!(data.parent_scope, Some(parent));
478 writeln!(w, "{0:1$}scope {2} {{", "", indent, child.index())?;
480 // User variable types (including the user's name in a comment).
481 for local in mir.vars_iter() {
482 let var = &mir.local_decls[local];
483 let (name, source_info) = if var.source_info.scope == child {
484 (var.name.unwrap(), var.source_info)
486 // Not a variable or not declared in this scope.
490 let mut_str = if var.mutability == Mutability::Mut {
496 let indent = indent + INDENT.len();
497 let mut indented_var = format!(
498 "{0:1$}let {2}{3:?}: {4:?}",
505 for user_ty in var.user_ty.projections() {
506 write!(indented_var, " as {:?}", user_ty).unwrap();
508 indented_var.push_str(";");
511 "{0:1$} // \"{2}\" in {3}",
515 comment(tcx, source_info)
519 write_scope_tree(tcx, mir, scope_tree, w, child, depth + 1)?;
521 writeln!(w, "{0:1$}}}", "", depth * INDENT.len())?;
527 /// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
528 /// local variables (both user-defined bindings and compiler temporaries).
529 pub fn write_mir_intro<'a, 'gcx, 'tcx>(
530 tcx: TyCtxt<'a, 'gcx, 'tcx>,
534 ) -> io::Result<()> {
535 write_mir_sig(tcx, src, mir, w)?;
538 // construct a scope tree and write it out
539 let mut scope_tree: FxHashMap<SourceScope, Vec<SourceScope>> = Default::default();
540 for (index, scope_data) in mir.source_scopes.iter().enumerate() {
541 if let Some(parent) = scope_data.parent_scope {
545 .push(SourceScope::new(index));
547 // Only the argument scope has no parent, because it's the root.
548 assert_eq!(index, OUTERMOST_SOURCE_SCOPE.index());
552 // Print return place
553 let indented_retptr = format!("{}let mut {:?}: {};",
556 mir.local_decls[RETURN_PLACE].ty);
557 writeln!(w, "{0:1$} // return place",
561 write_scope_tree(tcx, mir, &scope_tree, w, OUTERMOST_SOURCE_SCOPE, 1)?;
563 write_temp_decls(mir, w)?;
565 // Add an empty line before the first block is printed.
571 fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut dyn Write) -> io::Result<()> {
572 let id = tcx.hir().as_local_node_id(src.def_id).unwrap();
573 let body_owner_kind = tcx.hir().body_owner_kind(id);
574 match (body_owner_kind, src.promoted) {
575 (_, Some(i)) => write!(w, "{:?} in", i)?,
576 (hir::BodyOwnerKind::Closure, _) |
577 (hir::BodyOwnerKind::Fn, _) => write!(w, "fn")?,
578 (hir::BodyOwnerKind::Const, _) => write!(w, "const")?,
579 (hir::BodyOwnerKind::Static(hir::MutImmutable), _) => write!(w, "static")?,
580 (hir::BodyOwnerKind::Static(hir::MutMutable), _) => write!(w, "static mut")?,
583 item_path::with_forced_impl_filename_line(|| {
584 // see notes on #41697 elsewhere
585 write!(w, " {}", tcx.item_path_str(src.def_id))
588 match (body_owner_kind, src.promoted) {
589 (hir::BodyOwnerKind::Closure, None) |
590 (hir::BodyOwnerKind::Fn, None) => {
593 // fn argument types.
594 for (i, arg) in mir.args_iter().enumerate() {
598 write!(w, "{:?}: {}", Place::Local(arg), mir.local_decls[arg].ty)?;
601 write!(w, ") -> {}", mir.return_ty())?;
603 (hir::BodyOwnerKind::Const, _) | (hir::BodyOwnerKind::Static(_), _) | (_, Some(_)) => {
604 assert_eq!(mir.arg_count, 0);
605 write!(w, ": {} =", mir.return_ty())?;
609 if let Some(yield_ty) = mir.yield_ty {
611 writeln!(w, "yields {}", yield_ty)?;
617 fn write_temp_decls(mir: &Mir, w: &mut dyn Write) -> io::Result<()> {
618 // Compiler-introduced temporary types.
619 for temp in mir.temps_iter() {
624 if mir.local_decls[temp].mutability == Mutability::Mut {"mut "} else {""},
626 mir.local_decls[temp].ty
633 fn write_user_type_annotations(mir: &Mir, w: &mut dyn Write) -> io::Result<()> {
634 if !mir.user_type_annotations.is_empty() {
635 writeln!(w, "| User Type Annotations")?;
637 for (index, annotation) in mir.user_type_annotations.iter_enumerated() {
638 writeln!(w, "| {:?}: {:?} at {:?}", index.index(), annotation.user_ty, annotation.span)?;
640 if !mir.user_type_annotations.is_empty() {
646 pub fn dump_mir_def_ids(tcx: TyCtxt, single: Option<DefId>) -> Vec<DefId> {
647 if let Some(i) = single {
650 tcx.mir_keys(LOCAL_CRATE).iter().cloned().collect()